From b81530924b65855b58944bfcfc8ab461cf980779 Mon Sep 17 00:00:00 2001 From: antopilo Date: Fri, 24 Mar 2023 09:45:09 -0400 Subject: [PATCH] Added capsule gizmo --- Editor/src/Misc/GizmoDrawer.cpp | 114 +++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-) diff --git a/Editor/src/Misc/GizmoDrawer.cpp b/Editor/src/Misc/GizmoDrawer.cpp index bfd60480..7bfd482d 100644 --- a/Editor/src/Misc/GizmoDrawer.cpp +++ b/Editor/src/Misc/GizmoDrawer.cpp @@ -1,11 +1,15 @@ #include "GizmoDrawer.h" #include + +#include #include #include +#include + #include #include + #include -#include GizmoDrawer::GizmoDrawer() { @@ -22,6 +26,7 @@ GizmoDrawer::GizmoDrawer() mAxisLineBuffer->AddBuffer(*mAxisLineVertexBuffer, *vblayout); GenerateSphereGizmo(); + GenerateCapsuleGizmo(); // Load gizmos ModelLoader loader; @@ -72,6 +77,101 @@ 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) { using namespace Nuake; @@ -99,6 +199,18 @@ void GizmoDrawer::DrawGizmos(Ref scene) Nuake::RenderCommand::DrawLines(0, 128); } + auto capsuleColliderView = scene->m_Registry.view(); + for (auto e : capsuleColliderView) + { + auto [transform, capsule] = scene->m_Registry.get(e); + mLineShader->Bind(); + mLineShader->SetUniformMat4f("u_View", glm::translate(scene->m_EditorCamera->GetTransform(), transform.Translation)); + mLineShader->SetUniformMat4f("u_Projection", scene->m_EditorCamera->GetPerspective()); + + mCapsuleBuffer->Bind(); + Nuake::RenderCommand::DrawLines(0, 264); + } + auto flatShader = ShaderManager::GetShader("resources/Shaders/flat.shader"); flatShader->Bind(); flatShader->SetUniformMat4f("u_View", scene->m_EditorCamera->GetTransform());