views:

89

answers:

5

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

+1  A: 

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.

genpfault
YOU ARE MY NEW BEST FRIEND... Okay, so any ideas of where to properly bind the texture now? Clearly I was binding too early before and something got wiped out, and (I think?) rebinding on every single display callback is also not the right solution... Thoughts?
japollock
@japollock: I generally rebind the texture right before I use it. So in your case right before your `glBegin(GL_QUADS)` call.
genpfault
A: 

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.

Bryan
After reading up on it more, I changed to GL_RGBA and GL_RGB - still didn't fix my problem though. Neither did your lighting superstition. ;)
japollock
A: 

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.

How do I make sure "the texture dimensions are powers of 2" - does that mean that the image pixel width and height need to be a power of 2? Or does this relate to vertex locations?
japollock
power of two textures have not been a requirement in GL for a long time. Which version of GL does your implementation ?
Bahbar
I can tell you GLUT 3.7.6 is included; not sure what version of GL. Does that help?
japollock
The MSDN entry for glTexImage2D states that width and height must be 2^n + 2(border) for some integer n, where (border) is the 6th argument.
A: 

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

grinliz
Neither filter parameter specified mipmapping.
genpfault
Good catch - I missed that. You are correct.
grinliz
A: 

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.

Bahbar
Tried it. Didn't see any change. Any other ideas?
japollock