71 lines
2.1 KiB
C++
71 lines
2.1 KiB
C++
#include <destrum/Components/CameraComponent.h>
|
|
|
|
#include <destrum/ObjectModel/GameObject.h>
|
|
#include <destrum/ObjectModel/Transform.h>
|
|
#include <destrum/Scene/Scene.h>
|
|
#include <destrum/Serialization/Reflect.h>
|
|
|
|
#include <glm/gtc/quaternion.hpp>
|
|
#include <glm/gtx/quaternion.hpp>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <imgui.h>
|
|
|
|
CameraComponent::CameraComponent(GameObject& parent)
|
|
: Component(parent, "CameraComponent")
|
|
{
|
|
}
|
|
|
|
Camera& CameraComponent::GetCamera(float aspectRatio)
|
|
{
|
|
auto& transform = GetTransform();
|
|
const glm::quat rotation = transform.GetWorldRotation();
|
|
|
|
m_Camera.m_position = transform.GetWorldPosition();
|
|
m_Camera.SetOrientation(rotation * glm::vec3{0.0f, 0.0f, -1.0f}, rotation * glm::vec3{0.0f, 1.0f, 0.0f});
|
|
m_Camera.SetFOVY(m_FieldOfView);
|
|
m_Camera.SetZNear(m_ZNear);
|
|
m_Camera.SetZFar(m_ZFar);
|
|
m_Camera.setAspectRatio(aspectRatio);
|
|
return m_Camera;
|
|
}
|
|
|
|
void CameraComponent::SetFieldOfView(float fovDegrees)
|
|
{
|
|
m_FieldOfView = std::clamp(fovDegrees, 1.0f, 179.0f);
|
|
}
|
|
|
|
void CameraComponent::SetClipPlanes(float zNear, float zFar)
|
|
{
|
|
m_ZNear = std::max(zNear, 0.0001f);
|
|
m_ZFar = std::max(zFar, m_ZNear + 0.01f);
|
|
}
|
|
|
|
void CameraComponent::MakeCurrent()
|
|
{
|
|
if (Scene* scene = GetGameObject()->GetScene()) {
|
|
scene->SetCurrentCamera(this);
|
|
}
|
|
}
|
|
|
|
bool CameraComponent::IsCurrent() const
|
|
{
|
|
const Scene* scene = GetGameObject()->GetScene();
|
|
return scene != nullptr && scene->GetCurrentCamera() == this;
|
|
}
|
|
|
|
void CameraComponent::Reflect(FieldVisitor& v) {
|
|
v.Property("fieldOfView", m_FieldOfView, [this](float fov) { SetFieldOfView(fov); }, {.speed = 0.5f, .min = 1.0, .max = 179.0});
|
|
v.Property("zNear", m_ZNear, [this](float zNear) { SetClipPlanes(zNear, m_ZFar); }, {.speed = 0.01f, .min = 0.0001, .label = "Near"});
|
|
v.Property("zFar", m_ZFar, [this](float zFar) { SetClipPlanes(m_ZNear, zFar); }, {.speed = 1.0f, .min = 0.1, .label = "Far"});
|
|
}
|
|
|
|
void CameraComponent::ImGuiInspector() {
|
|
bool current = IsCurrent();
|
|
if (ImGui::Checkbox("Current Camera", ¤t) && current) {
|
|
MakeCurrent();
|
|
}
|
|
Component::ImGuiInspector();
|
|
}
|