[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 }  

 


 

转载于:https://www.cnblogs.com/WhyEngine/p/3415237.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值