使用 OpenGL 3 绘制 3D 立方体的边缘时出现问题
Having issues drawing 3D cube's edges with OpenGL 3
我正在尝试使用 OpenGL(4.3 核心配置文件)绘制 3D 立方体的顶点(仅边缘),我知道 glPolygonMode
函数,但我不想绘制中间对角线。我这样声明我的顶点和索引:
struct Vertex {
glm::vec3 pos;
glm::vec3 color;
glm::vec3 normal;
glm::vec2 uv;
};
Vertex vertices[8];
// Front vertices
vertices[0].pos = glm::vec3(-0.5f, -0.5f, +0.5f);
vertices[1].pos = glm::vec3(+0.5f, -0.5f, +0.5f);
vertices[2].pos = glm::vec3(+0.5f, +0.5f, +0.5f);
vertices[3].pos = glm::vec3(-0.5f, +0.5f, +0.5f);
// Back vertices
vertices[4].pos = glm::vec3(-0.5f, -0.5f, -0.5f);
vertices[5].pos = glm::vec3(+0.5f, -0.5f, -0.5f);
vertices[6].pos = glm::vec3(+0.5f, +0.5f, -0.5f);
vertices[7].pos = glm::vec3(-0.5f, +0.5f, -0.5f);
GLuint indices[36] = {
0, 1, 2, 2, 3, 0, // Front
1, 5, 6, 6, 2, 1, // Right
7, 6, 5, 5, 4, 7, // Back
4, 0, 3, 3, 7, 4, // Left
4, 5, 1, 1, 0, 4, // Bottom
3, 2, 6, 6, 7, 3 // Top
};
我的缓冲区已相应更新:
// Bind Vertex Array
glBindVertexArray(_VAO);
// Bind VBO to GL_ARRAY_BUFFER type so that all calls to GL_ARRAY_BUFFER use VBO
glBindBuffer(GL_ARRAY_BUFFER, _VBO);
// Upload vertices to VBO
glBufferData(GL_ARRAY_BUFFER, verticesNb * sizeof(Vertex), vertices, GL_STATIC_DRAW);
// Bind EBO to GL_ARRAY_BUFFER type so that all calls to GL_ARRAY_BUFFER use EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _EBO);
// Updload indices to EBO
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesNb * sizeof(GLuint), indices, GL_STATIC_DRAW);
我正在使用 glDrawElements(GL_LINES, 36, GL_UNSIGNED_INT, 0);
绘制我的立方体的边缘,但由于某些原因它没有绘制一些边缘,我不明白为什么。
如果我使用 GL_TRIANGLES,尽管当我想以填充模式渲染我的 3D 立方体时它工作得很好。有谁知道我在这里错过了什么?索引有问题吗?
(下面的“立方体”自定义大小为(1.0f, 2.0f, 3.0f)
)
您的索引形成 GL_TRIANGLES
primitives rather than GL_LINES
原语。见 GL_LINES
:
Vertices 0 and 1 are considered a line. Vertices 2 and 3 are considered a line. And so on.
索引构成基元。更改索引:
GLuint indices[] = {
0, 1, 1, 2, 2, 3, 3, 0, // Front
4, 5, 5, 6, 6, 7, 7, 4, // Back
0, 4, 1, 5, 2, 6, 3, 7
};
glDrawElements(GL_LINES, 24, GL_UNSIGNED_INT, 0);
我正在尝试使用 OpenGL(4.3 核心配置文件)绘制 3D 立方体的顶点(仅边缘),我知道 glPolygonMode
函数,但我不想绘制中间对角线。我这样声明我的顶点和索引:
struct Vertex {
glm::vec3 pos;
glm::vec3 color;
glm::vec3 normal;
glm::vec2 uv;
};
Vertex vertices[8];
// Front vertices
vertices[0].pos = glm::vec3(-0.5f, -0.5f, +0.5f);
vertices[1].pos = glm::vec3(+0.5f, -0.5f, +0.5f);
vertices[2].pos = glm::vec3(+0.5f, +0.5f, +0.5f);
vertices[3].pos = glm::vec3(-0.5f, +0.5f, +0.5f);
// Back vertices
vertices[4].pos = glm::vec3(-0.5f, -0.5f, -0.5f);
vertices[5].pos = glm::vec3(+0.5f, -0.5f, -0.5f);
vertices[6].pos = glm::vec3(+0.5f, +0.5f, -0.5f);
vertices[7].pos = glm::vec3(-0.5f, +0.5f, -0.5f);
GLuint indices[36] = {
0, 1, 2, 2, 3, 0, // Front
1, 5, 6, 6, 2, 1, // Right
7, 6, 5, 5, 4, 7, // Back
4, 0, 3, 3, 7, 4, // Left
4, 5, 1, 1, 0, 4, // Bottom
3, 2, 6, 6, 7, 3 // Top
};
我的缓冲区已相应更新:
// Bind Vertex Array
glBindVertexArray(_VAO);
// Bind VBO to GL_ARRAY_BUFFER type so that all calls to GL_ARRAY_BUFFER use VBO
glBindBuffer(GL_ARRAY_BUFFER, _VBO);
// Upload vertices to VBO
glBufferData(GL_ARRAY_BUFFER, verticesNb * sizeof(Vertex), vertices, GL_STATIC_DRAW);
// Bind EBO to GL_ARRAY_BUFFER type so that all calls to GL_ARRAY_BUFFER use EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _EBO);
// Updload indices to EBO
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesNb * sizeof(GLuint), indices, GL_STATIC_DRAW);
我正在使用 glDrawElements(GL_LINES, 36, GL_UNSIGNED_INT, 0);
绘制我的立方体的边缘,但由于某些原因它没有绘制一些边缘,我不明白为什么。
如果我使用 GL_TRIANGLES,尽管当我想以填充模式渲染我的 3D 立方体时它工作得很好。有谁知道我在这里错过了什么?索引有问题吗?
(下面的“立方体”自定义大小为(1.0f, 2.0f, 3.0f)
)
您的索引形成 GL_TRIANGLES
primitives rather than GL_LINES
原语。见 GL_LINES
:
Vertices 0 and 1 are considered a line. Vertices 2 and 3 are considered a line. And so on.
索引构成基元。更改索引:
GLuint indices[] = {
0, 1, 1, 2, 2, 3, 3, 0, // Front
4, 5, 5, 6, 6, 7, 7, 4, // Back
0, 4, 1, 5, 2, 6, 3, 7
};
glDrawElements(GL_LINES, 24, GL_UNSIGNED_INT, 0);