Question

I'm trying build a simple test of OpenGL using Tao.Framework. I need draw a square with a texture. So far I came to this:

private void simpleOpenGlControl1_Paint(object sender, PaintEventArgs e)
{
    simpleOpenGlControl1.InitializeContexts();

    Gl.glClearColor(0f, 0f, 0f, 0f);
    Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
    Gl.glOrtho(-1, 2, -1, 2, 1, -1);

    // Open image
    var fileName = @"C:\mypath\image.bmp";
    var bmp = new Bitmap(fileName);
    var bmpData = bmp.LockBits(
        new Rectangle(0, 0, bmp.Width, bmp.Height),
        ImageLockMode.ReadOnly,
        PixelFormat.Format24bppRgb);

    // Texture
    Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, (int)Gl.GL_RGB8,
        bmp.Width, bmp.Height, 0, Gl.GL_BGR_EXT,
        Gl.GL_UNSIGNED_BYTE, bmpData.Scan0);

    Gl.glBindTexture(Gl.GL_TEXTURE_2D, 0);

    // Draw a square.
    Gl.glBegin(Gl.GL_POLYGON);
        Gl.glVertex2f(0, 0);
        Gl.glVertex2f(0, 1);
        Gl.glVertex2f(1, 1);
        Gl.glVertex2f(1, 0);
    Gl.glEnd();
}

The square is rendered in the expected shape, but without texture. I'm new in OpenGL, even more in Tao.Framework. How can I fix it? Or how is the correct way to add a texture?

EDITED
With help of @j-p I'm try this:

    //...
    Gl.glEnable(Gl.GL_TEXTURE_2D);

    // Draw a square.
    Gl.glBegin(Gl.GL_POLYGON);

    Gl.glTexCoord2f(0, 0); Gl.glVertex2f(0, 0);
    Gl.glTexCoord2f(0, 1); Gl.glVertex2f(0, 1);
    Gl.glTexCoord2f(1, 1); Gl.glVertex2f(1, 1);
    Gl.glTexCoord2f(1, 0); Gl.glVertex2f(1, 0);

    Gl.glEnd();

But the square keep without texture.

Was it helpful?

Solution

you miss at least

GL.Enable(GL.TEXTURE2D);

which is not enabled by default.

Also you have to generate a new texture slot by calling:

int texID;
Gl.glGenTextures(1, texID); //where 1 is the count, and texID a new texture slot in the gpu

then you have to bind that texture slot to which all further operation will be applied.

Gl.glBindTexture(Gl.GL_TEXTURE_2D, texID);

note that calling Gl.glBindTexture(Gl.GL_TEXTURE_2D, 0); disable current texture.

now all further operation are directed to that texture so, now you can pass your bitmap data to that texture slot by calling:

Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, (int)Gl.GL_RGB8,
    bmp.Width, bmp.Height, 0, Gl.GL_BGR_EXT,
    Gl.GL_UNSIGNED_BYTE, bmpData.Scan0);

if you want to set some texture parameters like texGen and clamping, and many other things, you do it here, example:

Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);

Your vertex declaration should also include some texture coordinate or you have to set texture coordinate generation active.

    GL.TexCoord(0,1); Gl.glVertex2f(0, 0);
    GL.TexCoord(0,0); Gl.glVertex2f(0, 1);
    GL.TexCoord(1,0); Gl.glVertex2f(1, 1);
    GL.TexCoord(1,1); Gl.glVertex2f(1, 0);

if you want those coordinate to be automaticaly generated you can omit GL.TexCoord call and enable it via:

Gl.glEnable(Gl.GL_TEXTURE_GEN_S); //enable texture coordinate generation
Gl.glEnable(Gl.GL_TEXTURE_GEN_T);

Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_CLAMP);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_CLAMP);

Don't forget to unlock your bitmap data (even if garbage collector is a must):

bmp.Unlock(bmpData);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top