[英]openGL indices rendering issues
我希望有人能帮助我了解我遗漏了什么或做错了什么。 我正在尝试制作一个简单的金字塔,但由于某种原因,金字塔的底部不会呈现(构成底部的两个三角形)。
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);
发现一个错误:
//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
编号为 16 和 17 的顶点具有相同的值。 您确定应该为三角形打印相同的顶点吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.