[3] 球(Sphere)图形的生成算法-LMLPHP

[3] 球(Sphere)图形的生成算法-LMLPHP
顶点数据的生成

 bool                        YfBuildSphereVertices
(
Yreal radius,
Yuint slices,
Yuint stacks,
YeOriginPose originPose,
Yuint vertexStriding,
Yuint vertexPos,
void* pVerticesBuffer
)
{
if (slices < || stacks < || !pVerticesBuffer)
{
return false;
} Yuint numVertices = slices * (stacks - ) + ; // 顶点赋值
char* vertexPtr = (char*)pVerticesBuffer + vertexPos;
YsVector3* curVertexPtr = NULL;
Yuint nOffset = ; Yreal originOffsetY = 0.0f;
if (originPose == YE_ORIGIN_POSE_TOP)
{
originOffsetY = -radius;
}
else if (originPose == YE_ORIGIN_POSE_BOTTOM)
{
originOffsetY = radius;
} 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 < stacks; i++)
{
if (i == ) // 第一个顶点
{
nOffset = ;
curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
curVertexPtr->x = 0.0f;
curVertexPtr->y = radius + originOffsetY;
curVertexPtr->z = 0.0f;
continue;
}
else if (i == stacks - ) // 最后一个顶点
{
nOffset = (numVertices - ) * vertexStriding;
curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
curVertexPtr->x = 0.0f;
curVertexPtr->y = -radius + originOffsetY;
curVertexPtr->z = 0.0f;
continue;
} Yreal angleY = YD_REAL_PI * i / (stacks - );
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];
posZ = radiusXZ * pCosList[j];
nOffset = ((i - ) * slices + j + ) * vertexStriding;
curVertexPtr = (YsVector3*)(vertexPtr + nOffset);
curVertexPtr->x = posX;
curVertexPtr->y = posY + originOffsetY;
curVertexPtr->z = posZ;
}
}
} YD_SAFE_DELETE_ARRAY(pSinList);
YD_SAFE_DELETE_ARRAY(pCosList); return true;
}

三角形索引数据的生成

 bool                        YfBuildSphereTriIndices
(
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++)
{
if (i == ) // 第一层
{
for (Yuint j = ; j < slices; j++)
{
nOffset = j * indexStriding;
triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
triIndexPtr->index0 = ;
triIndexPtr->index1 = + j;
triIndexPtr->index2 = + (j + )%slices;
}
}
else if (i == stacks - ) // 最后一层
{
for (Yuint j = ; j < slices; j++)
{
nOffset = (numTriangles - slices + j) * indexStriding;
triIndexPtr = (YsTriIndex16*)(indexPtr + nOffset);
triIndexPtr->index0 = numVertices - ;
triIndexPtr->index1 = numVertices - - slices + (j + )%slices;
triIndexPtr->index2 = numVertices - - slices + 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;
}
}
}
}
else
{
YsTriIndex32* triIndexPtr = NULL; // 赋值
for (Yuint i = ; i < stacks - ; 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;
}
}
else if (i == stacks - ) // 最后一层
{
for (Yuint j = ; j < slices; j++)
{
nOffset = (numTriangles - slices + j) * indexStriding;
triIndexPtr = (YsTriIndex32*)(indexPtr + nOffset);
triIndexPtr->index0 = numVertices - ;
triIndexPtr->index1 = numVertices - - slices + (j + )%slices;
triIndexPtr->index2 = numVertices - - slices + j;
}
}
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 + )%slices;
}
}
}
} return true;
}

线框索引数据的生成

 bool                        YfBuildSphereWireIndices
(
Yuint slices,
Yuint stacks,
YeIndexType indexType,
Yuint indexStriding,
Yuint indexPos,
void* pWireIndicesBuffer
)
{
if (slices < || stacks < || !pWireIndicesBuffer)
{
return false;
} Yuint numVertices = slices * (stacks - ) + ;
if (indexType == YE_INDEX_16_BIT &&
numVertices > YD_MAX_UNSIGNED_INT16)
{
return false;
}
Yuint numLines = slices * (stacks - ) + slices * (stacks - ); char* indexPtr = (char*)pWireIndicesBuffer + indexPos;
Yuint nOffset = ; if (indexType == YE_INDEX_16_BIT)
{
YsLineIndex16* lineIndexPtr = NULL; // 行
for (Yuint j = ; j < stacks - ; j++)
{
for (Yuint i = ; i < slices; i++)
{
nOffset = ((j - )*slices + i) * indexStriding;
lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
lineIndexPtr->index0 = + (j - )*slices + i;
lineIndexPtr->index1 = + (j - )*slices + (i + )%slices;
}
} // 列
Yuint half = slices * (stacks - );
for (Yuint i = ; i < slices; i++)
{
for (Yuint j = ; j < stacks - ; j++)
{
nOffset = (half + (i*(stacks - ) + j)) * indexStriding;
lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
if (j == )
{
lineIndexPtr->index0 = ;
}
else
{
lineIndexPtr->index0 = + (j - )*slices + i;
}
if (j == stacks - )
{
lineIndexPtr->index1 = numVertices - ;
}
else
{
lineIndexPtr->index1 = + j*slices + i;
}
}
}
}
else
{
YsLineIndex32* lineIndexPtr = NULL; // 行
for (Yuint j = ; j < stacks - ; j++)
{
for (Yuint i = ; i < slices; i++)
{
nOffset = ((j - )*slices + i) * indexStriding;
lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
lineIndexPtr->index0 = + j*slices + i;
lineIndexPtr->index1 = + j*slices + (i + )%slices;
}
} // 列
Yuint half = slices * (stacks - );
for (Yuint i = ; i < slices; i++)
{
for (Yuint j = ; j < stacks - ; j++)
{
nOffset = (half + (i*(stacks - ) + j)) * indexStriding;
lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
if (j == )
{
lineIndexPtr->index0 = ;
}
else
{
lineIndexPtr->index0 = + (j - )*slices + i;
}
if (j == stacks - )
{
lineIndexPtr->index1 = numVertices - ;
}
else
{
lineIndexPtr->index1 = + j*slices + i;
}
}
}
} return true;
}
05-11 19:53