[8] 圆面(Round)图形的生成算法


顶点数据的生成

 

 1 bool                        YfBuildRoundVertices
 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 < 3 || !pVerticesBuffer)
13     {
14         return false;
15     }
16 
17     Yuint numVertices  = slices + 1;
18 
19     char* vertexPtr = (char*)pVerticesBuffer + vertexPos;
20     YsVector3* curVertexPtr   = NULL;
21     Yuint nOffset = 0;
22 
23     // 圆心顶点赋值
24     {
25         nOffset = 0;   
26         curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
27         curVertexPtr->x = 0.0f;
28         curVertexPtr->y = height;
29         curVertexPtr->z = 0.0f;
30     }
31 
32     Yreal angleXZ;
33     Yreal posX, posZ;        
34     for (Yuint i = 0; i < slices; i++)
35     {
36         angleXZ = YD_REAL_TWAIN_PI * i / slices;
37         posX = yf_sin(angleXZ);
38         posZ = yf_cos(angleXZ);
39 
40         nOffset = (i + 1) * vertexStriding; 
41         curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
42         curVertexPtr->x = radius * posX;
43         curVertexPtr->y = height;
44         curVertexPtr->z = radius * posZ;
45     }
46 
47     return true;
48 } 

 

三角形索引数据的生成

 

 1 bool                        YfBuildRoundTriIndices
 2 (
 3     Yuint                   slices,
 4     YeIndexType             indexType,
 5     Yuint                   indexStriding,  
 6     Yuint                   indexPos,
 7     void*                   pTriIndicesBuffer
 8 )
 9 {
10     if (slices < 3 || !pTriIndicesBuffer)
11     {
12         return false;
13     }
14 
15     Yuint numVertices  = slices + 1;
16     Yuint numTriangles = slices;
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 * indexStriding;
32             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
33             triIndexPtr->index0 = 0;
34             triIndexPtr->index1 = 1 + i;
35             triIndexPtr->index2 = 1 + (i + 1)%slices;
36         }
37     }
38     else
39     {
40         YsTriIndex32* triIndexPtr = NULL;
41         for (Yuint i= 0; i < slices; i++)
42         {
43             nOffset = i * indexStriding;
44             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
45             triIndexPtr->index0 = 0;
46             triIndexPtr->index1 = 1 + i;
47             triIndexPtr->index2 = 1 + (i + 1)%slices;
48         }
49     }
50 
51     return true;
52 }     

 

线框索引数据的生成

 1 bool                        YfBuildRoundWireIndices
 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 + 1;
16     Yuint numLines    = slices;
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 * indexStriding;
32             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
33             lineIndexPtr->index0 = 1 + i;
34             lineIndexPtr->index1 = 1 + (i + 1)%slices;
35         }
36     }
37     else
38     {
39         YsLineIndex32* lineIndexPtr = NULL;
40         for (Yuint i= 0; i < slices; i++)
41         {
42             nOffset = i * indexStriding;
43             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
44             lineIndexPtr->index0 = 1 + i;
45             lineIndexPtr->index1 = 1 + (i + 1)%slices;
46         }
47     }
48 
49     return true;
50 }

 


 

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