diff --git a/resources/resources.qrc b/resources/resources.qrc index 1e53b8d4..99aa535c 100644 --- a/resources/resources.qrc +++ b/resources/resources.qrc @@ -56,5 +56,7 @@ ../src/noggit/rendering/glsl/square_frag.glsl ../src/noggit/rendering/glsl/cylinder_vert.glsl ../src/noggit/rendering/glsl/cylinder_frag.glsl + ../src/noggit/rendering/glsl/line_vert.glsl + ../src/noggit/rendering/glsl/line_frag.glsl diff --git a/src/noggit/rendering/Primitives.cpp b/src/noggit/rendering/Primitives.cpp index 5d882bc8..c994a5cc 100755 --- a/src/noggit/rendering/Primitives.cpp +++ b/src/noggit/rendering/Primitives.cpp @@ -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 vertices = { {0, 0, 0}, (to - from) }; + std::vector 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 (_vertices_vbo, vertices, GL_STATIC_DRAW); + gl.bufferData (_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 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; + } \ No newline at end of file diff --git a/src/noggit/rendering/Primitives.hpp b/src/noggit/rendering/Primitives.hpp index 9ce9bd82..ea1d8405 100755 --- a/src/noggit/rendering/Primitives.hpp +++ b/src/noggit/rendering/Primitives.hpp @@ -154,4 +154,21 @@ namespace Noggit::Rendering::Primitives std::unique_ptr _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 _program; + }; + } diff --git a/src/noggit/rendering/WorldRender.cpp b/src/noggit/rendering/WorldRender.cpp index 02c160ed..eb9f67f3 100755 --- a/src/noggit/rendering/WorldRender.cpp +++ b/src/noggit/rendering/WorldRender.cpp @@ -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(); diff --git a/src/noggit/rendering/WorldRender.hpp b/src/noggit/rendering/WorldRender.hpp index 383f6707..fb8614c3 100755 --- a/src/noggit/rendering/WorldRender.hpp +++ b/src/noggit/rendering/WorldRender.hpp @@ -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;