diff --git a/Editor/src/Misc/GizmoDrawer.cpp b/Editor/src/Misc/GizmoDrawer.cpp index 3a9524ee..31d5987a 100644 --- a/Editor/src/Misc/GizmoDrawer.cpp +++ b/Editor/src/Misc/GizmoDrawer.cpp @@ -13,6 +13,10 @@ #include #include "src/Scene/Components/CapsuleColliderComponent.h" +#include +#include +#include + GizmoDrawer::GizmoDrawer() { @@ -29,7 +33,46 @@ GizmoDrawer::GizmoDrawer() mAxisLineBuffer->AddBuffer(*mAxisLineVertexBuffer, *vblayout); GenerateSphereGizmo(); - GenerateCapsuleGizmo(); + + // Box + const Color cubeColor = Color(1, 0, 0, 1); + std::vector mBoxVertices = + { + LineVertex{Vector3(-1.f, -1.f, -1.f), cubeColor}, + LineVertex{Vector3(1.0f, -1.f, -1.f), cubeColor}, + LineVertex{Vector3(-1.f, -1.f, 1.f), cubeColor}, + LineVertex{Vector3(1.0f, -1.f, 1.f), cubeColor}, + LineVertex{Vector3(-1.f, 1.f, -1.f), cubeColor}, + LineVertex{Vector3(1.0f, 1.f, -1.f), cubeColor}, + LineVertex{Vector3(-1.f, 1.f, 1.f), cubeColor}, + LineVertex{Vector3(1.0f, 1.f, 1.f), cubeColor}, + + LineVertex{Vector3(-1.f, -1.f, -1.f), cubeColor}, + LineVertex{Vector3(-1.0f, -1.f, 1.f), cubeColor}, + LineVertex{Vector3(1.f, -1.f, -1.f), cubeColor}, + LineVertex{Vector3(1.0f, -1.f, 1.f), cubeColor}, + LineVertex{Vector3(-1.f, 1.f, -1.f), cubeColor}, + LineVertex{Vector3(-1.0f, 1.f, 1.f), cubeColor}, + LineVertex{Vector3(1.f, 1.f, -1.f), cubeColor}, + LineVertex{Vector3(1.0f, 1.f, 1.f), cubeColor}, + + LineVertex{Vector3(-1.0f, -1.0f, -1.f), cubeColor}, + LineVertex{Vector3(-1.f, 1.0f, -1.f), cubeColor}, + LineVertex{Vector3(1.0f, -1.0f, -1.f), cubeColor}, + LineVertex{Vector3(1.f, 1.0f, -1.f), cubeColor}, + LineVertex{Vector3(-1.0f, -1.0f, 1.f), cubeColor}, + LineVertex{Vector3(-1.f, 1.0f, 1.f), cubeColor}, + LineVertex{Vector3(1.0f, -1.0f, 1.f), cubeColor}, + LineVertex{Vector3(1.0f, 1.0f, 1.f), cubeColor} + }; + + mBoxBuffer = CreateRef(); + mBoxBuffer->Bind(); + + mBoxVertexBuffer = CreateRef(mBoxVertices.data(), mBoxVertices.size() * sizeof(Nuake::LineVertex)); + + mBoxBuffer->AddBuffer(*mBoxVertexBuffer, *vblayout); + // Load gizmos ModelLoader loader; @@ -66,8 +109,8 @@ void GizmoDrawer::GenerateSphereGizmo() vert1 = Vector3(x, 0, z); vert2 = Vector3(x2, 0, z2); } - circleVertices.push_back(LineVertex{ vert1, Color(1.0, 0, 0.7, 1.0) }); - circleVertices.push_back(LineVertex{ vert2, Color(1.0, 0, 0.7, 1.0) }); + circleVertices.push_back(LineVertex{ vert1, Color(1.0, 0, 0.0, 1.0) }); + circleVertices.push_back(LineVertex{ vert2, Color(1.0, 0, 0.0, 1.0) }); } mCircleBuffer = CreateRef(); @@ -81,100 +124,6 @@ void GizmoDrawer::GenerateSphereGizmo() mCircleBuffer->AddBuffer(*mCircleVertexBuffer, *vblayout); } -void GizmoDrawer::GenerateCapsuleGizmo() -{ - float height = 1.5f; - float radius = 0.5f; - - float halfHeight = height / 2.0f; - - float bottomCircleHeight = -halfHeight + radius; - float topCircleHeight = halfHeight - radius; - - // Generate circles - const float subDivision = 32.0f; - constexpr const float pi = glm::pi() * 4.0; - float increment = pi / subDivision; - for (int i = 0; i < subDivision * 2.0; i++) - { - float current = increment * (i); - float x = glm::cos(current) * radius; - float z = glm::sin(current) * radius; - - current = increment * (i + 1); - float x2 = glm::cos(current) * radius; - float z2 = glm::sin(current) * radius; - - Vector3 vert1, vert2; - if (i < subDivision) - { - vert1 = Vector3(x, topCircleHeight, z); - vert2 = Vector3(x2, topCircleHeight, z2); - } - else - { - vert1 = Vector3(x, bottomCircleHeight, z); - vert2 = Vector3(x2, bottomCircleHeight, z2); - } - - capsuleVertices.push_back(LineVertex{ vert1, Color(1.0, 0, 0.7, 1.0) }); - capsuleVertices.push_back(LineVertex{ vert2, Color(1.0, 0, 0.7, 1.0) }); - } - - for (int i = 0; i < subDivision * 2.0; i++) - { - float current = increment * (i); - float x = glm::cos(current) * radius; - float z = glm::sin(current) * radius; - - current = increment * (i + 1); - float x2 = glm::cos(current) * radius; - float z2 = glm::sin(current) * radius; - - float heightOffset = topCircleHeight; - if (z < 0.0) - { - heightOffset = bottomCircleHeight; - } - - Vector3 vert1, vert2; - if (i < subDivision) - { - vert1 = Vector3(x, z + heightOffset, 0); - vert2 = Vector3(x2, z2 + heightOffset, 0); - } - else - { - vert1 = Vector3(0, z + heightOffset, x); - vert2 = Vector3(0, z2 + heightOffset, x2 ); - } - - capsuleVertices.push_back(LineVertex{ vert1, Color(1.0, 0, 0.7, 1.0) }); - capsuleVertices.push_back(LineVertex{ vert2, Color(1.0, 0, 0.7, 1.0) }); - } - - capsuleVertices.push_back(LineVertex{ Vector3(radius, bottomCircleHeight, 0), Color(1.0, 0, 0.7, 1.0)}); - capsuleVertices.push_back(LineVertex{ Vector3(radius, topCircleHeight, 0), Color(1.0, 0, 0.7, 1.0) }); - - capsuleVertices.push_back(LineVertex{ Vector3(-radius, bottomCircleHeight, 0), Color(1.0, 0, 0.7, 1.0) }); - capsuleVertices.push_back(LineVertex{ Vector3(-radius, topCircleHeight, 0), Color(1.0, 0, 0.7, 1.0) }); - - capsuleVertices.push_back(LineVertex{ Vector3(0, bottomCircleHeight, radius), Color(1.0, 0, 0.7, 1.0) }); - capsuleVertices.push_back(LineVertex{ Vector3(0, topCircleHeight, radius), Color(1.0, 0, 0.7, 1.0) }); - - capsuleVertices.push_back(LineVertex{ Vector3(0, bottomCircleHeight, -radius), Color(1.0, 0, 0.7, 1.0) }); - capsuleVertices.push_back(LineVertex{ Vector3(0, topCircleHeight, -radius), Color(1.0, 0, 0.7, 1.0) }); - - mCapsuleBuffer = CreateRef(); - mCapsuleBuffer->Bind(); - - mCapsuleVertexBuffer = CreateRef(capsuleVertices.data(), capsuleVertices.size() * sizeof(Nuake::LineVertex)); - auto vblayout = CreateRef(); - vblayout->Push(3); - vblayout->Push(4); - - mCapsuleBuffer->AddBuffer(*mCapsuleVertexBuffer, *vblayout); -} void GizmoDrawer::DrawGizmos(Ref scene) { @@ -191,6 +140,20 @@ void GizmoDrawer::DrawGizmos(Ref scene) Nuake::RenderCommand::DrawLines(0, 6); } + + glLineWidth(2.0f); + auto boxColliderView = scene->m_Registry.view(); + for (auto e : boxColliderView) + { + auto [transform, box] = scene->m_Registry.get(e); + mLineShader->Bind(); + mLineShader->SetUniformMat4f("u_View", glm::scale(glm::translate(scene->m_EditorCamera->GetTransform(), transform.Translation), box.Size)); + mLineShader->SetUniformMat4f("u_Projection", scene->m_EditorCamera->GetPerspective()); + + mBoxBuffer->Bind(); + Nuake::RenderCommand::DrawLines(0, 26); + } + auto sphereColliderView = scene->m_Registry.view(); for (auto e : sphereColliderView) { @@ -206,15 +169,80 @@ void GizmoDrawer::DrawGizmos(Ref scene) auto capsuleColliderView = scene->m_Registry.view(); for (auto e : capsuleColliderView) { - auto [transform, capsule] = scene->m_Registry.get(e); + auto& [transform, capsule] = scene->m_Registry.get(e); + + const auto entityId = (uint32_t)e; + if (_CapsuleEntity.find(entityId) == _CapsuleEntity.end()) + { + _CapsuleEntity[entityId] = CreateScope(); + } + _CapsuleEntity[entityId]->UpdateShape(capsule.Radius, capsule.Height); + mLineShader->Bind(); mLineShader->SetUniformMat4f("u_View", glm::translate(scene->m_EditorCamera->GetTransform(), transform.Translation)); mLineShader->SetUniformMat4f("u_Projection", scene->m_EditorCamera->GetPerspective()); - mCapsuleBuffer->Bind(); + _CapsuleEntity[entityId]->Bind(); Nuake::RenderCommand::DrawLines(0, 264); } + auto cylinderColliderView = scene->m_Registry.view(); + for (auto e : cylinderColliderView) + { + auto& [transform, cylinder] = scene->m_Registry.get(e); + + const auto entityId = (uint32_t)e; + if (_CylinderEntity.find(entityId) == _CylinderEntity.end()) + { + _CylinderEntity[entityId] = CreateScope(); + } + _CylinderEntity[entityId]->UpdateShape(cylinder.Radius, cylinder.Height); + + mLineShader->Bind(); + mLineShader->SetUniformMat4f("u_View", glm::translate(scene->m_EditorCamera->GetTransform(), transform.Translation)); + mLineShader->SetUniformMat4f("u_Projection", scene->m_EditorCamera->GetPerspective()); + + _CylinderEntity[entityId]->Bind(); + Nuake::RenderCommand::DrawLines(0, 264); + } + + auto meshColliderView = scene->m_Registry.view(); + for (auto e : meshColliderView) + { + auto& [transform, mesh, model] = scene->m_Registry.get(e); + + // Component has no mesh set. + if (!model.ModelResource) + { + continue; + } + + auto& resource = model.ModelResource; + + auto meshes = resource->GetMeshes(); + + if (mesh.SubMesh >= meshes.size()) + { + continue; + } + + mLineShader->Bind(); + + const Vector3& localTranslate = transform.GetGlobalPosition(); + const Quat& localRot = glm::normalize(transform.GetGlobalRotation()); + const Matrix4& translationMatrix = glm::translate(Matrix4(1.0f), localTranslate); + const Matrix4& rotationMatrix = glm::mat4_cast(localRot); + const Matrix4& newLocalTransform = translationMatrix * rotationMatrix; + + mLineShader->SetUniformMat4f("u_View", glm::translate(scene->m_EditorCamera->GetTransform(), transform.Translation) * rotationMatrix); + mLineShader->SetUniformMat4f("u_Projection", scene->m_EditorCamera->GetPerspective()); + + meshes[mesh.SubMesh]->Bind(); + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + meshes[mesh.SubMesh]->Draw(nullptr, false); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + } + auto flatShader = ShaderManager::GetShader("resources/Shaders/flat.shader"); flatShader->Bind(); flatShader->SetUniformMat4f("u_View", scene->m_EditorCamera->GetTransform()); @@ -225,6 +253,7 @@ void GizmoDrawer::DrawGizmos(Ref scene) RenderCommand::Enable(RendererEnum::FACE_CULL); glCullFace(GL_BACK); + glLineWidth(1.0f); RenderList renderList; // Camera diff --git a/Editor/src/Misc/GizmoDrawer.h b/Editor/src/Misc/GizmoDrawer.h index c7fa213a..e16faafa 100644 --- a/Editor/src/Misc/GizmoDrawer.h +++ b/Editor/src/Misc/GizmoDrawer.h @@ -6,6 +6,10 @@ #include #include #include +#include + +#include "Gizmos/CapsuleGizmo.h" +#include "Gizmos/CylinderGizmo.h" using namespace Nuake; @@ -14,6 +18,9 @@ class GizmoDrawer private: std::map> _gizmos; + std::map> _CapsuleEntity; + std::map> _CylinderEntity; + const std::vector vertices { LineVertex {{10000.f, 0.0f, 0.0f}, {1.f, 0.f, 0.f, 1.f}}, @@ -27,9 +34,9 @@ private: Ref mCircleBuffer; Ref mCircleVertexBuffer; - std::vector capsuleVertices; - Ref mCapsuleBuffer; - Ref mCapsuleVertexBuffer; + std::vector mBoxVertices; + Ref mBoxBuffer; + Ref mBoxVertexBuffer; Ref mAxisLineBuffer; Ref mAxisLineVertexBuffer; @@ -37,7 +44,6 @@ private: Shader* mLineShader; void GenerateSphereGizmo(); - void GenerateCapsuleGizmo(); public: GizmoDrawer(); ~GizmoDrawer() = default; diff --git a/Editor/src/Misc/Gizmos/CapsuleGizmo.cpp b/Editor/src/Misc/Gizmos/CapsuleGizmo.cpp new file mode 100644 index 00000000..abb3fd4e --- /dev/null +++ b/Editor/src/Misc/Gizmos/CapsuleGizmo.cpp @@ -0,0 +1,122 @@ +#include "CapsuleGizmo.h" + +#include + +CapsuleGizmo::CapsuleGizmo() : + _Radius(0.0f), + _Height(0.0f) +{ +} + +void CapsuleGizmo::Bind() +{ + _CapsuleBuffer->Bind(); +} + +void CapsuleGizmo::UpdateShape(float radius, float height) +{ + if (_Radius == radius && _Height == height) + { + return; + } + + _Radius = radius; + _Height = height; + CreateMesh(); +} + +void CapsuleGizmo::CreateMesh() +{ + using namespace Nuake; + + _CapsuleVertices.clear(); + + const float halfHeight = _Height / 2.0f; + const float radius = _Radius; + const float bottomCircleHeight = -halfHeight + radius; + const float topCircleHeight = halfHeight - radius; + + // Generate circles + const float subDivision = 32.0f; + constexpr const float pi = glm::pi() * 4.0; + float increment = pi / subDivision; + for (int i = 0; i < subDivision * 2.0; i++) + { + float current = increment * (i); + float x = glm::cos(current) * radius; + float z = glm::sin(current) * radius; + + current = increment * (i + 1); + float x2 = glm::cos(current) * radius; + float z2 = glm::sin(current) * radius; + + Vector3 vert1, vert2; + if (i < subDivision) + { + vert1 = Vector3(x, topCircleHeight, z); + vert2 = Vector3(x2, topCircleHeight, z2); + } + else + { + vert1 = Vector3(x, bottomCircleHeight, z); + vert2 = Vector3(x2, bottomCircleHeight, z2); + } + + _CapsuleVertices.push_back(LineVertex{ vert1, Color(1.0, 0, 0.0, 1.0) }); + _CapsuleVertices.push_back(LineVertex{ vert2, Color(1.0, 0, 0.0, 1.0) }); + } + + for (int i = 0; i < subDivision * 2.0; i++) + { + float current = increment * (i); + float x = glm::cos(current) * radius; + float z = glm::sin(current) * radius; + + current = increment * (i + 1); + float x2 = glm::cos(current) * radius; + float z2 = glm::sin(current) * radius; + + float heightOffset = topCircleHeight; + if (z < 0.0) + { + heightOffset = bottomCircleHeight; + } + + Vector3 vert1, vert2; + if (i < subDivision) + { + vert1 = Vector3(x, z + heightOffset, 0); + vert2 = Vector3(x2, z2 + heightOffset, 0); + } + else + { + vert1 = Vector3(0, z + heightOffset, x); + vert2 = Vector3(0, z2 + heightOffset, x2); + } + + _CapsuleVertices.push_back(LineVertex{ vert1, Color(1.0, 0, 0.0, 1.0) }); + _CapsuleVertices.push_back(LineVertex{ vert2, Color(1.0, 0, 0.0, 1.0) }); + } + + _CapsuleVertices.push_back(LineVertex{ Vector3(radius, bottomCircleHeight, 0), Color(1.0, 0, 0.0, 1.0) }); + _CapsuleVertices.push_back(LineVertex{ Vector3(radius, topCircleHeight, 0), Color(1.0, 0, 0.0, 1.0) }); + + _CapsuleVertices.push_back(LineVertex{ Vector3(-radius, bottomCircleHeight, 0), Color(1.0, 0, 0.0, 1.0) }); + _CapsuleVertices.push_back(LineVertex{ Vector3(-radius, topCircleHeight, 0), Color(1.0, 0, 0.0, 1.0) }); + + _CapsuleVertices.push_back(LineVertex{ Vector3(0, bottomCircleHeight, radius), Color(1.0, 0, 0.0, 1.0) }); + _CapsuleVertices.push_back(LineVertex{ Vector3(0, topCircleHeight, radius), Color(1.0, 0, 0.0, 1.0) }); + + _CapsuleVertices.push_back(LineVertex{ Vector3(0, bottomCircleHeight, -radius), Color(1.0, 0, 0.0, 1.0) }); + _CapsuleVertices.push_back(LineVertex{ Vector3(0, topCircleHeight, -radius), Color(1.0, 0, 0.0, 1.0) }); + + _CapsuleBuffer = CreateRef(); + _CapsuleBuffer->Bind(); + + _CapsuleVertexBuffer = CreateRef(_CapsuleVertices.data(), _CapsuleVertices.size() * sizeof(Nuake::LineVertex)); + auto vblayout = CreateRef(); + vblayout->Push(3); + vblayout->Push(4); + + _CapsuleBuffer->AddBuffer(*_CapsuleVertexBuffer, *vblayout); +} \ No newline at end of file diff --git a/Editor/src/Misc/Gizmos/CapsuleGizmo.h b/Editor/src/Misc/Gizmos/CapsuleGizmo.h new file mode 100644 index 00000000..6bb825ea --- /dev/null +++ b/Editor/src/Misc/Gizmos/CapsuleGizmo.h @@ -0,0 +1,26 @@ +#pragma once +#include +#include +#include +#include + +#include + +class CapsuleGizmo +{ +private: + std::vector _CapsuleVertices; + Ref _CapsuleBuffer; + Ref _CapsuleVertexBuffer; + + float _Radius; + float _Height; + +public: + CapsuleGizmo(); + ~CapsuleGizmo() = default; + + void UpdateShape(float radius, float height); + void CreateMesh(); + void Bind(); +}; \ No newline at end of file diff --git a/Editor/src/Misc/Gizmos/CylinderGizmo.cpp b/Editor/src/Misc/Gizmos/CylinderGizmo.cpp new file mode 100644 index 00000000..dc86ce3d --- /dev/null +++ b/Editor/src/Misc/Gizmos/CylinderGizmo.cpp @@ -0,0 +1,90 @@ +#include "CylinderGizmo.h" + +#include + +CylinderGizmo::CylinderGizmo() : + _Radius(0.0f), + _Height(0.0f) +{ +} + +void CylinderGizmo::Bind() +{ + _CylinderBuffer->Bind(); +} + +void CylinderGizmo::UpdateShape(float radius, float height) +{ + if (_Radius == radius && _Height == height) + { + return; + } + + _Radius = radius; + _Height = height; + CreateMesh(); +} + +void CylinderGizmo::CreateMesh() +{ + using namespace Nuake; + + _CylinderVertices.clear(); + + const float halfHeight = _Height / 2.0f; + const float radius = _Radius; + const float bottomCircleHeight = -halfHeight; + const float topCircleHeight = halfHeight; + + // Generate circles + const float subDivision = 32.0f; + constexpr const float pi = glm::pi() * 4.0; + float increment = pi / subDivision; + for (int i = 0; i < subDivision * 2.0; i++) + { + float current = increment * (i); + float x = glm::cos(current) * radius; + float z = glm::sin(current) * radius; + + current = increment * (i + 1); + float x2 = glm::cos(current) * radius; + float z2 = glm::sin(current) * radius; + + Vector3 vert1, vert2; + if (i < subDivision) + { + vert1 = Vector3(x, topCircleHeight, z); + vert2 = Vector3(x2, topCircleHeight, z2); + } + else + { + vert1 = Vector3(x, bottomCircleHeight, z); + vert2 = Vector3(x2, bottomCircleHeight, z2); + } + + _CylinderVertices.push_back(LineVertex{ vert1, Color(1.0, 0, 0.0, 1.0) }); + _CylinderVertices.push_back(LineVertex{ vert2, Color(1.0, 0, 0.0, 1.0) }); + } + + _CylinderVertices.push_back(LineVertex{ Vector3(radius, bottomCircleHeight, 0), Color(1.0, 0, 0.0, 1.0) }); + _CylinderVertices.push_back(LineVertex{ Vector3(radius, topCircleHeight, 0), Color(1.0, 0, 0.0, 1.0) }); + + _CylinderVertices.push_back(LineVertex{ Vector3(-radius, bottomCircleHeight, 0), Color(1.0, 0, 0.0, 1.0) }); + _CylinderVertices.push_back(LineVertex{ Vector3(-radius, topCircleHeight, 0), Color(1.0, 0, 0.0, 1.0) }); + + _CylinderVertices.push_back(LineVertex{ Vector3(0, bottomCircleHeight, radius), Color(1.0, 0, 0.0, 1.0) }); + _CylinderVertices.push_back(LineVertex{ Vector3(0, topCircleHeight, radius), Color(1.0, 0, 0.0, 1.0) }); + + _CylinderVertices.push_back(LineVertex{ Vector3(0, bottomCircleHeight, -radius), Color(1.0, 0, 0.0, 1.0) }); + _CylinderVertices.push_back(LineVertex{ Vector3(0, topCircleHeight, -radius), Color(1.0, 0, 0.0, 1.0) }); + + _CylinderBuffer = CreateRef(); + _CylinderBuffer->Bind(); + + _CylinderVertexBuffer = CreateRef(_CylinderVertices.data(), _CylinderVertices.size() * sizeof(Nuake::LineVertex)); + auto vblayout = CreateRef(); + vblayout->Push(3); + vblayout->Push(4); + + _CylinderBuffer->AddBuffer(*_CylinderVertexBuffer, *vblayout); +} \ No newline at end of file diff --git a/Editor/src/Misc/Gizmos/CylinderGizmo.h b/Editor/src/Misc/Gizmos/CylinderGizmo.h new file mode 100644 index 00000000..cecb7c6e --- /dev/null +++ b/Editor/src/Misc/Gizmos/CylinderGizmo.h @@ -0,0 +1,26 @@ +#pragma once +#include +#include +#include +#include + +#include + +class CylinderGizmo +{ +private: + std::vector _CylinderVertices; + Ref _CylinderBuffer; + Ref _CylinderVertexBuffer; + + float _Radius; + float _Height; + +public: + CylinderGizmo(); + ~CylinderGizmo() = default; + + void UpdateShape(float radius, float height); + void CreateMesh(); + void Bind(); +}; \ No newline at end of file diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index 5af71753..1731eddf 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -1219,7 +1219,6 @@ namespace Nuake { } ImGui::PopStyleVar(); ImGui::End(); - int corner2 = 1; diff --git a/Editor/src/Windows/EditorSelectionPanel.cpp b/Editor/src/Windows/EditorSelectionPanel.cpp index 8409401b..2c1d478f 100644 --- a/Editor/src/Windows/EditorSelectionPanel.cpp +++ b/Editor/src/Windows/EditorSelectionPanel.cpp @@ -102,6 +102,7 @@ void EditorSelectionPanel::DrawEntity(Nuake::Entity entity) mBoxColliderPanel.Draw(entity); mSphereColliderPanel.Draw(entity); mCapsuleColliderPanel.Draw(entity); + mCylinderColliderPanel.Draw(entity); mMeshColliderPanel.Draw(entity); mCharacterControllerPanel.Draw(entity); } @@ -126,8 +127,10 @@ void EditorSelectionPanel::DrawAddComponentMenu(Nuake::Entity entity) ImGui::Separator(); MenuItemComponent("Character Controller", Nuake::CharacterControllerComponent) MenuItemComponent("Rigid body", Nuake::RigidBodyComponent) + ImGui::Separator(); MenuItemComponent("Box collider", Nuake::BoxColliderComponent) MenuItemComponent("Capsule collider", Nuake::CapsuleColliderComponent) + MenuItemComponent("Cylinder collider", Nuake::CylinderColliderComponent) MenuItemComponent("Sphere collider", Nuake::SphereColliderComponent) MenuItemComponent("Mesh collider", Nuake::MeshColliderComponent) ImGui::Separator(); @@ -139,7 +142,6 @@ void EditorSelectionPanel::DrawAddComponentMenu(Nuake::Entity entity) } - void EditorSelectionPanel::DrawFile(Nuake::File* file) { using namespace Nuake; diff --git a/Editor/src/Windows/EditorSelectionPanel.h b/Editor/src/Windows/EditorSelectionPanel.h index ff19422a..4b6dc0b8 100644 --- a/Editor/src/Windows/EditorSelectionPanel.h +++ b/Editor/src/Windows/EditorSelectionPanel.h @@ -14,6 +14,7 @@ #include "../ComponentsPanel/RigidbodyPanel.h" #include "../ComponentsPanel/BoxColliderPanel.h" #include "../ComponentsPanel/CapsuleColliderPanel.h" +#include "../ComponentsPanel/CylinderColliderPanel.h" #include "../ComponentsPanel/SphereColliderPanel.h" #include "../ComponentsPanel/MeshColliderPanel.h" #include "../ComponentsPanel/CharacterControllerPanel.h" @@ -31,6 +32,7 @@ private: SphereColliderPanel mSphereColliderPanel; MeshColliderPanel mMeshColliderPanel; CapsuleColliderPanel mCapsuleColliderPanel; + CylinderColliderPanel mCylinderColliderPanel; CharacterControllerPanel mCharacterControllerPanel; diff --git a/Nuake/src/Core/Physics/PhysicsShapes.h b/Nuake/src/Core/Physics/PhysicsShapes.h index f21c28b3..49e3b167 100644 --- a/Nuake/src/Core/Physics/PhysicsShapes.h +++ b/Nuake/src/Core/Physics/PhysicsShapes.h @@ -49,6 +49,7 @@ namespace Nuake float Radius; float Height; public: + Capsule() = default; Capsule(float radius, float height) : Radius(radius), Height(height) diff --git a/Nuake/src/Rendering/Mesh/Mesh.cpp b/Nuake/src/Rendering/Mesh/Mesh.cpp index 1d847f44..fce1fcd5 100644 --- a/Nuake/src/Rendering/Mesh/Mesh.cpp +++ b/Nuake/src/Rendering/Mesh/Mesh.cpp @@ -91,6 +91,11 @@ namespace Nuake m_VertexArray->Unbind(); } + void Mesh::Bind() const + { + m_VertexArray->Bind(); + } + void Mesh::Draw(Shader* shader, bool bindMaterial) { if (bindMaterial) diff --git a/Nuake/src/Rendering/Mesh/Mesh.h b/Nuake/src/Rendering/Mesh/Mesh.h index 68458ca2..5e6ecaf1 100644 --- a/Nuake/src/Rendering/Mesh/Mesh.h +++ b/Nuake/src/Rendering/Mesh/Mesh.h @@ -25,6 +25,7 @@ namespace Nuake Ref GetMaterial() inline const; void SetMaterial(Ref material); + void Bind() const; void Draw(Shader* shader, bool bindMaterial = true); void DebugDraw(); diff --git a/Nuake/src/Scene/Components/CapsuleColliderComponent.h b/Nuake/src/Scene/Components/CapsuleColliderComponent.h index c6c482d0..359bf02f 100644 --- a/Nuake/src/Scene/Components/CapsuleColliderComponent.h +++ b/Nuake/src/Scene/Components/CapsuleColliderComponent.h @@ -7,7 +7,7 @@ namespace Nuake class CapsuleColliderComponent { public: - Ref Capsule; + Ref Capsule; float Radius = 0.5f; float Height = 1.0f; diff --git a/Nuake/src/Scene/Components/Components.h b/Nuake/src/Scene/Components/Components.h index 614f5c7d..8b40a93a 100644 --- a/Nuake/src/Scene/Components/Components.h +++ b/Nuake/src/Scene/Components/Components.h @@ -8,11 +8,15 @@ #include "CameraComponent.h" #include "ModelComponent.h" #include "QuakeMap.h" + #include "RigidbodyComponent.h" +#include "CharacterControllerComponent.h" + #include "SphereCollider.h" #include "MeshCollider.h" +#include "CylinderColliderComponent.h" #include "NativeScriptComponent.h" -#include "CharacterControllerComponent.h" + #include "ParentComponent.h" #include "NameComponent.h" #include "BoxCollider.h" diff --git a/Nuake/src/Scene/Components/CylinderColliderComponent.cpp b/Nuake/src/Scene/Components/CylinderColliderComponent.cpp new file mode 100644 index 00000000..58c905ed --- /dev/null +++ b/Nuake/src/Scene/Components/CylinderColliderComponent.cpp @@ -0,0 +1,24 @@ +#include "CylinderColliderComponent.h" +#include "src/Resource/Serializable.h" + +namespace Nuake +{ + json CylinderColliderComponent::Serialize() + { + BEGIN_SERIALIZE() + + j["IsTrigger"] = IsTrigger; + SERIALIZE_VAL(Radius) + SERIALIZE_VAL(Height) + END_SERIALIZE() + } + + bool CylinderColliderComponent::Deserialize(const std::string& str) + { + BEGIN_DESERIALIZE() + this->IsTrigger = j["IsTrigger"]; + this->Radius = j["Radius"]; + this->Height = j["Height"]; + return true; + } +} \ No newline at end of file diff --git a/Nuake/src/Scene/Components/CylinderColliderComponent.h b/Nuake/src/Scene/Components/CylinderColliderComponent.h new file mode 100644 index 00000000..54e9146e --- /dev/null +++ b/Nuake/src/Scene/Components/CylinderColliderComponent.h @@ -0,0 +1,20 @@ +#pragma once +#include "src/Core/Physics/PhysicsShapes.h" +#include "src/Core/Core.h" + +namespace Nuake +{ + class CylinderColliderComponent + { + public: + Ref Cylinder; + + float Radius = 0.5f; + float Height = 1.0f; + + bool IsTrigger = false; + + json Serialize(); + bool Deserialize(const std::string& str); + }; +} diff --git a/Nuake/src/Scene/Components/MeshCollider.h b/Nuake/src/Scene/Components/MeshCollider.h index c821d5b8..eaca94cc 100644 --- a/Nuake/src/Scene/Components/MeshCollider.h +++ b/Nuake/src/Scene/Components/MeshCollider.h @@ -6,7 +6,7 @@ namespace Nuake { class MeshColliderComponent { public: - int SubMesh = 0; + uint32_t SubMesh = 0; bool IsTrigger; Ref Shape; diff --git a/Nuake/src/Scene/Entities/Entity.cpp b/Nuake/src/Scene/Entities/Entity.cpp index e187c3a8..30c9583a 100644 --- a/Nuake/src/Scene/Entities/Entity.cpp +++ b/Nuake/src/Scene/Entities/Entity.cpp @@ -49,6 +49,8 @@ namespace Nuake SERIALIZE_OBJECT_REF_LBL("BoxColliderComponent", GetComponent()) if (HasComponent()) SERIALIZE_OBJECT_REF_LBL("CapsuleColliderComponent", GetComponent()) + if (HasComponent()) + SERIALIZE_OBJECT_REF_LBL("CylinderColliderComponent", GetComponent()) if (HasComponent()) SERIALIZE_OBJECT_REF_LBL("SphereColliderComponent", GetComponent()) if (HasComponent()) @@ -84,6 +86,7 @@ namespace Nuake DESERIALIZE_COMPONENT(CharacterControllerComponent) DESERIALIZE_COMPONENT(BoxColliderComponent) DESERIALIZE_COMPONENT(CapsuleColliderComponent) + DESERIALIZE_COMPONENT(CylinderColliderComponent) DESERIALIZE_COMPONENT(MeshColliderComponent) DESERIALIZE_COMPONENT(SphereColliderComponent) DESERIALIZE_COMPONENT(RigidBodyComponent) diff --git a/Nuake/src/Scene/Scene.cpp b/Nuake/src/Scene/Scene.cpp index 3979488f..ca524e05 100644 --- a/Nuake/src/Scene/Scene.cpp +++ b/Nuake/src/Scene/Scene.cpp @@ -95,7 +95,6 @@ namespace Nuake { Logger::Log("Init system"); if (!system->Init()) { - return false; } } diff --git a/Nuake/src/Scene/Systems/PhysicsSystem.cpp b/Nuake/src/Scene/Systems/PhysicsSystem.cpp index a1eecbd5..2f5e0ff7 100644 --- a/Nuake/src/Scene/Systems/PhysicsSystem.cpp +++ b/Nuake/src/Scene/Systems/PhysicsSystem.cpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace Nuake { @@ -174,6 +175,15 @@ namespace Nuake capsuleComponent.Capsule = CreateRef(radius, height); } + const auto cylinderView = m_Scene->m_Registry.view(); + for (auto entity : cylinderView) + { + auto& cylinderComponent = cylinderView.get(entity); + float radius = cylinderComponent.Radius; + float height = cylinderComponent.Height; + cylinderComponent.Cylinder = CreateRef(radius, height); + } + const auto sphereView = m_Scene->m_Registry.view(); for (auto entity : sphereView) { @@ -238,6 +248,17 @@ namespace Nuake PhysicsManager::Get().RegisterBody(rigidBody); } + if (ent.HasComponent()) + { + auto& cylinderComponent = ent.GetComponent(); + float radius = cylinderComponent.Radius; + float height = cylinderComponent.Height; + auto cylinderShape = CreateRef(radius, height); + + rigidBody = CreateRef(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalRotation(), transform.GetGlobalTransform(), cylinderShape, ent); + PhysicsManager::Get().RegisterBody(rigidBody); + } + if (ent.HasComponent()) { float mass = rigidBodyComponent.Mass;