View Full Version : how do I pass color info to vertex shader?

12-19-2011, 10:41 AM
Ok, so I'm making fireworks and it works rather well.
As a basis for this I am using sparks, which are rendered as follows:

void Fireworks::render()
//set the size of the rendering area
glViewport(0, 0, width, height);

//modelview transform matrix
Matrix4 modelviewmatrix = Matrix4::lookAtMatrix(viewpoint,
Vector3(0.0f, 0.0f, 1.0f));
//projection transform matrix
Matrix4 projectionmatrix = Matrix4::perspectiveMatrix((float)M_PI_4, (float)width / height, 1.0f, 10000.0f);

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

//use the terrain shader
//set our matrices as "uniform" variables
glUniformMatrix4fv(glGetUniformLocation(terrainsha der.getProgram(), "matmodelview"), 1, GL_TRUE, modelviewmatrix.elements());
glUniformMatrix4fv(glGetUniformLocation(terrainsha der.getProgram(), "matprojection"), 1, GL_TRUE, projectionmatrix.elements());
spark* p = sparks;
while(p != NULL){
for(int i = 0; i < 3; i++){
glVertex3f(p->position.x(), p->position.y(), p->position.z());
glVertex3f(20.0f*sin(rand())+p->position.x(), 20.0f*cos(rand())+p->position.y(), 20.0f*cos(rand())+p->position.z());

p = p->next;


These go through the vertex shader which is

out vec3 _color;

void main()
// Get a copy of the normal vector
_color = ?

// Transform the vertex position to screencoordinates
gl_Position = matprojection * (matmodelview * in_position);
and the fragment shader which is

in vec3 _color; //normal vector

void main()
//severely simple lighting model
fragcolor = vec4(_color , 1.0);

now I think I got the passing from the vertex to the fragment shader right, but I want to know how I can pass the color information from my program to the vertex shader. I can't seem to find how to do that...

I could post the rest of the code, but I don't think it'll matter (and it's probably bad anyway). I currently set fragcolor to a fixed color and it works.

12-19-2011, 08:37 PM
From the snippet you have posted, it appears you want to assign the per-vertex normal as color. To do that, you would have to pass the per-vertex normals (glNormal3f) along with positions (glVertex3f). Assuming that the normal is passed in to vertex shader as in_normal, then you can simply assign it to the color output variable like this,

in vec3 in_normal;
in vec4 in_position;
out vec3 _color;

uniform mat4 MVP; //combined modelview/projection matrix

void main()
// Assign the normal vector
_color = in_normal;

// Transform the vertex position to clip space
gl_Position = MVP * in_position;

See if this helps.