From 0e924656fdab59a9ac50e98271a5bd44f6b61cbb Mon Sep 17 00:00:00 2001 From: Jay Robson Date: Fri, 16 Feb 2024 21:50:46 +1100 Subject: [PATCH] improved renderer --- assets/shader/main.fsh | 4 +- assets/shader/main.vsh | 6 +- src/graphics/mesh/arrays.cpp | 5 +- src/graphics/mesh/arrays.hpp | 1 + src/graphics/mesh/mesh.hpp | 1 + src/graphics/monitor/core.cpp | 164 ++++++++++++++++++++++------------ src/graphics/monitor/core.hpp | 3 + 7 files changed, 121 insertions(+), 63 deletions(-) diff --git a/assets/shader/main.fsh b/assets/shader/main.fsh index 13e4a90..a31d3f1 100644 --- a/assets/shader/main.fsh +++ b/assets/shader/main.fsh @@ -2,7 +2,7 @@ #version 460 core #extension GL_ARB_bindless_texture : require -in float brightness; +in vec4 colour; in flat sampler2D tex; in vec2 texPos; @@ -13,7 +13,7 @@ uniform mat4 tex_mat; void main() { vec4 texdata = texture2D(tex, texPos); - FragColour = tex_mat * texdata * vec4(vec3(brightness), 1); + FragColour = tex_mat * texdata * colour; if(FragColour.a == 0) discard; } diff --git a/assets/shader/main.vsh b/assets/shader/main.vsh index d7d62c3..115f28b 100644 --- a/assets/shader/main.vsh +++ b/assets/shader/main.vsh @@ -6,12 +6,13 @@ layout (location = 0) in sampler2D aTex; layout (location = 1) in vec2 aTexPos; layout (location = 2) in vec4 aPos; layout (location = 3) in vec3 aNormal; +layout (location = 4) in vec4 aColour; uniform mat4 model; uniform mat4 camera; uniform mat4 projection; -out float brightness; +out vec4 colour; out flat sampler2D tex; out vec2 texPos; @@ -20,8 +21,9 @@ void main() vec4 pos = camera * model * aPos; vec3 cNormal = vec3(0.f, 0.f, 1.f) * mat3(camera * model); - brightness = dot(normalize(aNormal), normalize(cNormal)) * 0.25f + 0.75f; + float brightness = dot(normalize(aNormal), normalize(cNormal)) * 0.25f + 0.75f; + colour = aColour * vec4(vec3(brightness), 1); gl_Position = projection * pos; texPos = aTexPos; tex = aTex; diff --git a/src/graphics/mesh/arrays.cpp b/src/graphics/mesh/arrays.cpp index 8750dfc..ce943de 100644 --- a/src/graphics/mesh/arrays.cpp +++ b/src/graphics/mesh/arrays.cpp @@ -10,8 +10,6 @@ using namespace Sim::Graphics; -static unsigned int vao, vbo, ebo; - static void* ptr_diff(void* a, void* b) { return (void*)((size_t)a - (size_t)b); @@ -32,6 +30,9 @@ void Arrays::vertex_attrib_pointers() glVertexAttribPointer(3, 3, GL_FLOAT, false, sizeof(v), ptr_diff(&v.normal, &v)); glEnableVertexAttribArray(3); + + glVertexAttribPointer(4, 4, GL_FLOAT, false, sizeof(v), ptr_diff(&v.colour, &v)); + glEnableVertexAttribArray(4); } glm::mat4 Arrays::colour(glm::vec4 c) diff --git a/src/graphics/mesh/arrays.hpp b/src/graphics/mesh/arrays.hpp index aea2384..2f7014d 100644 --- a/src/graphics/mesh/arrays.hpp +++ b/src/graphics/mesh/arrays.hpp @@ -12,6 +12,7 @@ struct Vertex glm::vec2 texpos = {0, 0}; glm::vec4 pos = {0, 0, 0, 1}; glm::vec3 normal = {0, 0, 0}; + glm::vec4 colour = {1, 1, 1, 1}; }; void vertex_attrib_pointers(); diff --git a/src/graphics/mesh/mesh.hpp b/src/graphics/mesh/mesh.hpp index ccce880..e3f9d2e 100644 --- a/src/graphics/mesh/mesh.hpp +++ b/src/graphics/mesh/mesh.hpp @@ -26,6 +26,7 @@ struct Mesh void load_model(std::string path) { load_model(".", path); } void load_text(const char* text, double size); void add(const Mesh& o, glm::mat4 mat); + void add(const Mesh& o) { add(o, glm::mat4(1)); } Mesh to_lines() const; bool check_focus() const; diff --git a/src/graphics/monitor/core.cpp b/src/graphics/monitor/core.cpp index f6ac8e4..a63e49d 100644 --- a/src/graphics/monitor/core.cpp +++ b/src/graphics/monitor/core.cpp @@ -33,6 +33,13 @@ static void set_all(bool state) struct CoreMonitor : public Focus::FocusType { + Core* parent; + + CoreMonitor(Core* parent) + { + this->parent = parent; + } + virtual void on_keypress(int key, int sc, int action, int mods) { if(action != GLFW_PRESS) @@ -68,15 +75,27 @@ struct CoreMonitor : public Focus::FocusType case GLFW_KEY_KP_2: sys.reactor->move_cursor(sys.reactor->height); break; + default: + return; } + + parent->is_dirty = true; } }; struct CoreJoystick : public Focus::FocusType { + Core* parent; + + CoreJoystick(Core* parent) + { + this->parent = parent; + } + virtual void on_cursor_pos(double x, double y) { Sim::System::active.reactor->add_rod_speed(y * 1e-6); + parent->is_dirty = true; } virtual ~CoreJoystick() @@ -104,8 +123,8 @@ Core::Core() void Core::init() { - mesh1.model_matrix = Locations::monitors[2]; - mesh1.colour_matrix = Arrays::colour({1, 1, 1, 1}); + mesh1.model_matrix = mesh2.model_matrix = Locations::monitors[2]; + mesh1.colour_matrix = mesh2.colour_matrix = Arrays::colour({1, 1, 1, 1}); Sim::Graphics::Mesh rmesh; @@ -113,19 +132,6 @@ void Core::init() mesh1.bind(); mesh1.set(rmesh, GL_STATIC_DRAW); - unsigned int indices[] = {0, 1, 3, 0, 3, 2}; - Arrays::Vertex vertices[] = { - {Texture::handle_white, {0, 0}, {-0.75, -0.75, 0, 1}, {0, 0, -1}}, - {Texture::handle_white, {0, 1}, {-0.75, 0.75, 0, 1}, {0, 0, -1}}, - {Texture::handle_white, {1, 0}, { 0.75, -0.75, 0, 1}, {0, 0, -1}}, - {Texture::handle_white, {1, 1}, { 0.75, 0.75, 0, 1}, {0, 0, -1}}, - }; - - rmesh.set_indices(indices, 6); - rmesh.set_vertices(vertices, 4); - mesh2.bind(); - mesh2.set(rmesh, GL_STATIC_DRAW); - m_buttons[0].load_model("../assets/model/", "reactor_core_button1.stl"); m_buttons[1].load_model("../assets/model/", "reactor_core_button2.stl"); m_buttons[2].load_model("../assets/model/", "reactor_core_button3.stl"); @@ -140,37 +146,86 @@ void Core::init() m_scram.load_model("../assets/model/", "reactor_core_scram.stl"); } +static Mesh add_dot(glm::mat4 model_mat, glm::vec4 colour) +{ + unsigned int indices[] = {0, 1, 3, 0, 3, 2}; + Arrays::Vertex vertices[] = { + {Texture::handle_white, {0, 0}, model_mat * glm::vec4(-0.75, -0.75, 0, 1), {0, 0, -1}, colour}, + {Texture::handle_white, {0, 1}, model_mat * glm::vec4(-0.75, 0.75, 0, 1), {0, 0, -1}, colour}, + {Texture::handle_white, {1, 0}, model_mat * glm::vec4( 0.75, -0.75, 0, 1), {0, 0, -1}, colour}, + {Texture::handle_white, {1, 1}, model_mat * glm::vec4( 0.75, 0.75, 0, 1), {0, 0, -1}, colour}, + }; + + Mesh mesh; + mesh.set_indices(indices, 6); + mesh.set_vertices(vertices, 4); + return mesh; +} + void Core::update(double dt) { Sim::System& sys = Sim::System::active; - if(m_monitor.check_focus()) - Focus::set(std::make_unique()); - if(m_joystick.check_focus()) - Focus::set(std::make_unique()); - if(m_scram.check_focus()) + if(m_monitor.check_focus()) { + Focus::set(std::make_unique(this)); + } + if(m_joystick.check_focus()) { + Focus::set(std::make_unique(this)); + } + if(m_scram.check_focus()) { sys.reactor->scram(); - if(m_buttons[0].check_focus()) + is_dirty = true; + } + if(m_buttons[0].check_focus()) { set_all(true); - if(m_buttons[1].check_focus()) + is_dirty = true; + } + if(m_buttons[1].check_focus()) { sys.reactor->move_cursor(-sys.reactor->height); - if(m_buttons[2].check_focus()) + is_dirty = true; + } + if(m_buttons[2].check_focus()) { set_all(false); - if(m_buttons[3].check_focus()) + is_dirty = true; + } + if(m_buttons[3].check_focus()) { sys.reactor->move_cursor(-1); - if(m_buttons[4].check_focus()) + is_dirty = true; + } + if(m_buttons[4].check_focus()) { sys.reactor->toggle_selected(); - if(m_buttons[5].check_focus()) + is_dirty = true; + } + if(m_buttons[5].check_focus()) { sys.reactor->move_cursor(1); - if(m_buttons[6].check_focus()) + is_dirty = true; + } + if(m_buttons[6].check_focus()) { sys.reactor->reset_rod_speed(); - if(m_buttons[7].check_focus()) + is_dirty = true; + } + if(m_buttons[7].check_focus()) { sys.reactor->move_cursor(sys.reactor->height); -} + is_dirty = true; + } -void Core::render() -{ - Sim::System& sys = Sim::System::active; + clock_now += dt; + + if(clock_at + 1.0/30.0 > clock_now) + { + if(!is_dirty) + { + return; + } + } + + else + { + clock_at += 1.0/30.0; + } + + Sim::Graphics::Mesh rmesh; + is_dirty = false; double step = 1 / (sys.vessel->diameter / sys.reactor->cell_width * 0.8); double sx = 0.5 - (sys.reactor->width - 1) * step / 2.0; @@ -180,14 +235,7 @@ void Core::render() glm::mat4 mat_select = glm::translate(glm::mat4(1), glm::vec3(-0.8, -0.8, -0.001)) * glm::scale(glm::mat4(1), glm::vec3(0.25, 0.25, 1)); glm::mat4 mat_cursor = glm::translate(glm::mat4(1), glm::vec3(-0.8, 0.8, -0.001)) * glm::scale(glm::mat4(1), glm::vec3(0.25, 0.25, 1)); glm::mat4 mat_spec = glm::translate(glm::mat4(1), glm::vec3(0.8, -0.8, -0.001)) * glm::scale(glm::mat4(1), glm::vec3(0.25, 0.25, 1)); - - mesh1.bind(); - mesh1.uniform(); - mesh1.render(); - mesh2.bind(); - - // this renderer is disgusting - + for(int i = 0; i < sys.reactor->size; i++) { int x = i % sys.reactor->width; @@ -210,36 +258,38 @@ void Core::render() continue; } - glm::mat4 mat = mesh1.model_matrix * glm::translate(glm::mat4(1), glm::vec3(ox, oy, 0)) * mat_scale; + glm::mat4 mat = glm::translate(glm::mat4(1), glm::vec3(ox, oy, 0)) * mat_scale; - mesh2.model_matrix = mat; - mesh2.colour_matrix = Arrays::colour(colour_heat); - mesh2.uniform(); - mesh2.render(); + rmesh.add(add_dot(mat, colour_heat)); if(sys.reactor->cursor == i) { - mesh2.model_matrix = mat * mat_cursor; - mesh2.colour_matrix = Arrays::colour({1, 0, 0, 1}); - mesh2.uniform(); - mesh2.render(); + rmesh.add(add_dot(mat * mat_cursor, {1, 0, 0, 1})); } if(r->selected) { - mesh2.model_matrix = mat * mat_select; - mesh2.colour_matrix = Arrays::colour({1, 1, 0, 1}); - mesh2.uniform(); - mesh2.render(); + rmesh.add(add_dot(mat * mat_select, {1, 1, 0, 1})); } if(colour_spec[3] != 0) { - mesh2.model_matrix = mat * mat_spec; - mesh2.colour_matrix = Arrays::colour(colour_spec); - mesh2.uniform(); - mesh2.render(); + rmesh.add(add_dot(mat * mat_spec, colour_spec)); } } + + mesh2.bind(); + mesh2.set(rmesh, GL_DYNAMIC_DRAW); +} + +void Core::render() +{ + mesh1.bind(); + mesh1.uniform(); + mesh1.render(); + + mesh2.bind(); + mesh2.uniform(); + mesh2.render(); } diff --git a/src/graphics/monitor/core.hpp b/src/graphics/monitor/core.hpp index e4a3189..a78eb22 100644 --- a/src/graphics/monitor/core.hpp +++ b/src/graphics/monitor/core.hpp @@ -9,6 +9,7 @@ namespace Sim::Graphics::Monitor class Core { Sim::Graphics::GLMesh mesh1, mesh2; + double clock_at = 0, clock_now = 0; Sim::Graphics::Mesh m_monitor; Sim::Graphics::Mesh m_buttons[9]; @@ -17,6 +18,8 @@ class Core public: + bool is_dirty = false; + Core(); void init(); void update(double dt);