improved switch model and performance

This commit is contained in:
Jay Robson 2024-02-24 18:10:29 +11:00
parent 95dd2cc2f9
commit 10b2d24f8b
30 changed files with 404 additions and 452 deletions

BIN
assets/model/pump_switch_1.glb (Stored with Git LFS)

Binary file not shown.

BIN
assets/model/pump_switch_2.glb (Stored with Git LFS)

Binary file not shown.

BIN
assets/model/pump_switch_3.glb (Stored with Git LFS)

Binary file not shown.

BIN
assets/model/resistor_bank_switch.glb (Stored with Git LFS)

Binary file not shown.

BIN
assets/model/turbine_breaker_switch.glb (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
assets/scene.blend (Stored with Git LFS)

Binary file not shown.

View File

@ -30,7 +30,6 @@ layout(std140, binding = 1) buffer ssbo_lights
in flat sampler2D frag_tex;
out vec4 frag_colour;
uniform mat4 tex_mat;
uniform vec3 brightness;
uniform vec3 camera_pos;
@ -77,7 +76,7 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
void main()
{
vec4 albedo = (tex_mat * texture2D(frag_tex, vin.tex_pos)) * vin.colour;
vec4 albedo = texture2D(frag_tex, vin.tex_pos) * vin.colour;
float roughness = vin.material[0];
float metalness = vin.material[1];

View File

@ -38,13 +38,3 @@ void Arrays::vertex_attrib_pointers()
glEnableVertexAttribArray(5);
}
glm::mat4 Arrays::colour(glm::vec4 c)
{
return glm::mat4({
c.r, c.g, c.b, c.a,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
});
}

View File

@ -14,10 +14,12 @@ struct Vertex
glm::vec3 normal = {0, 0, 0};
glm::vec4 colour = {1, 1, 1, 1};
glm::vec3 material = {0, 0, 0};
constexpr bool operator==(const Vertex&) const = default;
} __attribute__((packed));
void vertex_attrib_pointers();
glm::mat4 colour(glm::vec4 code);
};

View File

@ -48,6 +48,7 @@ void Font::init()
FT_Set_Pixel_Sizes(face, 0, size);
GLuint texids[128];
std::vector<glm::vec<4, unsigned char>> pixels;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
@ -74,10 +75,17 @@ void Font::init()
continue;
}
pixels.resize(width * height);
for(int i = 0; i < width * height; i++)
{
pixels[i] = glm::vec<4, unsigned char>(face->glyph->bitmap.buffer[i]);
}
glCreateTextures(GL_TEXTURE_2D, 1, &texids[i]);
glTextureStorage2D(texids[i], 1, GL_R8, width, height);
glTextureSubImage2D(texids[i], 0, 0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, face->glyph->bitmap.buffer);
glTextureStorage2D(texids[i], 1, GL_RGBA8, width, height);
glTextureSubImage2D(texids[i], 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, &pixels[0]);
glTextureParameteri(texids[i], GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameteri(texids[i], GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
@ -150,3 +158,31 @@ void Mesh::load_text(const char* text, double size)
this->indices = std::move(indices);
}
void Mesh::load_text(const char* text, double size, glm::vec2 align)
{
glm::vec2 max;
load_text(text, size);
for(Arrays::Vertex& v : vertices)
{
if(v.pos.x > max.x)
{
max.x = v.pos.x;
}
if(v.pos.y > max.y)
{
max.y = v.pos.y;
}
}
align *= max;
for(Arrays::Vertex& v : vertices)
{
v.pos.x -= align.x;
v.pos.y -= align.y;
}
}

View File

@ -33,7 +33,6 @@ GLMesh::GLMesh(GLMesh&& o)
ebo = o.ebo;
vao = o.vao;
size = o.size;
colour_matrix = o.colour_matrix;
model_matrix = o.model_matrix;
o.vbo = 0;
@ -53,18 +52,17 @@ void GLMesh::bind()
init(this);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
}
void GLMesh::uniform()
{
glUniformMatrix4fv(Shader::MAIN["model"], 1, false, &model_matrix[0][0]);
glUniformMatrix4fv(Shader::MAIN["tex_mat"], 1, false, &colour_matrix[0][0]);
}
void GLMesh::set(const Mesh& m, int mode)
{
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ARRAY_BUFFER, m.vertices.size() * sizeof(m.vertices[0]), &m.vertices[0], mode);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m.indices.size() * sizeof(m.indices[0]), &m.indices[0], mode);
this->size = m.indices.size();

View File

@ -16,7 +16,6 @@ struct GLMesh
unsigned int vao = 0, vbo = 0, ebo = 0, size = 0;
glm::mat4 model_matrix {1.0f};
glm::mat4 colour_matrix {1.0f};
constexpr GLMesh() { }

View File

@ -19,6 +19,8 @@ struct Light
glm::vec3 colour;
float padding2;
constexpr bool operator==(const Light&) const = default;
} __attribute__((packed));
};

View File

@ -265,3 +265,4 @@ Mesh Mesh::to_lines() const
return m;
}

View File

@ -29,6 +29,7 @@ struct Mesh
void load_model(std::string base, std::string path);
void load_model(std::string path);
void load_text(const char* text, double size);
void load_text(const char* text, double size, glm::vec2 align);
void add(const Mesh& o, glm::mat4 mat);
void add(const Mesh& o);
@ -39,6 +40,8 @@ struct Mesh
glm::vec<3, double> calc_intersect(glm::vec<3, double> pos, glm::vec<3, double> path) const;
glm::vec<3, double> calc_intersect(glm::vec<3, double> pos, glm::vec<3, double> path, glm::vec<3, double>& normal_last) const;
bool operator==(const Mesh&) const = default;
template <class T>
void load_text(const char* header, T& item, double size)
{

View File

@ -226,19 +226,6 @@ static unsigned int proc_embedded_texture(aiTexture* tex)
return Texture::load_mem((unsigned char*)tex->pcData, tex->mWidth, tex->mHeight, 4);
}
glm::mat4 get_transforms(aiNode* node)
{
glm::mat4 mat(1);
while(node->mParent != nullptr)
{
mat *= get_mat(node->mTransformation);
node = node->mParent;
}
return mat;
}
void Mesh::load_model(std::string path)
{
load_model(".", path);
@ -265,10 +252,12 @@ void Mesh::load_model(std::string base, std::string filename)
state.handles[tex] = handle;
}
proc_node(state, glm::mat4(1), scene->mRootNode, scene);
for(int i = 0; i < scene->mNumLights; i++)
{
aiLight* light = scene->mLights[i];
glm::mat4 mat = get_transforms(scene->mRootNode->FindNode(light->mName));
glm::mat4 mat = state.mat_nodes[light->mName.C_Str()];
auto [x, y, z] = light->mPosition;
auto [r, g, b] = light->mColorDiffuse;
@ -281,8 +270,6 @@ void Mesh::load_model(std::string base, std::string filename)
});
}
proc_node(state, glm::mat4(1), scene->mRootNode, scene);
mat_nodes = std::move(state.mat_nodes);
vertices = std::move(state.vertices);
indices = std::move(state.indices);

View File

@ -80,8 +80,6 @@ struct CoreMonitor : public Focus::FocusType
default:
return;
}
parent->is_dirty = true;
}
};
@ -97,7 +95,6 @@ struct CoreJoystick : public Focus::FocusType
virtual void on_cursor_pos(double x, double y)
{
System::active->reactor.add_rod_speed(y * 1e-6);
parent->is_dirty = true;
}
virtual ~CoreJoystick()
@ -123,16 +120,13 @@ Core::Core()
{
}
void Core::init()
void Core::init(Mesh& rmesh)
{
mesh1.model_matrix = mesh2.model_matrix = Locations::monitors[2];
mesh1.colour_matrix = mesh2.colour_matrix = Arrays::colour({1, 1, 1, 1});
Mesh mesh;
mat = Locations::monitors[2];
Sim::Graphics::Mesh rmesh;
rmesh.load_text("Reactor Core", 0.04);
mesh1.bind();
mesh1.set(rmesh, GL_STATIC_DRAW);
mesh.load_text("Reactor Core", 0.04);
rmesh.add(mesh, mat);
m_buttons[0].load_model("../assets/model/", "reactor_core_button1.stl");
m_buttons[1].load_model("../assets/model/", "reactor_core_button2.stl");
@ -168,66 +162,44 @@ void Core::update(double dt)
{
Sim::System& sys = *System::active;
if(m_monitor.check_focus()) {
if(m_monitor.check_focus())
Focus::set(std::make_unique<CoreMonitor>(this));
}
if(m_joystick.check_focus()) {
if(m_joystick.check_focus())
Focus::set(std::make_unique<CoreJoystick>(this));
}
if(m_scram.check_focus()) {
if(m_scram.check_focus())
sys.reactor.scram();
is_dirty = true;
}
if(m_buttons[0].check_focus()) {
if(m_buttons[0].check_focus())
set_all(true);
is_dirty = true;
}
if(m_buttons[1].check_focus()) {
if(m_buttons[1].check_focus())
sys.reactor.move_cursor(-sys.reactor.height);
is_dirty = true;
}
if(m_buttons[2].check_focus()) {
if(m_buttons[2].check_focus())
set_all(false);
is_dirty = true;
}
if(m_buttons[3].check_focus()) {
if(m_buttons[3].check_focus())
sys.reactor.move_cursor(-1);
is_dirty = true;
}
if(m_buttons[4].check_focus()) {
if(m_buttons[4].check_focus())
sys.reactor.toggle_selected();
is_dirty = true;
}
if(m_buttons[5].check_focus()) {
if(m_buttons[5].check_focus())
sys.reactor.move_cursor(1);
is_dirty = true;
}
if(m_buttons[6].check_focus()) {
if(m_buttons[6].check_focus())
sys.reactor.reset_rod_speed();
is_dirty = true;
}
if(m_buttons[7].check_focus()) {
if(m_buttons[7].check_focus())
sys.reactor.move_cursor(sys.reactor.height);
is_dirty = true;
}
clock_now += dt;
if(clock_at + 1.0/30.0 > clock_now)
void Core::remesh_slow(Mesh& rmesh)
{
if(!is_dirty)
{
return;
}
remesh(rmesh, false);
}
else
void Core::remesh_fast(Mesh& rmesh)
{
clock_at += 1.0/30.0;
remesh(rmesh, true);
}
Sim::Graphics::Mesh rmesh;
is_dirty = false;
void Core::remesh(Mesh& rmesh, bool fast)
{
Sim::System& sys = *System::active;
Sim::Graphics::Mesh mesh;
double step = 1 / (sys.vessel.diameter / sys.reactor.cell_width * 0.8);
double sx = 0.5 - (sys.reactor.width - 1) * step / 2.0;
@ -252,6 +224,10 @@ void Core::update(double dt)
continue;
}
glm::mat4 mat = glm::translate(glm::mat4(1), glm::vec3(ox, oy, 0)) * mat_scale;
if(!fast)
{
glm::vec4 colour_heat = r->get_heat_colour() * glm::vec4(glm::vec3(1), 1);
glm::vec4 colour_spec = r->get_colour();
@ -260,38 +236,32 @@ void Core::update(double dt)
continue;
}
glm::mat4 mat = glm::translate(glm::mat4(1), glm::vec3(ox, oy, 0)) * mat_scale;
mesh.add(add_dot(mat, colour_heat));
rmesh.add(add_dot(mat, colour_heat));
if(colour_spec[3] != 0)
{
mesh.add(add_dot(mat * mat_spec, colour_spec));
}
}
else
{
if(sys.reactor.cursor == i)
{
rmesh.add(add_dot(mat * mat_cursor, {1, 0, 0, 1}));
mesh.add(add_dot(mat * mat_cursor, {1, 0, 0, 1}));
}
if(r->selected)
{
rmesh.add(add_dot(mat * mat_select, {1, 1, 0, 1}));
mesh.add(add_dot(mat * mat_select, {1, 1, 0, 1}));
}
if(colour_spec[3] != 0)
{
rmesh.add(add_dot(mat * mat_spec, colour_spec));
}
}
mesh2.bind();
mesh2.set(rmesh, GL_DYNAMIC_DRAW);
rmesh.add(mesh, mat);
}
void Core::render()
{
mesh1.bind();
mesh1.uniform();
mesh1.render();
mesh2.bind();
mesh2.uniform();
mesh2.render();
}

View File

@ -8,21 +8,20 @@ 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];
Sim::Graphics::Mesh m_joystick;
Sim::Graphics::Mesh m_scram;
glm::mat4 mat;
Mesh m_monitor;
Mesh m_buttons[9];
Mesh m_joystick;
Mesh m_scram;
public:
bool is_dirty = false;
Core();
void init();
void init(Mesh& rmesh);
void update(double dt);
void remesh_slow(Mesh& rmesh);
void remesh_fast(Mesh& rmesh);
void remesh(Mesh& rmesh, bool fast);
void render();
};

View File

@ -55,20 +55,12 @@ PrimaryLoop::PrimaryLoop()
}
void PrimaryLoop::init()
void PrimaryLoop::init(Mesh& rmesh)
{
mesh1.model_matrix = Locations::monitors[3];
mesh2.model_matrix = glm::translate(mesh1.model_matrix, glm::vec3(0.5, 0, 0));
mesh1.colour_matrix = mesh2.colour_matrix = {
1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
mat = Locations::monitors[3];
std::stringstream ss;
Sim::Graphics::Mesh rmesh;
Sim::Graphics::Mesh mesh;
ss << "Turbine Bypass Valve\n\n";
ss << "Opened\nFlow\nSetpoint\n\n";
@ -82,21 +74,12 @@ void PrimaryLoop::init()
ss << "Pressure\n";
ss << "Level\n";
rmesh.load_text(ss.str().c_str(), 0.04);
mesh1.bind();
mesh1.set(rmesh, GL_STATIC_DRAW);
mesh.load_text(ss.str().c_str(), 0.04);
rmesh.add(mesh, mat);
rmesh.load_model("../assets/model", "pump_switch_1.glb");
gm_switch_pump.bind();
gm_switch_pump.set(rmesh, GL_STATIC_DRAW);
rmesh.load_model("../assets/model", "turbine_valve_bypass_switch.glb");
gm_switch_bypass.bind();
gm_switch_bypass.set(rmesh, GL_STATIC_DRAW);
rmesh.load_model("../assets/model", "turbine_valve_inlet_switch.glb");
gm_switch_inlet.bind();
gm_switch_inlet.set(rmesh, GL_STATIC_DRAW);
g_switch_pump.load_model("../assets/model", "pump_switch_1.glb");
g_switch_bypass.load_model("../assets/model", "turbine_valve_bypass_switch.glb");
g_switch_inlet.load_model("../assets/model", "turbine_valve_inlet_switch.glb");
m_joystick_turbine_bypass.load_model("../assets/model", "turbine_valve_bypass_joystick.stl");
m_joystick_turbine_inlet.load_model("../assets/model", "turbine_valve_inlet_joystick.stl");
@ -108,13 +91,24 @@ void PrimaryLoop::init()
void PrimaryLoop::update(double dt)
{
System& sys = *System::active;
clock_now += dt;
if(clock_at + 1.0/30.0 < clock_now)
if(m_joystick_turbine_bypass.check_focus())
Focus::set(std::make_unique<ValveJoystick>(&sys.loop.turbine_bypass_valve));
if(m_joystick_turbine_inlet.check_focus())
Focus::set(std::make_unique<ValveJoystick>(&sys.loop.turbine_inlet_valve));
if(m_switch_pump.check_focus())
sys.loop.primary_pump.powered = !sys.loop.primary_pump.powered;
if(m_switch_inlet.check_focus())
sys.loop.turbine_inlet_valve.toggle_auto();
if(m_switch_bypass.check_focus())
sys.loop.turbine_bypass_valve.toggle_auto();
}
void PrimaryLoop::remesh_slow(Mesh& rmesh)
{
std::stringstream ss;
Sim::Graphics::Mesh rmesh;
clock_at += 1.0/30.0;
Sim::Graphics::Mesh mesh;
System& sys = *System::active;
ss << "\n\n";
ss << show( sys.loop.turbine_bypass_valve.get_state() * 100 ) << " %\n";
@ -154,47 +148,24 @@ void PrimaryLoop::update(double dt)
show_units( ss, sys.loop.condenser.get_pressure() ) << "Pa\n";
ss << show( sys.loop.condenser.get_level() / 1000 ) << " / " << show( sys.loop.condenser.get_volume() / 1000 ) << " kL\n";
rmesh.load_text(ss.str().c_str(), 0.04);
mesh2.bind();
mesh2.set(rmesh, GL_DYNAMIC_DRAW);
mesh.load_text(ss.str().c_str(), 0.04);
rmesh.add(mesh, glm::translate(mat, glm::vec3(0.5, 0, 0)));
}
if(m_joystick_turbine_bypass.check_focus())
Focus::set(std::make_unique<ValveJoystick>(&sys.loop.turbine_bypass_valve));
if(m_joystick_turbine_inlet.check_focus())
Focus::set(std::make_unique<ValveJoystick>(&sys.loop.turbine_inlet_valve));
if(m_switch_pump.check_focus())
sys.loop.primary_pump.powered = !sys.loop.primary_pump.powered;
if(m_switch_inlet.check_focus())
sys.loop.turbine_inlet_valve.toggle_auto();
if(m_switch_bypass.check_focus())
sys.loop.turbine_bypass_valve.toggle_auto();
void PrimaryLoop::remesh_fast(Mesh& rmesh)
{
System& sys = *System::active;
gm_switch_inlet.model_matrix = glm::translate(glm::mat4(1), glm::vec3(0, sys.loop.turbine_inlet_valve.get_auto() ? 0.07 : 0, 0));
gm_switch_bypass.model_matrix = glm::translate(glm::mat4(1), glm::vec3(0, sys.loop.turbine_bypass_valve.get_auto() ? 0.07 : 0, 0));
gm_switch_pump.model_matrix = glm::translate(glm::mat4(1), glm::vec3(0, sys.loop.primary_pump.powered ? 0.07 : 0, 0));
float off1 = sys.loop.primary_pump.powered ? 0.07 : 0;
float off2 = sys.loop.turbine_bypass_valve.get_auto() ? 0.07 : 0;
float off3 = sys.loop.turbine_inlet_valve.get_auto() ? 0.07 : 0;
rmesh.add(g_switch_pump, glm::translate(glm::mat4(1), glm::vec3(0, off1, 0)));
rmesh.add(g_switch_bypass, glm::translate(glm::mat4(1), glm::vec3(0, off2, 0)));
rmesh.add(g_switch_inlet, glm::translate(glm::mat4(1), glm::vec3(0, off3, 0)));
}
void PrimaryLoop::render()
{
mesh1.bind();
mesh1.uniform();
mesh1.render();
mesh2.bind();
mesh2.uniform();
mesh2.render();
gm_switch_pump.bind();
gm_switch_pump.uniform();
gm_switch_pump.render();
gm_switch_inlet.bind();
gm_switch_inlet.uniform();
gm_switch_inlet.render();
gm_switch_bypass.bind();
gm_switch_bypass.uniform();
gm_switch_bypass.render();
}

View File

@ -8,16 +8,15 @@ namespace Sim::Graphics::Monitor
class PrimaryLoop
{
GLMesh mesh1, mesh2;
double clock_at = 0, clock_now = 0;
GLMesh gm_switch_pump;
GLMesh gm_switch_bypass;
GLMesh gm_switch_inlet;
glm::mat4 mat;
Mesh m_joystick_turbine_bypass;
Mesh m_joystick_turbine_inlet;
Mesh g_switch_pump;
Mesh g_switch_bypass;
Mesh g_switch_inlet;
Mesh m_switch_pump;
Mesh m_switch_bypass;
Mesh m_switch_inlet;
@ -25,8 +24,10 @@ class PrimaryLoop
public:
PrimaryLoop();
void init();
void init(Mesh& rmesh);
void update(double dt);
void remesh_slow(Mesh& rmesh);
void remesh_fast(Mesh& rmesh);
void render();
};

View File

@ -21,20 +21,12 @@ SecondaryLoop::SecondaryLoop()
}
void SecondaryLoop::init()
void SecondaryLoop::init(Mesh& rmesh)
{
mesh1.model_matrix = Locations::monitors[5];
mesh2.model_matrix = glm::translate(mesh1.model_matrix, glm::vec3(0.5, 0, 0));
mesh1.colour_matrix = mesh2.colour_matrix = {
1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
mat = Locations::monitors[5];
std::stringstream ss;
Sim::Graphics::Mesh rmesh;
Sim::Graphics::Mesh mesh;
ss << "Cooling Tower\n\n";
ss << "Heat\nSteam\nPressure\nLevel\n\n";
@ -43,17 +35,11 @@ void SecondaryLoop::init()
ss << "Freight Pump\n\n";
ss << "Power\nSpeed\nFlow\n\n";
rmesh.load_text(ss.str().c_str(), 0.04);
mesh1.bind();
mesh1.set(rmesh, GL_STATIC_DRAW);
mesh.load_text(ss.str().c_str(), 0.04);
rmesh.add(mesh, mat);
rmesh.load_model("../assets/model", "pump_switch_2.glb");
gm_switch_2.bind();
gm_switch_2.set(rmesh, GL_STATIC_DRAW);
rmesh.load_model("../assets/model", "pump_switch_3.glb");
gm_switch_3.bind();
gm_switch_3.set(rmesh, GL_STATIC_DRAW);
g_switch_2.load_model("../assets/model", "pump_switch_2.glb");
g_switch_3.load_model("../assets/model", "pump_switch_3.glb");
m_joystick_turbine_bypass.load_model("../assets/model", "turbine_valve_bypass_joystick.stl");
m_joystick_turbine_inlet.load_model("../assets/model", "turbine_valve_inlet_joystick.stl");
@ -64,13 +50,18 @@ void SecondaryLoop::init()
void SecondaryLoop::update(double dt)
{
System& sys = *System::active;
clock_now += dt;
if(clock_at + 1.0/30.0 < clock_now)
if(m_switch_2.check_focus())
sys.loop.secondary_pump.powered = !sys.loop.secondary_pump.powered;
if(m_switch_3.check_focus())
sys.freight_pump.powered = !sys.freight_pump.powered;
}
void SecondaryLoop::remesh_slow(Mesh& rmesh)
{
std::stringstream ss;
Sim::Graphics::Mesh rmesh;
clock_at += 1.0/30.0;
Sim::Graphics::Mesh mesh;
System& sys = *System::active;
ss << "\n\n";
ss << show( sys.evaporator.get_heat() ) << " C\n";
@ -86,37 +77,23 @@ void SecondaryLoop::update(double dt)
ss << show( sys.freight_pump.get_rpm() ) << " r/min\n";
show_units( ss, sys.freight_pump.get_flow_mass() ) << "g/s\n";
rmesh.load_text(ss.str().c_str(), 0.04);
mesh2.bind();
mesh2.set(rmesh, GL_DYNAMIC_DRAW);
mesh.load_text(ss.str().c_str(), 0.04);
rmesh.add(mesh, glm::translate(mat, glm::vec3(0.5, 0, 0)));
}
if(m_switch_2.check_focus())
sys.loop.secondary_pump.powered = !sys.loop.secondary_pump.powered;
if(m_switch_3.check_focus())
sys.freight_pump.powered = !sys.freight_pump.powered;
void SecondaryLoop::remesh_fast(Mesh& rmesh)
{
System& sys = *System::active;
gm_switch_2.model_matrix = glm::translate(glm::mat4(1), glm::vec3(0, sys.loop.secondary_pump.powered ? 0.07 : 0, 0));
gm_switch_3.model_matrix = glm::translate(glm::mat4(1), glm::vec3(0, sys.freight_pump.powered ? 0.07 : 0, 0));
float off2 = sys.loop.secondary_pump.powered ? 0.07 : 0;
float off3 = sys.freight_pump.powered ? 0.07 : 0;
rmesh.add(g_switch_2, glm::translate(glm::mat4(1), glm::vec3(0, off2, 0)));
rmesh.add(g_switch_3, glm::translate(glm::mat4(1), glm::vec3(0, off3, 0)));
}
void SecondaryLoop::render()
{
mesh1.bind();
mesh1.uniform();
mesh1.render();
mesh2.bind();
mesh2.uniform();
mesh2.render();
gm_switch_2.bind();
gm_switch_2.uniform();
gm_switch_2.render();
gm_switch_3.bind();
gm_switch_3.uniform();
gm_switch_3.render();
}

View File

@ -8,22 +8,23 @@ namespace Sim::Graphics::Monitor
class SecondaryLoop
{
Sim::Graphics::GLMesh mesh1, mesh2;
double clock_at = 0, clock_now = 0;
glm::mat4 mat;
Sim::Graphics::GLMesh gm_switch_2;
Sim::Graphics::GLMesh gm_switch_3;
Mesh g_switch_2;
Mesh g_switch_3;
Sim::Graphics::Mesh m_joystick_turbine_bypass;
Sim::Graphics::Mesh m_joystick_turbine_inlet;
Sim::Graphics::Mesh m_switch_2;
Sim::Graphics::Mesh m_switch_3;
Mesh m_joystick_turbine_bypass;
Mesh m_joystick_turbine_inlet;
Mesh m_switch_2;
Mesh m_switch_3;
public:
SecondaryLoop();
void init();
void init(Mesh& rmesh);
void update(double dt);
void remesh_slow(Mesh& rmesh);
void remesh_fast(Mesh& rmesh);
void render();
};

View File

@ -21,80 +21,33 @@ Turbine::Turbine()
}
void Turbine::init()
void Turbine::init(Mesh& rmesh)
{
mesh1.model_matrix = mesh2.model_matrix = Locations::monitors[4];
mesh1.colour_matrix = mesh2.colour_matrix = {
1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
mat = Locations::monitors[4];
std::stringstream ss;
Sim::Graphics::Mesh rmesh, rmesh2;
Sim::Graphics::Mesh mesh;
ss << "Turbine\n\n";
ss << "Heat\nPressure\nSpeed\n\n";
rmesh.load_text(ss.str().c_str(), 0.04);
rmesh2.load_text("Synchroscope", 0.04);
rmesh.add(rmesh2, glm::translate(glm::mat4(1), glm::vec3(0, 0.6, 0)));
mesh.load_text(ss.str().c_str(), 0.04);
rmesh.add(mesh, mat);
mesh1.bind();
mesh1.set(rmesh, GL_STATIC_DRAW);
mesh.load_text("Synchroscope", 0.04);
rmesh.add(mesh, glm::translate(mat, glm::vec3(0, 0.6, 0)));
rmesh.load_model("../assets/model", "synchroscope_dial.stl");
mesh.load_model("../assets/model", "synchroscope_dial.glb");
gm_synchroscope_dial.bind();
gm_synchroscope_dial.set(rmesh, GL_STATIC_DRAW);
rmesh.load_model("../assets/model", "turbine_breaker_switch.glb");
gm_switch_breaker.bind();
gm_switch_breaker.set(rmesh, GL_STATIC_DRAW);
gm_synchroscope_dial.set(mesh, GL_STATIC_DRAW);
g_switch_breaker.load_model("../assets/model", "turbine_breaker_switch.glb");
m_switch_breaker.load_model("../assets/model", "turbine_breaker_switch_click.stl");
}
void Turbine::update(double dt)
{
System& sys = *System::active;
clock_now += dt;
if(clock_at + 1.0/30.0 < clock_now)
{
std::stringstream ss;
Sim::Graphics::Mesh rmesh, rmesh2;
clock_at += 1.0/30.0;
ss << "\n\n";
ss << show( sys.loop.turbine.get_heat() ) << " C\n";
ss << show( sys.loop.turbine.get_pressure() / 1000 ) << " kPa\n";
ss << show( sys.loop.generator.get_rpm() ) << " r/min\n";
rmesh2.load_text(ss.str().c_str(), 0.04);
rmesh.add(rmesh2, glm::translate(glm::mat4(1), glm::vec3(0.5, 0, 0)));
ss = std::stringstream();
ss << "Local\n\n";
ss << show( sys.loop.generator.get_rpm() / 60 ) << " Hz\n";
Util::Streams::show_units( ss, sys.loop.generator.get_energy_generated() ) << "W\n";
rmesh2.load_text(ss.str().c_str(), 0.04);
rmesh.add(rmesh2, glm::translate(glm::mat4(1), glm::vec3(0.4, 0.7, 0)));
ss = std::stringstream();
ss << "Grid\n\n";
ss << show( sys.grid.frequency ) << " Hz\n";
rmesh2.load_text(ss.str().c_str(), 0.04);
rmesh.add(rmesh2, glm::translate(glm::mat4(1), glm::vec3(0.7, 0.7, 0)));
mesh2.bind();
mesh2.set(rmesh, GL_DYNAMIC_DRAW);
}
double rpm = sys.loop.generator.get_rpm();
if(rpm > 3570 && rpm < 3630)
@ -109,19 +62,49 @@ void Turbine::update(double dt)
if(m_switch_breaker.check_focus())
sys.loop.generator.breaker_closed = !sys.loop.generator.breaker_closed;
gm_switch_breaker.model_matrix = glm::translate(glm::mat4(1), glm::vec3(0, sys.loop.generator.breaker_closed ? 0.07 : 0, 0));
}
void Turbine::remesh_slow(Mesh& rmesh)
{
std::stringstream ss;
Sim::Graphics::Mesh mesh;
System& sys = *System::active;
ss << "\n\n";
ss << show( sys.loop.turbine.get_heat() ) << " C\n";
ss << show( sys.loop.turbine.get_pressure() / 1000 ) << " kPa\n";
ss << show( sys.loop.generator.get_rpm() ) << " r/min\n";
mesh.load_text(ss.str().c_str(), 0.04);
rmesh.add(mesh, glm::translate(mat, glm::vec3(0.5, 0, 0)));
ss = std::stringstream();
ss << "Local\n\n";
ss << show( sys.loop.generator.get_rpm() / 60 ) << " Hz\n";
Util::Streams::show_units( ss, sys.loop.generator.get_energy_generated() ) << "W\n";
mesh.load_text(ss.str().c_str(), 0.04);
rmesh.add(mesh, glm::translate(mat, glm::vec3(0.4, 0.7, 0)));
ss = std::stringstream();
ss << "Grid\n\n";
ss << show( sys.grid.frequency ) << " Hz\n";
mesh.load_text(ss.str().c_str(), 0.04);
rmesh.add(mesh, glm::translate(mat, glm::vec3(0.7, 0.7, 0)));
}
void Turbine::remesh_fast(Mesh& rmesh)
{
System& sys = *System::active;
float off1 = sys.loop.generator.breaker_closed ? 0.07 : 0;
rmesh.add(g_switch_breaker, glm::translate(glm::mat4(1), glm::vec3(0, off1, 0)));
}
void Turbine::render()
{
mesh1.bind();
mesh1.uniform();
mesh1.render();
mesh2.bind();
mesh2.uniform();
mesh2.render();
double rpm = System::active->loop.generator.get_rpm();
if(rpm > 3570 && rpm < 3630)
@ -130,9 +113,5 @@ void Turbine::render()
gm_synchroscope_dial.uniform();
gm_synchroscope_dial.render();
}
gm_switch_breaker.bind();
gm_switch_breaker.uniform();
gm_switch_breaker.render();
}

View File

@ -8,18 +8,19 @@ namespace Sim::Graphics::Monitor
class Turbine
{
Sim::Graphics::GLMesh mesh1, mesh2;
double clock_at = 0, clock_now = 0;
glm::mat4 mat;
Sim::Graphics::GLMesh gm_synchroscope_dial;
Sim::Graphics::GLMesh gm_switch_breaker;
Sim::Graphics::Mesh m_switch_breaker;
GLMesh gm_synchroscope_dial;
Mesh g_switch_breaker;
Mesh m_switch_breaker;
public:
Turbine();
void init();
void init(Mesh& rmesh);
void update(double dt);
void remesh_slow(Mesh& rmesh);
void remesh_fast(Mesh& rmesh);
void render();
};

View File

@ -20,20 +20,12 @@ Vessel::Vessel()
}
void Vessel::init()
void Vessel::init(Mesh& rmesh)
{
mesh1.model_matrix = Locations::monitors[1];
mesh2.model_matrix = glm::translate(mesh1.model_matrix, glm::vec3(0.5, 0, 0));
mesh1.colour_matrix = mesh2.colour_matrix = {
1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
mat = Locations::monitors[1];
std::stringstream ss;
Sim::Graphics::Mesh rmesh;
Sim::Graphics::Mesh mesh;
ss << "Reactor Vessel\n\n";
ss << "Heat\n";
@ -47,27 +39,23 @@ void Vessel::init()
ss << "Temperature\nMin\nMax\n\n";
ss << "Control Rods\nMin\nMax\nSpeed\n";
rmesh.load_text(ss.str().c_str(), 0.04);
mesh1.bind();
mesh1.set(rmesh, GL_STATIC_DRAW);
mesh.load_text(ss.str().c_str(), 0.04);
rmesh.add(mesh, mat);
}
void Vessel::update(double dt)
{
std::stringstream ss;
Sim::Graphics::Mesh rmesh;
Sim::System& sys = *System::active;
clock_now += dt;
if(clock_at + 1.0/30.0 > clock_now)
{
return;
}
void Vessel::remesh_slow(Mesh& rmesh)
{
std::stringstream ss;
Sim::Graphics::Mesh mesh;
Sim::System& sys = *System::active;
double temp_min, temp_max;
double crod_min = INFINITY, crod_max = -INFINITY;
clock_at += 1.0/30.0;
sys.reactor.get_stats(Sim::Reactor::Rod::val_t::HEAT, temp_min, temp_max);
for(int i = 0; i < sys.reactor.size; i++)
@ -110,19 +98,15 @@ void Vessel::update(double dt)
if(sys.reactor.rod_speed == 0) ss << " (Stopped)";
ss << "\n";
rmesh.load_text(ss.str().c_str(), 0.04);
mesh2.bind();
mesh2.set(rmesh, GL_DYNAMIC_DRAW);
mesh.load_text(ss.str().c_str(), 0.04);
rmesh.add(mesh, glm::translate(mat, glm::vec3(0.5, 0, 0)));
}
void Vessel::remesh_fast(Mesh& rmesh)
{
}
void Vessel::render()
{
mesh1.bind();
mesh1.uniform();
mesh1.render();
mesh2.bind();
mesh2.uniform();
mesh2.render();
}

View File

@ -8,14 +8,15 @@ namespace Sim::Graphics::Monitor
class Vessel
{
Sim::Graphics::GLMesh mesh1, mesh2;
double clock_at = 0, clock_now = 0;
glm::mat4 mat;
public:
Vessel();
void init();
void init(Mesh& rmesh);
void update(double dt);
void remesh_slow(Mesh& rmesh);
void remesh_fast(Mesh& rmesh);
void render();
};

View File

@ -38,7 +38,6 @@ void Clock::update(double dt)
data.bind();
data.model_matrix = glm::translate(glm::mat4(1), glm::vec3(-wsize + glm::vec2(2, 2), 0));
data.colour_matrix = Arrays::colour({1, 1, 1, 1});
data.set(m, GL_DYNAMIC_DRAW);
}

View File

@ -34,8 +34,15 @@ using namespace Sim::Graphics;
static GLFWwindow* win;
static bool win_should_close = false;
static unsigned int ssbo_lights;
static double secs_wait_at = 0;
static double secs_wait_now = 0;
static GLMesh mesh_scene;
static int gm_dynamic_slow_at = 0;
static GLMesh gm_scene;
static GLMesh gm_dynamic_slow[2];
static GLMesh gm_dynamic_fast;
static Mesh m_dynamic_fast;
static Monitor::Vessel monitor_vessel;
static Monitor::Core monitor_core;
@ -151,21 +158,38 @@ void Window::create()
glUniform1i(Shader::MAIN["lights_count"], m.lights.size());
mesh_scene.bind();
mesh_scene.set(m, GL_STATIC_DRAW);
monitor_core.init(m);
monitor_vessel.init(m);
monitor_primary_loop.init(m);
monitor_secondary_loop.init(m);
monitor_turbine.init(m);
monitor_core.init();
monitor_vessel.init();
monitor_primary_loop.init();
monitor_secondary_loop.init();
monitor_turbine.init();
gm_scene.bind();
gm_scene.set(m, GL_STATIC_DRAW);
glfwShowWindow(win);
glViewport(0, 0, 800, 600);
}
void update_slow()
{
Mesh mesh;
monitor_core.remesh_slow(mesh);
monitor_vessel.remesh_slow(mesh);
monitor_primary_loop.remesh_slow(mesh);
monitor_secondary_loop.remesh_slow(mesh);
monitor_turbine.remesh_slow(mesh);
gm_dynamic_slow[gm_dynamic_slow_at].bind();
gm_dynamic_slow[gm_dynamic_slow_at].set(mesh, GL_DYNAMIC_DRAW);
gm_dynamic_slow_at = (gm_dynamic_slow_at + 1) % 2;
}
void Window::update(double dt)
{
Mesh mesh;
glfwPollEvents();
monitor_core.update(dt);
@ -175,13 +199,41 @@ void Window::update(double dt)
monitor_turbine.update(dt);
UI::update(dt);
monitor_core.remesh_fast(mesh);
monitor_vessel.remesh_fast(mesh);
monitor_primary_loop.remesh_fast(mesh);
monitor_secondary_loop.remesh_fast(mesh);
monitor_turbine.remesh_fast(mesh);
if(mesh != m_dynamic_fast)
{
gm_dynamic_fast.bind();
gm_dynamic_fast.set(mesh, GL_DYNAMIC_DRAW);
m_dynamic_fast = mesh;
}
secs_wait_now += dt;
if(secs_wait_now > secs_wait_at + 1.0/30.0)
{
secs_wait_at += 1.0/30.0;
update_slow();
}
}
void render_scene()
{
mesh_scene.bind();
mesh_scene.uniform();
mesh_scene.render();
gm_scene.bind();
gm_scene.uniform();
gm_scene.render();
gm_dynamic_slow[gm_dynamic_slow_at].bind();
gm_dynamic_slow[gm_dynamic_slow_at].uniform();
gm_dynamic_slow[gm_dynamic_slow_at].render();
gm_dynamic_fast.bind();
gm_dynamic_fast.uniform();
gm_dynamic_fast.render();
monitor_core.render();
monitor_vessel.render();