在 OpenGL 中呈现空白的线框立方体

Wireframe cube rendering blank in OpenGL

本文关键字:线框 立方体 空白 OpenGL      更新时间:2023-10-16

我正在创建一个体素游戏,并希望使用线框模型突出显示相机指向的块。但是,我一辈子都想不通为什么这没有出现。我已将模型的位置设置为静态位置。

渲染:

constexpr std::array<float, 72> VERTICES =
{
    0.0f, 0.0f, 0.0f,
    1.0f, 0.0f, 0.0f,
    1.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    1.0f, 0.0f, 1.0f,
    0.0f, 0.0f, 1.0f,
    0.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f,
    0.0f, 1.0f, 0.0f,
    1.0f, 1.0f, 0.0f,
    1.0f, 1.0f, 1.0f,
    0.0f, 1.0f, 1.0f,
    0.0f, 0.0f, 1.0f,
    1.0f, 0.0f, 1.0f,
    1.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 1.0f,
    0.0f, 0.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 1.0f,
    1.0f, 0.0f, 0.0f,
    1.0f, 0.0f, 1.0f,
    1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 0.0f
};
constexpr std::array<int, 24> INDICES =
{
    0, 1, 2, 3,
    4, 5, 6, 7,
    8, 9, 10, 11,
    12, 13, 14, 15,
    16, 17, 18, 19,
    20, 21, 22, 23
};
SelectedBlockRenderer::SelectedBlockRenderer():
    should_render(false),
    shader("shader/selectedBlockVertexShader.txt", "shader/selectedBlockFragmentShader.txt")
{
    shader.set_uniforms({"position", "projectionView"});
    glGenVertexArrays(1, &vao_id);
    glGenBuffers(1, &v_buffer_id);
    glGenBuffers(1, &i_buffer_id);
    glBindVertexArray(vao_id);
    glBindBuffer(GL_ARRAY_BUFFER, v_buffer_id);
    glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), &VERTICES[0], GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, nullptr);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, i_buffer_id);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(INDICES), &INDICES[0], GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}
SelectedBlockRenderer::~SelectedBlockRenderer()
{
}
void SelectedBlockRenderer::render(const Display& display, const Camera& camera)
{
    if (!should_render) return;
    glDisable(GL_CULL_FACE);
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glLineWidth(10.0f);
    glm::mat4 projection_view = display.get_projection_matrix() * camera.get_view();
    shader.activate();
    shader.load_uniform("projectionView", projection_view);
    shader.load_uniform("position", glm::vec3(x, y, z));
    glBindVertexArray(vao_id);
    glEnableVertexAttribArray(0);
    glDrawElements(GL_QUADS, INDICES.size(), GL_UNSIGNED_INT, nullptr);
    glDisableVertexAttribArray(0);
    glBindVertexArray(0);
    shader.deactivate();
    glEnable(GL_CULL_FACE);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
void SelectedBlockRenderer::free()
{
    shader.free();
    glDeleteBuffers(1, &v_buffer_id);
    glDeleteBuffers(1, &i_buffer_id);
    glDeleteVertexArrays(1, &vao_id);
}

着色器代码:

#version 400 core
layout(location = 0) in vec3 vertex;
uniform mat4 projectionView;
uniform vec3 position;
void main () {
    gl_Position = projectionView * vec4(position, 1.0);
}

片段着色器:

#version 400 core
out vec4 fragment;
void main () {
    fragment = vec4(0.0, 0.0, 0.0, 1.0);
}

我知道 Shader 类有效,因为我已经在所有其他渲染器中使用过它。以下是相关的主.cpp代码:

void main () {
    SelectedBlockRenderer sb_renderer;
    while(!display.closed) {
        if (timer.update_requested) {
            //update display and camera
            sb_renderer.render(display, camera);
        }
    }
    sb_renderer.free();
}

如果需要任何其他代码,我很乐意分享。我一定错过了一些非常明显的东西。如果有人有任何想法,我很想听听。

position是一个

统一变量。顶点坐标属性的名称为 vertex

当前顶点的位置(gl_Position(应该由顶点坐标的函数来设置,例如:

gl_Position = projectionView * vec4(vertex, 1.0);

gl_Position = projectionView * vec4(vertex + position, 1.0);