From 8f4c5f8b08755b07a795ff9b59a241dfcfa13040 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 26 Jun 2021 13:13:52 -0400 Subject: [PATCH] Trigger physics update --- Editor/resources/Scenes/test.scene | 50 +++++++++--------- Editor/resources/Scripts/Cam.wren | 4 +- Editor/resources/Scripts/Player.wren | 4 ++ Editor/resources/Scripts/Scene.wren | 52 +++++++++---------- Editor/resources/Scripts/Trigger.wren | 29 +++++++++++ Editor/resources/Shaders/basic.shader | 2 +- Nuake/src/Core/MaterialManager.h | 3 ++ Nuake/src/Core/Physics/DynamicWorld.cpp | 5 ++ Nuake/src/Core/Physics/DynamicWorld.h | 4 ++ Nuake/src/Core/Physics/GhostObject.cpp | 51 ++++++++++++++++++ Nuake/src/Core/Physics/GhostObject.h | 27 ++++++++++ Nuake/src/Core/Physics/PhysicsManager.cpp | 9 ++++ Nuake/src/Core/Physics/PhysicsManager.h | 1 + Nuake/src/Core/Physics/Rigibody.h | 11 ++-- Nuake/src/Core/Physics/Rigidbody.cpp | 13 +++-- Nuake/src/Rendering/Camera.cpp | 1 - Nuake/src/Rendering/Mesh/Mesh.cpp | 6 +-- Nuake/src/Rendering/Mesh/Mesh.h | 2 +- Nuake/src/Rendering/Renderer.cpp | 18 ++++--- Nuake/src/Rendering/Shaders/Shader.cpp | 2 +- Nuake/src/Rendering/Textures/Material.cpp | 4 ++ Nuake/src/Rendering/Textures/Texture.cpp | 5 +- .../src/Scene/Components/BSPBrushComponent.h | 4 +- Nuake/src/Scene/Components/LightComponent.cpp | 37 ++++++++++--- Nuake/src/Scene/Components/LightComponent.h | 10 ++-- Nuake/src/Scene/Components/TriggerZone.h | 29 +++++++++++ Nuake/src/Scene/Scene.cpp | 30 +++++------ Nuake/src/Scene/Scene.h | 2 +- Nuake/src/Scene/Systems/PhysicsSystem.cpp | 38 ++++++++++++++ Nuake/src/Scene/Systems/QuakeMapBuilder.cpp | 40 +++++++++++--- Nuake/src/Scripting/Modules/SceneModule.h | 42 ++++++++++++++- Nuake/src/Window.cpp | 24 ++++----- 32 files changed, 431 insertions(+), 128 deletions(-) create mode 100644 Editor/resources/Scripts/Trigger.wren create mode 100644 Nuake/src/Core/Physics/GhostObject.cpp create mode 100644 Nuake/src/Core/Physics/GhostObject.h create mode 100644 Nuake/src/Scene/Components/TriggerZone.h diff --git a/Editor/resources/Scenes/test.scene b/Editor/resources/Scenes/test.scene index da8e1e5c..a3e44599 100644 --- a/Editor/resources/Scenes/test.scene +++ b/Editor/resources/Scenes/test.scene @@ -1,17 +1,13 @@ { "Entities": [ { - "CameraComponent": { - "CameraInstance": { - "AspectRatio": 1.774647831916809, - "Exposure": 1.0, - "Fov": 88.0, - "Speed": 1.0, - "m_Type": 1 - } + "CharacterControllerComponent": { + "Height": 1.2000000476837158, + "Mass": 2500.0, + "Radius": 0.20000000298023224 }, "NameComponent": { - "Name": "Camera" + "Name": "Player" }, "ParentComponent": { "HasParent": false @@ -28,15 +24,15 @@ "z": 1.0 }, "Translation": { - "x": -25.119064331054688, - "y": 19.491336822509766, - "z": 4.5285515785217285 + "x": 2.5405828952789307, + "y": 0.0, + "z": 0.0 }, "Type": "TransformComponent" }, "WrenScriptComponent": { - "Class": "CamScript", - "Script": "Scripts/Cam.wren" + "Class": "PlayerScript", + "Script": "Scripts/Player.wren" } }, { @@ -95,7 +91,7 @@ }, "QuakeMapComponent": { "HasCollisions": true, - "Path": "C:\\Dev\\Nuake\\Editor\\resources\\Maps\\texture_test.map" + "Path": "C:\\Dev\\Nuake\\Editor\\resources\\Maps\\debug.map" }, "TransformComponent": { "Rotation": { @@ -109,21 +105,25 @@ "z": 1.0 }, "Translation": { - "x": 9.199999809265137, - "y": -11.569494247436523, + "x": 0.0, + "y": 0.0, "z": 0.0 }, "Type": "TransformComponent" } }, { - "CharacterControllerComponent": { - "Height": 1.2000000476837158, - "Mass": 2500.0, - "Radius": 0.20000000298023224 + "CameraComponent": { + "CameraInstance": { + "AspectRatio": 1.774647831916809, + "Exposure": 1.0, + "Fov": 88.0, + "Speed": 1.0, + "m_Type": 1 + } }, "NameComponent": { - "Name": "Player" + "Name": "Camera" }, "ParentComponent": { "HasParent": false @@ -141,14 +141,14 @@ }, "Translation": { "x": -25.119064331054688, - "y": 18.649978637695313, + "y": 19.491336822509766, "z": 4.5285515785217285 }, "Type": "TransformComponent" }, "WrenScriptComponent": { - "Class": "PlayerScript", - "Script": "Scripts/Player.wren" + "Class": "CamScript", + "Script": "Scripts/Cam.wren" } } ], diff --git a/Editor/resources/Scripts/Cam.wren b/Editor/resources/Scripts/Cam.wren index 708d1267..afb3fee3 100644 --- a/Editor/resources/Scripts/Cam.wren +++ b/Editor/resources/Scripts/Cam.wren @@ -18,7 +18,7 @@ class CamScript is ScriptableEntity { _deltaTime = 0 - Input.HideMouse() + //Input.HideMouse() } init() {} @@ -63,7 +63,7 @@ class CamScript is ScriptableEntity { velocity = velocity.Sqrt() var amount = (Math.Sin(_deltaTime * (velocity * 0.01)) / 2 + 1) * _BobHeight - Engine.Log("BobAmount: %(amount)") + //Engine.Log("BobAmount: %(amount)") var pTransform = player.GetComponent("Transform") var pPos = pTransform.GetTranslation() diff --git a/Editor/resources/Scripts/Player.wren b/Editor/resources/Scripts/Player.wren index 2f05f4fb..3c4db303 100644 --- a/Editor/resources/Scripts/Player.wren +++ b/Editor/resources/Scripts/Player.wren @@ -26,6 +26,10 @@ class PlayerScript is ScriptableEntity { Engine.Log("Player init") } + update(ts) { + + } + fixedUpdate(ts) { this.CheckInput() diff --git a/Editor/resources/Scripts/Scene.wren b/Editor/resources/Scripts/Scene.wren index 89b7af0f..3acd7b66 100644 --- a/Editor/resources/Scripts/Scene.wren +++ b/Editor/resources/Scripts/Scene.wren @@ -28,7 +28,9 @@ class Scene { return TransformComponent.new(id) } else if (component == "Script") { return this.GetScript_(id) - } + } else if (component == "Trigger") { + return Trigger.new(id) + } } @@ -63,7 +65,8 @@ class Scene { foreign static IsCharacterControllerOnGround_(e) //foreign static IsOnGround_(e) - + foreign static TriggerGetOverlappingBodyCount_(e) + foreign static TriggerGetOverlappingBodies_(e) } class Entity { @@ -78,31 +81,6 @@ class Entity { GetComponent(component) { return Scene.EntityGetComponent(_entityId, component) } - - // Foreign engine functions -/* - // Transform - foreign static SetTranslation_(e, x, y, z) - foreign static SetRotation_(e, x, y, z) - foreign static SetScale_(e, x, y, z) - - // Character controller - foreign static SetVelocity(e, x, y, z) - foreign static SetStepHeight(e, x, y, z) - foreign static IsOnGround(e) - */ - // Light - // - /* - foreign static SetLightIsVolumetric_(e, bool) - foreign static SetLightSyncDirectionWithSky_(e, bool) - foreign static SetLightColor_(e, r, g, b) - - // Camera - foreign static SetCameraFov_(e, fov) - foreign static SetCameraType(e, type) - foreign static SetCameraDirection_(e, x, y, z) - */ } class TransformComponent { @@ -181,3 +159,23 @@ class Camera { } } + +class Trigger { + construct new(id) { + _entityId = id + } + + GetOverlappingBodyCount() { + return Scene.TriggerGetOverlappingBodyCount_(_entityId) + } + + GetOverlappingBodies() { + bodies = Scene.TriggerGetOverlappingBodies_(_entityId) + + entities = [] + for(b in bodies) { + entities.add(Entity.new(b)) + } + return entities + } +} \ No newline at end of file diff --git a/Editor/resources/Scripts/Trigger.wren b/Editor/resources/Scripts/Trigger.wren new file mode 100644 index 00000000..8aceac87 --- /dev/null +++ b/Editor/resources/Scripts/Trigger.wren @@ -0,0 +1,29 @@ +import "Scripts/ScriptableEntity" for ScriptableEntity +import "Scripts/Engine" for Engine +import "Scripts/Scene" for Scene, Trigger +import "Scripts/Math" for Vector3 +import "Scripts/Input" for Input +import "Scripts/Physics" for Physics, CollisionResult + +class TriggerScript is ScriptableEntity { + construct new() { + + } + + init() { + Engine.Log("Hello, I'm a trigger") + } + + update(ts) { + + } + fixedUpdate(ts) { + var trigger = this.GetComponent("Trigger") + + Engine.Log("Current overlap: %(trigger.GetOverlappingBodyCount())") + } + + exit() { + + } +} \ No newline at end of file diff --git a/Editor/resources/Shaders/basic.shader b/Editor/resources/Shaders/basic.shader index 9a42c83b..6beb329f 100644 --- a/Editor/resources/Shaders/basic.shader +++ b/Editor/resources/Shaders/basic.shader @@ -77,7 +77,7 @@ struct Light { // Debug uniform int u_ShowNormal; -const int MaxLight = 20; +const int MaxLight = 24; uniform int LightCount = 0; uniform Light Lights[MaxLight]; diff --git a/Nuake/src/Core/MaterialManager.h b/Nuake/src/Core/MaterialManager.h index cc41945a..975810b6 100644 --- a/Nuake/src/Core/MaterialManager.h +++ b/Nuake/src/Core/MaterialManager.h @@ -16,7 +16,10 @@ private: Material* ParseMaterialFile(const std::string path); void SaveMaterialFile(const std::string path, Material* material); bool IsMaterialLoaded(const std::string path); + + public: + std::string CurrentlyBoundedMaterial = ""; MaterialManager(); void LoadMaterials(); diff --git a/Nuake/src/Core/Physics/DynamicWorld.cpp b/Nuake/src/Core/Physics/DynamicWorld.cpp index 1954eb3d..87fdd7b7 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.cpp +++ b/Nuake/src/Core/Physics/DynamicWorld.cpp @@ -49,6 +49,11 @@ namespace Physics dynamicsWorld->addRigidBody(rb->GetBulletRigidbody()); } + void DynamicWorld::AddGhostbody(Ref gb) + { + dynamicsWorld->addCollisionObject(gb->GetBulletObject(), btBroadphaseProxy::KinematicFilter, btBroadphaseProxy::StaticFilter); + } + void DynamicWorld::AddCharacterController(Ref cc) { dynamicsWorld->addRigidBody(cc->m_Rigidbody); diff --git a/Nuake/src/Core/Physics/DynamicWorld.h b/Nuake/src/Core/Physics/DynamicWorld.h index 7fcb4bb5..04156baf 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.h +++ b/Nuake/src/Core/Physics/DynamicWorld.h @@ -6,6 +6,8 @@ #include "../Core/Core.h" #include #include "RaycastResult.h" + +#include #include "CharacterController.h" namespace Physics { class DynamicWorld { @@ -20,6 +22,8 @@ namespace Physics { void SetGravity(glm::vec3 g); void AddRigidbody(Ref rb); + void AddGhostbody(Ref gb); + void AddCharacterController(Ref < CharacterController> cc); RaycastResult Raycast(glm::vec3 from, glm::vec3 to); diff --git a/Nuake/src/Core/Physics/GhostObject.cpp b/Nuake/src/Core/Physics/GhostObject.cpp new file mode 100644 index 00000000..0324f1c4 --- /dev/null +++ b/Nuake/src/Core/Physics/GhostObject.cpp @@ -0,0 +1,51 @@ +#include "GhostObject.h" +#include + + +GhostObject::GhostObject(Vector3 position, Ref shape) +{ + m_OverlappingEntities = std::vector(); + m_BulletObject = new btGhostObject(); + + btTransform transform; + transform.setOrigin(btVector3(position.x, position.y, position.z)); + + m_BulletObject->setWorldTransform(transform); + m_BulletObject->setCollisionShape(shape->GetBulletShape()); +} + +int GhostObject::OverlappingCount() +{ + return m_BulletObject->getNumOverlappingObjects(); +} + +void GhostObject::ClearOverlappingList() +{ + m_OverlappingEntities.clear(); +} + +void GhostObject::SetEntityID(Entity ent) +{ + m_BulletObject->setUserIndex(ent.GetHandle()); +} + +void GhostObject::ScanOverlap() +{ + ClearOverlappingList(); + for (int i = 0; i < OverlappingCount(); i++) + { + Entity handle = Engine::GetCurrentScene()->GetEntity(m_BulletObject->getOverlappingObject(i)->getUserIndex()); + m_OverlappingEntities.push_back(handle); + } +} + +std::vector GhostObject::GetOverlappingEntities() +{ + return m_OverlappingEntities; +} + +// Internal use only. +btGhostObject* GhostObject::GetBulletObject() +{ + return m_BulletObject; +} \ No newline at end of file diff --git a/Nuake/src/Core/Physics/GhostObject.h b/Nuake/src/Core/Physics/GhostObject.h new file mode 100644 index 00000000..e8e440f8 --- /dev/null +++ b/Nuake/src/Core/Physics/GhostObject.h @@ -0,0 +1,27 @@ +#pragma once +#include "src/Core/Core.h" +#include "PhysicsShapes.h" + +#include +#include + +class btGhostObject; +class GhostObject { +private: + btGhostObject* m_BulletObject; + std::vector m_OverlappingEntities; + +public: + GhostObject(Vector3 position, Ref shape); + + int OverlappingCount(); + void ClearOverlappingList(); + void ScanOverlap(); + + void SetEntityID(Entity ent); + + std::vector GetOverlappingEntities(); + + // Internal use only. + btGhostObject* GetBulletObject(); +}; \ No newline at end of file diff --git a/Nuake/src/Core/Physics/PhysicsManager.cpp b/Nuake/src/Core/Physics/PhysicsManager.cpp index 68a5bced..9d74c228 100644 --- a/Nuake/src/Core/Physics/PhysicsManager.cpp +++ b/Nuake/src/Core/Physics/PhysicsManager.cpp @@ -8,6 +8,12 @@ void PhysicsManager::RegisterBody(Ref rb) { m_World->AddRigidbody(rb); } +void PhysicsManager::RegisterGhostBody(Ref rb) +{ + m_World->AddGhostbody(rb); + +} + void PhysicsManager::RegisterCharacterController(Ref cc) { m_World->AddCharacterController(cc); } @@ -42,5 +48,8 @@ void PhysicsManager::Init() { m_World = new Physics::DynamicWorld(); m_World->SetGravity(glm::vec3(0, -3, 0)); + + m_World->GetDynamicWorld()->getBroadphase()->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback()); + m_IsRunning = false; } \ No newline at end of file diff --git a/Nuake/src/Core/Physics/PhysicsManager.h b/Nuake/src/Core/Physics/PhysicsManager.h index 3e81df42..fc03b63a 100644 --- a/Nuake/src/Core/Physics/PhysicsManager.h +++ b/Nuake/src/Core/Physics/PhysicsManager.h @@ -47,5 +47,6 @@ public: RaycastResult Raycast(glm::vec3 from, glm::vec3 to); void RegisterBody(Ref rb); + void RegisterGhostBody(Ref rb); void RegisterCharacterController(Ref c); }; \ No newline at end of file diff --git a/Nuake/src/Core/Physics/Rigibody.h b/Nuake/src/Core/Physics/Rigibody.h index 0891bfdc..a22cc5b5 100644 --- a/Nuake/src/Core/Physics/Rigibody.h +++ b/Nuake/src/Core/Physics/Rigibody.h @@ -5,6 +5,7 @@ struct btTransform; struct btRigidBody; struct btVector3; +class Entity; namespace Physics { class RigidBody { private: @@ -12,22 +13,26 @@ namespace Physics { bool m_IsKinematic = false; glm::vec3 m_InitialVel; - btTransform* m_Transform; + btRigidBody* m_Rigidbody; Ref m_CollisionShape; public: + btTransform* m_Transform; float m_Mass; + RigidBody(); - RigidBody(glm::vec3 position); + RigidBody(glm::vec3 position, Entity handle); RigidBody(float mass, glm::vec3 position, Ref shape, glm::vec3 initialVel = glm::vec3(0, 0, 0)); btRigidBody* GetBulletRigidbody() const { return m_Rigidbody; } - + void UpdateTransform(btTransform t); glm::vec3 GetPosition() const; glm::vec3 GetRotation() const; + void SetEntityID(Entity ent); + void SetKinematic(bool value); bool IsKinematic() const { return m_IsKinematic; } diff --git a/Nuake/src/Core/Physics/Rigidbody.cpp b/Nuake/src/Core/Physics/Rigidbody.cpp index e8a8c1f0..bd43408e 100644 --- a/Nuake/src/Core/Physics/Rigidbody.cpp +++ b/Nuake/src/Core/Physics/Rigidbody.cpp @@ -3,6 +3,7 @@ #include #include "../Core.h" #include +#include namespace Physics { RigidBody::RigidBody() @@ -10,10 +11,9 @@ namespace Physics m_Transform = new btTransform(); m_Transform->setIdentity(); //m_Transform->setOrigin(btVector3(position.x, position.y, position.z)); - - } - RigidBody::RigidBody(glm::vec3 position) + + RigidBody::RigidBody(glm::vec3 position, Entity handle) { Ref shape = CreateRef(); m_CollisionShape = shape; @@ -38,6 +38,7 @@ namespace Physics btRigidBody::btRigidBodyConstructionInfo rbInfo(m_Mass, myMotionState, m_CollisionShape->GetBulletShape(), localInertia); m_Rigidbody = new btRigidBody(rbInfo); + m_Rigidbody->setUserIndex(handle.GetHandle()); } RigidBody::RigidBody(float mass, glm::vec3 position, Ref shape, glm::vec3 initialVel) @@ -63,7 +64,6 @@ namespace Physics btRigidBody::btRigidBodyConstructionInfo rbInfo(m_Mass, myMotionState, m_CollisionShape->GetBulletShape(), localInertia); m_Rigidbody = new btRigidBody(rbInfo); - } void RigidBody::SetShape(Ref shape) @@ -86,6 +86,11 @@ namespace Physics return glm::vec3(glm::degrees(x), glm::degrees(y), glm::degrees(z)); } + void RigidBody::SetEntityID(Entity ent) + { + m_Rigidbody->setUserIndex(ent.GetHandle()); + } + void RigidBody::SetKinematic(bool value) { if (value) // Kinematic bodies dont deactivate. diff --git a/Nuake/src/Rendering/Camera.cpp b/Nuake/src/Rendering/Camera.cpp index 0b599816..f16e0023 100644 --- a/Nuake/src/Rendering/Camera.cpp +++ b/Nuake/src/Rendering/Camera.cpp @@ -70,7 +70,6 @@ glm::mat4 Camera::GetTransform() { glm::mat4 tr = lookAt(Translation, Translation + cameraFront, cameraUp); return tr; - } glm::mat4 Camera::GetTransformRotation() diff --git a/Nuake/src/Rendering/Mesh/Mesh.cpp b/Nuake/src/Rendering/Mesh/Mesh.cpp index eb39254f..83de734f 100644 --- a/Nuake/src/Rendering/Mesh/Mesh.cpp +++ b/Nuake/src/Rendering/Mesh/Mesh.cpp @@ -56,10 +56,10 @@ void Mesh::setupMesh() glBindVertexArray(0); } -void Mesh::Draw() +void Mesh::Draw(bool drawMaterial) { - - m_Material->Bind(); + if(drawMaterial) + m_Material->Bind(); // draw mesh glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, m_Indices.size(), GL_UNSIGNED_INT, 0); diff --git a/Nuake/src/Rendering/Mesh/Mesh.h b/Nuake/src/Rendering/Mesh/Mesh.h index 2f2fbd6f..3be60955 100644 --- a/Nuake/src/Rendering/Mesh/Mesh.h +++ b/Nuake/src/Rendering/Mesh/Mesh.h @@ -20,7 +20,7 @@ public: Mesh(std::vector vertices, std::vector indices, Ref material); - void Draw(); + void Draw(bool bindMaterial = true); void DebugDraw(); private: unsigned int VAO, VBO, EBO; diff --git a/Nuake/src/Rendering/Renderer.cpp b/Nuake/src/Rendering/Renderer.cpp index 2f11853c..8f57cf7f 100644 --- a/Nuake/src/Rendering/Renderer.cpp +++ b/Nuake/src/Rendering/Renderer.cpp @@ -121,6 +121,8 @@ std::vector Renderer::m_Lights; void Renderer::RegisterLight(TransformComponent transform, LightComponent light, Ref cam) { + if (m_Lights.size() == 20) + return; m_Lights.push_back({ transform , light }); // What light idx is this? @@ -132,10 +134,14 @@ void Renderer::RegisterLight(TransformComponent transform, LightComponent light, //light.m_Framebuffer->GetTexture(GL_DEPTH_ATTACHMENT)->Bind(17); - light.m_Framebuffers[0]->GetTexture(GL_DEPTH_ATTACHMENT)->Bind(17); - light.m_Framebuffers[1]->GetTexture(GL_DEPTH_ATTACHMENT)->Bind(18); - light.m_Framebuffers[2]->GetTexture(GL_DEPTH_ATTACHMENT)->Bind(19); - light.m_Framebuffers[3]->GetTexture(GL_DEPTH_ATTACHMENT)->Bind(20); + if (light.CastShadows) + { + light.m_Framebuffers[0]->GetTexture(GL_DEPTH_ATTACHMENT)->Bind(17); + light.m_Framebuffers[1]->GetTexture(GL_DEPTH_ATTACHMENT)->Bind(18); + light.m_Framebuffers[2]->GetTexture(GL_DEPTH_ATTACHMENT)->Bind(19); + light.m_Framebuffers[3]->GetTexture(GL_DEPTH_ATTACHMENT)->Bind(20); + } + m_Shader->SetUniform1i ("LightCount", idx); m_Shader->SetUniform1i ("Lights[" + std::to_string(idx - 1) + "].Type" , light.Type); @@ -192,7 +198,7 @@ void Renderer::DrawCube(TransformComponent transform, glm::vec4 color) Ref window = Engine::GetCurrentWindow(); Ref fb = window->GetFrameBuffer(); - m_DebugShader->Bind(); + m_DebugShader->SetUniformMat4f("u_Model", transform.GetTransform()); m_DebugShader->SetUniform4f("u_Color", color.r, color.g, color.b, color.a); glBindVertexArray(CubeVAO); @@ -202,7 +208,7 @@ void Renderer::DrawCube(TransformComponent transform, glm::vec4 color) glPolygonMode(GL_FRONT, GL_LINE); glPolygonMode(GL_BACK, GL_LINE); - glLineWidth(8); + glLineWidth(4); glDrawArrays(GL_TRIANGLES, 0, 36); // Turn off wireframe mode diff --git a/Nuake/src/Rendering/Shaders/Shader.cpp b/Nuake/src/Rendering/Shaders/Shader.cpp index 8687c554..d2f09a85 100644 --- a/Nuake/src/Rendering/Shaders/Shader.cpp +++ b/Nuake/src/Rendering/Shaders/Shader.cpp @@ -103,7 +103,7 @@ unsigned int Shader::Compile(unsigned int type) // m alloc. casted as a pointer. char* message = (char*)malloc(length * sizeof(char)); - glGetShaderInfoLog(id, length, &length, message); + //glGetShaderInfoLog(id, length, &length, message); std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "Fragment") << " shader!" << std::endl; diff --git a/Nuake/src/Rendering/Textures/Material.cpp b/Nuake/src/Rendering/Textures/Material.cpp index 0f3e175c..6a79eb0f 100644 --- a/Nuake/src/Rendering/Textures/Material.cpp +++ b/Nuake/src/Rendering/Textures/Material.cpp @@ -85,6 +85,10 @@ Material::~Material() {} void Material::Bind() { + if (MaterialManager::Get()->CurrentlyBoundedMaterial == m_Name) + return; + + MaterialManager::Get()->CurrentlyBoundedMaterial = m_Name; glBindBuffer(GL_UNIFORM_BUFFER, UBO); glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(UBOStructure), &data); diff --git a/Nuake/src/Rendering/Textures/Texture.cpp b/Nuake/src/Rendering/Textures/Texture.cpp index f4cc2572..1410c828 100644 --- a/Nuake/src/Rendering/Textures/Texture.cpp +++ b/Nuake/src/Rendering/Textures/Texture.cpp @@ -45,10 +45,9 @@ Texture::Texture(glm::vec2 size, GLenum format) glTexImage2D(GL_TEXTURE_2D, 0, format, size.x, size.y, 0, format, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glGenerateMipmap(GL_TEXTURE_2D); + //glGenerateMipmap(GL_TEXTURE_2D); } - Texture::Texture(glm::vec2 size, msdfgen::BitmapConstRef& bitmap, bool t) { m_Width = size.x; @@ -86,7 +85,6 @@ void Texture::AttachToFramebuffer(GLenum attachment) glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, attachment, GL_TEXTURE_2D, m_RendererId, 0); } - void Texture::Bind(unsigned int slot) const { glActiveTexture(GL_TEXTURE0 + slot); glBindTexture(GL_TEXTURE_2D, m_RendererId); @@ -95,4 +93,3 @@ void Texture::Bind(unsigned int slot) const { void Texture::Unbind() const { glBindTexture(GL_TEXTURE_2D, 0); } - diff --git a/Nuake/src/Scene/Components/BSPBrushComponent.h b/Nuake/src/Scene/Components/BSPBrushComponent.h index 85a521dc..a6086b0e 100644 --- a/Nuake/src/Scene/Components/BSPBrushComponent.h +++ b/Nuake/src/Scene/Components/BSPBrushComponent.h @@ -4,12 +4,13 @@ #include "../Rendering/Textures/Material.h" #include "../Rendering/Mesh/Mesh.h" +#include class BSPBrushComponent { public: std::vector> Meshes; std::vector< Ref> Materials; - + std::vector> Rigidbody; bool IsSolid = true; bool IsTrigger = false; bool IsTransparent = false; @@ -17,5 +18,6 @@ public: BSPBrushComponent() { Meshes = std::vector>(); Materials = std::vector>(); + Rigidbody = std::vector>(); } }; \ No newline at end of file diff --git a/Nuake/src/Scene/Components/LightComponent.cpp b/Nuake/src/Scene/Components/LightComponent.cpp index f3d471a9..c917e59b 100644 --- a/Nuake/src/Scene/Components/LightComponent.cpp +++ b/Nuake/src/Scene/Components/LightComponent.cpp @@ -18,14 +18,34 @@ LightComponent::LightComponent() //mCascadeSplitDepth = std::vector(); //mCascadeSplits = std::vector(); // Framebuffer used for shadow mapping. - for (int i = 0; i < 4; i++) - { - m_Framebuffers[i] = CreateRef(false, glm::vec2(4096, 4096)); - m_Framebuffers[i]->SetTexture(CreateRef(glm::vec2(4096, 4096), GL_DEPTH_COMPONENT), GL_DEPTH_ATTACHMENT); - } + + //for (int i = 0; i < 4; i++) + //{ + // m_Framebuffers[i] = CreateRef(false, glm::vec2(4096, 4096)); + // m_Framebuffers[i]->SetTexture(CreateRef(glm::vec2(4096, 4096), GL_DEPTH_COMPONENT), GL_DEPTH_ATTACHMENT); + //} } +void LightComponent::SetCastShadows(bool toggle) +{ + CastShadows = toggle; + if (CastShadows) + { + for (int i = 0; i < 4; i++) + { + m_Framebuffers[i] = CreateRef(false, glm::vec2(4096, 4096)); + m_Framebuffers[i]->SetTexture(CreateRef(glm::vec2(4096, 4096), GL_DEPTH_COMPONENT), GL_DEPTH_ATTACHMENT); + } + } + else { + for (int i = 0; i < 4; i++) + { + m_Framebuffers[i] = nullptr; + } + } +} + glm::mat4 LightComponent::GetProjection() { return glm::ortho(-25.0f, 25.0f, -25.0f, 25.0f, -25.0f, 25.0f); @@ -86,7 +106,12 @@ void LightComponent::DrawEditor() { ImGui::TextColored(ImGui::GetStyleColorVec4(1), "Light properties"); ImGui::ColorEdit4("Light Color", &Color.r); ImGui::SliderFloat("Strength", &Strength, 0.0f, 50.0f); - + bool before = CastShadows; + ImGui::Checkbox("Cast shadows", &CastShadows); + + if (CastShadows && before == false) + SetCastShadows(true); + const char* types[] = { "Directional", "Point", "Spot" }; static const char* current_item = types[Type]; diff --git a/Nuake/src/Scene/Components/LightComponent.h b/Nuake/src/Scene/Components/LightComponent.h index fafdf112..53005c8f 100644 --- a/Nuake/src/Scene/Components/LightComponent.h +++ b/Nuake/src/Scene/Components/LightComponent.h @@ -15,8 +15,6 @@ enum LightType { class LightComponent { public: - glm::vec2 yes = glm::vec2(2, 2); - LightType Type = Point; glm::vec3 Direction = glm::vec3(0, -1, 0); glm::vec3 Color; @@ -25,7 +23,7 @@ public: bool SyncDirectionWithSky = false; Ref m_Framebuffer; - bool CastShadows = true; + bool CastShadows = false; float Attenuation = 0.0f; float LinearAttenuation = 0.0f; float QuadraticAttenuation = 0.0f; @@ -36,6 +34,8 @@ public: LightComponent(); + void SetCastShadows(bool toggle); + glm::mat4 GetProjection(); glm::mat4 GetLightTransform(); @@ -191,8 +191,8 @@ public: Strength = j["Strength"]; if (j.contains("SyncDirectionWithSky")) SyncDirectionWithSky = j["SyncDirectionWithSky"]; - if (j.contains("CastShadows")) - SyncDirectionWithSky = j["CastShadows"]; + if (j.contains("CastShadows")) + SetCastShadows(j["CastShadows"]); if (j.contains("Direction")) { float x = j["Direction"]["x"]; diff --git a/Nuake/src/Scene/Components/TriggerZone.h b/Nuake/src/Scene/Components/TriggerZone.h new file mode 100644 index 00000000..66df8248 --- /dev/null +++ b/Nuake/src/Scene/Components/TriggerZone.h @@ -0,0 +1,29 @@ +#pragma once +#include "src/Core//Physics/GhostObject.h" +#include + +class TriggerZone { +public: + Ref GhostObject; + + bool Enabled = true; + + TriggerZone() { + + } + + int GetOverLappingCount() + { + //if (!Enabled) return 0; + + return GhostObject->OverlappingCount(); + } + + std::vector GetOverlappingBodies() + { + if (!Enabled) + return std::vector(); + + return GhostObject->GetOverlappingEntities(); + } +}; \ No newline at end of file diff --git a/Nuake/src/Scene/Scene.cpp b/Nuake/src/Scene/Scene.cpp index 8d28e40d..aa1e0667 100644 --- a/Nuake/src/Scene/Scene.cpp +++ b/Nuake/src/Scene/Scene.cpp @@ -60,6 +60,11 @@ bool Scene::SetName(std::string& newName) return true; } +Entity Scene::GetEntity(int handle) +{ + return Entity((entt::entity)handle, this); +} + void Scene::OnInit() { for (auto& system : m_Systems) @@ -136,46 +141,38 @@ void Scene::DrawShadows() } glm::mat4 perspective = cam->GetPerspective(); - + Renderer::m_ShadowmapShader->Bind(); for (auto l : view) { auto [lightTransform, light] = view.get(l); - if (light.Type != LightType::Directional) + if (light.Type != LightType::Directional || !light.CastShadows) continue; light.CalculateViewProjection(cam->GetTransform(), cam->GetPerspective()); - light.BeginDrawShadow(); for (int i = 0; i < 4; i++) { light.m_Framebuffers[i]->Bind(); - + Renderer::m_ShadowmapShader->SetUniformMat4f("lightSpaceMatrix", light.mViewProjections[i]); for (auto e : modelView) { auto [transform, model] = modelView.get(e); - glm::vec3 pos = lightTransform.Translation; - glm::mat4 lightView = glm::lookAt(pos, pos - light.GetDirection(), glm::vec3(0.0f, 1.0f, 0.0f)); - Renderer::m_ShadowmapShader->SetUniformMat4f("lightSpaceMatrix", light.mViewProjections[i]); Renderer::m_ShadowmapShader->SetUniformMat4f("model", transform.GetTransform()); - model.Draw(); } - auto quakeView = m_Registry.view(); + Renderer::m_ShadowmapShader->SetUniformMat4f("lightSpaceMatrix", light.mViewProjections[i]); + auto quakeView = m_Registry.view(); for (auto e : quakeView) { - auto [transform, model, parent] = quakeView.get(e); - Renderer::m_ShadowmapShader->SetUniformMat4f("lightSpaceMatrix", light.mViewProjections[i]); + auto [transform, model] = quakeView.get(e); Renderer::m_ShadowmapShader->SetUniformMat4f("model", transform.GetTransform()); for (auto& e : model.Meshes) { - e->Draw(); + e->Draw(false); } } - - light.m_Framebuffers[i]->Unbind(); } - light.EndDrawShadow(); } } @@ -321,12 +318,13 @@ void Scene::EditorDraw() } } + Renderer::m_DebugShader->Bind(); auto boxCollider = m_Registry.view(); for (auto e : boxCollider) { auto [transform, box, parent] = boxCollider.get(e); - 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()); TransformComponent t = transform; diff --git a/Nuake/src/Scene/Scene.h b/Nuake/src/Scene/Scene.h index 549de6cc..06c5edcd 100644 --- a/Nuake/src/Scene/Scene.h +++ b/Nuake/src/Scene/Scene.h @@ -38,7 +38,7 @@ public: std::string GetName(); bool SetName(std::string& newName); - + Entity GetEntity(int handle); void Init(); void OnInit(); void OnExit(); diff --git a/Nuake/src/Scene/Systems/PhysicsSystem.cpp b/Nuake/src/Scene/Systems/PhysicsSystem.cpp index a4f0b5de..1dcd0da3 100644 --- a/Nuake/src/Scene/Systems/PhysicsSystem.cpp +++ b/Nuake/src/Scene/Systems/PhysicsSystem.cpp @@ -8,6 +8,9 @@ #include #include #include +#include + + PhysicsSystem::PhysicsSystem(Scene* scene) { m_Scene = scene; @@ -38,6 +41,8 @@ void PhysicsSystem::Init() } } + + // character controllers auto ccview = m_Scene->m_Registry.view(); for (auto e : ccview) @@ -61,9 +66,26 @@ void PhysicsSystem::Init() { Ref meshShape = CreateRef(m); Ref btRigidbody = CreateRef(0.0f, transform.GlobalTranslation, meshShape); + btRigidbody->SetEntityID(Entity{ e, m_Scene }); + brush.Rigidbody.push_back(btRigidbody); PhysicsManager::Get()->RegisterBody(btRigidbody); } } + + } + + auto bspTriggerView = m_Scene->m_Registry.view(); + for (auto e : bspTriggerView) { + auto [transform, brush, trigger] = bspTriggerView.get(e); + + for (auto m : brush.Meshes) + { + Ref meshShape = CreateRef(m); + Ref ghostBody = CreateRef(transform.GlobalTranslation, meshShape); + trigger.GhostObject = ghostBody; + ghostBody->SetEntityID(Entity{ e, m_Scene }); + PhysicsManager::Get()->RegisterGhostBody(ghostBody); + } } } @@ -72,6 +94,22 @@ void PhysicsSystem::Update(Timestep ts) // Update rigidbodies PhysicsManager::Get()->Step(ts); + auto brushes = m_Scene->m_Registry.view(); + for (auto e : brushes) { + auto [transform, brush] = brushes.get(e); + + for (auto& r : brush.Rigidbody) { + r->m_Transform->setOrigin(btVector3(transform.GlobalTranslation.x, transform.GlobalTranslation.y, transform.GlobalTranslation.z)); + r->UpdateTransform(*r->m_Transform); + } + } + + auto bspTriggerView = m_Scene->m_Registry.view(); + for (auto e : bspTriggerView) { + auto [transform, brush, trigger] = bspTriggerView.get(e); + Logger::Log(std::to_string(trigger.GetOverLappingCount())); + } + auto physicGroup = m_Scene->m_Registry.view(); for (auto e : physicGroup) { auto [transform, rb] = physicGroup.get(e); diff --git a/Nuake/src/Scene/Systems/QuakeMapBuilder.cpp b/Nuake/src/Scene/Systems/QuakeMapBuilder.cpp index 499f88cf..bc890279 100644 --- a/Nuake/src/Scene/Systems/QuakeMapBuilder.cpp +++ b/Nuake/src/Scene/Systems/QuakeMapBuilder.cpp @@ -6,6 +6,7 @@ #include #include #include + extern "C" { #include "libmap/h/map_parser.h" #include @@ -16,6 +17,8 @@ extern "C" { #include #include #include +#include +#include std::vector split(const std::string& s, char delim) { @@ -56,6 +59,11 @@ void QuakeMapBuilder::BuildQuakeMap(Entity& ent, bool Collisions) entity_geometry* entity_geo_inst = &entity_geo[e]; Entity newEntity = m_Scene->CreateEntity("Brush " + std::to_string(e) ); + + if (entity_inst->spawn_type == entity_spawn_type::EST_GROUP) + newEntity.GetComponent().Name = "Group " + std::to_string(e); + + bool isTrigger = false; ent.AddChild(newEntity); for (int i = 0; i < entity_inst->property_count; i++) { property* prop = &(entity_inst->properties)[i]; @@ -74,24 +82,42 @@ void QuakeMapBuilder::BuildQuakeMap(Entity& ent, bool Collisions) newEntity.GetComponent().Translation = position; } - if (key == "classname") { - if (value == "light") { + if (key == "classname") + { + if (value == "light") + { newEntity.AddComponent(); + newEntity.GetComponent().Name = "Light " + std::to_string(i); + } + else if (value == "trigger") + { + //newEntity.AddComponent(); + isTrigger = true; + + newEntity.GetComponent().Name = "Trigger " + std::to_string(i); } } } 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(); + + Entity brushEntity = m_Scene->CreateEntity("Brush " + std::to_string(e)); + TransformComponent& transformComponent = brushEntity.GetComponent(); + BSPBrushComponent& bsp = brushEntity.AddComponent(); + + newEntity.AddChild(brushEntity); + + if (isTrigger) { + bsp.IsTrigger = true; + bsp.IsSolid = false; + brushEntity.AddComponent(); + } + transformComponent.Translation = Vector3(brush_inst->center.y * (1.0f / 64), brush_inst->center.z * (1.0f / 64), brush_inst->center.x * (1.0f / 64)); diff --git a/Nuake/src/Scripting/Modules/SceneModule.h b/Nuake/src/Scripting/Modules/SceneModule.h index b08d906c..cdfe2cb6 100644 --- a/Nuake/src/Scripting/Modules/SceneModule.h +++ b/Nuake/src/Scripting/Modules/SceneModule.h @@ -14,11 +14,10 @@ #include #include #include +#include namespace ScriptAPI { - - class SceneModule : public ScriptModule { std::string GetModuleName() override @@ -30,16 +29,23 @@ namespace ScriptAPI { RegisterMethod("GetEntityID(_)", (void*)GetEntity); RegisterMethod("EntityHasComponent(_,_)", (void*)EntityHasComponent); + RegisterMethod("GetTranslation_(_)", (void*)GetTranslation); RegisterMethod("SetTranslation_(_,_,_,_)", (void*)SetTranslation); RegisterMethod("SetLightIntensity_(_,_)", (void*)SetLightIntensity); RegisterMethod("GetLightIntensity_(_)", (void*)GetLightIntensity); + RegisterMethod("GetScript_(_)", (void*)GetScript); + RegisterMethod("SetCameraDirection_(_,_,_,_)", (void*)SetCameraDirection); RegisterMethod("GetCameraDirection_(_)", (void*)GetCameraDirection); RegisterMethod("GetCameraRight_(_)", (void*)GetCameraRight); + RegisterMethod("MoveAndSlide_(_,_,_,_)", (void*)MoveAndSlide); RegisterMethod("IsCharacterControllerOnGround_(_)", (void*)IsCharacterControllerOnGround); + + RegisterMethod("TriggerGetOverlappingBodyCount_(_)", (void*)TriggerGetOverlappingBodyCount); + RegisterMethod("TriggerGetOverlappingBodies_(_)", (void*)TriggerGetOverlappingBodies); } static void GetEntity(WrenVM* vm) @@ -227,5 +233,37 @@ namespace ScriptAPI transform.Translation = Vector3(x, y, z); } + static void TriggerGetOverlappingBodyCount(WrenVM* vm) + { + int handle = wrenGetSlotDouble(vm, 1); + Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get()); + + TriggerZone trigger = ent.GetComponent(); + + int count = trigger.GetOverLappingCount(); + + wrenSetSlotDouble(vm, 0, count); + } + + static void TriggerGetOverlappingBodies(WrenVM* vm) + { + int handle = wrenGetSlotDouble(vm, 1); + Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get()); + + TriggerZone trigger = ent.GetComponent(); + + std::vector entities = trigger.GetOverlappingBodies(); + + wrenEnsureSlots(vm, entities.size()); + + int idx = 1; + for (auto& e : entities) + { + wrenSetSlotDouble(vm, idx, e.GetHandle()); + wrenInsertInList(vm, 0, -1, idx); + idx++; + } + wrenSetSlotNewList(vm, 0); + } }; } \ No newline at end of file diff --git a/Nuake/src/Window.cpp b/Nuake/src/Window.cpp index 4d1ce0e1..fd3b2c89 100644 --- a/Nuake/src/Window.cpp +++ b/Nuake/src/Window.cpp @@ -242,23 +242,23 @@ void Window::Draw() cam->AspectRatio = size.x / size.y; Renderer::BeginDraw(cam); - { + - //glCullFace(GL_FRONT); - //glCullFace(GL_BACK); - m_Scene->DrawShadows(); + //glCullFace(GL_FRONT); + //glCullFace(GL_BACK); + m_Scene->DrawShadows(); - m_Framebuffer->Bind(); + m_Framebuffer->Bind(); - if (Engine::IsPlayMode) - m_Scene->Draw(); - else - m_Scene->EditorDraw(); + if (Engine::IsPlayMode) + m_Scene->Draw(); + else + m_Scene->EditorDraw(); - m_Scene->DrawInterface(m_Framebuffer->GetSize()); + m_Scene->DrawInterface(m_Framebuffer->GetSize()); - m_Framebuffer->Unbind(); - } + m_Framebuffer->Unbind(); + Renderer::EndDraw(); }