diff --git a/assets/model/monitor_graphics.glb b/assets/model/monitor_graphics.glb deleted file mode 100644 index b47c924..0000000 --- a/assets/model/monitor_graphics.glb +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:17ea9c5be82c4fa4149bc9dd6819a54d71b36048bf737341831f0540c6975ee0 -size 29180 diff --git a/assets/model/monitor_graphics.stl b/assets/model/monitor_graphics.stl deleted file mode 100644 index 530ef42..0000000 Binary files a/assets/model/monitor_graphics.stl and /dev/null differ diff --git a/assets/model/monitor_turbine.stl b/assets/model/monitor_turbine.stl deleted file mode 100644 index 1f22efd..0000000 Binary files a/assets/model/monitor_turbine.stl and /dev/null differ diff --git a/assets/model/pump_switch_1.glb b/assets/model/pump_switch_1.glb deleted file mode 100644 index 0427e33..0000000 --- a/assets/model/pump_switch_1.glb +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:bcdbfed14d71fffb1f6a2a45a8ffb24a9cba1153ca3bf46d643871b8c8153bb2 -size 5296 diff --git a/assets/model/pump_switch_2.glb b/assets/model/pump_switch_2.glb deleted file mode 100644 index 9ed8644..0000000 --- a/assets/model/pump_switch_2.glb +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:92c0b6c29ef14f583836f4f5c3925343f882e15722566beada78e5313abaa0eb -size 5296 diff --git a/assets/model/pump_switch_3.glb b/assets/model/pump_switch_3.glb deleted file mode 100644 index fc91a83..0000000 --- a/assets/model/pump_switch_3.glb +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:445848e220b037d4889f1bf61d80efa5621c5eb989713cd28f4a143fc66390c9 -size 5284 diff --git a/assets/model/pump_switch_click_1.stl b/assets/model/pump_switch_click_1.stl deleted file mode 100644 index 311b4a5..0000000 Binary files a/assets/model/pump_switch_click_1.stl and /dev/null differ diff --git a/assets/model/pump_switch_click_2.stl b/assets/model/pump_switch_click_2.stl deleted file mode 100644 index 873b920..0000000 Binary files a/assets/model/pump_switch_click_2.stl and /dev/null differ diff --git a/assets/model/pump_switch_click_3.stl b/assets/model/pump_switch_click_3.stl deleted file mode 100644 index b632007..0000000 Binary files a/assets/model/pump_switch_click_3.stl and /dev/null differ diff --git a/assets/model/reactor_core_button1.stl b/assets/model/reactor_core_button1.stl deleted file mode 100644 index 7e26a5c..0000000 Binary files a/assets/model/reactor_core_button1.stl and /dev/null differ diff --git a/assets/model/reactor_core_button2.stl b/assets/model/reactor_core_button2.stl deleted file mode 100644 index 7166900..0000000 Binary files a/assets/model/reactor_core_button2.stl and /dev/null differ diff --git a/assets/model/reactor_core_button3.stl b/assets/model/reactor_core_button3.stl deleted file mode 100644 index 1cb9d84..0000000 Binary files a/assets/model/reactor_core_button3.stl and /dev/null differ diff --git a/assets/model/reactor_core_button4.stl b/assets/model/reactor_core_button4.stl deleted file mode 100644 index 7b94757..0000000 Binary files a/assets/model/reactor_core_button4.stl and /dev/null differ diff --git a/assets/model/reactor_core_button5.stl b/assets/model/reactor_core_button5.stl deleted file mode 100644 index a3539e2..0000000 Binary files a/assets/model/reactor_core_button5.stl and /dev/null differ diff --git a/assets/model/reactor_core_button6.stl b/assets/model/reactor_core_button6.stl deleted file mode 100644 index 50e62fe..0000000 Binary files a/assets/model/reactor_core_button6.stl and /dev/null differ diff --git a/assets/model/reactor_core_button7.stl b/assets/model/reactor_core_button7.stl deleted file mode 100644 index d0f5dc5..0000000 Binary files a/assets/model/reactor_core_button7.stl and /dev/null differ diff --git a/assets/model/reactor_core_button8.stl b/assets/model/reactor_core_button8.stl deleted file mode 100644 index 7ce1330..0000000 Binary files a/assets/model/reactor_core_button8.stl and /dev/null differ diff --git a/assets/model/reactor_core_button9.stl b/assets/model/reactor_core_button9.stl deleted file mode 100644 index a785ff8..0000000 Binary files a/assets/model/reactor_core_button9.stl and /dev/null differ diff --git a/assets/model/reactor_core_input.stl b/assets/model/reactor_core_input.stl deleted file mode 100644 index 7d86b99..0000000 Binary files a/assets/model/reactor_core_input.stl and /dev/null differ diff --git a/assets/model/reactor_core_interface_cell.stl b/assets/model/reactor_core_interface_cell.stl deleted file mode 100644 index 3d05d78..0000000 Binary files a/assets/model/reactor_core_interface_cell.stl and /dev/null differ diff --git a/assets/model/reactor_core_interface_circle.stl b/assets/model/reactor_core_interface_circle.stl deleted file mode 100644 index 8193953..0000000 Binary files a/assets/model/reactor_core_interface_circle.stl and /dev/null differ diff --git a/assets/model/reactor_core_joystick.stl b/assets/model/reactor_core_joystick.stl deleted file mode 100644 index f8643ba..0000000 Binary files a/assets/model/reactor_core_joystick.stl and /dev/null differ diff --git a/assets/model/reactor_core_scram.stl b/assets/model/reactor_core_scram.stl deleted file mode 100644 index 35f8899..0000000 Binary files a/assets/model/reactor_core_scram.stl and /dev/null differ diff --git a/assets/model/resistor_bank_switch.glb b/assets/model/resistor_bank_switch.glb deleted file mode 100644 index 67c4a73..0000000 --- a/assets/model/resistor_bank_switch.glb +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:572b3913405b3d881744ee8cbd17f5cde14941b22f8a24e24cfd886aee510ef3 -size 5300 diff --git a/assets/model/resistor_bank_switch_click.stl b/assets/model/resistor_bank_switch_click.stl deleted file mode 100644 index 1ba5006..0000000 Binary files a/assets/model/resistor_bank_switch_click.stl and /dev/null differ diff --git a/assets/model/scene_collisions.stl b/assets/model/scene_collisions.stl deleted file mode 100644 index d91ce4f..0000000 Binary files a/assets/model/scene_collisions.stl and /dev/null differ diff --git a/assets/model/synchroscope_dial.glb b/assets/model/synchroscope_dial.glb deleted file mode 100644 index 586cb88..0000000 --- a/assets/model/synchroscope_dial.glb +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:b2e85c91f5cb6630191ff38473356b5e84704c1bd9dd3035d17c6c5e5216c3d5 -size 4760 diff --git a/assets/model/synchroscope_dial.stl b/assets/model/synchroscope_dial.stl deleted file mode 100644 index 3b8c03e..0000000 Binary files a/assets/model/synchroscope_dial.stl and /dev/null differ diff --git a/assets/model/turbine_breaker_switch.glb b/assets/model/turbine_breaker_switch.glb deleted file mode 100644 index fd69f25..0000000 --- a/assets/model/turbine_breaker_switch.glb +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:36778a2cafb1baa2ae8447f7cdf117fe03b77ceb7d5082dca6cd1e50c562bb39 -size 5304 diff --git a/assets/model/turbine_breaker_switch_click.stl b/assets/model/turbine_breaker_switch_click.stl deleted file mode 100644 index 525d75c..0000000 Binary files a/assets/model/turbine_breaker_switch_click.stl and /dev/null differ diff --git a/assets/model/turbine_valve_bypass_joystick.stl b/assets/model/turbine_valve_bypass_joystick.stl deleted file mode 100644 index b793198..0000000 Binary files a/assets/model/turbine_valve_bypass_joystick.stl and /dev/null differ diff --git a/assets/model/turbine_valve_bypass_switch.glb b/assets/model/turbine_valve_bypass_switch.glb deleted file mode 100644 index 69797f8..0000000 --- a/assets/model/turbine_valve_bypass_switch.glb +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:7a1cd1faf27dd2bb8fa5b6ad57878d6b8a8f145313d4505f1fe104c0d5e261a6 -size 5308 diff --git a/assets/model/turbine_valve_bypass_switch_click.stl b/assets/model/turbine_valve_bypass_switch_click.stl deleted file mode 100644 index 88bef8a..0000000 Binary files a/assets/model/turbine_valve_bypass_switch_click.stl and /dev/null differ diff --git a/assets/model/turbine_valve_inlet_joystick.stl b/assets/model/turbine_valve_inlet_joystick.stl deleted file mode 100644 index 7411f74..0000000 Binary files a/assets/model/turbine_valve_inlet_joystick.stl and /dev/null differ diff --git a/assets/model/turbine_valve_inlet_switch.glb b/assets/model/turbine_valve_inlet_switch.glb deleted file mode 100644 index c568d0c..0000000 --- a/assets/model/turbine_valve_inlet_switch.glb +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:47326136f8f4a1374fe39372a56cf16c7c92647c207f17f1eefd94b633142e88 -size 5308 diff --git a/assets/model/turbine_valve_inlet_switch_click.stl b/assets/model/turbine_valve_inlet_switch_click.stl deleted file mode 100644 index b3a64c1..0000000 Binary files a/assets/model/turbine_valve_inlet_switch_click.stl and /dev/null differ diff --git a/assets/scene.blend b/assets/scene.blend index b1a4ede..47acb2d 100644 --- a/assets/scene.blend +++ b/assets/scene.blend @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a87cc44b5cf761be0decb7630a2ec95117236ff5e7ad4aded6c0ef70cfc440ae -size 11918805 +oid sha256:f52d92d7183e179115b962150686f0941375e7b1c48e947ab602b8524f92da8a +size 10202465 diff --git a/assets/scene.glb b/assets/scene.glb index 3b115cd..27de43d 100644 --- a/assets/scene.glb +++ b/assets/scene.glb @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:cabb817503128f7224435427da791afdcb2b2efe33b8ac6c4bc6ea5d8a50cac6 -size 3369184 +oid sha256:2de0ac1e7b1469a499270a6b4df4487e8a6c8d0972fd3152a2bc5a515b6a4ae3 +size 2835508 diff --git a/assets/shader/light.fsh b/assets/shader/light.fsh index 90a7060..464c614 100644 --- a/assets/shader/light.fsh +++ b/assets/shader/light.fsh @@ -8,6 +8,6 @@ uniform float far_plane; void main() { float distance = length(frag_pos); - gl_FragDepth = distance / far_plane + 1e-4f; + gl_FragDepth = distance / far_plane; } diff --git a/assets/shader/light.gsh b/assets/shader/light.gsh index 28101d0..2e66648 100644 --- a/assets/shader/light.gsh +++ b/assets/shader/light.gsh @@ -7,11 +7,12 @@ layout (triangle_strip, max_vertices=18) out; uniform mat4 shadow_mats[6]; in float emissive[]; +in float base_transparency[]; out vec3 frag_pos; void main() { - if(emissive[0] > 0) return; + if(emissive[0] > 0 || base_transparency[0] > 0) return; for(int i = 0; i < 6; i++) { diff --git a/assets/shader/light.vsh b/assets/shader/light.vsh index 4e3357e..f2f3d45 100644 --- a/assets/shader/light.vsh +++ b/assets/shader/light.vsh @@ -2,16 +2,19 @@ #version 460 core layout (location = 2) in vec4 aPos; +layout (location = 4) in vec4 aColour; layout (location = 5) in vec3 aMaterial; uniform mat4 model; uniform mat4 camera; out float emissive; +out float base_transparency; void main() { gl_Position = camera * model * aPos; + base_transparency = 1.f - aColour.a; emissive = aMaterial[2]; } diff --git a/assets/shader/main.fsh b/assets/shader/main.fsh index 72939f2..f4c3056 100644 --- a/assets/shader/main.fsh +++ b/assets/shader/main.fsh @@ -2,23 +2,7 @@ #version 460 core #extension GL_ARB_bindless_texture : require -const float PI = 3.141592; -const float Epsilon = 0.00001; - -// Constant normal incidence Fresnel factor for all dielectrics. -const vec3 Fdielectric = vec3(0.04); - -const int SampleOffsetsLen = 8; -const vec3 SampleOffsets[SampleOffsetsLen] = { - vec3(-1.f,-1.f,-1.f), - vec3(-1.f,-1.f, 1.f), - vec3(-1.f, 1.f,-1.f), - vec3(-1.f, 1.f, 1.f), - vec3( 1.f,-1.f,-1.f), - vec3( 1.f,-1.f, 1.f), - vec3( 1.f, 1.f,-1.f), - vec3( 1.f, 1.f, 1.f), -}; +const float PI = 3.141592f; in VS_OUT { vec3 normal; @@ -55,18 +39,18 @@ uniform bool shadows_enabled; vec3 FresnelSchlick(float cosTheta, vec3 F0) { - return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0); + return F0 + (1.f - F0) * pow(clamp(1.f - cosTheta, 0.f, 1.f), 5.f); } float DistributionGGX(vec3 N, vec3 H, float roughness) { float a = roughness*roughness; float a2 = a*a; - float NdotH = max(dot(N, H), 0.0); + float NdotH = max(dot(N, H), 0.f); float NdotH2 = NdotH*NdotH; float num = a2; - float denom = (NdotH2 * (a2 - 1.0) + 1.0); + float denom = (NdotH2 * (a2 - 1.f) + 1.f); denom = PI * denom * denom; return num / denom; @@ -74,19 +58,19 @@ float DistributionGGX(vec3 N, vec3 H, float roughness) float GeometrySchlickGGX(float NdotV, float roughness) { - float r = (roughness + 1.0); - float k = (r*r) / 8.0; + float r = (roughness + 1.f); + float k = (r*r) / 8.f; float num = NdotV; - float denom = NdotV * (1.0 - k) + k; + float denom = NdotV * (1.f - k) + k; return num / denom; } float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) { - float NdotV = max(dot(N, V), 0.0); - float NdotL = max(dot(N, L), 0.0); + float NdotV = max(dot(N, V), 0.f); + float NdotL = max(dot(N, L), 0.f); float ggx2 = GeometrySchlickGGX(NdotV, roughness); float ggx1 = GeometrySchlickGGX(NdotL, roughness); @@ -114,6 +98,8 @@ vec3 sRGB_To_LinRGB(vec3 c) void main() { vec4 albedo = texture2D(frag_tex, vin.tex_pos) * vin.colour; + if(albedo.a == 0.f) discard; + vec3 albedo_lin = sRGB_To_LinRGB(albedo.rgb); float roughness = vin.material[0]; @@ -123,10 +109,10 @@ void main() vec3 N = normalize(vin.normal); vec3 V = normalize(camera_pos - vin.pos.xyz); - vec3 F0 = vec3(0.04); + vec3 F0 = vec3(0.04f); F0 = mix(F0, albedo_lin, metalness); - vec3 Lo = vec3(0.0); + vec3 Lo = vec3(0.0f); for(int i = 0; i < lights_count; i++) { Light l = lights[i]; @@ -141,38 +127,35 @@ void main() // cook-torrance brdf float NDF = DistributionGGX(N, H, roughness); float G = GeometrySmith(N, V, L, roughness); - vec3 F = FresnelSchlick(max(dot(H, V), 0.0), F0); + vec3 F = FresnelSchlick(max(dot(H, V), 0.f), F0); vec3 kS = F; - vec3 kD = vec3(1.0) - kS; - kD *= 1.0 - metalness; + vec3 kD = vec3(1.f) - kS; + kD *= 1.f - metalness; vec3 numerator = NDF * G * F; - float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; + float denominator = 4.f * max(dot(N, V), 0.f) * max(dot(N, L), 0.f) + 1e-4f; vec3 specular = numerator / denominator; - float amount = 0; + float light_m; + float spec_m; if(shadows_enabled) { - float texel_size = 1.f / textureSize(shadow_maps[i], 0)[0]; - - for(int j = 0; j < SampleOffsetsLen; j++) - { - amount += ((texture(shadow_maps[i], -L + SampleOffsets[j] * texel_size).r * far_plane > d) ? 1.f : 0.5f); - } - - amount /= SampleOffsetsLen; + float max_d = texture(shadow_maps[i], -L).r * far_plane + 1e-2f; + spec_m = max_d > d ? 1.f : 0.f; + light_m = spec_m * 0.25f + 0.75f; } else { - amount = 1.f; + light_m = 1.f; + spec_m = 1.f; } // add to outgoing radiance Lo - float NdotL = max(dot(N, L), 0.0); - Lo += (kD * albedo_lin / PI + specular) * radiance * NdotL * amount; + float NdotL = max(dot(N, L), 0.f); + Lo += (kD * albedo_lin / PI + specular * spec_m) * radiance * NdotL * light_m; } vec3 ambient = vec3(0.03f) * albedo_lin * brightness; @@ -181,6 +164,5 @@ void main() light = mix(light, albedo.rgb, luminance); frag_colour = vec4(light, albedo.a); - if(frag_colour.a == 0.f) discard; } diff --git a/src/graphics/camera.cpp b/src/graphics/camera.cpp index 02e500c..4595953 100644 --- a/src/graphics/camera.cpp +++ b/src/graphics/camera.cpp @@ -79,9 +79,9 @@ glm::vec<3, double> Camera::get_pos() return pos; } -void Camera::init() +void Camera::init(const Model& model) { - collision_scene.load_model("../assets/model", "scene_collisions.stl"); + collision_scene = model.load("collision"); } void Camera::update(double dt) diff --git a/src/graphics/camera.hpp b/src/graphics/camera.hpp index aab31c0..8da9d6f 100644 --- a/src/graphics/camera.hpp +++ b/src/graphics/camera.hpp @@ -5,6 +5,7 @@ #include #include "../system.hpp" +#include "mesh/model.hpp" namespace Sim::Graphics::Camera { @@ -18,7 +19,7 @@ double get_yaw(); Json::Value serialize(); void load(const Json::Value& node); -void init(); +void init(const Model& model); void rotate(double pitch, double yaw); void move(double x, double y, double z); void update(double dt); diff --git a/src/graphics/mesh/gllight.cpp b/src/graphics/mesh/gllight.cpp index 7e98d5a..38eeae3 100644 --- a/src/graphics/mesh/gllight.cpp +++ b/src/graphics/mesh/gllight.cpp @@ -14,7 +14,7 @@ using namespace Sim::Graphics; static glm::mat4 shadow_mats[6]; -GLLight::GLLight(Light light) : light(light), size(4096) +GLLight::GLLight(Light light) : light(light), size(1024) { glGenTextures(1, &id); glBindTexture(GL_TEXTURE_CUBE_MAP, id); @@ -53,8 +53,6 @@ GLLight::GLLight(GLLight&& o) : light(o.light), size(o.size) GLLight::~GLLight() { - std::cout << "Destructor called for " << fbo << ":" << id << ":" << handle << "\n"; - if(fbo) glDeleteFramebuffers(1, &fbo); if(id) diff --git a/src/graphics/mesh/mesh.hpp b/src/graphics/mesh/mesh.hpp index 0ad861d..50d603f 100644 --- a/src/graphics/mesh/mesh.hpp +++ b/src/graphics/mesh/mesh.hpp @@ -17,17 +17,13 @@ namespace Sim::Graphics struct Mesh { - std::unordered_map mat_nodes; std::vector vertices; std::vector indices; - std::vector lights; Mesh(); void set_vertices(const Arrays::Vertex* data, size_t size); void set_indices(const unsigned int* data, size_t size); - 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); diff --git a/src/graphics/mesh/model.cpp b/src/graphics/mesh/model.cpp index 66ee752..838b428 100644 --- a/src/graphics/mesh/model.cpp +++ b/src/graphics/mesh/model.cpp @@ -2,10 +2,6 @@ #include #include -#include -#include -#include -#include #include #include @@ -15,6 +11,7 @@ #include "mesh.hpp" #include "arrays.hpp" #include "texture.hpp" +#include "model.hpp" #include "../../util/streams.hpp" using namespace Sim::Graphics; @@ -24,11 +21,9 @@ struct ProcState unsigned int offset = 0; std::string base; - std::vector lights; std::vector vertices; std::vector indices; std::unordered_map handles; - std::unordered_map mat_nodes; }; static unsigned int proc_texture(const ProcState& state, aiMaterial* mat, const aiScene* scene, aiTextureType type, int index) @@ -177,7 +172,7 @@ static void proc_mesh(ProcState& state, glm::mat4 mat, aiMesh* mesh, const aiSce state.offset += mesh->mNumVertices; } -glm::mat4 get_mat(aiMatrix4x4 m) +glm::mat4 convert_mat(aiMatrix4x4 m) { return { m.a1, m.a2, m.a3, m.a4, @@ -187,21 +182,38 @@ glm::mat4 get_mat(aiMatrix4x4 m) }; } -static void proc_node(ProcState& state, glm::mat4 mat, aiNode* node, const aiScene* scene) +bool starts_with(const char* base, const char* check) { - mat = get_mat(node->mTransformation) * mat; - std::string name(node->mName.C_Str()); - state.mat_nodes[name] = mat; - - for(size_t i = 0; i < node->mNumMeshes; i++) + while(base[0] != '\0' && check[0] != '\0') { - aiMesh* mesh = scene->mMeshes[node->mMeshes[i]]; - proc_mesh(state, mat, mesh, scene); + if(base[0] != check[0]) + { + return false; + } + + base++; + check++; + } + + return (check[0] == '\0'); +} + +static void proc_node(ProcState& state, glm::mat4 mat, aiNode* node, const aiScene* scene, const char* search) +{ + mat = convert_mat(node->mTransformation) * mat; + + if(starts_with(node->mName.C_Str(), search)) + { + for(size_t i = 0; i < node->mNumMeshes; i++) + { + aiMesh* mesh = scene->mMeshes[node->mMeshes[i]]; + proc_mesh(state, mat, mesh, scene); + } } for(size_t i = 0; i < node->mNumChildren; i++) { - proc_node(state, mat, node->mChildren[i], scene); + proc_node(state, mat, node->mChildren[i], scene, search); } } @@ -214,65 +226,92 @@ static unsigned int proc_embedded_texture(aiTexture* tex) return Texture::load_mem((unsigned char*)tex->pcData, tex->mWidth); } - // swizzle each pixel to get RGBA + std::vector> pixels; + pixels.reserve(tex->mWidth * tex->mHeight); + + // convert image to get RGBA for(int i = 0; i < tex->mWidth * tex->mHeight; i++) { aiTexel t = tex->pcData[i]; - tex->pcData[i] = {t.r, t.g, t.b, t.a}; + pixels.push_back({t.r, t.g, t.b, t.a}); } - return Texture::load_mem((unsigned char*)tex->pcData, tex->mWidth, tex->mHeight, 4); + return Texture::load_mem(&pixels[0][0], tex->mWidth, tex->mHeight, 4); } -void Mesh::load_model(std::string path) +glm::mat4 get_transforms(const aiNode* node) { - load_model(".", path); -} + glm::mat4 mat(1); -void Mesh::load_model(std::string base, std::string filename) -{ - ProcState state {.base = base}; - std::string path = base + "/" + filename; - Assimp::Importer importer; - - const aiScene *scene = importer.ReadFile(path.c_str(), aiProcess_Triangulate | aiProcess_FlipUVs); - - std::cout << "Loaded model: " << path << "\n"; - - if(scene == nullptr) + while(node->mParent != nullptr) { - std::cerr << "AssImp: Error loading model\n"; - return; + mat = mat * convert_mat(node->mTransformation); + node = node->mParent; } + return mat; +} + +glm::mat4 Model::get_matrix(const char* name) const +{ + return get_transforms(scene->mRootNode->FindNode(name)); +} + +Model::Model(std::string base, std::string filename) : base(base) +{ + std::string path = base + "/" + filename; + scene = importer.ReadFile(path.c_str(), aiProcess_Triangulate | aiProcess_FlipUVs); + + textures.reserve(scene->mNumTextures); + for(int i = 0; i < scene->mNumTextures; i++) { aiTexture* tex = scene->mTextures[i]; unsigned int handle = proc_embedded_texture(tex); - state.handles[tex] = handle; + textures.push_back(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 = state.mat_nodes[light->mName.C_Str()]; + glm::mat4 mat = get_matrix(light->mName.C_Str()); auto [x, y, z] = light->mPosition; auto [r, g, b] = light->mColorDiffuse; glm::vec4 pos = glm::vec4(x, y, z, 1) * mat; - state.lights.push_back({ + lights.push_back({ glm::vec3(pos), {r, g, b}, }); } - - mat_nodes = std::move(state.mat_nodes); - vertices = std::move(state.vertices); - indices = std::move(state.indices); - lights = std::move(state.lights); +} + +Mesh Model::load(const char* name, glm::mat4 mat) const +{ + Mesh mesh; + ProcState state {.base = base}; + proc_node(state, mat, scene->mRootNode, scene, name); + + mesh.vertices = std::move(state.vertices); + mesh.indices = std::move(state.indices); + + return mesh; +} + +Mesh Model::load_root(glm::mat4 mat) const +{ + return load("", mat); +} + +Mesh Model::load(const char* name) const +{ + return load(name, glm::mat4(1)); +} + +Mesh Model::load_root() const +{ + return load("", glm::mat4(1)); } diff --git a/src/graphics/mesh/model.hpp b/src/graphics/mesh/model.hpp new file mode 100644 index 0000000..7c8323d --- /dev/null +++ b/src/graphics/mesh/model.hpp @@ -0,0 +1,42 @@ + +#pragma once + +#include "mesh.hpp" +#include "light.hpp" + +#include +#include + +#include + +#include +#include +#include +#include + +namespace Sim::Graphics +{ + +class Model +{ + std::string base; + Assimp::Importer importer; + const aiScene* scene; + +public: + + std::vector textures; + std::vector lights; + + Model(std::string base, std::string filename); + Model(const Model&) = delete; + + Mesh load_root() const; + Mesh load_root(glm::mat4 mat) const; + Mesh load(const char* name) const; + Mesh load(const char* name, glm::mat4 mat) const; + glm::mat4 get_matrix(const char* name) const; +}; + +}; + diff --git a/src/graphics/monitor/core.cpp b/src/graphics/monitor/core.cpp index 6ac1370..fbae7e2 100644 --- a/src/graphics/monitor/core.cpp +++ b/src/graphics/monitor/core.cpp @@ -120,26 +120,26 @@ Core::Core() { } -void Core::init(Mesh& rmesh) +void Core::init(const Model& model, Mesh& rmesh) { - Mesh mesh; + Mesh mesh = model.load("translation_monitor_3"); mat = Locations::monitors[2]; 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"); - 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_monitor.load_model("../assets/model/", "reactor_core_input.stl"); - m_scram.load_model("../assets/model/", "reactor_core_scram.stl"); + m_buttons[0] = model.load("click_numpad_1"); + m_buttons[1] = model.load("click_numpad_2"); + m_buttons[2] = model.load("click_numpad_3"); + m_buttons[3] = model.load("click_numpad_4"); + m_buttons[4] = model.load("click_numpad_5"); + m_buttons[5] = model.load("click_numpad_6"); + m_buttons[6] = model.load("click_numpad_7"); + m_buttons[7] = model.load("click_numpad_8"); + m_buttons[8] = model.load("click_numpad_9"); + m_joystick = model.load("click_reactor_joystick"); + m_monitor = model.load("translation_monitor_3"); + m_scram = model.load("click_scram"); } static Mesh add_dot(glm::mat4 model_mat, glm::vec4 colour) diff --git a/src/graphics/monitor/core.hpp b/src/graphics/monitor/core.hpp index bd83e52..61a7993 100644 --- a/src/graphics/monitor/core.hpp +++ b/src/graphics/monitor/core.hpp @@ -1,7 +1,7 @@ #pragma once -#include "../mesh/glmesh.hpp" +#include "../mesh/model.hpp" namespace Sim::Graphics::Monitor { @@ -17,7 +17,7 @@ class Core public: Core(); - void init(Mesh& rmesh); + void init(const Model& model, Mesh& rmesh); void update(double dt); void remesh_slow(Mesh& rmesh); void remesh_fast(Mesh& rmesh); diff --git a/src/graphics/monitor/primary_loop.cpp b/src/graphics/monitor/primary_loop.cpp index 8d9822b..8c6dd62 100644 --- a/src/graphics/monitor/primary_loop.cpp +++ b/src/graphics/monitor/primary_loop.cpp @@ -55,7 +55,7 @@ PrimaryLoop::PrimaryLoop() } -void PrimaryLoop::init(Mesh& rmesh) +void PrimaryLoop::init(const Model& model, Mesh& rmesh) { mat = Locations::monitors[3]; @@ -77,15 +77,15 @@ void PrimaryLoop::init(Mesh& rmesh) mesh.load_text(ss.str().c_str(), 0.04); rmesh.add(mesh, mat); - 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"); + g_switch_pump = model.load("visual_pump_switch_1"); + g_switch_bypass = model.load("visual_bypass_switch"); + g_switch_inlet = model.load("visual_inlet_switch"); - 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"); - m_switch_pump.load_model("../assets/model", "pump_switch_click_1.stl"); - m_switch_bypass.load_model("../assets/model", "turbine_valve_bypass_switch_click.stl"); - m_switch_inlet.load_model("../assets/model", "turbine_valve_inlet_switch_click.stl"); + m_joystick_turbine_bypass = model.load("click_bypass_joystick"); + m_joystick_turbine_inlet = model.load("click_inlet_joystick"); + m_switch_pump = model.load("click_pump_switch_1"); + m_switch_bypass = model.load("click_bypass_switch"); + m_switch_inlet = model.load("click_inlet_switch"); } void PrimaryLoop::update(double dt) diff --git a/src/graphics/monitor/primary_loop.hpp b/src/graphics/monitor/primary_loop.hpp index d9d19e1..f9464a7 100644 --- a/src/graphics/monitor/primary_loop.hpp +++ b/src/graphics/monitor/primary_loop.hpp @@ -1,7 +1,7 @@ #pragma once -#include "../mesh/glmesh.hpp" +#include "../mesh/model.hpp" namespace Sim::Graphics::Monitor { @@ -24,7 +24,7 @@ class PrimaryLoop public: PrimaryLoop(); - void init(Mesh& rmesh); + void init(const Model& model, Mesh& rmesh); void update(double dt); void remesh_slow(Mesh& rmesh); void remesh_fast(Mesh& rmesh); diff --git a/src/graphics/monitor/secondary_loop.cpp b/src/graphics/monitor/secondary_loop.cpp index c2b0d12..78082d5 100644 --- a/src/graphics/monitor/secondary_loop.cpp +++ b/src/graphics/monitor/secondary_loop.cpp @@ -21,7 +21,7 @@ SecondaryLoop::SecondaryLoop() } -void SecondaryLoop::init(Mesh& rmesh) +void SecondaryLoop::init(const Model& model, Mesh& rmesh) { mat = Locations::monitors[5]; @@ -38,13 +38,13 @@ void SecondaryLoop::init(Mesh& rmesh) mesh.load_text(ss.str().c_str(), 0.04); rmesh.add(mesh, mat); - g_switch_2.load_model("../assets/model", "pump_switch_2.glb"); - g_switch_3.load_model("../assets/model", "pump_switch_3.glb"); + g_switch_2 = model.load("visual_pump_switch_2"); + g_switch_3 = model.load("visual_pump_switch_3"); - 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"); - m_switch_2.load_model("../assets/model", "pump_switch_click_2.stl"); - m_switch_3.load_model("../assets/model", "pump_switch_click_3.stl"); + m_joystick_turbine_bypass = model.load("click_bypass_joystick"); + m_joystick_turbine_inlet = model.load("click_inlet_joystick"); + m_switch_2 = model.load("click_pump_switch_2"); + m_switch_3 = model.load("click_pump_switch_3"); } void SecondaryLoop::update(double dt) diff --git a/src/graphics/monitor/secondary_loop.hpp b/src/graphics/monitor/secondary_loop.hpp index 9f1e3df..c1c7d72 100644 --- a/src/graphics/monitor/secondary_loop.hpp +++ b/src/graphics/monitor/secondary_loop.hpp @@ -1,7 +1,7 @@ #pragma once -#include "../mesh/glmesh.hpp" +#include "../mesh/model.hpp" namespace Sim::Graphics::Monitor { @@ -21,7 +21,7 @@ class SecondaryLoop public: SecondaryLoop(); - void init(Mesh& rmesh); + void init(const Model& model, Mesh& rmesh); void update(double dt); void remesh_slow(Mesh& rmesh); void remesh_fast(Mesh& rmesh); diff --git a/src/graphics/monitor/turbine.cpp b/src/graphics/monitor/turbine.cpp index 8621949..c8e0eeb 100644 --- a/src/graphics/monitor/turbine.cpp +++ b/src/graphics/monitor/turbine.cpp @@ -21,7 +21,7 @@ Turbine::Turbine() } -void Turbine::init(Mesh& rmesh) +void Turbine::init(const Model& model, Mesh& rmesh) { mat = Locations::monitors[4]; @@ -37,12 +37,12 @@ void Turbine::init(Mesh& rmesh) mesh.load_text("Synchroscope", 0.04); rmesh.add(mesh, glm::translate(mat, glm::vec3(0, 0.6, 0))); - mesh.load_model("../assets/model", "synchroscope_dial.glb"); + mesh = model.load("visual_synchroscope_dial"); gm_synchroscope_dial.bind(); 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"); + g_switch_breaker = model.load("visual_breaker_switch"); + m_switch_breaker = model.load("click_breaker_switch"); } void Turbine::update(double dt) @@ -61,7 +61,6 @@ void Turbine::update(double dt) if(m_switch_breaker.check_focus()) sys.loop.generator.breaker_closed = !sys.loop.generator.breaker_closed; - } void Turbine::remesh_slow(Mesh& rmesh) diff --git a/src/graphics/monitor/turbine.hpp b/src/graphics/monitor/turbine.hpp index 436249a..659d1f3 100644 --- a/src/graphics/monitor/turbine.hpp +++ b/src/graphics/monitor/turbine.hpp @@ -1,6 +1,7 @@ #pragma once +#include "../mesh/model.hpp" #include "../mesh/glmesh.hpp" namespace Sim::Graphics::Monitor @@ -17,7 +18,7 @@ class Turbine public: Turbine(); - void init(Mesh& rmesh); + void init(const Model& model, Mesh& rmesh); void update(double dt); void remesh_slow(Mesh& rmesh); void remesh_fast(Mesh& rmesh); diff --git a/src/graphics/monitor/vessel.cpp b/src/graphics/monitor/vessel.cpp index 18a6208..8ffea21 100644 --- a/src/graphics/monitor/vessel.cpp +++ b/src/graphics/monitor/vessel.cpp @@ -20,7 +20,7 @@ Vessel::Vessel() } -void Vessel::init(Mesh& rmesh) +void Vessel::init(const Model& model, Mesh& rmesh) { mat = Locations::monitors[1]; diff --git a/src/graphics/monitor/vessel.hpp b/src/graphics/monitor/vessel.hpp index 6254d68..c8e078f 100644 --- a/src/graphics/monitor/vessel.hpp +++ b/src/graphics/monitor/vessel.hpp @@ -1,7 +1,7 @@ #pragma once -#include "../mesh/glmesh.hpp" +#include "../mesh/model.hpp" namespace Sim::Graphics::Monitor { @@ -13,7 +13,7 @@ class Vessel public: Vessel(); - void init(Mesh& rmesh); + void init(const Model& model, Mesh& rmesh); void update(double dt); void remesh_slow(Mesh& rmesh); void remesh_fast(Mesh& rmesh); diff --git a/src/graphics/window.cpp b/src/graphics/window.cpp index c444cab..3dce591 100644 --- a/src/graphics/window.cpp +++ b/src/graphics/window.cpp @@ -25,6 +25,7 @@ #include "monitor/secondary_loop.hpp" #include "monitor/turbine.hpp" #include "mesh/texture.hpp" +#include "mesh/model.hpp" #include "mesh/gllight.hpp" #include "../system.hpp" #include "../util/streams.hpp" @@ -124,7 +125,6 @@ void Window::create() Mouse::init(); Resize::init(); Texture::init(); - Camera::init(); Font::init(); UI::init(); @@ -134,45 +134,30 @@ void Window::create() Sim::System& sys = *System::active; Mesh m_scene; - m_scene.load_model("../assets", "scene.glb"); - - m_scene.lights[0].pos.x = 6; - m_scene.lights[1].pos.x = 0; - - // find the floor parts of the model and set them slightly transparent - for(int i = 0; i < m_scene.indices.size(); i += 3) - { - Arrays::Vertex& v1 = m_scene.vertices[m_scene.indices[i]]; - Arrays::Vertex& v2 = m_scene.vertices[m_scene.indices[i + 1]]; - Arrays::Vertex& v3 = m_scene.vertices[m_scene.indices[i + 2]]; - - if(v1.pos.z <= 0 && v2.pos.z <= 0 && v3.pos.z <= 0) - { - v1.colour.w = 0.95; - v2.colour.w = 0.95; - v3.colour.w = 0.95; - } - } + Model model("../assets", "scene.glb"); + m_scene = model.load("scene"); + + Camera::init(model); // send all the light data glGenBuffers(1, &ssbo_lights); glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_lights); - glBufferData(GL_SHADER_STORAGE_BUFFER, m_scene.lights.size() * sizeof(m_scene.lights[0]), &m_scene.lights[0], GL_STATIC_DRAW); + glBufferData(GL_SHADER_STORAGE_BUFFER, model.lights.size() * sizeof(model.lights[0]), &model.lights[0], GL_STATIC_DRAW); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, ssbo_lights); - glUniform1i(Shader::MAIN["lights_count"], m_scene.lights.size()); + glUniform1i(Shader::MAIN["lights_count"], model.lights.size()); - monitor_core.init(m_scene); - monitor_vessel.init(m_scene); - monitor_primary_loop.init(m_scene); - monitor_secondary_loop.init(m_scene); - monitor_turbine.init(m_scene); + monitor_core.init(model, m_scene); + monitor_vessel.init(model, m_scene); + monitor_primary_loop.init(model, m_scene); + monitor_secondary_loop.init(model, m_scene); + monitor_turbine.init(model, m_scene); gm_scene.bind(); gm_scene.set(m_scene, GL_STATIC_DRAW); glfwShowWindow(win); -/* + // setup lighting and prerender shadows Shader::LIGHT.load("../assets/shader", "light.vsh", "light.gsh", "light.fsh"); glUniform1f(Shader::LIGHT["far_plane"], 100.0f); @@ -180,28 +165,24 @@ void Window::create() std::vector light_handles; - for(int i = 0; i < m_scene.lights.size(); i++) + for(int i = 0; i < model.lights.size(); i++) { - GLLight light(m_scene.lights[i]); + GLLight light(model.lights[i]); light.render(); light_handles.push_back(light.handle); lights.push_back(std::move(light)); } - for(int i = 0; i < lights.size(); i++) - { - std::cout << "handle " << i << ": " << light_handles[i] << "\n"; - } - - Shader::MAIN.use();*/ + Shader::MAIN.use(); glUniform1f(Shader::MAIN["far_plane"], 100.0f); + glUniform1i(Shader::MAIN["shadows_enabled"], 1); -/* // send all the light shadow map handles + // send all the light shadow map handles glGenBuffers(1, &ssbo_shadow_maps); glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_shadow_maps); glBufferData(GL_SHADER_STORAGE_BUFFER, light_handles.size() * sizeof(light_handles[0]), &light_handles[0], GL_STATIC_DRAW); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, ssbo_shadow_maps);*/ + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, ssbo_shadow_maps); } void update_slow() @@ -287,32 +268,19 @@ void Window::render() glm::vec3 camera_pos = Camera::get_pos(); glm::mat4 mat_camera = Camera::get_matrix(); - mat_camera = glm::scale(mat_camera, {1, 1, -1}); - camera_pos.z *= -1; Shader::MAIN.use(); glm::vec3 brightness = glm::vec3(System::active->grid.get_light_intensity()); - glm::mat4 mat_projection = glm::perspective(glm::radians(90.0f), Resize::get_aspect(), 0.01f, 20.f); + glm::mat4 mat_projection = glm::perspective(glm::radians(90.0f), Resize::get_aspect(), 0.01f, 50.f); glUniformMatrix4fv(Shader::MAIN["projection"], 1, false, &mat_projection[0][0]); glUniformMatrix4fv(Shader::MAIN["camera"], 1, false, &mat_camera[0][0]); - glUniform3fv(Shader::MAIN["brightness"], 1, &brightness[0]); glUniform3fv(Shader::MAIN["camera_pos"], 1, &camera_pos[0]); - projection_matrix = mat_projection; + glUniform3fv(Shader::MAIN["brightness"], 1, &brightness[0]); + glFrontFace(GL_CCW); glClearColor(0, 0, 0, 1.0f); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); - glCullFace(GL_BACK); - glFrontFace(GL_CW); - - render_scene(); - - camera_pos.z *= -1; - mat_camera = Camera::get_matrix(); - glUniformMatrix4fv(Shader::MAIN["camera"], 1, false, &mat_camera[0][0]); - glUniform3fv(Shader::MAIN["camera_pos"], 1, &camera_pos[0]); - glClear(GL_DEPTH_BUFFER_BIT); - glFrontFace(GL_CCW); render_scene();