Pergunta

I am using Tao Framework to work with OpenGl. I`m trying to load a texture and show it on the form. Image seems to be load correctly (texture id is generated and no errors occure) but it is not shown correctly.

Here is my code for loading texture

public static class TextureManager
{
    static TextureManager()
    {
        Il.ilInit();
        Il.ilEnable(Il.IL_ORIGIN_SET);
    }

    public static int LoadTexture(string url)
    {
        int texObject = -1; 

        int imageId;
        Il.ilGenImages(1, out imageId);
        Il.ilBindImage(imageId);

        if (Il.ilLoadImage(url))
        {


            int width = Il.ilGetInteger(Il.IL_IMAGE_WIDTH);
            int height = Il.ilGetInteger(Il.IL_IMAGE_HEIGHT);


            Gl.glGenTextures(1, out texObject);


            Gl.glPixelStorei(Gl.GL_UNPACK_ALIGNMENT, 1);


            Gl.glBindTexture(Gl.GL_TEXTURE_2D, texObject);


            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_REPEAT);
            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_REPEAT);
            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR);
            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);
            Gl.glTexEnvf(Gl.GL_TEXTURE_ENV, Gl.GL_TEXTURE_ENV_MODE, Gl.GL_REPLACE);


            Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGB, width, height, 0, Gl.GL_RGB, Gl.GL_UNSIGNED_BYTE, Il.ilGetData());

            Il.ilDeleteImages(1, ref imageId);
        }
        return texObject;
    }
}

And code for drawing it:

Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
            Gl.glClearColor(255, 255, 255, 1);

            Gl.glLoadIdentity();


            Gl.glEnable(Gl.GL_TEXTURE_2D);

            Gl.glBindTexture(Gl.GL_TEXTURE_2D, mGlTextureObject);


            Gl.glPushMatrix();


            Gl.glTranslated(0, -1, -10);

            Gl.glRotated(rot, 0, 1, 0);


            Gl.glBegin(Gl.GL_QUADS);


            Gl.glNormal3f(0, 0, 1);
            Gl.glTexCoord2f(0, 0); Gl.glVertex3d(0, 0, 0); 

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

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

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



            Gl.glEnd();


            Gl.glPopMatrix();

            Gl.glDisable(Gl.GL_TEXTURE_2D);


            AnT.Invalidate();

The form is just blank. But if I enable lightning then here what is showed:

enter image description here

The image I am loading is a simple bmp image 100x100 created in Paint.

Foi útil?

Solução

The problem appeared because the image was in incorrect size. Size of images should be 64x64, 128x128 or 256x256. Changed the size to 128x128 and everything worked fine.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top