Texture2d and disabled texturing

Hello everyone,

I have a shader that has something like a dozen color/texture pairs. All the textures are optional. I am trying to figure out, how to implement this, without the need to recompile the shader. So I have:

uniform sampler2D diffuseTex;
uniform vec3 diffuseColor;
uniform float diffuseAmount;

vec3 diff = diffuseColor;
diff += texture2D(diffuseTex, gl_TexCoord[0].st).rgb*diffuseAmount;

I don’t want to use #ifdefs for the texture2D lookup. So when there is a texture, I set the diffuseColor to black. I’m wondering, if the opposite variant is working properly - to disable texturing and expect texture2D to return black. Sometimes I get weird results, but can’t tell if this is the cause of the problem or I’ve messed something else :stuck_out_tongue: … Is the texture2D defined when texturing is disabled, i.e.

glActiveTexture(GL_TEXTURE0);
glDisable(GL_TEXTURE_2D);

Or is there a better way to do this?

to disable texturing and expect texture2D to return black.

Sampling from a texture unit to which nothing is bound either returns white or undefined values; I don’t remember which, but black isn’t what you’ll get.

Is the texture2D defined when texturing is disabled, i.e.

glActiveTexture(GL_TEXTURE0);
glDisable(GL_TEXTURE_2D);

That code doesn’t disable texturing. The glEnable/Disable calls for texture types only matters for fixed-function rendering. It means nothing for GLSL. You “disable” texturing by binding nothing to that texture unit.

Or is there a better way to do this?

The possible solutions are:

1: Have multiple shaders.

2: Use #defines

3: Use a uniform to tell whether to sample from the texture or not.

#3 is probably not the best, since you’ll be putting your texture function in non-uniform code. And that won’t work unless you use textureGrad.

Thank you for the quick reply. I’m wandering, if I can just create a black texture and use it when real texturing is not necessary… But this will take a texture slot, so probably not a good idea either…

The spec says:

I assume a non-bound texture counts as not complete, but the spec should really state something about it. I guess you could bind an incomplete texture to follow the spec, or a small (2x2 or so) black texture.

I assume a non-bound texture counts as not complete, but the spec should really state something about it.

The texture 0 represents an actual texture object, the default texture. All textures start out as 1x1 white textures.

Nope, all textures start as 0x0 (incomplete) according to state tables. Unless there is some exception to 0 textures, which i wouldnt be surprised, as they are special anyways.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.