Add colors on a texture using shaders

First post, yay \o/ :smiley:

Anyway, I couldn’t find a proper solution to my problem, so I’m asking you for help. I wrote a class for displaying text on the screen using anglecode fonts. Now I want also be able to color the text, but when I’m doing the following in my fragment shader, it does not work.

#version 150 core

uniform sampler2D texture_diffuse;

in vec4 pass_Color;
in vec2 pass_TexCoord;

out vec4 out_Color;

void main(void)
{
    out_Color = texture2D(texture_diffuse, pass_TexCoord);
    out_Color = vec4(out_Color.r + 0.5, out_Color.g, out_Color.b, out_Color.a);

    if (out_Color.a < 0.9)
    {
        discard;
    }

}

I expected to see the text slightly red, but it’s still white, as defined in the bitmap. What am I doing wrong here? Do I have to enable GL_BLEND or something first? The code for displaying textures is the same as in the LWJGL Tutorial.

Hmmm…
Is it my or are you supposed to do gl_FragColor somewhere in the fragment shader? I haven’t used any out or in functionality yet :stuck_out_tongue:

so would be gl_FragColor = out_color

I think you are right :slight_smile:

Actually you are both wrong… gl_FragColor is only required in older shaders

If the texture is white wouldn’t the frag already be (1, 1, 1, 1) so adding .5 to r would just make it (1.5, 1, 1, 1) wouldn’t it make more sense instead to minus .5 from g and b to get (1, .5, .5, 1). Im not 100% sure but I think it would work

Using gl_FragColor doesn’t work, it gives me an error like “Unsupported operation”, so The Lion King is right. Didn’t thought of it, of course they’re 1.

Thanks to you guys for your quick help.

Usually, when ‘combining’ colors - or better: tinting an Image with a color, you multiply the color by the color you want, for example:


vec4 texColor = texture2D(texture_diffuse, pass_TexCoord);
out_color = vec4(0.5, 0.0, 0.0, 1.0) * texColor;

That should make the trick. Then you can also more easily change the tinting color and your color components will always be in the range 0.0 to 1.0