您的位置:首页 > 其它

[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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: