views:

306

answers:

3

Hey all, I have a very basic texture map problem in GL on iPhone, and I'm wondering what strategies there are for debugging this kind of thing.

(Frankly, just staring at state machine calls and wondering if any of them is wrong or misordered is no way to live-- are there tools for this?)

I have a 512x512 PNG file that I'm loading up from disk (not specially packed), creating a CGBitmapContext, then calling CGContextDrawImage to get bytes out of it. (This code is essentially stolen from an Apple sample.) I'm trying to map the texture to a "quad", with code that looks essentially like this-- all flat 2D stuff, nothing fancy:

glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);    
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);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

GLfloat vertices[8] = {
    viewRect.origin.x, viewRect.size.height,
    viewRect.origin.x, viewRect.origin.y,
    viewRect.size.width, viewRect.origin.y,
    viewRect.size.width, viewRect.size.height        
};    
GLfloat texCoords[8] = {
    0, 1.0,
    0, 0,
    1.0, 0,
    1.0, 1.0
};

glBindTexture(GL_TEXTURE_2D, myTextureRef); // This was previously bound to 
glVertexPointer(2, GL_FLOAT , 0, vertices); 
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);    

glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
  • My supposedly textured area comes out just black.
  • I see no debug output from the CG calls to set up the texture.
  • glGetError reports nothing.
  • If I simplify this code block to just draw the verts, but set up a pure color, the quad area lights up exactly as expected.
  • If I clear the whole context immediately beforehand to red, I don't see the red-- which means something is being rendered there, but not the contents of my PNG.

What could I be doing wrong? And more importantly, what are the right tools and techniques for debugging this sort of thing, because running into this kind of problem and not being able to "step through it" in a debugger in any meaningful way is a bummer.

Thanks!

+2  A: 

glTexParameteri() and friends control per-texture state, not global GL state. Try binding the texture you want to change before you issue them.

As-is myTextureRef is most likely incomplete.

genpfault
+1  A: 

Yes, in general, there are tools. gDEBugger is the standard GL debugger for that kind of things. It's a commercial product.

Alternatives include shadowing the GL state yourself, or try to reverse engineer the implementation you use (in general, this is unlikely to work well, unless your platform provides a debug implementation, but some people had a lot of fun with this).

Or... you could always use Mesa, and just step in.

Bahbar
Thanks for the pointers-- had noticed gDEBugger, and saw it was expensive; couldn't tell if it was the real deal or some fly by night thing. Also, +1 for the video link alone.
quixoto
A: 

What's the result if you just draw the quad without texture and without setting a color? If it's black try using GL_REPLACE instead of GL_MODULATE. – Maurice Gilden Mar 11 at 16:15

This did it. MODULATE wasn't working. REPLACE did. Thanks Maurice.

quixoto