# OpenGL - 网格索引

《OpenGL ES 应用开发实践 iOS版》中第六章，关于网格索引的理解 ：

{0,4,1,5,2,6,3,7,......,13,17,12,16}

``````// 传入一个索引数组,根据宏定义的行列来设定索引数组的方式
void SceneMeshInitIndices(GLushort meshIndices[NUM_MESH_INDICES])
{
int    currentRow = 0;
int    currentColumn = 0;
int    currentMeshIndex = 0;

// 从1开始，因为算法在开始时返回一个索引
currentMeshIndex = 1;

for(currentColumn = 0; currentColumn < (NUM_MESH_COLUMNS - 1);currentColumn++)
{

if(0 == (currentColumn % 2))
{ //这是一个偶数列
currentMeshIndex--; // 返回：覆盖重复的顶点

for(currentRow = 0; currentRow < NUM_MESH_ROWS; currentRow++)
{
meshIndices[currentMeshIndex++] =  currentColumn * NUM_MESH_ROWS + currentRow;
meshIndices[currentMeshIndex++] = (currentColumn + 1) * NUM_MESH_ROWS + currentRow;
}
}else{ // 这是一个奇数列
currentMeshIndex--; // 返回：覆盖重复的顶点

for(currentRow = NUM_MESH_ROWS - 1; currentRow >= 0;currentRow--)
{
meshIndices[currentMeshIndex++] =  currentColumn * NUM_MESH_ROWS + currentRow;
meshIndices[currentMeshIndex++] = (currentColumn + 1) * NUM_MESH_ROWS + currentRow;
}
}
}

NSCAssert(currentMeshIndex == NUM_MESH_INDICES, @"Incorrect number of indices intialized.");
}
``````
``````#define NUM_MESH_ROWS (20)     // Must be at least 2            行
#define NUM_MESH_COLUMNS (40)  // Must be at least 2            列
``````