feat: Add Camera component / Add cooked model and texture loading
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
#include <destrum/Components/CameraComponent.h>
|
||||
|
||||
#include <destrum/ObjectModel/GameObject.h>
|
||||
#include <destrum/ObjectModel/Transform.h>
|
||||
#include <destrum/Scene/Scene.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;
|
||||
}
|
||||
|
||||
nlohmann::json CameraComponent::Serialize() const {
|
||||
return {
|
||||
{"fieldOfView", m_FieldOfView},
|
||||
{"zNear", m_ZNear},
|
||||
{"zFar", m_ZFar}
|
||||
};
|
||||
}
|
||||
|
||||
void CameraComponent::Deserialize(const nlohmann::json& data) {
|
||||
if (data.contains("fieldOfView")) {
|
||||
SetFieldOfView(data.at("fieldOfView").get<float>());
|
||||
}
|
||||
SetClipPlanes(data.value("zNear", m_ZNear), data.value("zFar", m_ZFar));
|
||||
}
|
||||
|
||||
void CameraComponent::ImGuiInspector() {
|
||||
bool current = IsCurrent();
|
||||
if (ImGui::Checkbox("Current Camera", ¤t) && current) {
|
||||
MakeCurrent();
|
||||
}
|
||||
|
||||
float fieldOfView = m_FieldOfView;
|
||||
if (ImGui::DragFloat("Field Of View", &fieldOfView, 0.5f, 1.0f, 179.0f)) {
|
||||
SetFieldOfView(fieldOfView);
|
||||
}
|
||||
|
||||
float zNear = m_ZNear;
|
||||
float zFar = m_ZFar;
|
||||
const bool nearChanged = ImGui::DragFloat("Near", &zNear, 0.01f, 0.0001f, 100.0f);
|
||||
const bool farChanged = ImGui::DragFloat("Far", &zFar, 1.0f, 0.1f, 100000.0f);
|
||||
if (nearChanged || farChanged) {
|
||||
SetClipPlanes(zNear, zFar);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user