I am trying to render a bunch of vertices with an own color for each vertex. The number of vertices is around 1 mio, so I use a Vertex Array Object to render them. It's no problem for me to render the points, but when I add the colors to the data array the points are still rendered white. I tried different tutorials I found, but they all didn't work for me ( like: tut1, tut2 or this ).
I have the data in one array with VCVCVC... 3 floats for the vertex position and 4 floats for the color.
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
unsigned VAO_ID;
glGenVertexArrays(1, &VAO_ID);
glBindVertexArray(VAO_ID);
unsigned VBO_ID;
glGenBuffers(1, &VBO_ID);
glBindBuffer(GL_ARRAY_BUFFER, VBO_ID);
glBufferData(GL_ARRAY_BUFFER, vertex_count*7*4, vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7*4, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 7*4, (char*)NULL + 3);
glEnableVertexAttribArray(1);
glVertexPointer(3, GL_FLOAT, 7*4, &vertices[0]);
glColorPointer(4, GL_FLOAT, 7*4, &vertices[3]);
glDrawArrays(GL_POINTS, 0, vertex_count);
I am new to OpenGL, so it is possible that I made a stupid mistake that I can't see.
BTW: the vertex locations are still correct even with the colors in the array
Pick one and only one method of vertex submission:
Vertex arrays (VA):
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 7*4, &vertices[0]);
glColorPointer(4, GL_FLOAT, 7*4, &vertices[3]);
glDrawArrays(GL_POINTS, 0, vertex_count);
Vertex buffer object (VBO):
unsigned VBO_ID;
glGenBuffers(1, &VBO_ID);
glBindBuffer(GL_ARRAY_BUFFER, VBO_ID);
glBufferData(GL_ARRAY_BUFFER, vertex_count*7*4, vertices, GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 7*4, 0);
glColorPointer(4, GL_FLOAT, 7*4, (char*)NULL + 3);
glDrawArrays(GL_POINTS, 0, vertex_count);
Vertex array object (VAO) + VBO (requires corresponding shaders):
unsigned VAO_ID;
glGenVertexArrays(1, &VAO_ID);
glBindVertexArray(VAO_ID);
unsigned VBO_ID;
glGenBuffers(1, &VBO_ID);
glBindBuffer(GL_ARRAY_BUFFER, VBO_ID);
glBufferData(GL_ARRAY_BUFFER, vertex_count*7*4, vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7*4, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 7*4, (char*)NULL + 3);
glEnableVertexAttribArray(1);