简体   繁体   English

使用 OpenGL 3 绘制 3D 立方体的边缘时遇到问题

[英]Having issues drawing 3D cube's edges with OpenGL 3

I'm trying to draw 3D cube's vertices (edges only) using OpenGL (4.3 core profile), I know the glPolygonMode function but I'd like not to draw the intermediate diagonal lines.我正在尝试使用 OpenGL(4.3 核心配置文件)绘制 3D 立方体的顶点(仅边缘),我知道glPolygonMode函数,但我不想绘制中间对角线。 I declare my vertices and my indices like so :我像这样声明我的顶点和索引:

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
};

My buffer is updated accordingly :我的缓冲区相应地更新:

// 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);

I'm using glDrawElements(GL_LINES, 36, GL_UNSIGNED_INT, 0);我正在使用glDrawElements(GL_LINES, 36, GL_UNSIGNED_INT, 0); to draw my cube's edges, but for some reason it doesn't draw some edges and I don't understand why.绘制我的立方体的边缘,但由于某种原因它没有绘制一些边缘,我不明白为什么。

If I use GL_TRIANGLES, it works pretty well though when I want to render my 3D cube in fill-mode.如果我使用 GL_TRIANGLES,当我想在填充模式下渲染我的 3D 立方体时,它工作得很好。 Does anyone know what I'm missing here ?有谁知道我在这里想念什么? Is it an issue with the indices ?是指数的问题吗?

(The "cube" below has a custom size of (1.0f, 2.0f, 3.0f) ) (下面的“立方体”的自定义大小为(1.0f, 2.0f, 3.0f)

OpenGL立方体

Your indices form GL_TRIANGLES primitives rather than GL_LINES primitives.您的索引形成GL_TRIANGLES基元而不是GL_LINES基元。 See GL_LINES :GL_LINES

Vertices 0 and 1 are considered a line.顶点 0 和 1 被视为一条线。 Vertices 2 and 3 are considered a line.顶点 2 和 3 被视为一条线。 And so on.等等。

The indices form the primitives.索引形成基元。 Change the indices:更改索引:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM