Merge pull request #221 from SHADE-DP/SP3-141-Camera-System
Separated Ortho and perspective matrices in camera
This commit is contained in:
commit
84dc63d6d3
|
@ -12,7 +12,7 @@ namespace SHADE
|
||||||
:yaw(0.0f), pitch(0.0f), roll(0.0f)
|
: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(0.5f)
|
, width(1920.0f), height(1080.0f), zNear(0.01f), zFar(10000.0f), fov(90.0f), movementSpeed(1.0f), turnSpeed(0.5f)
|
||||||
, perspProj(true), dirtyView(true), dirtyProj(true)
|
, perspProj(true), dirtyView(true), dirtyProj(true)
|
||||||
, viewMatrix(), projMatrix()
|
, viewMatrix(), perspProjMatrix(), orthoProjMatrix()
|
||||||
, position(), offset()
|
, position(), offset()
|
||||||
{
|
{
|
||||||
ComponentFamily::GetID<SHCameraComponent>();
|
ComponentFamily::GetID<SHCameraComponent>();
|
||||||
|
@ -213,7 +213,20 @@ namespace SHADE
|
||||||
|
|
||||||
const SHMatrix& SHCameraComponent::GetProjMatrix() const noexcept
|
const SHMatrix& SHCameraComponent::GetProjMatrix() const noexcept
|
||||||
{
|
{
|
||||||
return projMatrix;
|
if (perspProj)
|
||||||
|
return perspProjMatrix;
|
||||||
|
else
|
||||||
|
return orthoProjMatrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
const SHMatrix& SHCameraComponent::GetOrthoMatrix() const noexcept
|
||||||
|
{
|
||||||
|
return orthoProjMatrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
const SHMatrix& SHCameraComponent::GetPerspectiveMatrix() const noexcept
|
||||||
|
{
|
||||||
|
return orthoProjMatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
//void SHCameraComponent::SetMainCamera(size_t directorCameraIndex) noexcept
|
//void SHCameraComponent::SetMainCamera(size_t directorCameraIndex) noexcept
|
||||||
|
|
|
@ -29,7 +29,8 @@ namespace SHADE
|
||||||
|
|
||||||
|
|
||||||
SHMatrix viewMatrix;
|
SHMatrix viewMatrix;
|
||||||
SHMatrix projMatrix;
|
SHMatrix perspProjMatrix;
|
||||||
|
SHMatrix orthoProjMatrix;
|
||||||
SHVec3 position;
|
SHVec3 position;
|
||||||
|
|
||||||
bool perspProj;
|
bool perspProj;
|
||||||
|
@ -37,6 +38,7 @@ namespace SHADE
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
friend class SHCameraSystem;
|
friend class SHCameraSystem;
|
||||||
|
|
||||||
|
@ -78,6 +80,8 @@ namespace SHADE
|
||||||
|
|
||||||
const SHMatrix& GetViewMatrix() const noexcept;
|
const SHMatrix& GetViewMatrix() const noexcept;
|
||||||
const SHMatrix& GetProjMatrix() const noexcept;
|
const SHMatrix& GetProjMatrix() const noexcept;
|
||||||
|
const SHMatrix& GetOrthoMatrix() const noexcept;
|
||||||
|
const SHMatrix& GetPerspectiveMatrix() const noexcept;
|
||||||
|
|
||||||
//void SetMainCamera(size_t cameraDirectorIndex = 0) noexcept;
|
//void SetMainCamera(size_t cameraDirectorIndex = 0) noexcept;
|
||||||
|
|
||||||
|
|
|
@ -15,27 +15,27 @@ namespace SHADE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SHMatrix SHCameraDirector::GetViewMatrix() const noexcept
|
SHMatrix const& SHCameraDirector::GetViewMatrix() const noexcept
|
||||||
{
|
{
|
||||||
return viewMatrix;
|
return viewMatrix;
|
||||||
}
|
}
|
||||||
SHMatrix SHCameraDirector::GetProjMatrix() const noexcept
|
SHMatrix const& SHCameraDirector::GetProjMatrix() const noexcept
|
||||||
{
|
{
|
||||||
return projMatrix;
|
return projMatrix;
|
||||||
}
|
}
|
||||||
SHMatrix SHCameraDirector::GetVPMatrix() const noexcept
|
SHMatrix const& SHCameraDirector::GetVPMatrix() const noexcept
|
||||||
{
|
{
|
||||||
return projMatrix * viewMatrix;
|
return projMatrix * viewMatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SHCameraDirector::UpdateMatrix() noexcept
|
SHCameraComponent* SHCameraDirector::GetMainCameraComponent() noexcept
|
||||||
{
|
{
|
||||||
if (mainCameraEID == MAX_EID)
|
if (mainCameraEID == MAX_EID)
|
||||||
{
|
{
|
||||||
auto& dense = SHComponentManager::GetDense<SHCameraComponent>();
|
auto& dense = SHComponentManager::GetDense<SHCameraComponent>();
|
||||||
if (dense.size() == 0)
|
if (dense.size() == 0)
|
||||||
{
|
{
|
||||||
return;
|
return nullptr;
|
||||||
}
|
}
|
||||||
mainCameraEID = dense[0].GetEID();
|
mainCameraEID = dense[0].GetEID();
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,13 @@ namespace SHADE
|
||||||
{
|
{
|
||||||
SHLOG_WARNING("Camera Director warning: Entity does not have a camera");
|
SHLOG_WARNING("Camera Director warning: Entity does not have a camera");
|
||||||
}
|
}
|
||||||
else
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void SHCameraDirector::UpdateMatrix() noexcept
|
||||||
|
{
|
||||||
|
SHCameraComponent* camComponent = GetMainCameraComponent();
|
||||||
|
if(camComponent)
|
||||||
{
|
{
|
||||||
viewMatrix = camComponent->GetViewMatrix();
|
viewMatrix = camComponent->GetViewMatrix();
|
||||||
projMatrix = camComponent->GetProjMatrix();
|
projMatrix = camComponent->GetProjMatrix();
|
||||||
|
@ -62,6 +68,24 @@ namespace SHADE
|
||||||
mainCameraEID = camera.GetEID();
|
mainCameraEID = camera.GetEID();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SHMatrix const& SHCameraDirector::GetOrthoMatrix() noexcept
|
||||||
|
{
|
||||||
|
|
||||||
|
SHCameraComponent* camComponent = GetMainCameraComponent();
|
||||||
|
if (camComponent)
|
||||||
|
return camComponent->GetOrthoMatrix();
|
||||||
|
else
|
||||||
|
return SHMatrix::Identity;
|
||||||
|
}
|
||||||
|
|
||||||
|
SHMatrix const& SHCameraDirector::GetPerspectiveMatrix() noexcept
|
||||||
|
{
|
||||||
|
SHCameraComponent* camComponent = GetMainCameraComponent();
|
||||||
|
if (camComponent)
|
||||||
|
return camComponent->GetPerspectiveMatrix();
|
||||||
|
else
|
||||||
|
return SHMatrix::Identity;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,20 +23,20 @@ namespace SHADE
|
||||||
EntityID transitionCameraEID;
|
EntityID transitionCameraEID;
|
||||||
|
|
||||||
|
|
||||||
SHMatrix GetViewMatrix() const noexcept;
|
SHMatrix const& GetViewMatrix() const noexcept;
|
||||||
SHMatrix GetProjMatrix() const noexcept;
|
SHMatrix const& GetProjMatrix() const noexcept;
|
||||||
SHMatrix GetVPMatrix() const noexcept;
|
SHMatrix const& GetVPMatrix() const noexcept;
|
||||||
void UpdateMatrix() noexcept;
|
void UpdateMatrix() noexcept;
|
||||||
void SetMainCamera(SHCameraComponent& cam) noexcept;
|
void SetMainCamera(SHCameraComponent& cam) noexcept;
|
||||||
|
SHMatrix const& GetOrthoMatrix() noexcept;
|
||||||
|
SHMatrix const& GetPerspectiveMatrix() noexcept;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
||||||
protected:
|
|
||||||
SHMatrix viewMatrix;
|
SHMatrix viewMatrix;
|
||||||
SHMatrix projMatrix;
|
SHMatrix projMatrix;
|
||||||
|
|
||||||
|
SHCameraComponent* GetMainCameraComponent() noexcept;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef Handle<SHCameraDirector> DirectorHandle;
|
typedef Handle<SHCameraDirector> DirectorHandle;
|
||||||
|
|
|
@ -107,7 +107,11 @@ namespace SHADE
|
||||||
editorCamera.SetPitch(0.0f);
|
editorCamera.SetPitch(0.0f);
|
||||||
editorCamera.SetYaw(0.0f);
|
editorCamera.SetYaw(0.0f);
|
||||||
editorCamera.SetRoll(0.0f);
|
editorCamera.SetRoll(0.0f);
|
||||||
|
editorCamera.SetWidth(1080.0f);
|
||||||
|
editorCamera.SetHeight(720.0f);
|
||||||
|
editorCamera.SetFar(10000000.0f);
|
||||||
editorCamera.movementSpeed = 2.0f;
|
editorCamera.movementSpeed = 2.0f;
|
||||||
|
editorCamera.perspProj = true;
|
||||||
|
|
||||||
SHComponentManager::CreateComponentSparseSet<SHCameraComponent>();
|
SHComponentManager::CreateComponentSparseSet<SHCameraComponent>();
|
||||||
SHComponentManager::CreateComponentSparseSet<SHCameraArmComponent>();
|
SHComponentManager::CreateComponentSparseSet<SHCameraArmComponent>();
|
||||||
|
@ -210,39 +214,43 @@ namespace SHADE
|
||||||
}
|
}
|
||||||
if (camera.dirtyProj == true)
|
if (camera.dirtyProj == true)
|
||||||
{
|
{
|
||||||
if (camera.perspProj == true)
|
//Perspective projection matrix.
|
||||||
{
|
|
||||||
const float ASPECT_RATIO = (camera.GetAspectRatio());
|
const float ASPECT_RATIO = (camera.GetAspectRatio());
|
||||||
const float TAN_HALF_FOV = tan(SHMath::DegreesToRadians(camera.fov) * 0.5f);
|
const float TAN_HALF_FOV = tan(SHMath::DegreesToRadians(camera.fov) * 0.5f);
|
||||||
camera.projMatrix = SHMatrix::Identity;
|
camera.perspProjMatrix = SHMatrix::Identity;
|
||||||
camera.projMatrix(0, 0) = 1.0f / (ASPECT_RATIO * TAN_HALF_FOV);
|
camera.perspProjMatrix(0, 0) = 1.0f / (ASPECT_RATIO * TAN_HALF_FOV);
|
||||||
camera.projMatrix(1, 1) = 1.0f / TAN_HALF_FOV;
|
camera.perspProjMatrix(1, 1) = 1.0f / TAN_HALF_FOV;
|
||||||
camera.projMatrix(2, 2) = camera.zFar / (camera.zFar - camera.zNear);
|
camera.perspProjMatrix(2, 2) = camera.zFar / (camera.zFar - camera.zNear);
|
||||||
camera.projMatrix(3, 3) = 0.0f;
|
camera.perspProjMatrix(3, 3) = 0.0f;
|
||||||
|
|
||||||
camera.projMatrix(3, 2) = 1.0f;
|
camera.perspProjMatrix(3, 2) = 1.0f;
|
||||||
camera.projMatrix(2, 3) = -(camera.zFar * camera.zNear) / (camera.zFar - camera.zNear);
|
camera.perspProjMatrix(2, 3) = -(camera.zFar * camera.zNear) / (camera.zFar - camera.zNear);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//Orthographic projection matrix
|
||||||
|
|
||||||
|
const float right = camera.GetWidth() * 0.5f;
|
||||||
|
const float left = -right;
|
||||||
|
const float top = camera.GetHeight() * 0.5f;
|
||||||
|
const float btm = -top;
|
||||||
|
const float n = camera.GetNear();
|
||||||
|
const float f = camera.GetFar();
|
||||||
|
|
||||||
|
camera.orthoProjMatrix = SHMatrix::Identity;
|
||||||
|
camera.orthoProjMatrix(0, 0) = 2.0f / (right - left);
|
||||||
|
camera.orthoProjMatrix(1, 1) = 2.0f / (btm - top);
|
||||||
|
camera.orthoProjMatrix(2, 2) = 1.0f / (f-n);
|
||||||
|
camera.orthoProjMatrix(0, 3) = -(right + left) / (right - left);
|
||||||
|
camera.orthoProjMatrix(1, 3) = -(btm + top) / (btm - top);
|
||||||
|
camera.orthoProjMatrix(2, 3) = -n / (f-n);
|
||||||
|
camera.orthoProjMatrix(3, 3) = 1.0f;
|
||||||
|
|
||||||
|
camera.orthoProjMatrix = SHMatrix::OrthographicRH(camera.GetWidth(), camera.GetHeight(), camera.GetNear(), camera.GetFar());
|
||||||
|
//camera.projMatrix.Transpose();
|
||||||
|
|
||||||
camera.dirtyProj = false;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -252,8 +260,6 @@ namespace SHADE
|
||||||
SHVec3 up = { 0.0f,1.0f,0.0f };
|
SHVec3 up = { 0.0f,1.0f,0.0f };
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
target = SHVec3::RotateX(target, SHMath::DegreesToRadians(camera.pitch));
|
target = SHVec3::RotateX(target, SHMath::DegreesToRadians(camera.pitch));
|
||||||
target = SHVec3::RotateY(target, SHMath::DegreesToRadians(camera.yaw));
|
target = SHVec3::RotateY(target, SHMath::DegreesToRadians(camera.yaw));
|
||||||
target += camera.position;
|
target += camera.position;
|
||||||
|
@ -287,6 +293,9 @@ namespace SHADE
|
||||||
if (SHSceneManager::CheckNodeAndComponentsActive<SHCameraComponent>(cam.GetEID()))
|
if (SHSceneManager::CheckNodeAndComponentsActive<SHCameraComponent>(cam.GetEID()))
|
||||||
system->UpdateCameraComponent(cam);
|
system->UpdateCameraComponent(cam);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for (auto& handle : system->directorHandleList)
|
for (auto& handle : system->directorHandleList)
|
||||||
{
|
{
|
||||||
handle->UpdateMatrix();
|
handle->UpdateMatrix();
|
||||||
|
|
|
@ -218,7 +218,7 @@ namespace SHADE
|
||||||
EntityID result = MAX_EID;
|
EntityID result = MAX_EID;
|
||||||
for (auto& entity : entityVec)
|
for (auto& entity : entityVec)
|
||||||
{
|
{
|
||||||
if (entity->name == name)
|
if (entity && entity->name == name)
|
||||||
result = entity->GetEID();
|
result = entity->GetEID();
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|
Loading…
Reference in New Issue