New Cameras

V2 is quaternion based
This commit is contained in:
Dander7BD 2014-02-18 12:03:54 +01:00
parent 0a70839211
commit 2e75d168a1
6 changed files with 414 additions and 0 deletions

View File

@ -201,7 +201,9 @@
<ClCompile Include="DanBiasGame_Impl.cpp" />
<ClCompile Include="GameClientState\Camera_Basic.cpp" />
<ClCompile Include="GameClientState\Camera.cpp" />
<ClCompile Include="GameClientState\Camera_BasicV2.cpp" />
<ClCompile Include="GameClientState\Camera_FPS.cpp" />
<ClCompile Include="GameClientState\Camera_FPSV2.cpp" />
<ClCompile Include="GameClientState\C_obj\C_DynamicObj.cpp" />
<ClCompile Include="GameClientState\C_obj\C_Player.cpp" />
<ClCompile Include="GameClientState\C_obj\C_StaticObj.cpp" />
@ -226,7 +228,9 @@
<ClInclude Include="GameClientState\Buttons\EventButtonGUI.h" />
<ClInclude Include="GameClientState\Buttons\ButtonRectangle.h" />
<ClInclude Include="GameClientState\Camera.h" />
<ClInclude Include="GameClientState\Camera_BasicV2.h" />
<ClInclude Include="GameClientState\Camera_FPS.h" />
<ClInclude Include="GameClientState\Camera_FPSV2.h" />
<ClInclude Include="GameClientState\C_obj\C_DynamicObj.h" />
<ClInclude Include="GameClientState\C_obj\C_Player.h" />
<ClInclude Include="GameClientState\C_obj\C_StaticObj.h" />

View File

@ -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 );
}

View File

@ -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

View File

@ -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 );
}

View File

@ -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

View File

@ -36,6 +36,7 @@ namespace LinearAlgebra
const ScalarType & operator [] ( int i ) const;
Quaternion<ScalarType> & operator = ( const Quaternion<ScalarType> &quaternion );
Quaternion<ScalarType> & operator *= ( const Quaternion<ScalarType> &quaternion );
Quaternion<ScalarType> & operator *= ( const ScalarType &scalar );
Quaternion<ScalarType> & operator /= ( const ScalarType &scalar );
Quaternion<ScalarType> & operator += ( const Quaternion<ScalarType> &quaternion );
@ -112,6 +113,12 @@ namespace LinearAlgebra
return *this;
}
template<typename ScalarType>
Quaternion<ScalarType> & Quaternion<ScalarType>::operator *= ( const Quaternion<ScalarType> &quaternion )
{
return *this = *this * quaternion;
}
template<typename ScalarType>
Quaternion<ScalarType> & Quaternion<ScalarType>::operator *= ( const ScalarType &scalar )
{