Colors in OpenGL VBOs?
opengl, vbo
Solution
glVertexPointer(3, GL_FLOAT, 0, NULL);
^
The position values in an array of `Vertex`s are not tightly packed so you can't use `0` for `stride`. Use `sizeof( Vertex )`.
glColorPointer(4, GL_FLOAT, offsetof(Vertex, r), NULL);
^^^^^^^^^^^^^^^^^^^
I'm...not sure what you're going for here. Perhaps you thought the 3rd parameter of `glVertexPointer()`/`glColorPointer()` was `pointer` instead of `stride`? Move your `offsetof()` to the final parameter, `pointer`.
All together:
glVertexPointer(3, GL_FLOAT, sizeof( Vertex ), offsetof(Vertex, x) );
glColorPointer(4, GL_FLOAT, sizeof( Vertex ), offsetof(Vertex, r) );
You may have to cast the `pointer` parameter values to `void*` depending on how you've implemented `offsetof()`.
Problem
I'm trying to add colors to my object that using a single struct and single VBO in OpenGL. To do this, I have a `Vertex` struct that I created that looks like this: ``` typedef struct { float x; float y; float z; float r; float g; float b; float a; } Vertex; ``` I already know that I'm setting all of the coordinates and colors correctly to get what I want, because I ran a test iterating over each object I have stored in my list and drawing out the points and setting the colors using `glVertex3f` and `glColor4f` (this was significantly slower than what I'm looking for). But when I try to draw it using VBOs I just get a huge mess of colored triangles going everywhere. The part of my rendering loop that draws the VBO looks like this: ``` glBindBuffer(GL_ARRAY_BUFFER, vbo); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, NULL); glEnableClientState(GL_COLOR_ARRAY); glColorPointer(4, GL_FLOAT, offsetof(Vertex, r), NULL); glDrawArrays(GL_TRIANGLES, 0, vertex_amount); glBindBuffer(GL_ARRAY_BUFFER, 0); ``` What am I doing wrong?