[10] 圆管(Pipe)图形的生成算法


顶点数据的生成

 1 bool                        YfBuildPipeVertices
 2 (
 3     Yreal                   radius, 
 4     Yreal                   assistRadius, 
 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 * 4;
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     Yreal angleXZ;
36     Yreal posX, posZ;        
37     for (Yuint i = 0; i < slices; i++)
38     {
39         angleXZ = YD_REAL_TWAIN_PI * i / slices;
40         posX = yf_sin(angleXZ);
41         posZ = yf_cos(angleXZ);
42 
43         // 上顶点
44         {
45             //
46             nOffset = (i * 2) * vertexStriding; 
47             curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
48             curVertexPtr->x = radius * posX;
49             curVertexPtr->y = height + originOffsetY;
50             curVertexPtr->z = radius * posZ;
51 
52             //
53             nOffset += vertexStriding;
54             curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
55             curVertexPtr->x = assistRadius * posX;
56             curVertexPtr->y = height + originOffsetY;
57             curVertexPtr->z = assistRadius * posZ;
58         }
59 
60         // 下顶点
61         {
62             //
63             nOffset = (slices * 2 + i * 2) * vertexStriding; 
64             curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
65             curVertexPtr->x = radius * posX;
66             curVertexPtr->y = originOffsetY;
67             curVertexPtr->z = radius * posZ;
68 
69             //
70             nOffset += vertexStriding;
71             curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
72             curVertexPtr->x = assistRadius * posX;
73             curVertexPtr->y = originOffsetY;
74             curVertexPtr->z = assistRadius * posZ;
75         }
76     }
77 
78     return true;
79 }   

 

三角形索引数据的生成

 

  1 bool                        YfBuildPipeTriIndices
  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 * 4;
 16     Yuint numTriangles = slices * 8;
 17     if (indexType == YE_INDEX_16_BIT && 
 18         numVertices > YD_MAX_UNSIGNED_INT16)
 19     {
 20         return false;
 21     }
 22 
 23     Yuint halfVtxNum = numVertices / 2;
 24 
 25     // 索引赋值
 26     char* indexPtr = (char*)pTriIndicesBuffer + indexPos;
 27     Yuint nOffset = 0;
 28     if (indexType == YE_INDEX_16_BIT)
 29     {
 30         YsTriIndex16* triIndexPtr = NULL;
 31         for (Yuint i = 0; i < slices; i++)
 32         {
 33             // 外面
 34             nOffset = (i * 8) * indexStriding;
 35             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
 36             triIndexPtr->index0 = i * 2;
 37             triIndexPtr->index1 = halfVtxNum + i * 2;
 38             triIndexPtr->index2 = (i + 1) % slices * 2;
 39 
 40             nOffset += indexStriding;
 41             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
 42             triIndexPtr->index0 = (i + 1) % slices * 2;
 43             triIndexPtr->index1 = halfVtxNum + i * 2;
 44             triIndexPtr->index2 = halfVtxNum + (i + 1) % slices * 2;
 45 
 46             // 里面
 47             nOffset += indexStriding;
 48             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
 49             triIndexPtr->index0 = i * 2 + 1;
 50             triIndexPtr->index1 = (i + 1) % slices * 2 + 1;
 51             triIndexPtr->index2 = halfVtxNum + i * 2 + 1;
 52 
 53             nOffset += indexStriding;
 54             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
 55             triIndexPtr->index0 = (i + 1) % slices * 2 + 1;
 56             triIndexPtr->index1 = halfVtxNum + (i + 1) % slices * 2 + 1;
 57             triIndexPtr->index2 = halfVtxNum + i * 2 + 1;
 58 
 59             // 上面
 60             nOffset += indexStriding;
 61             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
 62             triIndexPtr->index0 = i * 2;
 63             triIndexPtr->index1 = (i + 1) % slices * 2 + 1;
 64             triIndexPtr->index2 = i * 2 + 1;
 65 
 66             nOffset += indexStriding;
 67             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
 68             triIndexPtr->index0 = i * 2;
 69             triIndexPtr->index1 = (i + 1) % slices * 2;
 70             triIndexPtr->index2 = (i + 1) % slices * 2 + 1;
 71 
 72             // 下面
 73             nOffset += indexStriding;
 74             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
 75             triIndexPtr->index0 = halfVtxNum + i * 2;
 76             triIndexPtr->index1 = halfVtxNum + i * 2 + 1;
 77             triIndexPtr->index2 = halfVtxNum + (i + 1) % slices * 2 + 1;
 78 
 79             nOffset += indexStriding;
 80             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
 81             triIndexPtr->index0 = halfVtxNum + i * 2;
 82             triIndexPtr->index1 = halfVtxNum + (i + 1) % slices * 2 + 1;
 83             triIndexPtr->index2 = halfVtxNum + (i + 1) % slices * 2;
 84         }
 85     }
 86     else
 87     {
 88         YsTriIndex32* triIndexPtr = NULL;
 89         for (Yuint i = 0; i < slices; i++)
 90         {
 91             // 外面
 92             nOffset = (i * 8) * indexStriding;
 93             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
 94             triIndexPtr->index0 = i * 2;
 95             triIndexPtr->index1 = halfVtxNum + i * 2;
 96             triIndexPtr->index2 = (i + 1) % slices * 2;
 97 
 98             nOffset += indexStriding;
 99             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
100             triIndexPtr->index0 = (i + 1) % slices * 2;
101             triIndexPtr->index1 = halfVtxNum + i * 2;
102             triIndexPtr->index2 = halfVtxNum + (i + 1) % slices * 2;
103 
104             // 里面
105             nOffset += indexStriding;
106             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
107             triIndexPtr->index0 = i * 2 + 1;
108             triIndexPtr->index1 = (i + 1) % slices * 2 + 1;
109             triIndexPtr->index2 = halfVtxNum + i * 2 + 1;
110 
111             nOffset += indexStriding;
112             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
113             triIndexPtr->index0 = (i + 1) % slices * 2 + 1;
114             triIndexPtr->index1 = halfVtxNum + (i + 1) % slices * 2 + 1;
115             triIndexPtr->index2 = halfVtxNum + i * 2 + 1;
116 
117             // 上面
118             nOffset += indexStriding;
119             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
120             triIndexPtr->index0 = i * 2;
121             triIndexPtr->index1 = (i + 1) % slices * 2 + 1;
122             triIndexPtr->index2 = i * 2 + 1;
123 
124             nOffset += indexStriding;
125             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
126             triIndexPtr->index0 = i * 2;
127             triIndexPtr->index1 = (i + 1) % slices * 2;
128             triIndexPtr->index2 = (i + 1) % slices * 2 + 1;
129 
130             // 下面
131             nOffset += indexStriding;
132             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
133             triIndexPtr->index0 = halfVtxNum + i * 2;
134             triIndexPtr->index1 = halfVtxNum + i * 2 + 1;
135             triIndexPtr->index2 = halfVtxNum + (i + 1) % slices * 2 + 1;
136 
137             nOffset += indexStriding;
138             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
139             triIndexPtr->index0 = halfVtxNum + i * 2;
140             triIndexPtr->index1 = halfVtxNum + (i + 1) % slices * 2 + 1;
141             triIndexPtr->index2 = halfVtxNum + (i + 1) % slices * 2;
142         }
143     }
144 
145     return true;
146 }   

 

线框索引数据的生成

  1 bool                        YfBuildPipeWireIndices
  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 * 4;
 16     Yuint numLines    = slices * 8;
 17     if (indexType == YE_INDEX_16_BIT && 
 18         numVertices > YD_MAX_UNSIGNED_INT16)
 19     {
 20         return false;
 21     }
 22 
 23     Yuint halfVtxNum = numVertices / 2;
 24 
 25     // 索引赋值
 26     char* indexPtr = (char*)pWireIndicesBuffer + indexPos;
 27     Yuint nOffset = 0;
 28     if (indexType == YE_INDEX_16_BIT)
 29     {
 30         YsLineIndex16* lineIndexPtr = NULL;
 31         for (Yuint i= 0; i < slices; i++)
 32         {
 33             // 外面
 34             nOffset = (i * 8) * indexStriding;
 35             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 36             lineIndexPtr->index0 = i * 2;
 37             lineIndexPtr->index1 = halfVtxNum + i * 2;
 38 
 39             // 里面
 40             nOffset += indexStriding;
 41             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 42             lineIndexPtr->index0 = i * 2 + 1;
 43             lineIndexPtr->index1 = halfVtxNum + i * 2 + 1;
 44 
 45             // 上面
 46             nOffset += indexStriding;
 47             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 48             lineIndexPtr->index0 = i * 2;
 49             lineIndexPtr->index1 = i * 2 + 1;
 50 
 51             nOffset += indexStriding;
 52             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 53             lineIndexPtr->index0 = i * 2;
 54             lineIndexPtr->index1 = (i + 1) % slices * 2;
 55 
 56             nOffset += indexStriding;
 57             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 58             lineIndexPtr->index0 = i * 2 + 1;
 59             lineIndexPtr->index1 = (i + 1) % slices * 2 + 1;
 60 
 61             // 下面
 62             nOffset += indexStriding;
 63             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 64             lineIndexPtr->index0 = halfVtxNum + i * 2;
 65             lineIndexPtr->index1 = halfVtxNum + i * 2 + 1;
 66 
 67             nOffset += indexStriding;
 68             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 69             lineIndexPtr->index0 = halfVtxNum + i * 2;
 70             lineIndexPtr->index1 = halfVtxNum + (i + 1) % slices * 2;
 71 
 72             nOffset += indexStriding;
 73             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
 74             lineIndexPtr->index0 = halfVtxNum + i * 2 + 1;
 75             lineIndexPtr->index1 = halfVtxNum + (i + 1) % slices * 2 + 1;
 76         }
 77     }
 78     else
 79     {
 80         YsLineIndex32* lineIndexPtr = NULL;
 81         for (Yuint i= 0; i < slices; i++)
 82         {
 83             // 外面
 84             nOffset = (i * 8) * indexStriding;
 85             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
 86             lineIndexPtr->index0 = i * 2;
 87             lineIndexPtr->index1 = halfVtxNum + i * 2;
 88 
 89             // 里面
 90             nOffset += indexStriding;
 91             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
 92             lineIndexPtr->index0 = i * 2 + 1;
 93             lineIndexPtr->index1 = halfVtxNum + i * 2 + 1;
 94 
 95             // 上面
 96             nOffset += indexStriding;
 97             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
 98             lineIndexPtr->index0 = i * 2;
 99             lineIndexPtr->index1 = i * 2 + 1;
100 
101             nOffset += indexStriding;
102             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
103             lineIndexPtr->index0 = i * 2;
104             lineIndexPtr->index1 = (i + 1) % slices * 2;
105 
106             nOffset += indexStriding;
107             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
108             lineIndexPtr->index0 = i * 2 + 1;
109             lineIndexPtr->index1 = (i + 1) % slices * 2 + 1;
110 
111             // 下面
112             nOffset += indexStriding;
113             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
114             lineIndexPtr->index0 = halfVtxNum + i * 2;
115             lineIndexPtr->index1 = halfVtxNum + i * 2 + 1;
116 
117             nOffset += indexStriding;
118             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
119             lineIndexPtr->index0 = halfVtxNum + i * 2;
120             lineIndexPtr->index1 = halfVtxNum + (i + 1) % slices * 2;
121 
122             nOffset += indexStriding;
123             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
124             lineIndexPtr->index0 = halfVtxNum + i * 2 + 1;
125             lineIndexPtr->index1 = halfVtxNum + (i + 1) % slices * 2 + 1;
126         }
127     }
128 
129     return true;
130 }

 


 

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