[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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?