[6] 胶囊体(Capsule)图形的生成算法
顶点数据的生成
bool YfBuildCapsuleVertices
(
Yreal radius,
Yreal height,
Yuint slices,
Yuint stacks,
YeOriginPose originPose,
Yuint vertexStriding,
Yuint vertexPos,
void* pVerticesBuffer
)
{
Yuint halfStacks = stacks / ;
if (slices < || halfStacks < || !pVerticesBuffer)
{
return false;
} Yuint numVertices = slices * (halfStacks - ) * + ;
Yuint numHalfVertices = numVertices / ; // 顶点赋值
char* vertexPtr = (char*)pVerticesBuffer + vertexPos;
YsVector3* curVertexPtr = NULL; Yuint nOffset = ; Yreal originOffsetY = 0.0f;
if (originPose == YE_ORIGIN_POSE_TOP)
{
originOffsetY = -radius - height*0.5f;
}
else if (originPose == YE_ORIGIN_POSE_BOTTOM)
{
originOffsetY = radius + height*0.5f;
} Yreal halfHeight = height * 0.5f;
Yreal tallness = radius* + height; // 胶囊体真正高度 // 最高顶点
{
nOffset = * vertexStriding;
curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
curVertexPtr->x = 0.0f;
curVertexPtr->y = halfHeight + radius + originOffsetY;
curVertexPtr->z = 0.0f;
}
// 最底顶点
{
nOffset = numHalfVertices * vertexStriding;
curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
curVertexPtr->x = 0.0f;
curVertexPtr->y = -halfHeight - radius + originOffsetY;
curVertexPtr->z = 0.0f;
} Yreal* pSinList = YD_NEW_ARRAY(Yreal, slices);
Yreal* pCosList = YD_NEW_ARRAY(Yreal, slices);
Yreal angleXZ;
for (Yuint j = ; j < slices; j++)
{
angleXZ = YD_REAL_TWAIN_PI * j / slices;
pSinList[j] = yf_sin(angleXZ);
pCosList[j] = yf_cos(angleXZ);
} for (Yuint i = ; i < halfStacks; i++)
{
Yreal angleY = YD_REAL_HALF_PI * i / (halfStacks - );
Yreal posY = radius * yf_cos(angleY);
Yreal radiusXZ = radius * yf_sin(angleY);
Yreal posX, posZ; for (Yuint j = ; j < slices; j++)
{
posX = radiusXZ * pSinList[j % slices];
posZ = radiusXZ * pCosList[j % slices]; nOffset = ( + (i - ) * slices + j) * vertexStriding;
curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
curVertexPtr->x = posX;
curVertexPtr->y = posY + halfHeight + originOffsetY;
curVertexPtr->z = posZ; nOffset = (numHalfVertices + + (i - ) * slices + j) * vertexStriding;
curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
curVertexPtr->x = posX;
curVertexPtr->y = -posY - halfHeight + originOffsetY;
curVertexPtr->z = posZ;
}
} YD_SAFE_DELETE_ARRAY(pSinList);
YD_SAFE_DELETE_ARRAY(pCosList); return true;
}
三角形索引数据的生成
bool YfBuildCapsuleTriIndices
(
Yuint slices,
Yuint stacks,
YeIndexType indexType,
Yuint indexStriding,
Yuint indexPos,
void* pTriIndicesBuffer
)
{
Yuint halfStacks = stacks / ;
if (slices < || halfStacks < || !pTriIndicesBuffer)
{
return false;
} Yuint numVertices = slices * (halfStacks - ) * + ;
if (indexType == YE_INDEX_16_BIT &&
numVertices > YD_MAX_UNSIGNED_INT16)
{
return false;
}
Yuint numHalfVertices = numVertices / ;
Yuint numTriangles = slices * (halfStacks - ) * ; // 索引赋值
char* indexPtr = (char*)pTriIndicesBuffer + indexPos;
Yuint nOffset = ;
if (indexType == YE_INDEX_16_BIT)
{
YsTriIndex16* triIndexPtr = NULL; for (Yuint i = ; i < halfStacks - ; i++)
{
if (i == ) // 第一层
{
for (Yuint j = ; j < slices; j++)
{
nOffset = j * * indexStriding;
triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
triIndexPtr->index0 = ;
triIndexPtr->index1 = + j;
triIndexPtr->index2 = + (j + )%slices; nOffset += indexStriding;
triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
triIndexPtr->index0 = numHalfVertices;
triIndexPtr->index1 = numHalfVertices + + (j + )%slices;
triIndexPtr->index2 = numHalfVertices + + j;
}
}
else
{
for (Yuint j = ; j < slices; j++)
{
nOffset = ((i - )*slices* + slices* + j*) * indexStriding;
triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
triIndexPtr->index0 = + slices * (i - ) + j;
triIndexPtr->index1 = + slices * i + j;
triIndexPtr->index2 = + slices * (i - ) + (j + )%slices; nOffset += indexStriding;
triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
triIndexPtr->index0 = + slices * (i - ) + (j + )%slices;
triIndexPtr->index1 = + slices * i + j;
triIndexPtr->index2 = + slices * i + (j + )%slices; nOffset += indexStriding;
triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
triIndexPtr->index0 = numHalfVertices + + slices * (i - ) + j;
triIndexPtr->index1 = numHalfVertices + + slices * (i - ) + (j + )%slices;
triIndexPtr->index2 = numHalfVertices + + slices * i + (j + )%slices; nOffset += indexStriding;
triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
triIndexPtr->index0 = numHalfVertices + + slices * (i - ) + j;
triIndexPtr->index1 = numHalfVertices + + slices * i + (j + )%slices;
triIndexPtr->index2 = numHalfVertices + + slices * i + j;
}
}
} // 连接两个半球
for (Yuint j = ; j < slices; j++)
{
nOffset = ((halfStacks - )*slices* + slices* + j*) * indexStriding;
triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
triIndexPtr->index0 = + slices * (halfStacks - ) + j;
triIndexPtr->index1 = numHalfVertices + + slices * (halfStacks - ) + j;
triIndexPtr->index2 = + slices * (halfStacks - ) + (j + )%slices; nOffset += indexStriding;
triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
triIndexPtr->index0 = + slices * (halfStacks - ) + (j + )%slices;
triIndexPtr->index1 = numHalfVertices + + slices * (halfStacks - ) + j;
triIndexPtr->index2 = numHalfVertices + + slices * (halfStacks - ) + (j + )%slices;
}
}
else
{
YsTriIndex32* triIndexPtr = NULL; for (Yuint i = ; i < halfStacks - ; i++)
{
if (i == ) // 第一层
{
for (Yuint j = ; j < slices; j++)
{
nOffset = j * * indexStriding;
triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
triIndexPtr->index0 = ;
triIndexPtr->index1 = + j;
triIndexPtr->index2 = + (j + )%slices; nOffset += indexStriding;
triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
triIndexPtr->index0 = numHalfVertices;
triIndexPtr->index1 = numHalfVertices + + (j + )%slices;
triIndexPtr->index2 = numHalfVertices + (j + )%slices;
}
}
else
{
for (Yuint j = ; j < slices; j++)
{
nOffset = ((i - )*slices* + slices* + j*) * indexStriding;
triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
triIndexPtr->index0 = + slices * (i - ) + j;
triIndexPtr->index1 = + slices * i + j;
triIndexPtr->index2 = + slices * (i - ) + (j + )%slices; nOffset += indexStriding;
triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
triIndexPtr->index0 = + slices * (i - ) + (j + )%slices;
triIndexPtr->index1 = + slices * i + j;
triIndexPtr->index2 = + slices * i + j + ; nOffset += indexStriding;
triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
triIndexPtr->index0 = numHalfVertices + + slices * (i - ) + j;
triIndexPtr->index1 = numHalfVertices + + slices * (i - ) + (j + )%slices;
triIndexPtr->index2 = numHalfVertices + + slices * i + (j + )%slices; nOffset += indexStriding;
triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
triIndexPtr->index0 = numHalfVertices + + slices * (i - ) + j;
triIndexPtr->index1 = numHalfVertices + + slices * i + (j + )%slices;
triIndexPtr->index2 = numHalfVertices + + slices * i + j;
}
}
} // 连接两个半球
for (Yuint j = ; j < slices; j++)
{
nOffset = ((halfStacks - )*slices* + slices* + j*) * indexStriding;
triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
triIndexPtr->index0 = + slices * (halfStacks - ) + j;
triIndexPtr->index1 = numHalfVertices + + slices * (halfStacks - ) + j;
triIndexPtr->index2 = + slices * (halfStacks - ) + (j + )%slices; nOffset += indexStriding;
triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
triIndexPtr->index0 = + slices * (halfStacks - ) + (j + )%slices;
triIndexPtr->index1 = numHalfVertices + + slices * (halfStacks - ) + j;
triIndexPtr->index2 = numHalfVertices + + slices * (halfStacks - ) + (j + )%slices;
}
} return true;
}
线框索引数据的生成
bool YfBuildCapsuleWireIndices
(
Yuint slices,
Yuint stacks,
YeIndexType indexType,
Yuint indexStriding,
Yuint indexPos,
void* pWireIndicesBuffer
)
{
Yuint halfStacks = stacks / ;
if (slices < || halfStacks < || !pWireIndicesBuffer)
{
return false;
} Yuint numVertices = slices * (halfStacks - ) * + ;
Yuint numLines = slices * (halfStacks - ) * + slices * ( * halfStacks - );
Yuint numHalfVertices = numVertices / ;
if (indexType == YE_INDEX_16_BIT &&
numVertices > YD_MAX_UNSIGNED_INT16)
{
return false;
} // 索引赋值
char* indexPtr = (char*)pWireIndicesBuffer + indexPos;
Yuint nOffset = ;
if (indexType == YE_INDEX_16_BIT)
{
YsLineIndex16* lineIndexPtr = NULL;
for (Yuint i = ; i < halfStacks; i++)
{
for (Yuint j = ; j < slices; j++)
{
nOffset = ((i - )*slices + j) * * indexStriding;
lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
lineIndexPtr->index0 = + (i - )*slices + j;
lineIndexPtr->index1 = + (i - )*slices + (j + )%slices; nOffset += indexStriding;
lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
lineIndexPtr->index0 = numHalfVertices + + (i - )*slices + j;
lineIndexPtr->index1 = numHalfVertices + + (i - )*slices + (j + )%slices;
}
} Yuint half = (halfStacks - ) * slices *;
for (Yuint j = ; j < slices; j++)
{
for (Yuint i = ; i < halfStacks - ; i++)
{
if (i == )
{
nOffset = (half + (halfStacks* - ) * j) * indexStriding;
lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
lineIndexPtr->index0 = ;
lineIndexPtr->index1 = + j; nOffset += indexStriding;
lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
lineIndexPtr->index0 = numHalfVertices;
lineIndexPtr->index1 = numHalfVertices + + j;
}
else
{
nOffset = (half + (halfStacks* - ) * j + i * ) * indexStriding;
lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
lineIndexPtr->index0 = + slices*(i - ) + j;
lineIndexPtr->index1 = + slices*i + j; nOffset += indexStriding;
lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
lineIndexPtr->index0 = numHalfVertices + + slices*(i - ) + j;
lineIndexPtr->index1 = numHalfVertices + + slices*i + j;
}
} nOffset = (half + (halfStacks* - ) * (j + ) - ) * indexStriding;
lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
lineIndexPtr->index0 = + slices * (halfStacks - ) + j;
lineIndexPtr->index1 = numHalfVertices + + slices * (halfStacks - ) + j;
}
}
else
{
YsLineIndex32* lineIndexPtr = NULL;
for (Yuint i= ; i < slices; i++)
{
YsLineIndex32* lineIndexPtr = NULL;
for (Yuint i = ; i < halfStacks; i++)
{
for (Yuint j = ; j < slices; j++)
{
nOffset = ((i - )*slices + j) * * indexStriding;
lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
lineIndexPtr->index0 = + (i - )*slices + j;
lineIndexPtr->index1 = + (i - )*slices + (j + )%slices; nOffset += indexStriding;
lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
lineIndexPtr->index0 = numHalfVertices + + (i - )*slices + j;
lineIndexPtr->index1 = numHalfVertices + + (i - )*slices + (j + )%slices;
}
} Yuint half = (halfStacks - ) * slices *;
for (Yuint j = ; j < slices; j++)
{
for (Yuint i = ; i < halfStacks - ; i++)
{
if (i == )
{
nOffset = (half + (halfStacks* - ) * j) * indexStriding;
lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
lineIndexPtr->index0 = ;
lineIndexPtr->index1 = + j; nOffset += indexStriding;
lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
lineIndexPtr->index0 = numHalfVertices;
lineIndexPtr->index1 = numHalfVertices + + j;
}
else
{
nOffset = (half + (halfStacks* - ) * j + i * ) * indexStriding;
lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
lineIndexPtr->index0 = + slices*(i - ) + j;
lineIndexPtr->index1 = + slices*i + j; nOffset += indexStriding;
lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
lineIndexPtr->index0 = numHalfVertices + + slices*(i - ) + j;
lineIndexPtr->index1 = numHalfVertices + + slices*i + j;
}
} nOffset = (half + (halfStacks* - ) * (j + ) - ) * indexStriding;
lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
lineIndexPtr->index0 = + slices * (halfStacks - ) + j;
lineIndexPtr->index1 = numHalfVertices + + slices * (halfStacks - ) + j;
}
}
} return true;
}
[6] 胶囊体(Capsule)图形的生成算法的更多相关文章
- [20] 鼓状物(Drum)图形的生成算法
顶点数据的生成 bool YfBuildDrumVertices ( Yreal radius, Yreal assistRadius, Yuint slices, Yuint stacks, YeO ...
- [17] 楼梯(Stairs)图形的生成算法
感觉这图形怎么看怎么像搓衣板. 顶点数据的生成 bool YfBuildStairsVertices ( Yreal width, Yreal length, Yreal height, Yuint ...
- [19] 半球形(Hemisphere)图形的生成算法
顶点数据的生成 bool YfBuildHemisphereVertices ( Yreal radius, Yuint slices, Yuint stacks, YeOriginPose orig ...
- [18] 螺旋楼梯(Spiral Stairs)图形的生成算法
顶点数据的生成 bool YfBuildSpiralStairsVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint sli ...
- [16] 螺旋面(Spire)图形的生成算法
顶点数据的生成 bool YfBuildSpireVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint slices, Yu ...
- [15] 星星(Star)图形的生成算法
顶点数据的生成 bool YfBuildStarVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint slices, YeO ...
- [14] 齿轮(Gear Wheel)图形的生成算法
顶点数据的生成 bool YfBuildGearwheelVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint slices ...
- [13] 弧面(Arc)图形的生成算法
顶点数据的生成 bool YfBuildArcVertices ( Yreal radius, Yreal degree, Yreal height, Yuint slices, Yuint stac ...
- [12] 扇形体(Fan)图形的生成算法
顶点数据的生成 bool YfBuildFunVertices ( Yreal radius, Yreal degree, Yreal height, Yuint slices, YeOriginPo ...
随机推荐
- html5+css3 手机屏幕的适配css
*{ margin:0;padding:0;outline:0}a{ text-decoration:none}body,html{ font-size:20px;font-family:'Micro ...
- 数据挖掘算法:关联分析二(Apriori)
二.Apriori算法 上文说到,大多数关联规则挖掘算法通常采用的策略是分解为两步: 频繁项集产生,其目标是发现满足具有最小支持度阈值的所有项集,称为频繁项集(frequent itemset). 规 ...
- 洛谷P3521 [POI2011]ROT-Tree Rotation [线段树合并]
题目传送门 Tree Rotation 题目描述 Byteasar the gardener is growing a rare tree called Rotatus Informatikus. I ...
- odoo基础数据加载
odoo 基础数据加载 这里介绍的odoo基础数据加载分两种方式,一种是演示数据加载,一种是默认数据加载,下面就是详细介绍 首先,当然是创建一个date文件夹 项目目录,右键自定义一个文件夹 XML数 ...
- 解决mongo 端口占用问题
在打开mongod之后如果不用了就按ctrl + c ,就不会出现以下的问题了 执行mongod报错 mongod 2016-08-03T14:31:15.691+0800 I CONTROL [in ...
- Linux设备驱动模型(sysfs)
<总线模型概述> 随着技术的发展,系统的拓扑结构也越来越复杂,对热插拔.跨平台移植性的要求越来越高,从Linux2.6内核开始提供全新的设备模型.将所有的驱动挂载到计算机的总线上(比如US ...
- alpha冲刺——代码规范、冲刺任务与计划(追光的人)
代码规范 代码规范整合了自身项目实践还有诸多好的大公司的代码规范.如阿里巴巴开发手册.华为Java规范.W3C前端规范等. 由于内容过于详细和细致,为了方便查看,将其放置在了showDoc网站上(同时 ...
- CentOS 7 下编译安装lnmp之MySQL篇详解
一.安装环境 宿主机=> win7,虚拟机 centos => 系统版本:centos-release-7-5.1804.el7.centos.x86_64 二.MySQL下载 MySQL ...
- PostgreSQL修改数据库目录/数据库目录迁移
说明:以9+版本为例,10+的版本只要把目录替换一下即可.迁移目录肯定是要停服的! 1.在数据库软件安装之后,初始化数据库时候,可以指定初始化时创建的数据库的默认文件路径 /usr/local/pgs ...
- FolderSync文件夹同步
FolderSync是一款支持各大国外网盘同步的软件,目前支持 SkyDrive, Dropbox, SugarSync, Ubuntu One, Box.net, LiveDrive, HiDriv ...