Skip to main content
Added links to more source.
Source Link

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?

EDIT:

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?

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?

EDIT:

Source Link

Using one GLSL shader program for textured and untextured rendering?

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?