PDA

View Full Version : any idea why glBindTexture has no effect?



g0l3m
12-19-2006, 12:52 AM
Hello all,

A bit of a wierd thing is happening and I've no idea why. I'm loading a bunch of textures as follows:

glBindTexture(GL_TEXTURE_2D, texBindNum);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image->w, image->h, 0, GL_RGB, GL_UNSIGNED_BYTE, image->pixels);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image->w, image->h, GL_RGB, GL_UNSIGNED_BYTE, image->pixels);

Then, when I want to use a given texture I just:
glBindTexture(GL_TEXTURE_2D, theTex);

NOTE: I don't need to call glEnable(GL_TEXTURE_2D) because I'm doing my texturing from the shaders...

My vert shader looks like:
gl_TexCoord[0] = gl_MultiTexCoord0;
And my frag shader looks like:
vec4 color = texture2D(tex,gl_TexCoord[0].st);
gl_FragColor = color;

where "tex" is a uniform sampler2D.

And now to the problem...
Instead of accesing the texture I've just glBind-ed, everything seems to be textured with the last texture to be loaded...I.e. glBindTexture has no effect. If I change the order of loading for the textures, it's always the last to be loaded that seems to be somehow permanently bound.

Any ideas what I could be doing wrong?

cheers,
g.

k_szczech
12-19-2006, 01:43 AM
You're probably binding your texture to wrong texture image unit or pass wrong sampler2D uniform value to shader.
Or perhaps you're missing glGenTextures?
Or both... :)

g0l3m
12-19-2006, 02:30 AM
If I was missing glGenTextures, it wouldn't texture everything using the last texture loaded...

Also, I'm only using texture unit 0.
I'm assuming since I'm only using tex unit 0, a simple glBindTexture should do, right?
I.e. I don't need to use any of:

glActiveTexture(GL_TEXTURE0);
glClientActiveTexture(GL_TEXTURE0);

etc...right?

Overmind
12-19-2006, 02:59 AM
That should be enough. Of course that's assuming you're setting the sampler uniform value to 0, but it wouldn't work at all if you didn't.

Btw. glTexImage and gluBuild2DMipmaps is redundant. gluBuild2DMipmaps uploads the base level for you. But this shouldn't cause an error.

g0l3m
12-19-2006, 03:08 AM
yes I am...I'm doing:
int sampler2D1 = glGetUniformLocation(mShaderProgram, "tex");
// check sampler2D1 is >= 0
glUniform1i(sampler2D1, 0);

Also, what do you mean glTexImage and gluBuild2dMips is redundant? I'm assuming you mean that if I cann gluBuild2DMips, I don't need to call glTexImage, right? (Not that they are both redundant) ;-)

cheers,
g.

Komat
12-19-2006, 03:12 AM
Originally posted by g0l3m:
If I was missing glGenTextures, it wouldn't texture everything using the last texture loaded...
If because of missing glGenTextures the texBindNum/theTex would end the same for all textures, you will get that affect.

The glGenTextures only assigns unused texture id. Any integer, even if it was not returned by that function, can be used to create texture object so if some constant value (e.g. 0, 0xcccccccc) was left inside the texBindNum/theTex, it will still work.

zeoverlord
12-19-2006, 03:59 AM
The only way for glBindTexture not to work would be if you called it within a glBegin() glEnd() pair.

Overmind
12-19-2006, 04:01 AM
Originally posted by g0l3m:
I'm assuming you mean that if I cann gluBuild2DMips, I don't need to call glTexImage, right? (Not that they are both redundant) ;-)Of course, that's what I meant :-)