diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index 3c12aa1d..fc61f140 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -18,32 +18,28 @@ int main() { - std::string TrenchbroomPath = "F:/TrenchBroom/"; - - FGDFile file(TrenchbroomPath + "Games/Nuake/Nuake.fgd"); - - FGDClass newClass(FGDClassType::Point, "light", "a nuake light"); - - ClassProperty prop{ - "Intensity", - ClassPropertyType::Integer, - "Changes the light intensity" - }; - - newClass.AddProperty(prop); - file.AddClass(newClass); - - file.Save(); + //std::string TrenchbroomPath = "F:/TrenchBroom/"; + // + //FGDFile file(TrenchbroomPath + "Games/Nuake/Nuake.fgd"); + // + //FGDClass newClass(FGDClassType::Point, "light", "a nuake light"); + // + //ClassProperty prop{ + // "Intensity", + // ClassPropertyType::Integer, + // "Changes the light intensity" + //}; + // + //newClass.AddProperty(prop); + //file.AddClass(newClass); + // + //file.Save(); Engine::Init(); - // ScriptingEngine::UpdateScript("test.lua"); - EditorInterface editor; editor.BuildFonts(); - //CreateScene(); - while (!Engine::GetCurrentWindow()->ShouldClose()) { Engine::Tick(); @@ -56,8 +52,6 @@ int main() editor.Draw(); Engine::EndDraw(); - - } Engine::Close(); diff --git a/Editor/resources/Scenes/test.scene b/Editor/resources/Scenes/test.scene index d5ac24ae..da8e1e5c 100644 --- a/Editor/resources/Scenes/test.scene +++ b/Editor/resources/Scenes/test.scene @@ -75,9 +75,9 @@ "z": 1.0 }, "Translation": { - "x": -359.2229919433594, - "y": -1.979316234588623, - "z": -322.51702880859375 + "x": 0.0, + "y": 17.93589973449707, + "z": 0.0 }, "Type": "TransformComponent" }, @@ -100,7 +100,7 @@ "TransformComponent": { "Rotation": { "x": 0.0, - "y": 0.0, + "y": -0.0, "z": 0.0 }, "Scale": { @@ -110,7 +110,7 @@ }, "Translation": { "x": 9.199999809265137, - "y": 5.599999904632568, + "y": -11.569494247436523, "z": 0.0 }, "Type": "TransformComponent" diff --git a/Editor/resources/Shaders/basic.shader b/Editor/resources/Shaders/basic.shader index 6f556afa..9a42c83b 100644 --- a/Editor/resources/Shaders/basic.shader +++ b/Editor/resources/Shaders/basic.shader @@ -1,10 +1,5 @@ #shader vertex #version 460 core -// Have you ever seen Godot shader. The whole engine has ONE monolithic shader. -// Also, how do you want me to split this in multiple shaders lmao. -// Click upper right Round thing -// DO CODE REVIEW yep -// im following u daddy layout(location = 0) in vec3 VertexPosition; layout(location = 1) in vec2 UVPosition; layout(location = 2) in vec3 Normal; @@ -12,14 +7,13 @@ layout(location = 3) in vec3 Tangent; layout(location = 4) in vec3 Bitangent; out flat vec2 v_UVPosition; -out flat float v_TextureId; out vec3 v_Normal; out vec3 v_FragPos; out vec3 v_ViewPos; out mat3 v_TBN; -out mat3 v_WTBN; out vec3 v_Tangent; out vec3 v_Bitangent; + uniform mat4 u_Projection; uniform mat4 u_View; uniform mat4 u_Model; @@ -43,12 +37,23 @@ void main() gl_Position = u_Projection * u_View * u_Model * vec4(VertexPosition, 1.0f); v_FragPos = vec3(u_Model * vec4(VertexPosition, 1.0f)); v_ViewPos = VertexPosition; - } #shader fragment #version 460 core +out vec4 FragColor; + +in vec3 v_FragPos; +in vec3 v_ViewPos; +in vec2 v_UVPosition; +in flat vec3 v_Normal; +in mat3 v_TBN; +in vec3 v_Tangent; +in vec3 v_Bitangent; + +const float PI = 3.141592653589793f; + struct Light { int Type; // 0 = directional, 1 = point vec3 Direction; @@ -69,34 +74,23 @@ struct Light { int Volumetric; }; -out vec4 FragColor; - -// Textures -uniform sampler2D u_Textures[2]; +// Debug +uniform int u_ShowNormal; const int MaxLight = 20; uniform int LightCount = 0; uniform Light Lights[MaxLight]; -// Debug -uniform int u_ShowNormal; - // Lighting uniform vec3 u_AmbientColor; uniform vec4 u_LightColor; uniform vec3 u_LightDirection; uniform float u_Exposure; - -// Material -uniform vec3 albedo; -uniform float metallic; -uniform float roughness; -uniform float ao; +uniform float u_FogAmount; // Specular uniform samplerCube u_Skybox; uniform samplerCube u_IrradianceMap; -uniform float u_Shininess; uniform float u_Strength; uniform vec3 u_EyePosition; @@ -106,37 +100,29 @@ uniform samplerCube prefilterMap; uniform sampler2D brdfLUT; // Material -uniform int u_HasAlbedo; // I would advise against doing this stuff. just need default normal which is 0.5f 0.5f 1.0f. -uniform sampler2D m_Albedo; // yeah just think about it // normal maps are in tangent space which means the default should be a vector pointing straight towards the camera right? ie 0.0, 0.0, 1.0 -uniform vec3 m_AlbedoColor; -uniform int u_HasMetalness; // But normal maps can also contain colors where the vectors face away such as vec3(0.2, 0.4, -1.0f) right? yeah -uniform sampler2D m_Metalness; // Well normal maps are stored as colors so you can't have negative values. So they are mapped from the range of -1 to 1, to 0 to 1 -uniform float u_MetalnessValue; -uniform int u_HasRoughness; // That is why you do the [normal * 2.0f - 1.0f]; to put it into to range of -1 to 1. yeah 0 - 1 -> -1 - 1 -uniform sampler2D m_Roughness; // So vec3(0.0, 0.0, 1.0) put into the range of 0 to 1 is (0.5, 0.5, 1.0). easy. -uniform float u_RoughnessValue; -uniform int u_HasAO; +uniform sampler2D m_Albedo; +uniform sampler2D m_Metalness; +uniform sampler2D m_Roughness; uniform sampler2D m_AO; -uniform float u_AOValue; -uniform int u_HasNormal; uniform sampler2D m_Normal; -uniform int u_HasDisplacement; uniform sampler2D m_Displacement; -in vec3 v_FragPos; -in vec3 v_ViewPos; -in vec2 v_UVPosition; -in flat vec3 v_Normal; -in mat3 v_TBN; -in flat float v_TextureId; +layout(std140, binding = 32) uniform u_MaterialUniform +{ + bool u_HasAlbedo; + vec3 m_AlbedoColor; + bool u_HasMetalness; + float u_MetalnessValue; + bool u_HasRoughness; + float u_RoughnessValue; + bool u_HasAO; + float u_AOValue; + bool u_HasNormal; + bool u_HasDisplacement; +}; -in vec3 v_Tangent; -in vec3 v_Bitangent; - -const float PI = 3.141592653589793f; // mark this as static const wait idk if you can do that in glsl float height_scale = 0.00f; - vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir) // nice never done this // its easy Af its basicalyy returns a uv coords . that u use everywhere { // number of depth layers @@ -276,35 +262,6 @@ float ShadowCalculation(Light light, vec3 FragPos, vec3 normal) return shadow /= 9; } -/* -float ShadowCalculation(vec4 fragPosLightSpace, sampler2D shadowMap, vec3 normal, vec3 lightDir) -{ - // perform perspective divide - vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w; - // transform to [0,1] range - projCoords = projCoords * 0.5 + 0.5; - // get closest depth value from light's perspective (using [0,1] range fragPosLight as coords) - float closestDepth = texture(shadowMap, projCoords.xy).r; - // get depth of current fragment from light's perspective - float currentDepth = projCoords.z; - // check whether current frag pos is in shadow - float bias = max(0.005 * (1.0 - dot(normal, lightDir)), 0.0005); - - float shadow = 0.0; - vec2 texelSize = 1.0 / textureSize(shadowMap, 0); - for (int x = -1; x <= 1; ++x) - { - for (int y = -1; y <= 1; ++y) - { - float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r; - shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0; - } - } - return shadow /= 9; -} -*/ - -uniform float u_FogAmount; // Mie scaterring approximated with Henyey-Greenstein phase function. float ComputeScattering(float lightDotView) { @@ -364,24 +321,22 @@ void main() vec2 texCoords = v_UVPosition;//ParallaxMapping(v_UVPosition, viewDir); vec2 finalTexCoords = texCoords; - vec3 finalAlbedo = m_AlbedoColor; - if(u_HasAlbedo == 1) + if(u_HasAlbedo) finalAlbedo = texture(m_Albedo, finalTexCoords).rgb; float finalRoughness = u_RoughnessValue; - if (u_HasRoughness == 1) + if (u_HasRoughness ) finalRoughness = texture(m_Roughness, finalTexCoords).r; float finalMetalness = u_MetalnessValue; - if (u_HasMetalness == 1) + if (u_HasMetalness) finalMetalness = texture(m_Metalness, finalTexCoords).r; float finalAO = u_AOValue; - if (u_HasAO == 1) + if (u_HasAO) finalAO = texture(m_AO, finalTexCoords).r; - vec3 finalNormal = texture(m_Normal, finalTexCoords).rgb; finalNormal = finalNormal * 2.0 - 1.0; finalNormal = v_TBN * normalize(finalNormal); @@ -393,8 +348,6 @@ void main() vec3 F0 = vec3(0.04); F0 = mix(F0, finalAlbedo, finalMetalness); - // reflectance equation - vec3 eyeDirection = normalize(u_EyePosition - v_FragPos); vec3 Fog = vec3(0.0); diff --git a/Editor/src/EditorInterface.cpp b/Editor/src/EditorInterface.cpp index a001da46..eae772eb 100644 --- a/Editor/src/EditorInterface.cpp +++ b/Editor/src/EditorInterface.cpp @@ -22,6 +22,7 @@ #include #include #include "ProjectInterface.h" +#include Ref userInterface; ImFont* normalFont; ImFont* EditorInterface::bigIconFont; @@ -161,6 +162,7 @@ void EditorInterface::DrawViewport() if (m_IsEntitySelected && !Engine::IsPlayMode) { TransformComponent& tc = m_SelectedEntity.GetComponent(); + ParentComponent& parent = m_SelectedEntity.GetComponent(); glm::mat4 oldTransform = tc.GetTransform(); ImGuizmo::Manipulate( glm::value_ptr(Engine::GetCurrentScene()->GetCurrentCamera()->GetTransform()), @@ -180,6 +182,20 @@ void EditorInterface::DrawViewport() scale = glm::vec3(0, 0, 0); rotation = glm::conjugate(rotation); glm::vec3 euler = glm::eulerAngles(rotation); + + Vector3 globalPos = Vector3(); + Entity currentParent = m_SelectedEntity; + if (parent.HasParent) + { + while (currentParent.GetComponent().HasParent) { + currentParent = currentParent.GetComponent().Parent; + globalPos -= currentParent.GetComponent().Translation; + } + + translation = globalPos - translation; + } + + tc.Translation = translation; tc.Rotation = glm::vec3(glm::degrees(euler.x), glm::degrees(euler.y), glm::degrees(euler.z)); tc.Scale = scale; @@ -188,6 +204,10 @@ void EditorInterface::DrawViewport() + } + else + { + ImGui::PopStyleVar(); } ImGui::End(); @@ -590,7 +610,11 @@ void EditorInterface::DrawEntityPropreties() ImGui::Checkbox("Build collisions?", &component.HasCollisions); if (ImGui::Button("Build")) - component.Build(); + { + QuakeMapBuilder mapBuilder; + + mapBuilder.BuildQuakeMap(m_SelectedEntity); + } ImGui::Separator(); } } @@ -888,9 +912,9 @@ void EditorInterface::DrawRessourceWindow() std::string texture = FileDialog::OpenFile("*.png | *.jpg"); } ImGui::SameLine(); - ImGui::Checkbox("Use##1", &m_SelectedMaterial->UseAlbedo); + //ImGui::Checkbox("Use##1", &(bool)(m_SelectedMaterial->data.u_HasAlbedo)); ImGui::SameLine(); - ImGui::ColorPicker4("Color", &m_SelectedMaterial->m_AlbedoColor.r); + ImGui::ColorPicker3("Color", &m_SelectedMaterial->data.m_AlbedoColor.r); } if (ImGui::CollapsingHeader("AO", ImGuiTreeNodeFlags_DefaultOpen)) { @@ -906,9 +930,9 @@ void EditorInterface::DrawRessourceWindow() } } ImGui::SameLine(); - ImGui::Checkbox("Use##2", &m_SelectedMaterial->UseAO); + //ImGui::Checkbox("Use##2", &m_SelectedMaterial->data.u_HasAO); ImGui::SameLine(); - ImGui::DragFloat("Value##2", &m_SelectedMaterial->m_AOValue, 0.01f, 0.0f, 1.0f); + ImGui::DragFloat("Value##2", &m_SelectedMaterial->data.u_AOValue, 0.01f, 0.0f, 1.0f); } if (ImGui::CollapsingHeader("Normal", ImGuiTreeNodeFlags_DefaultOpen)) { @@ -923,8 +947,8 @@ void EditorInterface::DrawRessourceWindow() m_SelectedMaterial->SetNormal(TextureManager::Get()->GetTexture(texture)); } } - ImGui::SameLine(); - ImGui::Checkbox("Use##3", &m_SelectedMaterial->UseNormal); + //ImGui::SameLine(); + //ImGui::Checkbox("Use##3", &m_SelectedMaterial->data.u_HasNormal); } if (ImGui::CollapsingHeader("Metalness", ImGuiTreeNodeFlags_DefaultOpen)) { @@ -936,9 +960,9 @@ void EditorInterface::DrawRessourceWindow() std::string texture = FileDialog::OpenFile("*.png | *.jpg"); } ImGui::SameLine(); - ImGui::Checkbox("Use##4", &m_SelectedMaterial->UseMetalness); + //ImGui::Checkbox("Use##4", &m_SelectedMaterial->data.u_HasMetalness); ImGui::SameLine(); - ImGui::DragFloat("Value##4", &m_SelectedMaterial->m_MetalnessValue, 0.01f, 0.0f, 1.0f); + ImGui::DragFloat("Value##4", &m_SelectedMaterial->data.u_MetalnessValue, 0.01f, 0.0f, 1.0f); } if (ImGui::CollapsingHeader("Roughness", ImGuiTreeNodeFlags_DefaultOpen)) { @@ -950,9 +974,9 @@ void EditorInterface::DrawRessourceWindow() std::string texture = FileDialog::OpenFile("*.png | *.jpg"); } ImGui::SameLine(); - ImGui::Checkbox("Use##5", &m_SelectedMaterial->UseRoughness); + //ImGui::Checkbox("Use##5", &m_SelectedMaterial->data.u_HasRoughness); ImGui::SameLine(); - ImGui::DragFloat("Value##5", &m_SelectedMaterial->m_RoughnessValue, 0.01f, 0.0f, 1.0f); + ImGui::DragFloat("Value##5", &m_SelectedMaterial->data.u_RoughnessValue, 0.01f, 0.0f, 1.0f); } } else diff --git a/Editor/src/ImGuiTextHelper.h b/Editor/src/ImGuiTextHelper.h new file mode 100644 index 00000000..4525cf79 --- /dev/null +++ b/Editor/src/ImGuiTextHelper.h @@ -0,0 +1,28 @@ +#pragma once +#include +#include +#include + + +void ImGuiTextSTD(const std::string& label, std::string& value) +{ + char buffer[256]; + memset(buffer, 0, sizeof(buffer)); + std::strncpy(buffer, value.c_str(), sizeof(buffer)); + if (ImGui::InputText(label.c_str(), buffer, sizeof(buffer))) + { + + value = std::string(buffer); + } +} + +void ImGuiTextMultiline(const std::string& label, std::string& value) +{ + char buffer[256]; + memset(buffer, 0, sizeof(buffer)); + std::strncpy(buffer, value.c_str(), sizeof(buffer)); + if (ImGui::InputTextMultiline(label.c_str(), buffer, sizeof(buffer))) + { + value = std::string(buffer); + } +} diff --git a/Editor/src/ProjectInterface.cpp b/Editor/src/ProjectInterface.cpp index 4faa6e34..c874e4cb 100644 --- a/Editor/src/ProjectInterface.cpp +++ b/Editor/src/ProjectInterface.cpp @@ -1,6 +1,7 @@ #include "ProjectInterface.h" #include #include "Engine.h" +#include "ImGuiTextHelper.h" void ProjectInterface::DrawProjectSettings() { @@ -20,6 +21,7 @@ void ProjectInterface::DrawProjectSettings() void ProjectInterface::DrawCreatePointEntity() { + char buffer[256]; memset(buffer, 0, sizeof(buffer)); std::strncpy(buffer, Engine::GetProject()->Name.c_str(), sizeof(buffer)); @@ -30,32 +32,81 @@ void ProjectInterface::DrawCreatePointEntity() } +FGDPointEntity newEntity; + +const char* items[] = { "String", "Integer", "Float", "Boolean"}; void ProjectInterface::DrawEntitySettings() { if (ImGui::Begin("Entity definitions")) { ImGui::Text("This is the entity definition used by trenchbroom. This files allows you to see your entities inside Trenchbroom"); ImGui::Text("Trenchbroom path:"); - // path here... + Ref file = Engine::GetProject()->EntityDefinitionsFile; - - auto flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize; + auto flags = ImGuiWindowFlags_NoTitleBar; if (ImGui::BeginPopupModal("Create new point entity", NULL, flags)) { - + ImGuiTextSTD("Name", newEntity.Name); + ImGuiTextMultiline("Description", newEntity.Description); + + if (ImGui::BeginTable("DictCreate", 2, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable)) + { + ImGui::TableSetupColumn("Name"); + ImGui::TableSetupColumn("Type"); + ImGui::TableHeadersRow(); + + ImGui::TableNextColumn(); + int idx = 0; + for (auto& p : newEntity.Properties) + { + ImGuiTextSTD("Name", p.name); + ImGui::TableNextColumn(); + std::string current_item = NULL; + if(ImGui::BeginCombo(("TypeSelection" + std::to_string(idx)).c_str(), current_item.c_str())) + { + for (int n = 0; n < IM_ARRAYSIZE(items); n++) + { + bool is_selected = (p.type == (ClassPropertyType)n); // You can store your selection however you want, outside or inside your objects + if (ImGui::Selectable(items[n], is_selected)) + if (is_selected) + { + p.type = (ClassPropertyType)n; + ImGui::SetItemDefaultFocus(); + } + + } + ImGui::EndCombo(); + } + idx++; + ImGui::TableNextColumn(); + } + if (ImGui::Button("Add new property")) { + newEntity.Properties.push_back(ClassProperty()); + } + + ImGui::EndTable(); + + } + ImGui::Button("Create"); ImGui::SameLine(); - ImGui::Button("Cancel"); + if (ImGui::Button("Cancel")) + ImGui::CloseCurrentPopup(); ImGui::EndPopup(); } + + ImGui::PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(0, 100)); if (ImGui::BeginTabBar("##Tabs", ImGuiTabBarFlags_None)) { if (ImGui::BeginTabItem("Point entities")) { + ImVec2 avail = ImGui::GetContentRegionAvail(); + avail.y *= .8; + ImGui::BeginChild("table_child", avail, false); if (ImGui::BeginTable("nested1", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable)) { ImGui::TableSetupColumn("Name"); @@ -78,10 +129,10 @@ void ProjectInterface::DrawEntitySettings() ImGui::Button("Browse"); } - - ImGui::EndTable(); + } + ImGui::EndChild(); ImGui::EndTabItem(); } if (ImGui::BeginTabItem("Brush entities")) @@ -91,9 +142,11 @@ void ProjectInterface::DrawEntitySettings() } ImGui::EndTabBar(); } - - + + ImGui::PopStyleVar(); + if (ImGui::Button("Add new")) + ImGui::OpenPopup("Create new point entity"); } ImGui::End(); } \ No newline at end of file diff --git a/Nuake/Engine.cpp b/Nuake/Engine.cpp index 0169b6d8..8da4a3b3 100644 --- a/Nuake/Engine.cpp +++ b/Nuake/Engine.cpp @@ -26,7 +26,6 @@ void Engine::Init() PhysicsManager::Get()->Init(); Logger::Log("Physics initialized"); - CurrentWindow = Window::Get(); Logger::Log("Window initialized"); @@ -48,22 +47,22 @@ void Engine::Tick() m_LastFrameTime = time; // Play mode update vs editor update. - if (Engine::IsPlayMode) { + if (Engine::IsPlayMode) + { CurrentWindow->Update(timestep); m_FixedUpdateDifference += timestep; + // Fixed update if (m_FixedUpdateDifference >= m_FixedUpdateRate) { - // call update here. CurrentWindow->FixedUpdate(m_FixedUpdateRate); m_FixedUpdateDifference = 0.f; } } else + { GetCurrentScene()->EditorUpdate(timestep); - - - + } Input::Update(); } @@ -105,7 +104,6 @@ void Engine::EndDraw() void Engine::Close() { - ScriptingEngine::Close(); glfwTerminate(); } diff --git a/Nuake/src/Rendering/Mesh/Mesh.h b/Nuake/src/Rendering/Mesh/Mesh.h index 909ebf9f..2f2fbd6f 100644 --- a/Nuake/src/Rendering/Mesh/Mesh.h +++ b/Nuake/src/Rendering/Mesh/Mesh.h @@ -14,17 +14,15 @@ struct MeshVertex class Mesh { public: - std::vector m_Textures; std::vector m_Indices; std::vector m_Vertices; Ref m_Material; - Mesh(std::vector vertices, std::vector indices, Ref material); + void Draw(); void DebugDraw(); private: - // render data unsigned int VAO, VBO, EBO; void setupMesh(); }; \ No newline at end of file diff --git a/Nuake/src/Rendering/RenderList.h b/Nuake/src/Rendering/RenderList.h new file mode 100644 index 00000000..5b0f4793 --- /dev/null +++ b/Nuake/src/Rendering/RenderList.h @@ -0,0 +1,24 @@ +#pragma once +#include +#include + +#include "src/Core/Core.h" +#include "src/Rendering/Textures/Material.h" +#include "src/Rendering/Mesh/Mesh.h" + +class RenderList +{ +private: + std::map, std::vector>> m_RenderList; + +public: + + RenderList() { + this->m_RenderList = std::map, std::vector>>(); + } + + void AddToRenderList(Ref mesh) + { + + } +}; \ No newline at end of file diff --git a/Nuake/src/Rendering/Renderer.cpp b/Nuake/src/Rendering/Renderer.cpp index ea9ba9ce..2f11853c 100644 --- a/Nuake/src/Rendering/Renderer.cpp +++ b/Nuake/src/Rendering/Renderer.cpp @@ -127,7 +127,7 @@ void Renderer::RegisterLight(TransformComponent transform, LightComponent light, int idx = m_Lights.size(); glm::vec3 direction = light.GetDirection(); - glm::vec3 pos = transform.Translation; + glm::vec3 pos = transform.GlobalTranslation; glm::mat4 lightView = glm::lookAt(pos, pos - direction, glm::vec3(0.0f, 1.0f, 0.0f)); //light.m_Framebuffer->GetTexture(GL_DEPTH_ATTACHMENT)->Bind(17); @@ -152,7 +152,7 @@ void Renderer::RegisterLight(TransformComponent transform, LightComponent light, m_Shader->SetUniform1f("Lights[" + std::to_string(idx - 1) + "].CascadeDepth[2]", light.mCascadeSplitDepth[2]); m_Shader->SetUniform1f("Lights[" + std::to_string(idx - 1) + "].CascadeDepth[3]", light.mCascadeSplitDepth[3]); m_Shader->SetUniformMat4f("Lights[" + std::to_string(idx - 1) + "].LightTransform", light.GetProjection() * lightView); - m_Shader->SetUniform3f ("Lights[" + std::to_string(idx - 1) + "].Position" , transform.Translation.x, transform.Translation.y, transform.Translation.z); + m_Shader->SetUniform3f ("Lights[" + std::to_string(idx - 1) + "].Position" , transform.GlobalTranslation.x, transform.GlobalTranslation.y, transform.GlobalTranslation.z); m_Shader->SetUniform3f ("Lights[" + std::to_string(idx - 1) + "].Direction" , direction.x, direction.y, direction.z); m_Shader->SetUniform3f ("Lights[" + std::to_string(idx - 1) + "].Color" , light.Color.r * light.Strength, light.Color.g * light.Strength, light.Color.b * light.Strength); m_Shader->SetUniform1i ("Lights[" + std::to_string(idx - 1) + "].Volumetric", light.IsVolumetric); diff --git a/Nuake/src/Rendering/Renderer2D.cpp b/Nuake/src/Rendering/Renderer2D.cpp index 61eaae8a..877db9d8 100644 --- a/Nuake/src/Rendering/Renderer2D.cpp +++ b/Nuake/src/Rendering/Renderer2D.cpp @@ -22,8 +22,6 @@ void Renderer2D::Init() 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, - - 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f diff --git a/Nuake/src/Rendering/Textures/Material.cpp b/Nuake/src/Rendering/Textures/Material.cpp index 7131a3f4..0f3e175c 100644 --- a/Nuake/src/Rendering/Textures/Material.cpp +++ b/Nuake/src/Rendering/Textures/Material.cpp @@ -8,6 +8,7 @@ #include #include #include +#include Ref Material::m_DefaultAlbedo; Ref Material::m_DefaultAO; Ref Material::m_DefaultNormal; @@ -18,10 +19,14 @@ Ref Material::m_DefaultDisplacement; Material::Material(const std::string albedo) { + glGenBuffers(1, &UBO); + glBindBuffer(GL_UNIFORM_BUFFER, UBO); + glBufferData(GL_UNIFORM_BUFFER, sizeof(UBOStructure), NULL, GL_STATIC_DRAW); + glBindBuffer(GL_UNIFORM_BUFFER, 0); m_Albedo = TextureManager::Get()->GetTexture(albedo); - UseAlbedo = true; + data.u_HasAlbedo = 1; std::stringstream ss(albedo); std::string item; @@ -42,8 +47,6 @@ Material::Material(const std::string albedo) m_DefaultRoughness = TextureManager::Get()->GetTexture("resources/Textures/default/Default.png"); if (m_DefaultMetalness == nullptr) m_DefaultMetalness = TextureManager::Get()->GetTexture("resources/Textures/default/Default.png"); - - } //Material::Material() @@ -52,11 +55,20 @@ Material::Material(const std::string albedo) Material::Material(const glm::vec3 albedoColor) { - m_AlbedoColor = albedoColor; + glGenBuffers(1, &UBO); + glBindBuffer(GL_UNIFORM_BUFFER, UBO); + glBufferData(GL_UNIFORM_BUFFER, sizeof(UBOStructure), NULL, GL_STATIC_DRAW); + glBindBuffer(GL_UNIFORM_BUFFER, 0); + + data.m_AlbedoColor = myVec3{ albedoColor.r, albedoColor.g, albedoColor.b}; + m_Name = "New material"; + if (m_DefaultAlbedo == nullptr) m_DefaultAlbedo = TextureManager::Get()->GetTexture("resources/Textures/default/Default.png"); + m_Albedo = m_DefaultAlbedo; + if (m_DefaultAO == nullptr) m_DefaultAO = TextureManager::Get()->GetTexture("resources/Textures/default/Default.png"); if (m_DefaultNormal == nullptr) @@ -69,23 +81,22 @@ Material::Material(const glm::vec3 albedoColor) m_DefaultMetalness = TextureManager::Get()->GetTexture("resources/Textures/default/Default.png"); } -Material::~Material() -{ - -} +Material::~Material() {} void Material::Bind() { - + glBindBuffer(GL_UNIFORM_BUFFER, UBO); + glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(UBOStructure), &data); + + glBindBuffer(GL_UNIFORM_BUFFER, 0); + glBindBufferBase(GL_UNIFORM_BUFFER, 32, UBO); + // Albedo if (m_Albedo != nullptr) m_Albedo->Bind(4); else m_DefaultAlbedo->Bind(4); Renderer::m_Shader->SetUniform1i("m_Albedo", 4); - Renderer::m_Shader->SetUniform1i("u_HasAlbedo", UseAlbedo); - Renderer::m_Shader->SetUniform3f("m_AlbedoColor", m_AlbedoColor.r, m_AlbedoColor.g, m_AlbedoColor.b); - Renderer::m_GBufferShader->SetUniform1i("m_Albedo", 4); // AO if (m_AO != nullptr) @@ -93,33 +104,28 @@ void Material::Bind() else m_DefaultAO->Bind(5); Renderer::m_Shader->SetUniform1i("m_AO", 5); - Renderer::m_Shader->SetUniform1i("u_HasAO", UseAO); - Renderer::m_Shader->SetUniform1f("u_AOValue", m_AOValue); - Renderer::m_GBufferShader->SetUniform1i("m_AO", 5); + // Metallic if (m_Metalness != nullptr) m_Metalness->Bind(6); else m_DefaultMetalness->Bind(6); Renderer::m_Shader->SetUniform1i("m_Metalness", 6); - Renderer::m_Shader->SetUniform1i("u_HasMetalness", UseMetalness); - Renderer::m_Shader->SetUniform1f("u_MetalnessValue", m_MetalnessValue); - Renderer::m_GBufferShader->SetUniform1i("m_Metalness", 6); + // Roughness if (m_Roughness != nullptr) m_Roughness->Bind(7); else m_DefaultRoughness->Bind(7); Renderer::m_Shader->SetUniform1i("m_Roughness", 7); - Renderer::m_Shader->SetUniform1i("u_HasRoughness", UseRoughness); - Renderer::m_Shader->SetUniform1f("u_RoughnessValue", m_RoughnessValue); - Renderer::m_GBufferShader->SetUniform1i("m_Roughness", 7); + // Normal if (m_Normal != nullptr) m_Normal->Bind(8); else m_DefaultNormal->Bind(8); Renderer::m_Shader->SetUniform1i("m_Normal", 8); + // Displacement if (m_Displacement != nullptr) m_Displacement->Bind(9); @@ -129,6 +135,10 @@ void Material::Bind() //Renderer::m_Shader->SetUniform1i("m_Displacement", 9); } +void Material::SetupUniformBuffer() +{ +} + void Material::SetName(const std::string name) { m_Name = name; diff --git a/Nuake/src/Rendering/Textures/Material.h b/Nuake/src/Rendering/Textures/Material.h index 050ad18e..159432cb 100644 --- a/Nuake/src/Rendering/Textures/Material.h +++ b/Nuake/src/Rendering/Textures/Material.h @@ -2,12 +2,35 @@ #include "../../Core/TextureManager.h" #include "Texture.h" #include +#include #include "../Core/Core.h" + + +struct myVec3 { + float r; + float g; + float b; + float a; +}; + +struct UBOStructure { + int u_HasAlbedo; + myVec3 m_AlbedoColor; + uint32_t u_HasMetalness; + float u_MetalnessValue; + uint32_t u_HasRoughness; + float u_RoughnessValue; + uint32_t u_HasAO; + float u_AOValue; + uint32_t u_HasNormal; + uint32_t u_HasDisplacement; +}; + class Material { private: std::string m_Name; - + unsigned int UBO; public: Ref m_Albedo; Ref m_AO; @@ -16,12 +39,19 @@ public: Ref m_Normal; Ref m_Displacement; - bool UseAlbedo = false; - bool UseNormal = false; - bool UseMetalness = false; - bool UseRoughness = false; - bool UseDisplacement = false; - bool UseAO = false; + + UBOStructure data{ + 0, // has labedo + myVec3{0.f, 0.f, 0.f, 1.0f}, // m_AlbedoColor + 0, // u_HasMetalness + 0.5f, // u_MetalnessValue + 0, // u_HasRoughness + 0.5f, // u_RoughnessValue + 0, // u_HasAO + 0.5f, // u_AOValue + 0, // u_HasNormal + 0 // u_HasDisplacement + }; static Ref m_DefaultAlbedo; static Ref m_DefaultAO; @@ -30,44 +60,40 @@ public: static Ref m_DefaultNormal; static Ref m_DefaultDisplacement; - glm::vec3 m_AlbedoColor = glm::vec3(1.f, 1.f, 1.f); - float m_AOValue = 1.0f; - float m_MetalnessValue = 0.5f; - float m_RoughnessValue = 0.5f; - - Material(const std::string albedo); Material(Ref texture) { m_Albedo = texture; } Material(const glm::vec3 albedoColor); + ~Material(); void Bind(); + void SetupUniformBuffer(); + void SetName(const std::string name); std::string GetName(); + bool HasAlbedo() { return m_Albedo != nullptr; } void SetAlbedo(const std::string path) { m_Albedo = CreateRef(path); } void SetAlbedo(Ref texture) { m_Albedo = texture; } + bool HasAO() { return m_AO != nullptr; } void SetAO(const std::string albedo); void SetAO(Ref texture) { m_AO = texture; } + bool HasMetalness() { return m_Metalness != nullptr; } void SetMetalness(const std::string albedo); void SetMetalness(Ref texture) { m_Metalness = texture; } + bool HasRougness() { return m_Roughness != nullptr; } void SetRoughness(const std::string albedo); void SetRoughness(Ref texture) { m_Roughness = texture; } + bool HasNormal() { return m_Normal != nullptr; } void SetNormal(const std::string albedo); void SetNormal(Ref texture) { m_Normal = texture; } + bool HasDisplacement() { return m_Displacement != nullptr; } void SetDisplacement(const std::string displacement); void SetDisplacement(Ref texture) { m_Displacement = texture; } - - bool HasAlbedo() { return m_Albedo != nullptr; } - bool HasNormal() { return m_Normal != nullptr; } - bool HasAO() { return m_AO != nullptr; } - bool HasMetalness() { return m_Metalness != nullptr; } - bool HasRougness() { return m_Roughness != nullptr; } - bool HasDisplacement() { return m_Displacement != nullptr; } }; \ No newline at end of file diff --git a/Nuake/src/Rendering/UniformBuffer.cpp b/Nuake/src/Rendering/UniformBuffer.cpp new file mode 100644 index 00000000..ce789a60 --- /dev/null +++ b/Nuake/src/Rendering/UniformBuffer.cpp @@ -0,0 +1,7 @@ +#include "UniformBuffer.h" + + +UniformBuffer::UniformBuffer() +{ + +} \ No newline at end of file diff --git a/Nuake/src/Rendering/UniformBuffer.h b/Nuake/src/Rendering/UniformBuffer.h new file mode 100644 index 00000000..bb7d6296 --- /dev/null +++ b/Nuake/src/Rendering/UniformBuffer.h @@ -0,0 +1,16 @@ +#pragma once + + +class UniformBuffer { +private: + unsigned int RendererID; +public: + + UniformBuffer(); + + + + void Bind(); + void Unbind(); + +}; \ No newline at end of file diff --git a/Nuake/src/Scene/Components/BSPBrushComponent.h b/Nuake/src/Scene/Components/BSPBrushComponent.h new file mode 100644 index 00000000..85a521dc --- /dev/null +++ b/Nuake/src/Scene/Components/BSPBrushComponent.h @@ -0,0 +1,21 @@ +#pragma once +#include "../Core/Core.h" +#include "../Core/Maths.h" + +#include "../Rendering/Textures/Material.h" +#include "../Rendering/Mesh/Mesh.h" + +class BSPBrushComponent { +public: + std::vector> Meshes; + std::vector< Ref> Materials; + + bool IsSolid = true; + bool IsTrigger = false; + bool IsTransparent = false; + + BSPBrushComponent() { + Meshes = std::vector>(); + Materials = std::vector>(); + } +}; \ No newline at end of file diff --git a/Nuake/src/Scene/Components/LightComponent.h b/Nuake/src/Scene/Components/LightComponent.h index b8c71cca..fafdf112 100644 --- a/Nuake/src/Scene/Components/LightComponent.h +++ b/Nuake/src/Scene/Components/LightComponent.h @@ -8,12 +8,12 @@ #include "../Resource/Serializable.h" #include + enum LightType { Directional, Point, Spot }; class LightComponent { - public: glm::vec2 yes = glm::vec2(2, 2); diff --git a/Nuake/src/Scene/Components/QuakeMap.cpp b/Nuake/src/Scene/Components/QuakeMap.cpp index 8feb86ff..a6a3fa3f 100644 --- a/Nuake/src/Scene/Components/QuakeMap.cpp +++ b/Nuake/src/Scene/Components/QuakeMap.cpp @@ -1,12 +1,7 @@ #include "QuakeMap.h" #include "../Core/Core.h" #include "../Core/MaterialManager.h" -// Lib map stuff. -extern "C" { -#include "libmap/h/map_parser.h" -#include -#include -} + void QuakeMapComponent::Draw() { @@ -15,8 +10,6 @@ void QuakeMapComponent::Draw() } } - - void QuakeMapComponent::Load(std::string path, bool collisions) { if (Path == path) @@ -24,106 +17,106 @@ void QuakeMapComponent::Load(std::string path, bool collisions) Path = path; - Build(); + //Build(); } void QuakeMapComponent::Build() { - m_Meshes.clear(); - map_parser_load(Path.c_str()); - - geo_generator_run(); - - Ref DefaultMaterial = MaterialManager::Get()->GetMaterial("resources/Textures/default/Default.png"); - for (int e = 0; e < entity_count; ++e) - { - entity* entity_inst = &entities[e]; - entity_geometry* entity_geo_inst = &entity_geo[e]; - - - for (int b = 0; b < entity_inst->brush_count; ++b) - { - brush* brush_inst = &entity_inst->brushes[b]; - brush_geometry* brush_geo_inst = &entity_geo_inst->brushes[b]; - - std::vector vertices; - std::vector indices; - - int index_offset = 0; - int lastTextureID = -1; - std::string lastTexturePath = ""; - for (int f = 0; f < brush_inst->face_count; ++f) - { - face* face = &brush_inst->faces[f]; - texture_data* texture = &textures[face->texture_idx]; - if (std::string(texture->name) == "__TB_empty") - { - texture->height = 1; - texture->width = 1; - } - else - { - std::string path = "resources/Textures/" + std::string(texture->name) + ".png"; - auto tex = TextureManager::Get()->GetTexture(path); - texture->height = tex->GetHeight(); - texture->width = tex->GetWidth(); - } - - - face_geometry* face_geo_inst = &brush_geo_inst->faces[f]; - //printf("Face %d\n", f); - for (int i = 0; i < face_geo_inst->vertex_count; ++i) - { - face_vertex vertex = face_geo_inst->vertices[i]; - vertex_uv vertex_uv = get_standard_uv(vertex.vertex, face, texture->width, texture->height); - vertices.push_back(Vertex{ - glm::vec3(vertex.vertex.y * (1.0f / 64), vertex.vertex.z * (1.0f / 64), vertex.vertex.x * (1.0f / 64)), - glm::vec2(vertex_uv.u, 1.0 -vertex_uv.v), - glm::vec3(vertex.normal.y, vertex.normal.z, vertex.normal.x), - glm::vec3(vertex.tangent.y, vertex.tangent.z, vertex.tangent.x), glm::vec3(0.0, 1.0, 0.0), 0.0f - }); - - //printf("vertex: (%f %f %f), normal: (%f %f %f)\n", - // vertex.vertex.x, vertex.vertex.y, vertex.vertex.z, - // vertex.normal.x, vertex.normal.y, vertex.normal.z); - } - - //puts("Indices:"); - for (int i = 0; i < (face_geo_inst->vertex_count - 2) * 3; ++i) - { - unsigned int index = face_geo_inst->indices[i]; - //printf("index: %d\n", index_offset + index); - indices.push_back(index_offset + (unsigned int)index); - } - if (lastTextureID != face->texture_idx) - { - lastTexturePath = "resources/Textures/" + std::string(texture->name) + ".png"; - if (std::string(texture->name) == "__TB_empty") - m_Meshes.push_back(CreateRef(vertices, indices, DefaultMaterial)); - else - m_Meshes.push_back(CreateRef(vertices, indices, MaterialManager::Get()->GetMaterial(lastTexturePath))); - - - - index_offset = 0; - vertices.clear(); - indices.clear(); - lastTextureID = face->texture_idx; - - - } - else - { - index_offset += (face_geo_inst->vertex_count); - } - } - - if (vertices.size() > 0) - m_Meshes.push_back(CreateRef(vertices, indices, MaterialManager::Get()->GetMaterial(lastTexturePath))); - //putchar('\n'); - //putchar('\n'); - } - } + //m_Meshes.clear(); + //map_parser_load(Path.c_str()); + // + //geo_generator_run(); + // + //Ref DefaultMaterial = MaterialManager::Get()->GetMaterial("resources/Textures/default/Default.png"); + //for (int e = 0; e < entity_count; ++e) + //{ + // entity* entity_inst = &entities[e]; + // entity_geometry* entity_geo_inst = &entity_geo[e]; + // + // + // for (int b = 0; b < entity_inst->brush_count; ++b) + // { + // brush* brush_inst = &entity_inst->brushes[b]; + // brush_geometry* brush_geo_inst = &entity_geo_inst->brushes[b]; + // + // std::vector vertices; + // std::vector indices; + // + // int index_offset = 0; + // int lastTextureID = -1; + // std::string lastTexturePath = ""; + // for (int f = 0; f < brush_inst->face_count; ++f) + // { + // face* face = &brush_inst->faces[f]; + // texture_data* texture = &textures[face->texture_idx]; + // if (std::string(texture->name) == "__TB_empty") { + // texture->height = 1; + // texture->width = 1; + // } + // else { + // std::string path = "resources/Textures/" + std::string(texture->name) + ".png"; + // auto tex = TextureManager::Get()->GetTexture(path); + // texture->height = tex->GetHeight(); + // texture->width = tex->GetWidth(); + // } + // + // + // face_geometry* face_geo_inst = &brush_geo_inst->faces[f]; + // //printf("Face %d\n", f); + // for (int i = 0; i < face_geo_inst->vertex_count; ++i) + // { + // face_vertex vertex = face_geo_inst->vertices[i]; + // vertex_uv vertex_uv = get_standard_uv(vertex.vertex, face, texture->width, texture->height); + // vertices.push_back(Vertex{ + // glm::vec3((vertex.vertex.y - brush_inst->center.y) * (1.0f / 64), + // (vertex.vertex.z - brush_inst->center.z) * (1.0f / 64), + // (vertex.vertex.x - brush_inst->center.x) * (1.0f / 64)), + // glm::vec2(vertex_uv.u, 1.0 - vertex_uv.v), + // glm::vec3(vertex.normal.y, vertex.normal.z, vertex.normal.x), + // glm::vec3(vertex.tangent.y, vertex.tangent.z, vertex.tangent.x), glm::vec3(0.0, 1.0, 0.0), 0.0f + // }); + // + // //printf("vertex: (%f %f %f), normal: (%f %f %f)\n", + // // vertex.vertex.x, vertex.vertex.y, vertex.vertex.z, + // // vertex.normal.x, vertex.normal.y, vertex.normal.z); + // } + // + // //puts("Indices:"); + // for (int i = 0; i < (face_geo_inst->vertex_count - 2) * 3; ++i) + // { + // unsigned int index = face_geo_inst->indices[i]; + // //printf("index: %d\n", index_offset + index); + // indices.push_back(index_offset + (unsigned int)index); + // } + // if (lastTextureID != face->texture_idx) + // { + // lastTexturePath = "resources/Textures/" + std::string(texture->name) + ".png"; + // if (std::string(texture->name) == "__TB_empty") + // m_Meshes.push_back(CreateRef(vertices, indices, DefaultMaterial)); + // else + // m_Meshes.push_back(CreateRef(vertices, indices, MaterialManager::Get()->GetMaterial(lastTexturePath))); + // + // + // + // index_offset = 0; + // vertices.clear(); + // indices.clear(); + // lastTextureID = face->texture_idx; + // + // + // } + // else + // { + // index_offset += (face_geo_inst->vertex_count); + // } + // } + // + // if (vertices.size() > 0) + // m_Meshes.push_back(CreateRef(vertices, indices, MaterialManager::Get()->GetMaterial(lastTexturePath))); + // //putchar('\n'); + // //putchar('\n'); + // } + //} } void QuakeMapComponent::DrawEditor() { diff --git a/Nuake/src/Scene/Components/QuakeMap.h b/Nuake/src/Scene/Components/QuakeMap.h index 2d4d21c0..35072248 100644 --- a/Nuake/src/Scene/Components/QuakeMap.h +++ b/Nuake/src/Scene/Components/QuakeMap.h @@ -8,7 +8,6 @@ class QuakeMapComponent { private: - public: std::vector> m_Meshes; Ref Map; diff --git a/Nuake/src/Scene/Components/TransformComponent.cpp b/Nuake/src/Scene/Components/TransformComponent.cpp index 9868b202..7a904dad 100644 --- a/Nuake/src/Scene/Components/TransformComponent.cpp +++ b/Nuake/src/Scene/Components/TransformComponent.cpp @@ -3,6 +3,7 @@ TransformComponent::TransformComponent() { + GlobalTranslation = Vector3(0, 0, 0); Translation = Vector3(0, 0, 0); Rotation = Vector3(0, 0, 0); Scale = Vector3(1, 1, 1); @@ -11,7 +12,7 @@ TransformComponent::TransformComponent() glm::mat4 TransformComponent::GetTransform() { Matrix4 transform = Matrix4(1.0f); - transform = glm::translate(transform, Translation); + transform = glm::translate(transform, GlobalTranslation); transform = glm::rotate(transform, glm::radians(Rotation.x), Vector3(1, 0, 0)); transform = glm::rotate(transform, glm::radians(Rotation.y), Vector3(0, 1, 0)); transform = glm::rotate(transform, glm::radians(Rotation.z), Vector3(0, 0, 1)); diff --git a/Nuake/src/Scene/Components/TransformComponent.h b/Nuake/src/Scene/Components/TransformComponent.h index bf9da049..957fb58a 100644 --- a/Nuake/src/Scene/Components/TransformComponent.h +++ b/Nuake/src/Scene/Components/TransformComponent.h @@ -4,6 +4,7 @@ class TransformComponent { public: + Vector3 GlobalTranslation; Vector3 Translation; Vector3 Rotation; // TODO: Should use quaternions. Vector3 Scale; diff --git a/Nuake/src/Scene/Entities/Entity.h b/Nuake/src/Scene/Entities/Entity.h index 6b0c9ed9..75ed12c4 100644 --- a/Nuake/src/Scene/Entities/Entity.h +++ b/Nuake/src/Scene/Entities/Entity.h @@ -38,7 +38,8 @@ public: } void Destroy() { - m_Scene->m_Registry.destroy(m_EntityHandle); + if(m_Scene->m_Registry.valid(m_EntityHandle)) + m_Scene->m_Registry.destroy(m_EntityHandle); } bool operator==(const Entity& other) const @@ -54,6 +55,9 @@ public: json Serialize() override; bool Deserialize(const std::string& str); + Scene* GetScene() { + return m_Scene; + } private: entt::entity m_EntityHandle; Scene* m_Scene; diff --git a/Nuake/src/Scene/Scene.cpp b/Nuake/src/Scene/Scene.cpp index 15feedab..8d28e40d 100644 --- a/Nuake/src/Scene/Scene.cpp +++ b/Nuake/src/Scene/Scene.cpp @@ -1,6 +1,11 @@ #pragma once #include #include + + +#include +#include + #include "Scene.h" #include "Entities/Entity.h" @@ -69,11 +74,10 @@ void Scene::OnExit() void Scene::Update(Timestep ts) { + UpdatePositions(); + for (auto& system : m_Systems) system->Update(ts); - - - } void Scene::FixedUpdate(Timestep ts) @@ -84,12 +88,36 @@ void Scene::FixedUpdate(Timestep ts) void Scene::EditorUpdate(Timestep ts) { + UpdatePositions(); + m_EditorCamera->Update(ts); for (auto i : m_Interfaces) i->Update(ts); } +void Scene::UpdatePositions() +{ + auto transformView = m_Registry.view(); + for (auto e : transformView) { + auto [parent, transform] = transformView.get(e); + Entity currentParent = Entity{ e, this }; + Vector3 globalPos = Vector3(); + if (parent.HasParent) + { + while (currentParent.GetComponent().HasParent) { + currentParent = currentParent.GetComponent().Parent; + globalPos += currentParent.GetComponent().Translation; + } + + transform.GlobalTranslation = globalPos + transform.Translation; + } + else + { + transform.GlobalTranslation = transform.Translation; + } + } +} void Scene::DrawShadows() { @@ -97,9 +125,8 @@ void Scene::DrawShadows() auto quakeView = m_Registry.view(); auto view = m_Registry.view(); - Ref cam = nullptr; - if (Engine::IsPlayMode) - { + Ref cam = m_EditorCamera; + if (Engine::IsPlayMode) { auto view = m_Registry.view(); for (auto e : view) { auto [transform, camera] = view.get(e); @@ -107,10 +134,6 @@ void Scene::DrawShadows() break; } } - else - { - cam = m_EditorCamera; - } glm::mat4 perspective = cam->GetPerspective(); @@ -121,7 +144,6 @@ void Scene::DrawShadows() light.CalculateViewProjection(cam->GetTransform(), cam->GetPerspective()); - light.BeginDrawShadow(); for (int i = 0; i < 4; i++) { @@ -139,22 +161,20 @@ void Scene::DrawShadows() model.Draw(); } + auto quakeView = m_Registry.view(); for (auto e : quakeView) { - auto [transform, model] = quakeView.get(e); - - glm::vec3 pos = lightTransform.Translation; - glm::mat4 lightView = glm::lookAt(pos, pos - light.GetDirection(), glm::vec3(0.0f, 1.0f, 0.0f)); - + auto [transform, model, parent] = quakeView.get(e); Renderer::m_ShadowmapShader->SetUniformMat4f("lightSpaceMatrix", light.mViewProjections[i]); Renderer::m_ShadowmapShader->SetUniformMat4f("model", transform.GetTransform()); - model.Draw(); + for (auto& e : model.Meshes) { + e->Draw(); + } } light.m_Framebuffers[i]->Unbind(); } - light.EndDrawShadow(); } } @@ -167,7 +187,6 @@ void Scene::DrawInterface(Vector2 screensize) } } - void Scene::Draw() { // Find the camera of the scene. @@ -176,24 +195,8 @@ void Scene::Draw() auto view = m_Registry.view(); for (auto e : view) { auto [transform, camera, parent] = view.get(e); - - TransformComponent copyT = transform; - if (parent.HasParent) - { - Entity currentParent = Entity{ e, this }; - glm::vec3 globalOffset = copyT.Translation; - - while (currentParent.GetComponent().HasParent) - { - currentParent = currentParent.GetComponent().Parent; - globalOffset += currentParent.GetComponent().Translation; - } - - copyT.Translation = globalOffset; - } - cam = camera.CameraInstance; - cam->Translation = copyT.Translation; + cam->Translation = transform.GlobalTranslation; break; } } @@ -216,26 +219,10 @@ void Scene::Draw() for (auto l : view) { auto [transform, light, parent] = view.get(l); - TransformComponent copyT = transform; - if (parent.HasParent) - { - Entity currentParent = Entity{ l, this }; - glm::vec3 globalOffset = copyT.Translation; - - while (currentParent.GetComponent().HasParent) - { - currentParent = currentParent.GetComponent().Parent; - globalOffset += currentParent.GetComponent().Translation; - - } - - copyT.Translation = globalOffset; - } - if (light.SyncDirectionWithSky) light.Direction = GetEnvironment()->ProceduralSkybox->GetSunDirection(); - light.Draw(copyT, m_EditorCamera); + light.Draw(transform, m_EditorCamera); } } glEnable(GL_CULL_FACE); @@ -249,59 +236,31 @@ void Scene::Draw() auto view = m_Registry.view(); for (auto e : view) { - auto [transform, model, parent] = view.get(e); - TransformComponent copyT = transform; - if (parent.HasParent) - { - Entity currentParent = Entity{ e, this }; - glm::vec3 globalOffset = copyT.Translation; - - while (currentParent.GetComponent().HasParent) - { - currentParent = currentParent.GetComponent().Parent; - globalOffset += currentParent.GetComponent().Translation; - } - - copyT.Translation = globalOffset; - } - - + auto [transform, model, parent] = view.get(e); Renderer::m_Shader->SetUniformMat4f("u_View", cam->GetTransform()); Renderer::m_Shader->SetUniformMat4f("u_Projection", cam->GetPerspective()); - Renderer::m_Shader->SetUniformMat4f("u_Model", copyT.GetTransform()); + Renderer::m_Shader->SetUniformMat4f("u_Model", transform.GetTransform()); model.Draw(); } - auto quakeView = m_Registry.view(); + auto quakeView = m_Registry.view(); for (auto e : quakeView) { - auto [transform, model, parent] = quakeView.get(e); - - TransformComponent copyT = transform; - if (parent.HasParent) - { - Entity currentParent = Entity{ e, this }; - glm::vec3 globalOffset = copyT.Translation; - - while (currentParent.GetComponent().HasParent) - { - currentParent = currentParent.GetComponent().Parent; - globalOffset += currentParent.GetComponent().Translation; - } - - copyT.Translation = globalOffset; - } + auto [transform, model, parent] = quakeView.get(e); + Renderer::m_Shader->SetUniformMat4f("u_View", cam->GetTransform()); Renderer::m_Shader->SetUniformMat4f("u_Projection", cam->GetPerspective()); - Renderer::m_Shader->SetUniformMat4f("u_Model", copyT.GetTransform()); - model.Draw(); + Renderer::m_Shader->SetUniformMat4f("u_Model", transform.GetTransform()); + + for (auto& e : model.Meshes) + { + e->Draw(); + } } Renderer::m_DebugShader->SetUniformMat4f("u_View", cam->GetTransform()); Renderer::m_DebugShader->SetUniformMat4f("u_Projection", cam->GetPerspective()); PhysicsManager::Get()->DrawDebug(); } - - } void Scene::EditorDraw() @@ -309,6 +268,7 @@ void Scene::EditorDraw() glDisable(GL_DEPTH_TEST); Ref env = GetEnvironment(); + glDisable(GL_CULL_FACE); if (env->ProceduralSkybox) { env->ProceduralSkybox->Draw(m_EditorCamera); @@ -323,29 +283,15 @@ void Scene::EditorDraw() auto view = m_Registry.view(); for (auto l : view) { auto [transform, light, parent] = view.get(l); - - TransformComponent copyT = transform; - if (parent.HasParent) - { - Entity currentParent = Entity{ l, this }; - glm::vec3 globalOffset = copyT.Translation; - - while (currentParent.GetComponent().HasParent) - { - currentParent = currentParent.GetComponent().Parent; - globalOffset += currentParent.GetComponent().Translation; - } - - copyT.Translation = globalOffset; - } - + if (light.SyncDirectionWithSky) light.Direction = GetEnvironment()->ProceduralSkybox->GetSunDirection(); - light.Draw(copyT, m_EditorCamera); + light.Draw(transform, m_EditorCamera); } } - + glEnable(GL_CULL_FACE); + glCullFace(GL_FRONT); Renderer::m_Shader->Bind(); Renderer::m_Shader->SetUniform1i("u_ShowNormal", 0); if (m_EditorCamera) @@ -355,77 +301,33 @@ void Scene::EditorDraw() auto view = m_Registry.view(); for (auto e : view) { auto [transform, model, parent] = view.get(e); - - TransformComponent copyT = transform; - if (parent.HasParent) - { - Entity currentParent = Entity{ e, this }; - glm::vec3 globalOffset = copyT.Translation; - - while (currentParent.GetComponent().HasParent) - { - currentParent = currentParent.GetComponent().Parent; - globalOffset += currentParent.GetComponent().Translation; - } - - copyT.Translation = globalOffset; - } - + Renderer::m_Shader->SetUniformMat4f("u_View", m_EditorCamera->GetTransform()); Renderer::m_Shader->SetUniformMat4f("u_Projection", m_EditorCamera->GetPerspective()); - Renderer::m_Shader->SetUniformMat4f("u_Model", copyT.GetTransform()); + Renderer::m_Shader->SetUniformMat4f("u_Model", transform.GetTransform()); model.Draw(); } - auto quakeView = m_Registry.view(); + auto quakeView = m_Registry.view(); for (auto e : quakeView) { - auto [transform, model, parent] = quakeView.get(e); - - - TransformComponent copyT = transform; - if (parent.HasParent) - { - Entity currentParent = Entity{ e, this }; - glm::vec3 globalOffset = copyT.Translation; - - while (currentParent.GetComponent().HasParent) - { - currentParent = currentParent.GetComponent().Parent; - globalOffset += currentParent.GetComponent().Translation; - } - - copyT.Translation = globalOffset; - } + auto [transform, model, parent] = quakeView.get(e); + Renderer::m_Shader->SetUniformMat4f("u_View", m_EditorCamera->GetTransform()); Renderer::m_Shader->SetUniformMat4f("u_Projection", m_EditorCamera->GetPerspective()); - - Renderer::m_Shader->SetUniformMat4f("u_Model", copyT.GetTransform()); - model.Draw(); + Renderer::m_Shader->SetUniformMat4f("u_Model", transform.GetTransform()); + + for (auto& e : model.Meshes) { + e->Draw(); + } } auto boxCollider = m_Registry.view(); for (auto e : boxCollider) { auto [transform, box, parent] = boxCollider.get(e); - TransformComponent copyT = transform; - if (parent.HasParent) - { - Entity currentParent = Entity{ e, this }; - glm::vec3 globalOffset = copyT.Translation; - - while (currentParent.GetComponent().HasParent) - { - currentParent = currentParent.GetComponent().Parent; - globalOffset += currentParent.GetComponent().Translation; - - } - - copyT.Translation = globalOffset; - } - Renderer::m_DebugShader->SetUniformMat4f("u_View", m_EditorCamera->GetTransform()); Renderer::m_DebugShader->SetUniformMat4f("u_Projection", m_EditorCamera->GetPerspective()); - Renderer::m_DebugShader->SetUniformMat4f("u_Model", copyT.GetTransform()); + Renderer::m_DebugShader->SetUniformMat4f("u_Model", transform.GetTransform()); TransformComponent t = transform; t.Scale = (box.Size * 2.f) * transform.Scale; @@ -436,22 +338,6 @@ void Scene::EditorDraw() for (auto e : sphereCollider) { auto [transform, box, parent] = sphereCollider.get(e); - TransformComponent copyT = transform; - if (parent.HasParent) - { - Entity currentParent = Entity{ e, this }; - glm::vec3 globalOffset = copyT.Translation; - - while (currentParent.GetComponent().HasParent) - { - currentParent = currentParent.GetComponent().Parent; - globalOffset += currentParent.GetComponent().Translation; - - } - - copyT.Translation = globalOffset; - } - Renderer::m_DebugShader->SetUniformMat4f("u_View", m_EditorCamera->GetTransform()); Renderer::m_DebugShader->SetUniformMat4f("u_Projection", m_EditorCamera->GetPerspective()); Renderer::m_DebugShader->SetUniformMat4f("u_Model", transform.GetTransform()); @@ -517,7 +403,16 @@ Entity Scene::CreateEntity(const std::string& name) { return entity; } -void Scene::DestroyEntity(Entity entity) { +void Scene::DestroyEntity(Entity entity) +{ + ParentComponent& parentComponent = entity.GetComponent(); + if (parentComponent.HasParent) { // Remove self from parents children lists. + int idx = 0; + ParentComponent& parentParentComponent = parentComponent.Parent.GetComponent(); + } + for (auto& c : parentComponent.Children) { + DestroyEntity(c); + } entity.Destroy(); } diff --git a/Nuake/src/Scene/Scene.h b/Nuake/src/Scene/Scene.h index 1b8ae0ff..549de6cc 100644 --- a/Nuake/src/Scene/Scene.h +++ b/Nuake/src/Scene/Scene.h @@ -46,6 +46,8 @@ public: void FixedUpdate(Timestep ts); void EditorUpdate(Timestep ts); + void UpdatePositions(); + // TODO: Maybe move this to Renderer::DrawScene() ? void DrawShadows(); void DrawInterface(Vector2 screensize); diff --git a/Nuake/src/Scene/Systems/PhysicsSystem.cpp b/Nuake/src/Scene/Systems/PhysicsSystem.cpp index 1412f072..a4f0b5de 100644 --- a/Nuake/src/Scene/Systems/PhysicsSystem.cpp +++ b/Nuake/src/Scene/Systems/PhysicsSystem.cpp @@ -7,6 +7,7 @@ #include #include #include +#include PhysicsSystem::PhysicsSystem(Scene* scene) { m_Scene = scene; @@ -19,7 +20,6 @@ void PhysicsSystem::Init() for (auto e : view) { auto [transform, rigidbody] = view.get(e); - Entity ent = Entity({ e, m_Scene }); if (ent.HasComponent()) @@ -50,22 +50,21 @@ void PhysicsSystem::Init() PhysicsManager::Get()->RegisterCharacterController(cc.CharacterController); } - auto quakeMapview = m_Scene->m_Registry.view(); - for (auto e : quakeMapview) + auto bspView = m_Scene->m_Registry.view(); + for (auto e : bspView) { - auto [transform, quake] = quakeMapview.get(e); + auto [transform, brush] = bspView.get(e); - if (quake.HasCollisions) + if (brush.IsSolid) { - for (auto m : quake.m_Meshes) + for (auto m : brush.Meshes) { Ref meshShape = CreateRef(m); - Ref btRigidbody = CreateRef(0.0f, transform.Translation, meshShape); + Ref btRigidbody = CreateRef(0.0f, transform.GlobalTranslation, meshShape); PhysicsManager::Get()->RegisterBody(btRigidbody); } } } - } void PhysicsSystem::Update(Timestep ts) diff --git a/Nuake/src/Scene/Systems/QuakeMapBuilder.cpp b/Nuake/src/Scene/Systems/QuakeMapBuilder.cpp new file mode 100644 index 00000000..499f88cf --- /dev/null +++ b/Nuake/src/Scene/Systems/QuakeMapBuilder.cpp @@ -0,0 +1,171 @@ +#include "QuakeMapBuilder.h" +#include +#include +#include +#include +#include +#include +#include +extern "C" { + #include "libmap/h/map_parser.h" + #include + #include +} + +#include +#include +#include +#include + + +std::vector split(const std::string& s, char delim) { + std::vector result; + std::stringstream ss(s); + std::string item; + + while (getline(ss, item, delim)) { + result.push_back(item); + } + + return result; +} + +void QuakeMapBuilder::BuildQuakeMap(Entity& ent, bool Collisions) +{ + if (!ent.HasComponent()) + return; + + QuakeMapComponent& quakeMapC = ent.GetComponent(); + Scene* m_Scene = ent.GetScene(); + + // Clear old map entities. + ParentComponent& currentParent = ent.GetComponent(); + for (auto& e : currentParent.Children) { + m_Scene->DestroyEntity(e); + } + + currentParent.Children.clear(); + + map_parser_load(quakeMapC.Path.c_str()); + geo_generator_run(); + + Ref DefaultMaterial = MaterialManager::Get()->GetMaterial("resources/Textures/default/Default.png"); + for (int e = 0; e < entity_count; ++e) + { + entity* entity_inst = &entities[e]; + entity_geometry* entity_geo_inst = &entity_geo[e]; + + Entity newEntity = m_Scene->CreateEntity("Brush " + std::to_string(e) ); + ent.AddChild(newEntity); + for (int i = 0; i < entity_inst->property_count; i++) { + property* prop = &(entity_inst->properties)[i]; + std::string key = prop->key; + std::string value = prop->value; + + if (key == "origin") { + // Position + std::vector splits = split(value, ' '); + + float x = std::stof(splits[1]) * (1.f / 64.f); + float y = std::stof(splits[2]) * (1.f / 64.f); + float z = std::stof(splits[0]) * (1.f / 64.f); + + Vector3 position = Vector3(x, y, z); + newEntity.GetComponent().Translation = position; + } + + if (key == "classname") { + if (value == "light") { + newEntity.AddComponent(); + } + } + + } + for (int b = 0; b < entity_inst->brush_count; ++b) + { + Entity brushEntiuty = m_Scene->CreateEntity("Brush " + std::to_string(e)); + newEntity.AddChild(brushEntiuty); + brushEntiuty.AddComponent(); + std::vector vertices; + std::vector indices; + brush* brush_inst = &entity_inst->brushes[b]; + brush_geometry* brush_geo_inst = &entity_geo_inst->brushes[b]; + BSPBrushComponent& bsp = brushEntiuty.GetComponent(); + TransformComponent& transformComponent = brushEntiuty.GetComponent(); + transformComponent.Translation = Vector3(brush_inst->center.y * (1.0f / 64), + brush_inst->center.z * (1.0f / 64), + brush_inst->center.x * (1.0f / 64)); + + int index_offset = 0; + int lastTextureID = -1; + std::string lastTexturePath = ""; + for (int f = 0; f < brush_inst->face_count; ++f) + { + face* face = &brush_inst->faces[f]; + texture_data* texture = &textures[face->texture_idx]; + if (std::string(texture->name) == "__TB_empty") { + texture->height = 1; + texture->width = 1; + } + else { + std::string path = "resources/Textures/" + std::string(texture->name) + ".png"; + auto tex = TextureManager::Get()->GetTexture(path); + texture->height = tex->GetHeight(); + texture->width = tex->GetWidth(); + } + + face_geometry* face_geo_inst = &brush_geo_inst->faces[f]; + + for (int i = 0; i < face_geo_inst->vertex_count; ++i) + { + face_vertex vertex = face_geo_inst->vertices[i]; + vertex_uv vertex_uv = get_standard_uv(vertex.vertex, face, texture->width, texture->height); + + Vector3 vertexPos = Vector3( + (vertex.vertex.y - brush_inst->center.y) * (1.0f / 64), + (vertex.vertex.z - brush_inst->center.z) * (1.0f / 64), + (vertex.vertex.x - brush_inst->center.x) * (1.0f / 64) + ); + Vector2 vertexUV = Vector2(vertex_uv.u, 1.0 - vertex_uv.v); + Vector3 vertexNormal = Vector3(vertex.normal.y, vertex.normal.z, vertex.normal.x); + Vector3 vertexTangent = Vector3(vertex.tangent.y, vertex.tangent.z, vertex.tangent.x); + + vertices.push_back(Vertex{ + vertexPos, + vertexUV, + vertexNormal, + vertexTangent, + glm::vec3(0.0, 1.0, 0.0), 0.0f + }); + } + + for (int i = 0; i < (face_geo_inst->vertex_count - 2) * 3; ++i) + { + unsigned int index = face_geo_inst->indices[i]; + indices.push_back(index_offset + (unsigned int)index); + } + + if (lastTextureID != face->texture_idx) + { + lastTexturePath = "resources/Textures/" + std::string(texture->name) + ".png"; + if (std::string(texture->name) == "__TB_empty") + bsp.Meshes.push_back(CreateRef(vertices, indices, DefaultMaterial)); + else + bsp.Meshes.push_back(CreateRef(vertices, indices, MaterialManager::Get()->GetMaterial(lastTexturePath))); + + index_offset = 0; + vertices.clear(); + indices.clear(); + lastTextureID = face->texture_idx; + } + else + { + index_offset += (face_geo_inst->vertex_count); + } + } + + if (vertices.size() > 0) + bsp.Meshes.push_back(CreateRef(vertices, indices, MaterialManager::Get()->GetMaterial(lastTexturePath))); + } + } +} \ No newline at end of file diff --git a/Nuake/src/Scene/Systems/QuakeMapBuilder.h b/Nuake/src/Scene/Systems/QuakeMapBuilder.h new file mode 100644 index 00000000..8d6a7046 --- /dev/null +++ b/Nuake/src/Scene/Systems/QuakeMapBuilder.h @@ -0,0 +1,10 @@ +#pragma once + +class Entity; +class QuakeMapBuilder +{ +public: + QuakeMapBuilder(){} + + void BuildQuakeMap(Entity& ent, bool Collisions = true); +}; \ No newline at end of file diff --git a/Nuake/src/Scene/Systems/TrenchbroomSystem.cpp b/Nuake/src/Scene/Systems/TrenchbroomSystem.cpp new file mode 100644 index 00000000..65d51d2e --- /dev/null +++ b/Nuake/src/Scene/Systems/TrenchbroomSystem.cpp @@ -0,0 +1,27 @@ +#include "TrenchbroomSystem.h" + + +TrenchbroomSystem::TrenchbroomSystem(Scene* scene) +{ + m_Scene = scene; +} + +void TrenchbroomSystem::Init() +{ + +} + +void TrenchbroomSystem::Update(Timestep ts) +{ + +} + +void TrenchbroomSystem::FixedUpdate(Timestep ts) +{ + +} + +void TrenchbroomSystem::Exit() +{ + +} \ No newline at end of file diff --git a/Nuake/src/Scene/Systems/TrenchbroomSystem.h b/Nuake/src/Scene/Systems/TrenchbroomSystem.h new file mode 100644 index 00000000..bf1b48be --- /dev/null +++ b/Nuake/src/Scene/Systems/TrenchbroomSystem.h @@ -0,0 +1,12 @@ +#pragma once +#include + +class TrenchbroomSystem : public System { +public: + TrenchbroomSystem(Scene* scene); + void Init() override; + void Update(Timestep ts) override; + void Draw() override {} + void FixedUpdate(Timestep ts) override; + void Exit() override; +}; \ No newline at end of file diff --git a/Nuake/src/Scripting/Modules/SceneModule.h b/Nuake/src/Scripting/Modules/SceneModule.h index 6f28c308..b08d906c 100644 --- a/Nuake/src/Scripting/Modules/SceneModule.h +++ b/Nuake/src/Scripting/Modules/SceneModule.h @@ -54,7 +54,7 @@ namespace ScriptAPI int handle = wrenGetSlotDouble(vm, 1); std::string name = wrenGetSlotString(vm, 2); Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get()); - + if (name == "Transform") { bool result = ent.HasComponent(); diff --git a/Nuake/src/Vendors/libmap/h/entity.h b/Nuake/src/Vendors/libmap/h/entity.h index 34086aab..0a489196 100644 --- a/Nuake/src/Vendors/libmap/h/entity.h +++ b/Nuake/src/Vendors/libmap/h/entity.h @@ -20,7 +20,7 @@ enum entity_spawn_type typedef struct entity { int property_count; - property *properties; + property* properties; int brush_count; brush *brushes; diff --git a/Nuake/src/Vendors/libmap/h/geo_generator.h b/Nuake/src/Vendors/libmap/h/geo_generator.h index 0c8c2d79..0b69c06c 100644 --- a/Nuake/src/Vendors/libmap/h/geo_generator.h +++ b/Nuake/src/Vendors/libmap/h/geo_generator.h @@ -1,3 +1,4 @@ +#pragma once #ifndef GEO_GENERATOR_H #define GEO_GENERATOR_H diff --git a/Nuake/src/Vendors/libmap/h/map_parser.h b/Nuake/src/Vendors/libmap/h/map_parser.h index 69e76790..53f98be5 100644 --- a/Nuake/src/Vendors/libmap/h/map_parser.h +++ b/Nuake/src/Vendors/libmap/h/map_parser.h @@ -1,3 +1,4 @@ +#pragma once #ifndef MAP_PARSER_H #define MAP_PARSER_H diff --git a/Nuake/src/Vendors/libmap/h/surface_gatherer.h b/Nuake/src/Vendors/libmap/h/surface_gatherer.h index c1df9eec..6f05d232 100644 --- a/Nuake/src/Vendors/libmap/h/surface_gatherer.h +++ b/Nuake/src/Vendors/libmap/h/surface_gatherer.h @@ -1,3 +1,4 @@ +#pragma once #ifndef SURFACE_GATHERER_H #define SURFACE_GATHERER_H diff --git a/Nuake/src/Window.cpp b/Nuake/src/Window.cpp index 18a12b18..4d1ce0e1 100644 --- a/Nuake/src/Window.cpp +++ b/Nuake/src/Window.cpp @@ -240,6 +240,7 @@ void Window::Draw() Vector2 size = m_Framebuffer->GetSize(); cam->AspectRatio = size.x / size.y; + Renderer::BeginDraw(cam); { @@ -258,7 +259,6 @@ void Window::Draw() m_Framebuffer->Unbind(); } - Renderer::EndDraw(); }