PDA

View Full Version : glDrawArrays with vector(STL)?



KeithS
03-25-2012, 09:31 AM
Hello.

Is it possible to use STL vector in glDrawArrays?

For example (which does not work):



vector<GLfloat> verts;
verts.push_back(0.0f); verts.push_back(0.0f); verts.push_back(0.0f);
verts.push_back(10.0f); verts.push_back(0.0f); verts.push_back(0.0f);
verts.push_back(0.0f); verts.push_back(10.0f); verts.push_back(0.0f);

void draw_scene()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, verts);

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableClientState(GL_VERTEX_ARRAY);
glFlush();
}


Instead of (which does work):



GLfloat verts_a[] =
{
0.0f, 0.0f, 0.0f,
10.0f, 0.0f, 0.0f,
0.0f, 10.0f, 0.0f
};

void draw_scene()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, verts_a);

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableClientState(GL_VERTEX_ARRAY);
glFlush();
}


Probably an old question, but I did not find much about it, hence this post. If it is not possible, then okay. If it is, then I am obviously doing something wrong. Could anyone suggest something please?

Thanks.

DindinX
03-25-2012, 11:51 AM
Just use &amp;verts[0] instead of verts as the fourth parameter of glVertexPointer. This will work with std::vector the same way it works with arrays.

KeithS
03-25-2012, 11:28 PM
My thanks!