1

Rather than have two separate shaders in my OpenGL code (one for when a texture is bound, one for when none is bound) I usually go for one shader program which handles both. This is my usual fragment shader:

#version 330

uniform bool textured;
uniform sampler2D sampler;

in vec4 fColor;
in vec2 tCoord;

out vec4 final_color;

void main()
{
    vec4 texel = vec4(1.0, 1.0, 1.0, 1.0);
    if (textured)
        texel = texture(sampler, tCoord);
    final_color = fColor * texel;
}

In a modern OpenGL profile, this compiled but did not output anything but black pixels (alternatively in LWJGL legacy profile, it outputted regular colors if textured == false and textured if textured == true). I don't see any reason that this wouldn't work, but what is a method which would work for both textured and untextured fragments?

  • 1
    How is this question any different from [your previous one](http://stackoverflow.com/questions/34467632/glsl-texture-method-in-if-statement-causes-screen-to-be-black)? – Nicol Bolas Dec 26 '15 at 21:28
  • I decided that one was too specific, which was maybe why nobody was helping me. So I deleted it and posted a new and improved one. –  Dec 26 '15 at 21:31
  • @ZachGoethel: I find it very likely that the error is in some other parts of the code which you are not showing. In principle, using a bool for that thing should work. And since this is completely uniform control flow, there should also be no issues with sampling the texture in a branch. – derhass Dec 28 '15 at 19:00
  • My solution is to always have a texture bound, and if I normally wouldn't I bind a 2x2 white texture. –  Dec 28 '15 at 19:02
  • hi there, has anybody found a solution to this? I am having a hard time googling for answers on why my fragment shader ignoring my if statements.. – dude Dec 30 '18 at 08:32

0 Answers0