Reputation:
I have been trying to optimize my drawing code for a model visualizer i am working on, here is the old solution I used before:
glBegin(GL_QUADS);
glColor4ub(255, 255, 255, 255);
for (int i = 0; i < C.vertices_prepared.size(); i+=12) {
glVertex3i(C.vertices_prepared[i], C.vertices_prepared[i+1], C.vertices_prepared[i+2]);
glVertex3i(C.vertices_prepared[i+3], C.vertices_prepared[i+4], C.vertices_prepared[i+5]);
glVertex3i(C.vertices_prepared[i+6], C.vertices_prepared[i+7], C.vertices_prepared[i+8]);
glVertex3i(C.vertices_prepared[i+9], C.vertices_prepared[i+10], C.vertices_prepared[i+11]);
}
glEnd();
This worked fine for a while, but it's really slow with huge models. I thought that maybe switching to glDrawArrays would make it faster. My new solution is:
glBegin(GL_QUADS);
glColor4ub(255, 255, 255, 255);
glVertexPointer(3, GL_INT, 0, C.vertices_prepared.data());
glDrawArrays(GL_QUADS, 0, C.vertices_prepared.size() / 3);
glEnd();
But this just doesn't work at all. What is the correct way to do this?
Upvotes: 1
Views: 558
Reputation: 210909
You'll get an invalid GL_INVALID_OPERATION
operation error. Only a few operations are allowed within a glBegin
/glEnd
sequence. It is used to specify a vertices with glVertex
. You don't need glBegin
/glEnd
at all when you use fixed function attributes and drawing with glDrawArrays
. However enable and disable the GL_VERTEX_ARRAY
:
glColor4ub(255, 255, 255, 255);
glVertexPointer(3, GL_INT, 0, C.vertices_prepared.data());
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_QUADS, 0, C.vertices_prepared.size() / 3);
glDisableClientState(GL_VERTEX_ARRAY);
Upvotes: 0