How to load a default colour if the sampler2D was empty

Viewed 283

I want to load a default colour vertColor if the sampler2D wasn't loaded with an image, to achieve that I have tried the following:

Fragment Shader:

#version 410

uniform sampler2D tex0;
in vec4 vertColor;
in vec2 TexCoords;

out vec4 fragColor;

void main( void )
{
     vec4 texColor = texture(tex0, TexCoords);
     if(tex0 == -1)  fragColor = blinnPhongModel(vertColor);
     else{
     texColor.a = 1.0;
     fragColor = texColor;
     }//end of else

} // End of main

but it didn't work, how can I do that?

1 Answers

How to load a default colour if the sampler2D was empty?

Why do you want to do that? I recommend making sure that the sampler is never "empty".

If you want to draw with a uniform color using a sampler, create a 1x1 texture with a single pixel. With this trick, you don't need to branch in the shader.

All you have to do is to create a 1x1 texture. This texture has 1 pixel and 1 color.
The fragment shader does just the texture lookup and has not branch at all:

#version 410

uniform sampler2D tex0;
in vec2 TexCoords;
out vec4 fragColor;

void main( void )
{
     vec4 texColor = texture(tex0, TexCoords);
     fragColor = texColor;
}
Related