0

I am trying to texture triangle with runtime generated texture using SharpGL wrapper.

I can't figure out why triangle remains not textured.

gl.Error() put in draw loop returns 0 which means GL_NO_ERROR.

 private void openGLControl_OpenGLDraw(object sender, OpenGLEventArgs args)
    {

        OpenGL gl = openGLControl.OpenGL;
        gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT );
        gl.LoadIdentity();
        gl.Color(1.0f,1.0f,1.0f,1.0f);
        gl.Begin(OpenGL.GL_TRIANGLES);
        gl.TexCoord(0, 1.0f);
        gl.Vertex(0.0f, 0.0f);
        gl.TexCoord(1.0f, 0f);
        gl.Vertex(1.0f, 0f);
        gl.TexCoord(1.0f, 1.0f);
        gl.Vertex(1.0f, 1.0f);
        gl.End();

    }

    private void openGLControl_OpenGLInitialized(object sender, OpenGLEventArgs args)
    {

        Random rnd = new Random();
        OpenGL gl = openGLControl.OpenGL;
        gl.ClearColor(0, 0, 0, 0);

        gl.Enable(OpenGL.GL_TEXTURE_2D);

        byte[] colors = new byte[256 * 256 * 4];

        for (int i = 0; i < 256 * 256 * 4; i++)
        {
            colors[i] = (byte)rnd.Next(256);
        }

        uint[] textureID = new uint[1];
        gl.GenTextures(1, textureID);
        gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGBA, 256, 256, 0, OpenGL.GL_RGBA, OpenGL.GL_BYTE, colors);
        gl.BindTexture(OpenGL.GL_TEXTURE_2D, textureID[0]);

    }

2 Answers2

0

You have to call gl.BindTexture before calling gl.TexImage2D.

The reason for this is in the first argument of both functions. OpenGL has a state machine that keeps track of what is bound. When you call gl.TexImage2D, you are telling GL to upload the pixels to the texture currently bound to OpenGL.GL_TEXTURE_2D. gl.BindTexture binds the texture you generated to OpenGL.GL_TEXTURE_2D.

Robert Rouhani
  • 14,512
  • 6
  • 44
  • 59
  • Thanks for involvement, I've tryied to call Bind texture before TexImage, with no result there must be something else. – Dawid Zych Oct 11 '16 at 17:17
  • The code you have posted is all valid OpenGL, assuming SharpGL does buffer swapping automatically in the control. Have you tried making the entire texture a solid color for testing? – Robert Rouhani Oct 11 '16 at 17:30
  • Can you see a triangle at all? Is there any other GL code anywhere? – Robert Rouhani Oct 11 '16 at 17:39
  • Yes, I see a white triangle. There is no other GL code apart this two funcitons. Full code: [link](http://pastebin.com/qjseJJuA) When I create and use bitmap instead of byte array everything works fine, but in the future I would like to use 3D Textures, so bitmap is not suitable. – Dawid Zych Oct 11 '16 at 17:45
  • This is more of a minor thing, but in C# a `byte` is unsigned, so perhaps `OpenGL.GL_UNSIGNED_BYTE` would be a better choice instead of just GL_BYTE? – Robert Rouhani Oct 11 '16 at 18:02
  • I've tested that also, no effect, any other ideas? – Dawid Zych Oct 11 '16 at 18:06
  • Try loading the colors array into a Bitmap and saving that out to a file to see if the array has the values you think it does? Your implementation with the Random class seems correct, but it's always good to check. – Robert Rouhani Oct 11 '16 at 18:15
  • I printed byte array to Bitmap in some dummy way: [link](http://pastebin.com/T0XKWZGR) It looks as I want, random noise. Maybe SharpGL just have internal bug? – Dawid Zych Oct 11 '16 at 19:11
0

The reason why texture wasn't visible was lacking setting:

uint[] array = new uint[] { OpenGL.GL_NEAREST };
        gl.TexParameterI(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, array);
        gl.TexParameterI(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, array);

By default SharpGL is using MipMap which weren't generated.