顶点数据的生成
bool YfBuildArcVertices
(
Yreal radius,
Yreal degree,
Yreal height,
Yuint slices,
Yuint stacks,
YeOriginPose originPose,
Yuint vertexStriding,
Yuint vertexPos,
void* pVerticesBuffer
)
{
if (degree < || degree > || !pVerticesBuffer)
{
return false;
}
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);
} // 赋值
{
Yreal radian = YD_DEGREE_TO_RADIAN(degree);
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;
} Yreal angleY = radian * 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 % slices];
posZ = radiusXZ * pCosList[j % slices]; 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 YfBuildArcTriIndices
(
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 - ) * + slices; 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
{
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
{
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 YfBuildArcWireIndices
(
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 - ) + 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 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++)
{
nOffset = (half + i) * indexStriding;
lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
lineIndexPtr->index0 = ;
lineIndexPtr->index1 = + i;
}
half += slices; for (Yuint j = ; j < stacks - ; j++)
{
for (Yuint i = ; i < slices; i++)
{
nOffset = (half + (j - )*slices + i) * indexStriding;
lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
lineIndexPtr->index0 = + (j - )*slices + i;
lineIndexPtr->index1 = + j*slices + i;
}
} //// 列
//Yuint half = slices * (stacks - 1);
//for (Yuint i = 0; i < slices; i++)
//{
// for (Yuint j = 0; j < stacks - 2; j++)
// {
// nOffset = (half + (i*(stacks - 1) + j)) * indexStriding;
// lineIndexPtr = (YsLineIndex16*)(indexPtr + nOffset);
// if (j == 0)
// {
// lineIndexPtr->index0 = 0;
// }
// else
// {
// lineIndexPtr->index0 = 1 + (j - 1)*slices + i;
// } // lineIndexPtr->index1 = 1 + 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++)
{
nOffset = (half + i) * indexStriding;
lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
lineIndexPtr->index0 = ;
lineIndexPtr->index1 = + i;
}
half += slices; for (Yuint j = ; j < stacks - ; j++)
{
for (Yuint i = ; i < slices; i++)
{
nOffset = (half + (j - )*slices + i) * indexStriding;
lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
lineIndexPtr->index0 = + (j - )*slices + i;
lineIndexPtr->index1 = + j*slices + i;
}
} // 列
//Yuint half = slices * (stacks - 1);
//for (Yuint i = 0; i < slices; i++)
//{
// for (Yuint j = 0; j < stacks - 2; j++)
// {
// nOffset = (half + (i*(stacks - 1) + j)) * indexStriding;
// lineIndexPtr = (YsLineIndex32*)(indexPtr + nOffset);
// if (j == 0)
// {
// lineIndexPtr->index0 = 0;
// }
// else
// {
// lineIndexPtr->index0 = 1 + (j - 1)*slices + i;
// } // lineIndexPtr->index1 = 1 + j*slices + i;
// }
//}
} return true;
}