did things

This commit is contained in:
Jay Robson 2024-01-28 00:02:54 +11:00
parent 18e541ba1c
commit 3c0dbabf16
25 changed files with 374 additions and 201 deletions

View File

@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.25)
project(FastNuclearSim VERSION 1.0)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "-g -lncurses -I/usr/include/freetype2")
set(CMAKE_CXX_FLAGS "-O3 -lncurses -I/usr/include/freetype2")
file(GLOB_RECURSE SOURCES src/*.cpp)

View File

@ -0,0 +1,30 @@
#include "locations.hpp"
#include <glm/ext/matrix_transform.hpp>
using namespace sim::graphics;
const glm::mat4 locations::monitors[4] = {
(
glm::translate(glm::mat4(1), glm::vec3(-2.949, -1.7778 + 0.05, 3 - 0.05)) *
glm::rotate(glm::mat4(1), glm::radians<float>(-90), glm::vec3(1, 0, 0)) *
glm::rotate(glm::mat4(1), glm::radians<float>(-90), glm::vec3(0, 1, 0)) *
glm::scale(glm::mat4(1), glm::vec3(1.9, 1.9, 1.9))
),
(
glm::translate(glm::mat4(1), glm::vec3(-1.5 + 0.05, 3.949, 3 - 0.05)) *
glm::rotate(glm::mat4(1), glm::radians<float>(-90), glm::vec3(1, 0, 0)) *
glm::scale(glm::mat4(1), glm::vec3(1.9, 1.9, 1.9))
),
(
glm::translate(glm::mat4(1), glm::vec3(1 + 0.05, 3.949, 3 - 0.05)) *
glm::rotate(glm::mat4(1), glm::radians<float>(-90), glm::vec3(1, 0, 0)) *
glm::scale(glm::mat4(1), glm::vec3(1.9, 1.9, 1.9))
),
(
glm::translate(glm::mat4(1), glm::vec3(3.5 + 0.05, 3.949, 3 - 0.05)) *
glm::rotate(glm::mat4(1), glm::radians<float>(-90), glm::vec3(1, 0, 0)) *
glm::scale(glm::mat4(1), glm::vec3(1.9, 1.9, 1.9))
)
};

View File

@ -0,0 +1,12 @@
#pragma once
#include <glm/matrix.hpp>
namespace sim::graphics::locations
{
extern const glm::mat4 monitors[4];
};

View File

@ -1,6 +1,7 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <ft2build.h>
#include FT_FREETYPE_H

View File

@ -28,14 +28,15 @@ struct mesh
void set_vertices(const arrays::vertex* data, size_t size, int mode);
void set_indices(const unsigned int* data, size_t size, int mode);
void load_model(std::string base, std::string path);
void load_model(std::string path) { load_model(".", path); }
void load_text(const char* text, double size);
void render();
template <class T>
void load_text(const char* header, T* item, double size)
void load_text(const char* header, T& item, double size)
{
std::stringstream ss;
ss << header << *item;
ss << header << item;
load_text(ss.str().c_str(), size);
}
};

View File

@ -56,7 +56,7 @@ static unsigned int proc_texture(const proc_state& state, aiMaterial* mat, const
return texture::load(state.base + "/" + filename);
}
return 0;
return texture::handle_white;
}
static void proc_mesh(proc_state& state, glm::mat4 mat, aiMesh* mesh, const aiScene* scene)

View File

@ -11,6 +11,13 @@
using namespace sim::graphics;
static std::unordered_map<std::string, unsigned int> loaded;
unsigned int texture::handle_white;
void texture::init()
{
unsigned char pixels[] = {255, 255, 255, 255};
handle_white = load_mem(pixels, 1, 1, 4);
}
unsigned int texture::load_mem(const unsigned char* data, int width, int height, int channels)
{
@ -43,7 +50,7 @@ unsigned int texture::load_mem(const unsigned char* data, int width, int height,
unsigned int texid;
glCreateTextures(GL_TEXTURE_2D, 1, &texid);
glTextureStorage2D(texid, 8, format_in, width, height);
glTextureStorage2D(texid, 1, format_in, width, height);
glTextureSubImage2D(texid, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, data);
glTextureParameteri(texid, GL_TEXTURE_WRAP_S, GL_REPEAT);

View File

@ -6,6 +6,9 @@
namespace sim::graphics::texture
{
extern unsigned int handle_white;
void init();
unsigned int load(std::string path);
unsigned int load_mem(const unsigned char* data, int width, int height, int channels);
unsigned int load_mem(const unsigned char* data, size_t len);

View File

@ -0,0 +1,44 @@
#include "core.hpp"
#include "../locations.hpp"
#include <glm/ext/matrix_transform.hpp>
#include <sstream>
using namespace sim::graphics::monitor;
core::core()
{
}
void core::init()
{
mesh1.model_matrix = mesh2.model_matrix = locations::monitors[2];
mesh1.colour_matrix = {
1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
std::stringstream ss;
ss << "Reactor Core";
mesh1.bind();
mesh1.load_text(ss.str().c_str(), 0.05);
mesh2.bind();
mesh2.load_model("../assets/model/", "reactor_core_interface.stl");
}
void core::update(sim::system& sys)
{
}
void core::render()
{
mesh1.bind(); mesh1.render();
mesh2.bind(); mesh2.render();
}

View File

@ -0,0 +1,23 @@
#pragma once
#include "../mesh/mesh.hpp"
#include "../../system.hpp"
namespace sim::graphics::monitor
{
class core
{
sim::graphics::mesh mesh1, mesh2;
public:
core();
void init();
void update(sim::system& sys);
void render();
};
};

View File

@ -0,0 +1,60 @@
#include "vessel.hpp"
#include "../locations.hpp"
#include <glm/ext/matrix_transform.hpp>
#include <sstream>
using namespace sim::graphics::monitor;
vessel::vessel()
{
}
void vessel::init()
{
mesh1.model_matrix = locations::monitors[1];
mesh2.model_matrix = glm::translate(mesh1.model_matrix, glm::vec3(3.0 / 8.0, 0, 0));
mesh1.colour_matrix = mesh2.colour_matrix = {
1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
std::stringstream ss;
ss << "Reactor Vessel\n\n";
ss << "Heat\n";
ss << "Steam\n";
ss << "Pressure\n";
ss << "Level\n";
ss << "Void Ratio\n";
mesh1.bind();
mesh1.load_text(ss.str().c_str(), 0.05);
}
void vessel::update(sim::system& sys)
{
std::stringstream ss;
ss << "\n\n";
ss << sys.vessel.get_heat() << " C\n";
ss << sys.vessel.get_steam() << " g\n";
ss << (sys.vessel.get_pressure() * 0.001) << " kPa\n";
ss << sys.vessel.get_level() << " / " << sys.vessel.get_volume() << " L\n";
ss << (sys.vessel.get_void_ratio() * 100) << " %\n";
mesh2.bind();
mesh2.load_text(ss.str().c_str(), 0.05);
}
void vessel::render()
{
mesh1.bind(); mesh1.render();
mesh2.bind(); mesh2.render();
}

View File

@ -0,0 +1,23 @@
#pragma once
#include "../mesh/mesh.hpp"
#include "../../system.hpp"
namespace sim::graphics::monitor
{
class vessel
{
sim::graphics::mesh mesh1, mesh2;
public:
vessel();
void init();
void update(sim::system& sys);
void render();
};
};

View File

@ -53,6 +53,7 @@ static void cb_framebuffer_size(GLFWwindow* win, int w, int h)
{
win_w = w;
win_h = h;
glViewport(0, 0, w, h);
}

View File

@ -3,85 +3,53 @@
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include "shader.hpp"
#include "window.hpp"
using namespace sim::graphics;
static const char* VERTEX_SHADER = R"(
#version 460 core
#extension GL_ARB_bindless_texture : require
layout (location = 0) in sampler2D aTex;
layout (location = 1) in vec2 aTexPos;
layout (location = 2) in vec4 aPos;
layout (location = 3) in vec3 aNormal;
uniform mat4 model;
uniform mat4 projection;
out float brightness;
out flat sampler2D tex;
out vec2 texPos;
void main()
{
vec4 pos = model * aPos;
vec3 cNormal = vec3(0.f, 0.f, 1.f) * mat3(model);
brightness = dot(normalize(aNormal), normalize(cNormal)) * 0.25f + 0.75f;
gl_Position = projection * pos;
texPos = aTexPos;
tex = aTex;
}
)";
static const char* FRAGMENT_SHADER = R"(
#version 460 core
#extension GL_ARB_bindless_texture : require
in float brightness;
in flat sampler2D tex;
in vec2 texPos;
out vec4 FragColour;
uniform mat4 tex_mat;
void main()
{
vec4 texdata = texture2D(tex, texPos);
FragColour = tex_mat * texdata * vec4(vec3(brightness), 1);
if(FragColour.a == 0) discard;
}
)";
static unsigned int prog_id;
int shader::gl_tex_mat;
int shader::gl_model;
int shader::gl_projection;
static int load_shader(const char** src, int type)
static int load_shader(const char* src, int type)
{
int id = glCreateShader(type);
glShaderSource(id, 1, src, nullptr);
glShaderSource(id, 1, &src, nullptr);
glCompileShader(id);
return id;
}
static std::string read_shader(const char* path)
{
std::stringstream ss;
std::ifstream file(path, std::ios::binary);
char buff[1024];
while(!file.eof())
{
file.read(buff, 1024);
ss.write(buff, file.gcount());
}
return ss.str();
}
unsigned int shader::init_program()
{
std::string shader_vsh = read_shader("../assets/shader/main.vsh");
std::string shader_fsh = read_shader("../assets/shader/main.fsh");
int success;
int vsh_id = load_shader(&VERTEX_SHADER, GL_VERTEX_SHADER);
int fsh_id = load_shader(&FRAGMENT_SHADER, GL_FRAGMENT_SHADER);
int vsh_id = load_shader(shader_vsh.c_str(), GL_VERTEX_SHADER);
int fsh_id = load_shader(shader_fsh.c_str(), GL_FRAGMENT_SHADER);
prog_id = glCreateProgram();
glAttachShader(prog_id, vsh_id);

View File

@ -17,15 +17,20 @@
#include "window.hpp"
#include "shader.hpp"
#include "mesh/font.hpp"
#include "../parts.hpp"
#include "locations.hpp"
#include "../system.hpp"
#include "monitor/vessel.hpp"
#include "monitor/core.hpp"
#include "mesh/texture.hpp"
using namespace sim::graphics;
static GLFWwindow* win;
static bool win_should_close = false;
static mesh MeshScene, MeshText;
static mesh MeshMon1, MeshMon2, MeshMon3;
static mesh MeshScene;
static monitor::vessel MonitorVessel;
static monitor::core MonitorCore;
void GLAPIENTRY cb_debug_message(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam)
{
@ -42,18 +47,19 @@ void window::create()
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
glfwWindowHint(GLFW_VISIBLE, false);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
#endif
win = glfwCreateWindow(800, 600, "FastNuclearSim", nullptr, nullptr);
glfwMakeContextCurrent(win);
glfwSwapInterval(1);
GLenum err = glewInit();
if(err != GLEW_OK)
{
std::cerr << "GLEW Init Failed: " << glewGetErrorString(err) << "\n";
@ -86,6 +92,7 @@ void window::create()
keyboard::init();
mouse::init();
resize::init();
texture::init();
font::init();
shader::init_program();
@ -93,60 +100,17 @@ void window::create()
MeshScene.bind();
MeshScene.load_model("../assets", "scene-baked.glb");
glm::mat4 mat = glm::mat4(1);
mat = glm::translate(mat, glm::vec3(-2.949, -1.7778 + 0.05, 3 - 0.05));
mat = glm::rotate(mat, glm::radians<float>(-90), glm::vec3(1, 0, 0));
mat = glm::rotate(mat, glm::radians<float>(-90), glm::vec3(0, 1, 0));
MeshText.model_matrix = mat;
MeshText.colour_matrix = {
1, 1, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
mat = glm::mat4(1);
mat = glm::translate(mat, glm::vec3(-1.5 + 0.05, 3.949, 3 - 0.05));
mat = glm::rotate(mat, glm::radians<float>(-90), glm::vec3(1, 0, 0));
MeshMon1.model_matrix = mat;
MeshMon1.colour_matrix = {
1, 0.5, 0.5, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
mat = glm::translate(glm::mat4(1), glm::vec3(2.5, 0, 0)) * mat;
MeshMon2.model_matrix = mat;
MeshMon2.colour_matrix = {
0.5, 1, 0.5, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
mat = glm::translate(glm::mat4(1), glm::vec3(2.5, 0, 0)) * mat;
MeshMon3.model_matrix = mat;
MeshMon3.colour_matrix = {
0.5, 0.5, 1, 1,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
MonitorCore.init();
MonitorVessel.init();
glfwShowWindow(win);
glViewport(0, 0, 800, 600);
}
void window::loop()
void window::loop(sim::system& sys)
{
MeshText.bind(); MeshText.load_text("Reactor Core\n\nTODO", 0.1);
MeshMon1.bind(); MeshMon1.load_text("Reactor Vessel\n\n", parts::vessel, 0.1);
MeshMon2.bind(); MeshMon2.load_text("Steam Valve\n\n", parts::valve, 0.1);
MeshMon3.bind(); MeshMon3.load_text("Coolant Pump\n\n", parts::pump, 0.1);
MonitorCore.update(sys);
MonitorVessel.update(sys);
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]);
@ -155,10 +119,9 @@ void window::loop()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
MeshScene.bind(); MeshScene.render();
MeshText.bind(); MeshText.render();
MeshMon1.bind(); MeshMon1.render();
MeshMon2.bind(); MeshMon2.render();
MeshMon3.bind(); MeshMon3.render();
MonitorCore.render();
MonitorVessel.render();
glfwSwapBuffers(win);
glfwPollEvents();
@ -176,6 +139,7 @@ void window::close()
void window::destroy()
{
glfwDestroyWindow(win);
glfwTerminate();
}

View File

@ -3,12 +3,14 @@
#include <GLFW/glfw3.h>
#include "../system.hpp"
namespace sim::graphics::window
{
void create();
bool should_close();
void loop();
void loop(sim::system& sys);
void destroy();
void close();

View File

@ -13,7 +13,7 @@
#include "graphics/window.hpp"
#include "graphics/camera.hpp"
#include "parts.hpp"
#include "system.hpp"
using namespace sim;
@ -26,11 +26,8 @@ unsigned long get_now()
int main()
{
std::random_device rd;
std::mt19937 rand(rd());
parts::init();
graphics::window::create();
sim::system sys(system::generate());
long clock = get_now();
@ -41,13 +38,9 @@ int main()
double dt = (double)passed / 1e6;
clock += passed;
parts::reactor->update(rand, dt);
parts::pump->update(dt);
parts::valve->update(dt);
parts::vessel->update(dt);
sys.update(dt);
graphics::camera::update(dt);
graphics::window::loop();
graphics::window::loop(sys);
}
graphics::window::destroy();

View File

@ -1,37 +0,0 @@
#include "parts.hpp"
#include "reactor/builder.hpp"
#include "reactor/control/control_rod.hpp"
#include "reactor/fuel/fuel_rod.hpp"
#include "reactor/coolant/pipe.hpp"
#include "reactor/coolant/heater.hpp"
using namespace sim;
reactor::coolant::vessel* parts::vessel;
reactor::reactor* parts::reactor;
coolant::valve<sim::reactor::coolant::vessel>* parts::valve;
coolant::pump<sim::reactor::coolant::vessel>* parts::pump;
void parts::init()
{
const char* layout[] = {
"#C#C#",
"CFCFC",
"#C#C#",
"CFCFC",
"#C#C#"
};
vessel = new reactor::coolant::vessel(8, 10, 300, sim::coolant::WATER);
reactor = new sim::reactor::reactor(sim::reactor::builder(5, 5,
reactor::fuel::fuel_rod(2000, 4000),
reactor::control::control_rod(*vessel, 10000, 1),
reactor::coolant::pipe(*vessel),
layout));
valve = new coolant::valve<reactor::coolant::vessel>(*vessel, 1, 500);
pump = new coolant::pump<reactor::coolant::vessel>(*vessel, 1e4, 15);
}

View File

@ -1,20 +0,0 @@
#pragma once
#include "reactor/coolant/vessel.hpp"
#include "reactor/reactor.hpp"
#include "coolant/pump.hpp"
#include "coolant/valve.hpp"
namespace sim::parts
{
extern sim::reactor::coolant::vessel* vessel;
extern sim::reactor::reactor* reactor;
extern sim::coolant::valve<sim::reactor::coolant::vessel>* valve;
extern sim::coolant::pump<sim::reactor::coolant::vessel>* pump;
void init();
};

13
src/random.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "random.hpp"
using namespace sim;
std::mt19937 random::gen;
void random::init()
{
std::random_device rd;
gen = std::mt19937(rd());
}

14
src/random.hpp Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <random>
namespace sim::random
{
extern std::mt19937 gen;
void init();
};

View File

@ -1,5 +1,6 @@
#include "reactor.hpp"
#include "../random.hpp"
#include <algorithm>
@ -40,7 +41,7 @@ reactor::~reactor()
}
}
void reactor::update(std::mt19937& rand, double secs)
void reactor::update(double secs)
{
int rods_lookup[size];
@ -54,7 +55,7 @@ void reactor::update(std::mt19937& rand, double secs)
rods[i]->update(secs);
}
update_interactions(rand, rods_lookup, secs / 2);
update_interactions(rods_lookup, secs / 2);
}
void reactor::update_selected(int v)
@ -98,10 +99,10 @@ void reactor::toggle_selected()
}
}
void reactor::update_tile(std::mt19937& rand, double secs, int i, int x, int y)
void reactor::update_tile(double secs, int i, int x, int y)
{
int nb_lookup[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
std::shuffle(nb_lookup, &nb_lookup[3], rand);
std::shuffle(nb_lookup, &nb_lookup[3], sim::random::gen);
for(int j = 0; j < 4; j++)
{
@ -115,9 +116,9 @@ void reactor::update_tile(std::mt19937& rand, double secs, int i, int x, int y)
}
}
void reactor::update_interactions(std::mt19937& rand, int* rods_lookup, double secs)
void reactor::update_interactions(int* rods_lookup, double secs)
{
std::shuffle(rods_lookup, &rods_lookup[size - 1], rand);
std::shuffle(rods_lookup, &rods_lookup[size - 1], sim::random::gen);
for(int id = 0; id < size; id++)
{
@ -127,7 +128,7 @@ void reactor::update_interactions(std::mt19937& rand, int* rods_lookup, double s
for(int j = 0; j < 4; j++)
{
update_tile(rand, secs, i, x, y);
update_tile(secs, i, x, y);
}
}
}

View File

@ -3,7 +3,6 @@
#include "rod.hpp"
#include <random>
#include <iostream>
#include <memory>
#include <vector>
@ -25,15 +24,15 @@ struct reactor
reactor(reactor&& r);
~reactor();
void update(std::mt19937& rand, double secs);
void update(double secs);
void update_selected(int v);
int move_cursor(int d);
void toggle_selected();
private:
void update_tile(std::mt19937& rand, double secs, int i, int x, int y);
void update_interactions(std::mt19937& rand, int* rods_lookup, double secs);
void update_tile(double secs, int i, int x, int y);
void update_interactions(int* rods_lookup, double secs);
};
}

47
src/system.cpp Normal file
View File

@ -0,0 +1,47 @@
#include "system.hpp"
#include "reactor/builder.hpp"
#include "reactor/control/control_rod.hpp"
#include "reactor/fuel/fuel_rod.hpp"
#include "reactor/coolant/pipe.hpp"
#include "reactor/coolant/heater.hpp"
using namespace sim;
sim::system sim::system::generate()
{
const char* layout[] = {
"#C#C#",
"CFCFC",
"#C#C#",
"CFCFC",
"#C#C#"
};
reactor::coolant::vessel vessel(8, 10, 300, sim::coolant::WATER);
sim::reactor::reactor reactor(sim::reactor::builder(5, 5,
reactor::fuel::fuel_rod(2000, 4000),
reactor::control::control_rod(vessel, 10000, 1),
reactor::coolant::pipe(vessel),
layout));
coolant::valve<reactor::coolant::vessel> valve(vessel, 1, 500);
coolant::pump<reactor::coolant::vessel> pump(vessel, 1e4, 15);
return {
.reactor = reactor,
.vessel = vessel,
.valve = valve,
.pump = pump
};
}
void system::update(double dt)
{
vessel.update(dt);
reactor.update(dt);
valve.update(dt);
pump.update(dt);
}

24
src/system.hpp Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#include "reactor/coolant/vessel.hpp"
#include "reactor/reactor.hpp"
#include "coolant/pump.hpp"
#include "coolant/valve.hpp"
namespace sim
{
struct system
{
sim::reactor::reactor reactor;
sim::reactor::coolant::vessel vessel;
sim::coolant::valve<sim::reactor::coolant::vessel> valve;
sim::coolant::pump<sim::reactor::coolant::vessel> pump;
void update(double dt);
static system generate();
};
};