Reputation: 1075
I am trying to change the color of the mesh but it doesn't work. Please look at the code below.
typedef struct{
GLubyte red, green, blue, alpha;
} MTcolor;
typedef struct{
GLfloat x, y, z;
} MTvertex;
typedef struct{
MTvertex verts;
MTcolor colors;
} MTmesh;
GLuint vbo;
GLuint ibo;
static const MTmesh mesh[] = {
{ {0, 0, 0}, {0, 0, 255, 255} },
{ {1, 1, 0}, {0, 0, 255, 255} },
{ {2, 0, 0}, {0, 0, 255, 255} }
};
static const GLushort indices[] = {
0, 1, 2
};
MAKE THE BUFFERS
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(mesh), mesh, GL_STREAM_DRAW);
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
RENDER
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(3, GL_FLOAT, sizeof(MTmesh), (void*)offsetof(MTmesh, verts));
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(MTmesh), (void*)offsetof(MTmesh, colors));
I want to change the color but it's not working. Any ideas?
MTcolor col[] = {
255, 0, 0, 255,
255, 0, 0, 255,
255, 0, 0, 255
};
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(col), col);
//bind the indices and draw
Upvotes: 0
Views: 585
Reputation: 162299
You're using an interleaved array layout when creating the VBO. OpenGL doesn't know what is what in a VBO, to it it's just a bunch of bytes. When you update the VBO contents with glBufferSubData the layout of the 'col' array doesn't match that of the mesh array and hence you're messing up the data.
Either use a non interleaved layout or separate VBOs for each vertex attribute or use glMapBuffer to map the VBO into client address space and update the contents there.
Upvotes: 1