[7] 金字塔(Pyramid)图形的生成算法
顶点数据的生成
1 bool YfBuildPyramidVertices 2 ( 3 Yreal width, 4 Yreal length, 5 Yreal height, 6 YeOriginPose originPose, 7 Yuint vertexStriding, 8 Yuint vertexPos, 9 void* pVerticesBuffer 10 ) 11 { 12 if (!pVerticesBuffer) 13 { 14 return false; 15 } 16 17 Yuint numVertices = 5; 18 19 char* vertexPtr = (char*)pVerticesBuffer + vertexPos; 20 YsVector3* curVertexPtr = NULL; 21 Yuint nOffset = 0; 22 23 YsVector3 vOriginOffset(-width / 2, -height / 2, -length / 2); 24 if (originPose == YE_ORIGIN_POSE_TOP) 25 { 26 vOriginOffset.y = -height; 27 } 28 else if (originPose == YE_ORIGIN_POSE_BOTTOM) 29 { 30 vOriginOffset.y = 0.0f; 31 } 32 33 // 底四个顶点 34 for (Yuint j = 0; j < 2; j++) 35 { 36 for (Yuint i = 0; i < 2; i++) 37 { 38 nOffset = (j*2 + i) * vertexStriding; 39 curVertexPtr = (YsVector3*)(vertexPtr + nOffset); 40 curVertexPtr->x = width*i + vOriginOffset.x; 41 curVertexPtr->y = vOriginOffset.y; 42 curVertexPtr->z = length*j + vOriginOffset.z; 43 } 44 } 45 46 // 顶尖顶点 47 nOffset = 4 * vertexStriding; 48 curVertexPtr = (YsVector3*)(vertexPtr + nOffset); 49 curVertexPtr->x = 0.0f; 50 curVertexPtr->y = height + vOriginOffset.y; 51 curVertexPtr->z = 0.0f; 52 53 return true; 54 }
三角形索引数据的生成
1 bool YfBuildPyramidTriIndices 2 ( 3 YeIndexType indexType, 4 Yuint indexStriding, 5 Yuint indexPos, 6 void* pTriIndicesBuffer 7 ) 8 { 9 if (!pTriIndicesBuffer) 10 { 11 return false; 12 } 13 14 char* indexPtr = (char*)pTriIndicesBuffer + indexPos; 15 if (indexType == YE_INDEX_16_BIT) 16 { 17 YsTriIndex16* triIndexPtr = NULL; 18 19 // bottom 20 triIndexPtr = (YsTriIndex16*)(indexPtr + 0*indexStriding); 21 triIndexPtr->index0 = 0; triIndexPtr->index1 = 1; triIndexPtr->index2 = 3; 22 triIndexPtr = (YsTriIndex16*)(indexPtr + 1*indexStriding); 23 triIndexPtr->index0 = 0; triIndexPtr->index1 = 3; triIndexPtr->index2 = 2; 24 25 // 4 face 26 triIndexPtr = (YsTriIndex16*)(indexPtr + 2*indexStriding); 27 triIndexPtr->index0 = 4; triIndexPtr->index1 = 1; triIndexPtr->index2 = 0; 28 triIndexPtr = (YsTriIndex16*)(indexPtr + 3*indexStriding); 29 triIndexPtr->index0 = 4; triIndexPtr->index1 = 3; triIndexPtr->index2 = 1; 30 triIndexPtr = (YsTriIndex16*)(indexPtr + 4*indexStriding); 31 triIndexPtr->index0 = 4; triIndexPtr->index1 = 2; triIndexPtr->index2 = 3; 32 triIndexPtr = (YsTriIndex16*)(indexPtr + 5*indexStriding); 33 triIndexPtr->index0 = 4; triIndexPtr->index1 = 0; triIndexPtr->index2 = 2; 34 } 35 else 36 { 37 YsTriIndex32* triIndexPtr = NULL; 38 39 // bottom 40 triIndexPtr = (YsTriIndex32*)(indexPtr + 0*indexStriding); 41 triIndexPtr->index0 = 0; triIndexPtr->index1 = 1; triIndexPtr->index2 = 3; 42 triIndexPtr = (YsTriIndex32*)(indexPtr + 1*indexStriding); 43 triIndexPtr->index0 = 0; triIndexPtr->index1 = 3; triIndexPtr->index2 = 2; 44 45 // 4 face 46 triIndexPtr = (YsTriIndex32*)(indexPtr + 2*indexStriding); 47 triIndexPtr->index0 = 4; triIndexPtr->index1 = 1; triIndexPtr->index2 = 0; 48 triIndexPtr = (YsTriIndex32*)(indexPtr + 3*indexStriding); 49 triIndexPtr->index0 = 4; triIndexPtr->index1 = 3; triIndexPtr->index2 = 1; 50 triIndexPtr = (YsTriIndex32*)(indexPtr + 4*indexStriding); 51 triIndexPtr->index0 = 4; triIndexPtr->index1 = 2; triIndexPtr->index2 = 3; 52 triIndexPtr = (YsTriIndex32*)(indexPtr + 5*indexStriding); 53 triIndexPtr->index0 = 4; triIndexPtr->index1 = 0; triIndexPtr->index2 = 2; 54 } 55 56 return true; 57 }
线框索引数据的生成
1 bool YfBuildPyramidWireIndices 2 ( 3 YeIndexType indexType, 4 Yuint indexStriding, 5 Yuint indexPos, 6 void* pWireIndicesBuffer 7 ) 8 { 9 if (!pWireIndicesBuffer) 10 { 11 return false; 12 } 13 14 Yuint numVertices = 5; 15 Yuint numLines = 8; 16 if (indexType == YE_INDEX_16_BIT && 17 numVertices > YD_MAX_UNSIGNED_INT16) 18 { 19 return false; 20 } 21 22 // 索引赋值 23 char* indexPtr = (char*)pWireIndicesBuffer + indexPos; 24 Yuint nOffset = 0; 25 if (indexType == YE_INDEX_16_BIT) 26 { 27 YsLineIndex16* lineIndexPtr = NULL; 28 29 // bottom 30 lineIndexPtr = (YsLineIndex16*)(indexPtr + 0*indexStriding); 31 lineIndexPtr->index0 = 0; lineIndexPtr->index1 = 1; 32 lineIndexPtr = (YsLineIndex16*)(indexPtr + 1*indexStriding); 33 lineIndexPtr->index0 = 1; lineIndexPtr->index1 = 3; 34 lineIndexPtr = (YsLineIndex16*)(indexPtr + 2*indexStriding); 35 lineIndexPtr->index0 = 3; lineIndexPtr->index1 = 2; 36 lineIndexPtr = (YsLineIndex16*)(indexPtr + 3*indexStriding); 37 lineIndexPtr->index0 = 2; lineIndexPtr->index1 = 0; 38 39 // 4 lines 40 lineIndexPtr = (YsLineIndex16*)(indexPtr + 4*indexStriding); 41 lineIndexPtr->index0 = 4; lineIndexPtr->index1 = 0; 42 lineIndexPtr = (YsLineIndex16*)(indexPtr + 5*indexStriding); 43 lineIndexPtr->index0 = 4; lineIndexPtr->index1 = 1; 44 lineIndexPtr = (YsLineIndex16*)(indexPtr + 6*indexStriding); 45 lineIndexPtr->index0 = 4; lineIndexPtr->index1 = 2; 46 lineIndexPtr = (YsLineIndex16*)(indexPtr + 7*indexStriding); 47 lineIndexPtr->index0 = 4; lineIndexPtr->index1 = 3; 48 } 49 else 50 { 51 YsLineIndex32* lineIndexPtr = NULL; 52 53 // bottom 54 lineIndexPtr = (YsLineIndex32*)(indexPtr + 0*indexStriding); 55 lineIndexPtr->index0 = 0; lineIndexPtr->index1 = 1; 56 lineIndexPtr = (YsLineIndex32*)(indexPtr + 1*indexStriding); 57 lineIndexPtr->index0 = 1; lineIndexPtr->index1 = 3; 58 lineIndexPtr = (YsLineIndex32*)(indexPtr + 2*indexStriding); 59 lineIndexPtr->index0 = 3; lineIndexPtr->index1 = 2; 60 lineIndexPtr = (YsLineIndex32*)(indexPtr + 3*indexStriding); 61 lineIndexPtr->index0 = 2; lineIndexPtr->index1 = 0; 62 63 // 4 lines 64 lineIndexPtr = (YsLineIndex32*)(indexPtr + 4*indexStriding); 65 lineIndexPtr->index0 = 4; lineIndexPtr->index1 = 0; 66 lineIndexPtr = (YsLineIndex32*)(indexPtr + 5*indexStriding); 67 lineIndexPtr->index0 = 4; lineIndexPtr->index1 = 1; 68 lineIndexPtr = (YsLineIndex32*)(indexPtr + 6*indexStriding); 69 lineIndexPtr->index0 = 4; lineIndexPtr->index1 = 2; 70 lineIndexPtr = (YsLineIndex32*)(indexPtr + 7*indexStriding); 71 lineIndexPtr->index0 = 4; lineIndexPtr->index1 = 3; 72 } 73 74 return true; 75 }