Added capsule gizmo

This commit is contained in:
antopilo
2023-03-24 09:45:09 -04:00
parent 278e759105
commit b81530924b

View File

@@ -1,11 +1,15 @@
#include "GizmoDrawer.h"
#include <src/Rendering/Buffers/VertexBuffer.h>
#include <src/Scene/Components/LightComponent.h>
#include <src/Scene/Components/CameraComponent.h>
#include <src/Scene/Components/SphereCollider.h>
#include <src/Scene/Components/BoxCollider.h>
#include <src/Resource/ModelLoader.h>
#include <src/Rendering/RenderList.h>
#include <dependencies/GLEW/include/GL/glew.h>
#include <src/Scene/Components/LightComponent.h>
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<float>() * 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<Nuake::VertexArray>();
mCapsuleBuffer->Bind();
mCapsuleVertexBuffer = CreateRef<VertexBuffer>(capsuleVertices.data(), capsuleVertices.size() * sizeof(Nuake::LineVertex));
auto vblayout = CreateRef<VertexBufferLayout>();
vblayout->Push<float>(3);
vblayout->Push<float>(4);
mCapsuleBuffer->AddBuffer(*mCapsuleVertexBuffer, *vblayout);
}
void GizmoDrawer::DrawGizmos(Ref<Scene> scene)
{
using namespace Nuake;
@@ -99,6 +199,18 @@ void GizmoDrawer::DrawGizmos(Ref<Scene> scene)
Nuake::RenderCommand::DrawLines(0, 128);
}
auto capsuleColliderView = scene->m_Registry.view<TransformComponent, BoxColliderComponent>();
for (auto e : capsuleColliderView)
{
auto [transform, capsule] = scene->m_Registry.get<TransformComponent, BoxColliderComponent>(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());