View Full Version : Setting colour of vertex doesn't work

08-07-2015, 09:40 AM
I want to render a mesh using OpenGL and shaders. So, first I create an array which stores the list of vertices, where each vertex is represented by 7 floats: 3 for position, and 4 for colour:

GLfloat vertices[] = {x1, y1, z1, r1, g1, b1, a1, x2, y2, z2, r2, g2, b2, a2, x1, y3, z3, r3, g3, b3, a3 ........};

Then, I tell OpenGL how to interpret this data:

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GL_FLOAT), 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 7 * sizeof(GL_FLOAT), (void*)(3 * sizeof(GL_FLOAT)));

Then, in my vertex shader, I extract the position and colour of each vertex:

layout (location = 0) in vec3 vertex_position;
layout (location = 1) in vec4 vertex_colour;

uniform mat4 projection_matrix;
uniform mat4 view_matrix;

out vec4 frag_colour;

void main()
gl_Position = projection_matrix * view_matrix * vec4(vertex_position, 1.0f);
frag_colour = vertex_colour;

Finally, I set the colour in my fragment shader:

in vec4 frag_colour;

void main()
gl_FragColor = frag_colour;

However, no matter what values I use for `(r, g, b, a)` for each vertex, it always renders the model in red. In fact, if I debug in the vertex shader, it turns out that the `vertex_position` variable is not the same as the one I originally loaded into the vertex buffer.

What am I doing wrong?

08-07-2015, 10:13 AM
There's nothing wrong with the code you posted, so whatever the problem is, it lies in the code which wasn't posted.