【发布时间】:2021-07-24 23:22:22
【问题描述】:
我希望有人可以帮助我了解我缺少什么或做错了什么。我正在尝试制作一个简单的金字塔,但由于某种原因,金字塔的底部不会呈现(构成底部的两个三角形)。
GLfloat verts[] = {
// Vertex Positions // Colors (r,g,b,a)
//front triangle
0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, // Top Right Vertex 0
-1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, // Bottom Right Vertex 1
1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Bottom Left Vertex 2
//right triangle
0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, // Top Right vertex 3
1.0f, -1.0f, 1.0f, 0.5f, 0.5f, 1.0f, 1.0f, // 4
1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 0.5f, 1.0f, // 5
//back triangle
0.0f, 1.0f, 0.0f, 0.2f, 0.2f, 0.5f, 1.0f, // 6
1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // 7
-1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, // 8
//left triangle
0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.5f, // 9
-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // 10
-1.0f, -1.0f, 1.0f, 0.5f, 0.5f, 1.0f, 1.0f // 11
//bottom triangle
- 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, //12
1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, //13
-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, //14
//bottom triangle right
-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, //15
1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, //16
1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, //17
};
以下是为每个三角形列出的索引
GLushort indices[] = {
0, 1, 2, // Triangle 1
3, 4, 5, // Triangle 2
6, 7, 8, // Triangle 3
9, 10, 11, // Triangle 4
12, 13, 14, // Triangle 5
15, 16, 17, // Triangle 6
}
您可以在下面看到我在哪里根据索引的大小定义顶点数并创建 VBO 和缓冲区
const GLuint floatsPerVertex = 3;
const GLuint floatsPerColor = 4;
//mesh.nVertices = sizeof(verts) / (sizeof(verts[0]) * (floatsPerVertex + floatsPerColor));
mesh.nVertices = sizeof(indices) / (sizeof(indices[0]));
glGenVertexArrays(1, &mesh.vao); // we can also generate multiple VAOs or buffers at the same time
glBindVertexArray(mesh.vao);
// Create VBO
glGenBuffers(2, mesh.vbo);
glBindBuffer(GL_ARRAY_BUFFER, mesh.vbo[0]); // Activates the buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); // Sends vertex or coordinate data to the GPU
//buffer for indices
//mesh.nVertices = sizeof(indices) / sizeof(indices[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.vbo[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// Strides between vertex coordinates
GLint stride = sizeof(float) * (floatsPerVertex + floatsPerColor);
// Create Vertex Attribute Pointers
glVertexAttribPointer(0, floatsPerVertex, GL_FLOAT, GL_FALSE, stride, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, floatsPerColor, GL_FLOAT, GL_FALSE, stride, (char*)(sizeof(float) * floatsPerVertex));
glEnableVertexAttribArray(1);
在下面我激活网格来绘制元素
// Activate the VBOs contained within the mesh's VAO
glBindVertexArray(gMesh.vao);
glDrawElements(GL_TRIANGLES, gMesh.nVertices, GL_UNSIGNED_SHORT, NULL); // draws the triangle for pyramids
glBindVertexArray(0);
【问题讨论】:
-
您检查过它们的朝向是否正确吗?
-
@Neil 我相信我有,我不知道是否有更好的方法,但我尝试通过使三角形的顶点更长并在不同的位置来调整三角形的顶点,看看它是否会扭曲我的金字塔,但没有任何改变......我想我在尝试渲染底部的两个三角形时做错了,但我看不到什么。 (我基本上是自学openGL)