博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[15] 星星(Star)图形的生成算法
阅读量:4883 次
发布时间:2019-06-11

本文共 6685 字,大约阅读时间需要 22 分钟。

顶点数据的生成

 

1 bool                        YfBuildStarVertices 2 ( 3     Yreal                   radius,  4     Yreal                   assistRadius, 5     Yreal                   height,  6     Yuint                   slices, 7     YeOriginPose            originPose, 8     Yuint                   vertexStriding,  9     Yuint                   vertexPos,10     void*                   pVerticesBuffer11 )12 {13     if (slices < 2 || !pVerticesBuffer)14     {15         return false;16     }17 18     Yuint numVertices  = (slices*2) + 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 = -height;29     }30     else if (originPose == YE_ORIGIN_POSE_CENTER)31     {32         originOffsetY = -height * 0.5f;33     }34 35     // 顶赋值36     {37         nOffset = 0;   38         curVertexPtr = (YsVector3*)(vertexPtr + nOffset);39         curVertexPtr->x = 0.0f;40         curVertexPtr->y = height + originOffsetY;41         curVertexPtr->z = 0.0f;42     }43 44     // 底赋值45     {46         nOffset = (numVertices - 1) * vertexStriding;   47         curVertexPtr = (YsVector3*)(vertexPtr + nOffset);48         curVertexPtr->x = 0.0f;49         curVertexPtr->y = originOffsetY;50         curVertexPtr->z = 0.0f;51     }52 53     Yreal angleXZ;54     Yreal posX, posZ;        55     Yreal fRadius;56     for (Yuint i = 0; i < 2*slices; i++)57     {58         angleXZ = YD_REAL_TWAIN_PI * i / (slices*2);59         posX = yf_sin(angleXZ);60         posZ = yf_cos(angleXZ);61         fRadius = (i%2 == 0) ? radius : assistRadius;62 63         nOffset = (1 + i) * vertexStriding; 64         curVertexPtr = (YsVector3*)(vertexPtr + nOffset);65         curVertexPtr->x = fRadius * posX;66         curVertexPtr->y = originOffsetY;67         curVertexPtr->z = fRadius * posZ;68     }69 70     return true;71 }

 

三角形索引数据的生成

 

1 bool                        YfBuildStarTriIndices 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 + 2;16     if (indexType == YE_INDEX_16_BIT && 17         numVertices > YD_MAX_UNSIGNED_INT16)18     {19         return false;20     }21     Yuint numTriangles = slices * 4;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 < 2 * 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) % (2*slices);36 37             nOffset += indexStriding;38             triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);39             triIndexPtr->index0 = numVertices - 1;40             triIndexPtr->index1 = 1 + (i + 1) % (2*slices);41             triIndexPtr->index2 = 1 + i;42         }43     }44     else45     {46         YsTriIndex32* triIndexPtr = NULL;47         for (Yuint i = 0; i < 2 * 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) % (2*slices);54 55             nOffset += indexStriding;56             triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);57             triIndexPtr->index0 = numVertices - 1;58             triIndexPtr->index1 = 1 + (i + 1) % (2*slices);59             triIndexPtr->index2 = 1 + i;60         }61     }62 63     return true;64 }

 

线框索引数据的生成

1 bool                        YfBuildStarWireIndices 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 + 2;16     Yuint numLines    = slices * 5;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 < 2 * 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*2);40         }41 42         Yuint half = 4 * slices * indexStriding;43         for (Yuint i = 0; i < slices; i++)44         {45             nOffset = half + i * indexStriding;46             lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);47             lineIndexPtr->index0 = numVertices - 1;48             lineIndexPtr->index1 = 1 + i*2 + 1;49         }50     }51     else52     {53         YsLineIndex32* lineIndexPtr = NULL;54         for (Yuint i = 0; i < 2 * slices; i++)55         {56             nOffset = (i * 2) * indexStriding;57             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);58             lineIndexPtr->index0 = 0;59             lineIndexPtr->index1 = 1 + i;60 61             nOffset += indexStriding;62             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);63             lineIndexPtr->index0 = 1 + i;64             lineIndexPtr->index1 = 1 + (i + 1)%(slices*2);65         }66 67         Yuint half = 4 * slices * indexStriding;68         for (Yuint i = 0; i < slices; i++)69         {70             nOffset = half + i * indexStriding;71             lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);72             lineIndexPtr->index0 = numVertices - 1;73             lineIndexPtr->index1 = 1 + i*2 + 1;74         }75     }76 77     return true;78 }

 

 

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

你可能感兴趣的文章
URL to Blob
查看>>
bzoj 3643: Phi的反函数
查看>>
BizTalk Server 2009 Beta初体验
查看>>
HTML中解决双击会选中文本的问题
查看>>
3.单例模式-singleton
查看>>
说说Vue.js的v-for
查看>>
Java第四次作业
查看>>
屏幕录像软件 (Desktop Screen Recorder)
查看>>
【codevs1069】关押罪犯
查看>>
iOS 设计模式之单例
查看>>
POJ 1664 放苹果
查看>>
Pthon3各平台的安装
查看>>
python编程快速上手之第11章实践项目参考答案(11.11.3)
查看>>
JS 之CLASS类应用
查看>>
一个tga工具
查看>>
64bit CPU 知识 (IA32,IA64,EM64T,AMD64)
查看>>
结构体 枚举
查看>>
srtlen实现以及与sizeof的比较
查看>>
linux+win7双系统重装win7修复grub的办法
查看>>
让应用在横屏模式下启动
查看>>