vertex, normal and texcoordinate indices
I have a question a开发者_Go百科bout glDrawElements and vertex, normal and texcoordinate indices.
If I have geometric vertex, vertex normals and texture vertices, each with its own indices.
Which of those indices may I use?
If I have this code:
glVertexAttribPointer(vertexHandle, 3, GL_FLOAT, GL_FALSE, 0,
(const GLvoid*) &teapotVertices[0]);
glVertexAttribPointer(normalHandle, 3, GL_FLOAT, GL_FALSE, 0,
(const GLvoid*) &teapotNormals[0]);
glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, GL_FALSE, 0,
(const GLvoid*) &teapotTexCoords[0]);
glEnableVertexAttribArray(vertexHandle);
glEnableVertexAttribArray(normalHandle);
glEnableVertexAttribArray(textureCoordHandle);
glBindTexture(GL_TEXTURE_2D, thisTexture->mTextureID);
glUniformMatrix4fv(mvpMatrixHandle, 1, GL_FALSE,
(GLfloat*)&modelViewProjection.data[0] );
glDrawElements(GL_TRIANGLES, NUM_TEAPOT_OBJECT_INDEX, GL_UNSIGNED_SHORT,
(const GLvoid*) &teapotIndices[0]);
What should hold teapotIndices?
Thanks.
I'm not sure I understand your question.
TeapotIndices should be a collection of indices indicating the order in which you want your vertices to be rendered.
For instance, let's say you have an object made out of three vertices (a triangle):
0.0, 0.0, 0.0 //index 0, lower left corner
0.5, 0.5, 0.0 //index 1, top corner
1.0, 0.0, 0.0 //index 2, lower right corner
You specify in the indices collection/array the order in which you want them rendered. Say you want your triangle to be rendered counterclockwise, you'll specify your indices as
0,2,1
glDrawElements will then draw the lower left corner first, then the lower right corner, and finally the top corner
Of course this doesn't make much sense when just one triangle is involved, but suppose you wanted to add another triangle that mirrors the first one downwards. This means they touch at the first triangle's bottom. Instead of specifying three vertices for that one again, you only need to add the one that is different to your vertices:
0.0, 0.0, 0.0 //index 0, lower left corner for first triangle
0.5, 0.5, 0.0 //index 1, top corner for first triangle
1.0, 0.0, 0.0 //index 2, lower right corner for first triangle
0.5, -0.5, 0.0 //new: index 3, bottom corner of a "mirrored" triangle
And you would have your indices like this:
0,2,1, 2,0,3
// ^^^^^ second triangle vertex indices
See, we've added a whole second triangle with only one new vertex, and re-used two of the old vertices for the new triangle.
精彩评论