user3709119
user3709119

Reputation: 107

OpenGL - vertex shader color not being passed to fragment shader

I'm trying to pass a color from the vertex shader to the fragment shader and set gl_FragColor to this value. When I seem to do this, the color of the object I made flashes all different colors in no discernible pattern. However, when I set the color in the fragment shader to something similar to this gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0) the color is perfectly white with no flashing. I'll post my shaders below:

Vertex Shader:

#version 130
//not using these for now
//uniform mat4 uniform_modelMatrix;
//uniform mat4 uniform_viewMatrix;     
//uniform mat4 uniform_projectionMatrix;

in vec3 in_Position;
in vec4 in_Color;

void main(void) 
{
    //Set Position to XYZW
    vec4 position = vec4(in_Position.xyz, 1.0);
    gl_Position = position;

    //Set Passed Color to white
    out_Color = vec4(1.0, 1.0, 1.0, 1.0);
}

Fragment Shader:

#version 130
in  vec4 pass_Color;

void main(void) 
{
    //Set Color to color from vertex shader - doesn't work
    gl_FragColor = pass_Color;

    //Set Color to white anyways - works
    //gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
} 

I don't know if I'm passing the values incorrectly in the vertex shader, or if I'm not receiving them properly in the fragment shader. Can anyone spot anything wrong?

Upvotes: 2

Views: 1496

Answers (1)

user3709119
user3709119

Reputation: 107

So my buddy helped me out, I had the out variable in the vertex shader named out_Color (which I also just realized I didn't copy properly). But I had this declaration in the vertex shader out vec4 out_Color. I also had in the fragment shader in vec4 pass_Color. As soon as I changed the names to both be pass_Color the program ran perfectly.

So, for future use, if you're passing information between shaders, make sure the names of these variables are the same.

Upvotes: 3

Related Questions