Merge pull request #101 from SHADE-DP/SP3-141-Camera-System

Sp3 141 camera system
Done with editor camera
This commit is contained in:
XiaoQiDigipen 2022-10-20 11:18:57 +08:00 committed by GitHub
commit 2db6644536
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 83 additions and 84 deletions

View File

@ -1,16 +1,16 @@
[Window][MainStatusBar]
Pos=0,1389
Size=2547,20
Pos=0,1060
Size=1920,20
Collapsed=0
[Window][SHEditorMenuBar]
Pos=0,48
Size=2547,1341
Size=1920,1012
Collapsed=0
[Window][Hierarchy Panel]
Pos=0,172
Size=571,1217
Pos=0,142
Size=571,918
Collapsed=0
DockId=0x00000004,0
@ -20,25 +20,25 @@ Size=400,400
Collapsed=0
[Window][Inspector]
Pos=2276,48
Size=271,1341
Pos=1649,48
Size=271,1012
Collapsed=0
DockId=0x00000006,0
[Window][Profiler]
Pos=0,48
Size=571,122
Size=571,92
Collapsed=0
DockId=0x00000003,0
[Window][Viewport]
Pos=573,48
Size=1701,1341
Size=1074,1012
Collapsed=0
DockId=0x00000002,0
[Docking][Data]
DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=8,79 Size=2547,1341 Split=X
DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=8,79 Size=1920,1012 Split=X
DockNode ID=0x00000005 Parent=0xC5C9B8AB SizeRef=1992,1036 Split=X
DockNode ID=0x00000001 Parent=0x00000005 SizeRef=571,1036 Split=Y Selected=0x1E6EB881
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=225,94 Selected=0x1E6EB881

View File

@ -7,7 +7,7 @@ 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)
, 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)
, viewMatrix(), projMatrix()
, position()

View File

@ -2,6 +2,7 @@
#include "SHCameraSystem.h"
#include "Math/SHMathHelpers.h"
#include "Input/SHInputManager.h"
#include "Math/Vector/SHVec2.h"
@ -12,56 +13,51 @@ namespace SHADE
{
SHCameraSystem* system = static_cast<SHCameraSystem*>(GetSystem());
auto& camera = system->editorCamera;
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));
target += camera.position;
////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(view, right);
SHVec3 view, right, UP;
system->GetCameraAxis(camera, view, right, UP);
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::A))
{
system->editorCamera.position -= right * dt * camera.movementSpeed;
system->editorCamera.dirtyView = true;
camera.position -= right * dt * camera.movementSpeed;
camera.dirtyView = true;
}
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::D))
{
system->editorCamera.position += right * dt * camera.movementSpeed;
system->editorCamera.dirtyView = true;
camera.position += right * dt * camera.movementSpeed;
camera.dirtyView = true;
}
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::W))
{
system->editorCamera.position += view * dt * camera.movementSpeed;
system->editorCamera.dirtyView = true;
camera.position += view * dt * camera.movementSpeed;
camera.dirtyView = true;
}
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::S))
{
system->editorCamera.position -= view * dt * camera.movementSpeed;
system->editorCamera.dirtyView = true;
camera.position -= view * dt * camera.movementSpeed;
camera.dirtyView = true;
}
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::Q))
{
system->editorCamera.position += UP * dt * camera.movementSpeed;
system->editorCamera.dirtyView = true;
camera.position += UP * dt * camera.movementSpeed;
camera.dirtyView = true;
}
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::E))
{
system->editorCamera.position -= UP * dt * camera.movementSpeed;
system->editorCamera.dirtyView = true;
camera.position -= UP * dt * camera.movementSpeed;
camera.dirtyView = true;
}
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::RMB))
{
double mouseX, mouseY;
SHInputManager::GetMouseVelocity(&mouseX,&mouseY);
//std::cout << camera.yaw << std::endl;
camera.pitch -= mouseY * dt * camera.turnSpeed.x;
camera.yaw -= mouseX * dt * camera.turnSpeed.y;
camera.dirtyView = true;
}
system->UpdateCameraComponent(system->editorCamera);
}
@ -89,24 +85,9 @@ namespace SHADE
{
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));
target += camera.position;
////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(view, right);
SHVec3 view, right, UP;
GetCameraAxis(camera, view, right, UP);
camera.viewMatrix = SHMatrix::Identity;
camera.viewMatrix(0, 0) = right[0];
@ -142,38 +123,51 @@ namespace SHADE
camera.projMatrix(3, 2) = 1.0f;
camera.projMatrix(2, 3) = -(camera.zFar * camera.zNear) / (camera.zFar - camera.zNear);
//const float fov_rad = SHMath::DegreesToRadians(camera.fov);
//const float focal_length = 1.0f / tan(fov_rad * 0.5f);
//camera.projMatrix(0,0) = focal_length / camera.GetAspectRatio();
//camera.projMatrix(1,1) = -focal_length;
//camera.projMatrix(2,2) = camera.zNear / (camera.zFar - camera.zNear);
//camera.projMatrix(2,3) = camera.zFar * (camera.zNear / (camera.zFar - camera.zNear));
//camera.projMatrix(3,2) = -1.0f;
//camera.projMatrix(3,3) = 0.0f;
//camera.projMatrix = SHMatrix::Inverse(camera.projMatrix);
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;
//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.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;
}
}
}
void SHCameraSystem::GetCameraAxis(SHCameraComponent const& camera, SHVec3& forward, SHVec3& right, SHVec3& upVec) const noexcept
{
SHVec3 target{ 0.0f,0.0f,-1.0f };
SHVec3 up = { 0.0f,1.0f,0.0f };
target = SHVec3::RotateY(target, SHMath::DegreesToRadians(camera.yaw));
target =SHVec3::RotateX(target, SHMath::DegreesToRadians(camera.pitch));
std::cout << "Target vec: " << target.x<<", "<<target.y<<", "<<target.z << std::endl;
target += camera.position;
////SHVec3::RotateZ(target, SHMath::DegreesToRadians(camera.roll));
//target = SHVec3::Normalise(target);
SHVec3::RotateZ(up, camera.roll);
up = SHVec3::Normalise(up);
forward = target - camera.position; forward = SHVec3::Normalise(forward);
right = SHVec3::Cross(forward, up); right = SHVec3::Normalise(right);
upVec = SHVec3::Cross(forward, right);
}
}

View File

@ -40,6 +40,8 @@ namespace SHADE
void UpdateCameraComponent(SHCameraComponent& camera) noexcept;
void GetCameraAxis(SHCameraComponent const& camera, SHVec3& forward, SHVec3& right, SHVec3& up) const noexcept;
};

View File

@ -48,6 +48,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.
@ -59,7 +62,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;
}