[16] 螺旋面(Spire)图形的生成算法-LMLPHP

[16] 螺旋面(Spire)图形的生成算法-LMLPHP
顶点数据的生成

 bool                        YfBuildSpireVertices
(
Yreal radius,
Yreal assistRadius,
Yreal height,
Yuint slices,
Yuint stacks,
YeOriginPose originPose,
Yuint vertexStriding,
Yuint vertexPos,
void* pVerticesBuffer
)
{
if (stacks < || slices < || !pVerticesBuffer)
{
return false;
} Yuint numVertices = + stacks * ;
//Yuint numTriangles = stacks * 2; char* vertexPtr = (char*)pVerticesBuffer + vertexPos;
YsVector3* curVertexPtr = NULL;
Yuint nOffset = ; Yreal originOffsetY = 0.0f;
if (originPose == YE_ORIGIN_POSE_TOP)
{
originOffsetY = -height;
}
else if (originPose == YE_ORIGIN_POSE_CENTER)
{
originOffsetY = -height * 0.5f;
} Yreal fAngle = YD_REAL_TWAIN_PI / slices; Yreal angleXZ;
Yreal posX, posZ;
for (Yuint i = ; i <= stacks; i++)
{
angleXZ = i * fAngle;
posX = yf_sin(angleXZ);
posZ = yf_cos(angleXZ);
nOffset = i * * vertexStriding; curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
curVertexPtr->x = assistRadius * posX;
curVertexPtr->y = height * i * 1.0f / stacks + originOffsetY;
curVertexPtr->z = assistRadius * posZ; nOffset += vertexStriding; curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
curVertexPtr->x = radius * posX;
curVertexPtr->y = height * i * 1.0f / stacks + originOffsetY;
curVertexPtr->z = radius * posZ;
} return true;
}

三角形索引数据的生成

 bool                        YfBuildSpireTriIndices
(
Yuint stacks,
YeIndexType indexType,
Yuint indexStriding,
Yuint indexPos,
void* pTriIndicesBuffer
)
{
if (stacks < || !pTriIndicesBuffer)
{
return false;
} Yuint numVertices = + stacks * ;
Yuint numTriangles = stacks * ;
if (indexType == YE_INDEX_16_BIT &&
numVertices > YD_MAX_UNSIGNED_INT16)
{
return false;
} // 索引赋值
char* indexPtr = (char*)pTriIndicesBuffer + indexPos;
Yuint nOffset = ;
if (indexType == YE_INDEX_16_BIT)
{
YsTriIndex16* triIndexPtr = NULL;
for (Yuint i = ; i < stacks; i++)
{
nOffset = * i * indexStriding;
triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
triIndexPtr->index0 = i*;
triIndexPtr->index1 = i* + ;
triIndexPtr->index2 = i* + ; nOffset += indexStriding;
triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
triIndexPtr->index0 = i*;
triIndexPtr->index1 = i* + ;
triIndexPtr->index2 = i* + ;
}
}
else
{
YsTriIndex32* triIndexPtr = NULL;
for (Yuint i = ; i < stacks; i++)
{
nOffset = * i * indexStriding;
triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
triIndexPtr->index0 = i*;
triIndexPtr->index1 = i* + ;
triIndexPtr->index2 = i* + ; nOffset += indexStriding;
triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
triIndexPtr->index0 = i*;
triIndexPtr->index1 = i* + ;
triIndexPtr->index2 = i* + ;
}
} return true;
}

线框索引数据的生成

 bool                        YfBuildSpireWireIndices
(
Yuint stacks,
YeIndexType indexType,
Yuint indexStriding,
Yuint indexPos,
void* pWireIndicesBuffer
)
{
if (stacks < || !pWireIndicesBuffer)
{
return false;
} Yuint numVertices = + stacks * ;
Yuint numLines = + stacks * ;
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 < stacks; i++)
{
nOffset = (i * ) * indexStriding;
lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
lineIndexPtr->index0 = i * ;
lineIndexPtr->index1 = i * + ; nOffset += indexStriding;
lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
lineIndexPtr->index0 = i * + ;
lineIndexPtr->index1 = i * ; nOffset += indexStriding;
lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
lineIndexPtr->index0 = i * + ;
lineIndexPtr->index1 = i * + ;
} nOffset = (stacks * ) * indexStriding;
lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
lineIndexPtr->index0 = stacks * ;
lineIndexPtr->index1 = stacks * + ;
}
else
{
YsLineIndex32* lineIndexPtr = NULL;
for (Yuint i = ; i < stacks; i++)
{
nOffset = (i * ) * indexStriding;
lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
lineIndexPtr->index0 = i * ;
lineIndexPtr->index1 = i * + ; nOffset += indexStriding;
lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
lineIndexPtr->index0 = i * + ;
lineIndexPtr->index1 = i * ; nOffset += indexStriding;
lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
lineIndexPtr->index0 = i * + ;
lineIndexPtr->index1 = i * + ;
} nOffset = (stacks * ) * indexStriding;
lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
lineIndexPtr->index0 = stacks * ;
lineIndexPtr->index1 = stacks * + ;
} return true;
}
05-11 22:44