Accessing vertex color in GLSL vertex shader

I’ve managed to get vertex, geometry, and fragment shaders working using GLSL. Vertices and colors are processed correctly through my rendering pipeline, but the problem is I can’t seem to access the vertex color input from the vertex shader. I have tried every variant of a vertex shader that I could find here and elsewhere on the web, but nothing lets me get the initial vertex color. My model ends up being rendered all white except for a bit of yellow and cyan mixed in. What is the correct way to access the vertex color in a GLSL vertex shader?

#version 330

in vec3 vertex;
in vec4 color;
out vec4 vColorVS;

void main()
{
    gl_Position = gl_ModelViewProjectionMatrix * vec4(vertex, 1.0);
    vColorVS = color;
}

If I change the last line of the shader to:

vColorVS = vec4(1.0, 0.0, 0.0, 1.0);

then the model is correctly drawn red, so I know the problem is just getting the vertex color.

This works for me on latest CVS:

#version 120
attribute vec4 p3d_Vertex; 
attribute vec4 p3d_Color;
varying vec4 vColorVS; 
uniform mat4 p3d_ModelViewProjectionMatrix;

void main() 
{ 
    gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex; 
}

(my card doesn’t support newer shaders, so I can’t say anything about GLSL 3.3)

You cannot use the old gl_ModelViewProjectionMatrix on newer versions of GLSL.

The syntax with just “vertex” and “color” is supposed to work as well, but due to a bug, it only works on some models (presumably those that actually have vertex colours). I’ll investigate.

My investigation seems to have uncovered a bug in Panda, but it only appears when one of the attributes is actually missing. Is it perhaps the case that the model you are applying it on doesn’t actually have vertex colours?

Thanks for looking into this.

I am creating my geometry procedurally using GeomVertexFormat.getV3c4() as my vertex format, so I’m positive the vertices have colors. If I don’t apply the shaders, the colors come through as expected.

I’m new to shader programming so various versions and syntax have been a nightmare. For your code, doesn’t the color also have to be assigned:

#version 120
attribute vec4 p3d_Vertex;
attribute vec4 p3d_Color;
varying vec4 vColorVS;
uniform mat4 p3d_ModelViewProjectionMatrix;

void main()
{
    gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex;
    vColorVS = p3d_Color;
}

I’ve tried mixing GLSL version 1.2 and 3.3 shaders, but this often results in a “program not responding” crash even though the shaders compile successfully.

Which version of Panda3D are you using?

I’m using 1.8.0. I tried 1.7.2 but the shaders don’t compile.

Could you try the latest devel version? GLSL has some bugs in 1.8.0.

It worked in the latest version!