Skip to content

Commit

Permalink
Merge pull request #53 from vilbeyli/dev
Browse files Browse the repository at this point in the history
Keyboard/Mouse events + Camera movement
  • Loading branch information
vilbeyli authored Jul 21, 2020
2 parents f4a6a90 + f65ed79 commit 202c9a4
Show file tree
Hide file tree
Showing 27 changed files with 2,232 additions and 664 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ set (HeaderVQE
"Source/Application/Window.h"
"Source/Application/Settings.h"
"Source/Application/Types.h"
"Source/Application/Math.h"
"Source/Application/VQEngine.h"
"Source/Application/Events.h"
"Source/Application/Mesh.h"
"Source/Application/Geometry.h"
"Source/Application/Transform.h"
"Source/Application/Quaternion.h"
"Source/Application/Camera.h"
"Source/Application/Input.h"
)

set (SourceVQE
Expand All @@ -56,12 +58,15 @@ set (SourceVQE
"Source/Application/VQEngine_Render.cpp"
"Source/Application/VQEngine_Update.cpp"
"Source/Application/VQEngine_WindowEvents.cpp"
"Source/Application/VQEngine_EventHandlers.cpp"
"Source/Application/Events.cpp"
"Source/Application/Mesh.cpp"
"Source/Application/Geometry.cpp"
"Source/Application/Transform.cpp"
"Source/Application/Math.cpp"
"Source/Application/Quaternion.cpp"
"Source/Application/Camera.cpp"
"Source/Application/Input.cpp"
)

add_link_options(/SUBSYSTEM:WINDOWS)
Expand Down
6 changes: 3 additions & 3 deletions Data/EngineSettings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ RenderScale=1.0
TripleBuffer=true

[Engine]
Width=1600
Height=900
Width=768
Height=432
DisplayMode=BorderlessFullscreen
PreferredDisplay=0

DebugWindow=true
DebugWindowWidth=450
DebugWindowHeight=450
DebugWindowDisplayMode=Windowed
DebugWindowDisplayMode=BorderlessFullscreen
DebugWindowPreferredDisplay=1
8 changes: 4 additions & 4 deletions Data/Resources/VQE.rc
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ BEGIN
BLOCK "040904b0"
BEGIN
VALUE "CompanyName", "Volkan Ilbeyli @vilbeyli - GitHub/VQE"
VALUE "FileDescription", "VQE Application"
VALUE "FileVersion", "0.1.0.0"
VALUE "FileDescription", "VQEngine Application"
VALUE "FileVersion", "0.3.0.0"
VALUE "InternalName", "VQE.exe"
VALUE "OriginalFilename", "VQE.exe"
VALUE "ProductName", "VQE"
VALUE "ProductVersion", "0.1.0.0"
VALUE "ProductName", "VQEngine"
VALUE "ProductVersion", "0.3.0.0"
END
END
BLOCK "VarFileInfo"
Expand Down
142 changes: 47 additions & 95 deletions Source/Application/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@

#include "Camera.h"


// TODO: remove duplicate definition
#define DEG2RAD (DirectX::XM_PI / 180.0f)
#define RAD2DEG (180.0f / DirectX::XM_PI)
#define PI DirectX::XM_PI
#define PI_DIV2 DirectX::XM_PIDIV2

#define CAMERA_DEBUG 1

using namespace DirectX;
Expand All @@ -46,74 +39,49 @@ Camera::Camera()
Camera::~Camera(void)
{}

void Camera::InitializeCamera(const CameraData& data, int ViewportX, int ViewportY)
void Camera::InitializeCamera(const FCameraData& data)
{
const auto& NEAR_PLANE = data.nearPlane;
const auto& FAR_PLANE = data.farPlane;
const float AspectRatio = static_cast<float>(ViewportX) / ViewportY;
const float VerticalFoV = data.fovV_Degrees * DEG2RAD;
const auto& NEAR_PLANE = data.nearPlane;
const auto& FAR_PLANE = data.farPlane;
const float AspectRatio = data.width / data.height;
const float VerticalFoV = data.fovV_Degrees * DEG2RAD;
const float& ViewportX = data.width;
const float& ViewportY = data.height;

this->mProjParams.NearZ = NEAR_PLANE;
this->mProjParams.FarZ = FAR_PLANE;
this->mProjParams.ViewporHeight = ViewportY;
this->mProjParams.ViewporWidth = ViewportX;
this->mProjParams.FieldOfView = data.fovV_Degrees * DEG2RAD;
this->mProjParams.bPerspectiveProjection = data.bPerspectiveProjection;

//SetOthoMatrix(ViewportX, ViewportY, NEAR_PLANE, FAR_PLANE); // quick test
SetProjectionMatrix(VerticalFoV, AspectRatio, NEAR_PLANE, FAR_PLANE);
SetProjectionMatrix(this->mProjParams);

SetPosition(data.x, data.y, data.z);
mYaw = mPitch = 0;
Rotate(data.yaw * DEG2RAD, data.pitch * DEG2RAD, 1.0f);
}


void Camera::SetOthoMatrix(int screenWidth, int screenHeight, float screenNear, float screenFar)
void Camera::SetProjectionMatrix(const ProjectionMatrixParameters& params)
{
XMStoreFloat4x4(&mMatProj, XMMatrixOrthographicLH((float)screenWidth, (float)screenHeight, screenNear, screenFar));
}
assert(params.ViewporHeight > 0.0f);
const float AspectRatio = params.ViewporWidth / params.ViewporHeight;

void Camera::SetProjectionMatrix(float fovy, float screenAspect, float screenNear, float screenFar)
{
XMStoreFloat4x4(&mMatProj, XMMatrixPerspectiveFovLH(fovy, screenAspect, screenNear, screenFar));
mMatProj = params.bPerspectiveProjection
? MakePerspectiveProjectionMatrix(params.FieldOfView, AspectRatio, params.NearZ, params.FarZ)
: MakeOthographicProjectionMatrix(params.ViewporWidth, params.ViewporHeight, params.NearZ, params.FarZ);
}

void Camera::SetProjectionMatrixHFov(float fovx, float screenAspectInverse, float screenNear, float screenFar)
{ // horizonital FOV
const float FarZ = screenFar; float NearZ = screenNear;
const float r = screenAspectInverse;

const float Width = 1.0f / tanf(fovx*0.5f);
const float Height = Width / r;
const float fRange = FarZ / (FarZ - NearZ);

XMMATRIX M;
M.r[0].m128_f32[0] = Width;
M.r[0].m128_f32[1] = 0.0f;
M.r[0].m128_f32[2] = 0.0f;
M.r[0].m128_f32[3] = 0.0f;

M.r[1].m128_f32[0] = 0.0f;
M.r[1].m128_f32[1] = Height;
M.r[1].m128_f32[2] = 0.0f;
M.r[1].m128_f32[3] = 0.0f;

M.r[2].m128_f32[0] = 0.0f;
M.r[2].m128_f32[1] = 0.0f;
M.r[2].m128_f32[2] = fRange;
M.r[2].m128_f32[3] = 1.0f;

M.r[3].m128_f32[0] = 0.0f;
M.r[3].m128_f32[1] = 0.0f;
M.r[3].m128_f32[2] = -fRange * NearZ;
M.r[3].m128_f32[3] = 0.0f;
XMStoreFloat4x4(&mMatProj, M);
}


void Camera::Update(float dt)
void Camera::Update(const float dt, const FCameraInput& input)
{
Rotate(dt);
Move(dt);
Rotate(dt, input);
Move(dt, input);

XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
XMVECTOR lookAt = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
const XMVECTOR pos = XMLoadFloat3(&mPosition);
const XMMATRIX MRot = RotMatrix();
const XMMATRIX MRot = GetRotationMatrix();

//transform the lookat and up vector by rotation matrix
lookAt = XMVector3TransformCoord(lookAt, MRot);
Expand Down Expand Up @@ -148,7 +116,7 @@ XMMATRIX Camera::GetViewInverseMatrix() const
const XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
const XMVECTOR lookAt = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
const XMVECTOR pos = XMLoadFloat3(&mPosition);
const XMMATRIX MRot = RotMatrix();
const XMMATRIX MRot = GetRotationMatrix();

const XMVECTOR dir = XMVector3Normalize(lookAt - pos);
const XMVECTOR wing = XMVector3Cross(up, dir);
Expand Down Expand Up @@ -180,12 +148,12 @@ XMMATRIX Camera::GetProjectionMatrix() const
return XMLoadFloat4x4(&mMatProj);
}

FrustumPlaneset Camera::GetViewFrustumPlanes() const
FFrustumPlaneset Camera::GetViewFrustumPlanes() const
{
return FrustumPlaneset::ExtractFromMatrix(GetViewMatrix() * GetProjectionMatrix());
return FFrustumPlaneset::ExtractFromMatrix(GetViewMatrix() * GetProjectionMatrix());
}

XMMATRIX Camera::RotMatrix() const
XMMATRIX Camera::GetRotationMatrix() const
{
return XMMatrixRotationRollPitchYaw(mPitch, mYaw, 0.0f);
}
Expand All @@ -204,47 +172,31 @@ void Camera::Rotate(float yaw, float pitch, const float dt)
if (mPitch < -90.0f * DEG2RAD) mPitch = -90.0f * DEG2RAD;
}

#if 0
void Camera::Reset() // TODO: input
{
const Settings::Camera & data = m_settings;
SetPosition(data.x, data.y, data.z);
mYaw = mPitch = 0;
Rotate(data.yaw * DEG2RAD, data.pitch * DEG2RAD, 1.0f);
}

//void Camera::Reset() // TODO: input
//{
// const Settings::Camera & data = m_settings;
// SetPosition(data.x, data.y, data.z);
// mYaw = mPitch = 0;
// Rotate(data.yaw * DEG2RAD, data.pitch * DEG2RAD, 1.0f);
//}

// internal update functions
void Camera::Rotate(const float dt)
void Camera::Rotate(const float dt, const FCameraInput& input)
{
auto m_input = ENGINE->INP();
const long* dxdy = m_input->GetDelta();
float dy = static_cast<float>(dxdy[1]);
float dx = static_cast<float>(dxdy[0]);
float dy = input.DeltaMouseXY[1];
float dx = input.DeltaMouseXY[0];

const float delta = AngularSpeedDeg * DEG2RAD * dt;
Rotate(dx, dy, delta);
}

void Camera::Move(const float dt)
void Camera::Move(const float dt, const FCameraInput& input)
{
auto m_input = ENGINE->INP();
XMMATRIX MRotation = RotMatrix();
XMVECTOR translation = XMVectorSet(0,0,0,0);
if (m_input->IsKeyDown('A')) translation += XMVector3TransformCoord(XMFLOAT3::Left, MRotation);
if (m_input->IsKeyDown('D')) translation += XMVector3TransformCoord(XMFLOAT3::Right, MRotation);
if (m_input->IsKeyDown('W')) translation += XMVector3TransformCoord(XMFLOAT3::Forward, MRotation);
if (m_input->IsKeyDown('S')) translation += XMVector3TransformCoord(XMFLOAT3::Back, MRotation);
if (m_input->IsKeyDown('E')) translation += XMVector3TransformCoord(XMFLOAT3::Up, MRotation);
if (m_input->IsKeyDown('Q')) translation += XMVector3TransformCoord(XMFLOAT3::Down, MRotation);
if (m_input->IsKeyDown(VK_SHIFT)) translation *= 2.0f;
translation *= 4.0f;

XMVECTOR V = mVelocity;
V += (translation * MoveSpeed - V * Drag) * dt;
mVelocity = V;
const XMMATRIX MRotation = GetRotationMatrix();
const XMVECTOR WorldSpaceTranslation = XMVector3TransformCoord(input.LocalTranslationVector, MRotation);

XMVECTOR V = XMLoadFloat3(&mVelocity);
V += (WorldSpaceTranslation * MoveSpeed - V * Drag) * dt;
XMStoreFloat3(&mVelocity, V);
}
#else
void Camera::Reset() {}
void Camera::Rotate(const float dt) {}
void Camera::Move(const float dt) {}
#endif
Loading

0 comments on commit 202c9a4

Please sign in to comment.