[9] 圆环(Ring)图形的生成算法-LMLPHP

[9] 圆环(Ring)图形的生成算法-LMLPHP
顶点数据的生成

 bool                        YfBuildRingVertices
(
Yreal radius,
Yreal assistRadius,
Yreal height,
Yuint slices,
Yuint stacks,
YeOriginPose originPose,
Yuint vertexStriding,
Yuint vertexPos,
void* pVerticesBuffer
)
{
if (slices < || stacks < || !pVerticesBuffer)
{
return false;
} Yuint numVertices = slices * stacks;
Yuint numTriangles = slices * stacks * ; char* vertexPtr = (char*)pVerticesBuffer + vertexPos;
YsVector3* curVertexPtr = NULL;
Yuint nOffset = ; 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 + ];
for (Yuint j = ; 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 = ; i < stacks; i++)
{
angle = YD_REAL_TWAIN_PI * i / stacks;
s = yf_sin(angle);
c = yf_cos(angle); for (Yuint j = ; 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 < || stacks < || !pTriIndicesBuffer)
{
return false;
} Yuint numVertices = slices * stacks;
Yuint numTriangles = slices * 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++)
{
for (Yuint j = ; j < slices; j++)
{
nOffset = (i*slices + j)* * indexStriding;
triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
triIndexPtr->index0 = slices * i + j;
triIndexPtr->index1 = slices * i + (j + )%slices;
triIndexPtr->index2 = slices * ((i + ) % stacks) + j; nOffset += indexStriding;
triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
triIndexPtr->index0 = slices * i + (j + )%slices;
triIndexPtr->index1 = slices * ((i + ) % stacks) + (j + )%slices;
triIndexPtr->index2 = slices * ((i + ) % stacks) + j;
}
}
}
else
{
YsTriIndex32* triIndexPtr = NULL;
for (Yuint i = ; i < stacks; i++)
{
for (Yuint j = ; j < slices; j++)
{
nOffset = (i*slices + j)* * indexStriding;
triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
triIndexPtr->index0 = slices * i + j;
triIndexPtr->index1 = slices * i + (j + )%slices;
triIndexPtr->index2 = slices * ((i + ) % stacks) + j; nOffset += indexStriding;
triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
triIndexPtr->index0 = slices * i + (j + )%slices;
triIndexPtr->index1 = slices * ((i + ) % stacks) + (j + )%slices;
triIndexPtr->index2 = slices * ((i + ) % stacks) + j;
}
}
} return true;
}

线框索引数据的生成

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