diff --git a/assets/model/reactor_core_button1.stl b/assets/model/reactor_core_button1.stl new file mode 100644 index 0000000..3d3fce2 --- /dev/null +++ b/assets/model/reactor_core_button1.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b594ea9cd0031912e8a5c9863dea546e73378f35d47e656af52294c50b0aca51 +size 184 diff --git a/assets/model/reactor_core_button2.stl b/assets/model/reactor_core_button2.stl new file mode 100644 index 0000000..15f9cfe --- /dev/null +++ b/assets/model/reactor_core_button2.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:190c68a2ed7921f81550d7941702b831df25d98326dcd56014007445847fe452 +size 184 diff --git a/assets/model/reactor_core_button3.stl b/assets/model/reactor_core_button3.stl new file mode 100644 index 0000000..dcbead4 --- /dev/null +++ b/assets/model/reactor_core_button3.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b3acb43261f03bccdc93fbcd98355d8cec0fed10128141fa28db9b7d1d492080 +size 184 diff --git a/assets/model/reactor_core_button4.stl b/assets/model/reactor_core_button4.stl new file mode 100644 index 0000000..fa8b86c --- /dev/null +++ b/assets/model/reactor_core_button4.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c128c721e16ccdbd9f74f5fa8904d318e102d259e6d00b357c57a5b37a0ac917 +size 184 diff --git a/assets/model/reactor_core_button5.stl b/assets/model/reactor_core_button5.stl new file mode 100644 index 0000000..48d2138 --- /dev/null +++ b/assets/model/reactor_core_button5.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b5224a7a9d5c33f58bda8ed3f8f81c437d0a66a067208efa3acbdf3fe7ad47a9 +size 184 diff --git a/assets/model/reactor_core_button6.stl b/assets/model/reactor_core_button6.stl new file mode 100644 index 0000000..1b129f5 --- /dev/null +++ b/assets/model/reactor_core_button6.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c83e3f6e3c70ace7038f8d14d3022cb29bee16fdba96b3ef74cbdb969519663f +size 184 diff --git a/assets/model/reactor_core_button7.stl b/assets/model/reactor_core_button7.stl new file mode 100644 index 0000000..217e8ac --- /dev/null +++ b/assets/model/reactor_core_button7.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:97395233de91c1ab2394815064b95577d084e4578e03047e73ea88fbe034889b +size 184 diff --git a/assets/model/reactor_core_button8.stl b/assets/model/reactor_core_button8.stl new file mode 100644 index 0000000..589996c --- /dev/null +++ b/assets/model/reactor_core_button8.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5d2579100bca7712adac18e6bc7e8464f1ea19bf018831ea2f2890fb1159e310 +size 184 diff --git a/assets/model/reactor_core_button9.stl b/assets/model/reactor_core_button9.stl new file mode 100644 index 0000000..a116721 --- /dev/null +++ b/assets/model/reactor_core_button9.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:21a64c7c0a83a701cee945727b1c75287c868dbc54acce4373748ddebe72c1ca +size 184 diff --git a/assets/model/reactor_core_joystick.stl b/assets/model/reactor_core_joystick.stl new file mode 100644 index 0000000..817f7a6 --- /dev/null +++ b/assets/model/reactor_core_joystick.stl @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5cf737ad5e6ac820cae1eaccdff3770800a5a163d130fb2a19d75653e066b937 +size 2384 diff --git a/assets/model/reactor_core_scram.stl b/assets/model/reactor_core_scram.stl index b4353eb..68ee5d2 100644 --- a/assets/model/reactor_core_scram.stl +++ b/assets/model/reactor_core_scram.stl @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:37aaffb2aa5cd87eaadda8a0eb15e86040b9eb880517eb9d6c82f82c17ae0373 -size 6284 +oid sha256:6f0f43d99e13f879e1158803a14a7427054a947535e30e34ba1511cccf40019c +size 1584 diff --git a/assets/unbaked/scene.blend b/assets/unbaked/scene.blend index b894166..e58cc31 100644 --- a/assets/unbaked/scene.blend +++ b/assets/unbaked/scene.blend @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:de45f07d6afa36e19985970817efc827b060d18a7166350dfd27d2df8d1b120f -size 80493828 +oid sha256:66f728c728db0905aadf7f0052cb42cbaf72308100e3088e271af70d22ab3ec4 +size 80123820 diff --git a/src/graphics/input/focus.cpp b/src/graphics/input/focus.cpp index 4b58b12..d7674a2 100644 --- a/src/graphics/input/focus.cpp +++ b/src/graphics/input/focus.cpp @@ -1,16 +1,23 @@ -#include "focus.hpp" -#include "../camera.hpp" -#include "../../util/math.hpp" -#include "mouse.hpp" - -#include - #include #include +#include "focus.hpp" +#include "../../util/math.hpp" +#include "../window.hpp" +#include "../camera.hpp" +#include "../resize.hpp" +#include "mouse.hpp" + +#include + +#include + using namespace sim::graphics; +static glm::vec<3, double> trigger_near; +static glm::vec<3, double> trigger_far; + static std::unique_ptr state = nullptr; static bool mouse_visible = false; static bool mouse_locked = false; @@ -44,9 +51,28 @@ void focus::on_mouse_button(int button, int action, int mods) state->on_mouse_button(button, action, mods); } - else if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) + else if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { triggered = true; + + if(is_mouse_locked()) + { + double mx, my; + mouse::get(mx, my); + + glm::vec2 wsize = resize::get_size(); + glm::vec4 viewport = glm::vec4(0, 0, wsize); + glm::vec2 mouse(mx, wsize.y - my); + + trigger_near = glm::unProject(glm::vec3(mouse, -1), camera::get_matrix(), window::projection_matrix, viewport); + trigger_far = glm::unProject(glm::vec3(mouse, 1), camera::get_matrix(), window::projection_matrix, viewport); + } + + else + { + trigger_near = camera::get_pos(); + trigger_far = trigger_near + camera::get_normal(); + } } } @@ -66,15 +92,30 @@ void focus::on_charcode(unsigned int c) } } +glm::vec<3, double> focus::get_trigger_near() +{ + return trigger_near; +} + +glm::vec<3, double> focus::get_trigger_far() +{ + return trigger_far; +} + void focus::update() { triggered = false; - bool locked = is_mouse_locked(); + bool c = is_mouse_locked(); - if(locked != mouse_visible) + if(state && !state->cursor_is_visible()) { - if(locked) + c = false; + } + + if(c != mouse_visible) + { + if(c) { mouse::show_cursor(); } @@ -84,7 +125,7 @@ void focus::update() mouse::hide_cursor(); } - mouse_visible = locked; + mouse_visible = c; } if(state) diff --git a/src/graphics/input/focus.hpp b/src/graphics/input/focus.hpp index b641f30..a586f0f 100644 --- a/src/graphics/input/focus.hpp +++ b/src/graphics/input/focus.hpp @@ -1,6 +1,8 @@ #pragma once +#include + #include namespace sim::graphics::focus @@ -9,6 +11,7 @@ namespace sim::graphics::focus struct focus_t { virtual ~focus_t() { } + virtual bool cursor_is_visible() { return true; } virtual void on_keypress(int key, int sc, int action, int mods) { } virtual void on_mouse_button(int button, int action, int mods) { } virtual void on_cursor_pos(double x, double y) { } @@ -21,6 +24,8 @@ void clear_focus(); bool is_triggered(); bool is_mouse_locked(); void clear_mouse_locked(); +glm::vec<3, double> get_trigger_near(); +glm::vec<3, double> get_trigger_far(); void set(std::unique_ptr f); void on_keypress(int key, int sc, int action, int mods); void on_mouse_button(int button, int action, int mods); diff --git a/src/graphics/input/mouse.cpp b/src/graphics/input/mouse.cpp index 5059b6a..4403e11 100644 --- a/src/graphics/input/mouse.cpp +++ b/src/graphics/input/mouse.cpp @@ -38,6 +38,11 @@ void mouse::get(double& x, double& y) y = ypos; } +glm::vec2 mouse::get() +{ + return {xpos, ypos}; +} + void mouse::show_cursor() { double x, y; diff --git a/src/graphics/input/mouse.hpp b/src/graphics/input/mouse.hpp index 0534ead..8d2dc86 100644 --- a/src/graphics/input/mouse.hpp +++ b/src/graphics/input/mouse.hpp @@ -1,10 +1,13 @@ #pragma once +#include + namespace sim::graphics::mouse { void init(); +glm::vec2 get(); void get(double& x, double& y); void show_cursor(); void hide_cursor(); diff --git a/src/graphics/mesh/mesh.cpp b/src/graphics/mesh/mesh.cpp index db7fde6..bf9644e 100644 --- a/src/graphics/mesh/mesh.cpp +++ b/src/graphics/mesh/mesh.cpp @@ -98,7 +98,10 @@ bool ray_intersects_triangle(vec3 ray_origin, bool mesh::check_focus(double len) const { - return focus::is_triggered() && check_intersect(camera::get_pos(), camera::get_normal() * len); + auto near = focus::get_trigger_near(); + auto far = focus::get_trigger_far(); + + return focus::is_triggered() && check_intersect(near, glm::normalize(far - near) * len); } bool mesh::check_focus() const diff --git a/src/graphics/monitor/core.cpp b/src/graphics/monitor/core.cpp index 6b2a0fe..3a04381 100644 --- a/src/graphics/monitor/core.cpp +++ b/src/graphics/monitor/core.cpp @@ -15,83 +15,42 @@ using namespace sim::graphics; using namespace sim::graphics::monitor; -struct core_focus_t : public focus::focus_t +struct core_joystick : public focus::focus_t { virtual void on_cursor_pos(double x, double y) { - sim::system::active.reactor->add_rod_speed(-y * 1e-6); + sim::system::active.reactor->add_rod_speed(y * 1e-6); } - void set_all(bool state) + virtual void on_mouse_button(int button, int action, int mods) { - for(int i = 0; i < sim::system::active.reactor->rods.size(); i++) + if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) { - sim::reactor::rod* r = sim::system::active.reactor->rods[i].get(); - - if(r->should_select()) - { - r->selected = state; - } + focus::clear_focus(); } } - void toggle_auto() + virtual bool cursor_is_visible() { - //TODO - } - - virtual void on_charcode(unsigned int c) - { - sim::system& sys = sim::system::active; - - switch(c) - { - case 'a': case 'A': - sys.reactor->move_cursor(-1); - break; - case 'd': case 'D': - sys.reactor->move_cursor(1); - break; - case 'w': case 'W': - sys.reactor->move_cursor(-sim::system::active.reactor->height); - break; - case 's': case 'S': - sys.reactor->move_cursor(sim::system::active.reactor->height); - break; - } - } - - virtual void on_keypress(int key, int sc, int action, int mods) - { - if(action != GLFW_PRESS) - { - return; - } - - switch(key) - { - case GLFW_KEY_Z: - sim::system::active.reactor->toggle_selected(); - break; - case GLFW_KEY_C: - toggle_auto(); - break; - case GLFW_KEY_X: - sim::system::active.reactor->reset_rod_speed(); - break; - case GLFW_KEY_Q: - set_all(true); - break; - case GLFW_KEY_E: - set_all(false); - break; - } + return false; } }; core::core() { +} +static void set_all(bool state) +{ + for(int i = 0; i < sim::system::active.reactor->rods.size(); i++) + { + sim::reactor::rod* r = sim::system::active.reactor->rods[i].get(); + + if(r->should_select()) + { + r->selected = state; + } + } } void core::init() @@ -110,22 +69,46 @@ void core::init() rmesh2.load_model("../assets/model/", "reactor_core_interface_cell.stl"); mesh2.bind(); mesh2.set(rmesh2, GL_STATIC_DRAW); - - mesh_click.load_model("../assets/model/", "reactor_core_input.stl"); - mesh_scram.load_model("../assets/model/", "reactor_core_scram.stl"); + + 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"); + m_buttons[3].load_model("../assets/model/", "reactor_core_button4.stl"); + m_buttons[4].load_model("../assets/model/", "reactor_core_button5.stl"); + m_buttons[5].load_model("../assets/model/", "reactor_core_button6.stl"); + m_buttons[6].load_model("../assets/model/", "reactor_core_button7.stl"); + m_buttons[7].load_model("../assets/model/", "reactor_core_button8.stl"); + m_buttons[8].load_model("../assets/model/", "reactor_core_button9.stl"); + m_joystick.load_model("../assets/model/", "reactor_core_joystick.stl"); + m_scram.load_model("../assets/model/", "reactor_core_scram.stl"); } void core::update() { - if(mesh_click.check_focus()) - { - focus::set(std::make_unique()); - } - - if(mesh_scram.check_focus()) - { + sim::system& sys = sim::system::active; + + if(m_joystick.check_focus()) + focus::set(std::make_unique()); + if(m_scram.check_focus()) sim::system::active.reactor->scram(); - } + if(m_buttons[0].check_focus()) + set_all(true); + if(m_buttons[1].check_focus()) + sys.reactor->move_cursor(-sim::system::active.reactor->height); + if(m_buttons[2].check_focus()) + set_all(false); + if(m_buttons[3].check_focus()) + sys.reactor->move_cursor(-1); + if(m_buttons[4].check_focus()) + sys.reactor->move_cursor(sim::system::active.reactor->height); + if(m_buttons[5].check_focus()) + sys.reactor->move_cursor(1); + if(m_buttons[6].check_focus()) + sim::system::active.reactor->toggle_selected(); + if(m_buttons[7].check_focus()) + sim::system::active.reactor->reset_rod_speed(); +// if(m_buttons[8].check_focus()) +// } void core::render() diff --git a/src/graphics/monitor/core.hpp b/src/graphics/monitor/core.hpp index 677bc31..71653dd 100644 --- a/src/graphics/monitor/core.hpp +++ b/src/graphics/monitor/core.hpp @@ -9,8 +9,11 @@ namespace sim::graphics::monitor class core { - sim::graphics::mesh mesh_click, mesh_scram; sim::graphics::glmesh mesh1, mesh2; + + sim::graphics::mesh m_buttons[9]; + sim::graphics::mesh m_joystick; + sim::graphics::mesh m_scram; public: diff --git a/src/graphics/window.cpp b/src/graphics/window.cpp index 8c9843d..abbfc1d 100644 --- a/src/graphics/window.cpp +++ b/src/graphics/window.cpp @@ -32,6 +32,8 @@ static glmesh MeshScene; static monitor::vessel MonitorVessel; static monitor::core MonitorCore; +glm::mat4 window::projection_matrix; + void GLAPIENTRY cb_debug_message(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam) { if(severity != GL_DEBUG_SEVERITY_NOTIFICATION) @@ -132,6 +134,7 @@ void window::render() glm::mat4 mat_projection = glm::perspective(glm::radians(90.0f), resize::get_aspect(), 0.01f, 20.f); glUniformMatrix4fv(shader::gl_projection, 1, false, &mat_projection[0][0]); glUniformMatrix4fv(shader::gl_camera, 1, false, &mat_camera[0][0]); + projection_matrix = mat_projection; glClearColor(0, 0, 0, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); diff --git a/src/graphics/window.hpp b/src/graphics/window.hpp index 8626cce..f30ae3e 100644 --- a/src/graphics/window.hpp +++ b/src/graphics/window.hpp @@ -2,12 +2,15 @@ #pragma once #include +#include #include "../system.hpp" namespace sim::graphics::window { +extern glm::mat4 projection_matrix; + bool should_close(); void create(); void update(double dt); diff --git a/src/reactor/control/boron_rod.cpp b/src/reactor/control/boron_rod.cpp index 12d893d..f7196ad 100644 --- a/src/reactor/control/boron_rod.cpp +++ b/src/reactor/control/boron_rod.cpp @@ -50,7 +50,7 @@ void boron_rod::update(double secs) void boron_rod::update_selected(double a) { - inserted += a; + inserted -= a; if(inserted > 1) inserted = 1; if(inserted < 0) inserted = 0; diff --git a/src/reactor/reactor.cpp b/src/reactor/reactor.cpp index 809dd02..ced8e84 100644 --- a/src/reactor/reactor.cpp +++ b/src/reactor/reactor.cpp @@ -58,7 +58,7 @@ void reactor::add_rod_speed(double a) void reactor::scram() { - rod_speed = 0.2; + rod_speed = -0.2; for(int i = 0; i < size; i++) {