From 929b2afd06d28e15c9f5a9fc0b56d626fab09151 Mon Sep 17 00:00:00 2001 From: Skarn Date: Sun, 16 Jan 2022 21:23:56 +0300 Subject: [PATCH] Further refactor | Update resources.qrc, cursor_frag.glsl, and 68 more files... --- resources/resources.qrc | 52 +- src/noggit/TileWater.cpp | 2 +- src/noggit/TileWater.hpp | 2 +- src/noggit/World.h | 6 +- src/noggit/{ => rendering}/CursorRender.cpp | 4 +- src/noggit/{ => rendering}/CursorRender.hpp | 4 +- .../{ => rendering}/LiquidTextureManager.cpp | 6 +- .../{ => rendering}/LiquidTextureManager.hpp | 8 +- src/noggit/rendering/Primitives.cpp | 381 +++++++++++++ src/noggit/rendering/Primitives.hpp | 133 +++++ src/noggit/rendering/WorldRender.hpp | 10 +- .../rendering}/glsl/cursor_frag.glsl | 0 .../rendering}/glsl/cursor_vert.glsl | 0 src/noggit/rendering/glsl/grid_frag.glsl | 10 + src/noggit/rendering/glsl/grid_vert.glsl | 17 + .../rendering}/glsl/horizon_frag.glsl | 0 .../rendering}/glsl/horizon_vert.glsl | 0 .../rendering}/glsl/liquid_frag.glsl | 0 .../rendering}/glsl/liquid_vert.glsl | 0 .../rendering}/glsl/m2_box_frag.glsl | 0 .../rendering}/glsl/m2_box_vert.glsl | 0 src/{ => noggit/rendering}/glsl/m2_frag.glsl | 0 src/{ => noggit/rendering}/glsl/m2_vert.glsl | 0 .../rendering}/glsl/mfbo_frag.glsl | 0 .../rendering}/glsl/mfbo_vert.glsl | 0 .../rendering}/glsl/occluder_frag.glsl | 0 .../rendering}/glsl/occluder_vert.glsl | 0 .../rendering}/glsl/particle_frag.glsl | 0 .../rendering}/glsl/particle_vert.glsl | 0 .../rendering}/glsl/ribbon_frag.glsl | 0 .../rendering}/glsl/ribbon_vert.glsl | 0 src/noggit/rendering/glsl/sphere_frag.glsl | 10 + src/noggit/rendering/glsl/sphere_vert.glsl | 15 + src/noggit/rendering/glsl/square_frag.glsl | 10 + src/noggit/rendering/glsl/square_vert.glsl | 24 + .../rendering}/glsl/terrain_frag.glsl | 0 .../rendering}/glsl/terrain_vert.glsl | 0 src/noggit/rendering/glsl/wire_box_frag.glsl | 10 + src/noggit/rendering/glsl/wire_box_vert.glsl | 11 + src/{ => noggit/rendering}/glsl/wmo_frag.glsl | 0 src/{ => noggit/rendering}/glsl/wmo_vert.glsl | 0 .../tools/PreviewRenderer/PreviewRenderer.hpp | 6 +- src/opengl/primitives.cpp | 508 ------------------ src/opengl/primitives.hpp | 134 ----- 44 files changed, 675 insertions(+), 688 deletions(-) rename src/noggit/{ => rendering}/CursorRender.cpp (99%) rename src/noggit/{ => rendering}/CursorRender.hpp (94%) rename src/noggit/{ => rendering}/LiquidTextureManager.cpp (97%) rename src/noggit/{ => rendering}/LiquidTextureManager.hpp (90%) create mode 100644 src/noggit/rendering/Primitives.cpp create mode 100644 src/noggit/rendering/Primitives.hpp rename src/{ => noggit/rendering}/glsl/cursor_frag.glsl (100%) rename src/{ => noggit/rendering}/glsl/cursor_vert.glsl (100%) create mode 100644 src/noggit/rendering/glsl/grid_frag.glsl create mode 100644 src/noggit/rendering/glsl/grid_vert.glsl rename src/{ => noggit/rendering}/glsl/horizon_frag.glsl (100%) rename src/{ => noggit/rendering}/glsl/horizon_vert.glsl (100%) rename src/{ => noggit/rendering}/glsl/liquid_frag.glsl (100%) rename src/{ => noggit/rendering}/glsl/liquid_vert.glsl (100%) rename src/{ => noggit/rendering}/glsl/m2_box_frag.glsl (100%) rename src/{ => noggit/rendering}/glsl/m2_box_vert.glsl (100%) rename src/{ => noggit/rendering}/glsl/m2_frag.glsl (100%) rename src/{ => noggit/rendering}/glsl/m2_vert.glsl (100%) rename src/{ => noggit/rendering}/glsl/mfbo_frag.glsl (100%) rename src/{ => noggit/rendering}/glsl/mfbo_vert.glsl (100%) rename src/{ => noggit/rendering}/glsl/occluder_frag.glsl (100%) rename src/{ => noggit/rendering}/glsl/occluder_vert.glsl (100%) rename src/{ => noggit/rendering}/glsl/particle_frag.glsl (100%) rename src/{ => noggit/rendering}/glsl/particle_vert.glsl (100%) rename src/{ => noggit/rendering}/glsl/ribbon_frag.glsl (100%) rename src/{ => noggit/rendering}/glsl/ribbon_vert.glsl (100%) create mode 100644 src/noggit/rendering/glsl/sphere_frag.glsl create mode 100644 src/noggit/rendering/glsl/sphere_vert.glsl create mode 100644 src/noggit/rendering/glsl/square_frag.glsl create mode 100644 src/noggit/rendering/glsl/square_vert.glsl rename src/{ => noggit/rendering}/glsl/terrain_frag.glsl (100%) rename src/{ => noggit/rendering}/glsl/terrain_vert.glsl (100%) create mode 100644 src/noggit/rendering/glsl/wire_box_frag.glsl create mode 100644 src/noggit/rendering/glsl/wire_box_vert.glsl rename src/{ => noggit/rendering}/glsl/wmo_frag.glsl (100%) rename src/{ => noggit/rendering}/glsl/wmo_vert.glsl (100%) delete mode 100644 src/opengl/primitives.cpp delete mode 100644 src/opengl/primitives.hpp diff --git a/resources/resources.qrc b/resources/resources.qrc index da0519d9..a5456949 100644 --- a/resources/resources.qrc +++ b/resources/resources.qrc @@ -24,27 +24,35 @@ ../media/project-icon-shadow.png - ../src/glsl/m2_vert.glsl - ../src/glsl/m2_frag.glsl - ../src/glsl/m2_box_vert.glsl - ../src/glsl/m2_box_frag.glsl - ../src/glsl/particle_vert.glsl - ../src/glsl/particle_frag.glsl - ../src/glsl/ribbon_vert.glsl - ../src/glsl/ribbon_frag.glsl - ../src/glsl/terrain_vert.glsl - ../src/glsl/terrain_frag.glsl - ../src/glsl/wmo_vert.glsl - ../src/glsl/wmo_frag.glsl - ../src/glsl/liquid_vert.glsl - ../src/glsl/liquid_frag.glsl - ../src/glsl/mfbo_vert.glsl - ../src/glsl/mfbo_frag.glsl - ../src/glsl/cursor_vert.glsl - ../src/glsl/cursor_frag.glsl - ../src/glsl/horizon_vert.glsl - ../src/glsl/horizon_frag.glsl - ../src/glsl/occluder_vert.glsl - ../src/glsl/occluder_frag.glsl + ../src/noggit/rendering/glsl/m2_vert.glsl + ../src/noggit/rendering/glsl/m2_frag.glsl + ../src/noggit/rendering/glsl/m2_box_vert.glsl + ../src/noggit/rendering/glsl/m2_box_frag.glsl + ../src/noggit/rendering/glsl/particle_vert.glsl + ../src/noggit/rendering/glsl/particle_frag.glsl + ../src/noggit/rendering/glsl/ribbon_vert.glsl + ../src/noggit/rendering/glsl/ribbon_frag.glsl + ../src/noggit/rendering/glsl/terrain_vert.glsl + ../src/noggit/rendering/glsl/terrain_frag.glsl + ../src/noggit/rendering/glsl/wmo_vert.glsl + ../src/noggit/rendering/glsl/wmo_frag.glsl + ../src/noggit/rendering/glsl/liquid_vert.glsl + ../src/noggit/rendering/glsl/liquid_frag.glsl + ../src/noggit/rendering/glsl/mfbo_vert.glsl + ../src/noggit/rendering/glsl/mfbo_frag.glsl + ../src/noggit/rendering/glsl/cursor_vert.glsl + ../src/noggit/rendering/glsl/cursor_frag.glsl + ../src/noggit/rendering/glsl/horizon_vert.glsl + ../src/noggit/rendering/glsl/horizon_frag.glsl + ../src/noggit/rendering/glsl/occluder_vert.glsl + ../src/noggit/rendering/glsl/occluder_frag.glsl + ../src/noggit/rendering/glsl/wire_box_vert.glsl + ../src/noggit/rendering/glsl/wire_box_frag.glsl + ../src/noggit/rendering/glsl/grid_vert.glsl + ../src/noggit/rendering/glsl/grid_frag.glsl + ../src/noggit/rendering/glsl/sphere_vert.glsl + ../src/noggit/rendering/glsl/sphere_frag.glsl + ../src/noggit/rendering/glsl/square_vert.glsl + ../src/noggit/rendering/glsl/square_frag.glsl diff --git a/src/noggit/TileWater.cpp b/src/noggit/TileWater.cpp index 9eb2ebfd..af7ea8ee 100644 --- a/src/noggit/TileWater.cpp +++ b/src/noggit/TileWater.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/noggit/TileWater.hpp b/src/noggit/TileWater.hpp index bd8d2b24..727ce280 100644 --- a/src/noggit/TileWater.hpp +++ b/src/noggit/TileWater.hpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/noggit/World.h b/src/noggit/World.h index 233d43b8..ce59dfb6 100644 --- a/src/noggit/World.h +++ b/src/noggit/World.h @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include // ModelManager #include @@ -18,10 +18,10 @@ #include #include #include -#include +#include #include #include -#include +#include #include #include #include diff --git a/src/noggit/CursorRender.cpp b/src/noggit/rendering/CursorRender.cpp similarity index 99% rename from src/noggit/CursorRender.cpp rename to src/noggit/rendering/CursorRender.cpp index 67a8e357..1301ef35 100644 --- a/src/noggit/CursorRender.cpp +++ b/src/noggit/rendering/CursorRender.cpp @@ -1,8 +1,8 @@ // This file is part of Noggit3, licensed under GNU General Public License (version 3). -#include +#include "CursorRender.hpp" #include "math/trig.hpp" -#include +#include "opengl/shader.hpp" namespace Noggit { diff --git a/src/noggit/CursorRender.hpp b/src/noggit/rendering/CursorRender.hpp similarity index 94% rename from src/noggit/CursorRender.hpp rename to src/noggit/rendering/CursorRender.hpp index fc8cf774..dc88e09d 100644 --- a/src/noggit/CursorRender.hpp +++ b/src/noggit/rendering/CursorRender.hpp @@ -1,8 +1,8 @@ // This file is part of Noggit3, licensed under GNU General Public License (version 3). #pragma once -#include -#include +#include "opengl/scoped.hpp" +#include "opengl/shader.fwd.hpp" #include diff --git a/src/noggit/LiquidTextureManager.cpp b/src/noggit/rendering/LiquidTextureManager.cpp similarity index 97% rename from src/noggit/LiquidTextureManager.cpp rename to src/noggit/rendering/LiquidTextureManager.cpp index e4184461..713b2494 100644 --- a/src/noggit/LiquidTextureManager.cpp +++ b/src/noggit/rendering/LiquidTextureManager.cpp @@ -1,9 +1,9 @@ // This file is part of Noggit3, licensed under GNU General Public License (version 3). #include "LiquidTextureManager.hpp" -#include -#include -#include +#include "opengl/context.inl" +#include "noggit/DBC.h" +#include "noggit/application/NoggitApplication.hpp" LiquidTextureManager::LiquidTextureManager(Noggit::NoggitRenderContext context) : _context(context) diff --git a/src/noggit/LiquidTextureManager.hpp b/src/noggit/rendering/LiquidTextureManager.hpp similarity index 90% rename from src/noggit/LiquidTextureManager.hpp rename to src/noggit/rendering/LiquidTextureManager.hpp index 8d3675ed..451bec6e 100644 --- a/src/noggit/LiquidTextureManager.hpp +++ b/src/noggit/rendering/LiquidTextureManager.hpp @@ -3,12 +3,12 @@ #ifndef NOGGIT_LIQUIDTEXTUREMANAGER_HPP #define NOGGIT_LIQUIDTEXTUREMANAGER_HPP -#include -#include -#include +#include "noggit/TextureManager.h" +#include "noggit/ContextObject.hpp" +#include "external/tsl/robin_map.h" #include -#include +#include "external/glm/vec2.hpp" template std::string string_format(const std::string& format, Args ... args) diff --git a/src/noggit/rendering/Primitives.cpp b/src/noggit/rendering/Primitives.cpp new file mode 100644 index 00000000..c08f84d0 --- /dev/null +++ b/src/noggit/rendering/Primitives.cpp @@ -0,0 +1,381 @@ +// This file is part of Noggit3, licensed under GNU General Public License (version 3). + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +using namespace Noggit::Rendering::Primitives; + +void WireBox::draw ( glm::mat4x4 const& model_view + , glm::mat4x4 const& projection + , glm::mat4x4 const& transform + , glm::vec4 const& color + , glm::vec3 const& min_point + , glm::vec3 const& max_point + ) +{ + + if (!_buffers_are_setup) + { + setup_buffers(); + } + + OpenGL::Scoped::use_program wire_box_shader {*_program.get()}; + + auto points = math::box_points(min_point, max_point); + + auto glmPoints = std::vector(); + + for(auto const point : points) + { + glmPoints.push_back(glm::vec3(point.x, point.y, point.z)); + } + + wire_box_shader.uniform("model_view", model_view); + wire_box_shader.uniform("projection", projection); + wire_box_shader.uniform("transform", transform); + wire_box_shader.uniform("color", color); + wire_box_shader.uniform("pointPositions", glmPoints); + + OpenGL::Scoped::bool_setter const line_smooth; + gl.hint(GL_LINE_SMOOTH_HINT, GL_NICEST); + + OpenGL::Scoped::vao_binder const _(_vao[0]); + + gl.drawElements (GL_LINE_STRIP, _indices, 16, GL_UNSIGNED_BYTE, nullptr); +} + +void WireBox::setup_buffers() +{ + _program.reset(new OpenGL::program( {{ GL_VERTEX_SHADER, OpenGL::shader::src_from_qrc("wire_box_vs") } + , { GL_FRAGMENT_SHADER, OpenGL::shader::src_from_qrc("wire_box_fs")}})); + + _vao.upload(); + _buffers.upload(); + + //std::vector positions (math::box_points (min_point, max_point)); + + static std::array const indices + {{5, 7, 3, 2, 0, 1, 3, 1, 5, 4, 0, 4, 6, 2, 6, 7}}; + + OpenGL::Scoped::buffer_binder const index_buffer (_indices); + gl.bufferData ( GL_ELEMENT_ARRAY_BUFFER + , indices.size() * sizeof (*indices.data()) + , indices.data() + , GL_STATIC_DRAW + ); + + OpenGL::Scoped::use_program shader (*_program.get()); + + OpenGL::Scoped::vao_binder const _ (_vao[0]); + + _buffers_are_setup = true; + +} + + void WireBox::unload() + { + _vao.unload(); + _buffers.unload(); + _program.reset(); + + _buffers_are_setup = false; + } + + void Grid::draw(glm::mat4x4 const& mvp + , glm::vec3 const& pos + , glm::vec4 const& color + , float radius + ) + { + if (!_buffers_are_setup) + { + setup_buffers(); + } + + OpenGL::Scoped::use_program sphere_shader {*_program.get()}; + + sphere_shader.uniform("model_view_projection", mvp); + sphere_shader.uniform("origin", glm::vec3(pos.x,pos.y,pos.z)); + sphere_shader.uniform("color", color); + sphere_shader.uniform("radius", radius); + + OpenGL::Scoped::vao_binder const _(_vao[0]); + gl.drawElements(GL_LINES, _indices_vbo, _indice_count, GL_UNSIGNED_SHORT, nullptr); + } + + + void Grid::setup_buffers() + { + _vao.upload(); + _buffers.upload(); + + _program.reset(new OpenGL::program({{ GL_VERTEX_SHADER + , OpenGL::shader::src_from_qrc("grid_vs") + } + , { GL_FRAGMENT_SHADER + , OpenGL::shader::src_from_qrc("grid_fs") + } + })); + + + std::vector vertices; + std::vector indices; + + int slices = 20; + + for(int j = 0; j <= slices; ++j) + { + for(int i = 0; i <= slices; ++i) + { + float x = static_cast(i) / static_cast(slices); + float y = 0; + float z = static_cast(j) / static_cast(slices); + vertices.push_back(glm::vec3(x, y, z)); + } + } + + for(int j = 0; j < slices; ++j) + { + for(int i = 0; i < slices; ++i) + { + + int row1 = j * (slices + 1); + int row2 = (j + 1) * (slices + 1); + + indices.push_back(row1 + i); + indices.push_back(row1 + i + 1); + indices.push_back(row1 + i + 1); + indices.push_back(row2 + i + 1); + + indices.push_back(row2 + i + 1); + indices.push_back(row2 + i); + indices.push_back(row2 + i); + indices.push_back(row1 + i); + + } + } + + _indice_count = indices.size(); + + 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 Grid::unload() + { + _vao.unload(); + _buffers.unload(); + _program.reset(); + + _buffers_are_setup = false; + } + + + void Sphere::draw(glm::mat4x4 const& mvp + , glm::vec3 const& pos + , glm::vec4 const& color + , float radius + ) +{ + if (!_buffers_are_setup) + { + setup_buffers(); + } + + OpenGL::Scoped::use_program sphere_shader {*_program.get()}; + + sphere_shader.uniform("model_view_projection", mvp); + sphere_shader.uniform("origin", glm::vec3(pos.x,pos.y,pos.z)); + sphere_shader.uniform("radius", radius); + sphere_shader.uniform("color", color); + + OpenGL::Scoped::vao_binder const _(_vao[0]); + gl.drawElements(GL_TRIANGLES, _indices_vbo, _indice_count, GL_UNSIGNED_SHORT, nullptr); +} + + +void Sphere::setup_buffers() +{ + _vao.upload(); + _buffers.upload(); + + std::vector vertices; + std::vector indices; + + _program.reset(new OpenGL::program({{ GL_VERTEX_SHADER + , OpenGL::shader::src_from_qrc("sphere_vs")} + , { GL_FRAGMENT_SHADER + , OpenGL::shader::src_from_qrc("sphere_fs") + }})); + + + int segment = 27; + + // add overlapping vertices at the end for an easier vertices generations + for (int rotation_step = 0; rotation_step <= segment; ++rotation_step) + { + math::degrees rotation(360.f*rotation_step / static_cast(segment)); + auto rotationQuat = glm::angleAxis(rotation._, glm::vec3(0, 0, 1)); + for (int i = 0; i < segment; ++i) + { + float x = glm::cos(math::radians(math::degrees(i * 360 / segment))._); + float z = glm::sin(math::radians(math::degrees(i * 360 / segment))._); + + glm::vec4 v(x, 0.f, z,0.0f); + + vertices.emplace_back(glm::toMat4(rotationQuat) * v); + + if (rotation_step < segment) + { + indices.emplace_back(i + rotation_step*segment); + indices.emplace_back(((i + 1) % segment) + rotation_step * segment); + indices.emplace_back(i + (rotation_step+1) * segment); + + indices.emplace_back(i + (rotation_step+1) * segment); + indices.emplace_back(((i + 1) % segment) + rotation_step * segment); + indices.emplace_back(((i + 1) % segment) + (rotation_step+1) * segment); + } + } + } + + _indice_count = indices.size(); + + 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 Sphere::unload() + { + _vao.unload(); + _buffers.unload(); + _program.reset(); + + _buffers_are_setup = false; + } + + + void Square::draw(glm::mat4x4 const& mvp + , glm::vec3 const& pos + , float radius + , math::radians inclination + , math::radians orientation + , glm::vec4 const& color + ) +{ + if (!_buffers_are_setup) + { + setup_buffers(); + } + + OpenGL::Scoped::use_program sphere_shader {*_program.get()}; + + sphere_shader.uniform("model_view_projection", mvp); + sphere_shader.uniform("origin", glm::vec3(pos.x,pos.y,pos.z)); + sphere_shader.uniform("radius", radius); + sphere_shader.uniform("inclination", inclination._); + sphere_shader.uniform("orientation", orientation._); + sphere_shader.uniform("color", color); + + OpenGL::Scoped::vao_binder const _ (_vao[0]); + gl.drawElements(GL_TRIANGLES, _indices_vbo, 6, GL_UNSIGNED_SHORT, nullptr); +} + + +void Square::setup_buffers() +{ + _vao.upload(); + _buffers.upload(); + + std::vector vertices = + { + {-1.f, 0.f, -1.f} + ,{-1.f, 0.f, 1.f} + ,{ 1.f, 0.f, 1.f} + ,{ 1.f, 0.f, -1.f} + }; + std::vector indices = {0,1,2, 2,3,0}; + + _program.reset(new OpenGL::program({{ GL_VERTEX_SHADER + , OpenGL::shader::src_from_qrc("square_vs") + }, { GL_FRAGMENT_SHADER + , OpenGL::shader::src_from_qrc("square_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 Square::unload() + { + _vao.unload(); + _buffers.unload(); + _program.reset(); + + _buffers_are_setup = false; + + } + diff --git a/src/noggit/rendering/Primitives.hpp b/src/noggit/rendering/Primitives.hpp new file mode 100644 index 00000000..e63ddde2 --- /dev/null +++ b/src/noggit/rendering/Primitives.hpp @@ -0,0 +1,133 @@ +// This file is part of Noggit3, licensed under GNU General Public License (version 3). + +#pragma once + +#include +#include +#include +#include + +#include +#include + +namespace math +{ + struct vector_3d; + struct vector_4d; +} + +namespace Noggit::Rendering::Primitives +{ + class WireBox + { + public: + WireBox() {} + WireBox(const WireBox&); + WireBox& operator=(WireBox& box ) { return *this; }; + + public: + static WireBox& getInstance(Noggit::NoggitRenderContext context) + { + static std::unordered_map instances; + + if (instances.find(context) == instances.end()) + { + WireBox instance; + instances[context] = instance; + } + + return instances.at(context); + } + + void draw ( glm::mat4x4 const& model_view + , glm::mat4x4 const& projection + , glm::mat4x4 const& transform + , glm::vec4 const& color + , glm::vec3 const& min_point + , glm::vec3 const& max_point + ); + + void unload(); + + private: + bool _buffers_are_setup = false; + + void setup_buffers(); + + OpenGL::Scoped::deferred_upload_vertex_arrays<1> _vao; + OpenGL::Scoped::deferred_upload_buffers<1> _buffers; + GLuint const& _indices = _buffers[0]; + std::unique_ptr _program; + }; + + class Grid + { + public: + void draw(glm::mat4x4 const& mvp + , glm::vec3 const& pos + , glm::vec4 const& color + , float radius + ); + void unload(); + private: + bool _buffers_are_setup = false; + + void setup_buffers(); + + int _indice_count = 0; + + 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; + }; + + class Sphere + { + public: + void draw(glm::mat4x4 const& mvp + , glm::vec3 const& pos + , glm::vec4 const& color + , float radius + ); + void unload(); + + private: + bool _buffers_are_setup = false; + + void setup_buffers(); + + int _indice_count = 0; + + 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; + }; + + class Square + { + public: + void draw(glm::mat4x4 const& mvp + , glm::vec3 const& pos + , float radius // radius of the biggest circle fitting inside the square drawn + , math::radians inclination + , math::radians orientation + , glm::vec4 const& color + ); + void unload(); + private: + bool _buffers_are_setup = false; + + void setup_buffers(); + + 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.hpp b/src/noggit/rendering/WorldRender.hpp index ad3ed920..70b9f823 100644 --- a/src/noggit/rendering/WorldRender.hpp +++ b/src/noggit/rendering/WorldRender.hpp @@ -9,13 +9,13 @@ #include #include -#include -#include +#include +#include #include #include #include -#include +#include #include @@ -123,8 +123,8 @@ namespace Noggit::Rendering // cursor Noggit::CursorRender _cursor_render; - OpenGL::primitives::sphere _sphere_render; - OpenGL::primitives::square _square_render; + Noggit::Rendering::Primitives::Sphere _sphere_render; + Noggit::Rendering::Primitives::Square _square_render; // buffers OpenGL::Scoped::deferred_upload_buffers<8> _buffers; diff --git a/src/glsl/cursor_frag.glsl b/src/noggit/rendering/glsl/cursor_frag.glsl similarity index 100% rename from src/glsl/cursor_frag.glsl rename to src/noggit/rendering/glsl/cursor_frag.glsl diff --git a/src/glsl/cursor_vert.glsl b/src/noggit/rendering/glsl/cursor_vert.glsl similarity index 100% rename from src/glsl/cursor_vert.glsl rename to src/noggit/rendering/glsl/cursor_vert.glsl diff --git a/src/noggit/rendering/glsl/grid_frag.glsl b/src/noggit/rendering/glsl/grid_frag.glsl new file mode 100644 index 00000000..cde311a8 --- /dev/null +++ b/src/noggit/rendering/glsl/grid_frag.glsl @@ -0,0 +1,10 @@ +#version 330 core + +uniform vec4 color; + +out vec4 out_color; + +void main() +{ + out_color = color; +} \ No newline at end of file diff --git a/src/noggit/rendering/glsl/grid_vert.glsl b/src/noggit/rendering/glsl/grid_vert.glsl new file mode 100644 index 00000000..34ed576a --- /dev/null +++ b/src/noggit/rendering/glsl/grid_vert.glsl @@ -0,0 +1,17 @@ +#version 330 core + +in vec4 position; + +uniform mat4 model_view_projection; +uniform vec3 origin; +uniform float radius; + +void main() +{ + vec4 pos = position; + pos.xyz *= radius; + + vec3 origin_fixed = vec3(origin.x - radius / 2.0f, origin.y, origin.z - radius / 2.0f); + pos.xyz += origin_fixed; + gl_Position = model_view_projection * pos; +} \ No newline at end of file diff --git a/src/glsl/horizon_frag.glsl b/src/noggit/rendering/glsl/horizon_frag.glsl similarity index 100% rename from src/glsl/horizon_frag.glsl rename to src/noggit/rendering/glsl/horizon_frag.glsl diff --git a/src/glsl/horizon_vert.glsl b/src/noggit/rendering/glsl/horizon_vert.glsl similarity index 100% rename from src/glsl/horizon_vert.glsl rename to src/noggit/rendering/glsl/horizon_vert.glsl diff --git a/src/glsl/liquid_frag.glsl b/src/noggit/rendering/glsl/liquid_frag.glsl similarity index 100% rename from src/glsl/liquid_frag.glsl rename to src/noggit/rendering/glsl/liquid_frag.glsl diff --git a/src/glsl/liquid_vert.glsl b/src/noggit/rendering/glsl/liquid_vert.glsl similarity index 100% rename from src/glsl/liquid_vert.glsl rename to src/noggit/rendering/glsl/liquid_vert.glsl diff --git a/src/glsl/m2_box_frag.glsl b/src/noggit/rendering/glsl/m2_box_frag.glsl similarity index 100% rename from src/glsl/m2_box_frag.glsl rename to src/noggit/rendering/glsl/m2_box_frag.glsl diff --git a/src/glsl/m2_box_vert.glsl b/src/noggit/rendering/glsl/m2_box_vert.glsl similarity index 100% rename from src/glsl/m2_box_vert.glsl rename to src/noggit/rendering/glsl/m2_box_vert.glsl diff --git a/src/glsl/m2_frag.glsl b/src/noggit/rendering/glsl/m2_frag.glsl similarity index 100% rename from src/glsl/m2_frag.glsl rename to src/noggit/rendering/glsl/m2_frag.glsl diff --git a/src/glsl/m2_vert.glsl b/src/noggit/rendering/glsl/m2_vert.glsl similarity index 100% rename from src/glsl/m2_vert.glsl rename to src/noggit/rendering/glsl/m2_vert.glsl diff --git a/src/glsl/mfbo_frag.glsl b/src/noggit/rendering/glsl/mfbo_frag.glsl similarity index 100% rename from src/glsl/mfbo_frag.glsl rename to src/noggit/rendering/glsl/mfbo_frag.glsl diff --git a/src/glsl/mfbo_vert.glsl b/src/noggit/rendering/glsl/mfbo_vert.glsl similarity index 100% rename from src/glsl/mfbo_vert.glsl rename to src/noggit/rendering/glsl/mfbo_vert.glsl diff --git a/src/glsl/occluder_frag.glsl b/src/noggit/rendering/glsl/occluder_frag.glsl similarity index 100% rename from src/glsl/occluder_frag.glsl rename to src/noggit/rendering/glsl/occluder_frag.glsl diff --git a/src/glsl/occluder_vert.glsl b/src/noggit/rendering/glsl/occluder_vert.glsl similarity index 100% rename from src/glsl/occluder_vert.glsl rename to src/noggit/rendering/glsl/occluder_vert.glsl diff --git a/src/glsl/particle_frag.glsl b/src/noggit/rendering/glsl/particle_frag.glsl similarity index 100% rename from src/glsl/particle_frag.glsl rename to src/noggit/rendering/glsl/particle_frag.glsl diff --git a/src/glsl/particle_vert.glsl b/src/noggit/rendering/glsl/particle_vert.glsl similarity index 100% rename from src/glsl/particle_vert.glsl rename to src/noggit/rendering/glsl/particle_vert.glsl diff --git a/src/glsl/ribbon_frag.glsl b/src/noggit/rendering/glsl/ribbon_frag.glsl similarity index 100% rename from src/glsl/ribbon_frag.glsl rename to src/noggit/rendering/glsl/ribbon_frag.glsl diff --git a/src/glsl/ribbon_vert.glsl b/src/noggit/rendering/glsl/ribbon_vert.glsl similarity index 100% rename from src/glsl/ribbon_vert.glsl rename to src/noggit/rendering/glsl/ribbon_vert.glsl diff --git a/src/noggit/rendering/glsl/sphere_frag.glsl b/src/noggit/rendering/glsl/sphere_frag.glsl new file mode 100644 index 00000000..cde311a8 --- /dev/null +++ b/src/noggit/rendering/glsl/sphere_frag.glsl @@ -0,0 +1,10 @@ +#version 330 core + +uniform vec4 color; + +out vec4 out_color; + +void main() +{ + out_color = color; +} \ No newline at end of file diff --git a/src/noggit/rendering/glsl/sphere_vert.glsl b/src/noggit/rendering/glsl/sphere_vert.glsl new file mode 100644 index 00000000..0a38b541 --- /dev/null +++ b/src/noggit/rendering/glsl/sphere_vert.glsl @@ -0,0 +1,15 @@ +#version 330 core + +in vec4 position; + +uniform mat4 model_view_projection; +uniform vec3 origin; +uniform float radius; + +void main() +{ + vec4 pos = position; + pos.xyz *= radius; + pos.xyz += origin; + gl_Position = model_view_projection * pos; +} \ No newline at end of file diff --git a/src/noggit/rendering/glsl/square_frag.glsl b/src/noggit/rendering/glsl/square_frag.glsl new file mode 100644 index 00000000..cde311a8 --- /dev/null +++ b/src/noggit/rendering/glsl/square_frag.glsl @@ -0,0 +1,10 @@ +#version 330 core + +uniform vec4 color; + +out vec4 out_color; + +void main() +{ + out_color = color; +} \ No newline at end of file diff --git a/src/noggit/rendering/glsl/square_vert.glsl b/src/noggit/rendering/glsl/square_vert.glsl new file mode 100644 index 00000000..e2148629 --- /dev/null +++ b/src/noggit/rendering/glsl/square_vert.glsl @@ -0,0 +1,24 @@ +#version 330 core + +in vec4 position; + +uniform mat4 model_view_projection; +uniform vec3 origin; +uniform float radius; +uniform float inclination; +uniform float orientation; + +void main() +{ + vec4 pos = position; + float cos_o = cos(orientation); + float sin_o = sin(orientation); + + pos.y += pos.x * tan(inclination) * radius; + + pos.x = (position.x*cos_o - position.z * sin_o) * radius; + pos.z = (position.z*cos_o + position.x * sin_o) * radius; + + pos.xyz += origin; + gl_Position = model_view_projection * pos; +} \ No newline at end of file diff --git a/src/glsl/terrain_frag.glsl b/src/noggit/rendering/glsl/terrain_frag.glsl similarity index 100% rename from src/glsl/terrain_frag.glsl rename to src/noggit/rendering/glsl/terrain_frag.glsl diff --git a/src/glsl/terrain_vert.glsl b/src/noggit/rendering/glsl/terrain_vert.glsl similarity index 100% rename from src/glsl/terrain_vert.glsl rename to src/noggit/rendering/glsl/terrain_vert.glsl diff --git a/src/noggit/rendering/glsl/wire_box_frag.glsl b/src/noggit/rendering/glsl/wire_box_frag.glsl new file mode 100644 index 00000000..cde311a8 --- /dev/null +++ b/src/noggit/rendering/glsl/wire_box_frag.glsl @@ -0,0 +1,10 @@ +#version 330 core + +uniform vec4 color; + +out vec4 out_color; + +void main() +{ + out_color = color; +} \ No newline at end of file diff --git a/src/noggit/rendering/glsl/wire_box_vert.glsl b/src/noggit/rendering/glsl/wire_box_vert.glsl new file mode 100644 index 00000000..649ba566 --- /dev/null +++ b/src/noggit/rendering/glsl/wire_box_vert.glsl @@ -0,0 +1,11 @@ +#version 330 core + +uniform vec3 pointPositions[8]; +uniform mat4 model_view; +uniform mat4 projection; +uniform mat4 transform; + +void main() +{ + gl_Position = projection * model_view * transform * vec4(pointPositions[gl_VertexID], 1.0); +} \ No newline at end of file diff --git a/src/glsl/wmo_frag.glsl b/src/noggit/rendering/glsl/wmo_frag.glsl similarity index 100% rename from src/glsl/wmo_frag.glsl rename to src/noggit/rendering/glsl/wmo_frag.glsl diff --git a/src/glsl/wmo_vert.glsl b/src/noggit/rendering/glsl/wmo_vert.glsl similarity index 100% rename from src/glsl/wmo_vert.glsl rename to src/noggit/rendering/glsl/wmo_vert.glsl diff --git a/src/noggit/ui/tools/PreviewRenderer/PreviewRenderer.hpp b/src/noggit/ui/tools/PreviewRenderer/PreviewRenderer.hpp index 419a730f..fd2635e4 100644 --- a/src/noggit/ui/tools/PreviewRenderer/PreviewRenderer.hpp +++ b/src/noggit/ui/tools/PreviewRenderer/PreviewRenderer.hpp @@ -9,8 +9,8 @@ #include #include #include -#include -#include +#include +#include "noggit/rendering/LiquidTextureManager.hpp" #include #include @@ -68,7 +68,7 @@ class PreviewRenderer : public Noggit::Ui::Tools::ViewportManager::Viewport std::vector _model_instances; std::vector _wmo_instances; - OpenGL::primitives::grid _grid; + Noggit::Rendering::Primitives::Grid _grid; float _animtime = 0.f; diff --git a/src/opengl/primitives.cpp b/src/opengl/primitives.cpp deleted file mode 100644 index 5750f382..00000000 --- a/src/opengl/primitives.cpp +++ /dev/null @@ -1,508 +0,0 @@ -// This file is part of Noggit3, licensed under GNU General Public License (version 3). - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -namespace OpenGL -{ - namespace primitives - { - void wire_box::draw ( glm::mat4x4 const& model_view - , glm::mat4x4 const& projection - , glm::mat4x4 const& transform - , glm::vec4 const& color - , glm::vec3 const& min_point - , glm::vec3 const& max_point - ) - { - - if (!_buffers_are_setup) - { - setup_buffers(); - } - - OpenGL::Scoped::use_program wire_box_shader {*_program.get()}; - - auto points = math::box_points(min_point, max_point); - - - auto glmPoints = std::vector(); - - for(auto const point : points) - { - glmPoints.push_back(glm::vec3(point.x, point.y, point.z)); - } - - wire_box_shader.uniform("model_view", model_view); - wire_box_shader.uniform("projection", projection); - wire_box_shader.uniform("transform", transform); - wire_box_shader.uniform("color", color); - wire_box_shader.uniform("pointPositions", glmPoints); - - OpenGL::Scoped::bool_setter const line_smooth; - gl.hint(GL_LINE_SMOOTH_HINT, GL_NICEST); - - OpenGL::Scoped::vao_binder const _(_vao[0]); - - gl.drawElements (GL_LINE_STRIP, _indices, 16, GL_UNSIGNED_BYTE, nullptr); - } - - void wire_box::setup_buffers() - { - _program.reset(new OpenGL::program( {{ GL_VERTEX_SHADER - , R"code( - #version 330 core - - uniform vec3 pointPositions[8]; - uniform mat4 model_view; - uniform mat4 projection; - uniform mat4 transform; - - void main() - { - gl_Position = projection * model_view * transform * vec4(pointPositions[gl_VertexID], 1.0); - } - )code"} - , { GL_FRAGMENT_SHADER - , R"code( - #version 330 core - - uniform vec4 color; - - out vec4 out_color; - - void main() - { - out_color = color; - } - )code" - } - })); - - _vao.upload(); - _buffers.upload(); - - //std::vector positions (math::box_points (min_point, max_point)); - - static std::array const indices - {{5, 7, 3, 2, 0, 1, 3, 1, 5, 4, 0, 4, 6, 2, 6, 7}}; - - Scoped::buffer_binder const index_buffer (_indices); - gl.bufferData ( GL_ELEMENT_ARRAY_BUFFER - , indices.size() * sizeof (*indices.data()) - , indices.data() - , GL_STATIC_DRAW - ); - - OpenGL::Scoped::use_program shader (*_program.get()); - - OpenGL::Scoped::vao_binder const _ (_vao[0]); - - _buffers_are_setup = true; - - } - - void wire_box::unload() - { - _vao.unload(); - _buffers.unload(); - _program.reset(); - - _buffers_are_setup = false; - } - - void grid::draw(glm::mat4x4 const& mvp - , glm::vec3 const& pos - , glm::vec4 const& color - , float radius - ) - { - if (!_buffers_are_setup) - { - setup_buffers(); - } - - OpenGL::Scoped::use_program sphere_shader {*_program.get()}; - - sphere_shader.uniform("model_view_projection", mvp); - sphere_shader.uniform("origin", glm::vec3(pos.x,pos.y,pos.z)); - sphere_shader.uniform("color", color); - sphere_shader.uniform("radius", radius); - - OpenGL::Scoped::vao_binder const _(_vao[0]); - gl.drawElements(GL_LINES, _indices_vbo, _indice_count, GL_UNSIGNED_SHORT, nullptr); - } - - - void grid::setup_buffers() - { - _vao.upload(); - _buffers.upload(); - - _program.reset(new OpenGL::program({{ GL_VERTEX_SHADER - , R"code( - #version 330 core - - in vec4 position; - - uniform mat4 model_view_projection; - uniform vec3 origin; - uniform float radius; - - void main() - { - vec4 pos = position; - pos.xyz *= radius; - - vec3 origin_fixed = vec3(origin.x - radius / 2.0f, origin.y, origin.z - radius / 2.0f); - pos.xyz += origin_fixed; - gl_Position = model_view_projection * pos; - } - )code" - } - , { GL_FRAGMENT_SHADER - , R"code( - #version 330 core - - uniform vec4 color; - - out vec4 out_color; - - void main() - { - out_color = color; - } - )code" - } - })); - - - std::vector vertices; - std::vector indices; - - int slices = 20; - - for(int j = 0; j <= slices; ++j) - { - for(int i = 0; i <= slices; ++i) - { - float x = static_cast(i) / static_cast(slices); - float y = 0; - float z = static_cast(j) / static_cast(slices); - vertices.push_back(glm::vec3(x, y, z)); - } - } - - for(int j = 0; j < slices; ++j) - { - for(int i = 0; i < slices; ++i) - { - - int row1 = j * (slices + 1); - int row2 = (j + 1) * (slices + 1); - - indices.push_back(row1 + i); - indices.push_back(row1 + i + 1); - indices.push_back(row1 + i + 1); - indices.push_back(row2 + i + 1); - - indices.push_back(row2 + i + 1); - indices.push_back(row2 + i); - indices.push_back(row2 + i); - indices.push_back(row1 + i); - - } - } - - _indice_count = indices.size(); - - gl.bufferData - (_vertices_vbo, vertices, GL_STATIC_DRAW); - gl.bufferData - (_indices_vbo, indices, GL_STATIC_DRAW); - - - 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 grid::unload() - { - _vao.unload(); - _buffers.unload(); - _program.reset(); - - _buffers_are_setup = false; - } - - - void sphere::draw(glm::mat4x4 const& mvp - , glm::vec3 const& pos - , glm::vec4 const& color - , float radius - ) - { - if (!_buffers_are_setup) - { - setup_buffers(); - } - - OpenGL::Scoped::use_program sphere_shader {*_program.get()}; - - sphere_shader.uniform("model_view_projection", mvp); - sphere_shader.uniform("origin", glm::vec3(pos.x,pos.y,pos.z)); - sphere_shader.uniform("radius", radius); - sphere_shader.uniform("color", color); - - OpenGL::Scoped::vao_binder const _(_vao[0]); - gl.drawElements(GL_TRIANGLES, _indices_vbo, _indice_count, GL_UNSIGNED_SHORT, nullptr); - } - - - void sphere::setup_buffers() - { - _vao.upload(); - _buffers.upload(); - - std::vector vertices; - std::vector indices; - - _program.reset(new OpenGL::program({{ GL_VERTEX_SHADER - , R"code( -#version 330 core - -in vec4 position; - -uniform mat4 model_view_projection; -uniform vec3 origin; -uniform float radius; - -void main() -{ - vec4 pos = position; - pos.xyz *= radius; - pos.xyz += origin; - gl_Position = model_view_projection * pos; -} -)code" - } - , { GL_FRAGMENT_SHADER - , R"code( -#version 330 core - -uniform vec4 color; - -out vec4 out_color; - -void main() -{ - out_color = color; -} -)code" - } - })); - - - int segment = 27; - - // add overlapping vertices at the end for an easier vertices generations - for (int rotation_step = 0; rotation_step <= segment; ++rotation_step) - { - math::degrees rotation(360.f*rotation_step / static_cast(segment)); - auto rotationQuat = glm::angleAxis(rotation._, glm::vec3(0, 0, 1)); - for (int i = 0; i < segment; ++i) - { - float x = glm::cos(math::radians(math::degrees(i * 360 / segment))._); - float z = glm::sin(math::radians(math::degrees(i * 360 / segment))._); - - glm::vec4 v(x, 0.f, z,0.0f); - - vertices.emplace_back(glm::toMat4(rotationQuat) * v); - - if (rotation_step < segment) - { - indices.emplace_back(i + rotation_step*segment); - indices.emplace_back(((i + 1) % segment) + rotation_step * segment); - indices.emplace_back(i + (rotation_step+1) * segment); - - indices.emplace_back(i + (rotation_step+1) * segment); - indices.emplace_back(((i + 1) % segment) + rotation_step * segment); - indices.emplace_back(((i + 1) % segment) + (rotation_step+1) * segment); - } - } - } - - _indice_count = indices.size(); - - gl.bufferData - (_vertices_vbo, vertices, GL_STATIC_DRAW); - gl.bufferData - (_indices_vbo, indices, GL_STATIC_DRAW); - - - 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 sphere::unload() - { - _vao.unload(); - _buffers.unload(); - _program.reset(); - - _buffers_are_setup = false; - } - - - void square::draw(glm::mat4x4 const& mvp - , glm::vec3 const& pos - , float radius - , math::radians inclination - , math::radians orientation - , glm::vec4 const& color - ) - { - if (!_buffers_are_setup) - { - setup_buffers(); - } - - OpenGL::Scoped::use_program sphere_shader {*_program.get()}; - - sphere_shader.uniform("model_view_projection", mvp); - sphere_shader.uniform("origin", glm::vec3(pos.x,pos.y,pos.z)); - sphere_shader.uniform("radius", radius); - sphere_shader.uniform("inclination", inclination._); - sphere_shader.uniform("orientation", orientation._); - sphere_shader.uniform("color", color); - - OpenGL::Scoped::vao_binder const _ (_vao[0]); - gl.drawElements(GL_TRIANGLES, _indices_vbo, 6, GL_UNSIGNED_SHORT, nullptr); - } - - - void square::setup_buffers() - { - _vao.upload(); - _buffers.upload(); - - std::vector vertices = - { - {-1.f, 0.f, -1.f} - ,{-1.f, 0.f, 1.f} - ,{ 1.f, 0.f, 1.f} - ,{ 1.f, 0.f, -1.f} - }; - std::vector indices = {0,1,2, 2,3,0}; - - _program.reset(new OpenGL::program({{ GL_VERTEX_SHADER - , R"code( -#version 330 core - -in vec4 position; - -uniform mat4 model_view_projection; -uniform vec3 origin; -uniform float radius; -uniform float inclination; -uniform float orientation; - -void main() -{ - vec4 pos = position; - float cos_o = cos(orientation); - float sin_o = sin(orientation); - - pos.y += pos.x * tan(inclination) * radius; - - pos.x = (position.x*cos_o - position.z * sin_o) * radius; - pos.z = (position.z*cos_o + position.x * sin_o) * radius; - - pos.xyz += origin; - gl_Position = model_view_projection * pos; -} -)code" - } - , { GL_FRAGMENT_SHADER - , R"code( -#version 330 core - -uniform vec4 color; - -out vec4 out_color; - -void main() -{ - out_color = color; -} -)code" - } - })); - - - gl.bufferData - (_vertices_vbo, vertices, GL_STATIC_DRAW); - gl.bufferData - (_indices_vbo, indices, GL_STATIC_DRAW); - - - 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 square::unload() - { - _vao.unload(); - _buffers.unload(); - _program.reset(); - - _buffers_are_setup = false; - - } - } -} diff --git a/src/opengl/primitives.hpp b/src/opengl/primitives.hpp deleted file mode 100644 index 08a3b424..00000000 --- a/src/opengl/primitives.hpp +++ /dev/null @@ -1,134 +0,0 @@ -// This file is part of Noggit3, licensed under GNU General Public License (version 3). - -#pragma once - -#include -#include -#include - -#include -#include - -namespace math -{ - struct vector_3d; - struct vector_4d; -} - -namespace OpenGL -{ - namespace primitives - { - class wire_box - { - public: - wire_box() {} - wire_box( const wire_box&); - wire_box& operator=( wire_box& box ) { return *this; }; - - public: - static wire_box& getInstance(Noggit::NoggitRenderContext context) - { - static std::unordered_map instances; - - if (instances.find(context) == instances.end()) - { - wire_box instance; - instances[context] = instance; - } - - return instances.at(context); - } - - void draw ( glm::mat4x4 const& model_view - , glm::mat4x4 const& projection - , glm::mat4x4 const& transform - , glm::vec4 const& color - , glm::vec3 const& min_point - , glm::vec3 const& max_point - ); - - void unload(); - - private: - bool _buffers_are_setup = false; - - void setup_buffers(); - - Scoped::deferred_upload_vertex_arrays<1> _vao; - Scoped::deferred_upload_buffers<1> _buffers; - GLuint const& _indices = _buffers[0]; - std::unique_ptr _program; - }; - - class grid - { - public: - void draw(glm::mat4x4 const& mvp - , glm::vec3 const& pos - , glm::vec4 const& color - , float radius - ); - void unload(); - private: - bool _buffers_are_setup = false; - - void setup_buffers(); - - int _indice_count = 0; - - Scoped::deferred_upload_vertex_arrays<1> _vao; - Scoped::deferred_upload_buffers<2> _buffers; - GLuint const& _vertices_vbo = _buffers[0]; - GLuint const& _indices_vbo = _buffers[1]; - std::unique_ptr _program; - }; - - class sphere - { - public: - void draw(glm::mat4x4 const& mvp - , glm::vec3 const& pos - , glm::vec4 const& color - , float radius - ); - void unload(); - - private: - bool _buffers_are_setup = false; - - void setup_buffers(); - - int _indice_count = 0; - - Scoped::deferred_upload_vertex_arrays<1> _vao; - Scoped::deferred_upload_buffers<2> _buffers; - GLuint const& _vertices_vbo = _buffers[0]; - GLuint const& _indices_vbo = _buffers[1]; - std::unique_ptr _program; - }; - - class square - { - public: - void draw(glm::mat4x4 const& mvp - , glm::vec3 const& pos - , float radius // radius of the biggest circle fitting inside the square drawn - , math::radians inclination - , math::radians orientation - , glm::vec4 const& color - ); - void unload(); - private: - bool _buffers_are_setup = false; - - void setup_buffers(); - - Scoped::deferred_upload_vertex_arrays<1> _vao; - Scoped::deferred_upload_buffers<2> _buffers; - GLuint const& _vertices_vbo = _buffers[0]; - GLuint const& _indices_vbo = _buffers[1]; - std::unique_ptr _program; - }; - } -}