[20] 鼓状物(Drum)图形的生成算法


顶点数据的生成

 1 bool   YfBuildDrumVertices
 2 (
 3     Yreal                   radius, 
 4     Yreal                   assistRadius,
 5     Yuint                   slices,
 6     Yuint                   stacks, 
 7     YeOriginPose            originPose,  
 8     Yuint                   vertexStriding, 
 9     Yuint                   vertexPos,
10     void*                   pVerticesBuffer
11 )
12 {
13     if (slices < 2 || stacks < 3 || !pVerticesBuffer)
14     {
15         return false;
16     }
17 
18     Yuint numVertices  = slices * stacks + 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 = -radius;
29     }
30     else if (originPose == YE_ORIGIN_POSE_BOTTOM)
31     {
32         originOffsetY = radius;
33     }
34 
35     Yreal* pSinList = YD_NEW_ARRAY(Yreal, slices);
36     Yreal* pCosList = YD_NEW_ARRAY(Yreal, slices);
37     Yreal angleXZ;
38     for (Yuint j = 0; j < slices; j++)
39     {
40         angleXZ = YD_REAL_TWAIN_PI * j / slices;
41         pSinList[j] = yf_sin(angleXZ);
42         pCosList[j] = yf_cos(angleXZ);
43     }
44 
45     // 赋值
46     {
47         // 第一个顶点
48         {
49             nOffset = 0;            
50             curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
51             curVertexPtr->x = 0.0f;
52             curVertexPtr->y = radius + originOffsetY;
53             curVertexPtr->z = 0.0f;
54         }
55 
56         // 最后一个顶点
57         {        
58             nOffset = (numVertices - 1) * vertexStriding; 
59             curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
60             curVertexPtr->x = 0.0f;
61             curVertexPtr->y = -radius + originOffsetY;
62             curVertexPtr->z = 0.0f;
63         }
64 
65         for (Yuint i = 0; i < stacks; i++)
66         {
67             Yreal angleY = YD_REAL_PI * i / (stacks - 1);
68             Yreal posY = radius * yf_cos(angleY);
69             Yreal radiusXZ = assistRadius + radius * yf_sin(angleY);
70             Yreal posX, posZ;
71 
72             for (Yuint j = 0; j < slices; j++)
73             {
74                 posX = radiusXZ * pSinList[j];
75                 posZ = radiusXZ * pCosList[j];
76                 nOffset = (i * slices + j + 1) * vertexStriding; 
77                 curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
78                 curVertexPtr->x = posX;
79                 curVertexPtr->y = posY + originOffsetY;
80                 curVertexPtr->z = posZ;
81             }
82         }
83     }
84 
85     YD_SAFE_DELETE_ARRAY(pSinList);
86     YD_SAFE_DELETE_ARRAY(pCosList);
87 
88     return true;
89 }

三角形索引数据的生成和线框索引数据的生成算法与球的类似

 1 bool   YfBuildDrumTriIndices
 2 (
 3     Yuint                   slices,
 4     Yuint                   stacks, 
 5     YeIndexType             indexType,
 6     Yuint                   indexStriding,  
 7     Yuint                   indexPos,
 8     void*                   pTriIndicesBuffer
 9 )
10 {
11     return YfBuildSphereTriIndices(
12         slices,
13         stacks + 2, 
14         indexType,
15         indexStriding,  
16         indexPos,
17         pTriIndicesBuffer);
18 }
19 
20 bool   YfBuildDrumWireIndices
21 (
22     Yuint                   slices,
23     Yuint                   stacks, 
24     YeIndexType             indexType,
25     Yuint                   indexStriding,  
26     Yuint                   indexPos,
27     void*                   pWireIndicesBuffer
28 )
29 {
30     return YfBuildSphereWireIndices(
31         slices,
32         stacks + 2, 
33         indexType,
34         indexStriding,  
35         indexPos,
36         pWireIndicesBuffer);
37 }


 

原文地址:https://www.cnblogs.com/WhyEngine/p/3415268.html