[9] 圆环(Ring)图形的生成算法
2013-11-09 08:38
369 查看
顶点数据的生成
bool YfBuildRingVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint slices, Yuint stacks, YeOriginPose originPose, Yuint vertexStriding, Yuint vertexPos, void* pVerticesBuffer ) { if (slices < 2 || stacks < 3 || !pVerticesBuffer) { return false; } Yuint numVertices = slices * stacks; Yuint numTriangles = slices * stacks * 2; char* vertexPtr = (char*)pVerticesBuffer + vertexPos; YsVector3* curVertexPtr = NULL; Yuint nOffset = 0; Yreal halfHeight = height * 0.5f; Yreal originOffsetY = 0.0f; if (originPose == YE_ORIGIN_POSE_TOP) { originOffsetY = -halfHeight; } else if (originPose == YE_ORIGIN_POSE_BOTTOM) { originOffsetY = halfHeight; } Yreal angle, s, c; YsVector3* initVerticesPtr = new YsVector3[slices + 1]; for (Yuint j = 0; j < slices; j++) { angle = YD_REAL_TWAIN_PI * j / slices; s = yf_sin(angle); c = yf_cos(angle); initVerticesPtr[j].x = radius + assistRadius*s; initVerticesPtr[j].y = halfHeight*c + originOffsetY; initVerticesPtr[j].z = 0.0f; } for (Yuint i = 0; i < stacks; i++) { angle = YD_REAL_TWAIN_PI * i / stacks; s = yf_sin(angle); c = yf_cos(angle); for (Yuint j = 0; j < slices; j++) { nOffset = (i * slices + j) * vertexStriding; curVertexPtr = (YsVector3*)(vertexPtr + nOffset); curVertexPtr->x = initVerticesPtr[j].x * s; curVertexPtr->y = initVerticesPtr[j].y; curVertexPtr->z = initVerticesPtr[j].x * c; } } YD_SAFE_DELETE_ARRAY(initVerticesPtr); return true; }
三角形索引数据的生成
bool YfBuildRingTriIndices ( Yuint slices, Yuint stacks, YeIndexType indexType, Yuint indexStriding, Yuint indexPos, void* pTriIndicesBuffer ) { if (slices < 2 || stacks < 3 || !pTriIndicesBuffer) { return false; } Yuint numVertices = slices * stacks; Yuint numTriangles = slices * stacks * 2; if (indexType == YE_INDEX_16_BIT && numVertices > YD_MAX_UNSIGNED_INT16) { return false; } // 索引赋值 char* indexPtr = (char*)pTriIndicesBuffer + indexPos; Yuint nOffset = 0; if (indexType == YE_INDEX_16_BIT) { YsTriIndex16* triIndexPtr = NULL; for (Yuint i = 0; i < stacks; i++) { for (Yuint j = 0; j < slices; j++) { nOffset = (i*slices + j)*2 * indexStriding; triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset); triIndexPtr->index0 = slices * i + j; triIndexPtr->index1 = slices * i + (j + 1)%slices; triIndexPtr->index2 = slices * ((i + 1) % stacks) + j; nOffset += indexStriding; triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset); triIndexPtr->index0 = slices * i + (j + 1)%slices; triIndexPtr->index1 = slices * ((i + 1) % stacks) + (j + 1)%slices; triIndexPtr->index2 = slices * ((i + 1) % stacks) + j; } } } else { YsTriIndex32* triIndexPtr = NULL; for (Yuint i = 0; i < stacks; i++) { for (Yuint j = 0; j < slices; j++) { nOffset = (i*slices + j)*2 * indexStriding; triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset); triIndexPtr->index0 = slices * i + j; triIndexPtr->index1 = slices * i + (j + 1)%slices; triIndexPtr->index2 = slices * ((i + 1) % stacks) + j; nOffset += indexStriding; triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset); triIndexPtr->index0 = slices * i + (j + 1)%slices; triIndexPtr->index1 = slices * ((i + 1) % stacks) + (j + 1)%slices; triIndexPtr->index2 = slices * ((i + 1) % stacks) + j; } } } return true; }
线框索引数据的生成
bool YfBuildRingWireIndices ( Yuint slices, Yuint stacks, YeIndexType indexType, Yuint indexStriding, Yuint indexPos, void* pWireIndicesBuffer ) { if (slices < 2 || !pWireIndicesBuffer) { return false; } Yuint numVertices = slices * stacks; Yuint numLines = slices * stacks * 2; if (indexType == YE_INDEX_16_BIT && numVertices > YD_MAX_UNSIGNED_INT16) { return false; } // 索引赋值 char* indexPtr = (char*)pWireIndicesBuffer + indexPos; Yuint nOffset = 0; if (indexType == YE_INDEX_16_BIT) { YsLineIndex16* lineIndexPtr = NULL; for (Yuint i = 0; i < stacks; i++) { for (Yuint j = 0; j < slices; j++) { nOffset = (i*slices + j) * 2 * indexStriding; lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset); lineIndexPtr->index0 = slices * i + j; lineIndexPtr->index1 = slices * i + (j + 1)%slices; nOffset += indexStriding; lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset); lineIndexPtr->index0 = slices * i + j; lineIndexPtr->index1 = slices * ((i + 1) % stacks) + j; } } } else { YsLineIndex32* lineIndexPtr = NULL; for (Yuint i = 0; i < stacks; i++) { for (Yuint j = 0; j < slices; j++) { nOffset = (i*slices + j) * 2 * indexStriding; lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset); lineIndexPtr->index0 = slices * i + j; lineIndexPtr->index1 = slices * i + (j + 1)%slices; nOffset += indexStriding; lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset); lineIndexPtr->index0 = slices * i + j; lineIndexPtr->index1 = slices * ((i + 1) % stacks) + j; } } } return true; }
相关文章推荐
- [9] 圆环(Ring)图形的生成算法
- [9] 圆环(Ring)图形的生成算法
- 基本图形生成算法
- [10] 圆管(Pipe)图形的生成算法
- [1] 平面(Plane)图形的生成算法
- [17] 楼梯(Stairs)图形的生成算法
- [11] 楔形体(Wedge)图形的生成算法
- 常用直线图形生成算法(一)
- [12] 扇形体(Fan)图形的生成算法
- [2] 立方体(Box)图形的生成算法
- [12] 扇形体(Fan)图形的生成算法
- [13] 弧面(Arc)图形的生成算法
- 【计算机图形学】基本图形元素:直线的生成算法
- 【计算机图形学】基本图形元素:圆的生成算法
- [14] 齿轮(Gear Wheel)图形的生成算法
- [11] 楔形体(Wedge)图形的生成算法
- [15] 星星(Star)图形的生成算法
- [19] 半球形(Hemisphere)图形的生成算法
- [4] 圆锥(Cone)图形的生成算法
- 基本图形生成算法注记