diff --git a/SHADE_Engine/src/Camera/SHCameraComponent.cpp b/SHADE_Engine/src/Camera/SHCameraComponent.cpp new file mode 100644 index 00000000..9b84081f --- /dev/null +++ b/SHADE_Engine/src/Camera/SHCameraComponent.cpp @@ -0,0 +1,130 @@ +#include "SHpch.h" +#include "SHCameraComponent.h" + + +namespace SHADE +{ + SHCameraComponent::SHCameraComponent() + :yaw(0.0f), pitch(0.0f), roll(0.0f) + , width(1920.0f), height(1080.0f), zNear(0.01f), zFar(10000.0f), fov(90.0f), movementSpeed(1.0f), turnSpeed(1.0f) + , perspProj(false), dirtyView(true), dirtyProj(true) + , viewMatrix(), projMatrix() + , position() + { + + } + + SHCameraComponent::~SHCameraComponent() + { + } + + void SHCameraComponent::SetYaw(float yaw) noexcept + { + this->yaw = yaw; + dirtyView = true; + } + + void SHCameraComponent::SetPitch(float pitch) noexcept + { + this->pitch = pitch; + dirtyView = true; + } + + void SHCameraComponent::SetRoll(float roll) noexcept + { + this->roll = roll; + dirtyView = true; + } + void SHCameraComponent::SetPositionX(float x) noexcept + { + position[0] = x; + dirtyView = true; + } + void SHCameraComponent::SetPositionY(float y) noexcept + { + position[1] = y; + dirtyView = true; + } + void SHCameraComponent::SetPositionZ(float z) noexcept + { + position[2] = z; + dirtyView = true; + } + void SHCameraComponent::SetPosition(float x,float y, float z) noexcept + { + position[0] = x; + position[1] = y; + position[2] = z; + dirtyView = true; + } + void SHCameraComponent::SetPosition(SHVec3& pos) noexcept + { + this->position = pos; + dirtyView = true; + } + + void SHCameraComponent::SetWidth(float width) noexcept + { + this->width = width; + dirtyProj = true; + } + + + void SHCameraComponent::SetHeight(float height) noexcept + { + this->height = height; + dirtyProj = true; + } + + void SHCameraComponent::SetNear(float znear) noexcept + { + this->zNear = znear; + dirtyProj = true; + } + + void SHCameraComponent::SetFar(float zFar) noexcept + { + this->zFar = zFar; + dirtyProj = true; + } + + void SHCameraComponent::SetFOV(float fov) noexcept + { + this->fov = fov; + dirtyProj = true; + } + + float SHCameraComponent::GetYaw() const noexcept + { + return yaw; + } + + float SHCameraComponent::GetPitch() const noexcept + { + return pitch; + } + float SHCameraComponent::GetRoll() const noexcept + { + return roll; + } + float SHCameraComponent::GetAspectRatio() const noexcept + { + return width/height; + } + + float SHCameraComponent::GetFOV() const noexcept + { + return fov; + } + + const SHMatrix& SHCameraComponent::GetViewMatrix() const noexcept + { + return viewMatrix; + } + + const SHMatrix& SHCameraComponent::GetProjMatrix() const noexcept + { + return projMatrix; + } + +} diff --git a/SHADE_Engine/src/Camera/SHCameraComponent.h b/SHADE_Engine/src/Camera/SHCameraComponent.h new file mode 100644 index 00000000..acfc9740 --- /dev/null +++ b/SHADE_Engine/src/Camera/SHCameraComponent.h @@ -0,0 +1,85 @@ +#pragma once + +#include "ECS_Base/Components/SHComponent.h" +#include "Math/Vector/SHVec3.h" +#include "Math/SHMatrix.h" +#include "SH_API.h" + +namespace SHADE +{ + + class SH_API SHCameraComponent : public SHComponent + { + private: + + float yaw; + float pitch; + float roll; + + float width; + float height; + float zNear; + float zFar; + float fov; + + bool dirtyView; + bool dirtyProj; + + + SHMatrix viewMatrix; + SHMatrix projMatrix; + SHVec3 position; + + bool perspProj; + + + + + public: + friend class SHCameraSystem; + + SHCameraComponent(); + ~SHCameraComponent(); + + + //Getters and setters. + void SetYaw(float yaw) noexcept; + void SetPitch(float pitch) noexcept; + void SetRoll(float roll) noexcept; + void SetPositionX(float x) noexcept; + void SetPositionY(float y) noexcept; + void SetPositionZ(float z) noexcept; + void SetPosition(float x, float y, float z) noexcept; + void SetPosition(SHVec3& pos) noexcept; + + void SetWidth(float width) noexcept; + void SetHeight(float height) noexcept; + void SetNear(float znear) noexcept; + void SetFar(float zfar) noexcept; + void SetFOV(float fov) noexcept; + + + + float GetYaw() const noexcept; + float GetPitch() const noexcept; + float GetRoll() const noexcept; + + float GetAspectRatio() const noexcept; + float GetFOV() const noexcept; + + const SHMatrix& GetViewMatrix() const noexcept; + const SHMatrix& GetProjMatrix() const noexcept; + + + float movementSpeed; + SHVec3 turnSpeed; + + protected: + + + + + }; + + +} diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.cpp b/SHADE_Engine/src/Camera/SHCameraSystem.cpp new file mode 100644 index 00000000..24d96028 --- /dev/null +++ b/SHADE_Engine/src/Camera/SHCameraSystem.cpp @@ -0,0 +1,95 @@ +#include "SHpch.h" +#include "SHCameraSystem.h" +#include "Math/SHMathHelpers.h" +#include "Input/SHInputManagerSystem.h" + + + +namespace SHADE +{ + + void SHCameraSystem::EditorCameraUpdate::Execute(double dt) noexcept + { + SHCameraSystem* system = static_cast(GetSystem()); + + if (SHInputManagerSystem::GetKey(SHInputManagerSystem::SH_KEYCODE::A)) + { + system->editorCamera.SetPositionX(system->editorCamera.position[0] - dt * system->editorCamera.movementSpeed); + + } + system->UpdateCameraComponent(system->editorCamera); + } + + void SHCameraSystem::UpdateCameraComponent(SHCameraComponent& camera) noexcept + { + if (camera.dirtyView) + { + SHVec3 target{ 0.0f,0.0f,1.0f }; + SHVec3 up = { 0.0f,1.0f,0.0f }; + + SHVec3::RotateY(target, SHMath::DegreesToRadians(camera.yaw)); + SHVec3::RotateX(target, SHMath::DegreesToRadians(camera.pitch)); + //SHVec3::RotateZ(target, SHMath::DegreesToRadians(camera.roll)); + + target = SHVec3::Normalise(target); + + SHVec3::RotateZ(up, camera.roll); + up = SHVec3::Normalise(up); + + + SHVec3 view = target - camera.position; view = SHVec3::Normalise(view); + SHVec3 right = SHVec3::Cross(view, up); right = SHVec3::Normalise(right); + const SHVec3 UP = SHVec3::Cross(right, view); + + camera.viewMatrix = SHMatrix::Identity; + camera.viewMatrix(0, 0) = UP[0]; + camera.viewMatrix(1, 0) = UP[1]; + camera.viewMatrix(2, 0) = UP[2]; + camera.viewMatrix(0, 1) = right[0]; + camera.viewMatrix(1, 1) = right[1]; + camera.viewMatrix(2, 1) = right[2]; + camera.viewMatrix(0, 2) = view[0]; + camera.viewMatrix(1, 2) = view[1]; + camera.viewMatrix(2, 2) = view[2]; + camera.viewMatrix(3, 0) = -UP.Dot(camera.position); + camera.viewMatrix(3, 1) = -right.Dot(camera.position); + camera.viewMatrix(3, 2) = -view.Dot(camera.position); + + camera.dirtyView = false; + } + if (camera.dirtyProj == true) + { + if (camera.perspProj == true) + { + const float ASPECT_RATIO = camera.GetAspectRatio(); + const float TAN_HALF_FOV = tan(camera.fov * 0.5f); + camera.projMatrix = SHMatrix::Identity; + camera.projMatrix(0, 0) = 1.0f / (ASPECT_RATIO * TAN_HALF_FOV); + camera.projMatrix(1, 1) = 1.0f / TAN_HALF_FOV; + camera.projMatrix(2, 2) = camera.zFar / (camera.zFar - camera.zNear); + camera.projMatrix(2, 3) = 1.0f; + camera.projMatrix(3, 2) = -(camera.zFar * camera.zNear) / (camera.zFar - camera.zNear); + + camera.dirtyProj = false; + } + else + { + const float R = camera.width * 0.5f; + const float L = -R; + const float T = camera.height * 0.5f; + const float B = -T; + + camera.projMatrix = SHMatrix::Identity; + camera.projMatrix(0, 0) = 2.0f / (R - L); + camera.projMatrix(1, 1) = 2.0f / (B - T); + camera.projMatrix(2, 2) = 1.0f / (camera.zFar - camera.zNear); + camera.projMatrix(3, 0) = -(R + L) / (R - L); + camera.projMatrix(3, 1) = -(B + T) / (B - T); + camera.projMatrix(3, 2) = -camera.zNear / (camera.zFar - camera.zNear); + + camera.dirtyProj = false; + } + } + } + +} diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.h b/SHADE_Engine/src/Camera/SHCameraSystem.h new file mode 100644 index 00000000..7742078d --- /dev/null +++ b/SHADE_Engine/src/Camera/SHCameraSystem.h @@ -0,0 +1,40 @@ +#pragma once + +#include "ECS_Base/System/SHSystem.h" +#include "SHCameraComponent.h" +#include "ECS_Base/System/SHSystemRoutine.h" + + +namespace SHADE +{ + class SHCameraSystem :public SHSystem + { + private: + //A camera component that represents editor camera. + //This is not tied to any entity. Hence this EID should not be used. + SHCameraComponent editorCamera; + + + + public: + + class EditorCameraUpdate final : public SHSystemRoutine + { + public: + + EditorCameraUpdate() : SHSystemRoutine("Editor Camera Update", true) { }; + virtual void Execute(double dt) noexcept override final; + + }; + friend class EditorCameraUpdate; + + + protected: + + void UpdateCameraComponent(SHCameraComponent& camera) noexcept; + + }; + + + +} diff --git a/SHADE_Engine/src/ECS_Base/Components/SHComponent.h b/SHADE_Engine/src/ECS_Base/Components/SHComponent.h index aba3ba51..865f3078 100644 --- a/SHADE_Engine/src/ECS_Base/Components/SHComponent.h +++ b/SHADE_Engine/src/ECS_Base/Components/SHComponent.h @@ -47,6 +47,9 @@ namespace SHADE { } + + + public: //Whether or not this component is active. //Systems using this component should are responsible for checking the active state of the component before running their functionality. @@ -58,7 +61,7 @@ namespace SHADE * \return uint32_t * The entityID that this component belongs to. ***************************************************************************/ - uint32_t GetEID()const + uint32_t GetEID()const noexcept { return this->entityID; }