added improvements

This commit is contained in:
Jay Robson 2024-01-31 00:23:44 +11:00
parent e51b4f5c80
commit 9c677cc32f
19 changed files with 118 additions and 120 deletions

View File

@ -2,6 +2,7 @@
# How to build # How to build
This is built using CMake. You will also need the required libraries to build. This is built using CMake. You will also need the required libraries to build.
This project currently only works on Linux.
``` ```
mkdir build mkdir build

View File

@ -47,7 +47,7 @@ glm::vec<3, double> camera::get_pos()
return pos; return pos;
} }
void camera::update(const system& sys, double dt) void camera::update(double dt)
{ {
glm::vec<2, double> off(0, 0); glm::vec<2, double> off(0, 0);
double m = 30; double m = 30;
@ -89,8 +89,8 @@ void camera::update(const system& sys, double dt)
glm::vec<3, double> normal_last(0); glm::vec<3, double> normal_last(0);
glm::vec<3, double> velocity2; glm::vec<3, double> velocity2;
velocity2 = sys.scene.calc_intersect(pos, velocity * dt, normal_last); velocity2 = system::active.scene.calc_intersect(pos, velocity * dt, normal_last);
velocity2 = sys.scene.calc_intersect(pos + glm::vec<3, double>(0, 0, -1.5), velocity2, normal_last) / dt; velocity2 = system::active.scene.calc_intersect(pos + glm::vec<3, double>(0, 0, -1.5), velocity2, normal_last) / dt;
pos += velocity2 * dt; pos += velocity2 * dt;
on_ground = ((velocity * dt / dt).z != velocity2.z); on_ground = ((velocity * dt / dt).z != velocity2.z);

View File

@ -14,7 +14,7 @@ glm::vec<3, double> get_pos();
void rotate(double pitch, double yaw); void rotate(double pitch, double yaw);
void move(double x, double y, double z); void move(double x, double y, double z);
void update(const system& sys, double dt); void update(double dt);
}; };

View File

@ -55,6 +55,16 @@ void focus::on_charcode(unsigned int c)
} }
} }
void focus::update()
{
triggered = false;
if(state)
{
state->update();
}
}
bool focus::is_focused() bool focus::is_focused()
{ {
return (state != nullptr); return (state != nullptr);
@ -75,8 +85,3 @@ bool focus::is_triggered()
return triggered; return triggered;
} }
void focus::clear_trigger()
{
triggered = false;
}

View File

@ -13,18 +13,18 @@ struct focus_t
virtual void on_mouse_button(int button, int action, int mods) { } virtual void on_mouse_button(int button, int action, int mods) { }
virtual void on_cursor_pos(double x, double y) { } virtual void on_cursor_pos(double x, double y) { }
virtual void on_charcode(unsigned int c) { } virtual void on_charcode(unsigned int c) { }
virtual void update() { }
}; };
bool is_triggered();
void clear_trigger();
void clear(); void clear();
bool is_focused(); bool is_focused();
bool is_triggered();
void set(std::unique_ptr<focus_t> f); void set(std::unique_ptr<focus_t> f);
void on_keypress(int key, int sc, int action, int mods); void on_keypress(int key, int sc, int action, int mods);
void on_mouse_button(int button, int action, int mods); void on_mouse_button(int button, int action, int mods);
void on_cursor_pos(double x, double y); void on_cursor_pos(double x, double y);
void on_charcode(unsigned int c); void on_charcode(unsigned int c);
void update();
}; };

View File

@ -13,14 +13,15 @@ static double xpos = 0, ypos = 0;
static void cb_cursor_pos(GLFWwindow* win, double x, double y) static void cb_cursor_pos(GLFWwindow* win, double x, double y)
{ {
focus::on_cursor_pos(x, y);
if(focus::is_focused()) if(focus::is_focused())
{ {
return; focus::on_cursor_pos(x - xpos, y - ypos);
} }
else
{
camera::rotate(x - xpos, y - ypos); camera::rotate(x - xpos, y - ypos);
}
xpos = x; xpos = x;
ypos = y; ypos = y;

View File

@ -17,37 +17,16 @@ using namespace sim::graphics::monitor;
struct core_focus_t : public focus::focus_t struct core_focus_t : public focus::focus_t
{ {
sim::system* sys; virtual void on_cursor_pos(double x, double y)
core_focus_t(sim::system& sys)
{ {
this->sys = &sys; sim::system::active.reactor->rod_speed -= y * 1e-6;
}
void on_keypress(int key, int sc, int action, int mods)
{
if(action != GLFW_PRESS)
{
return;
}
}
void on_mouse_button(int button, int action, int mods)
{
}
void on_cursor_pos(double x, double y)
{
} }
void set_all(bool state) void set_all(bool state)
{ {
for(int i = 0; i < sys->reactor->rods.size(); i++) for(int i = 0; i < sim::system::active.reactor->rods.size(); i++)
{ {
sim::reactor::rod* r = sys->reactor->rods[i].get(); sim::reactor::rod* r = sim::system::active.reactor->rods[i].get();
if(r->should_select() && (r->is_selected() != state)) if(r->should_select() && (r->is_selected() != state))
{ {
@ -61,28 +40,51 @@ struct core_focus_t : public focus::focus_t
//TODO //TODO
} }
void on_charcode(unsigned int c) virtual void on_charcode(unsigned int c)
{ {
sim::system& sys = sim::system::active;
switch(c) switch(c)
{ {
case 'a': case 'A': case 'a': case 'A':
sys->reactor->move_cursor(-1); sys.reactor->move_cursor(-1);
break; break;
case 'd': case 'D': case 'd': case 'D':
sys->reactor->move_cursor(1); sys.reactor->move_cursor(1);
break;
case 's': case 'S':
sys->reactor->toggle_selected();
break; break;
case 'w': case 'W': 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(); toggle_auto();
break; break;
case 'q': case 'Q': case GLFW_KEY_X:
set_all(false); sim::system::active.reactor->rod_speed = 0;
break; break;
case 'e': case 'E': case GLFW_KEY_Q:
set_all(true); set_all(true);
break; break;
case GLFW_KEY_E:
set_all(false);
break;
} }
} }
}; };
@ -112,16 +114,18 @@ void core::init()
mesh_click.load_model("../assets/model/", "reactor_core_input.stl"); mesh_click.load_model("../assets/model/", "reactor_core_input.stl");
} }
void core::update(sim::system& sys) void core::update()
{ {
if(mesh_click.check_focus(2)) if(mesh_click.check_focus(2))
{ {
focus::set(std::make_unique<core_focus_t>(sys)); focus::set(std::make_unique<core_focus_t>());
} }
} }
void core::render(sim::system& sys) void core::render()
{ {
sim::system& sys = sim::system::active;
double step = 1 / (sys.vessel->diameter / sys.reactor->cell_width * 0.8); double step = 1 / (sys.vessel->diameter / sys.reactor->cell_width * 0.8);
double sx = 0.5 - (sys.reactor->width - 1) * step / 2.0; double sx = 0.5 - (sys.reactor->width - 1) * step / 2.0;
double sy = 0.5 - (sys.reactor->height - 1) * step / 2.0; double sy = 0.5 - (sys.reactor->height - 1) * step / 2.0;
@ -157,7 +161,7 @@ void core::render(sim::system& sys)
mesh2.uniform(); mesh2.uniform();
mesh2.render(); mesh2.render();
if(sys.reactor->cursor == i && r->should_select()) if(sys.reactor->cursor == i)
{ {
mesh2.model_matrix = mat * mat_cursor; mesh2.model_matrix = mat * mat_cursor;
mesh2.colour_matrix = arrays::colour({1, 0, 0, 1}); mesh2.colour_matrix = arrays::colour({1, 0, 0, 1});

View File

@ -16,8 +16,8 @@ public:
core(); core();
void init(); void init();
void update(sim::system& sys); void update();
void render(sim::system& sys); void render();
}; };
}; };

View File

@ -41,20 +41,27 @@ void vessel::init()
ss << "Reactor Core\n\n"; ss << "Reactor Core\n\n";
ss << "Temperature\nMin\nMax\n\n"; ss << "Temperature\nMin\nMax\n\n";
ss << "Neutron Flux\nSlow\nFast\n\n"; ss << "Neutron Flux\nSlow\nFast\n\n";
ss << "Control Rods\nMin\nMax\n\n"; ss << "Control Rods\nMin\nMax\nSpeed\n";
rmesh.load_text(ss.str().c_str(), 0.04); rmesh.load_text(ss.str().c_str(), 0.04);
mesh1.bind(); mesh1.bind();
mesh1.set(rmesh, GL_STATIC_DRAW); mesh1.set(rmesh, GL_STATIC_DRAW);
} }
static double show(double v, double m)
{
return std::round(v * m) / m;
}
static double show(double v) static double show(double v)
{ {
return std::round(v * 1e3) * 1e-3; return std::round(v * 1e3) * 1e-3;
} }
void vessel::update(sim::system& sys) void vessel::update()
{ {
sim::system& sys = sim::system::active;
std::stringstream ss; std::stringstream ss;
sim::graphics::mesh rmesh; sim::graphics::mesh rmesh;
@ -98,6 +105,7 @@ void vessel::update(sim::system& sys)
ss << sys.reactor->get_total(sim::reactor::rod::val_t::N_FAST) << " mol\n\n\n"; ss << sys.reactor->get_total(sim::reactor::rod::val_t::N_FAST) << " mol\n\n\n";
ss << show( crod_min * 100 ) << " %\n"; ss << show( crod_min * 100 ) << " %\n";
ss << show( crod_max * 100 ) << " %\n"; ss << show( crod_max * 100 ) << " %\n";
ss << show( sys.reactor->rod_speed * 100, 1e-5 ) << " %/s\n";
rmesh.load_text(ss.str().c_str(), 0.04); rmesh.load_text(ss.str().c_str(), 0.04);
mesh2.bind(); mesh2.bind();

View File

@ -15,7 +15,7 @@ public:
vessel(); vessel();
void init(); void init();
void update(sim::system& sys); void update();
void render(); void render();
}; };

View File

@ -18,7 +18,6 @@
#include "shader.hpp" #include "shader.hpp"
#include "mesh/font.hpp" #include "mesh/font.hpp"
#include "locations.hpp" #include "locations.hpp"
#include "../system.hpp"
#include "monitor/vessel.hpp" #include "monitor/vessel.hpp"
#include "monitor/core.hpp" #include "monitor/core.hpp"
#include "mesh/texture.hpp" #include "mesh/texture.hpp"
@ -40,7 +39,7 @@ void GLAPIENTRY cb_debug_message(GLenum source, GLenum type, GLuint id, GLenum s
} }
} }
void window::create(system& sys) void window::create()
{ {
glfwInit(); glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
@ -97,6 +96,8 @@ void window::create(system& sys)
shader::init_program(); shader::init_program();
sim::system& sys = sim::system::active;
sys.scene.load_model("../assets", "scene-baked.glb"); sys.scene.load_model("../assets", "scene-baked.glb");
MeshScene.bind(); MeshScene.bind();
MeshScene.set(sys.scene, GL_STATIC_DRAW); MeshScene.set(sys.scene, GL_STATIC_DRAW);
@ -116,12 +117,12 @@ void window::create(system& sys)
glViewport(0, 0, 800, 600); glViewport(0, 0, 800, 600);
} }
void window::loop(sim::system& sys) void window::loop()
{ {
glfwPollEvents(); glfwPollEvents();
MonitorCore.update(sys); MonitorCore.update();
MonitorVessel.update(sys); MonitorVessel.update();
glm::mat4 mat_projection = glm::perspective(glm::radians(80.0f), resize::get_aspect(), 0.01f, 20.f); glm::mat4 mat_projection = glm::perspective(glm::radians(80.0f), resize::get_aspect(), 0.01f, 20.f);
glUniformMatrix4fv(shader::gl_projection, 1, false, &mat_projection[0][0]); glUniformMatrix4fv(shader::gl_projection, 1, false, &mat_projection[0][0]);
@ -137,7 +138,7 @@ void window::loop(sim::system& sys)
MeshScene.uniform(); MeshScene.uniform();
MeshScene.render(); MeshScene.render();
MonitorCore.render(sys); MonitorCore.render();
MonitorVessel.render(); MonitorVessel.render();
glfwSwapBuffers(win); glfwSwapBuffers(win);

View File

@ -9,8 +9,8 @@ namespace sim::graphics::window
{ {
bool should_close(); bool should_close();
void create(sim::system& sys); void create();
void loop(sim::system& sys); void loop();
void destroy(); void destroy();
void close(); void close();

View File

@ -29,8 +29,7 @@ unsigned long get_now()
int main() int main()
{ {
sim::system sys; graphics::window::create();
graphics::window::create(sys);
long clock = get_now(); long clock = get_now();
@ -41,10 +40,10 @@ int main()
double dt = (double)passed / 1e6; double dt = (double)passed / 1e6;
clock += passed; clock += passed;
sys.update(dt); sim::system::active.update(dt);
graphics::camera::update(sys, dt); graphics::camera::update(dt);
graphics::window::loop(sys); graphics::window::loop();
graphics::focus::clear_trigger(); graphics::focus::update();
} }
graphics::window::destroy(); graphics::window::destroy();

View File

@ -9,6 +9,7 @@ using namespace sim::reactor;
reactor::reactor(std::unique_ptr<rod>* rods, int w, int h, double cw, double ch) : cell_width(cw), cell_height(ch), width(w), height(h), size(w * h) reactor::reactor(std::unique_ptr<rod>* rods, int w, int h, double cw, double ch) : cell_width(cw), cell_height(ch), width(w), height(h), size(w * h)
{ {
this->rods = std::vector<std::unique_ptr<rod>>(w * h); this->rods = std::vector<std::unique_ptr<rod>>(w * h);
this->cursor = w * h;
for(int i = 0; i < size; i++) for(int i = 0; i < size; i++)
{ {
@ -55,9 +56,14 @@ void reactor::update(double secs)
} }
update_interactions(rods_lookup, secs / 2); update_interactions(rods_lookup, secs / 2);
if(rod_speed != 0)
{
update_selected(secs);
}
} }
void reactor::update_selected(int v) void reactor::update_selected(double dt)
{ {
for(int i = 0; i < size; i++) for(int i = 0; i < size; i++)
{ {
@ -65,34 +71,34 @@ void reactor::update_selected(int v)
if(r->is_selected()) if(r->is_selected())
{ {
r->update_rod_selected(v); r->update_selected(rod_speed * dt);
} }
} }
} }
int reactor::move_cursor(int d) int reactor::move_cursor(int d)
{ {
for(int i = 0; i < size; i++) goto logic;
while(cursor == size || !rods[cursor]->should_display())
{ {
cursor = (cursor + d) % size; logic: cursor = (cursor + d) % (size + 1);
if(cursor < 0) if(cursor < 0)
{ {
cursor += size; cursor += size + 1;
}
if(d > 1) d = 1;
if(d < -1) d = -1;
} }
if(rods[cursor]->should_select())
{
return cursor; return cursor;
}
}
return 0;
} }
void reactor::toggle_selected() void reactor::toggle_selected()
{ {
if(rods[cursor]->should_select()) if(cursor < size && rods[cursor]->should_select())
{ {
rods[cursor]->toggle_selected(); rods[cursor]->toggle_selected();
} }

View File

@ -19,14 +19,14 @@ struct reactor
const int size; const int size;
std::vector<std::unique_ptr<rod>> rods; std::vector<std::unique_ptr<rod>> rods;
int cursor = 0; double rod_speed = 0;
int cursor;
reactor(std::unique_ptr<rod>* rods, int width, int height, double cell_width, double cell_height); reactor(std::unique_ptr<rod>* rods, int width, int height, double cell_width, double cell_height);
reactor(const reactor& r); reactor(const reactor& r);
reactor(reactor&& r); reactor(reactor&& r);
void update(double secs); void update(double secs);
void update_selected(int v);
void get_stats(rod::val_t type, double& min, double& max); void get_stats(rod::val_t type, double& min, double& max);
void get_rod_stats(int type, double& min, double& max); void get_rod_stats(int type, double& min, double& max);
double get_total(rod::val_t type); double get_total(rod::val_t type);
@ -37,6 +37,7 @@ private:
void update_tile(double secs, int i, int x, int y); void update_tile(double secs, int i, int x, int y);
void update_interactions(int* rods_lookup, double secs); void update_interactions(int* rods_lookup, double secs);
void update_selected(double dt);
}; };
} }

View File

@ -43,12 +43,6 @@ void rod::interact(rod* o, double secs)
} }
} }
double rod::get_speed() const
{
int m = motion < 0 ? -1 : 1;
return motion == 0 ? 0 : (std::pow(10, std::abs(motion)) * 1e-6 * m);
}
double rod::get_volume() const double rod::get_volume() const
{ {
auto r = (sim::reactor::reactor*)reactor; auto r = (sim::reactor::reactor*)reactor;
@ -61,23 +55,5 @@ void rod::update_rod(double secs)
double m = std::pow(0.5, secs / 879.4); double m = std::pow(0.5, secs / 879.4);
vals[val_t::N_FAST] *= m; vals[val_t::N_FAST] *= m;
vals[val_t::N_SLOW] *= m; vals[val_t::N_SLOW] *= m;
if(motion != 0 && !is_selected())
{
motion = 0;
}
if(motion != 0)
{
update_selected(get_speed() * secs);
}
}
void rod::update_rod_selected(int m)
{
motion += m;
if(motion > 5) motion = 5;
if(motion < -5) motion = -5;
} }

View File

@ -35,7 +35,7 @@ public:
virtual bool has_sensors(val_t t) const { return false; } virtual bool has_sensors(val_t t) const { return false; }
virtual bool should_display() const { return false; } virtual bool should_display() const { return false; }
virtual bool should_select() const { return false; } virtual bool should_select() const { return false; }
void update_rod_selected(int m); virtual void update_selected(double a) { }
double get_volume() const; double get_volume() const;
constexpr void toggle_selected() { selected = !selected; } constexpr void toggle_selected() { selected = !selected; }
@ -47,11 +47,6 @@ public:
o << r.get_name() << "\n"; o << r.get_name() << "\n";
if(r.is_selected())
{
o << "Speed: " << r.get_speed() << "\n";
}
r.display(o); r.display(o);
o << "Heat: " << r.get(val_t::HEAT) << " C\n"; o << "Heat: " << r.get(val_t::HEAT) << " C\n";
@ -65,15 +60,12 @@ protected:
double vals[VAL_N] = {0}; double vals[VAL_N] = {0};
bool selected = false; bool selected = false;
int motion = 0;
virtual void display(std::ostream& o) const { }; virtual void display(std::ostream& o) const { };
virtual double get_k(val_t type) const { return 0; } virtual double get_k(val_t type) const { return 0; }
virtual const char* get_name() const { return "Empty"; } virtual const char* get_name() const { return "Empty"; }
virtual void update_selected(double a) { }
void update_rod(double secs); void update_rod(double secs);
double get_speed() const;
}; };
} }

View File

@ -9,6 +9,8 @@
using namespace sim; using namespace sim;
sim::system system::active;
system::system() system::system()
{ {
const char* layout[] = { const char* layout[] = {

View File

@ -14,6 +14,8 @@ namespace sim
struct system struct system
{ {
static system active;
std::unique_ptr<sim::reactor::reactor> reactor; std::unique_ptr<sim::reactor::reactor> reactor;
std::unique_ptr<sim::reactor::coolant::vessel> vessel; std::unique_ptr<sim::reactor::coolant::vessel> vessel;
std::unique_ptr<sim::coolant::valve<sim::reactor::coolant::vessel>> valve; std::unique_ptr<sim::coolant::valve<sim::reactor::coolant::vessel>> valve;