bool ReleaseGridsResource(Vertex_VCN **ppVertices, unsigned short **ppIndices) { if ( *ppVertices ) { GutRelease16BytesAlignedMemory(*ppVertices); *ppVertices = NULL; } if ( *ppIndices ) { delete [] *ppIndices; *ppIndices = NULL; } return true; }
void sModelMaterial_OpenGL::operator delete[](void *p) { GutRelease16BytesAlignedMemory(p); }
// `產生x_grids * y_grids個格子的棋盤格模型` bool GenerateGrids(int x_grids, int y_grids, Vertex_VCN **ppVertices, int &num_vertices, unsigned short **ppIndices, int &num_indices, int &num_triangles) { const int triangles_per_row = x_grids * 2; const int indices_per_row = triangles_per_row + 2; num_vertices = (x_grids + 1) * (y_grids + 1); Vertex_VCN *pVertices = (Vertex_VCN *) GutAllocate16BytesAlignedMemory(sizeof(Vertex_VCN)*num_vertices); *ppVertices = pVertices; if ( pVertices==NULL ) return false; num_indices = indices_per_row * y_grids; unsigned short *pIndices = new unsigned short[num_indices]; *ppIndices = pIndices; if ( pIndices==NULL ) { GutRelease16BytesAlignedMemory(pVertices); return false; } //` 使用triangle strip時, 三角形數目永遠等於索引值數目減去2` num_triangles = num_indices-2; Vector4 vCorner(-0.5f, 0.5f, 0.0f, 1.0f); Vector4 vStep(1.0f/float(x_grids), -1.0f/float(y_grids), 0.0f, 0.0f); Vector4 vPosition = vCorner; Vector4 vNormal(0.0f, 0.0f, 1.0f); Vector4 vColor(1.0f, 1.0f, 1.0f, 1.0f); int x,y; int vertex_index = 0; for ( y=0; y<=y_grids; y++) { vPosition[0] = vCorner[0]; for ( x=0; x<=x_grids; x++, vertex_index++) { pVertices[vertex_index].m_Position = vPosition; pVertices[vertex_index].m_Normal = vNormal; pVertices[vertex_index].m_Color = vColor; vPosition[0] += vStep[0]; } vPosition[1] += vStep[1]; } const int vertices_per_row = x_grids + 1; bool from_left_to_right = true; int index_index = 0; for ( y=0; y<y_grids; y++ ) { if ( from_left_to_right ) { // `在奇數列的時候, 三角形從左排到右.` pIndices[index_index++] = y * vertices_per_row; pIndices[index_index++] = y * vertices_per_row + vertices_per_row; for ( x=0; x<x_grids; x++ ) { vertex_index = y * vertices_per_row + x; pIndices[index_index++] = vertex_index + 1; pIndices[index_index++] = vertex_index + 1 + vertices_per_row; } } else { // `在偶數列的時候, 三角形從右排到左.` pIndices[index_index++] = y * vertices_per_row + x_grids; pIndices[index_index++] = (y+1) * vertices_per_row + x_grids; for ( x=x_grids; x>0; x-- ) { vertex_index = y * vertices_per_row + x; pIndices[index_index++] = vertex_index - 1; pIndices[index_index++] = vertex_index - 1 + vertices_per_row; } } from_left_to_right = !from_left_to_right; } return true; }