OpenGL noob: Using VBO to draw a colored triangle [closed]

I tried using straight vertex arrays to draw a triangle with different colors for each vertex and it works fine, but when I use VBO it won't work, so I'm doing something wrong.

// point 1
side_1.push_back(-2.0f); //x1
side_1.push_back(-2.0f); //y1
side_1.push_back(0.0f); //z1

// side color
side_color_1.push_back(0.8f); //r1
side_color_1.push_back(0.0f); //g1
side_color_1.push_back(1.0f); //b1

// point 2
side_1.push_back(2.0f); //x2
side_1.push_back(-2.0f); //y2
side_1.push_back(0.0f); //z2

...etc

// init code
////////////
glGenBuffers(2, &m_vertexBuffer[0]); //Generate a buffer for the vertices

glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer[0]); //Bind the vertex buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * side_1.size(),
&side_1[0], GL_STATIC_DRAW); //Send the data to OpenGL

glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer[1]); //Bind the vertex buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * side_color_1.size(),
&side_color_1[0], GL_STATIC_DRAW); //Send the data to OpenGL

// render code
////////////
glEnableClientState(GL_VERTEX_ARRAY);   // enable vertex array
glEnableClientState(GL_COLOR_ARRAY);    // enable color array

// bind the buffer to the VBO
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer[0]);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer[1]);

glVertexPointer(3, GL_FLOAT, 0, 0);
glColorPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(sizeof(GLfloat) * side_1.size()));

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);


closed as too localized by msell, bummzack, Maximus Minimus, Anko, MichaelHouse♦May 8 '13 at 13:17

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

• What buffer is bound when you call glVertexPointer? What buffer do you think should be bound when you call it? – Maximus Minimus May 8 '13 at 8:04
• – user1075940 May 8 '13 at 10:41
• msell, bummzack, mh01, Anko or Byte56 if i can get a constructive reason for closing this post rather than a "too localized" that would be better. – ArmenB May 8 '13 at 20:34

You are currently putting the position data into one VBO (vertexBuffer[0]) and the color data into another one (vertexBuffer[1]). But when drawing you bind both buffers one after the other, so the second binding will just override the previous one. Then you set the pointers as if both attributes were in the same buffer. This is slightly messed up. The gl...Pointer calls always use the VBO that is currently bound (the one last called with glBindBuffer(GL_ARRAY_BUFFER, ...)).

If you want to draw from your two buffers, you have to first bind the first buffer and tell OpenGL to find the position data at offset 0 of the currently bound buffer:

glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer[0]);
glVertexPointer(3, GL_FLOAT, 0, 0);


Then you bind the second buffer and again tell the GL to take the color data from offset 0 of this buffer.

glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer[1]);
glColorPointer(3, GL_FLOAT, 0, 0);


If you on the other hand really want to source both attributes from the same buffer, using the offset to reference sub-regions of it, then you would have to create a single buffer only, filling it's sub-regions with the corrsesponding data:

// init code
////////////
glGenBuffers(1, &m_vertexBuffer); //Generate a buffer for the vertices

glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer); //Bind the vertex buffer
glBufferData(GL_ARRAY_BUFFER,
sizeof(GLfloat) * (side_1.size()+side_color_1.size(), //large enough for both
nullptr, GL_STATIC_DRAW);         //No data yet, just allocate

//fill sub-regions with corresponding data
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat)*side_1.size(), side_1.data());
glBufferSubData(GL_ARRAY_BUFFER, sizeof(GLfloat)*side_1.size(),
sizeof(GLfloat)*side_color_1.size(), side_color_1.data());

// render code
////////////
// bind the buffer to the VBO
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);

glVertexPointer(3, GL_FLOAT, 0, 0);
glColorPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(sizeof(GLfloat) * side_1.size()));