From 8138afb6b59a5b958e947f090cda10bf12a11b25 Mon Sep 17 00:00:00 2001 From: Erik Persson Date: Fri, 29 Nov 2013 09:00:12 +0100 Subject: [PATCH] object update --- Code/GameLogic/Camera.cpp | 182 ++++++++++++++++++++++++++++++++++++++ Code/GameLogic/Camera.h | 63 +++++++++++++ Code/GameLogic/Object.cpp | 9 +- 3 files changed, 246 insertions(+), 8 deletions(-) create mode 100644 Code/GameLogic/Camera.cpp create mode 100644 Code/GameLogic/Camera.h diff --git a/Code/GameLogic/Camera.cpp b/Code/GameLogic/Camera.cpp new file mode 100644 index 00000000..bad0bdf9 --- /dev/null +++ b/Code/GameLogic/Camera.cpp @@ -0,0 +1,182 @@ +#include "Camera.h" + +Camera::Camera() +{ + this->m_position = Oyster::Math::Float3(0, 50, 0); + this->mRight = Oyster::Math::Float3(1, 0, 0); + this->mUp = Oyster::Math::Float3(0, 1, 0); + this->mLook = Oyster::Math::Float3(0, 0, 1); +} + +Camera::~Camera() +{ +} + +void Camera::SetPosition(const Oyster::Math::Float3& v) +{ + this->m_position = v; +} + +Oyster::Math::Float3 Camera::GetPosition()const +{ + return this->m_position; +} + +Oyster::Math::Float3 Camera::GetRight()const +{ + return this->mRight; +} + +Oyster::Math::Float3 Camera::GetUp()const +{ + return this->mUp; +} + +Oyster::Math::Float3 Camera::GetLook()const +{ + return this->mLook; +} + +float Camera::GetNearZ()const +{ + return this->mNearZ; +} + +float Camera::GetFarZ()const +{ + return this->mFarZ; +} + +float Camera::GetAspect()const +{ + return this->mAspect; +} + +Oyster::Math::Float3 Camera::CrossMatrix(const Oyster::Math::Float3& vector, const Oyster::Math::Float4x4& matrix) +{ + Oyster::Math::Float3 vec; + vec.x = matrix.m11*vector.x + matrix.m12*vector.y + matrix.m13*vector.z; + vec.y = matrix.m21*vector.x + matrix.m22*vector.y + matrix.m23*vector.z; + vec.z = matrix.m31*vector.x + matrix.m32*vector.y + matrix.m33*vector.z; + return vec; +} + +void Camera::SetLens(float fovY, float aspect, float zn, float zf) +{ + this->mFovY = fovY; + this->mAspect = aspect; + this->mNearZ = zn; + this->mFarZ = zf; + + float yScale = tan((Oyster::Math::pi*0.5f) - (mFovY*0.5f)); + float xScale = yScale/this->mAspect; + + mProj = Oyster::Math::Float4x4(xScale, 0, 0, 0, + 0, yScale, 0, 0, + 0, 0, zf/(zf-zn), 1, + 0, 0, -zn*zf/(zf-zn), 0); +} + +void Camera::LookAt(Oyster::Math::Float3 pos, Oyster::Math::Float3 target, Oyster::Math::Float3 worldUp) +{ + Oyster::Math::Float3 L; + + D3DXVec3Subtract(&L, &target, &pos); + L = target - pos; + D3DXVec3Normalize(&L, &L); + Oyster::Math::Float3 R; + D3DXVec3Cross(&R, &worldUp, &L); + D3DXVec3Normalize(&R, &R); + Oyster::Math::Float3 U; + D3DXVec3Cross(&U, &L, &R); + + this->m_position = pos; + this->mLook = L; + this->mRight = R; + this->mUp = U; +} + +Oyster::Math::Float4x4 Camera::View()const +{ + return this->mView; +} + +Oyster::Math::Float4x4 Camera::Proj()const +{ + return this->mProj; +} + +Oyster::Math::Float4x4 Camera::ViewsProj()const +{ + Oyster::Math::Float4x4 M; + D3DXMatrixMultiply(&M, &this->mView, &this->mProj); + return M; +} + +void Camera::Walk(float dist) +{ + this->m_position += dist*Oyster::Math::Float3(1,0,0); +} + +void Camera::Strafe(float dist) +{ + this->m_position += dist*this->mRight; +} + +void Camera::Pitch(float angle) +{ + float radians = angle * 0.0174532925f; + + Oyster::Math::Float4x4 R; + + D3DXMatrixRotationAxis(&R, &-mRight, radians); + this->mUp = CrossMatrix(this->mUp, R); + this->mLook = CrossMatrix(this->mLook, R); +} + +void Camera::Yaw(float angle) +{ + float radians = angle * 0.0174532925f; + + Oyster::Math::Float4x4 R; + + Oyster::Math::Float3 up(0,1,0); + D3DXMatrixRotationAxis(&R, &-up, radians); + + this->mRight = CrossMatrix(this->mRight, R); + this->mUp = CrossMatrix(mUp, R); + this->mLook = CrossMatrix(this->mLook, R); +} + +void Camera::UpdateViewMatrix() +{ + D3DXVec3Normalize(&this->mLook, &this->mLook); + D3DXVec3Cross(&this->mUp, &this->mLook, &this->mRight); + D3DXVec3Normalize(&this->mUp, &this->mUp); + D3DXVec3Cross(&this->mRight, &this->mUp, &this->mLook); + + + float x = -D3DXVec3Dot(&this->m_position, &mRight); + float y = -D3DXVec3Dot(&this->m_position, &mUp); + float z = -D3DXVec3Dot(&this->m_position, &mLook); + + this->mView(0, 0) = this->mRight.x; + this->mView(1, 0) = this->mRight.y; + this->mView(2, 0) = this->mRight.z; + this->mView(3, 0) = x; + + this->mView(0, 1) = this->mUp.x; + this->mView(1, 1) = this->mUp.y; + this->mView(2, 1) = this->mUp.z; + this->mView(3, 1) = y; + + this->mView(0, 2) = this->mLook.x; + this->mView(1, 2) = this->mLook.y; + this->mView(2, 2) = this->mLook.z; + this->mView(3, 2) = z; + + this->mView(0, 3) = 0.0f; + this->mView(1, 3) = 0.0f; + this->mView(2, 3) = 0.0f; + this->mView(3, 3) = 1.0f; +} \ No newline at end of file diff --git a/Code/GameLogic/Camera.h b/Code/GameLogic/Camera.h new file mode 100644 index 00000000..deb9df62 --- /dev/null +++ b/Code/GameLogic/Camera.h @@ -0,0 +1,63 @@ +#ifndef CAMERA__H +#define CAMERA__H + +#include "OysterMath.h" + +class Camera +{ +private: + + Oyster::Math::Float3 m_position; + Oyster::Math::Float3 mRight; + Oyster::Math::Float3 mUp; + Oyster::Math::Float3 mLook; + + + + float mNearZ; + float mFarZ; + float mAspect; + float mFovY; + + Oyster::Math::Float4x4 mView; + Oyster::Math::Float4x4 mProj; + +public: + Camera(); + virtual ~Camera(); + + void SetPosition(const Oyster::Math::Float3& v); + + Oyster::Math::Float3 GetPosition()const; + + Oyster::Math::Float3 GetRight()const; + Oyster::Math::Float3 GetUp()const; + Oyster::Math::Float3 GetLook()const; + + float GetNearZ()const; + float GetFarZ()const; + float GetAspect()const; + + Oyster::Math::Float3 CrossMatrix(const Oyster::Math::Float3& v, const Oyster::Math::Float4x4& m); + + void SetLens(float fovY, float aspect, float zn, float zf); + + void LookAt(Oyster::Math::Float3 pos, Oyster::Math::Float3 target, Oyster::Math::Float3 worldUp); + + void setLook(Oyster::Math::Float3 look) { mLook = look; } + void setUp(Oyster::Math::Float3 up) { mUp = up; } + void setRight(Oyster::Math::Float3 right) { mRight = right; } + + Oyster::Math::Float4x4 View()const; + Oyster::Math::Float4x4 Proj()const; + Oyster::Math::Float4x4 ViewsProj()const; + + void Walk(float dist); + void Strafe(float dist); + + void Pitch(float angle); + void Yaw(float angle); + + void UpdateViewMatrix(); +}; +#endif \ No newline at end of file diff --git a/Code/GameLogic/Object.cpp b/Code/GameLogic/Object.cpp index a83d1fb6..ddcb4656 100644 --- a/Code/GameLogic/Object.cpp +++ b/Code/GameLogic/Object.cpp @@ -18,14 +18,7 @@ Object::Object(void) model = new Model(); model = Oyster::Graphics::API::CreateModel(L"bth.obj"); - ICustomBody* temp = rigidBody = API::Instance().CreateSimpleRigidBody().Release(); - - rigidBody->SetCenter(Float3(50,0,0)); - rigidBody->SetMass_KeepMomentum(30); - rigidBody->SetSize(Float3(2,2,2)); - rigidBody->SetSubscription(true); - rigidBody->SetMomentOfInertiaTensor_KeepMomentum(Float4x4(MomentOfInertia::CreateCuboidMatrix(30, 2, 2, 2))); - + ICustomBody* temp = rigidBody = API::Instance().CreateRigidBody().Release(); GameLogic::RefManager::getInstance()->AddMapping(*rigidBody, *this);