PDA

View Full Version : texture2d and disabled texturing



ivalylo
03-29-2011, 04:25 AM
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 :p ... 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?

Alfonse Reinheart
03-29-2011, 04:41 AM
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 (http://www.opengl.org/wiki/GLSL_Samplers#Non-uniform_flow_control). And that won't work unless you use textureGrad.

ivalylo
03-29-2011, 05:10 AM
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...

Dan Bartlett
03-29-2011, 05:28 AM
The spec says:

Using a sampler in a shader will return (R; G;B;A) = (0; 0; 0; 1) if the samplerís
associated texture is not complete, as defined in section 3.8.14.
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.

Alfonse Reinheart
03-29-2011, 12:07 PM
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.

kyle_
03-29-2011, 12:21 PM
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.