问题
I have a sample code which initialize a VBO with a triangle and then renders it.
My main rendering loop is:
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Set the camera
gluLookAt( x, 1.0f, z,
x+lx, 1.0f, z+lz,
0.0f, 1.0f, 0.0f);
// Draw ground
glColor3f(0.9f, 0.9f, 0.9f);
glBegin(GL_QUADS);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, -100.0f);
glEnd();
// draw triangle
glColor3f(0.9f, 0.2f, 0.1f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
if(vboTest) vboTest->draw();
/* The above ->draw() would be equivalent
to the following immediate mode
glBegin(GL_TRIANGLES);
glVertex3f(-1.0f, 0.2f, -1.0f);
glVertex3f(-1.0f, 0.2f, 1.0f);
glVertex3f( 1.0f, 0.2f, 1.0f);
glEnd();*/
The data contained in vboTest is initialized with exactly the same values as the commented code above, but then when I render it with following code
// bind vertexes and indexes
glBindBuffer(GL_ARRAY_BUFFER, vbos_[0]); // vertexes
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos_[2]); // indexes
// do same as vertex array except pointer
glEnableClientState(GL_VERTEX_ARRAY); // activate vertex coords array
glVertexPointer(3, GL_FLOAT, 0, 0); // last param is offset, not ptr
// draw n_tris_ triangles using offset of index array
glDrawElements(GL_TRIANGLES, n_vertex_, GL_UNSIGNED_INT, 0);
// deactivate vertex array
glDisableClientState(GL_VERTEX_ARRAY);
// bind with 0, so, switch back to normal pointer operation
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
The triangle appear to be rotating but is being deformed whilst spinning.
Of course, if I'm to use the immediate mode code above, I can see a spinning triangle without issues.
Aren't VBO supposed to behave semantically equivalent immediate mode? What am I doing wrong?
回答1:
I found the issue!
Surprisingly, during vertex declarations, I was missing one comma. I can't believe it:
GLfloat v[] = { -1.0f, 0.2f, -1.0f // comma missing here!
-1.0f, 0.2f, 1.0f,
1.0f, 0.2f, 1.0f };
Almost the same as this question.
I'm speechless!
Cheers,
E
来源:https://stackoverflow.com/questions/36132402/should-there-be-any-behavioural-differece-in-behaviour-for-vbo-vs-immediate-mode