Я пытаюсь создать базовую c сферу с DirectX 11. См. Код ниже. Но некоторые точки имеют неправильное значение z. Я исправляю их в конце кода вручную.
Я скорее не исправляю их вручную, но математика работает правильно. Я не вижу, что не так. Кто-нибудь еще может это увидеть?
void CreateSphere(std::vector<EJM_DGE::Vertex_Color>& Vertex_Color_Vector_Out, std::vector<unsigned short>& us_Indices_Vector_Out)
{
int int_Factor = 10;
float flt_Radius = 10.0f;
float flt_XT1, flt_YT1, flt_ZT1, flt_XT2, flt_YT2, flt_ZT2;
int iPos = 0;
UINT ui_ShapeCount = int_Factor * int_Factor * 2;
Vertex_Color_Vector_Out.clear();
us_Indices_Vector_Out.clear();
std::vector<float> flt_Vector_PosX;
std::vector<float> flt_Vector_PosY;
std::vector<float> flt_Vector_PosZ;
for (int i = 0; i < int_Factor * int_Factor; i++)
{
flt_Vector_PosX.push_back(0.0f);
flt_Vector_PosY.push_back(0.0f);
flt_Vector_PosZ.push_back(0.0f);
}
for (DWORD j = 0; j < int_Factor; j++)
{
FLOAT theta = (DirectX::XM_PI * j) / (int_Factor);
for (DWORD i = 0; i < int_Factor; i++)
{
iPos = j * int_Factor + i;
FLOAT phi = (2 * DirectX::XM_PI * i) / (int_Factor);
flt_Vector_PosX[iPos] = ((float)(sin(theta) * cos(phi))) * flt_Radius;
flt_Vector_PosY[iPos] = ((float)(sin(theta) * sin(phi))) * flt_Radius;
flt_Vector_PosZ[iPos] = ((float)(cos(theta))) * flt_Radius;
}
}
int iNext = 0;
for (DWORD j = 0; j < int_Factor; j++)
{
for (DWORD i = 0; i < int_Factor; i++)
{
if (i == int_Factor - 1)
iNext = 0;
else iNext = i + 1;
iPos = (j * int_Factor * 6) + (i * 6);
Vertex_Color_Vector_Out.push_back(EJM_DGE::Vertex_Color{ flt_Vector_PosX[j * int_Factor + i], flt_Vector_PosY[j * int_Factor + i], flt_Vector_PosZ[j * int_Factor + i],
0.0f, 0.0f, 0.0f, 1.0f }); // = color.
us_Indices_Vector_Out.push_back(iPos);
flt_XT1 = flt_Vector_PosX[j * int_Factor + iNext];
flt_YT1 = flt_Vector_PosY[j * int_Factor + iNext];
flt_ZT1 = flt_Vector_PosZ[j * int_Factor + iNext];
Vertex_Color_Vector_Out.push_back(EJM_DGE::Vertex_Color{ flt_XT1, flt_YT1, flt_ZT1,
0.0f, 0.0f, 0.0f, 1.0f }); // = color.
us_Indices_Vector_Out.push_back(iPos + 1);
if (j != int_Factor - 1)
{
flt_XT2 = flt_Vector_PosX[((j + 1) * int_Factor) + i];
flt_YT2 = flt_Vector_PosY[((j + 1) * int_Factor) + i];
flt_ZT2 = flt_Vector_PosZ[((j + 1) * int_Factor) + i];
}
else
{
flt_XT2 = 0;
flt_YT2 = 0;
flt_ZT2 = -1;
}
Vertex_Color_Vector_Out.push_back(EJM_DGE::Vertex_Color{ flt_XT2, flt_YT2, flt_ZT2,
0.0f, 0.0f, 0.0f, 1.0f }); // = color.
us_Indices_Vector_Out.push_back(iPos + 2);
Vertex_Color_Vector_Out.push_back(EJM_DGE::Vertex_Color{ flt_XT2, flt_YT2, flt_ZT2,
0.0f, 0.0f, 0.0f, 1.0f }); // = color.
us_Indices_Vector_Out.push_back(iPos + 3);
Vertex_Color_Vector_Out.push_back(EJM_DGE::Vertex_Color{ flt_XT1, flt_YT1, flt_ZT1,
0.0f, 0.0f, 0.0f, 1.0f }); // = color.
us_Indices_Vector_Out.push_back(iPos + 4);
if (j != int_Factor - 1)
{
Vertex_Color_Vector_Out.push_back(EJM_DGE::Vertex_Color{ flt_Vector_PosX[((j + 1) * int_Factor) + iNext], flt_Vector_PosY[((j + 1) * int_Factor) + iNext], flt_Vector_PosZ[((j + 1) * int_Factor) + iNext],
0.0f, 0.0f, 0.0f, 1.0f }); // = color.
us_Indices_Vector_Out.push_back(iPos + 5);
}
else
{
Vertex_Color_Vector_Out.push_back(EJM_DGE::Vertex_Color{ 0, 0, -1,
0.0f, 0.0f, 0.0f, 1.0f }); // = color.
us_Indices_Vector_Out.push_back(iPos + 5);
}
}
}
// Some z values are incorrect. They are near the origin.
for (int i = 542; i <= 599; i += 3)
{
Vertex_Color_Vector_Out.at(i).z = -flt_Radius;
}
for (int i = 543; i <= 599; i += 6)
{
Vertex_Color_Vector_Out.at(i).z = -flt_Radius;
Vertex_Color_Vector_Out.at(i).z = -flt_Radius;
Vertex_Color_Vector_Out.at(i).z = -flt_Radius;
}
}
Спасибо за любую информацию, которую вы можете предоставить.