any idea why glBindTexture has no effect?

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_FILTER,GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,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.

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… :slight_smile:

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?

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.

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) :wink:

cheers,
g.

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.

The only way for glBindTexture not to work would be if you called it within a glBegin() glEnd() pair.

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) :wink:
Of course, that’s what I meant :slight_smile: