[4] 圆锥(Cone)图形的生成算法


顶点数据的生成

 

 1 bool                        YfBuildConeVertices
 2 (
 3     Yreal                   radius, 
 4     Yreal                   height, 
 5     Yuint                   slices, 
 6     YeOriginPose            originPose,
 7     Yuint                   vertexStriding, 
 8     Yuint                   vertexPos,
 9     void*                   pVerticesBuffer
10 )
11 {
12     if (slices < 2 || !pVerticesBuffer)
13     {
14         return false;
15     }
16 
17     Yuint numVertices  = slices + 2;
18 
19     // 顶点赋值
20     char* vertexPtr = (char*)pVerticesBuffer + vertexPos;
21     YsVector3* curVertexPtr   = NULL;
22     Yuint nOffset = 0;
23 
24     Yreal originOffsetY = 0.0f;
25     if (originPose == YE_ORIGIN_POSE_TOP)
26     {
27         originOffsetY = -height;
28     }
29     else if (originPose == YE_ORIGIN_POSE_CENTER)
30     {
31         originOffsetY = -height * 0.5f;
32     }
33 
34     // 锥尖顶点赋值
35     {
36         nOffset = 0;   
37         curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
38         curVertexPtr->x = 0.0f;
39         curVertexPtr->y = height + originOffsetY;
40         curVertexPtr->z = 0.0f;
41     }
42 
43     // 锥底顶点赋值
44     {
45         nOffset = (numVertices - 1) * vertexStriding;   
46         curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
47         curVertexPtr->x = 0.0f;
48         curVertexPtr->y = originOffsetY;
49         curVertexPtr->z = 0.0f;
50     }
51 
52     Yreal angleXZ;
53     Yreal posX, posZ;        
54     for (Yuint i = 0; i < slices; i++)
55     {
56         angleXZ = YD_REAL_TWAIN_PI * i / slices;
57         posX = yf_sin(angleXZ);
58         posZ = yf_cos(angleXZ);
59         nOffset = (i + 1) * vertexStriding; 
60 
61         curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
62         curVertexPtr->x = radius * posX;
63         curVertexPtr->y = originOffsetY;
64         curVertexPtr->z = radius * posZ;
65     }
66 
67     return true;
68 }     

 

三角形索引数据的生成

 1 bool                        YfBuildConeTriIndices
 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;
16     Yuint numTriangles = slices * 2;
17     if (indexType == YE_INDEX_16_BIT && 
18         numVertices > YD_MAX_UNSIGNED_INT16)
19     {
20         return false;
21     }
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 < 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)%slices;
36 
37             nOffset += indexStriding;
38             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
39             triIndexPtr->index0 = numVertices - 1;
40             triIndexPtr->index1 = 1 + (i + 1)%slices;
41             triIndexPtr->index2 = 1 + i;
42         }
43     }
44     else
45     {
46         YsTriIndex32* triIndexPtr = NULL;
47         for (Yuint i= 0; i < 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)%slices;
54 
55             nOffset += indexStriding;
56             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
57             triIndexPtr->index0 = numVertices - 1;
58             triIndexPtr->index1 = 1 + (i + 1)%slices;
59             triIndexPtr->index2 = 1 + i;
60         }
61     }
62 
63     return true;
64 }     

线框索引数据的生成

 1 bool                        YfBuildConeWireIndices
 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;
16     Yuint numLines    = slices * 2;
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 < 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;
40         }
41     }
42     else
43     {
44         YsLineIndex32* lineIndexPtr = NULL;
45         for (Yuint i= 0; i < slices; i++)
46         {
47             nOffset = (i * 2) * indexStriding;
48             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
49             lineIndexPtr->index0 = 0;
50             lineIndexPtr->index1 = 1 + i;
51 
52             nOffset += indexStriding;
53             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
54             lineIndexPtr->index0 = 1 + i;
55             lineIndexPtr->index1 = 1 + (i + 1)%slices;
56         }
57     }
58 
59     return true;
60 }  

 


 

posted on 2013-11-09 08:15  叶飞影  阅读(1178)  评论(0编辑  收藏  举报