From 06c728644f7f7047d1251dae663a4fb1efff4ae5 Mon Sep 17 00:00:00 2001 From: Light Date: Mon, 2 Aug 2021 12:18:00 +0430 Subject: [PATCH] SceneCamera Properties - PropertiesPanel now support SceneCamera component - Added perspective support to SceneCamera --- Engine/src/Engine/Camera/SceneCamera.cpp | 72 ++++++++++++++++++++---- Engine/src/Engine/Camera/SceneCamera.h | 45 ++++++++++++++- Mirror/src/MirrorLayer.h | 2 +- Mirror/src/Panels/PropertiesPanel.cpp | 64 +++++++++++++++++++++ 4 files changed, 169 insertions(+), 14 deletions(-) diff --git a/Engine/src/Engine/Camera/SceneCamera.cpp b/Engine/src/Engine/Camera/SceneCamera.cpp index 75c84c3..d7f26f7 100644 --- a/Engine/src/Engine/Camera/SceneCamera.cpp +++ b/Engine/src/Engine/Camera/SceneCamera.cpp @@ -6,10 +6,10 @@ namespace Light { SceneCamera::SceneCamera() - : m_OrthoSize(1000.0f), - m_OrthoNearPlane(-1.0f), - m_OrthoFarPlane(1.0f), - m_AspectRatio(16.0f / 9.0f) + : m_OrthographicSpecification{ 10.0f, -1.0f, 10000.0f }, + m_PerspectiveSpecification{ glm::radians(45.0f), 0.01f, 10000.0f }, + m_AspectRatio(16.0f / 9.0f), + m_ProjectionType(ProjectionType::Orthographic) { CalculateProjection(); } @@ -20,14 +20,66 @@ namespace Light { CalculateProjection(); } + void SceneCamera::SetProjectionType(ProjectionType projectionType) + { + m_ProjectionType = projectionType; + CalculateProjection(); + } + + void SceneCamera::SetOrthographicSize(float size) + { + m_OrthographicSpecification.size = size; + CalculateProjection(); + } + + void SceneCamera::SetOrthographicFarPlane(float farPlane) + { + m_OrthographicSpecification.farPlane = farPlane; + CalculateProjection(); + } + + void SceneCamera::SetOrthographicNearPlane(float nearPlane) + { + m_OrthographicSpecification.nearPlane = nearPlane; + CalculateProjection(); + } + + void SceneCamera::SetPerspectiveVerticalFOV(float verticalFOV) + { + m_PerspectiveSpecification.verticalFOV = verticalFOV; + CalculateProjection(); + } + + void SceneCamera::SetPerspectiveFarPlane(float farPlane) + { + m_PerspectiveSpecification.farPlane = farPlane; + CalculateProjection(); + } + + void SceneCamera::SetPerspectiveNearPlane(float nearPlane) + { + m_PerspectiveSpecification.nearPlane = nearPlane; + CalculateProjection(); + } + void SceneCamera::CalculateProjection() { - m_Projection = glm::ortho(-m_OrthoSize * 0.5f * m_AspectRatio, - m_OrthoSize * 0.5f * m_AspectRatio, - -m_OrthoSize * 0.5f, - m_OrthoSize * 0.5f, - m_OrthoFarPlane, - m_OrthoNearPlane); + if(m_ProjectionType == ProjectionType::Orthographic) + { + m_Projection = glm::ortho(-m_OrthographicSpecification.size * 0.5f * m_AspectRatio, + m_OrthographicSpecification.size * 0.5f * m_AspectRatio, + -m_OrthographicSpecification.size * 0.5f, + m_OrthographicSpecification.size * 0.5f, + m_OrthographicSpecification.farPlane, + m_OrthographicSpecification.nearPlane); + } + else // perspective + { + m_Projection = glm::perspective(m_PerspectiveSpecification.verticalFOV, + m_AspectRatio, + m_PerspectiveSpecification.nearPlane, + m_PerspectiveSpecification.farPlane); + } } } \ No newline at end of file diff --git a/Engine/src/Engine/Camera/SceneCamera.h b/Engine/src/Engine/Camera/SceneCamera.h index 3640973..72da15a 100644 --- a/Engine/src/Engine/Camera/SceneCamera.h +++ b/Engine/src/Engine/Camera/SceneCamera.h @@ -8,17 +8,56 @@ namespace Light { class SceneCamera : public Camera { - private: - float m_OrthoSize; - float m_OrthoNearPlane, m_OrthoFarPlane; + public: + enum class ProjectionType + { + Orthographic = 0, + Perspetcive = 1 + }; + struct OrthographicSpecification // :#todo use this + { + float size; + float nearPlane, farPlane; + }; + + struct PerspectiveSpecification + { + float verticalFOV; + float nearPlane, farPlane; + }; + + private: + OrthographicSpecification m_OrthographicSpecification; + PerspectiveSpecification m_PerspectiveSpecification; float m_AspectRatio; + ProjectionType m_ProjectionType; + public: SceneCamera(); void SetViewportSize(unsigned int width, unsigned int height); + void SetProjectionType(ProjectionType projectionType); + + void SetOrthographicSize(float size); + void SetOrthographicFarPlane(float farPlane); + void SetOrthographicNearPlane(float nearPlane); + + void SetPerspectiveVerticalFOV(float verticalFov); + void SetPerspectiveFarPlane(float farPlane); + void SetPerspectiveNearPlane(float nearPlane); + + inline float GetOrthographicSize() const { return m_OrthographicSpecification.size; } + inline float GetOrthographicFarPlane() const { return m_OrthographicSpecification.farPlane; } + inline float GetOrthographicNearPlane() const { return m_OrthographicSpecification.nearPlane; } + + inline float GetPerspectiveverticalFOV() const { return m_PerspectiveSpecification.verticalFOV; } + inline float GetPerspectiveFarPlane() const { return m_PerspectiveSpecification.farPlane; } + inline float GetPerspectiveNearPlane() const { return m_PerspectiveSpecification.nearPlane; } + + inline ProjectionType GetProjectionType() const { return m_ProjectionType; } private: void CalculateProjection(); }; diff --git a/Mirror/src/MirrorLayer.h b/Mirror/src/MirrorLayer.h index bd45873..0fad0f0 100644 --- a/Mirror/src/MirrorLayer.h +++ b/Mirror/src/MirrorLayer.h @@ -48,7 +48,7 @@ namespace Light { } - m_CameraEntity = m_Scene->CreateEntity("camera", glm::mat4(1.0f)); + m_CameraEntity = m_Scene->CreateEntity("camera", glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 1000.0f))); m_CameraEntity.AddComponent(SceneCamera(), true); m_CameraEntity.AddComponent("Camera"); diff --git a/Mirror/src/Panels/PropertiesPanel.cpp b/Mirror/src/Panels/PropertiesPanel.cpp index a59952a..1c8bf0d 100644 --- a/Mirror/src/Panels/PropertiesPanel.cpp +++ b/Mirror/src/Panels/PropertiesPanel.cpp @@ -41,6 +41,70 @@ namespace Light { ImGui::TreePop(); } } + + if(m_EntityContext.HasComponent()) + { + auto& cameraComp = m_EntityContext.GetComponent(); + auto& camera = cameraComp.camera; + + SceneCamera::ProjectionType projectionType = camera.GetProjectionType(); + const char* projectionTypesString[] = { "Orthographic", "Perspective" }; + + if(ImGui::BeginCombo("ProjectionType", projectionTypesString[(int)projectionType])) + { + for(int i = 0; i < 2; i++) + { + const bool isSelected = (int)projectionType == i; + if (ImGui::Selectable(projectionTypesString[i], isSelected)) + { + projectionType = (SceneCamera::ProjectionType)i; + camera.SetProjectionType(projectionType); + } + + if (isSelected) + ImGui::SetItemDefaultFocus(); + } + + ImGui::EndCombo(); + } + + if(projectionType == SceneCamera::ProjectionType::Orthographic) + { + float orthoSize, nearPlane, farPlane; + + orthoSize = camera.GetOrthographicSize(); + nearPlane = camera.GetOrthographicNearPlane(); + farPlane = camera.GetOrthographicFarPlane(); + + if(ImGui::DragFloat("Orthographic Size", &orthoSize)) + camera.SetOrthographicSize(orthoSize); + + if (ImGui::DragFloat("Near Plane", &nearPlane)) + camera.SetOrthographicNearPlane(nearPlane); + + if(ImGui::DragFloat("Far Plane", &farPlane)) + camera.SetOrthographicFarPlane(farPlane); + } + + else // perspective + { + float verticalFOV, nearPlane, farPlane; + + verticalFOV = glm::degrees(camera.GetPerspectiveverticalFOV()); + nearPlane = camera.GetPerspectiveNearPlane(); + farPlane = camera.GetPerspectiveFarPlane(); + + if (ImGui::DragFloat("Vertical FOV", &verticalFOV)) + camera.SetPerspectiveVerticalFOV(glm::radians(verticalFOV)); + + if (ImGui::DragFloat("Near Plane", &nearPlane)) + camera.SetPerspectiveNearPlane(nearPlane); + + if (ImGui::DragFloat("Far Plane", &farPlane)) + camera.SetPerspectiveFarPlane(farPlane); + } + + } } ImGui::End();