Files
directx-plane-game/Graphics2/Camera.cpp
iDunnoDev 7a57c73ac3 Added the camera class
Added the DDS Texture Loading for future use
Added the gamepad class for future use
Added first child method to the scenegraph
Added RGB to intensity and lerp methods to the shared methods class
Added the terrain node class with normals
Fixed issue with submeshnode not passing the world transform to the actual meshes
2022-04-18 22:33:15 +01:00

133 lines
3.3 KiB
C++

#include "Camera.h"
XMVECTOR defaultForward = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
XMVECTOR defaultRight = XMVectorSet(1.0f, 0.0f, 0.0f, 0.0f);
XMVECTOR defaultUp = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
Camera::Camera()
{
_cameraPosition = XMFLOAT4(0.0f, 0.0f, 0.0f, 0.0f);
_moveLeftRight = 0.0f;
_moveForwardBack = 0.0f;
_cameraYaw = 0.0f;
_cameraPitch = 0.0f;
_cameraRoll = 0.0f;
}
Camera::~Camera()
{
}
void Camera::SetPitch(float pitch)
{
_cameraPitch += XMConvertToRadians(pitch);
}
void Camera::SetTotalPitch(float pitch)
{
_cameraPitch = XMConvertToRadians(pitch);
}
float Camera::GetPitch() const
{
return XMConvertToDegrees(_cameraPitch);
}
void Camera::SetYaw(float yaw)
{
_cameraYaw += XMConvertToRadians(yaw);
}
void Camera::SetTotalYaw(float yaw)
{
_cameraYaw = XMConvertToRadians(yaw);
}
float Camera::GetYaw() const
{
return XMConvertToDegrees(_cameraYaw);
}
void Camera::SetRoll(float roll)
{
_cameraRoll += XMConvertToRadians(roll);
}
void Camera::SetTotalRoll(float roll)
{
_cameraRoll = XMConvertToRadians(roll);
}
float Camera::GetRoll() const
{
return XMConvertToDegrees(_cameraRoll);
}
void Camera::SetLeftRight(float leftRight)
{
_moveLeftRight = leftRight;
}
void Camera::SetForwardBack(float forwardBack)
{
_moveForwardBack = forwardBack;
}
XMMATRIX Camera::GetViewMatrix(void)
{
return XMLoadFloat4x4(&_viewMatrix);
}
XMVECTOR Camera::GetCameraPosition(void)
{
return XMLoadFloat4(&_cameraPosition);
}
XMFLOAT4 Camera::GetRawCameraPosition(void)
{
return _cameraPosition;
}
void Camera::SetCameraPosition(float x, float y, float z)
{
_cameraPosition = XMFLOAT4(x, y, z, 0.0f);
}
void Camera::Update(void)
{
XMVECTOR cameraPosition;
XMVECTOR cameraTarget;
XMVECTOR cameraRight;
XMVECTOR cameraForward;
XMVECTOR cameraUp;
// Yaw (rotation around the Y axis) will have an impact on the forward and right vectors
XMMATRIX cameraRotationYaw = XMMatrixRotationAxis(defaultUp, _cameraYaw);
cameraRight = XMVector3TransformCoord(defaultRight, cameraRotationYaw);
cameraForward = XMVector3TransformCoord(defaultForward, cameraRotationYaw);
// Pitch (rotation around the X axis) impact the up and forward vectors
XMMATRIX cameraRotationPitch = XMMatrixRotationAxis(cameraRight, _cameraPitch);
cameraUp = XMVector3TransformCoord(defaultUp, cameraRotationPitch);
cameraForward = XMVector3TransformCoord(cameraForward, cameraRotationPitch);
// Roll (rotation around the Z axis) will impact the Up and Right vectors
XMMATRIX cameraRotationRoll = XMMatrixRotationAxis(cameraForward, _cameraRoll);
cameraUp = XMVector3TransformCoord(cameraUp, cameraRotationRoll);
cameraRight = XMVector3TransformCoord(cameraRight, cameraRotationRoll);
// Adjust the camera position by the appropriate amount forward/back and left/right
cameraPosition = XMLoadFloat4(&_cameraPosition) + _moveLeftRight * cameraRight + _moveForwardBack * cameraForward;
XMStoreFloat4(&_cameraPosition, cameraPosition);
// Reset the amount we are moving
_moveLeftRight = 0.0f;
_moveForwardBack = 0.0f;
// Calculate a vector that tells us the direction the camera is looking in
cameraTarget = cameraPosition + XMVector3Normalize(cameraForward);
// and calculate our view matrix
XMStoreFloat4x4(&_viewMatrix, XMMatrixLookAtLH(cameraPosition, cameraTarget, cameraUp));
}