views:

29

answers:

1
glBindTexture(GL_TEXTURE_2D, texidx);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_FLOAT, texdata);

If texidx == 0 everything works perfectly, but if texidx != 0, or is generated using glGenTexture(&texidx,1), the eventual rendering shows just solid color (last glColor) instead of the texture. I've been debugging for over a day (glGetError shows no trouble) and am running out of ideas. Any thoughts? I can post more code as needed.

Setup code:

glEnable (GL_BLEND); 
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 

glDisable(GL_LIGHTING);  

glClearColor(0.5,0.5,0.5,0);
glClearDepth(1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
glDisable(GL_COLOR_MATERIAL);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
+1  A: 

TexID can't be zero, that's the default texture or meaning no texture.

The problem is that the default OpenGL minification filter uses mipmaps, but a common error is not provide them, change the MIN/MAG filters to something usable, like GL_LINEAR or GL_NEAREST, and your texture will work.

More information (and other common errors) are discussed in http://www.opengl.org/wiki/Common_Mistakes#Creating_a_Texture

Matias Valdenegro
Hey Matias, that is a very good point, but in this case is not the problem since I'm using GL_NEAREST. I've added my setup code above to help clarify.As said, strangely TexId=0 is the ONLY case that works correctly for me.
Meekohi
Aha, however I need to set that filter for EACH TEXTURE (duh).The problem is fixed after making sure to run glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); IMMEDIATELY after binding the texture, and BEFORE glTexImage2D(). ::sigh:: wouldn't it be nice if openGL had some reasonable behavior in this case, or at least raised an error?!
Meekohi
see more specifically: http://www.opengl.org/wiki/Common_Mistakes#Creating_a_Texture_.232.2C_glTexEnvi
Meekohi