2010-12-03 18 views
0

我有一個關於glDrawElements和頂點,正常和texcoordinate指數的問題。頂點,正常和texcoordinate指數

如果我有幾何頂點,頂點法線和紋理頂點,每個都有自己的指數。

我可以使用哪些指數?

如果我有這樣的代碼:

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]); 

我應該持有teapotIndices?

謝謝。

回答

3

我不知道我理解你的問題。

TeapotIndices應指示在你希望你的頂點渲染訂單指數集。

舉例來說,假設你有做出來的三個頂點(三角形)的對象:

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 

您的收藏指數/陣列中,你希望他們呈現的順序指定。假設你希望你的三角形逆時針渲染,你會爲指定索引

0,2,1 

glDrawElements然後會先畫左下角,然後右下角,終於上角

中當然,如果只涉及一個三角形,這並沒有什麼意義,但是假設您想添加另一個三角形來反射第一個三角形。這意味着他們觸摸到第一個三角形的底部。相反,再次爲一個指定三個頂點的,你只需要添加一個那就是你的頂點不同:

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 

而你也有你的指數是這樣的:

0,2,1, 2,0,3 
//  ^^^^^ second triangle vertex indices 

看,我們已經添加了只有一個新頂點的整個第二個三角形,並重新使用了新三角形的兩個舊頂點。