Added follow cam

Added "Controlled" mesh classes
Added Global Lighting Class
Added Gamepad controls
Split terrain nodes into Height and Perlin classes
Fixed Splitmesh node stuff
This commit is contained in:
iDunnoDev
2022-05-09 17:50:22 +01:00
committed by iDunnoDev
parent bc906064e5
commit f6bba67897
58 changed files with 1743 additions and 351 deletions

View File

@ -1,17 +1,16 @@
#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()
{
_nodeFollowed = nullptr;
_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;
_followPositionOnly = false;
_followOffset = XMFLOAT3(0.0f, 0.0f, 0.0f);
}
Camera::~Camera()
@ -83,14 +82,28 @@ 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);
SetCameraPosition(XMFLOAT4(x, y, z, 0.0f));
}
void Camera::SetCameraPosition(XMVECTOR vectorIn)
{
XMFLOAT4 floatIn;
XMStoreFloat4(&floatIn, vectorIn);
SetCameraPosition(floatIn);
}
void Camera::SetCameraPosition(XMFLOAT4 floatIn)
{
_cameraPosition = floatIn;
}
void Camera::SetFollowNode(shared_ptr<ObjectNode> nodeFollowed, XMFLOAT3 followOffset, bool positionOnly)
{
_nodeFollowed = nodeFollowed;
_followOffset = followOffset;
_followPositionOnly = positionOnly;
}
void Camera::Update(void)
@ -101,31 +114,84 @@ void Camera::Update(void)
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);
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);
// 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);
if (_nodeFollowed == nullptr)
{
// 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);
// 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);
// 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);
// Adjust the camera position by the appropriate amount forward/back and left/right
cameraPosition = XMLoadFloat4(&_cameraPosition) + _moveLeftRight * cameraRight + _moveForwardBack * cameraForward;
XMStoreFloat4(&_cameraPosition, cameraPosition);
// 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);
// Reset the amount we are moving
_moveLeftRight = 0.0f;
_moveForwardBack = 0.0f;
// Adjust the camera position by the appropriate amount forward/back and left/right
cameraPosition = XMLoadFloat4(&_cameraPosition) + _moveLeftRight * cameraRight + _moveForwardBack * cameraForward;
XMStoreFloat4(&_cameraPosition, cameraPosition);
// Calculate a vector that tells us the direction the camera is looking in
cameraTarget = cameraPosition + XMVector3Normalize(cameraForward);
// 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);
}
else
{
if (_followPositionOnly)
{
_cameraYaw = 0.0f;
_cameraPitch = 0.0f;
_cameraRoll = 0.0f;
}
else
{
_cameraYaw = XMConvertToRadians(_nodeFollowed->GetYaw());
_cameraPitch = XMConvertToRadians(_nodeFollowed->GetPitch());
_cameraRoll = XMConvertToRadians(_nodeFollowed->GetRoll());
}
// 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);
XMFLOAT4 nodePosition;
XMStoreFloat4(&nodePosition, _nodeFollowed->GetNodePosition());
if (_followPositionOnly)
{
cameraPosition = XMVectorSet(nodePosition.x + _followOffset.x, nodePosition.y + _followOffset.y, nodePosition.z + _followOffset.z, 1.0f);
cameraTarget = cameraPosition + XMVector3Normalize(cameraForward);
}
else
{
XMVECTOR offset = XMVectorSet(_followOffset.x, _followOffset.y, _followOffset.z, 1.0f);
cameraPosition = _nodeFollowed->GetNodePosition() + offset;
cameraTarget = XMLoadFloat4(&nodePosition) + XMVector3Normalize(cameraForward);
}
// Set the camera position
XMStoreFloat4(&_cameraPosition, cameraPosition);
}
// and calculate our view matrix
XMStoreFloat4x4(&_viewMatrix, XMMatrixLookAtLH(cameraPosition, cameraTarget, cameraUp));