[15] 星星(Star)图形的生成算法
顶点数据的生成
1 bool YfBuildStarVertices 2 ( 3 Yreal radius, 4 Yreal assistRadius, 5 Yreal height, 6 Yuint slices, 7 YeOriginPose originPose, 8 Yuint vertexStriding, 9 Yuint vertexPos, 10 void* pVerticesBuffer 11 ) 12 { 13 if (slices < 2 || !pVerticesBuffer) 14 { 15 return false; 16 } 17 18 Yuint numVertices = (slices*2) + 2; 19 20 // 顶点赋值 21 char* vertexPtr = (char*)pVerticesBuffer + vertexPos; 22 YsVector3* curVertexPtr = NULL; 23 Yuint nOffset = 0; 24 25 Yreal originOffsetY = 0.0f; 26 if (originPose == YE_ORIGIN_POSE_TOP) 27 { 28 originOffsetY = -height; 29 } 30 else if (originPose == YE_ORIGIN_POSE_CENTER) 31 { 32 originOffsetY = -height * 0.5f; 33 } 34 35 // 顶赋值 36 { 37 nOffset = 0; 38 curVertexPtr = (YsVector3*)(vertexPtr + nOffset); 39 curVertexPtr->x = 0.0f; 40 curVertexPtr->y = height + originOffsetY; 41 curVertexPtr->z = 0.0f; 42 } 43 44 // 底赋值 45 { 46 nOffset = (numVertices - 1) * vertexStriding; 47 curVertexPtr = (YsVector3*)(vertexPtr + nOffset); 48 curVertexPtr->x = 0.0f; 49 curVertexPtr->y = originOffsetY; 50 curVertexPtr->z = 0.0f; 51 } 52 53 Yreal angleXZ; 54 Yreal posX, posZ; 55 Yreal fRadius; 56 for (Yuint i = 0; i < 2*slices; i++) 57 { 58 angleXZ = YD_REAL_TWAIN_PI * i / (slices*2); 59 posX = yf_sin(angleXZ); 60 posZ = yf_cos(angleXZ); 61 fRadius = (i%2 == 0) ? radius : assistRadius; 62 63 nOffset = (1 + i) * vertexStriding; 64 curVertexPtr = (YsVector3*)(vertexPtr + nOffset); 65 curVertexPtr->x = fRadius * posX; 66 curVertexPtr->y = originOffsetY; 67 curVertexPtr->z = fRadius * posZ; 68 } 69 70 return true; 71 }
三角形索引数据的生成
1 bool YfBuildStarTriIndices 2 ( 3 Yuint slices, 4 YeIndexType indexType, 5 Yuint indexStriding, 6 Yuint indexPos, 7 void* pTriIndicesBuffer 8 ) 9 { 10 if (slices < 2 || !pTriIndicesBuffer) 11 { 12 return false; 13 } 14 15 Yuint numVertices = slices*2 + 2; 16 if (indexType == YE_INDEX_16_BIT && 17 numVertices > YD_MAX_UNSIGNED_INT16) 18 { 19 return false; 20 } 21 Yuint numTriangles = slices * 4; 22 23 // 索引赋值 24 char* indexPtr = (char*)pTriIndicesBuffer + indexPos; 25 Yuint nOffset = 0; 26 if (indexType == YE_INDEX_16_BIT) 27 { 28 YsTriIndex16* triIndexPtr = NULL; 29 for (Yuint i = 0; i < 2 * slices; i++) 30 { 31 nOffset = (i * 2) * indexStriding; 32 triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset); 33 triIndexPtr->index0 = 0; 34 triIndexPtr->index1 = 1 + i; 35 triIndexPtr->index2 = 1 + (i + 1) % (2*slices); 36 37 nOffset += indexStriding; 38 triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset); 39 triIndexPtr->index0 = numVertices - 1; 40 triIndexPtr->index1 = 1 + (i + 1) % (2*slices); 41 triIndexPtr->index2 = 1 + i; 42 } 43 } 44 else 45 { 46 YsTriIndex32* triIndexPtr = NULL; 47 for (Yuint i = 0; i < 2 * slices; i++) 48 { 49 nOffset = (i * 2) * indexStriding; 50 triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset); 51 triIndexPtr->index0 = 0; 52 triIndexPtr->index1 = 1 + i; 53 triIndexPtr->index2 = 1 + (i + 1) % (2*slices); 54 55 nOffset += indexStriding; 56 triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset); 57 triIndexPtr->index0 = numVertices - 1; 58 triIndexPtr->index1 = 1 + (i + 1) % (2*slices); 59 triIndexPtr->index2 = 1 + i; 60 } 61 } 62 63 return true; 64 }
线框索引数据的生成
1 bool YfBuildStarWireIndices 2 ( 3 Yuint slices, 4 YeIndexType indexType, 5 Yuint indexStriding, 6 Yuint indexPos, 7 void* pWireIndicesBuffer 8 ) 9 { 10 if (slices < 2 || !pWireIndicesBuffer) 11 { 12 return false; 13 } 14 15 Yuint numVertices = slices*2 + 2; 16 Yuint numLines = slices * 5; 17 if (indexType == YE_INDEX_16_BIT && 18 numVertices > YD_MAX_UNSIGNED_INT16) 19 { 20 return false; 21 } 22 23 // 索引赋值 24 char* indexPtr = (char*)pWireIndicesBuffer + indexPos; 25 Yuint nOffset = 0; 26 if (indexType == YE_INDEX_16_BIT) 27 { 28 YsLineIndex16* lineIndexPtr = NULL; 29 for (Yuint i = 0; i < 2 * slices; i++) 30 { 31 nOffset = (i * 2) * indexStriding; 32 lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset); 33 lineIndexPtr->index0 = 0; 34 lineIndexPtr->index1 = 1 + i; 35 36 nOffset += indexStriding; 37 lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset); 38 lineIndexPtr->index0 = 1 + i; 39 lineIndexPtr->index1 = 1 + (i + 1)%(slices*2); 40 } 41 42 Yuint half = 4 * slices * indexStriding; 43 for (Yuint i = 0; i < slices; i++) 44 { 45 nOffset = half + i * indexStriding; 46 lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset); 47 lineIndexPtr->index0 = numVertices - 1; 48 lineIndexPtr->index1 = 1 + i*2 + 1; 49 } 50 } 51 else 52 { 53 YsLineIndex32* lineIndexPtr = NULL; 54 for (Yuint i = 0; i < 2 * slices; i++) 55 { 56 nOffset = (i * 2) * indexStriding; 57 lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset); 58 lineIndexPtr->index0 = 0; 59 lineIndexPtr->index1 = 1 + i; 60 61 nOffset += indexStriding; 62 lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset); 63 lineIndexPtr->index0 = 1 + i; 64 lineIndexPtr->index1 = 1 + (i + 1)%(slices*2); 65 } 66 67 Yuint half = 4 * slices * indexStriding; 68 for (Yuint i = 0; i < slices; i++) 69 { 70 nOffset = half + i * indexStriding; 71 lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset); 72 lineIndexPtr->index0 = numVertices - 1; 73 lineIndexPtr->index1 = 1 + i*2 + 1; 74 } 75 } 76 77 return true; 78 }