user3316633
user3316633

Reputation: 137

GLSL - Set color of vertex

I want to darken the corners of my little quad in my program. I have the following vertex shader:

#version 130

varying vec4 v_color;
varying vec2 v_texcoord;

void main()
{
    v_color = gl_Color.rgba;
    v_texcoord = gl_MultiTexCoord0.xy;
    gl_FrontColor = vec4(v_color.r, v_color.g, v_color.b, 1.0f);
    gl_Position = ftransform(); 
}

And my fragment shader:

#version 130

uniform sampler2D u_texture;

varying vec4 v_color;
varying vec2 v_texcoord;

void main()
{
    gl_FragColor = v_color * texture2D(u_texture, v_texcoord);
}

I read somewhere that gl_FrontColor could be used to "color" vertices, but no matter what I change the values to, it always seems to stay the same.

My question is, what function can I use to set the color of my vertices? I want the vertices to be slightly darker than the rest of the quad so it looks a little "nicer".

Upvotes: 0

Views: 5524

Answers (1)

keltar
keltar

Reputation: 18409

You output to both v_color (your varying), and gl_FrontColor (GLSL builtin). But, in fragment shader, you only use v_color, so anything that is in gl_FrontColor is being ignored.

You should use only one of these. Either

// vertex
#version 130

#define SCALE_FACTOR 0.5

varying vec4 v_color;
varying vec2 v_texcoord;

void main()
{
    v_color = vec4(gl_Color.rgb * SCALE_FACTOR, 1.0);
    v_texcoord = gl_MultiTexCoord0.xy;
    gl_Position = ftransform(); 
}

// fragment
#version 130

uniform sampler2D u_texture;

varying vec4 v_color;
varying vec2 v_texcoord;

void main()
{
    gl_FragColor = v_color * texture2D(u_texture, v_texcoord);
}

Or use gl_FrontColor in vertex and gl_Color in fragment shader, instead of your v_color (and remove this varying as it no longer needed).

Of course vertex gl_Color attribute comes from glColorPointer, - if you changed that colors, it would be changed in shader too.

Upvotes: 2

Related Questions