Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: texture2d and disabled texturing

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2011
    Posts
    2

    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 ... 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?

  2. #2
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,792

    Re: texture2d and disabled texturing

    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.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2011
    Posts
    2

    Re: texture2d and disabled texturing

    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...

  4. #4
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    393

    Re: texture2d and disabled texturing

    The spec says:
    Quote Originally Posted by OpenGL_4.1_spec
    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.

  5. #5
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,792

    Re: texture2d and disabled texturing

    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.

  6. #6
    Member Regular Contributor
    Join Date
    Apr 2009
    Posts
    258

    Re: texture2d and disabled texturing

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •