Check if sampler2D is empty

Here below the 2 versions, with and without if... else... conditional statement. The conditional statement avoids to have to sample the texture if not used.

The uniform int textureSample is set to 1 or 0 for the texture or the color to show up respectively. Both uniform variables are normally set up by the program, not the shader.

uniform int textureSample = 1;
uniform vec3 color = vec3(1.0, 1.0, 0.0);


void main() {     // without if... else...

    // ...
    vec3 materialDiffuseColor = textureSample  * texture( textureSampler, fragmentTexture ).rgb - (textureSample - 1) * color;
    // ...
}


void main() {      // with if... else...

    // ...

if (textureSample == 1) {     // 1 if texture, 0 if color

    vec3 materialDiffuseColor = textureSample  * texture( textureSampler, fragmentTexture ).rgb;
    vec3 materialAmbientColor = vec3(0.5, 0.5, 0.5) * materialDiffuseColor;
    vec3 materialSpecularColor = vec3(0.3, 0.3, 0.3);

    gl_Color = brightness *
        (materialAmbientColor +
        materialDiffuseColor * lightPowerColor * cosTheta / distanceLight2 +
        materialSpecularColor * lightPowerColor * pow(cosAlpha, 10000) / distanceLight2);
}
else {
    vec3 materialDiffuseColor = color;
    vec3 materialAmbientColor = vec3(0.5, 0.5, 0.5) * materialDiffuseColor;
    vec3 materialSpecularColor = vec3(0.3, 0.3, 0.3);

    gl_Color = brightness *
        (materialAmbientColor +
        materialDiffuseColor * lightPowerColor * cosTheta / distanceLight2 +
        materialSpecularColor * lightPowerColor * pow(cosAlpha, 10000) / distanceLight2);
    }

    // ...
}

A sampler cannot be "empty". A valid texture must be bound to the texture units referenced by each sampler in order for rendering to have well-defined behavior.

But that doesn't mean you have to read from the texture that's bound there. It's perfectly valid to use a uniform value to tell the shader whether to read from the texture or not.

But you still have to bind a simple, 1x1 texture there. Indeed, you can use textureSize on the sampler; if it is a 1x1 texture, then don't bother to read from it. Note that this might be slower than using a uniform.

Tags:

Opengl

Glsl