簡體   English   中英

線框立方體在 OpenGL 中渲染為空白

[英]Wireframe cube rendering blank in OpenGL

我正在創建一個體素游戲,並想用線框模型突出顯示相機指向的塊。 但是,我一生都無法弄清楚為什么這沒有出現。 我已將模型的位置設置為靜態位置。

渲染器:

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 類有效,因為我已經在所有其他渲染器中使用過它。 這是相關的 main.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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM