Question

I'm trying to render an image to the window. Super simple (or so I thought). No matter what I do, I always get this purple square. Some tidbits of my code:

// load image from file, called one time on load:
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);

RgbImage theTexMap( filename );

glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, theTexMap.GetNumCols(), theTexMap.GetNumRows(), 0, GL_RGB, GL_UNSIGNED_BYTE, theTexMap.ImageData() );

// render quad and texture, called inside glutDisplayFunc callback
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-50.0, -50.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-50.0, 50.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(50.0, 50.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(50.0, -50.0, 0.0);
glEnd();

glFlush();
glDisable(GL_TEXTURE_2D);

I'm cutting out a lot of code because I'm attempting to extend an example from third party library (ARToolkit). There's a lot more code in there than needs displaying here.

Any ideas for a mistake I might be making that would display the quad, but not render the texture/image?

alt text

Was it helpful?

Solution

Rebind your texture object in your glutDisplayFunc() callback, Just In Case™.

Also, I'm slightly leery of the GL_RGBA8. Try GL_RGBA. Probably superstition on my part though.

OTHER TIPS

Dunno without trying it myself, but it seems a bit strange that you're using GL_RGBA8 for internal format and GL_RGB for pixel format.

Personally, unless I'm using it on my texture I'll also do a GL_DISABLE(GL_LIGHTING) too for textured objects, dunno if that will help but I know I've run into some things I didn't really understand as far as light & texture combinations are concerned.

A few ideas:

Shouldn't you call glEnable(GL_TEXTURE_2D) before you configure and upload the texture?

Did you make sure that texture dimensions are powers of 2?

If you want to use GL_LINEAR magnification/minification functions, you will probably want to generate mipmaps from your original texture.

did you try to use GL_REPLACE rather than GL_MODULATE ?

I don't see you passing any color in with your vertices, and GL_MODULATE will modulate with whatever is the current color.

Your texture only specifies MIP level 0, and not the other MIP levels, so the result may be undefined if it isn't rendering at MIP level 0. The quick fix is:

glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

It's an easy mistake. A good reference:

http://www.opengl.org/wiki/Common_Mistakes

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top