Rendering::Primitives::Line, adding primitive to render a line

This commit is contained in:
EIntemporel
2022-12-02 21:59:35 +01:00
parent f981b03940
commit c629a88a66
5 changed files with 87 additions and 0 deletions

View File

@@ -56,5 +56,7 @@
<file alias="square_fs">../src/noggit/rendering/glsl/square_frag.glsl</file>
<file alias="cylinder_vs">../src/noggit/rendering/glsl/cylinder_vert.glsl</file>
<file alias="cylinder_fs">../src/noggit/rendering/glsl/cylinder_frag.glsl</file>
<file alias="line_vs">../src/noggit/rendering/glsl/line_vert.glsl</file>
<file alias="line_fs">../src/noggit/rendering/glsl/line_frag.glsl</file>
</qresource>
</RCC>

View File

@@ -501,4 +501,70 @@ void Square::setup_buffers()
}
_buffers_are_setup = true;
}
void Line::draw(glm::mat4x4 const& mvp
, glm::vec3 const& from
, glm::vec3 const& to
, glm::vec4 const& color
)
{
if (!_buffers_are_setup)
{
setup_buffers(from, to);
}
OpenGL::Scoped::use_program line_shader{ *_program.get() };
line_shader.uniform("model_view_projection", mvp);
line_shader.uniform("origin", glm::vec3(from.x, from.y, from.z));
line_shader.uniform("color", color);
OpenGL::Scoped::vao_binder const _(_vao[0]);
gl.drawElements(GL_LINES, _indices_vbo, 2, GL_UNSIGNED_SHORT, nullptr);
}
void Line::setup_buffers(glm::vec3 const& from, glm::vec3 const& to)
{
_vao.upload();
_buffers.upload();
std::vector<glm::vec3> vertices = { {0, 0, 0}, (to - from) };
std::vector<std::uint16_t> indices = { 0,1 };
_program.reset(new OpenGL::program(
{
{ GL_VERTEX_SHADER, OpenGL::shader::src_from_qrc("line_vs") },
{ GL_FRAGMENT_SHADER, OpenGL::shader::src_from_qrc("line_fs") }
}
));
gl.bufferData<GL_ARRAY_BUFFER, glm::vec3> (_vertices_vbo, vertices, GL_STATIC_DRAW);
gl.bufferData<GL_ELEMENT_ARRAY_BUFFER, std::uint16_t> (_indices_vbo, indices, GL_STATIC_DRAW);
OpenGL::Scoped::index_buffer_manual_binder indices_binder(_indices_vbo);
OpenGL::Scoped::use_program shader(*_program.get());
{
OpenGL::Scoped::vao_binder const _(_vao[0]);
OpenGL::Scoped::buffer_binder<GL_ARRAY_BUFFER> const vertices_binder(_vertices_vbo);
shader.attrib("position", 3, GL_FLOAT, GL_FALSE, 0, 0);
indices_binder.bind();
}
_buffers_are_setup = true;
}
void Line::unload()
{
_vao.unload();
_buffers.unload();
_program.reset();
_buffers_are_setup = false;
}

View File

@@ -154,4 +154,21 @@ namespace Noggit::Rendering::Primitives
std::unique_ptr<OpenGL::program> _program;
};
class Line
{
public:
void draw(glm::mat4x4 const& mvp, glm::vec3 const& from, glm::vec3 const& to, glm::vec4 const& color);
void unload();
private:
bool _buffers_are_setup = false;
void setup_buffers(glm::vec3 const& from, glm::vec3 const& to);
OpenGL::Scoped::deferred_upload_vertex_arrays<1> _vao;
OpenGL::Scoped::deferred_upload_buffers<2> _buffers;
GLuint const& _vertices_vbo = _buffers[0];
GLuint const& _indices_vbo = _buffers[1];
std::unique_ptr<OpenGL::program> _program;
};
}

View File

@@ -1190,6 +1190,7 @@ void WorldRender::unload()
_sphere_render.unload();
_square_render.unload();
_cylinder_render.unload();
_line_render.unload();
_horizon_render.reset();
_liquid_texture_manager.unload();

View File

@@ -129,6 +129,7 @@ namespace Noggit::Rendering
Noggit::Rendering::Primitives::Sphere _sphere_render;
Noggit::Rendering::Primitives::Square _square_render;
Noggit::Rendering::Primitives::Cylinder _cylinder_render;
Noggit::Rendering::Primitives::Line _line_render;
// buffers
OpenGL::Scoped::deferred_upload_buffers<8> _buffers;