我正在创建一个体素游戏,并希望使用线框模型突出显示相机所指向的块。但是,我无法终生弄清楚为什么这种现象没有出现。我已经将模型的位置设置为静态位置。
渲染器:
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();
}
如果需要任何其他代码,我很乐意分享。 我一定想念一些确实很明显的东西。如果有人有任何想法,我很想听听。
答案 0 :(得分:1)
position
是一个统一变量。顶点坐标属性的名称为vertex
。
当前顶点(gl_Position
)的位置应通过顶点坐标的函数来设置。例如:
gl_Position = projectionView * vec4(vertex, 1.0);
或
gl_Position = projectionView * vec4(vertex + position, 1.0);