[5] 柱台(Cylinder)图形的生成算法


顶点数据的生成

 

 1 bool                        YfBuildCylinderVertices
 2 (
 3     Yreal                   topRadius, 
 4     Yreal                   bottomRadius, 
 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     for (Yuint i = 0; i < slices; i++)
56     {
57         angleXZ = YD_REAL_TWAIN_PI * i / slices;
58         posX = yf_sin(angleXZ);
59         posZ = yf_cos(angleXZ);
60 
61         // 上顶点
62         {
63             nOffset = (i + 1) * vertexStriding; 
64             curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
65             curVertexPtr->x = topRadius * posX;
66             curVertexPtr->y = height + originOffsetY;
67             curVertexPtr->z = topRadius * posZ;
68         }
69 
70         // 下顶点
71         {
72             nOffset = (i + 1 + slices) * vertexStriding; 
73             curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
74             curVertexPtr->x = bottomRadius * posX;
75             curVertexPtr->y = originOffsetY;
76             curVertexPtr->z = bottomRadius * posZ;
77         }
78     }
79 
80     return true;
81 }

 

三角形索引数据的生成

 

 1 bool                        YfBuildCylinderTriIndices
 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     Yuint numTriangles = slices * 4;
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 * 4) * 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 + slices + (i + 1)%slices;
41             triIndexPtr->index2 = 1 + slices + i;
42 
43             nOffset += indexStriding;
44             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
45             triIndexPtr->index0 = 1 + i;
46             triIndexPtr->index1 = 1 + slices + i;
47             triIndexPtr->index2 = 1 + (i + 1)%slices;
48 
49             nOffset += indexStriding;
50             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
51             triIndexPtr->index0 = 1 + (i + 1)%slices;
52             triIndexPtr->index1 = 1 + slices + i;
53             triIndexPtr->index2 = 1 + slices + (i + 1)%slices;
54         }
55     }
56     else
57     {
58         YsTriIndex32* triIndexPtr = NULL;
59         for (Yuint i= 0; i < slices; i++)
60         {
61             nOffset = (i * 4) * indexStriding;
62             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
63             triIndexPtr->index0 = 0;
64             triIndexPtr->index1 = 1 + i;
65             triIndexPtr->index2 = 1 + (i + 1)%slices;
66 
67             nOffset += indexStriding;
68             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
69             triIndexPtr->index0 = numVertices - 1;
70             triIndexPtr->index1 = 1 + slices + (i + 1)%slices;
71             triIndexPtr->index2 = 1 + slices + i;
72 
73             nOffset += indexStriding;
74             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
75             triIndexPtr->index0 = 1 + i;
76             triIndexPtr->index1 = 1 + slices + i;
77             triIndexPtr->index2 = 1 + (i + 1)%slices;
78 
79             nOffset += indexStriding;
80             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
81             triIndexPtr->index0 = 1 + (i + 1)%slices;
82             triIndexPtr->index1 = 1 + slices + i;
83             triIndexPtr->index2 = 1 + slices + (i + 1)%slices;
84         }
85     }
86 
87     return true;
88 }  

 

线框索引数据的生成

 1 bool                        YfBuildCylinderWireIndices
 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 < slices; i++)
30         {
31             nOffset = (i * 5) * 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             nOffset += indexStriding;
42             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
43             lineIndexPtr->index0 = 1 + i;
44             lineIndexPtr->index1 = 1 + slices + i;
45 
46             nOffset += indexStriding;
47             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
48             lineIndexPtr->index0 = 1 + slices + i;
49             lineIndexPtr->index1 = 1 + slices + (i + 1)%slices;
50 
51             nOffset += indexStriding;
52             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
53             lineIndexPtr->index0 = numVertices - 1;
54             lineIndexPtr->index1 = 1 + slices + i;
55         }
56     }
57     else
58     {
59         YsLineIndex32* lineIndexPtr = NULL;
60         for (Yuint i= 0; i < slices; i++)
61         {
62             nOffset = (i * 5) * indexStriding;
63             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
64             lineIndexPtr->index0 = 0;
65             lineIndexPtr->index1 = 1 + i;
66 
67             nOffset += indexStriding;
68             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
69             lineIndexPtr->index0 = 1 + i;
70             lineIndexPtr->index1 = 1 + (i + 1)%slices;
71 
72             nOffset += indexStriding;
73             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
74             lineIndexPtr->index0 = 1 + i;
75             lineIndexPtr->index1 = 1 + slices + i;
76 
77             nOffset += indexStriding;
78             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
79             lineIndexPtr->index0 = 1 + slices + i;
80             lineIndexPtr->index1 = 1 + slices + (i + 1)%slices;
81 
82             nOffset += indexStriding;
83             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
84             lineIndexPtr->index0 = numVertices - 1;
85             lineIndexPtr->index1 = 1 + slices + i;
86         }
87     }
88 
89     return true;
90 }

 


 

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