這篇用一個頂點量龐大的真實模型,把三種提交幾何的方式放在一起比:glBegin/glEnd 立即模式、顯示列表、以及頂點索引陣列。測試對象是 F-16 Thunderbird 飛機模型,機身部分有 3074 個三角形,經 Deep Exploration 以索引模式整理後,得到 1898 個獨立頂點、2716 組法線與 2925 組紋理座標。
最直覺的做法是遍歷每個三角形的三個頂點,逐一送出紋理、法線與座標:
void DrawBody(void)
{
int iFace, iPoint;
glBegin(GL_TRIANGLES);
for(iFace = 0; iFace < 3074; iFace++)
for(iPoint = 0; iPoint < 3; iPoint++)
{
glTexCoord2fv(textures[face_indices[iFace][iPoint+6]]);
glNormal3fv(normals[face_indices[iFace][iPoint+3]]);
glVertex3fv(vertices[face_indices[iFace][iPoint]]);
}
glEnd();
}
這種寫法在「必須壓縮儲存空間」時很合適——嵌入式環境要省記憶體、網路傳輸要省流量。但在即時應用中它的效能很差:每個頂點都要走一次函數呼叫,而 3074 個三角形意味著上萬次呼叫。
最直接的加速手段是包進顯示列表:
glNewList(bodyList, GL_COMPILE);
DrawBody();
glEndList();
...
glCallList(bodyList);
原始資料的宣告如下:
extern short face_indicies[3704][9]; extern GLfloat vertices [1898][3]; extern GLfloat normals [2716][3]; extern GLfloat textures [2925][2];
face_indicies 每列同時存了頂點、法線、紋理三組索引:
short face_indicies[3704][9] = {
{6, 8, 7, 0, 1, 2, 0, 1, 2},
{6, 9, 8, 0, 3, 1, 0, 3, 1},
{10, 8, 11, 4, 1, 5, 4, 1, 5}
...
};
以 3074 列計算:face_indicies 約 55,332 位元組,vertices 22,776 位元組,normals 32,592 位元組,textures 23,400 位元組,合計 134,100 位元組,約 130 KB。
顯示列表需要把這些資料再複製一份(命令與資料經優化後進命令緩衝區或顯卡)。實際用量無法精算,但可以估計頂點部分:每個三角形 3 個頂點、3 組法線、2 組紋理座標,共 3704 × 3 = 11,112 個頂點,換算成浮點數是 33,336 + 33,336 + 22,224,乘以 4 位元組得 335,584 位元組;加上原始資料共約 469,684 位元組,接近 460 KB。代價是 11,112 個頂點全都得走一趟變換管線,而管線裡盡是矩陣運算。
原始資料不能直接餵給頂點陣列,因為 OpenGL 要求頂點、法線、紋理座標陣列「等長且對齊」:第 0 個頂點必須對應第 0 個法線與第 0 組紋理座標。所以得先重建索引。這裡用一個三角形網格類別來做這件事:
CTriangleMesh thunderBirdBody;
CTriangleMesh thunderBirdGlass;
M3DVector3f vVerts[3];
M3DVector3f vNorms[3];
M3DVector2f vTex[3];
thunderBirdBody.BeginMesh(3074 * 3); // 最壞情況的上限
for(int iFace = 0; iFace < 3074; iFace++)
{
for(int iPoint = 0; iPoint < 3; iPoint++)
{
memcpy(&vVerts[iPoint][0], &vertices[face_indices[iFace][iPoint][0]], sizeof(M3DVector3f));
memcpy(&vNorms[iPoint][0], &normals[face_indices[iFace][iPoint+3][0]], sizeof(M3DVector3f));
memcpy(&vTex[iPoint][0], &textures[face_indices[iFace][iPoint+6][0]], sizeof(M3DVector2f));
}
thunderBirdBody.AddTriangle(vVerts, vNorms, vTex);
}
thunderBirdBody.EndMesh();
thunderBirdBody.Scale(fScale);
AddTriangle 內部做的事,是把傳進來的三個頂點與既有清單逐一比對,若位置、法線、紋理座標三者都相同就沿用舊索引,否則附加到清單尾端:
for(GLuint iVertex = 0; iVertex < 3; iVertex++)
{
GLuint iMatch = 0;
for(iMatch = 0; iMatch < nNumVerts; iMatch++)
{
if(m3dCloseEnough(pVerts[iMatch][0], verts[iVertex][0], e)
&& m3dCloseEnough(pVerts[iMatch][1], verts[iVertex][1], e)
&& m3dCloseEnough(pVerts[iMatch][2], verts[iVertex][2], e)
&& m3dCloseEnough(pNorms[iMatch][0], vNorms[iVertex][0], e)
&& m3dCloseEnough(pNorms[iMatch][1], vNorms[iVertex][1], e)
&& m3dCloseEnough(pNorms[iMatch][2], vNorms[iVertex][2], e)
&& m3dCloseEnough(pTexCoords[iMatch][0], vTexCoords[iVertex][0], e)
&& m3dCloseEnough(pTexCoords[iMatch][1], vTexCoords[iVertex][1], e))
{
pIndexes[nNumIndexes++] = iMatch;
break;
}
}
if(iMatch == nNumVerts) // 沒有重複,附加到尾端
{
memcpy(pVerts[nNumVerts], verts[iVertex], sizeof(M3DVector3f));
memcpy(pNorms[nNumVerts], vNorms[iVertex], sizeof(M3DVector3f));
memcpy(pTexCoords[nNumVerts], &vTexCoords[iVertex], sizeof(M3DVector2f));
pIndexes[nNumIndexes++] = nNumVerts;
nNumVerts++;
}
}
座艙蓋(352 個三角形)用同樣的方式處理成另一個網格物件。
去重後,機身共有 3265 個唯一頂點(含法線與紋理座標)與 11,112 個索引。每個頂點與法線各 3 個浮點、紋理 2 個,合計 3265 × 8 = 26,120 個浮點 = 104,480 位元組;索引用 short 存,11,112 × 2 = 22,224 位元組;總計 126,704 位元組,約 124 KB。
| 渲染方式 | 記憶體用量 | 需變換的頂點數 |
|---|---|---|
| 立即模式 | 約 130 KB | 11,112 |
| 顯示列表 | 約 460 KB | 11,112 |
| 頂點索引陣列 | 約 124 KB | 3,265 |
結論很清楚:索引頂點陣列不只省記憶體,需要跑變換的頂點數只有另外兩種方式的三分之一左右。這個比例取決於模型本身——稜角銳利、面與面不共用的模型去重效果有限;曲面平滑的模型則能省下大量重複。差異一放大,效能差距就很可觀了。