2020-02-13 22:13:16 -08:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
// GLM
|
|
|
|
#include <glm/glm.hpp >
|
|
|
|
#include <glm/ext/matrix_clip_space.hpp>
|
|
|
|
#include <glm/ext/matrix_transform.hpp >
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
// DGL
|
2020-02-13 22:13:16 -08:00
|
|
|
#include "DGL_FundamentalTypes.hpp"
|
|
|
|
#include "DGL_MiscTypes.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "Cpp_Alias.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
namespace DGL
|
2020-02-13 22:13:16 -08:00
|
|
|
{
|
|
|
|
using CoordSpace = Matrix4x4;
|
|
|
|
using Projection = Matrix4x4;
|
|
|
|
|
|
|
|
|
|
|
|
// Function
|
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
sfn Cosine(gFloat _angleInRadians)
|
|
|
|
{
|
|
|
|
return glm::cos(_angleInRadians);
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn Sine(gFloat _angleInRadians)
|
|
|
|
{
|
|
|
|
return glm::sin(_angleInRadians);
|
|
|
|
}
|
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
template<typename Type>
|
|
|
|
sfn CreateLookAtView(const Generic::Vector3<Type> _viewPosition, const Generic::Vector3<Type> _lookAtPosition, const Generic::Vector3<Type> _upDirection) -> Matrix4x4
|
|
|
|
{
|
|
|
|
return glm::lookAt(_viewPosition, _lookAtPosition, _upDirection);
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn CreateOrthographic(gFloat _leftScreenCoord, gFloat _rightScreenCoord, gFloat _bottomScreenCoord, gFloat _topScreenCoord, gFloat _nearPlane, gFloat _farPlane)
|
|
|
|
{
|
|
|
|
return glm::ortho(_leftScreenCoord, _rightScreenCoord, _bottomScreenCoord, _topScreenCoord, _nearPlane, _farPlane);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename FloatType>
|
|
|
|
sfn CreatePerspective(const FloatType _fieldOfView, const FloatType _aspectRatio, const FloatType _nearPlane, const FloatType _farPlane)
|
|
|
|
{
|
|
|
|
return glm::perspective(_fieldOfView, _aspectRatio, _nearPlane, _farPlane);
|
|
|
|
}
|
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
sfn GetCrossNormal(Vector3 _subj, Vector3 _ref) -> Vector3
|
2020-02-14 23:21:27 -08:00
|
|
|
{
|
2020-02-15 19:32:26 -08:00
|
|
|
return glm::cross(_subj, _ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
sfn GetDirection(Vector3 _vectorSpecified)
|
|
|
|
{
|
|
|
|
return glm::normalize(_vectorSpecified);
|
2020-02-14 23:21:27 -08:00
|
|
|
}
|
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
sfn Rotate(const Matrix4x4 _matrix, gFloat _rotationAngleAmount, Vector3 _axis) -> Matrix4x4
|
|
|
|
{
|
|
|
|
return glm::rotate(_matrix, _rotationAngleAmount, _axis);
|
|
|
|
}
|
|
|
|
|
2020-02-16 22:28:24 -08:00
|
|
|
sfn Scale(const Matrix4x4 _matrix, Vector3 _scaleBy)
|
|
|
|
{
|
|
|
|
return glm::scale(_matrix, _scaleBy);
|
|
|
|
}
|
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
template<typename Type>
|
|
|
|
sfn ToRadians(const Ref(Type) _degrees) -> Type
|
|
|
|
{
|
|
|
|
return glm::radians(_degrees);
|
|
|
|
}
|
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
sfn Translate(const Matrix4x4 _matrix, Vector3 _translationAmount)
|
2020-02-14 23:21:27 -08:00
|
|
|
{
|
2020-02-15 19:32:26 -08:00
|
|
|
return glm::translate(_matrix, _translationAmount);
|
2020-02-14 23:21:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
struct ClippingPlanes
|
|
|
|
{
|
|
|
|
gFloat Near, Far;
|
|
|
|
|
|
|
|
ClippingPlanes(gFloat _near, gFloat _far) : Near(_near), Far(_far) {};
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace DefaultSpace
|
|
|
|
{
|
|
|
|
gFloat
|
|
|
|
AspectRatio = 16.0f / 10.0f,
|
|
|
|
FieldOfView = 90.0f ,
|
2020-02-16 22:28:24 -08:00
|
|
|
NearClippingPlane = 0.01f ,
|
|
|
|
FarClippingPlane = 1000.0f ;
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
Vector3 CameraPosition( 0, 0, 2),
|
|
|
|
LookAtPosition( 0, 0, 0),
|
|
|
|
RightDirection( 1, 0, 0),
|
|
|
|
UpDirection ( 0, 1, 0),
|
|
|
|
FrontDirection( 0, 0, 1) ;
|
2020-02-13 22:13:16 -08:00
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
gFloat ScreenWidth = 720.0f, ScreenHeight = 540.0f, ScreenCenterWidth = ScreenWidth / 2, ScreenCenterHeight = ScreenHeight / 2;
|
2020-02-13 22:13:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Camera
|
|
|
|
{
|
2020-02-14 23:21:27 -08:00
|
|
|
gFloat AspectRatio, FieldOfView, Yaw, Pitch, Roll;
|
2020-02-13 22:13:16 -08:00
|
|
|
|
|
|
|
ClippingPlanes ClipSpace;
|
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
CoordSpace Viewport;
|
2020-02-13 22:13:16 -08:00
|
|
|
|
|
|
|
Projection Orthographic, Perspective;
|
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
Vector3 Position, LookAtPosition, UpDirection, FrontDirection, RightDirection;
|
2020-02-13 22:13:16 -08:00
|
|
|
|
|
|
|
|
|
|
|
Camera
|
|
|
|
(
|
|
|
|
gFloat _aspectRatio ,
|
|
|
|
gFloat _fieldOfView ,
|
|
|
|
ClippingPlanes _clippingPlanes,
|
|
|
|
Vector3 _position ,
|
|
|
|
Vector3 _lookAtPosition,
|
2020-02-14 23:21:27 -08:00
|
|
|
Vector3 _upDirection ,
|
|
|
|
Vector3 _frontDirection
|
2020-02-13 22:13:16 -08:00
|
|
|
) :
|
|
|
|
AspectRatio (_aspectRatio ),
|
|
|
|
FieldOfView (_fieldOfView ),
|
|
|
|
ClipSpace (_clippingPlanes),
|
|
|
|
Position (_position ),
|
|
|
|
LookAtPosition(_lookAtPosition),
|
2020-02-14 23:21:27 -08:00
|
|
|
UpDirection (_upDirection ),
|
|
|
|
FrontDirection(_frontDirection)
|
2020-02-13 22:13:16 -08:00
|
|
|
{
|
2020-02-14 23:21:27 -08:00
|
|
|
Yaw = -90.0f; Pitch = 0; Roll = 0;
|
|
|
|
|
|
|
|
UpdateCamera();
|
|
|
|
|
2020-02-13 22:13:16 -08:00
|
|
|
Orthographic = CreateOrthographic(0.0f, DefaultSpace::ScreenWidth, 0.0f, DefaultSpace::ScreenHeight, ClipSpace.Near, ClipSpace.Far);
|
|
|
|
|
|
|
|
Perspective = CreatePerspective<gFloat>(ToRadians(FieldOfView), AspectRatio, ClipSpace.Near, ClipSpace.Far);
|
|
|
|
}
|
2020-02-14 23:21:27 -08:00
|
|
|
|
|
|
|
|
|
|
|
sfn Move(EDirection _direction, gFloat _translationAmount, gFloat _deltaTime)
|
|
|
|
{
|
|
|
|
switch (_direction)
|
|
|
|
{
|
|
|
|
case EDirection::Up:
|
|
|
|
{
|
2020-02-15 19:32:26 -08:00
|
|
|
Position -= UpDirection * _translationAmount * _deltaTime;
|
2020-02-14 23:21:27 -08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EDirection::Down:
|
|
|
|
{
|
2020-02-15 19:32:26 -08:00
|
|
|
Position += UpDirection * _translationAmount * _deltaTime;
|
2020-02-14 23:21:27 -08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EDirection::Left:
|
|
|
|
{
|
2020-02-15 19:32:26 -08:00
|
|
|
Position -= GetDirection(GetCrossNormal(FrontDirection, UpDirection)) * _translationAmount * _deltaTime;
|
2020-02-14 23:21:27 -08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EDirection::Right:
|
|
|
|
{
|
2020-02-15 19:32:26 -08:00
|
|
|
Position += GetDirection(GetCrossNormal(FrontDirection, UpDirection)) * _translationAmount * _deltaTime;
|
2020-02-14 23:21:27 -08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EDirection::Forward:
|
|
|
|
{
|
2020-02-15 19:32:26 -08:00
|
|
|
Position += FrontDirection * _translationAmount * _deltaTime;
|
2020-02-14 23:21:27 -08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case EDirection::Backward:
|
|
|
|
{
|
2020-02-15 19:32:26 -08:00
|
|
|
Position -= FrontDirection * _translationAmount * _deltaTime;
|
2020-02-14 23:21:27 -08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
throw std::logic_error("Direction move not defined.");
|
|
|
|
}
|
|
|
|
}
|
2020-02-15 19:32:26 -08:00
|
|
|
|
|
|
|
return;
|
2020-02-14 23:21:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
sfn Move(Vector3 _translationAmount, Ref(gFloat) _deltaTime)
|
|
|
|
{
|
|
|
|
Position += _translationAmount * _deltaTime;
|
2020-02-15 19:32:26 -08:00
|
|
|
|
|
|
|
return;
|
2020-02-14 23:21:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
sfn Rotate(ERotationAxis _pivot, gFloat _rotationAmount, gFloat _deltaTime)
|
|
|
|
{
|
|
|
|
switch (_pivot)
|
|
|
|
{
|
|
|
|
case ERotationAxis::Pitch:
|
|
|
|
{
|
|
|
|
Pitch -= _rotationAmount * _deltaTime;
|
|
|
|
|
|
|
|
if (Pitch > 89.9f)
|
|
|
|
{
|
|
|
|
Pitch = 89.9f;
|
|
|
|
}
|
|
|
|
else if (Pitch < -89.9f)
|
|
|
|
{
|
|
|
|
Pitch = -89.9f;
|
|
|
|
}
|
|
|
|
|
2020-02-16 22:28:24 -08:00
|
|
|
return;
|
2020-02-14 23:21:27 -08:00
|
|
|
}
|
|
|
|
case ERotationAxis::Roll:
|
|
|
|
{
|
|
|
|
Roll += _rotationAmount * _deltaTime;
|
|
|
|
|
2020-02-16 22:28:24 -08:00
|
|
|
return;
|
2020-02-14 23:21:27 -08:00
|
|
|
}
|
|
|
|
case ERotationAxis::Yaw:
|
|
|
|
{
|
|
|
|
Yaw += _rotationAmount * _deltaTime;
|
|
|
|
|
2020-02-16 22:28:24 -08:00
|
|
|
return;
|
2020-02-14 23:21:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sfn UpdateCamera() -> void
|
|
|
|
{
|
|
|
|
FrontDirection.x = Cosine(ToRadians(Yaw )) * Cosine(ToRadians(Pitch));
|
|
|
|
FrontDirection.y = Sine (ToRadians(Pitch)) ;
|
|
|
|
FrontDirection.z = Sine (ToRadians(Yaw )) * Cosine(ToRadians(Pitch));
|
|
|
|
|
|
|
|
FrontDirection = GetDirection(FrontDirection );
|
|
|
|
RightDirection = GetDirection(GetCrossNormal(FrontDirection, DefaultSpace::UpDirection ));
|
|
|
|
UpDirection = GetDirection(GetCrossNormal(RightDirection, FrontDirection));
|
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
LookAtPosition = Position + FrontDirection;
|
2020-02-14 23:21:27 -08:00
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
Viewport = CreateLookAtView(Position, LookAtPosition, UpDirection);
|
2020-02-14 23:21:27 -08:00
|
|
|
|
2020-02-15 19:32:26 -08:00
|
|
|
return;
|
2020-02-14 23:21:27 -08:00
|
|
|
}
|
2020-02-13 22:13:16 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace DefaultSpace
|
|
|
|
{
|
2020-02-14 23:21:27 -08:00
|
|
|
Camera WorldCamera
|
|
|
|
(
|
|
|
|
AspectRatio,
|
|
|
|
FieldOfView,
|
|
|
|
ClippingPlanes(NearClippingPlane, FarClippingPlane),
|
|
|
|
CameraPosition,
|
|
|
|
LookAtPosition,
|
|
|
|
UpDirection,
|
|
|
|
FrontDirection
|
|
|
|
);
|
2020-02-13 22:13:16 -08:00
|
|
|
|
|
|
|
CoordSpace WorldSpace(Matrix4x4(1.0f));
|
|
|
|
|
2020-02-14 23:21:27 -08:00
|
|
|
CoordSpace Screenspace = WorldCamera.Perspective * WorldCamera.Viewport * WorldSpace;
|
|
|
|
|
|
|
|
sfn UpdateScreenspace()
|
|
|
|
{
|
2020-02-15 19:32:26 -08:00
|
|
|
Screenspace = WorldCamera.Perspective * WorldCamera.Viewport * WorldSpace;
|
|
|
|
|
|
|
|
return;
|
2020-02-14 23:21:27 -08:00
|
|
|
}
|
2020-02-13 22:13:16 -08:00
|
|
|
}
|
|
|
|
}
|