[9] 圆环(Ring)图形的生成算法


顶点数据的生成

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

 

三角形索引数据的生成

 

 1 bool                        YfBuildRingTriIndices
 2 (
 3     Yuint                   slices,
 4     Yuint                   stacks,
 5     YeIndexType             indexType,
 6     Yuint                   indexStriding,  
 7     Yuint                   indexPos,
 8     void*                   pTriIndicesBuffer
 9 )
10 {
11     if (slices < 2 || stacks < 3 || !pTriIndicesBuffer)
12     {
13         return false;
14     }
15 
16     Yuint numVertices  = slices * stacks;
17     Yuint numTriangles = slices * stacks * 2;
18     if (indexType == YE_INDEX_16_BIT && 
19         numVertices > YD_MAX_UNSIGNED_INT16)
20     {
21         return false;
22     }
23 
24     // 索引赋值
25     char* indexPtr = (char*)pTriIndicesBuffer + indexPos;
26     Yuint nOffset = 0;
27     if (indexType == YE_INDEX_16_BIT)
28     {
29         YsTriIndex16* triIndexPtr = NULL;
30         for (Yuint i = 0; i < stacks; i++)
31         {
32             for (Yuint j = 0; j < slices; j++)
33             {
34                 nOffset = (i*slices + j)*2 * indexStriding;
35                 triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
36                 triIndexPtr->index0 = slices * i + j;
37                 triIndexPtr->index1 = slices * i + (j + 1)%slices;
38                 triIndexPtr->index2 = slices * ((i + 1) % stacks) + j;
39 
40                 nOffset += indexStriding;
41                 triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
42                 triIndexPtr->index0 = slices * i + (j + 1)%slices;
43                 triIndexPtr->index1 = slices * ((i + 1) % stacks) + (j + 1)%slices;
44                 triIndexPtr->index2 = slices * ((i + 1) % stacks) + j;
45             }
46         }
47     }
48     else
49     {
50         YsTriIndex32* triIndexPtr = NULL;
51         for (Yuint i = 0; i < stacks; i++)
52         {
53             for (Yuint j = 0; j < slices; j++)
54             {
55                 nOffset = (i*slices + j)*2 * indexStriding;
56                 triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
57                 triIndexPtr->index0 = slices * i + j;
58                 triIndexPtr->index1 = slices * i + (j + 1)%slices;
59                 triIndexPtr->index2 = slices * ((i + 1) % stacks) + j;
60 
61                 nOffset += indexStriding;
62                 triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
63                 triIndexPtr->index0 = slices * i + (j + 1)%slices;
64                 triIndexPtr->index1 = slices * ((i + 1) % stacks) + (j + 1)%slices;
65                 triIndexPtr->index2 = slices * ((i + 1) % stacks) + j;
66             }
67         }
68     }
69 
70     return true;
71 }   

 

线框索引数据的生成

 1 bool                        YfBuildRingWireIndices
 2 (
 3     Yuint                   slices,
 4     Yuint                   stacks,
 5     YeIndexType             indexType,
 6     Yuint                   indexStriding,  
 7     Yuint                   indexPos,
 8     void*                   pWireIndicesBuffer
 9 )
10 {
11     if (slices < 2 || !pWireIndicesBuffer)
12     {
13         return false;
14     }
15 
16     Yuint numVertices = slices * stacks;
17     Yuint numLines    = slices * stacks * 2;
18     if (indexType == YE_INDEX_16_BIT && 
19         numVertices > YD_MAX_UNSIGNED_INT16)
20     {
21         return false;
22     }
23 
24     // 索引赋值
25     char* indexPtr = (char*)pWireIndicesBuffer + indexPos;
26     Yuint nOffset = 0;
27     if (indexType == YE_INDEX_16_BIT)
28     {
29         YsLineIndex16* lineIndexPtr = NULL;
30         for (Yuint i = 0; i < stacks; i++)
31         {
32             for (Yuint j = 0; j < slices; j++)
33             {
34                 nOffset = (i*slices + j) * 2 * indexStriding;
35                 lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
36                 lineIndexPtr->index0 = slices * i + j;
37                 lineIndexPtr->index1 = slices * i + (j + 1)%slices;
38 
39                 nOffset += indexStriding;
40                 lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
41                 lineIndexPtr->index0 = slices * i + j;
42                 lineIndexPtr->index1 = slices * ((i + 1) % stacks) + j;
43             }
44         }
45     }
46     else
47     {
48         YsLineIndex32* lineIndexPtr = NULL;
49         for (Yuint i = 0; i < stacks; i++)
50         {
51             for (Yuint j = 0; j < slices; j++)
52             {
53                 nOffset = (i*slices + j) * 2 * indexStriding;
54                 lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
55                 lineIndexPtr->index0 = slices * i + j;
56                 lineIndexPtr->index1 = slices * i + (j + 1)%slices;
57 
58                 nOffset += indexStriding;
59                 lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
60                 lineIndexPtr->index0 = slices * i + j;
61                 lineIndexPtr->index1 = slices * ((i + 1) % stacks) + j;
62             }
63         }
64     }
65 
66     return true;
67 }

 

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