在 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 类有效,因为我已经在所有其他渲染器中使用过它。以下是相关的主.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);
相关文章:
- 如何在 Win32 中用一条线连接 2 个文本框?
- MFC正在从线程将文本附加到富编辑框
- 如何抓取指向Qt中弹出对话框的指针,该对话框阻止了QTest中的UI线程
- 如何在另一个线程中关闭 MFC 模式对话框并获取对话框返回值?
- 在 OpenGL 中呈现空白的线框立方体
- 从 Visual C++ 2010 中的单独线程调用消息框
- 如何从另一个线程关闭对话框?QT
- 当主GUI线程被阻塞时,如何从工作线程创建无模式对话框
- 为什么消息框不阻止线程?
- 在std ::线程中打开MFC对话框
- 如何在对话框编辑框中显示来自 UI 线程类 Run() 函数的字符串
- 想要在从搅拌机导入的实体3D对象上绘制线框网格
- 显示Q对话框时出现QT线程问题
- 关闭其他线程的对话框后无法从主窗口聚焦
- 关闭MFC对话框时多线程对象被破坏
- 来自x、y和z文本文件的OpenGL线框
- 线程退出时 DLL 中的 MFC 无模式对话框被销毁
- C ++如何使用Win32 API通过线程从文本框获取文本
- 将纹理应用于线框
- 将GL_TRIANGLE_STRIP显示为线框