-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working editor scene that utilizes our scene structure with basic ECS…
… system working.
- Loading branch information
Showing
12 changed files
with
249 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#pragma once | ||
|
||
#include <Core/SceneManagment/Components/GameComponent.hpp> | ||
#include <glm/glm.hpp> | ||
#include <string> | ||
|
||
namespace engine3d{ | ||
class EditorCamera : public GameComponent{ | ||
public: | ||
struct CameraData{ | ||
float PerspectiveFOV = glm::radians(45.0f); | ||
float PerspectiveNear = 0.01; | ||
float PerspectiveFar = 1000.0f; | ||
float OrthographicSize = 10.0f; | ||
float OrthographicNear = 1.0f; | ||
float OrthographicFar = 1.0f; | ||
}; | ||
|
||
enum CameraProjectionType{ | ||
UNDEFINED = -1, | ||
ORTHOGRAPHIC = 0, | ||
PERSPECTIVE = 1 | ||
}; | ||
|
||
|
||
EditorCamera() = default; | ||
EditorCamera(const std::string& p_Tag); | ||
|
||
void SetInitialProperties(float p_Fov, float p_NearClip, float p_FarClip); | ||
void OnUpdate(); | ||
|
||
//! @note Eventually we will refer to OnIntegrate as OnCreate or OnPlay or something. | ||
//! @note Come up with a better name for this for accurate API. | ||
void OnIntegrate(); | ||
|
||
glm::quat GetOrientation() const; | ||
glm::vec3 GetUpDirection() const; | ||
glm::vec3 GetRightDirection() const; | ||
glm::vec3 GetForwardDirection() const; | ||
|
||
glm::mat4 GetProjection() const { return m_ProjectionMatrix; } | ||
glm::mat4 GetView() const { return m_ViewMatrix; } | ||
|
||
|
||
//! @note Getting pans. | ||
void CameraPan(const glm::vec2& p_Data); | ||
void CameraZoom(float delta); | ||
void CameraRotate(const glm::vec2& p_Data); | ||
|
||
private: | ||
glm::vec3 CalculateNewPosition() const; | ||
void RecalculateView(); | ||
|
||
void RecalculateProjection(); | ||
|
||
glm::vec3 RecalculatePosition() const; | ||
|
||
glm::vec2 PanSpeed() const; | ||
float ZoomSpeed() const; | ||
|
||
float RotationSpeed() const; | ||
|
||
private: | ||
std::string m_Tag; | ||
glm::vec2 m_InitialMousePosition = {0.0f, 0.0f}; | ||
float m_Fov = 45.0f, m_AspectRatio = 1.778f, m_NearClip=0.1f, m_FarClip = 1000.0f; | ||
|
||
glm::mat4 m_ViewMatrix; | ||
glm::mat4 m_ProjectionMatrix; | ||
glm::vec3 m_Position; | ||
glm::vec3 m_FocalPoint; | ||
//! @note Moving this out of Editor Camera. | ||
// glm::vec2 m_InitialMousePosition | ||
float m_Distance = 10.0f; | ||
float m_Pitch = 0.f, m_Yaw = 0.f; | ||
glm::vec2 m_ViewportSize; | ||
|
||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// #include "EditorCamera.hpp" | ||
#include <Core/SceneManagment/Components/SPComps/EditorCamera.hpp> | ||
#include <Core/Event/InputPoll.hpp> | ||
#include <Core/TimeManagement/UpdateManagers/SyncUpdateManager.hpp> | ||
#include <Core/ApplicationInstance.hpp> | ||
#include <glm/glm.hpp> | ||
#include <glm/gtc/matrix_transform.hpp> | ||
|
||
#define GLM_ENABLE_EXPERIMENTAL | ||
#include <glm/gtx/quaternion.hpp> | ||
|
||
#include <glm/gtc/matrix_transform.hpp> | ||
#include <glm/gtx/matrix_decompose.hpp> | ||
#include <glm/gtc/type_ptr.hpp> | ||
|
||
namespace engine3d{ | ||
EditorCamera::EditorCamera(const std::string& p_Tag) : m_Tag(p_Tag) {} | ||
|
||
void EditorCamera::OnIntegrate(){ | ||
} | ||
|
||
void EditorCamera::OnUpdate(){ | ||
if(InputPoll::IsKeyPressed(ENGINE_KEY_LEFT_ALT)){ | ||
glm::vec2 delta = (InputPoll::GetMousePosition() - m_InitialMousePosition) * 0.003f; | ||
m_InitialMousePosition = InputPoll::GetMousePosition(); | ||
|
||
if(InputPoll::IsMousePressed(Mouse::ButtonMiddle)){ | ||
CameraPan(delta); | ||
} | ||
else if(InputPoll::IsMousePressed(Mouse::ButtonLast)){ | ||
CameraRotate(delta); | ||
} | ||
else if(InputPoll::IsMousePressed(Mouse::ButtonRight)){ | ||
CameraZoom(delta.y); | ||
} | ||
} | ||
|
||
RecalculateView(); | ||
} | ||
|
||
void EditorCamera::SetInitialProperties(float p_Fov, float p_NearClip, float p_FarClip){ | ||
m_Fov = p_Fov; | ||
m_AspectRatio = ((float)ApplicationInstance::GetWindow().GetWidth() / (float)ApplicationInstance::GetWindow().GetHeight()); | ||
m_NearClip = p_NearClip; | ||
m_FarClip = p_FarClip; | ||
|
||
m_ViewportSize.x = (float)ApplicationInstance::GetWindow().GetWidth(); | ||
m_ViewportSize.y = (float)ApplicationInstance::GetWindow().GetHeight(); | ||
RecalculateView(); | ||
} | ||
|
||
glm::quat EditorCamera::GetOrientation() const { | ||
return glm::quat(glm::vec3(-m_Pitch, -m_Yaw, 1.0f)); | ||
} | ||
|
||
glm::vec3 EditorCamera::GetUpDirection() const{ | ||
return glm::rotate(GetOrientation(), glm::vec3{0.0f, 1.0f, 0.0f}); | ||
} | ||
|
||
glm::vec3 EditorCamera::GetRightDirection() const { | ||
return glm::rotate(GetOrientation(), glm::vec3(1.0f, 0.0f, 0.0f)); | ||
} | ||
|
||
glm::vec3 EditorCamera::GetForwardDirection() const { | ||
return glm::rotate(GetOrientation(), glm::vec3(0.0f, 0.0f, -1.0f)); | ||
} | ||
|
||
|
||
//! @note Getting pans. | ||
void EditorCamera::CameraPan(const glm::vec2& p_Data){} | ||
|
||
void EditorCamera::CameraZoom(float delta) { | ||
// float dt = SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime; | ||
m_Distance -= (delta * ZoomSpeed()); | ||
|
||
if(m_Distance < 1.0f){ | ||
m_FocalPoint += GetForwardDirection(); | ||
m_Distance = 1.0f; | ||
} | ||
} | ||
|
||
void EditorCamera::CameraRotate(const glm::vec2& p_Position) { | ||
float yawSign = GetUpDirection().y < 0 ? -1.0f : 1.0f; | ||
m_Yaw += yawSign * p_Position.x * RotationSpeed(); | ||
m_Pitch += p_Position.y * RotationSpeed(); | ||
} | ||
|
||
float EditorCamera::RotationSpeed() const { | ||
return 0.8f; | ||
} | ||
|
||
glm::vec3 EditorCamera::CalculateNewPosition() const{ | ||
return m_FocalPoint - GetForwardDirection() * m_Distance; | ||
} | ||
|
||
float EditorCamera::ZoomSpeed() const{ | ||
float distance = m_Distance * 0.2f; | ||
distance = std::max(distance, 0.0f); | ||
|
||
float speed = distance * distance; | ||
speed = std::min(speed, 100.0f); | ||
return speed; | ||
} | ||
|
||
void EditorCamera::RecalculateProjection() { | ||
m_ProjectionMatrix = glm::perspective(glm::radians(m_Fov), m_AspectRatio, m_NearClip, m_FarClip); | ||
RecalculateView(); | ||
} | ||
|
||
void EditorCamera::RecalculateView() { | ||
m_Position = CalculateNewPosition(); | ||
glm::quat orientation = GetOrientation(); | ||
m_ViewMatrix = glm::translate(glm::mat4(1.0f), m_Position) * glm::toMat4(orientation); | ||
m_ViewMatrix = glm::inverse(m_ViewMatrix); | ||
} | ||
|
||
glm::vec3 EditorCamera::RecalculatePosition() const { | ||
return m_FocalPoint * GetForwardDirection() * m_Distance; | ||
|
||
} | ||
|
||
glm::vec2 EditorCamera::PanSpeed() const{ | ||
float x = std::min(m_ViewportSize.x / 1000.0f, 2.4f); // max = 2.4.f | ||
float xFactor = 0.0366f * (x * x) - 0.1778f * x + 0.3021f; | ||
|
||
float y = std::min(m_ViewportSize.y / 1000.0f, 2.4f); // max = 2.4f | ||
float yFactor = 0.0366f * (y * y) - 0.1778f * y + 0.3021f; | ||
|
||
return {xFactor, yFactor}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters