在opengl中将金字塔更改为四面体
Changing a pyramid to tetrahedron in opengl
首先,我是 openGL 的新手,所以我还不了解所有“基本”知识。我正在一本书中做这个练习,我已经能够画出一个金字塔,但我现在想画一个四面体。但我似乎无法理解它。我总是以奇怪或不完整的形状结束。如果我也没有做任何其他事情,请告诉我。
这是代码。感谢任何帮助。
float pt1 = 1.0f;
float pt2 = 0.0f;
glBegin(GL_TRIANGLES);
// Front
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(pt2, pt1, pt2);
glVertex3f(-pt1, -pt1, pt1);
glVertex3f(pt1, -pt1, pt1);
// Right
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(pt2, pt1, pt2);
glVertex3f(pt1, -pt1, pt1);
glVertex3f(pt1, -pt1, -pt1);
// Back
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(pt2, pt1, pt2);
glVertex3f(pt1, -pt1, -pt1);
glVertex3f(-pt1, -pt1, -pt1);
// Left
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f(pt2, pt1, pt2);
glVertex3f(-pt1, -pt1, -pt1);
glVertex3f(-pt1, -pt1, pt1);
glEnd();
一个Tetrahedron由4个顶点组成:
const float s_8_9 = sqrt(0.8f / 0.9f); // = 0.9428f
const float s_2_9 = sqrt(0.2f / 0.9f); // = 0.4714f
const float s_2_3 = sqrt(0.2f / 0.3f); // = 0.8165f
float vertices[4][3] = {
{ 0.0f, 0.0f, 1.0f },
{ s_8_9, 0.0f, -1.0f / 3.0f},
{-s_2_9, s_2_3, -1.0f / 3.0f},
{-s_2_9, -s_2_3, -1.0f / 3.0f},
};
顶点形成 4 个三角形面:
int indices[4][3] = {{0, 2, 1}, {0, 3, 2}, {1, 3, 0}, {1, 2, 3}};
使用vertices
和indices
画四面体:
glBegin(GL_TRIANGLES);
// Front
glColor3f(1.0f, 0.0f, 0.0f); // Red
for (int i = 0; i < 3; i++)
glVertex3fv(vertices[indices[0][i]]);
// Right
glColor3f(0.0f, 0.0f, 1.0f); // Blue
for (int i = 0; i < 3; i++)
glVertex3fv(vertices[indices[1][i]]);
// Back
glColor3f(0.0f, 1.0f, 0.0f); // Green
for (int i = 0; i < 3; i++)
glVertex3fv(vertices[indices[2][i]]);
// Left
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
for (int i = 0; i < 3; i++)
glVertex3fv(vertices[indices[3][i]]);
首先,我是 openGL 的新手,所以我还不了解所有“基本”知识。我正在一本书中做这个练习,我已经能够画出一个金字塔,但我现在想画一个四面体。但我似乎无法理解它。我总是以奇怪或不完整的形状结束。如果我也没有做任何其他事情,请告诉我。 这是代码。感谢任何帮助。
float pt1 = 1.0f;
float pt2 = 0.0f;
glBegin(GL_TRIANGLES);
// Front
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(pt2, pt1, pt2);
glVertex3f(-pt1, -pt1, pt1);
glVertex3f(pt1, -pt1, pt1);
// Right
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(pt2, pt1, pt2);
glVertex3f(pt1, -pt1, pt1);
glVertex3f(pt1, -pt1, -pt1);
// Back
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(pt2, pt1, pt2);
glVertex3f(pt1, -pt1, -pt1);
glVertex3f(-pt1, -pt1, -pt1);
// Left
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f(pt2, pt1, pt2);
glVertex3f(-pt1, -pt1, -pt1);
glVertex3f(-pt1, -pt1, pt1);
glEnd();
一个Tetrahedron由4个顶点组成:
const float s_8_9 = sqrt(0.8f / 0.9f); // = 0.9428f
const float s_2_9 = sqrt(0.2f / 0.9f); // = 0.4714f
const float s_2_3 = sqrt(0.2f / 0.3f); // = 0.8165f
float vertices[4][3] = {
{ 0.0f, 0.0f, 1.0f },
{ s_8_9, 0.0f, -1.0f / 3.0f},
{-s_2_9, s_2_3, -1.0f / 3.0f},
{-s_2_9, -s_2_3, -1.0f / 3.0f},
};
顶点形成 4 个三角形面:
int indices[4][3] = {{0, 2, 1}, {0, 3, 2}, {1, 3, 0}, {1, 2, 3}};
使用vertices
和indices
画四面体:
glBegin(GL_TRIANGLES);
// Front
glColor3f(1.0f, 0.0f, 0.0f); // Red
for (int i = 0; i < 3; i++)
glVertex3fv(vertices[indices[0][i]]);
// Right
glColor3f(0.0f, 0.0f, 1.0f); // Blue
for (int i = 0; i < 3; i++)
glVertex3fv(vertices[indices[1][i]]);
// Back
glColor3f(0.0f, 1.0f, 0.0f); // Green
for (int i = 0; i < 3; i++)
glVertex3fv(vertices[indices[2][i]]);
// Left
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
for (int i = 0; i < 3; i++)
glVertex3fv(vertices[indices[3][i]]);