From 2e75d168a105b5676c6ce3f7deaebb8258fd5e55 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Tue, 18 Feb 2014 12:03:54 +0100 Subject: [PATCH] New Cameras V2 is quaternion based --- Code/Game/GameClient/GameClient.vcxproj | 4 + .../GameClientState/Camera_BasicV2.cpp | 117 +++++++++++ .../GameClientState/Camera_BasicV2.h | 42 ++++ .../GameClientState/Camera_FPSV2.cpp | 182 ++++++++++++++++++ .../GameClient/GameClientState/Camera_FPSV2.h | 62 ++++++ Code/Misc/OysterMath/Quaternion.h | 7 + 6 files changed, 414 insertions(+) create mode 100644 Code/Game/GameClient/GameClientState/Camera_BasicV2.cpp create mode 100644 Code/Game/GameClient/GameClientState/Camera_BasicV2.h create mode 100644 Code/Game/GameClient/GameClientState/Camera_FPSV2.cpp create mode 100644 Code/Game/GameClient/GameClientState/Camera_FPSV2.h diff --git a/Code/Game/GameClient/GameClient.vcxproj b/Code/Game/GameClient/GameClient.vcxproj index 3daafff3..349a88ec 100644 --- a/Code/Game/GameClient/GameClient.vcxproj +++ b/Code/Game/GameClient/GameClient.vcxproj @@ -201,7 +201,9 @@ + + @@ -226,7 +228,9 @@ + + diff --git a/Code/Game/GameClient/GameClientState/Camera_BasicV2.cpp b/Code/Game/GameClient/GameClientState/Camera_BasicV2.cpp new file mode 100644 index 00000000..8454b3bd --- /dev/null +++ b/Code/Game/GameClient/GameClientState/Camera_BasicV2.cpp @@ -0,0 +1,117 @@ +#include "Camera_BasicV2.h" + +using namespace ::Oyster::Math3D; + +Camera_BasicV2::Camera_BasicV2() +{ + this->translation = Float3::null; + this->rotation = Quaternion::identity; + this->projection = Float4x4::identity; +} + +Camera_BasicV2::Camera_BasicV2( const Float3 &position, const Quaternion &rotation, const Float4x4 &projection ) +{ + this->translation = position; + this->rotation = rotation; + this->projection = projection; +} + +Camera_BasicV2::~Camera_BasicV2() {} + +Camera_BasicV2 & Camera_BasicV2::operator = ( const Camera_BasicV2 &camera ) +{ + this->translation = camera.translation; + this->rotation = camera.rotation; + this->projection = camera.projection; + return *this; +} + +void Camera_BasicV2::SetPosition( const Float3 &translation ) +{ + this->translation = translation; +} + +void Camera_BasicV2::SetRotation( const Quaternion &rotation ) +{ + this->rotation = rotation; +} + +void Camera_BasicV2::SetAngular( const Float3 &axis ) +{ + this->rotation = Rotation( axis ); +} + +void Camera_BasicV2::SetProjection( const Float4x4 &matrix ) +{ + this->projection = matrix; +} + +void Camera_BasicV2::SetOrthographicProjection( Float width, Float height, Float nearClip, Float farClip ) +{ + ProjectionMatrix_Orthographic( width, height, nearClip, farClip, this->projection ); +} + +void Camera_BasicV2::SetPerspectiveProjection( Float verticalFoV, Float aspectRatio, Float nearClip, Float farClip ) +{ + ProjectionMatrix_Perspective( verticalFoV, aspectRatio, nearClip, farClip, this->projection ); +} + +void Camera_BasicV2::Move( const Float3 &deltaPosition ) +{ + this->translation += deltaPosition; +} + +void Camera_BasicV2::Rotate( const Quaternion &deltaRotation ) +{ + this->rotation *= deltaRotation; +} + +void Camera_BasicV2::Rotate( const Float3 &deltaAngularAxis ) +{ + this->rotation *= Rotation( deltaAngularAxis ); +} + +const Float3 & Camera_BasicV2::GetPosition() const +{ + return this->translation; +} + +Float3 & Camera_BasicV2::GetAngularAxis( Float3 &targetMem ) const +{ + return targetMem = AngularAxis( this->rotation ); +} + +Float3 Camera_BasicV2::GetNormalOf( const Float3 &axis ) const +{ + return WorldAxisOf( this->rotation, axis ); +} + +const Quaternion & Camera_BasicV2::GetRotation() const +{ + return this->rotation; +} + +Float3x3 & Camera_BasicV2::GetRotationMatrix( Float3x3 &targetMem ) const +{ + return RotationMatrix( this->rotation, targetMem ); +} + +Float4x4 & Camera_BasicV2::GetRotationMatrix( Float4x4 &targetMem ) const +{ + return RotationMatrix( this->rotation, targetMem ); +} + +Float4x4 & Camera_BasicV2::GetViewMatrix( Float4x4 &targetMem ) const +{ + return ViewMatrix( this->rotation, this->translation, targetMem ); +} + +const Float4x4 & Camera_BasicV2::GetProjectionMatrix() const +{ + return this->projection; +} + +Float4x4 & Camera_BasicV2::GetViewsProjMatrix( Float4x4 &targetMem ) const +{ + return TransformMatrix( this->projection, this->GetViewMatrix(), targetMem ); +} \ No newline at end of file diff --git a/Code/Game/GameClient/GameClientState/Camera_BasicV2.h b/Code/Game/GameClient/GameClientState/Camera_BasicV2.h new file mode 100644 index 00000000..aaa9d491 --- /dev/null +++ b/Code/Game/GameClient/GameClientState/Camera_BasicV2.h @@ -0,0 +1,42 @@ +#ifndef CAMERA_BASIC_V2_H +#define CAMERA_BASIC_V2_H + +#include "OysterMath.h" + +class Camera_BasicV2 +{ +public: + Camera_BasicV2(); + Camera_BasicV2( const ::Oyster::Math::Float3 &position, const ::Oyster::Math::Quaternion &rotation, const ::Oyster::Math::Float4x4 &projection ); + virtual ~Camera_BasicV2(); + + Camera_BasicV2 & operator = ( const Camera_BasicV2 &camera ); + + void SetPosition( const ::Oyster::Math::Float3 &translation ); + void SetRotation( const ::Oyster::Math::Quaternion &rotation ); + void SetAngular( const ::Oyster::Math::Float3 &axis ); + void SetProjection( const ::Oyster::Math::Float4x4 &matrix ); + void SetOrthographicProjection( ::Oyster::Math::Float width, ::Oyster::Math::Float height, ::Oyster::Math::Float nearClip, ::Oyster::Math::Float farClip ); + void SetPerspectiveProjection( ::Oyster::Math::Float verticalFoV, ::Oyster::Math::Float aspectRatio, ::Oyster::Math::Float nearClip, ::Oyster::Math::Float farClip ); + + void Move( const ::Oyster::Math::Float3 &deltaPosition ); + void Rotate( const ::Oyster::Math::Quaternion &deltaRotation ); + void Rotate( const ::Oyster::Math::Float3 &deltaAngularAxis ); + + const ::Oyster::Math::Float3 & GetPosition() const; + ::Oyster::Math::Float3 & GetAngularAxis( ::Oyster::Math::Float3 &targetMem = ::Oyster::Math::Float3() ) const; + ::Oyster::Math::Float3 GetNormalOf( const ::Oyster::Math::Float3 &axis ) const; + const ::Oyster::Math::Quaternion & GetRotation() const; + ::Oyster::Math::Float3x3 & GetRotationMatrix( ::Oyster::Math::Float3x3 &targetMem ) const; + ::Oyster::Math::Float4x4 & GetRotationMatrix( ::Oyster::Math::Float4x4 &targetMem ) const; + ::Oyster::Math::Float4x4 & GetViewMatrix( Oyster::Math::Float4x4 &targetMem = ::Oyster::Math::Float4x4() ) const; + const ::Oyster::Math::Float4x4 & GetProjectionMatrix() const; + ::Oyster::Math::Float4x4 & GetViewsProjMatrix( Oyster::Math::Float4x4 &targetMem = ::Oyster::Math::Float4x4() ) const; + + private: + ::Oyster::Math::Float3 translation; + mutable ::Oyster::Math::Quaternion rotation; + ::Oyster::Math::Float4x4 projection; +}; + +#endif \ No newline at end of file diff --git a/Code/Game/GameClient/GameClientState/Camera_FPSV2.cpp b/Code/Game/GameClient/GameClientState/Camera_FPSV2.cpp new file mode 100644 index 00000000..b1863242 --- /dev/null +++ b/Code/Game/GameClient/GameClientState/Camera_FPSV2.cpp @@ -0,0 +1,182 @@ +#include "Camera_FPSV2.h" +#include "Utilities.h" + +using namespace ::Oyster::Math3D; +using namespace ::Utility::Value; + +Camera_FPSV2::Camera_FPSV2() +{ // this->head is default set to identity uniformprojection at origo + this->pitchUp = 0.0f; + this->headOffset = + this->body.translation = Float3::null; + this->body.rotation = Quaternion::identity; +} + +Camera_FPSV2::~Camera_FPSV2() {} + +Camera_FPSV2 & Camera_FPSV2::operator = ( const Camera_FPSV2 &camera ) +{ + this->head = camera.head; + this->pitchUp = camera.pitchUp; + this->headOffset = camera.headOffset; + this->body.translation = camera.body.translation; + this->body.rotation = camera.body.rotation; + return *this; +} + +void Camera_FPSV2::SetHeadOffset( const Float3 &translation ) +{ + this->head.Move( translation - this->headOffset ); + this->headOffset = translation; +} + +void Camera_FPSV2::SetPosition( const Float3 &translation ) +{ + this->head.Move( translation - this->body.translation ); + this->body.translation = translation; +} + +void Camera_FPSV2::SetAngular( const Float3 &axis ) +{ + this->body.rotation = Rotation( axis ); + this->head.SetRotation( this->body.rotation ); + this->pitchUp = 0.0f; +} + +void Camera_FPSV2::SetProjection( const Float4x4 &matrix ) +{ + this->head.SetProjection( matrix ); +} + +void Camera_FPSV2::SetOrthographicProjection( Float width, Float height, Float nearClip, Float farClip ) +{ + this->head.SetOrthographicProjection( width, height, nearClip, farClip ); +} + +void Camera_FPSV2::SetPerspectiveProjection( Float verticalFoV, Float aspectRatio, Float nearClip, Float farClip ) +{ + this->head.SetPerspectiveProjection( verticalFoV, aspectRatio, nearClip, farClip ); +} + +void Camera_FPSV2::UpdateOrientation() +{ + Float4x4 orientation; + OrientationMatrix( this->body.rotation, this->body.translation, orientation ); + + this->head.SetPosition( (orientation * Float4(this->headOffset, 1.0f)).xyz ); +} + +void Camera_FPSV2::SnapUpToNormal( const Float3 &normal ) +{ + this->body.rotation = Rotation( SnapAngularAxis(AngularAxis(this->body.rotation), WorldAxisOf(this->body.rotation, Float3::standard_unit_y), normal) ); + this->head.SetRotation( this->body.rotation * Rotation(this->pitchUp * Float3::standard_unit_x) ); +} + +void Camera_FPSV2::Move( const Float3 &deltaPosition ) +{ + this->head.Move( deltaPosition ); + this->body.translation += deltaPosition; +} + +void Camera_FPSV2::Rotate( const Quaternion &deltaRotation ) +{ + this->head.Rotate( deltaRotation ); + this->body.rotation *= deltaRotation; +} + +void Camera_FPSV2::Rotate( const Float3 &deltaAngularAxis ) +{ + this->Rotate( Rotation(deltaAngularAxis) ); +} + +void Camera_FPSV2::MoveForward( Float distance ) +{ + this->MoveBackward( -distance ); +} + +void Camera_FPSV2::MoveBackward( Float distance ) +{ + this->Move( distance * WorldAxisOf(this->body.rotation, Float3::standard_unit_z) ); +} + +void Camera_FPSV2::StrafeRight( Float distance ) +{ + this->Move( distance * WorldAxisOf(this->body.rotation, Float3::standard_unit_x) ); +} + +void Camera_FPSV2::StrafeLeft( Float distance ) +{ + this->StrafeRight( -distance ); +} + +void Camera_FPSV2::PitchUp( Float radian ) +{ + this->pitchUp = Clamp( this->pitchUp + radian, -0.48f * pi, 0.48f * pi ); + this->head.SetRotation( this->body.rotation * Rotation(this->pitchUp, Float3::standard_unit_x) ); +} + +void Camera_FPSV2::PitchDown( Float radian ) +{ + this->PitchUp( -radian ); +} + +void Camera_FPSV2::YawRight( Float radian ) +{ + this->YawLeft( -radian ); +} + +void Camera_FPSV2::YawLeft( Float radian ) +{ + Quaternion deltaRotation = Rotation( radian, WorldAxisOf(this->body.rotation, Float3::standard_unit_y) ); + this->Rotate( deltaRotation ); +} + +const Float3 & Camera_FPSV2::GetHeadOffset() const +{ + return this->headOffset; +} + +const Float3 & Camera_FPSV2::GetPosition() const +{ + return this->body.translation; +} + +Float4x4 & Camera_FPSV2::GetViewMatrix( Float4x4 &targetMem ) const +{ + return this->head.GetViewMatrix( targetMem ); +} + +const Float4x4 & Camera_FPSV2::GetProjectionMatrix() const +{ + return this->head.GetProjectionMatrix(); +} + +Float4x4 & Camera_FPSV2::GetViewsProjMatrix( Float4x4 &targetMem ) const +{ + return this->head.GetViewsProjMatrix( targetMem ); +} + +Float3 Camera_FPSV2::GetNormalOf( const Float3 &axis ) const +{ + return this->head.GetNormalOf( axis ); +} + +Float3 Camera_FPSV2::GetRight() const +{ + return WorldAxisOf( this->body.rotation, Float3::standard_unit_x ); +} + +Float3 Camera_FPSV2::GetUp() const +{ + return WorldAxisOf( this->body.rotation, Float3::standard_unit_y ); +} + +Float3 Camera_FPSV2::GetLook() const +{ + return this->head.GetNormalOf( -Float3::standard_unit_z ); +} + +Float3 Camera_FPSV2::GetForward() const +{ + return WorldAxisOf( this->body.rotation, -Float3::standard_unit_z ); +} \ No newline at end of file diff --git a/Code/Game/GameClient/GameClientState/Camera_FPSV2.h b/Code/Game/GameClient/GameClientState/Camera_FPSV2.h new file mode 100644 index 00000000..968053c7 --- /dev/null +++ b/Code/Game/GameClient/GameClientState/Camera_FPSV2.h @@ -0,0 +1,62 @@ +#ifndef CAMERA_FPSV2_H +#define CAMERA_FPSV2_H + +#include "OysterMath.h" +#include "Camera_BasicV2.h" + +class Camera_FPSV2 +{ +public: + Camera_FPSV2(); + virtual ~Camera_FPSV2(); + + Camera_FPSV2 & operator = ( const Camera_FPSV2 &camera ); + + void SetHeadOffset( const ::Oyster::Math::Float3 &translation ); + void SetPosition( const ::Oyster::Math::Float3 &translation ); + void SetAngular( const ::Oyster::Math::Float3 &axis ); + void SetProjection( const ::Oyster::Math::Float4x4 &matrix ); + void SetOrthographicProjection( ::Oyster::Math::Float width, ::Oyster::Math::Float height, ::Oyster::Math::Float nearClip, ::Oyster::Math::Float farClip ); + void SetPerspectiveProjection( ::Oyster::Math::Float verticalFoV, ::Oyster::Math::Float aspectRatio, ::Oyster::Math::Float nearClip, ::Oyster::Math::Float farClip ); + + void UpdateOrientation(); + + void SnapUpToNormal( const ::Oyster::Math::Float3 &normal ); + + void Move( const ::Oyster::Math::Float3 &deltaPosition ); + void Rotate( const ::Oyster::Math::Quaternion &deltaRotation ); + void Rotate( const ::Oyster::Math::Float3 &deltaAngularAxis ); + + void MoveForward( ::Oyster::Math::Float distance ); + void MoveBackward( ::Oyster::Math::Float distance ); + void StrafeRight( ::Oyster::Math::Float distance ); + void StrafeLeft( ::Oyster::Math::Float distance ); + + void PitchUp( ::Oyster::Math::Float radian ); + void PitchDown( ::Oyster::Math::Float radian ); + void YawRight( ::Oyster::Math::Float radian ); + void YawLeft( ::Oyster::Math::Float radian ); + + const ::Oyster::Math::Float3 & GetHeadOffset() const; + const ::Oyster::Math::Float3 & GetPosition() const; + ::Oyster::Math::Float4x4 & GetViewMatrix( Oyster::Math::Float4x4 &targetMem = ::Oyster::Math::Float4x4() ) const; + const ::Oyster::Math::Float4x4 & GetProjectionMatrix() const; + ::Oyster::Math::Float4x4 & GetViewsProjMatrix( Oyster::Math::Float4x4 &targetMem = ::Oyster::Math::Float4x4() ) const; + ::Oyster::Math::Float3 GetNormalOf( const ::Oyster::Math::Float3 &axis ) const; + ::Oyster::Math::Float3 GetRight() const; + ::Oyster::Math::Float3 GetUp() const; + ::Oyster::Math::Float3 GetLook() const; + ::Oyster::Math::Float3 GetForward() const; + +private: + Camera_BasicV2 head; + ::Oyster::Math::Float pitchUp; + ::Oyster::Math::Float3 headOffset; + struct + { + ::Oyster::Math::Float3 translation; + ::Oyster::Math::Quaternion rotation; + } body; +}; + +#endif \ No newline at end of file diff --git a/Code/Misc/OysterMath/Quaternion.h b/Code/Misc/OysterMath/Quaternion.h index da790f75..f5ea5951 100644 --- a/Code/Misc/OysterMath/Quaternion.h +++ b/Code/Misc/OysterMath/Quaternion.h @@ -36,6 +36,7 @@ namespace LinearAlgebra const ScalarType & operator [] ( int i ) const; Quaternion & operator = ( const Quaternion &quaternion ); + Quaternion & operator *= ( const Quaternion &quaternion ); Quaternion & operator *= ( const ScalarType &scalar ); Quaternion & operator /= ( const ScalarType &scalar ); Quaternion & operator += ( const Quaternion &quaternion ); @@ -112,6 +113,12 @@ namespace LinearAlgebra return *this; } + template + Quaternion & Quaternion::operator *= ( const Quaternion &quaternion ) + { + return *this = *this * quaternion; + } + template Quaternion & Quaternion::operator *= ( const ScalarType &scalar ) {