diff --git a/src/AnimCamera.cpp b/src/AnimCamera.cpp index eae251d..4d2454f 100644 --- a/src/AnimCamera.cpp +++ b/src/AnimCamera.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include namespace Donut @@ -92,18 +91,18 @@ std::unique_ptr AnimCamera::LoadP3D(const std::string& filename) return std::make_unique(p3d.GetRoot()); } -glm::mat4 AnimCamera::Update(double dt) +Matrix4x4 AnimCamera::Update(double dt) { - return glm::mat4(1.0f); + return Matrix4x4::Identity; //const auto& trans = _trans->Evaluate(0, (float)_time); //const auto& forward = _forward->EvaluateDirection(0, (float)_time); - //const auto& up = glm::vec3(0, 1, 0); + //const auto& up = Vector3(0, 1, 0); //const auto& right = glm::normalize(glm::cross(up, forward)); - //glm::mat3 rotation(right.x, up.x, forward.x, right.y, up.y, forward.y, right.z, up.z, forward.z); - //auto lookAt = glm::quat_cast(rotation); + //Matrix3x3 rotation(right.x, up.x, forward.x, right.y, up.y, forward.y, right.z, up.z, forward.z); + //auto lookAt = Quat_cast(rotation); //_time += dt; - //return glm::toMat4(lookAt) * glm::translate(glm::mat4(1.0f), -glm::vec3(trans[3])); + //return glm::toMat4(lookAt) * glm::translate(Matrix(1.0f), -Vector3(trans[3])); } } // namespace Donut diff --git a/src/AnimCamera.h b/src/AnimCamera.h index 899a0bb..1e5d0cd 100644 --- a/src/AnimCamera.h +++ b/src/AnimCamera.h @@ -2,12 +2,15 @@ #pragma once -#include +#include "Core/Math/Fwd.h" + #include #include namespace Donut { + +/* forward declarations */ class SkinAnimation; namespace P3D @@ -22,7 +25,7 @@ class AnimCamera static std::unique_ptr LoadP3D(const std::string& filename); - glm::mat4 Update(double dt); + Matrix4x4 Update(double dt); private: double _time; diff --git a/src/Character.cpp b/src/Character.cpp index 235673c..8223db3 100644 --- a/src/Character.cpp +++ b/src/Character.cpp @@ -16,7 +16,7 @@ namespace Donut { Character::Character(std::string name): - _name(std::move(name)), _position(glm::vec3(0.0f)), _rotation(glm::quat()) + _name(std::move(name)), _position(Vector3(0.0f)), _rotation(Quaternion()) { _characterController = std::make_unique(this, &Game::GetInstance().GetWorldPhysics()); _boneBuffer = std::make_unique(); @@ -79,13 +79,15 @@ void Character::LoadAnimations(const std::string& name) SetAnimation(_animations.begin()->first); } -void Character::Draw(const glm::mat4& viewProjection, GL::ShaderProgram& shaderProgram, const ResourceManager& rm) +void Character::Draw(const Matrix4x4& viewProjection, GL::ShaderProgram& shaderProgram, const ResourceManager& rm) { - const auto localPosition = _position; // -glm::vec3(0.0f, _characterController->GetShape().getHalfHeight() * 2, 0.0f); - const glm::mat4 mvp = glm::translate(viewProjection, localPosition) * glm::toMat4(_rotation); + // wtf just + const auto localPosition = _position; // -Vector3(0.0f, _characterController->GetShape().getHalfHeight() * 2, 0.0f); + //const Matrix4x4 mvp = glm::translate(viewProjection, localPosition) * glm::toMat4(_rotation); + //const Matrix4x4 mvp = glm::translate(viewProjection, localPosition) * glm::toMat4(_rotation); shaderProgram.Bind(); // todo optimize: should already be bound? - shaderProgram.SetUniformValue("viewProj", mvp); + shaderProgram.SetUniformValue("viewProj", viewProjection); shaderProgram.SetUniformValue("diffuseTex", 0); // todo optimize: should already be set shaderProgram.SetUniformValue("boneBuffer", 1); // todo optimize: should already be set @@ -118,19 +120,19 @@ void Character::Update(double deltatime) // update our bonebuffer auto joints = _skeleton->GetJoints(); - std::vector matrices(joints.size()); + std::vector matrices(joints.size()); for (auto i = 0; i < joints.size(); i++) matrices[i] = joints[i].finalGlobal; - _boneBuffer->SetBuffer(matrices.data(), matrices.size() * sizeof(glm::mat4)); + _boneBuffer->SetBuffer(matrices.data(), matrices.size() * sizeof(Matrix4x4)); } -void Character::SetPosition(const glm::vec3& position) +void Character::SetPosition(const Vector3& position) { _position = position; _characterController->warp(BulletCast(position)); } -void Character::SetRotation(const glm::quat& rotation) +void Character::SetRotation(const Quaternion& rotation) { _rotation = rotation; } @@ -158,8 +160,8 @@ void Character::addAnimation(const P3D::Animation& p3dAnim) auto track = std::make_unique(joint.name); const auto& jointRestPose = joint.rest; - const auto& jointTranslation = jointRestPose[3]; - const auto& jointRotation = glm::quat_cast(jointRestPose); + const auto& jointTranslation = jointRestPose.Translation(); + const auto& jointRotation = jointRestPose.Quat(); if (groupNameIndex.find(joint.name) == groupNameIndex.end()) { @@ -202,7 +204,7 @@ void Character::addAnimation(const P3D::Animation& p3dAnim) float x = (int16_t)((value >> 16) & 0xFFFF) / (float)0x7FFF; float w = (int16_t)(value & 0xFFFF) / (float)0x7FFF; - track->AddRotationKey(frames[i], glm::quat(w, x, y, z)); + track->AddRotationKey(frames[i], Quaternion(w, x, y, z)); } } else diff --git a/src/Character.h b/src/Character.h index 62408f5..189b6e5 100644 --- a/src/Character.h +++ b/src/Character.h @@ -2,8 +2,10 @@ #pragma once -#include -#include +#include "Core/Math/Matrix4x4.h" +#include "Core/Math/Quaternion.h" +#include "Core/Math/Vector3.h" + #include #include #include @@ -37,9 +39,9 @@ struct SkeletonJoint { std::string name; uint32_t parent; - glm::mat4 restPose; + Matrix4x4 restPose; - SkeletonJoint(std::string name, const uint32_t parent, const glm::mat4 restPose): + SkeletonJoint(std::string name, const uint32_t parent, const Matrix4x4 restPose): name(std::move(name)), parent(parent), restPose(restPose) { } @@ -57,16 +59,16 @@ class Character const std::string& GetName() const { return _name; } const std::string& GetModelName() const { return _modelName; } const std::string& GetAnimName() const { return _animName; } - void SetPosition(const glm::vec3& position); - void SetRotation(const glm::quat& rotation); - const glm::vec3& GetPosition() const { return _position; } - const glm::quat& GetRotation() const { return _rotation; } + void SetPosition(const Vector3& position); + void SetRotation(const Quaternion& rotation); + const Vector3& GetPosition() const { return _position; } + const Quaternion& GetRotation() const { return _rotation; } void SetAnimation(const std::string&); CharacterController& GetCharacterController() const { return *_characterController; } Skeleton& GetSkeleton() const { return *_skeleton; } void Update(double deltatime); - void Draw(const glm::mat4& viewProjection, GL::ShaderProgram&, const ResourceManager&); + void Draw(const Matrix4x4& viewProjection, GL::ShaderProgram&, const ResourceManager&); // maybe change this to just anim names const std::unordered_map>& GetAnimations() const { return _animations; } @@ -82,8 +84,8 @@ class Character std::string _modelName; // just for debug select std::string _animName; // just for debug select - glm::vec3 _position; - glm::quat _rotation; + Vector3 _position; + Quaternion _rotation; std::unique_ptr _characterController; diff --git a/src/CharacterController.cpp b/src/CharacterController.cpp index 68e6800..08b9f97 100644 --- a/src/CharacterController.cpp +++ b/src/CharacterController.cpp @@ -12,7 +12,7 @@ namespace Donut CharacterController::CharacterController(Character* character, WorldPhysics* physics): _character(character), _worldPhysics(physics) { - _walkDirection = glm::vec3(0.0f, 0.0f, 0.0f); + _walkDirection = Vector3(0.0f, 0.0f, 0.0f); _verticalVelocity = 0.0f; _verticalOffset = 0.0f; _stepHeight = 0.05; @@ -58,7 +58,7 @@ void CharacterController::reset(btCollisionWorld* collisionWorld) {} void CharacterController::setWalkDirection(const btVector3& walkDirection) { - _walkDirection = BulletCast(walkDirection); + _walkDirection = BulletCast(walkDirection); } void CharacterController::setVelocityForTimeInterval(const btVector3& velocity, btScalar timeInterval) @@ -73,7 +73,7 @@ void CharacterController::warp(const btVector3& origin) transform.setOrigin(origin); _physGhostObject->setWorldTransform(transform); - _position = BulletCast(origin); + _position = BulletCast(origin); _targetPosition = origin; } @@ -83,7 +83,7 @@ void CharacterController::preStep(btCollisionWorld* collisionWorld) while (recoverFromPenetration(collisionWorld) && numPenetrationLoops < 4) numPenetrationLoops++; - _position = BulletCast(_physGhostObject->getWorldTransform().getOrigin()); + _position = BulletCast(_physGhostObject->getWorldTransform().getOrigin()); _targetPosition = BulletCast(_position); } @@ -108,7 +108,7 @@ void CharacterController::playerStep(btCollisionWorld* collisionWorld, btScalar // stepForwardAndStrafe() stepDown(collisionWorld, dt); - _position = BulletCast(_targetPosition); + _position = BulletCast(_targetPosition); transform.setOrigin(BulletCast(_position)); _physGhostObject->setWorldTransform(transform); _character->SetPosition(_position); @@ -191,7 +191,7 @@ bool CharacterController::recoverFromPenetration(btCollisionWorld* collisionWorl collisionWorld->getDispatchInfo(), collisionWorld->getDispatcher()); - _position = BulletCast(_physGhostObject->getWorldTransform().getOrigin()); + _position = BulletCast(_physGhostObject->getWorldTransform().getOrigin()); btScalar maxPenetration = 0.0; @@ -233,7 +233,7 @@ bool CharacterController::recoverFromPenetration(btCollisionWorld* collisionWorl _touchingNormal = pt.m_normalWorldOnB * directionSign; } - _position += BulletCast(pt.m_normalWorldOnB * directionSign * dist * 0.2); + _position += BulletCast(pt.m_normalWorldOnB * directionSign * dist * 0.2); penetration = true; } } @@ -266,8 +266,8 @@ btVector3 CharacterController::perpendicularComponent(const btVector3& direction void CharacterController::stepUp(btCollisionWorld* world) { btTransform start, end; - _targetPosition = BulletCast(_position + glm::vec3(0.0f, 1.0f, 0.0f) * _stepHeight); - _position = BulletCast(_targetPosition); + _targetPosition = BulletCast(_position + Vector3(0.0f, 1.0f, 0.0f) * _stepHeight); + _position = BulletCast(_targetPosition); } void CharacterController::updateTargetPositionBasedOnCollision(const btVector3& hitNormal, diff --git a/src/CharacterController.h b/src/CharacterController.h index 306a605..370ec47 100644 --- a/src/CharacterController.h +++ b/src/CharacterController.h @@ -5,8 +5,7 @@ #include #include #include -#include -#include + #include class btDiscreteDynamicsWorld; @@ -64,12 +63,12 @@ class CharacterController: public btCharacterControllerInterface btConvexShape* _convexShape; btManifoldArray _manifoldArray; - glm::vec3 _position; - glm::quat _rotation; + Vector3 _position; + Quaternion _rotation; btVector3 _targetPosition; - glm::vec3 _walkDirection; + Vector3 _walkDirection; btScalar _verticalVelocity; btScalar _verticalOffset; btScalar _stepHeight; diff --git a/src/Core/BoundingBox.h b/src/Core/BoundingBox.h deleted file mode 100644 index 5714c09..0000000 --- a/src/Core/BoundingBox.h +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2019 the donut authors. See AUTHORS.md - -#pragma once - -#include - -namespace Donut -{ - -class BoundingBox -{ - public: - BoundingBox(): - _min(glm::vec3(0.0f)), _max(glm::vec3(0.0f)) {} - BoundingBox(const glm::vec3& min, const glm::vec3& max): - _min(min), _max(max) {} - - glm::vec3 GetMin() const { return _min; } - glm::vec3 GetMax() const { return _max; } - - private: - glm::vec3 _min; - glm::vec3 _max; -}; - -} // namespace Donut diff --git a/src/Core/Math/BoundingBox.h b/src/Core/Math/BoundingBox.h new file mode 100644 index 0000000..5fe6973 --- /dev/null +++ b/src/Core/Math/BoundingBox.h @@ -0,0 +1,26 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#pragma once + +#include "Core/Math/Vector3.h" + +namespace Donut +{ + +class BoundingBox +{ + public: + BoundingBox(): + _min(Vector3(0.0f)), _max(Vector3(0.0f)) {} + BoundingBox(const Vector3& min, const Vector3& max): + _min(min), _max(max) {} + + Vector3 GetMin() const { return _min; } + Vector3 GetMax() const { return _max; } + + private: + Vector3 _min; + Vector3 _max; +}; + +} // namespace Donut diff --git a/src/Core/BoundingSphere.h b/src/Core/Math/BoundingSphere.h similarity index 58% rename from src/Core/BoundingSphere.h rename to src/Core/Math/BoundingSphere.h index 840b56f..bb043a7 100644 --- a/src/Core/BoundingSphere.h +++ b/src/Core/Math/BoundingSphere.h @@ -2,7 +2,7 @@ #pragma once -#include +#include "Core/Math/Vector3.h" namespace Donut { @@ -11,15 +11,15 @@ class BoundingSphere { public: BoundingSphere(): - _center(glm::vec3(0.0f)), _radius(0.0f) {} - BoundingSphere(const glm::vec3& center, const float radius): + _center(Vector3(0.0f)), _radius(0.0f) {} + BoundingSphere(const Vector3& center, const float radius): _center(center), _radius(radius) {} - glm::vec3 GetCenter() const { return _center; } + Vector3 GetCenter() const { return _center; } float GetRadius() const { return _radius; } private: - glm::vec3 _center; + Vector3 _center; float _radius; }; diff --git a/src/Core/Math/Fwd.h b/src/Core/Math/Fwd.h new file mode 100644 index 0000000..94fa3ea --- /dev/null +++ b/src/Core/Math/Fwd.h @@ -0,0 +1,15 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#pragma once + +namespace Donut +{ +struct Matrix3x3; +struct Matrix4x4; +struct Quaternion; +struct Vector2; +struct Vector2Int; +struct Vector3; +struct Vector3Int; +struct Vector4; +} // namespace Donut diff --git a/src/Core/Math/Math.cpp b/src/Core/Math/Math.cpp new file mode 100644 index 0000000..2790f8f --- /dev/null +++ b/src/Core/Math/Math.cpp @@ -0,0 +1,12 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#include "Math.h" + +namespace Donut +{ +const float Math::Pi = 3.1415926535897932f; +const float Math::Pi2 = 6.2831853071795865f; +const float Math::InvPi = 0.31830988618f; +const float Math::HalfPi = 1.57079632679f; +const float Math::Epsilon = 1.192092896e-07F; +} diff --git a/src/Core/Math/Math.h b/src/Core/Math/Math.h new file mode 100644 index 0000000..9b44045 --- /dev/null +++ b/src/Core/Math/Math.h @@ -0,0 +1,54 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#pragma once + +#include "Core/Platform.h" + +#include + +namespace Donut +{ +struct Math +{ + static const float Pi; + static const float Pi2; + static const float InvPi; + static const float HalfPi; + static const float Epsilon; + + static FORCEINLINE float CeilF(float value) { return ceilf(value); } + static FORCEINLINE float Sin(float value) { return sinf(value); } + static FORCEINLINE float Cos(float value) { return cosf(value); } + static FORCEINLINE float Tan(float value) { return tanf(value); } + static FORCEINLINE float Sqrt(float value) { return sqrtf(value); } + static FORCEINLINE float InvSqrt(float value) { return 1.0f / sqrtf(value); } + + template + static FORCEINLINE T Clamp(const T x, const T min, const T max) + { + return x < min ? min : x < max ? x : max; + } + + template + static FORCEINLINE T Square(const T A) + { + return A * A; + } + + template + static constexpr FORCEINLINE T Abs(const T A) + { + return (A >= (T)0) ? A : -A; + } + + static FORCEINLINE float RadiansToDegrees(float rad) + { + return rad * (180.0f / Pi); + } + + static FORCEINLINE float DegreesToRadians(float deg) + { + return deg * (Pi / 180.0f); + } +}; +} // namespace Donut diff --git a/src/Core/Math/Matrix3x3.cpp b/src/Core/Math/Matrix3x3.cpp new file mode 100644 index 0000000..15f1111 --- /dev/null +++ b/src/Core/Math/Matrix3x3.cpp @@ -0,0 +1,25 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#include "Matrix3x3.h" + +#include "Core/Math/Math.h" +#include "Core/Math/Quaternion.h" + +namespace Donut +{ + +const Matrix3x3 Matrix3x3::Identity(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f); + +Quaternion Matrix3x3::Quat() const +{ + return Quaternion(*this); +} + +Matrix3x3 Matrix3x3::Transpose() const +{ + return Matrix3x3(M[0][0], M[1][0], M[2][0], + M[0][1], M[1][1], M[2][1], + M[0][2], M[1][2], M[2][2]); +} + +} // namespace Donut diff --git a/src/Core/Math/Matrix3x3.h b/src/Core/Math/Matrix3x3.h new file mode 100644 index 0000000..d64d97b --- /dev/null +++ b/src/Core/Math/Matrix3x3.h @@ -0,0 +1,79 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#pragma once + +#include "Core/Math/Quaternion.h" +#include "Core/Math/Vector3.h" +#include "Core/Platform.h" + +#include +#include +#include +#include + +namespace Donut +{ + +struct Matrix3x3 +{ + public: + float M[3][3]; + + static const Matrix3x3 Identity; + + FORCEINLINE Matrix3x3(); + FORCEINLINE Matrix3x3(float xx, float xy, float xz, + float yx, float yy, float yz, + float zx, float zy, float zz); + + FORCEINLINE Matrix3x3 operator*(const Matrix3x3& Other) const; + FORCEINLINE Vector3 operator*(const Vector3& vec) const; + FORCEINLINE void operator*=(const Matrix3x3& Other); + FORCEINLINE Matrix3x3 operator+(const Matrix3x3& Other) const; + FORCEINLINE void operator+=(const Matrix3x3& Other); + FORCEINLINE Matrix3x3 operator*(float Other) const; + FORCEINLINE void operator*=(float Other); + inline bool operator==(const Matrix3x3& Other) const; + inline bool operator!=(const Matrix3x3& Other) const; + + FORCEINLINE Matrix3x3 operator-() const; + + inline Matrix3x3 Inverse() const; + Matrix3x3 Transpose() const; + + Quaternion Quat() const; + + std::string ToString() const; +}; + +FORCEINLINE Matrix3x3::Matrix3x3() +{ +} + +FORCEINLINE Matrix3x3::Matrix3x3(float xx, float xy, float xz, + float yx, float yy, float yz, + float zx, float zy, float zz) +{ + // clang-format off + M[0][0] = xx; M[0][1] = xy; M[0][2] = xz; + M[1][0] = yx; M[1][1] = yy; M[1][2] = yz; + M[2][0] = zx; M[2][1] = zy; M[2][2] = zz; + // clang-format on +} + +FORCEINLINE Vector3 Matrix3x3::operator*(const Vector3& v) const +{ + return Vector3(M[0][0] * v.X + M[0][1] * v.Y + M[0][2] * v.Z, + M[1][0] * v.X + M[1][1] * v.Y + M[1][2] * v.Z, + M[2][0] * v.X + M[2][1] * v.Y + M[2][2] * v.Z); +} + +FORCEINLINE Matrix3x3 Matrix3x3::operator-() const +{ + return Matrix3x3( + -M[0][0], -M[0][1], -M[0][2], + -M[1][0], -M[1][1], -M[1][2], + -M[2][0], -M[2][1], -M[2][2]); +} + +} // namespace Donut diff --git a/src/Core/Math/Matrix4x4.cpp b/src/Core/Math/Matrix4x4.cpp new file mode 100644 index 0000000..5f6ef4f --- /dev/null +++ b/src/Core/Math/Matrix4x4.cpp @@ -0,0 +1,113 @@ +#include "Matrix4x4.h" + +#include "Core/Math/Math.h" +#include "Core/Math/Matrix3x3.h" +#include "Core/Math/Quaternion.h" + +namespace Donut +{ + +const Matrix4x4 Matrix4x4::Zero(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); +const Matrix4x4 Matrix4x4::Identity(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); + +Quaternion Matrix4x4::Quat() const +{ + return Quaternion(*this); +} + +Matrix4x4 Matrix4x4::Inverse() const +{ + float det = Determinant(); + + if (det) + { + det = 1.0f / det; + + return Matrix4x4(det * (M[1][1] * (M[2][2] * M[3][3] - M[3][2] * M[2][3]) + M[2][1] * (M[3][2] * M[1][3] - M[1][2] * M[3][3]) + M[3][1] * (M[1][2] * M[2][3] - M[2][2] * M[1][3])), det * (M[2][1] * (M[0][2] * M[3][3] - M[3][2] * M[0][3]) + M[3][1] * (M[2][2] * M[0][3] - M[0][2] * M[2][3]) + M[0][1] * (M[3][2] * M[2][3] - M[2][2] * M[3][3])), det * (M[3][1] * (M[0][2] * M[1][3] - M[1][2] * M[0][3]) + M[0][1] * (M[1][2] * M[3][3] - M[3][2] * M[1][3]) + M[1][1] * (M[3][2] * M[0][3] - M[0][2] * M[3][3])), det * (M[0][1] * (M[2][2] * M[1][3] - M[1][2] * M[2][3]) + M[1][1] * (M[0][2] * M[2][3] - M[2][2] * M[0][3]) + M[2][1] * (M[1][2] * M[0][3] - M[0][2] * M[1][3])), + det * (M[1][2] * (M[2][0] * M[3][3] - M[3][0] * M[2][3]) + M[2][2] * (M[3][0] * M[1][3] - M[1][0] * M[3][3]) + M[3][2] * (M[1][0] * M[2][3] - M[2][0] * M[1][3])), det * (M[2][2] * (M[0][0] * M[3][3] - M[3][0] * M[0][3]) + M[3][2] * (M[2][0] * M[0][3] - M[0][0] * M[2][3]) + M[0][2] * (M[3][0] * M[2][3] - M[2][0] * M[3][3])), det * (M[3][2] * (M[0][0] * M[1][3] - M[1][0] * M[0][3]) + M[0][2] * (M[1][0] * M[3][3] - M[3][0] * M[1][3]) + M[1][2] * (M[3][0] * M[0][3] - M[0][0] * M[3][3])), det * (M[0][2] * (M[2][0] * M[1][3] - M[1][0] * M[2][3]) + M[1][2] * (M[0][0] * M[2][3] - M[2][0] * M[0][3]) + M[2][2] * (M[1][0] * M[0][3] - M[0][0] * M[1][3])), + det * (M[1][3] * (M[2][0] * M[3][1] - M[3][0] * M[2][1]) + M[2][3] * (M[3][0] * M[1][1] - M[1][0] * M[3][1]) + M[3][3] * (M[1][0] * M[2][1] - M[2][0] * M[1][1])), det * (M[2][3] * (M[0][0] * M[3][1] - M[3][0] * M[0][1]) + M[3][3] * (M[2][0] * M[0][1] - M[0][0] * M[2][1]) + M[0][3] * (M[3][0] * M[2][1] - M[2][0] * M[3][1])), det * (M[3][3] * (M[0][0] * M[1][1] - M[1][0] * M[0][1]) + M[0][3] * (M[1][0] * M[3][1] - M[3][0] * M[1][1]) + M[1][3] * (M[3][0] * M[0][1] - M[0][0] * M[3][1])), det * (M[0][3] * (M[2][0] * M[1][1] - M[1][0] * M[2][1]) + M[1][3] * (M[0][0] * M[2][1] - M[2][0] * M[0][1]) + M[2][3] * (M[1][0] * M[0][1] - M[0][0] * M[1][1])), + det * (M[1][0] * (M[3][1] * M[2][2] - M[2][1] * M[3][2]) + M[2][0] * (M[1][1] * M[3][2] - M[3][1] * M[1][2]) + M[3][0] * (M[2][1] * M[1][2] - M[1][1] * M[2][2])), det * (M[2][0] * (M[3][1] * M[0][2] - M[0][1] * M[3][2]) + M[3][0] * (M[0][1] * M[2][2] - M[2][1] * M[0][2]) + M[0][0] * (M[2][1] * M[3][2] - M[3][1] * M[2][2])), det * (M[3][0] * (M[1][1] * M[0][2] - M[0][1] * M[1][2]) + M[0][0] * (M[3][1] * M[1][2] - M[1][1] * M[3][2]) + M[1][0] * (M[0][1] * M[3][2] - M[3][1] * M[0][2])), det * (M[0][0] * (M[1][1] * M[2][2] - M[2][1] * M[1][2]) + M[1][0] * (M[2][1] * M[0][2] - M[0][1] * M[2][2]) + M[2][0] * (M[0][1] * M[1][2] - M[1][1] * M[0][2]))); + } + + return Identity; +} + +Matrix4x4 Matrix4x4::Transpose() const +{ + return Matrix4x4(M[0][0], M[1][0], M[2][0], M[3][0], + M[0][1], M[1][1], M[2][1], M[3][1], + M[0][2], M[1][2], M[2][2], M[3][2], + M[0][3], M[1][3], M[2][3], M[3][3]); +} + +/** + * Makes a translation matrix from a Vector3. + */ +Matrix4x4 Matrix4x4::MakeTranslate(const Vector3& v) +{ + Matrix4x4 m; + + m.M[0][0] = 1.0f; m.M[0][1] = 0.0f; m.M[0][2] = 0.0f; m.M[0][3] = 0.0f; + m.M[1][0] = 0.0f; m.M[1][1] = 1.0f; m.M[1][2] = 0.0f; m.M[1][3] = 0.0f; + m.M[2][0] = 0.0f; m.M[2][1] = 0.0f; m.M[2][2] = 1.0f; m.M[2][3] = 0.0f; + m.M[3][0] = v.X; m.M[3][1] = v.Y; m.M[3][2] = v.Z; m.M[3][3] = 1.0f; + + return m; +} + +Matrix4x4 Matrix4x4::MakeView(const Vector3& position, const Quaternion& orientation) +{ + // View matrix is: + // + // [ Lx Uy Dz Tx ] + // [ Lx Uy Dz Ty ] + // [ Lx Uy Dz Tz ] + // [ 0 0 0 1 ] + // + // Where T = -(Rot.Transpose() * Pos) + + Matrix3x3 rot; + orientation.ToRotationMatrix(rot); + + // Make the translation relative to new axes + Matrix3x3 rotT = rot.Transpose(); + Vector3 trans = -rotT * position; + + // Make final matrix + Matrix4x4 viewMatrix(rotT); + viewMatrix[3][0] = trans.X; + viewMatrix[3][1] = trans.Y; + viewMatrix[3][2] = trans.Z; + + return viewMatrix; +} + +Matrix4x4 Matrix4x4::MakePerspective(float fov, float aspect, float nearZ, float farZ) +{ + // convert degrees to radians + fov = Math::DegreesToRadians(fov); + + float tanHalfFovY = Math::Tan(fov / 2.0f); + + Matrix4x4 m = Matrix4x4::Zero; + m[0][0] = 1.0f / (aspect * tanHalfFovY); + m[1][1] = 1.0f / (tanHalfFovY); + m[2][2] = (farZ + nearZ) / (farZ - nearZ); + m[2][3] = 1.0f; + m[3][2] = -(2.0f * farZ * nearZ) / (farZ - nearZ); + + return m; +} + +Matrix4x4 Matrix4x4::MakeOrtho(float left, float right, float bottom, float top) +{ + Matrix4x4 m( + 2.0f / (right - left), 0.0f, 0.0f, 0.0f, + 0.0f, 2.0f / (top - bottom), 0.0f, 0.0f, + 0.0f, 0.0f, -1.0f, 0.0f, + -(right + left) / (right - left), -(top + bottom) / (top - bottom), 0.0f, 1.0f); + + return m; +} + +} // namespace Donut diff --git a/src/Core/Math/Matrix4x4.h b/src/Core/Math/Matrix4x4.h new file mode 100644 index 0000000..c6bc825 --- /dev/null +++ b/src/Core/Math/Matrix4x4.h @@ -0,0 +1,161 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#pragma once + +#include "Core/Math/Matrix3x3.h" +#include "Core/Math/Vector3.h" +#include "Core/Math/Vector4.h" +#include "Core/Platform.h" + +#include +#include + +namespace Donut +{ + +struct Matrix3x3; +struct Quaternion; + +/* + Values are stored in the matrix in column major notation order same as OpenGL. + + [ M[0][0] M[0][1] M[0][2] M[0][3] ] + | M[1][0] M[1][1] M[1][2] M[1][3] | + | M[2][0] M[2][1] M[2][2] M[2][3] | + [ M[3][0] M[3][1] M[3][2] M[3][3] ] + +*/ +struct Matrix4x4 +{ + public: + union + { + MS_ALIGN(16) float M[4][4] GCC_ALIGN(16); + float M16[16]; + }; + + MS_ALIGN(16) + static const Matrix4x4 Zero GCC_ALIGN(16); + static const Matrix4x4 Identity GCC_ALIGN(16); + + FORCEINLINE Matrix4x4(); + FORCEINLINE Matrix4x4(float xx, float xy, float xz, float xw, + float yx, float yy, float yz, float yw, + float zx, float zy, float zz, float zw, + float wx, float wy, float wz, float ww); + FORCEINLINE Matrix4x4(Matrix3x3 m); + FORCEINLINE Matrix4x4(Quaternion rot); + + FORCEINLINE void operator=(const Matrix3x3& m); + + FORCEINLINE Matrix4x4 operator*(const Matrix4x4& Other) const; + FORCEINLINE Matrix4x4 operator*(const Vector3& v) const; + FORCEINLINE Matrix4x4 operator*(const Vector4& v) const; + FORCEINLINE void operator*=(const Matrix4x4& Other); + FORCEINLINE Matrix4x4 operator+(const Matrix4x4& Other) const; + FORCEINLINE void operator+=(const Matrix4x4& Other); + FORCEINLINE Matrix4x4 operator*(float Other) const; + FORCEINLINE void operator*=(float Other); + inline bool operator==(const Matrix4x4& Other) const; + inline bool operator!=(const Matrix4x4& Other) const; + + inline float* operator[](std::size_t row); + inline const float* operator[](std::size_t row) const; + + FORCEINLINE operator float*() { return M16; } + FORCEINLINE operator const float*() const { return M16; } + + FORCEINLINE float Determinant() const; + Matrix4x4 Inverse() const; + Matrix4x4 Transpose() const; + + // Simple decompose methods + FORCEINLINE Vector3 Translation() const; + Quaternion Quat() const; + + // Static factory methods + static Matrix4x4 MakeTranslate(const Vector3& v); + static Matrix4x4 MakeView(const Vector3& position, const Quaternion& rotation); + static Matrix4x4 MakePerspective(float fov, float aspect, float nearZ, float farZ); + static Matrix4x4 MakeOrtho(float left, float right, float bottom, float top); + + std::string ToString() const; +}; + +#pragma warning(suppress : 26495) // don't initalize for speed +FORCEINLINE Matrix4x4::Matrix4x4() +{ +} + +FORCEINLINE Matrix4x4::Matrix4x4(float xx, float xy, float xz, float xw, + float yx, float yy, float yz, float yw, + float zx, float zy, float zz, float zw, + float wx, float wy, float wz, float ww) +{ + // clang-format off + M[0][0] = xx; M[0][1] = xy; M[0][2] = xz; M[0][3] = xw; + M[1][0] = yx; M[1][1] = yy; M[1][2] = yz; M[1][3] = yw; + M[2][0] = zx; M[2][1] = zy; M[2][2] = zz; M[2][3] = zw; + M[3][0] = wx; M[3][1] = wy; M[3][2] = wz; M[3][3] = ww; + // clang-format on +} + +FORCEINLINE Matrix4x4::Matrix4x4(Matrix3x3 m) +{ + M[0][0] = m.M[0][0]; M[0][1] = m.M[0][1]; M[0][2] = m.M[0][2]; M[0][3] = 0.0f; + M[1][0] = m.M[1][0]; M[1][1] = m.M[1][1]; M[1][2] = m.M[1][2]; M[1][3] = 0.0f; + M[2][0] = m.M[2][0]; M[2][1] = m.M[2][1]; M[2][2] = m.M[2][2]; M[2][3] = 0.0f; + M[3][0] = 0.0f; M[3][1] = 0.0f; M[3][2] = 0.0f; M[3][3] = 1.0f; +} + +// mykeis +FORCEINLINE Matrix4x4 Matrix4x4::operator*(const Matrix4x4& m) const +{ + return Matrix4x4( + M[0][0] * m.M[0][0] + M[1][0] * m.M[0][1] + M[2][0] * m.M[0][2] + M[3][0] * m.M[0][3], + M[0][1] * m.M[0][0] + M[1][1] * m.M[0][1] + M[2][1] * m.M[0][2] + M[3][1] * m.M[0][3], + M[0][2] * m.M[0][0] + M[1][2] * m.M[0][1] + M[2][2] * m.M[0][2] + M[3][2] * m.M[0][3], + M[0][3] * m.M[0][0] + M[1][3] * m.M[0][1] + M[2][3] * m.M[0][2] + M[3][3] * m.M[0][3], + + M[0][0] * m.M[1][0] + M[1][0] * m.M[1][1] + M[2][0] * m.M[1][2] + M[3][0] * m.M[1][3], + M[0][1] * m.M[1][0] + M[1][1] * m.M[1][1] + M[2][1] * m.M[1][2] + M[3][1] * m.M[1][3], + M[0][2] * m.M[1][0] + M[1][2] * m.M[1][1] + M[2][2] * m.M[1][2] + M[3][2] * m.M[1][3], + M[0][3] * m.M[1][0] + M[1][3] * m.M[1][1] + M[2][3] * m.M[1][2] + M[3][3] * m.M[1][3], + + M[0][0] * m.M[2][0] + M[1][0] * m.M[2][1] + M[2][0] * m.M[2][2] + M[3][0] * m.M[2][3], + M[0][1] * m.M[2][0] + M[1][1] * m.M[2][1] + M[2][1] * m.M[2][2] + M[3][1] * m.M[2][3], + M[0][2] * m.M[2][0] + M[1][2] * m.M[2][1] + M[2][2] * m.M[2][2] + M[3][2] * m.M[2][3], + M[0][3] * m.M[2][0] + M[1][3] * m.M[2][1] + M[2][3] * m.M[2][2] + M[3][3] * m.M[2][3], + + M[0][0] * m.M[3][0] + M[1][0] * m.M[3][1] + M[2][0] * m.M[3][2] + M[3][0] * m.M[3][3], + M[0][1] * m.M[3][0] + M[1][1] * m.M[3][1] + M[2][1] * m.M[3][2] + M[3][1] * m.M[3][3], + M[0][2] * m.M[3][0] + M[1][2] * m.M[3][1] + M[2][2] * m.M[3][2] + M[3][2] * m.M[3][3], + M[0][3] * m.M[3][0] + M[1][3] * m.M[3][1] + M[2][3] * m.M[3][2] + M[3][3] * m.M[3][3]); +} + + +inline float* Matrix4x4::operator[](std::size_t row) +{ + assert(row < 4); + return M[row]; +} + +inline const float* Matrix4x4::operator[](std::size_t row) const +{ + assert(row < 4); + return M[row]; +} + +FORCEINLINE float Matrix4x4::Determinant() const +{ + return (M[0][0] * M[1][1] - M[1][0] * M[0][1]) * (M[2][2] * M[3][3] - M[3][2] * M[2][3]) - (M[0][0] * M[2][1] - M[2][0] * M[0][1]) * (M[1][2] * M[3][3] - M[3][2] * M[1][3]) + + (M[0][0] * M[3][1] - M[3][0] * M[0][1]) * (M[1][2] * M[2][3] - M[2][2] * M[1][3]) + (M[1][0] * M[2][1] - M[2][0] * M[1][1]) * (M[0][2] * M[3][3] - M[3][2] * M[0][3]) - + (M[1][0] * M[3][1] - M[3][0] * M[1][1]) * (M[0][2] * M[2][3] - M[2][2] * M[0][3]) + (M[2][0] * M[3][1] - M[3][0] * M[2][1]) * (M[0][2] * M[1][3] - M[1][2] * M[0][3]); +} + +FORCEINLINE Vector3 Matrix4x4::Translation() const +{ + return Vector3(M[0][3], M[1][3], M[2][3]); +} + +} // namespace Donut diff --git a/src/Core/Math/Quaternion.cpp b/src/Core/Math/Quaternion.cpp new file mode 100644 index 0000000..e213fb9 --- /dev/null +++ b/src/Core/Math/Quaternion.cpp @@ -0,0 +1,187 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#include "Quaternion.h" + +#include "Core/Math/Math.h" +#include "Core/Math/Matrix3x3.h" +#include "Core/Math/Matrix4x4.h" +#include "Core/Math/Vector3.h" + +namespace Donut +{ + +const Quaternion Quaternion::Zero(0.0f, 0.0f, 0.0f, 0.0f); +const Quaternion Quaternion::Identity(0.0f, 0.0f, 0.0f, 1.0f); + +Quaternion::Quaternion(const Matrix3x3& rot) +{ + FromRotationMatrix(rot); +} + +Quaternion::Quaternion(const Matrix4x4& rot) +{ + FromRotationMatrix(rot); +} + +Quaternion::Quaternion(Vector3 axis, float angle) +{ + // Check whether the angle is 0, in that case we do not need to calculate sin/cos stuff... + if (Math::Abs(angle) < Math::Epsilon) + { + X = 0.0f; + Y = 0.0f; + Z = 0.0f; + W = 1.0f; + return; + } + + float sinAngle = Math::Sin(angle * 0.5f); + + X = axis.X * sinAngle; + Y = axis.Y * sinAngle; + Z = axis.Z * sinAngle; + W = Math::Cos(angle * 0.5f); +} + + +void Quaternion::ToRotationMatrix(Matrix3x3& m) const +{ + const float x2 = X + X; const float y2 = Y + Y; const float z2 = Z + Z; + const float xx = X * x2; const float xy = X * y2; const float xz = X * z2; + const float yy = Y * y2; const float yz = Y * z2; const float zz = Z * z2; + const float wx = W * x2; const float wy = W * y2; const float wz = W * z2; + + m.M[0][0] = 1.0f - (yy + zz); m.M[1][0] = xy - wz; m.M[2][0] = xz + wy; + m.M[0][1] = xy + wz; m.M[1][1] = 1.0f - (xx + zz); m.M[2][1] = yz - wx; + m.M[0][2] = xz - wy; m.M[1][2] = yz + wx; m.M[2][2] = 1.0f - (xx + yy); +} + +void Quaternion::ToRotationMatrix(Matrix4x4& m) const +{ + const float x2 = X + X; const float y2 = Y + Y; const float z2 = Z + Z; + const float xx = X * x2; const float xy = X * y2; const float xz = X * z2; + const float yy = Y * y2; const float yz = Y * z2; const float zz = Z * z2; + const float wx = W * x2; const float wy = W * y2; const float wz = W * z2; + + m.M[0][0] = 1.0f - (yy + zz); m.M[1][0] = xy - wz; m.M[2][0] = xz + wy; m.M[3][0] = 0.0f; + m.M[0][1] = xy + wz; m.M[1][1] = 1.0f - (xx + zz); m.M[2][1] = yz - wx; m.M[3][1] = 0.0f; + m.M[0][2] = xz - wy; m.M[1][2] = yz + wx; m.M[2][2] = 1.0f - (xx + yy); m.M[3][2] = 0.0f; + m.M[0][3] = 0.0f; m.M[1][3] = 0.0f; m.M[2][3] = 0.0f; m.M[3][3] = 0.0f; +} + +Quaternion& Quaternion::FromRotationMatrix(const Matrix3x3& m) +{ + return *this; // TODO +} + +Quaternion& Quaternion::FromRotationMatrix(const Matrix4x4& m) +{ + // todo: IF NULL RETURN IDENTITY QUAT + // todo: MAKE SURE ROTATION PART IS UNIT LENGTH + + float s; + const float tr = m.M[0][0] + m.M[1][1] + m.M[2][2]; + + if (tr > 0.0f) + { + float InvS = Math::InvSqrt(tr + 1.f); + this->W = 0.5f * (1.f / InvS); + s = 0.5f * InvS; + + this->X = (m.M[1][2] - m.M[2][1]) * s; + this->Y = (m.M[2][0] - m.M[0][2]) * s; + this->Z = (m.M[0][1] - m.M[1][0]) * s; + } + else + { + // diagonal is negative + int32_t i = 0; + + if (m.M[1][1] > m.M[0][0]) + i = 1; + + if (m.M[2][2] > m.M[i][i]) + i = 2; + + static const int32_t nxt[3] = { 1, 2, 0 }; + const int32_t j = nxt[i]; + const int32_t k = nxt[j]; + + s = m.M[i][i] - m.M[j][j] - m.M[k][k] + 1.0f; + + float InvS = Math::InvSqrt(s); + + float qt[4]; + qt[i] = 0.5f * (1.f / InvS); + + s = 0.5f * InvS; + + qt[3] = (m.M[j][k] - m.M[k][j]) * s; + qt[j] = (m.M[i][j] + m.M[j][i]) * s; + qt[k] = (m.M[i][k] + m.M[k][i]) * s; + + this->X = qt[0]; + this->Y = qt[1]; + this->Z = qt[2]; + this->W = qt[3]; + } + + return *this; +} + +/** +* @brief +* Returns the x (left) axis +*/ +Vector3 Quaternion::GetXAxis() const +{ + float ty = 2.0f * Y; + float tz = 2.0f * Z; + float twy = ty * W; + float twz = tz * W; + float txy = ty * X; + float txz = tz * X; + float tyy = ty * Y; + float tzz = tz * Z; + + return Vector3(1.0f - (ty + tz), txy + twz, txz - twy); +} + +/** +* @brief +* Returns the y (up) axis +*/ +Vector3 Quaternion::GetYAxis() const +{ + float tx = 2.0f * X; + float ty = 2.0f * Y; + float tz = 2.0f * Z; + float twx = tx * W; + float twz = tz * W; + float txx = tx * X; + float txy = ty * X; + float tyz = tz * Y; + float tzz = tz * Z; + + return Vector3(txy - twz, 1.0f - (txx + tzz), tyz + twx); +} + +/** +* @brief +* Returns the z (forward) axis +*/ +Vector3 Quaternion::GetZAxis() const +{ + float tx = 2.0f * X; + float ty = 2.0f * Y; + float tz = 2.0f * Z; + float twx = tx * W; + float twy = ty * W; + float txx = tx * X; + float txz = tz * X; + float tyy = ty * Y; + float tyz = tz * Y; + return Vector3(txz + twy, tyz - twx, 1.0f - (txx + tyy)); +} + +} diff --git a/src/Core/Math/Quaternion.h b/src/Core/Math/Quaternion.h new file mode 100644 index 0000000..68920bf --- /dev/null +++ b/src/Core/Math/Quaternion.h @@ -0,0 +1,250 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#pragma once + +#include "Core/Math/Math.h" +#include "Core/Math/Vector3.h" +#include "Core/Math/Vector4.h" +#include "Core/Platform.h" + +#include +#include + +namespace Donut +{ + +struct Vector3; +struct Vector4; +struct Matrix3x3; +struct Matrix4x4; + +MS_ALIGN(16) +struct Quaternion +{ + public: + float X, Y, Z, W; + + public: + static const Quaternion Zero; + static const Quaternion Identity; + + public: + // constructors + FORCEINLINE Quaternion(); + FORCEINLINE Quaternion(float x, float y, float z, float w); + Quaternion(Vector3 axis, float angle); + Quaternion(const Matrix3x3& rot); + Quaternion(const Matrix4x4& rot); + + // operators + FORCEINLINE Quaternion operator+(const Quaternion& qQ) const; + FORCEINLINE Quaternion& operator+=(const Quaternion& qQ); + FORCEINLINE Quaternion operator-() const; + FORCEINLINE Quaternion operator-(const Quaternion& qQ) const; + FORCEINLINE Quaternion& operator-=(const Quaternion& qQ); + FORCEINLINE Quaternion operator*(float f) const; + FORCEINLINE Quaternion& operator*=(float f); + FORCEINLINE Quaternion operator*(const Quaternion& qQ) const; + FORCEINLINE Quaternion& operator*=(const Quaternion& qQ); + FORCEINLINE Vector3 operator*(const Vector3& vV) const; + FORCEINLINE Vector4 operator*(const Vector4& vV) const; + FORCEINLINE Quaternion operator/(float f) const; + FORCEINLINE Quaternion& operator/=(float f); + FORCEINLINE Quaternion& operator=(const Quaternion& qQ); + FORCEINLINE bool operator==(const Quaternion& qQ) const; + FORCEINLINE bool operator!=(const Quaternion& qQ) const; + + Vector3 Euler() const; + FORCEINLINE Quaternion Inverse() const; + FORCEINLINE Quaternion Normal() const; + + Quaternion& FromRotationMatrix(const Matrix3x3& rot); + Quaternion& FromRotationMatrix(const Matrix4x4& rot); + void ToRotationMatrix(Matrix3x3& rot) const; + void ToRotationMatrix(Matrix4x4& rot) const; + + Vector3 GetXAxis() const; + Vector3 GetYAxis() const; + Vector3 GetZAxis() const; + + static Quaternion MakeFromEuler(const Vector3& Euler); + + FORCEINLINE std::string ToString() const; +}; + +#pragma warning(suppress : 26495) // don't initalize for speed +Quaternion::Quaternion() +{ +} + +Quaternion::Quaternion(float x, float y, float z, float w): + X(x), Y(y), Z(z), W(w) +{ +} + +// operators +FORCEINLINE Quaternion Quaternion::operator+(const Quaternion& q) const +{ + return Quaternion(X + q.X, Y + q.Y, Z + q.Z, W + q.W); +} + +FORCEINLINE Quaternion& Quaternion::operator+=(const Quaternion& qQ) +{ + X += qQ.X; + Y += qQ.Y; + Z += qQ.Z; + W += qQ.W; + + return *this; +} + +FORCEINLINE Quaternion Quaternion::operator-() const +{ + const float norm = X * X + Y * Y + Z * Z + W * W; + if (norm == 0.0f) + return Quaternion::Zero; + + const float invNorm = 1.0f / norm; + return Quaternion(-X * invNorm, -Y * invNorm, -Z * invNorm, W * invNorm); +} + +FORCEINLINE Quaternion Quaternion::operator-(const Quaternion& q) const +{ + return Quaternion(X - q.X, Y - q.Y, Z - q.Z, W - q.W); +} + +FORCEINLINE Quaternion& Quaternion::operator-=(const Quaternion& q) +{ + X -= q.X; + Y -= q.Y; + Z -= q.Z; + W -= q.W; + + return *this; +} + +FORCEINLINE Quaternion Quaternion::operator*(float f) const +{ + return Quaternion(X * f, Y * f, Z * f, W * f); +} + +FORCEINLINE Quaternion& Quaternion::operator*=(float f) +{ + X *= f; + Y *= f; + Z *= f; + W *= f; + + return *this; +} + +FORCEINLINE Quaternion Quaternion::operator*(const Quaternion& q) const +{ + return Quaternion(W * q.X + X * q.W + Y * q.Z - Z * q.Y, + W * q.Y + Y * q.W + Z * q.X - X * q.Z, + W * q.Z + Z * q.W + X * q.Y - Y * q.X, + W * q.W - X * q.X - Y * q.Y - Z * q.Z); +} + +FORCEINLINE Quaternion& Quaternion::operator*=(const Quaternion& q) +{ + const float qx = X, qy = Y, qz = Z, qw = W; + X = qw * q.X + qx * q.W + qy * q.Z - qz * q.Y; + Y = qw * q.Y + qy * q.W + qz * q.X - qx * q.Z; + Z = qw * q.Z + qz * q.W + qx * q.Y - qy * q.X; + W = qw * q.W - qx * q.X - qy * q.Y - qz * q.Z; + + return *this; +} + +FORCEINLINE Vector3 Quaternion::operator*(const Vector3& v) const +{ + const float x2 = X * X; + const float y2 = Y * Y; + const float z2 = Z * Z; + const float w2 = W * W; + const float xa = X * v.X; + const float yb = Y * v.Y; + const float zc = Z * v.Z; + return Vector3(v.X * (x2 - y2 - z2 + w2) + 2 * (W * (Y * v.Z - Z * v.Y) + X * (yb + zc)), + v.Y * (-x2 + y2 - z2 + w2) + 2 * (W * (Z * v.X - X * v.Z) + Y * (xa + zc)), + v.Z * (-x2 - y2 + z2 + w2) + 2 * (W * (X * v.Y - Y * v.X) + Z * (xa + yb))); +} + +FORCEINLINE Vector4 Quaternion::operator*(const Vector4& v) const +{ + const float x2 = X * X; + const float y2 = Y * Y; + const float z2 = Z * Z; + const float w2 = W * W; + const float xa = X * v.X; + const float yb = Y * v.Y; + const float zc = Z * v.Z; + return Vector4(v.X * (x2 - y2 - z2 + w2) + 2 * (W * (Y * v.Z - Z * v.Y) + X * (yb + zc)), + v.Y * (-x2 + y2 - z2 + w2) + 2 * (W * (Z * v.X - X * v.Z) + Y * (xa + zc)), + v.Z * (-x2 - y2 + z2 + w2) + 2 * (W * (X * v.Y - Y * v.X) + Z * (xa + yb)), + 1.0f); +} + +FORCEINLINE Quaternion Quaternion::operator/(float f) const +{ + return Quaternion(X / f, Y / f, Z / f, W / f); +} + +FORCEINLINE Quaternion& Quaternion::operator/=(float f) +{ + X /= f; + Y /= f; + Z /= f; + W /= f; + + return *this; +} + +FORCEINLINE Quaternion& Quaternion::operator=(const Quaternion& q) +{ + X = q.X; + Y = q.Y; + Z = q.Z; + W = q.W; + + return *this; +} + +FORCEINLINE bool Quaternion::operator==(const Quaternion& q) const +{ + return (X == q.X && Y == q.Y && Z == q.Z && W == q.W); +} + +FORCEINLINE bool Quaternion::operator!=(const Quaternion& q) const +{ + return (X != q.X || Y != q.Y || Z != q.Z || W != q.W); +} + +FORCEINLINE Quaternion Quaternion::Inverse() const +{ + const float norm = X * X + Y * Y + Z * Z + W * W; + + if (norm == 0.0f) + return Quaternion::Zero; + + const float invNorm = 1.0f / norm; + return Quaternion(-X * invNorm, -Y * invNorm, -Z * invNorm, W * invNorm); +} + +FORCEINLINE Quaternion Quaternion::Normal() const +{ + const float u = X * X + Y * Y + Z * Z + W * W; + if (u == 0.0f) + return Quaternion::Zero; + + const float factor = Math::InvSqrt(u); + return Quaternion(X * factor, Y * factor, Z * factor, W * factor); +} + +FORCEINLINE std::string Quaternion::ToString() const +{ + return fmt::format("X={:3.3f} Y={:3.3f} Z={:3.3f} W={:3.3f}", X, Y, Z, W); +} + +} // namespace Donut diff --git a/src/Core/Math/Vector2.cpp b/src/Core/Math/Vector2.cpp new file mode 100644 index 0000000..d26e76b --- /dev/null +++ b/src/Core/Math/Vector2.cpp @@ -0,0 +1,10 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#include "Vector2.h" + +namespace Donut +{ + +const Vector2 Vector2::Zero(0.0f, 0.0f); + +} // namespace Donut diff --git a/src/Core/Math/Vector2.h b/src/Core/Math/Vector2.h new file mode 100644 index 0000000..421fff1 --- /dev/null +++ b/src/Core/Math/Vector2.h @@ -0,0 +1,221 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#pragma once + +#include "Core/Platform.h" +#include "Core/Math/Math.h" +#include "Core/Math/Vector2Int.h" + +#include +#include +#include +#include + +namespace Donut +{ + +struct Vector2 +{ + public: + float X, Y; + + public: + static const Vector2 Zero; + + public: + FORCEINLINE Vector2(); + FORCEINLINE constexpr Vector2(const float x, const float y); + FORCEINLINE Vector2(const Vector2Int& v); + explicit FORCEINLINE Vector2(const float scaler); + + // arithmetic operators + FORCEINLINE constexpr Vector2 operator+(const Vector2& a) const; + FORCEINLINE constexpr Vector2 operator-(const Vector2& a) const; + FORCEINLINE constexpr Vector2 operator*(const Vector2& a) const; + FORCEINLINE constexpr Vector2 operator/(const Vector2& a) const; + FORCEINLINE constexpr Vector2 operator*(const float a) const; + FORCEINLINE constexpr Vector2 operator/(const float a) const; + + // comparison operators + FORCEINLINE constexpr bool operator==(const Vector2& a) const; + FORCEINLINE constexpr bool operator!=(const Vector2& a) const; + + // negated copy + FORCEINLINE constexpr Vector2 operator-() const; + + // arithmetic updater operators + FORCEINLINE constexpr Vector2 operator+=(const Vector2& a); + FORCEINLINE constexpr Vector2 operator-=(const Vector2& a); + FORCEINLINE constexpr Vector2 operator*=(const Vector2& a); + FORCEINLINE constexpr Vector2 operator/=(const Vector2& a); + FORCEINLINE constexpr Vector2 operator*=(float scale); + FORCEINLINE constexpr Vector2 operator/=(float scale); + + // get specific components + FORCEINLINE constexpr float& operator[](std::size_t i); + FORCEINLINE constexpr float operator[](std::size_t i) const; + + FORCEINLINE float Length() const; + FORCEINLINE float LengthSquared() const; + FORCEINLINE Vector2 Normalize() const; + + FORCEINLINE static float Distance(const Vector2& v1, const Vector2& v2); + FORCEINLINE static float DistanceSquared(const Vector2& v1, const Vector2& v2); + + public: + std::string ToString() const; +}; + +/* shove all the code here so we can inline everything */ + +#pragma warning(suppress : 26495) // don't initalize for speed +FORCEINLINE Vector2::Vector2() +{ +} + +FORCEINLINE constexpr Vector2::Vector2(const float x, const float y): + X(x), Y(y) +{ +} + +FORCEINLINE Vector2::Vector2(const float scaler): + X(scaler), Y(scaler) +{ +} + +FORCEINLINE Vector2::Vector2(const Vector2Int& v): + X(static_cast(v.X)), Y(static_cast(v.Y)) +{ +} + + +// comparison operators +FORCEINLINE constexpr bool Vector2::operator==(const Vector2& a) const +{ + return X == a.X && Y == a.Y; +} + +FORCEINLINE constexpr bool Vector2::operator!=(const Vector2& a) const +{ + return X != a.X || Y != a.Y; +} + +// arithmetic operators +FORCEINLINE constexpr Vector2 Vector2::operator+(const Vector2& a) const +{ + return Vector2(X + a.X, Y + a.Y); +} + +FORCEINLINE constexpr Vector2 Vector2::operator-(const Vector2& a) const +{ + return Vector2(X - a.X, Y - a.Y); +} + +FORCEINLINE constexpr Vector2 Vector2::operator*(const float scale) const +{ + return Vector2(X * scale, Y * scale); +} + +FORCEINLINE constexpr Vector2 Vector2::operator*(const Vector2& a) const +{ + return Vector2(X * a.X, Y * a.Y); +} + +FORCEINLINE constexpr Vector2 Vector2::operator/(float scale) const +{ + assert(scale != 0.0); + + const float rScale = 1.f / scale; + return Vector2(X * rScale, Y * rScale); +} + +FORCEINLINE constexpr Vector2 Vector2::operator/(const Vector2& a) const +{ + return Vector2(X / a.X, Y / a.Y); +} + +FORCEINLINE constexpr Vector2 Vector2::operator+=(const Vector2& a) +{ + X += a.X; + Y += a.Y; + return *this; +} + +FORCEINLINE constexpr Vector2 Vector2::operator-=(const Vector2& a) +{ + X -= a.X; + Y -= a.Y; + return *this; +} + +FORCEINLINE constexpr Vector2 Vector2::operator*=(float scale) +{ + X *= scale; + Y *= scale; + return *this; +} + +FORCEINLINE constexpr Vector2 Vector2::operator*=(const Vector2& a) +{ + X *= a.X; + Y *= a.Y; + return *this; +} + +FORCEINLINE constexpr Vector2 Vector2::operator/=(float scale) +{ + assert(scale != 0.0); + + const float rScale = 1.f / scale; + X *= rScale; + Y *= rScale; + return *this; +} + +FORCEINLINE constexpr Vector2 Vector2::operator/=(const Vector2& a) +{ + X /= a.X; + Y /= a.Y; + return *this; +} + +FORCEINLINE constexpr Vector2 Vector2::operator-() const +{ + return Vector2(-X, -Y); +} + +// access operators +FORCEINLINE constexpr float Vector2::operator[](std::size_t i) const +{ + assert(i >= 0 && i < 2); + return (&X)[i]; +} + +FORCEINLINE constexpr float& Vector2::operator[](std::size_t i) +{ + assert(i >= 0 && i < 2); + return (&X)[i]; +} + +FORCEINLINE float Vector2::Length() const +{ + return Math::Sqrt(X * X + Y * Y); +} + +FORCEINLINE float Vector2::LengthSquared() const +{ + return X * X + Y * Y; +} + +FORCEINLINE Vector2 Vector2::Normalize() const +{ + const float squareSum = X * X + Y * Y; + + if (squareSum == 0.0f) + return Vector2::Zero; + + const float scale = Math::InvSqrt(squareSum); + return Vector2(X * scale, Y * scale); +} + +} // namespace Donut diff --git a/src/Core/Math/Vector2Int.h b/src/Core/Math/Vector2Int.h new file mode 100644 index 0000000..5713682 --- /dev/null +++ b/src/Core/Math/Vector2Int.h @@ -0,0 +1,78 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#pragma once + +#include "Core/Platform.h" + +#include +#include +#include +#include + +namespace Donut +{ + +struct Vector2Int +{ + public: + int X, Y; + + public: + static const Vector2Int Zero; + + public: + FORCEINLINE Vector2Int(); + FORCEINLINE Vector2Int(const int x, const int y); + explicit FORCEINLINE Vector2Int(const int scaler); + + FORCEINLINE Vector2Int operator+(const Vector2Int& v) const; + FORCEINLINE Vector2Int operator-(const Vector2Int& v) const; + FORCEINLINE Vector2Int operator*(int Scale) const; + Vector2Int operator/(int Scale) const; + FORCEINLINE Vector2Int operator+(int A) const; + FORCEINLINE Vector2Int operator-(int A) const; + FORCEINLINE Vector2Int operator*(const Vector2Int& v) const; + Vector2Int operator/(const Vector2Int& v) const; + FORCEINLINE float operator|(const Vector2Int& v) const; + FORCEINLINE float operator^(const Vector2Int& v) const; + + bool operator==(const Vector2Int& v) const; + bool operator!=(const Vector2Int& v) const; + + FORCEINLINE Vector2Int operator-() const; + + FORCEINLINE Vector2Int operator+=(const Vector2Int& v); + FORCEINLINE Vector2Int operator-=(const Vector2Int& v); + FORCEINLINE Vector2Int operator*=(int Scale); + Vector2Int operator/=(int V); + Vector2Int operator*=(const Vector2Int& v); + Vector2Int operator/=(const Vector2Int& v); + + int& operator[](std::size_t Index); + int operator[](std::size_t Index) const; + + FORCEINLINE static float Distance(const Vector2Int& v1, const Vector2Int& v2); + FORCEINLINE static float DistanceSquared(const Vector2Int& v1, const Vector2Int& v2); + + public: + std::string ToString() const; +}; + +/* shove all the code here so we can inline everything */ + +#pragma warning(suppress : 26495) // don't initalize for speed +FORCEINLINE Vector2Int::Vector2Int() +{ +} + +FORCEINLINE Vector2Int::Vector2Int(const int x, const int y): + X(x), Y(y) +{ +} + +FORCEINLINE Vector2Int::Vector2Int(const int scaler): + X(scaler), Y(scaler) +{ +} + +} // namespace Donut diff --git a/src/Core/Math/Vector3.cpp b/src/Core/Math/Vector3.cpp new file mode 100644 index 0000000..ab7d83c --- /dev/null +++ b/src/Core/Math/Vector3.cpp @@ -0,0 +1,17 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#include "Vector3.h" + +namespace Donut +{ + +const Vector3 Vector3::Zero(0.0f, 0.0f, 0.0f); +const Vector3 Vector3::One(1.0f, 1.0f, 1.0f); +const Vector3 Vector3::Up(0.0f, 1.0f, 0.0f); +const Vector3 Vector3::Down(0.0f, -1.0f, 0.0f); +const Vector3 Vector3::Forward(0.0f, 0.0f, 1.0f); +const Vector3 Vector3::Backward(0.0f, 0.0f, -1.0f); +const Vector3 Vector3::Right(1.0f, 0.0f, 0.0f); +const Vector3 Vector3::Left(-1.0f, 0.0f, 0.0f); + +} // namespace Ogre diff --git a/src/Core/Math/Vector3.h b/src/Core/Math/Vector3.h new file mode 100644 index 0000000..056c4ac --- /dev/null +++ b/src/Core/Math/Vector3.h @@ -0,0 +1,241 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#pragma once + +#include "Core/Math/Math.h" +#include "Core/Platform.h" + +#include +#include +#include +#include + +namespace Donut +{ + +struct Vector3 +{ + public: + float X, Y, Z; + + public: + static const Vector3 Zero; + static const Vector3 One; + static const Vector3 Up; + static const Vector3 Down; + static const Vector3 Forward; + static const Vector3 Backward; + static const Vector3 Right; + static const Vector3 Left; + + public: + FORCEINLINE Vector3(); + FORCEINLINE constexpr Vector3(const float x, const float y, const float z); + explicit FORCEINLINE constexpr Vector3(const float scaler); + + // arithmetic operators + FORCEINLINE constexpr Vector3 operator+(const Vector3& a) const; + FORCEINLINE constexpr Vector3 operator-(const Vector3& a) const; + FORCEINLINE constexpr Vector3 operator*(const Vector3& a) const; + FORCEINLINE constexpr Vector3 operator/(const Vector3& a) const; + FORCEINLINE constexpr Vector3 operator*(const float a) const; + FORCEINLINE constexpr Vector3 operator/(const float a) const; + + // comparison operators + FORCEINLINE constexpr bool operator==(const Vector3& a) const; + FORCEINLINE constexpr bool operator!=(const Vector3& a) const; + + // negated copy + FORCEINLINE constexpr Vector3 operator-() const; + + // arithmetic updater operators + FORCEINLINE constexpr Vector3 operator+=(const Vector3& a); + FORCEINLINE constexpr Vector3 operator-=(const Vector3& a); + FORCEINLINE constexpr Vector3 operator*=(const Vector3& a); + FORCEINLINE constexpr Vector3 operator/=(const Vector3& a); + FORCEINLINE constexpr Vector3 operator*=(float scale); + FORCEINLINE constexpr Vector3 operator/=(float scale); + + // get specific components + FORCEINLINE constexpr float& operator[](std::size_t i); + FORCEINLINE constexpr float operator[](std::size_t i) const; + + public: + FORCEINLINE float Length() const; + FORCEINLINE float LengthSquared() const; + FORCEINLINE Vector3 Normalize() const; + + std::string ToString() const; + + static FORCEINLINE float Distance(const Vector3& a, const Vector3& b); + static FORCEINLINE float DistanceSquared(const Vector3& a, const Vector3& b); +}; + +/* shove all the code here so we can inline everything */ + +#pragma warning(suppress : 26495) // don't initalize for speed +FORCEINLINE Vector3::Vector3() +{ +} + +FORCEINLINE constexpr Vector3::Vector3(const float x, const float y, const float z): + X(x), Y(y), Z(z) +{ +} + +FORCEINLINE constexpr Vector3::Vector3(const float scaler): + X(scaler), Y(scaler), Z(scaler) +{ +} + +// comparison operators +FORCEINLINE constexpr bool Vector3::operator==(const Vector3& a) const +{ + return X == a.X && Y == a.Y && Z == a.Z; +} + +FORCEINLINE constexpr bool Vector3::operator!=(const Vector3& a) const +{ + return X != a.X || Y != a.Y || Z != a.Z; +} + +// arithmetic operators +FORCEINLINE constexpr Vector3 Vector3::operator+(const Vector3& a) const +{ + return Vector3(X + a.X, Y + a.Y, Z + a.Z); +} + +FORCEINLINE constexpr Vector3 Vector3::operator-(const Vector3& a) const +{ + return Vector3(X - a.X, Y - a.Y, Z - a.Z); +} + +FORCEINLINE constexpr Vector3 Vector3::operator*(const float scale) const +{ + return Vector3(X * scale, Y * scale, Z * scale); +} + +FORCEINLINE constexpr Vector3 Vector3::operator*(const Vector3& a) const +{ + return Vector3(X * a.X, Y * a.Y, Z * a.Z); +} + +FORCEINLINE constexpr Vector3 Vector3::operator/(float scale) const +{ + assert(scale != 0.0); + + const float rScale = 1.f / scale; + return Vector3(X * rScale, Y * rScale, Z * rScale); +} + +FORCEINLINE constexpr Vector3 Vector3::operator/(const Vector3& a) const +{ + return Vector3(X / a.X, Y / a.Y, Z / a.Z); +} + +FORCEINLINE constexpr Vector3 Vector3::operator+=(const Vector3& a) +{ + X += a.X; + Y += a.Y; + Z += a.Z; + return *this; +} + +FORCEINLINE constexpr Vector3 Vector3::operator-=(const Vector3& a) +{ + X -= a.X; + Y -= a.Y; + Z -= a.Z; + return *this; +} + +FORCEINLINE constexpr Vector3 Vector3::operator*=(float scale) +{ + X *= scale; + Y *= scale; + Z *= scale; + return *this; +} + +FORCEINLINE constexpr Vector3 Vector3::operator*=(const Vector3& a) +{ + X *= a.X; + Y *= a.Y; + Z *= a.Z; + return *this; +} + +FORCEINLINE constexpr Vector3 Vector3::operator/=(float scale) +{ + assert(scale != 0.0); + + const float rScale = 1.f / scale; + X *= rScale; + Y *= rScale; + Z *= rScale; + return *this; +} + +FORCEINLINE constexpr Vector3 Vector3::operator/=(const Vector3& a) +{ + X /= a.X; + Y /= a.Y; + Z /= a.Z; + return *this; +} + +FORCEINLINE constexpr Vector3 Vector3::operator-() const +{ + return Vector3(-X, -Y, -Z); +} + +// access operators +FORCEINLINE constexpr float Vector3::operator[](std::size_t i) const +{ + assert(i >= 0 && i < 3); + return (&X)[i]; +} + +FORCEINLINE constexpr float& Vector3::operator[](std::size_t i) +{ + assert(i >= 0 && i < 3); + return (&X)[i]; +} + +FORCEINLINE float Vector3::Length() const +{ + return Math::Sqrt(X * X + Y * Y + Z * Z); +} + +FORCEINLINE float Vector3::LengthSquared() const +{ + return X * X + Y * Y + Z * Z; +} + +FORCEINLINE Vector3 Vector3::Normalize() const +{ + const float squareSum = X * X + Y * Y + Z * Z; + + if (squareSum == 0.0f) + return Vector3::Zero; + + const float scale = Math::InvSqrt(squareSum); + return Vector3(X * scale, Y * scale, Z * scale); +} + +FORCEINLINE std::string Vector3::ToString() const +{ + return fmt::format("X={:3.3f} Y={:3.3f} Z={:3.3f}", X, Y, Z); +} + +FORCEINLINE float Vector3::Distance(const Vector3& a, const Vector3& b) +{ + return Math::Sqrt(Vector3::DistanceSquared(a, b)); +} + +FORCEINLINE float Vector3::DistanceSquared(const Vector3& a, const Vector3& b) +{ + return Math::Square(b.X - a.X) + Math::Square(b.Y - a.Y) + Math::Square(b.Z - a.Z); +} + +} // namespace Donut diff --git a/src/Core/Math/Vector3Int.cpp b/src/Core/Math/Vector3Int.cpp new file mode 100644 index 0000000..570b44d --- /dev/null +++ b/src/Core/Math/Vector3Int.cpp @@ -0,0 +1,11 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#include "Vector3Int.h" + +namespace Donut +{ + +const Vector3Int Vector3Int::Zero(0, 0, 0); +const Vector3Int Vector3Int::One(1, 1, 1); + +} // namespace Donut diff --git a/src/Core/Math/Vector3Int.h b/src/Core/Math/Vector3Int.h new file mode 100644 index 0000000..2b947c7 --- /dev/null +++ b/src/Core/Math/Vector3Int.h @@ -0,0 +1,192 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#pragma once + +#include "Core/Platform.h" + +#include +#include +#include +#include + +namespace Donut +{ + + +struct Vector3Int +{ + public: + int X, Y, Z; + + public: + static const Vector3Int Zero; + static const Vector3Int One; + + public: + FORCEINLINE Vector3Int(); + FORCEINLINE constexpr Vector3Int(const int x, const int y, const int z); + explicit FORCEINLINE constexpr Vector3Int(const int scaler); + + // arithmetic operators + FORCEINLINE constexpr Vector3Int operator+(const Vector3Int& a) const; + FORCEINLINE constexpr Vector3Int operator-(const Vector3Int& a) const; + FORCEINLINE constexpr Vector3Int operator*(const Vector3Int& a) const; + FORCEINLINE constexpr Vector3Int operator/(const Vector3Int& a) const; + FORCEINLINE constexpr Vector3Int operator*(const int a) const; + FORCEINLINE constexpr Vector3Int operator/(const int a) const; + + // comparison operators + FORCEINLINE constexpr bool operator==(const Vector3Int& a) const; + FORCEINLINE constexpr bool operator!=(const Vector3Int& a) const; + + // negated copy + FORCEINLINE constexpr Vector3Int operator-() const; + + // arithmetic updater operators + FORCEINLINE constexpr Vector3Int operator+=(const Vector3Int& a); + FORCEINLINE constexpr Vector3Int operator-=(const Vector3Int& a); + FORCEINLINE constexpr Vector3Int operator*=(const Vector3Int& a); + FORCEINLINE constexpr Vector3Int operator/=(const Vector3Int& a); + FORCEINLINE constexpr Vector3Int operator*=(int scale); + FORCEINLINE constexpr Vector3Int operator/=(int scale); + + // get specific components + FORCEINLINE constexpr int& operator[](std::size_t i); + FORCEINLINE constexpr int operator[](std::size_t i) const; + + public: + float Length() const; + float LengthSquared() const; + Vector3Int Normalize(); + + std::string ToString() const; + + static FORCEINLINE float Distance(const Vector3Int& a, const Vector3Int& b); + static FORCEINLINE float DistanceSquared(const Vector3Int& a, const Vector3Int& b); +}; + +/* shove all the code here so we can inline everything */ + +#pragma warning(suppress : 26495) // don't initalize for speed +FORCEINLINE Vector3Int::Vector3Int() +{ +} + +FORCEINLINE constexpr Vector3Int::Vector3Int(const int x, const int y, const int z): + X(x), Y(y), Z(z) +{ +} + +FORCEINLINE constexpr Vector3Int::Vector3Int(const int scaler): + X(scaler), Y(scaler), Z(scaler) +{ +} + +// comparison operators +FORCEINLINE constexpr bool Vector3Int::operator==(const Vector3Int& a) const +{ + return X == a.X && Y == a.Y && Z == a.Z; +} + +FORCEINLINE constexpr bool Vector3Int::operator!=(const Vector3Int& a) const +{ + return X != a.X || Y != a.Y || Z != a.Z; +} + +// arithmetic operators +FORCEINLINE constexpr Vector3Int Vector3Int::operator+(const Vector3Int& a) const +{ + return Vector3Int(X + a.X, Y + a.Y, Z + a.Z); +} + +FORCEINLINE constexpr Vector3Int Vector3Int::operator-(const Vector3Int& a) const +{ + return Vector3Int(X - a.X, Y - a.Y, Z - a.Z); +} + +FORCEINLINE constexpr Vector3Int Vector3Int::operator*(const int scale) const +{ + return Vector3Int(X * scale, Y * scale, Z * scale); +} + +FORCEINLINE constexpr Vector3Int Vector3Int::operator*(const Vector3Int& a) const +{ + return Vector3Int(X * a.X, Y * a.Y, Z * a.Z); +} + +FORCEINLINE constexpr Vector3Int Vector3Int::operator/(int scale) const +{ + assert(scale != 0.0); + + const float rScale = 1.f/scale; + return Vector3Int(X * rScale, Y * rScale, Z * rScale); +} + +FORCEINLINE constexpr Vector3Int Vector3Int::operator/(const Vector3Int& a) const +{ + return Vector3Int(X / a.X, Y / a.Y, Z / a.Z); +} + +FORCEINLINE constexpr Vector3Int Vector3Int::operator+=(const Vector3Int& a) +{ + X += a.X; Y += a.Y; Z += a.Z; + return *this; +} + +FORCEINLINE constexpr Vector3Int Vector3Int::operator-=(const Vector3Int& a) +{ + X -= a.X; Y -= a.Y; Z -= a.Z; + return *this; +} + +FORCEINLINE constexpr Vector3Int Vector3Int::operator*=(int scale) +{ + X *= scale; Y *= scale; Z *= scale; + return *this; +} + +FORCEINLINE constexpr Vector3Int Vector3Int::operator*=(const Vector3Int& a) +{ + X *= a.X; Y *= a.Y; Z *= a.Z; + return *this; +} + +FORCEINLINE constexpr Vector3Int Vector3Int::operator/=(int scale) +{ + assert(scale != 0.0); + + const float rScale = 1.f / scale; + X *= rScale; Y *= rScale; Z *= rScale; + return *this; +} + +FORCEINLINE constexpr Vector3Int Vector3Int::operator/=(const Vector3Int& a) +{ + X /= a.X; Y /= a.Y; Z /= a.Z; + return *this; +} + +FORCEINLINE constexpr Vector3Int Vector3Int::operator-() const +{ + return Vector3Int(-X, -Y, -Z); +} + +// access operators +FORCEINLINE constexpr int Vector3Int::operator[](std::size_t i) const +{ + assert(i >= 0 && i < 3); + return (&X)[i]; +} + +FORCEINLINE constexpr int& Vector3Int::operator[](std::size_t i) +{ + assert(i >= 0 && i < 3); + return (&X)[i]; +} + +FORCEINLINE std::string Vector3Int::ToString() const +{ + return fmt::format("X={:3.3f} Y={:3.3f} Z={:3.3f}", X, Y, Z); +} + +} // namespace Donut diff --git a/src/Core/Math/Vector4.cpp b/src/Core/Math/Vector4.cpp new file mode 100644 index 0000000..af31208 --- /dev/null +++ b/src/Core/Math/Vector4.cpp @@ -0,0 +1,11 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#include "Vector4.h" + +namespace Donut +{ + +// const Vector4 Vector4::Zero(0.0f, 0.0f, 0.0f); +// const Vector4 Vector4::One(1.0f, 1.0f, 1.0f); + +} // namespace Ogre diff --git a/src/Core/Math/Vector4.h b/src/Core/Math/Vector4.h new file mode 100644 index 0000000..546025b --- /dev/null +++ b/src/Core/Math/Vector4.h @@ -0,0 +1,187 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#pragma once + +#include "Core/Platform.h" + +#include +#include +#include +#include + +namespace Donut +{ + +struct Vector4 +{ + public: + float X, Y, Z, W; + + public: + FORCEINLINE Vector4(); + FORCEINLINE constexpr Vector4(const float x, const float y, const float z, const float w); + explicit FORCEINLINE constexpr Vector4(const float scaler); + + // arithmetic operators + FORCEINLINE constexpr Vector4 operator+(const Vector4& a) const; + FORCEINLINE constexpr Vector4 operator-(const Vector4& a) const; + FORCEINLINE constexpr Vector4 operator*(const Vector4& a) const; + FORCEINLINE constexpr Vector4 operator/(const Vector4& a) const; + FORCEINLINE constexpr Vector4 operator*(const float a) const; + FORCEINLINE constexpr Vector4 operator/(const float a) const; + + // comparison operators + FORCEINLINE constexpr bool operator==(const Vector4& a) const; + FORCEINLINE constexpr bool operator!=(const Vector4& a) const; + + // negated copy + FORCEINLINE constexpr Vector4 operator-() const; + + // arithmetic updater operators + FORCEINLINE constexpr Vector4 operator+=(const Vector4& a); + FORCEINLINE constexpr Vector4 operator-=(const Vector4& a); + FORCEINLINE constexpr Vector4 operator*=(const Vector4& a); + FORCEINLINE constexpr Vector4 operator/=(const Vector4& a); + FORCEINLINE constexpr Vector4 operator*=(float scale); + FORCEINLINE constexpr Vector4 operator/=(float scale); + + // get specific components + FORCEINLINE constexpr float& operator[](std::size_t i); + FORCEINLINE constexpr float operator[](std::size_t i) const; + + public: + // float Length() const; + // float LengthSquared() const; + // Vector4 Normalize(); + + std::string ToString() const; + + // static FORCEINLINE float Distance(const FVector &V1, const FVector &V2) + // static FORCEINLINE float DistSquared(const FVector &V1, const FVector &V2); +}; + +/* shove all the code here so we can inline everything */ + +#pragma warning(suppress : 26495) // don't initalize for speed +FORCEINLINE Vector4::Vector4() +{ +} + +FORCEINLINE constexpr Vector4::Vector4(const float x, const float y, const float z, const float w): + X(x), Y(y), Z(z), W(w) +{ +} + +FORCEINLINE constexpr Vector4::Vector4(const float scaler): + X(scaler), Y(scaler), Z(scaler), W(scaler) +{ +} + +// comparison operators +FORCEINLINE constexpr bool Vector4::operator==(const Vector4& a) const +{ + return X == a.X && Y == a.Y && Z == a.Z && W == a.W; +} + +FORCEINLINE constexpr bool Vector4::operator!=(const Vector4& a) const +{ + return X != a.X || Y != a.Y || Z != a.Z || W != a.W; +} + +// arithmetic operators +FORCEINLINE constexpr Vector4 Vector4::operator+(const Vector4& a) const +{ + return Vector4(X + a.X, Y + a.Y, Z + a.Z, W + a.W); +} + +FORCEINLINE constexpr Vector4 Vector4::operator-(const Vector4& a) const +{ + return Vector4(X - a.X, Y - a.Y, Z - a.Z, Z - a.Z); +} + +FORCEINLINE constexpr Vector4 Vector4::operator*(const float scale) const +{ + return Vector4(X * scale, Y * scale, Z * scale, W * scale); +} + +FORCEINLINE constexpr Vector4 Vector4::operator*(const Vector4& a) const +{ + return Vector4(X * a.X, Y * a.Y, Z * a.Z, W * a.W); +} + +FORCEINLINE constexpr Vector4 Vector4::operator/(float scale) const +{ + assert(scale != 0.0); + + const float rScale = 1.f/scale; + return Vector4(X * rScale, Y * rScale, Z * rScale, W * rScale); +} + +FORCEINLINE constexpr Vector4 Vector4::operator/(const Vector4& a) const +{ + return Vector4(X / a.X, Y / a.Y, Z / a.Z, W / a.W); +} + +FORCEINLINE constexpr Vector4 Vector4::operator+=(const Vector4& a) +{ + X += a.X; Y += a.Y; Z += a.Z; W += a.W; + return *this; +} + +FORCEINLINE constexpr Vector4 Vector4::operator-=(const Vector4& a) +{ + X -= a.X; Y -= a.Y; Z -= a.Z; W -= a.W; + return *this; +} + +FORCEINLINE constexpr Vector4 Vector4::operator*=(float scale) +{ + X *= scale; Y *= scale; Z *= scale; W *= scale; + return *this; +} + +FORCEINLINE constexpr Vector4 Vector4::operator*=(const Vector4& a) +{ + X *= a.X; Y *= a.Y; Z *= a.Z; W *= a.W; + return *this; +} + +FORCEINLINE constexpr Vector4 Vector4::operator/=(float scale) +{ + assert(scale != 0.0); + + const float rScale = 1.f / scale; + X *= rScale; Y *= rScale; Z *= rScale; W *= rScale; + return *this; +} + +FORCEINLINE constexpr Vector4 Vector4::operator/=(const Vector4& a) +{ + X /= a.X; Y /= a.Y; Z /= a.Z; W /= a.W; + return *this; +} + +FORCEINLINE constexpr Vector4 Vector4::operator-() const +{ + return Vector4(-X, -Y, -Z, -W); +} + +// access operators +FORCEINLINE constexpr float Vector4::operator[](std::size_t i) const +{ + assert(i >= 0 && i < 4); + return (&X)[i]; +} + +FORCEINLINE constexpr float& Vector4::operator[](std::size_t i) +{ + assert(i >= 0 && i < 4); + return (&X)[i]; +} + +FORCEINLINE std::string Vector4::ToString() const +{ + return fmt::format("X={:3.3f} Y={:3.3f} Z={:3.3f} W={:3.3f}", X, Y, Z, W); +} + +} // namespace Donut diff --git a/src/Core/Platform.h b/src/Core/Platform.h new file mode 100644 index 0000000..29ddf65 --- /dev/null +++ b/src/Core/Platform.h @@ -0,0 +1,30 @@ +// Copyright 2019 the donut authors. See AUTHORS.md + +#pragma once + +// Platform +#ifdef _WIN32 +#define DONUT_PLATFORM_WINDOWS +#elif __linux +#define DONUT_PLATFORM_LINUX +#endif + +// Force Inline +#if defined(__clang__) || defined(__GNUC__) +# define FORCEINLINE inline __attribute__((always_inline)) +#elif defined(_MSC_VER) +# define FORCEINLINE __forceinline +#else +# define FORCEINLINE inline +#endif + +// Alignment +#ifdef _MSC_VER +#define MS_ALIGN(n) __declspec(align(n)) +#define GCC_PACK(n) +#define GCC_ALIGN(n) +#else +#define MS_ALIGN(n) +#define GCC_PACK(n) __attribute__((packed, aligned(n))) +#define GCC_ALIGN(n) __attribute__((aligned(n))) +#endif diff --git a/src/Entity.cpp b/src/Entity.cpp index ee4b405..0608a2f 100644 --- a/src/Entity.cpp +++ b/src/Entity.cpp @@ -18,7 +18,7 @@ void StaticEntity::Draw(GL::ShaderProgram& shader, bool opaque) _mesh->Draw(shader, opaque); } -InstancedStaticEntity::InstancedStaticEntity(const P3D::Geometry& geometry, const std::vector& transforms) +InstancedStaticEntity::InstancedStaticEntity(const P3D::Geometry& geometry, const std::vector& transforms) { _name = geometry.GetName(); _mesh = std::make_unique(geometry, transforms); diff --git a/src/Entity.h b/src/Entity.h index ac3dfd2..5210e6b 100644 --- a/src/Entity.h +++ b/src/Entity.h @@ -51,7 +51,7 @@ class StaticEntity: public Entity class InstancedStaticEntity: public Entity { public: - InstancedStaticEntity(const P3D::Geometry&, const std::vector&); + InstancedStaticEntity(const P3D::Geometry&, const std::vector&); void Draw(GL::ShaderProgram&, bool opaque) override; diff --git a/src/FreeCamera.cpp b/src/FreeCamera.cpp index f0e064f..78cedfd 100644 --- a/src/FreeCamera.cpp +++ b/src/FreeCamera.cpp @@ -2,50 +2,64 @@ #include "FreeCamera.h" -#include -#include +#include "Core/Math/Math.h" namespace Donut { FreeCamera::FreeCamera(): - Pitch(0.0f), Yaw(0.0f), Position(0.0f) + _position(0.0f), _orientation(Quaternion::Identity), _fov(70.0f), + _aspectRatio(1.0f), _znear(1.0f), _zfar(10000.0f) { - UpdateRotationQuat(); - UpdateViewMatrix(); + +} + +void FreeCamera::SetPosition(const Vector3& position) +{ + _position = position; } -void FreeCamera::MoveTo(glm::vec3 position) +void FreeCamera::SetQuaternion(const Quaternion& orientation) { - Position = position; - UpdateViewMatrix(); + _orientation = orientation; } -void FreeCamera::Move(glm::vec3 force, float dt) +void FreeCamera::Move(Vector3 force, float dt) { - if (glm::length2(force) > 0.0f) - { - Position -= (glm::inverse(RotationQuat) * force) * dt; - UpdateViewMatrix(); - } + if (force.LengthSquared() == 0) + return; + + _position += _orientation.Inverse() * force * dt; } void FreeCamera::LookDelta(float x, float y) { - Yaw += x; - Yaw += glm::ceil(-Yaw / 360.0f) * 360.0f; - Pitch = glm::clamp(Pitch + y, -90.0f, 90.0f); + Vector3 xAxis = _orientation.GetXAxis(); // pitch axis + Vector3 yAxis = _orientation.GetYAxis(); // yaw axis + + rotate(yAxis, Math::DegreesToRadians(x)); + rotate(xAxis, Math::DegreesToRadians(y)); +} + +const Matrix4x4& FreeCamera::GetViewMatrix() const +{ + return Matrix4x4::MakeView(_position, _orientation); +} - UpdateRotationQuat(); - UpdateViewMatrix(); +const Matrix4x4& FreeCamera::GetProjectionMatrix() const +{ + return Matrix4x4::MakePerspective(_fov, _aspectRatio, _znear, _zfar); // todo: cache this } -void FreeCamera::UpdateViewMatrix() +void FreeCamera::rotate(const Quaternion& q) { - ViewMatrix = glm::toMat4(RotationQuat) * glm::translate(glm::mat4(1.0f), -Position); + Quaternion qres = q * _orientation; + _orientation = qres.Normal(); } -void FreeCamera::UpdateRotationQuat() +void FreeCamera::rotate(const Vector3& axis, const float angle) { - RotationQuat = glm::inverse(glm::quat(glm::vec3(glm::radians(Pitch), glm::radians(Yaw), 0.0f))); + Quaternion q(axis, angle); + rotate(q); } + } // namespace Donut diff --git a/src/FreeCamera.h b/src/FreeCamera.h index 5ef7522..5783461 100644 --- a/src/FreeCamera.h +++ b/src/FreeCamera.h @@ -2,8 +2,9 @@ #pragma once -#include -#include +#include "Core/Math/Matrix4x4.h" +#include "Core/Math/Quaternion.h" +#include "Core/Math/Vector3.h" namespace Donut { @@ -12,22 +13,41 @@ class FreeCamera public: FreeCamera(); - const glm::vec3& GetPosition() const { return Position; } - const glm::mat4 GetViewMatrix() const { return ViewMatrix; } + float GetFOV() const { return _fov; } + float GetZNear() const { return _znear; } + float GetZFar() const { return _zfar; } + float GetAspectRatio() const { return _aspectRatio; } - void MoveTo(glm::vec3 position); - void Move(glm::vec3 force, float dt); + void SetFOV(const float fov) { _fov = fov; } + void SetZNear(const float znear) { _znear = znear; } + void SetZFar(const float zfar) { _zfar = zfar; } + void SetAspectRatio(const float aspect) { _aspectRatio = aspect; } + + void SetPosition(const Vector3& position); + const Vector3& GetPosition() const { return _position; } + + void SetQuaternion(const Quaternion& orientation); + const Quaternion& GetOrientation() const { return _orientation; } + + void Move(Vector3 force, float dt); void LookDelta(float x, float y); + const Matrix4x4& GetViewMatrix() const; + const Matrix4x4& GetProjectionMatrix() const; + private: - void UpdateViewMatrix(); - void UpdateRotationQuat(); + void rotate(const Quaternion& q); + void rotate(const Vector3& axis, const float angle); + + Vector3 _position; + Quaternion _orientation; - float Pitch; - float Yaw; + //Matrix4x4 _projectionMatrix; + //Matrix4x4 _viewMatrix; - glm::vec3 Position; - glm::quat RotationQuat; - glm::mat4 ViewMatrix; + float _fov; + float _znear; + float _zfar; + float _aspectRatio; }; } // namespace Donut diff --git a/src/FrontendProject.cpp b/src/FrontendProject.cpp index 524e2f5..a4a5475 100644 --- a/src/FrontendProject.cpp +++ b/src/FrontendProject.cpp @@ -165,14 +165,14 @@ void FrontendProject::LoadP3D(const std::string& filename) } } -void FrontendProject::Draw(const glm::mat4& proj) +void FrontendProject::Draw(const Matrix4x4& proj) { for (const auto& sprite : _sprites) { _spriteBatch.Draw( sprite.texture, - glm::vec2(sprite.positionX, sprite.positionY), - glm::vec2(sprite.width, sprite.height), + Vector2(sprite.positionX, sprite.positionY), + Vector2(sprite.width, sprite.height), sprite.color); } diff --git a/src/FrontendProject.h b/src/FrontendProject.h index 2df58b6..0a165e4 100644 --- a/src/FrontendProject.h +++ b/src/FrontendProject.h @@ -25,7 +25,7 @@ class FrontendProject ~FrontendProject(); void LoadP3D(const std::string&); - void Draw(const glm::mat4& proj); + void Draw(const Matrix4x4& proj); private: void AddGroup(const P3D::FrontendGroup& group, int32_t resX, int32_t resY); @@ -38,7 +38,7 @@ class FrontendProject int32_t positionY; int32_t width; int32_t height; - glm::vec4 color; + Vector4 color; }; SpriteBatch _spriteBatch; diff --git a/src/Game.cpp b/src/Game.cpp index 185a424..7104413 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,47 +1,43 @@ // Copyright 2019 the donut authors. See AUTHORS.md +#include "Game.h" + +#include "AnimCamera.h" +#include "Character.h" +#include "Core/Math/Math.h" +#include "Core/FpsTimer.h" +#include "FreeCamera.h" +#include "FrontendProject.h" +#include "Input/Input.h" +#include "Level.h" +#include "P3D/P3D.generated.h" +#include "P3D/P3DFile.h" +#include "Physics/WorldPhysics.h" +#include "RCL/RCFFile.h" +#include "RCL/RSDFile.h" +#include "Render/imgui/imgui.h" +#include "Render/imgui/imgui_impl_opengl3.h" +#include "Render/imgui/imgui_impl_sdl.h" +#include "Render/OpenGL/FrameBuffer.h" +#include "Render/OpenGL/ShaderProgram.h" +#include "Render/OpenGL/glad/glad.h" +#include "Render/Font.h" +#include "Render/LineRenderer.h" +#include "Render/Shader.h" +#include "Render/SkinModel.h" +#include "Render/SpriteBatch.h" +#include "ResourceManager.h" +#include "Scripting/Commands.h" +#include "Window.h" + #include #include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include #include -#include -#include - -#include "Render/OpenGL/glad/glad.h" -#include "Render/imgui/imgui.h" -#include "Render/imgui/imgui_impl_opengl3.h" -#include "Render/imgui/imgui_impl_sdl.h" - #include -#include -#include -#include -#include -#include -#include #include #include #include @@ -207,7 +203,7 @@ Game::Game(int argc, char** argv) // _level->LoadP3D("l1r3.p3d"); // _level->LoadP3D("l1r4a.p3d"); // _level->LoadP3D("l1r6.p3d"); - // _level->LoadP3D("l1z2.p3d"); + _level->LoadP3D("l1z2.p3d"); // _level->LoadP3D("l1z3.p3d"); // _level->LoadP3D("l1z4.p3d"); // _level->LoadP3D("l1z6.p3d"); @@ -221,7 +217,10 @@ Game::Game(int argc, char** argv) LoadModel("homer", "homer"); _camera = std::make_unique(); - _camera->MoveTo(glm::vec3(228.0f, 5.0f, -174.0f)); + _camera->SetPosition(Vector3(228.0f, 5.0f, -174.0f)); + _camera->SetFOV(70.0f); + _camera->SetZNear(1.0f); + _camera->SetZFar(1000.0f); _mouseLocked = false; } @@ -256,7 +255,7 @@ void Game::LoadModel(const std::string& name, const std::string& anim) _character = std::make_unique("pc"); _character->LoadModel(name); _character->LoadAnimations(anim); - _character->SetPosition(glm::vec3(220, 4.5, -172)); + _character->SetPosition(Vector3(220, 4.5, -172)); if (anim == "homer") _character->SetAnimation("hom_loco_walk"); @@ -283,19 +282,19 @@ void Game::LockMouse(bool lockMouse) Input::ResetMouseDelta(); } -std::vector> locations { - { "Simpsons' House", glm::vec3(220, 3.5, -172), "l1z1.p3d;l1r1.p3d;l1r7.p3d;" }, - { "Kwik E Mart", glm::vec3(209, 3.6, -285), "l1z2.p3d;l1r1.p3d;l1r2.p3d;" }, - { "Church", glm::vec3(193.8, -0.9, -570), "l1r2.p3d;l1z2.p3d;l1z3.p3d;" }, - { "Springfield Elementary", glm::vec3(-11, 0.7, -586), "l1z3.p3d;l1r2.p3d;l1r3.p3d;" }, - { "Burns' Mansion", glm::vec3(-186, 3.5, -96), "l1z4.p3d;l1r3.p3d;l1r4a.p3d;" }, - { "Stonecutters Tunnel", glm::vec3(-405, 2, 60), "l1z4.p3d;l1r3.p3d;l1r4a.p3d;" }, - { "Power Plant Interior", glm::vec3(-80, 0.8, 297), "l1r4a.p3d;l1z6.p3d;l1r6.p3d;" }, - { "Power Plant Parking Lot", glm::vec3(40, 0, 296), "l1z6.p3d;l1r6.p3d;" }, - { "Tomacco", glm::vec3(190, -0.7, 425), "l1r6.p3d;l1z6.p3d;l1z7.p3d;" }, - { "Trailer Park", glm::vec3(391, -2.2, 494), "l1z7.p3d;l1r6.p3d;l1r7.p3d;" }, - { "Cletus' House", glm::vec3(333.5, -1.8, 356), "l1z7.p3d;l1r6.p3d;l1r7.p3d;" }, - { "Graveyard", glm::vec3(368, 5.1, 5.4), "l1z1.p3d;l1r1.p3d;l1r7.p3d;" } +std::vector> locations { + { "Simpsons' House", Vector3(220, 3.5, -172), "l1z1.p3d;l1r1.p3d;l1r7.p3d;" }, + { "Kwik E Mart", Vector3(209, 3.6, -285), "l1z2.p3d;l1r1.p3d;l1r2.p3d;" }, + { "Church", Vector3(193.8, -0.9, -570), "l1r2.p3d;l1z2.p3d;l1z3.p3d;" }, + { "Springfield Elementary", Vector3(-11, 0.7, -586), "l1z3.p3d;l1r2.p3d;l1r3.p3d;" }, + { "Burns' Mansion", Vector3(-186, 3.5, -96), "l1z4.p3d;l1r3.p3d;l1r4a.p3d;" }, + { "Stonecutters Tunnel", Vector3(-405, 2, 60), "l1z4.p3d;l1r3.p3d;l1r4a.p3d;" }, + { "Power Plant Interior", Vector3(-80, 0.8, 297), "l1r4a.p3d;l1z6.p3d;l1r6.p3d;" }, + { "Power Plant Parking Lot", Vector3(40, 0, 296), "l1z6.p3d;l1r6.p3d;" }, + { "Tomacco", Vector3(190, -0.7, 425), "l1r6.p3d;l1z6.p3d;l1z7.p3d;" }, + { "Trailer Park", Vector3(391, -2.2, 494), "l1z7.p3d;l1r6.p3d;l1r7.p3d;" }, + { "Cletus' House", Vector3(333.5, -1.8, 356), "l1z7.p3d;l1r6.p3d;l1r7.p3d;" }, + { "Graveyard", Vector3(368, 5.1, 5.4), "l1z1.p3d;l1r1.p3d;l1r7.p3d;" } }; std::vector> models { @@ -363,14 +362,16 @@ void Game::Run() _camera->LookDelta(mouseDeltaX * 0.25f, mouseDeltaY * 0.25f); } - auto inputForce = glm::vec3(0.0f); - if (Input::IsDown(Button::KeyW)) inputForce -= glm::vec3(0.0f, 0.0f, 1.0f); - if (Input::IsDown(Button::KeyS)) inputForce += glm::vec3(0.0f, 0.0f, 1.0f); - if (Input::IsDown(Button::KeyA)) inputForce += glm::vec3(1.0f, 0.0f, 0.0f); - if (Input::IsDown(Button::KeyD)) inputForce -= glm::vec3(1.0f, 0.0f, 0.0f); - if (glm::length2(inputForce) > 0.0f) + auto inputForce = Vector3(0.0f); + if (Input::IsDown(Button::KeyW)) inputForce += Vector3::Forward; + if (Input::IsDown(Button::KeyS)) inputForce += Vector3::Backward; + if (Input::IsDown(Button::KeyA)) inputForce += Vector3::Left; + if (Input::IsDown(Button::KeyD)) inputForce += Vector3::Right; + if (Input::IsDown(Button::KeySPACE)) inputForce += Vector3::Up; + if (Input::IsDown(Button::KeyLCONTROL)) inputForce += Vector3::Down; + if (inputForce.LengthSquared() > 0.0f) { - inputForce = glm::normalize(inputForce); + inputForce = inputForce.Normalize(); inputForce *= Input::IsDown(Button::KeyLSHIFT) ? 60.0f : 10.0f; _camera->Move(inputForce, static_cast(deltaTime)); } @@ -412,7 +413,14 @@ void Game::Run() if (ImGui::Begin("Camera position overlay", NULL, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav)) { auto const& camPos = _camera->GetPosition(); - ImGui::Text("Camera Position: (%.1f,%.1f, %.1f)", camPos.x, camPos.y, camPos.z); + auto const& camRot = _camera->GetOrientation(); + ImGui::Text("Camera Position: (%s)", camPos.ToString().c_str()); + ImGui::Text("Camera Orientation: (%s)", camRot.ToString().c_str()); + + float fov = _camera->GetFOV(); + if (ImGui::SliderFloat("FOV", &fov, 0.0f, 120.0f)) + _camera->SetFOV(fov); + } ImGui::End(); @@ -435,11 +443,11 @@ void Game::Run() glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glm::mat4 projectionMatrix = glm::perspective( - glm::radians(70.0f), (float)viewportWidth / (float)viewportHeight, 0.1f, 10000.0f); - - glm::mat4 viewMatrix = _camera->GetViewMatrix(); - glm::mat4 viewProjection = projectionMatrix * viewMatrix; + _camera->SetAspectRatio(static_cast(viewportWidth) / static_cast(viewportHeight)); + + Matrix4x4 viewMatrix = _camera->GetViewMatrix(); + Matrix4x4 projMatrix = _camera->GetProjectionMatrix(); + Matrix4x4 viewProjection = projMatrix * viewMatrix; if (_level != nullptr) _level->Draw(viewProjection); @@ -450,17 +458,17 @@ void Game::Run() _lineRenderer->Flush(viewProjection); glEnable(GL_DEPTH_TEST); - glm::mat4 proj = glm::ortho(0.0f, (float)viewportWidth, (float)viewportHeight, 0.0f); + Matrix4x4 proj = Matrix4x4::MakeOrtho(0.0f, viewportWidth, viewportHeight, 0.0f); if (_textureFontP3D != nullptr) { std::string fps = fmt::format("{0} fps", timer.GetFps()); auto font = _resourceManager->GetFont("boulder_16"); - sprites.DrawText(font, fps, glm::vec2(32 + 3, 32 + 3), glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)); - sprites.DrawText(font, fps, glm::vec2(32, 32), glm::vec4(1.0f, 1.0f, 0.0f, 1.0f)); + sprites.DrawText(font, fps, Vector2(32 + 3, 32 + 3), Vector4(0.0f, 0.0f, 0.0f, 1.0f)); + sprites.DrawText(font, fps, Vector2(32, 32), Vector4(1.0f, 1.0f, 0.0f, 1.0f)); } - sprites.Flush(proj); + sprites.Flush(proj); //frontend->Draw(proj); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); @@ -507,7 +515,7 @@ void Game::guiModelMenu(Character& character) ImGui::EndCombo(); } - glm::vec3 pos = character.GetPosition(); + Vector3 pos = character.GetPosition(); if (ImGui::InputFloat3("Position", &pos[0])) character.SetPosition(pos); @@ -525,14 +533,14 @@ void Game::guiTeleportMenu() { if (ImGui::MenuItem(std::get<0>(location).c_str())) { - const glm::vec3& dest = std::get<1>(location); + const Vector3& dest = std::get<1>(location); //_worldPhysics->GetCharacterController()->SetPosition(dest); - _camera->MoveTo(dest); + _camera->SetPosition(dest); } if (ImGui::IsItemHovered()) { ImGui::BeginTooltip(); - ImGui::TextUnformatted(glm::to_string(std::get<1>(location)).c_str()); + ImGui::TextUnformatted(std::get<1>(location).ToString().c_str()); ImGui::TextUnformatted(std::get<2>(location).c_str()); ImGui::EndTooltip(); } diff --git a/src/Level.cpp b/src/Level.cpp index f6a4a74..06502c1 100644 --- a/src/Level.cpp +++ b/src/Level.cpp @@ -49,7 +49,7 @@ Level::Level() { if (auto car = CompositeModel::LoadP3D(carFile)) { - auto transform = glm::translate(glm::mat4(1.0f), glm::vec3(240 + offset, 4.6f, -160)); + auto transform = glm::translate(Matrix(1.0f), Vector3(240 + offset, 4.6f, -160)); car->SetTransform(transform); _compositeModels.push_back(std::move(car)); offset += 3.0f; @@ -111,7 +111,7 @@ void Level::LoadP3D(const std::string& filename) { const auto& staticPhys = P3D::InstancedStaticPhysics::Load(*chunk); std::vector drawables; - std::vector transforms; + std::vector transforms; P3D::P3DUtil::GetDrawables(staticPhys->GetInstanceList(), drawables, transforms); const auto& geometries = staticPhys->GetGeometries(); @@ -121,7 +121,7 @@ void Level::LoadP3D(const std::string& filename) meshesNameIndex.insert({ geometry->GetName(), meshesNameIndex.size() }); } - std::unordered_map> meshTransforms; + std::unordered_map> meshTransforms; for (size_t i = 0; i < drawables.size(); ++i) { @@ -145,7 +145,7 @@ void Level::LoadP3D(const std::string& filename) { const auto& dynaPhys = P3D::DynamicPhysics::Load(*chunk); std::vector drawables; - std::vector transforms; + std::vector transforms; P3D::P3DUtil::GetDrawables(dynaPhys->GetInstanceList(), drawables, transforms); const auto& geometries = dynaPhys->GetGeometries(); @@ -155,7 +155,7 @@ void Level::LoadP3D(const std::string& filename) meshesNameIndex.insert({ geometry->GetName(), meshesNameIndex.size() }); } - std::unordered_map> meshTransforms; + std::unordered_map> meshTransforms; for (size_t i = 0; i < drawables.size(); ++i) { @@ -179,7 +179,7 @@ void Level::LoadP3D(const std::string& filename) { const auto& dynaPhys = P3D::AnimDynamicPhysics::Load(*chunk); std::vector drawables; - std::vector transforms; + std::vector transforms; P3D::P3DUtil::GetDrawables(dynaPhys->GetInstanceList(), drawables, transforms); const auto& animObjectWrapper = dynaPhys->GetAnimObjectWrapper(); @@ -299,7 +299,7 @@ void Level::Update(double deltatime) _worldSphere->Update(deltatime); } -void Level::Draw(glm::mat4& viewProj) +void Level::Draw(Matrix4x4& viewProj) { _worldShader->Bind(); _worldShader->SetUniformValue("viewProj", viewProj); diff --git a/src/Level.h b/src/Level.h index 106f68b..280509a 100644 --- a/src/Level.h +++ b/src/Level.h @@ -2,7 +2,8 @@ #pragma once -#include +#include "Core/Math/Fwd.h" + #include #include #include @@ -30,7 +31,7 @@ class Level ~Level(); void Update(double deltatime); - void Draw(glm::mat4& viewProj); + void Draw(Matrix4x4& viewProj); void LoadP3D(const std::string& filename); void DynaLoadData(const std::string& dynaLoadData); @@ -54,7 +55,7 @@ class Level class Path { public: - std::vector points; + std::vector points; }; std::vector _paths; diff --git a/src/P3D/P3D.generated.cpp b/src/P3D/P3D.generated.cpp index e990356..3ce651b 100644 --- a/src/P3D/P3D.generated.cpp +++ b/src/P3D/P3D.generated.cpp @@ -148,12 +148,12 @@ namespace Donut::P3D _version = stream.Read(); _param = stream.ReadString(4); _mapping = stream.Read(); - _constants = stream.Read(); + _constants = stream.Read(); _numFrames = stream.Read(); _frames.resize(_numFrames); stream.ReadBytes(reinterpret_cast(_frames.data()), _frames.size() * sizeof(uint16_t)); _values.resize(_numFrames); - stream.ReadBytes(reinterpret_cast(_values.data()), _values.size() * sizeof(glm::vec2)); + stream.ReadBytes(reinterpret_cast(_values.data()), _values.size() * sizeof(Vector2)); for (auto const& child : chunk.GetChildren()) { @@ -181,7 +181,7 @@ namespace Donut::P3D _frames.resize(_numFrames); stream.ReadBytes(reinterpret_cast(_frames.data()), _frames.size() * sizeof(uint16_t)); _values.resize(_numFrames); - stream.ReadBytes(reinterpret_cast(_values.data()), _values.size() * sizeof(glm::vec3)); + stream.ReadBytes(reinterpret_cast(_values.data()), _values.size() * sizeof(Vector3)); for (auto const& child : chunk.GetChildren()) { @@ -209,7 +209,7 @@ namespace Donut::P3D _frames.resize(_numFrames); stream.ReadBytes(reinterpret_cast(_frames.data()), _frames.size() * sizeof(uint16_t)); _values.resize(_numFrames); - stream.ReadBytes(reinterpret_cast(_values.data()), _values.size() * sizeof(glm::quat)); + stream.ReadBytes(reinterpret_cast(_values.data()), _values.size() * sizeof(Quaternion)); for (auto const& child : chunk.GetChildren()) { @@ -318,8 +318,8 @@ namespace Donut::P3D assert(chunk.IsType(ChunkType::BoundingBox)); MemoryStream stream(chunk.GetData()); - _min = stream.Read(); - _max = stream.Read(); + _min = stream.Read(); + _max = stream.Read(); } BoundingSphere::BoundingSphere(const P3DChunk& chunk) @@ -327,7 +327,7 @@ namespace Donut::P3D assert(chunk.IsType(ChunkType::BoundingSphere)); MemoryStream stream(chunk.GetData()); - _centre = stream.Read(); + _centre = stream.Read(); _radius = stream.Read(); } @@ -353,7 +353,7 @@ namespace Donut::P3D case ChunkType::PositionList: { _vertices.resize(data.Read()); - data.ReadBytes(reinterpret_cast(_vertices.data()), _vertices.size() * sizeof(glm::vec3)); + data.ReadBytes(reinterpret_cast(_vertices.data()), _vertices.size() * sizeof(Vector3)); break; } case ChunkType::IndexList: @@ -365,7 +365,7 @@ namespace Donut::P3D case ChunkType::NormalList: { _normals.resize(data.Read()); - data.ReadBytes(reinterpret_cast(_normals.data()), _normals.size() * sizeof(glm::vec3)); + data.ReadBytes(reinterpret_cast(_normals.data()), _normals.size() * sizeof(Vector3)); break; } case ChunkType::UVList: @@ -374,7 +374,7 @@ namespace Donut::P3D uint32_t channel = data.Read(); _uvs.resize(channel + 1); _uvs.at(channel).resize(length); - data.ReadBytes(reinterpret_cast(_uvs.at(channel).data()), length * sizeof(glm::vec2)); + data.ReadBytes(reinterpret_cast(_uvs.at(channel).data()), length * sizeof(Vector2)); break; } case ChunkType::MatrixList: @@ -392,7 +392,7 @@ namespace Donut::P3D case ChunkType::WeightList: { _weightList.resize(data.Read()); - data.ReadBytes(reinterpret_cast(_weightList.data()), _weightList.size() * sizeof(glm::vec3)); + data.ReadBytes(reinterpret_cast(_weightList.data()), _weightList.size() * sizeof(Vector3)); break; } case ChunkType::ColorList: @@ -414,7 +414,7 @@ namespace Donut::P3D MemoryStream stream(chunk.GetData()); _size = stream.Read(); _positions.resize(_size); - stream.ReadBytes(reinterpret_cast(_positions.data()), _positions.size() * sizeof(glm::vec3)); + stream.ReadBytes(reinterpret_cast(_positions.data()), _positions.size() * sizeof(Vector3)); } IndexList::IndexList(const P3DChunk& chunk) @@ -434,7 +434,7 @@ namespace Donut::P3D MemoryStream stream(chunk.GetData()); _size = stream.Read(); _normals.resize(_size); - stream.ReadBytes(reinterpret_cast(_normals.data()), _normals.size() * sizeof(glm::vec3)); + stream.ReadBytes(reinterpret_cast(_normals.data()), _normals.size() * sizeof(Vector3)); } UVList::UVList(const P3DChunk& chunk) @@ -445,7 +445,7 @@ namespace Donut::P3D _size = stream.Read(); _channel = stream.Read(); _uvs.resize(_size); - stream.ReadBytes(reinterpret_cast(_uvs.data()), _uvs.size() * sizeof(glm::vec2)); + stream.ReadBytes(reinterpret_cast(_uvs.data()), _uvs.size() * sizeof(Vector2)); } MatrixList::MatrixList(const P3DChunk& chunk) @@ -475,7 +475,7 @@ namespace Donut::P3D MemoryStream stream(chunk.GetData()); _size = stream.Read(); _uvs.resize(_size); - stream.ReadBytes(reinterpret_cast(_uvs.data()), _uvs.size() * sizeof(glm::vec3)); + stream.ReadBytes(reinterpret_cast(_uvs.data()), _uvs.size() * sizeof(Vector3)); } ColorList::ColorList(const P3DChunk& chunk) @@ -524,7 +524,7 @@ namespace Donut::P3D _primaryAxis = stream.Read(); _secondaryAxis = stream.Read(); _twistAxis = stream.Read(); - _restPose = stream.Read(); + _restPose = stream.Read(); for (auto const& child : chunk.GetChildren()) { @@ -552,7 +552,7 @@ namespace Donut::P3D MemoryStream stream(chunk.GetData()); _jointIndex = stream.Read(); - _axis = stream.Read(); + _axis = stream.Read(); } SkeletonJointBonePreserve::SkeletonJointBonePreserve(const P3DChunk& chunk) @@ -831,7 +831,7 @@ namespace Donut::P3D MemoryStream stream(chunk.GetData()); _name = stream.ReadLPString(); _numChildren = stream.Read(); - _transform = stream.Read(); + _transform = stream.Read(); for (auto const& child : chunk.GetChildren()) { @@ -1066,9 +1066,9 @@ namespace Donut::P3D _indices.resize(stream.Read()); stream.ReadBytes(reinterpret_cast(_indices.data()), _indices.size() * sizeof(uint32_t)); _positions.resize(stream.Read()); - stream.ReadBytes(reinterpret_cast(_positions.data()), _positions.size() * sizeof(glm::vec3)); + stream.ReadBytes(reinterpret_cast(_positions.data()), _positions.size() * sizeof(Vector3)); _normals.resize(stream.Read()); - stream.ReadBytes(reinterpret_cast(_normals.data()), _normals.size() * sizeof(glm::vec3)); + stream.ReadBytes(reinterpret_cast(_normals.data()), _normals.size() * sizeof(Vector3)); for (auto const& child : chunk.GetChildren()) { @@ -1171,16 +1171,16 @@ namespace Donut::P3D _version = stream.Read(); _name = stream.ReadLPString(); _mode = stream.ReadString(4); - _translation = stream.Read(); + _translation = stream.Read(); _color = stream.Read(); - _uv0 = stream.Read(); - _uv1 = stream.Read(); - _uv2 = stream.Read(); - _uv3 = stream.Read(); + _uv0 = stream.Read(); + _uv1 = stream.Read(); + _uv2 = stream.Read(); + _uv3 = stream.Read(); _width = stream.Read(); _height = stream.Read(); _distance = stream.Read(); - _uvOffset = stream.Read(); + _uvOffset = stream.Read(); for (auto const& child : chunk.GetChildren()) { @@ -1236,9 +1236,9 @@ namespace Donut::P3D MemoryStream stream(chunk.GetData()); _version = stream.Read(); - _rotation = stream.Read(); + _rotation = stream.Read(); _cutOffMode = stream.ReadString(4); - _uvOffsetRange = stream.Read(); + _uvOffsetRange = stream.Read(); _sourceRange = stream.Read(); _edgeRange = stream.Read(); } @@ -1654,7 +1654,7 @@ namespace Donut::P3D _translucent = stream.Read(); _numPoints = stream.Read(); _points.resize(_numPoints); - stream.ReadBytes(reinterpret_cast(_points.data()), _points.size() * sizeof(glm::vec3)); + stream.ReadBytes(reinterpret_cast(_points.data()), _points.size() * sizeof(Vector3)); _colors.resize(_numPoints); stream.ReadBytes(reinterpret_cast(_colors.data()), _colors.size() * sizeof(uint32_t)); } @@ -1700,8 +1700,8 @@ namespace Donut::P3D MemoryStream stream(chunk.GetData()); _name = stream.ReadLPString(); _isRect = stream.Read(); - _bounds = stream.Read(); - _transform = stream.Read(); + _bounds = stream.Read(); + _transform = stream.Read(); } Camera::Camera(const P3DChunk& chunk) @@ -1715,9 +1715,9 @@ namespace Donut::P3D _aspectRatio = stream.Read(); _nearClip = stream.Read(); _farClip = stream.Read(); - _position = stream.Read(); - _forward = stream.Read(); - _up = stream.Read(); + _position = stream.Read(); + _forward = stream.Read(); + _up = stream.Read(); } MultiController::MultiController(const P3DChunk& chunk) @@ -1860,7 +1860,7 @@ namespace Donut::P3D { case ChunkType::CollisionVector: { - _vectors.push_back(data.Read()); + _vectors.push_back(data.Read()); break; } default: @@ -1886,7 +1886,7 @@ namespace Donut::P3D { case ChunkType::CollisionVector: { - _vectors.push_back(data.Read()); + _vectors.push_back(data.Read()); break; } default: @@ -1900,7 +1900,7 @@ namespace Donut::P3D assert(chunk.IsType(ChunkType::CollisionOBBoxVolume)); MemoryStream stream(chunk.GetData()); - _halfExtents = stream.Read(); + _halfExtents = stream.Read(); for (auto const& child : chunk.GetChildren()) { @@ -1910,7 +1910,7 @@ namespace Donut::P3D { case ChunkType::CollisionVector: { - _vectors.push_back(data.Read()); + _vectors.push_back(data.Read()); break; } default: @@ -1932,7 +1932,7 @@ namespace Donut::P3D assert(chunk.IsType(ChunkType::CollisionVector)); MemoryStream stream(chunk.GetData()); - _value = stream.Read(); + _value = stream.Read(); } CollisionVolumeOwner::CollisionVolumeOwner(const P3DChunk& chunk) @@ -2007,9 +2007,9 @@ namespace Donut::P3D assert(chunk.IsType(ChunkType::Fence)); MemoryStream stream(chunk.GetData()); - _start = stream.Read(); - _end = stream.Read(); - _normal = stream.Read(); + _start = stream.Read(); + _end = stream.Read(); + _normal = stream.Read(); } Set::Set(const P3DChunk& chunk) @@ -2042,7 +2042,7 @@ namespace Donut::P3D MemoryStream stream(chunk.GetData()); _numPoints = stream.Read(); _points.resize(_numPoints); - stream.ReadBytes(reinterpret_cast(_points.data()), _points.size() * sizeof(glm::vec3)); + stream.ReadBytes(reinterpret_cast(_points.data()), _points.size() * sizeof(Vector3)); } Intersection::Intersection(const P3DChunk& chunk) @@ -2051,7 +2051,7 @@ namespace Donut::P3D MemoryStream stream(chunk.GetData()); _name = stream.ReadLPString(); - _position = stream.Read(); + _position = stream.Read(); _radius = stream.Read(); _trafficBehaviour = stream.Read(); } @@ -2065,9 +2065,9 @@ namespace Donut::P3D _todo0 = stream.Read(); _lanes = stream.Read(); _todo1 = stream.Read(); - _position0 = stream.Read(); - _position1 = stream.Read(); - _position2 = stream.Read(); + _position0 = stream.Read(); + _position1 = stream.Read(); + _position2 = stream.Read(); } Road::Road(const P3DChunk& chunk) @@ -2093,8 +2093,8 @@ namespace Donut::P3D MemoryStream stream(chunk.GetData()); _name = stream.ReadLPString(); _data = stream.ReadLPString(); - _transform = stream.Read(); - _transform2 = stream.Read(); + _transform = stream.Read(); + _transform2 = stream.Read(); } GameAttr::GameAttr(const P3DChunk& chunk) @@ -2206,7 +2206,7 @@ namespace Donut::P3D _yaw = stream.Read(); _pitch = stream.Read(); _distance = stream.Read(); - _offset = stream.Read(); + _offset = stream.Read(); } PhysicsObject::PhysicsObject(const P3DChunk& chunk) @@ -2256,7 +2256,7 @@ namespace Donut::P3D { case ChunkType::PhysicsVector: { - _vector = data.Read(); + _vector = data.Read(); break; } case ChunkType::PhysicsInertiaMatrix: @@ -2275,7 +2275,7 @@ namespace Donut::P3D assert(chunk.IsType(ChunkType::PhysicsVector)); MemoryStream stream(chunk.GetData()); - _value = stream.Read(); + _value = stream.Read(); } PhysicsInertiaMatrix::PhysicsInertiaMatrix(const P3DChunk& chunk) @@ -2283,10 +2283,10 @@ namespace Donut::P3D assert(chunk.IsType(ChunkType::PhysicsInertiaMatrix)); MemoryStream stream(chunk.GetData()); - _position = stream.Read(); - _forward = stream.Read(); - _right = stream.Read(); - _up = stream.Read(); + _position = stream.Read(); + _forward = stream.Read(); + _right = stream.Read(); + _up = stream.Read(); } CompositeDrawableSkinList::CompositeDrawableSkinList(const P3DChunk& chunk) diff --git a/src/P3D/P3D.generated.h b/src/P3D/P3D.generated.h index e74d0ab..dd7646b 100644 --- a/src/P3D/P3D.generated.h +++ b/src/P3D/P3D.generated.h @@ -16,12 +16,13 @@ +---------------------------------------------------+ */ -#include -#include -#include -#include -#include -#include +#include "Core/Math/Matrix4x4.h" +#include "Core/Math/Quaternion.h" +#include "Core/Math/Vector2.h" +#include "Core/Math/Vector3.h" +#include "Core/Math/Vector4.h" +#include "P3D/P3DChunk.h" + #include #include #include @@ -270,10 +271,10 @@ namespace Donut::P3D const uint32_t& GetVersion() const { return _version; } const std::string& GetParam() const { return _param; } const uint16_t& GetMapping() const { return _mapping; } - const glm::vec3& GetConstants() const { return _constants; } + const Vector3& GetConstants() const { return _constants; } const uint32_t& GetNumFrames() const { return _numFrames; } const std::vector& GetFrames() const { return _frames; } - const std::vector& GetValues() const { return _values; } + const std::vector& GetValues() const { return _values; } const std::unique_ptr& GetInterpolationMode() const { return _interpolationMode; } private: @@ -281,10 +282,10 @@ namespace Donut::P3D uint32_t _version; std::string _param; uint16_t _mapping; - glm::vec3 _constants; + Vector3 _constants; uint32_t _numFrames; std::vector _frames; - std::vector _values; + std::vector _values; std::unique_ptr _interpolationMode; }; @@ -301,7 +302,7 @@ namespace Donut::P3D const std::string& GetParam() const { return _param; } const uint32_t& GetNumFrames() const { return _numFrames; } const std::vector& GetFrames() const { return _frames; } - const std::vector& GetValues() const { return _values; } + const std::vector& GetValues() const { return _values; } const std::unique_ptr& GetInterpolationMode() const { return _interpolationMode; } private: @@ -310,7 +311,7 @@ namespace Donut::P3D std::string _param; uint32_t _numFrames; std::vector _frames; - std::vector _values; + std::vector _values; std::unique_ptr _interpolationMode; }; @@ -327,7 +328,7 @@ namespace Donut::P3D const std::string& GetParam() const { return _param; } const uint32_t& GetNumFrames() const { return _numFrames; } const std::vector& GetFrames() const { return _frames; } - const std::vector& GetValues() const { return _values; } + const std::vector& GetValues() const { return _values; } const std::unique_ptr& GetInterpolationMode() const { return _interpolationMode; } private: @@ -336,7 +337,7 @@ namespace Donut::P3D std::string _param; uint32_t _numFrames; std::vector _frames; - std::vector _values; + std::vector _values; std::unique_ptr _interpolationMode; }; @@ -425,13 +426,13 @@ namespace Donut::P3D static std::unique_ptr Load(const P3DChunk& chunk) { return std::make_unique(chunk); } - const glm::vec3& GetMin() const { return _min; } - const glm::vec3& GetMax() const { return _max; } + const Vector3& GetMin() const { return _min; } + const Vector3& GetMax() const { return _max; } private: - glm::vec3 _min; - glm::vec3 _max; + Vector3 _min; + Vector3 _max; }; @@ -443,12 +444,12 @@ namespace Donut::P3D static std::unique_ptr Load(const P3DChunk& chunk) { return std::make_unique(chunk); } - const glm::vec3& GetCentre() const { return _centre; } + const Vector3& GetCentre() const { return _centre; } const float& GetRadius() const { return _radius; } private: - glm::vec3 _centre; + Vector3 _centre; float _radius; }; @@ -468,13 +469,13 @@ namespace Donut::P3D const uint32_t& GetNumVerts() const { return _numVerts; } const uint32_t& GetNumIndices() const { return _numIndices; } const uint32_t& GetNumMatrices() const { return _numMatrices; } - const std::vector& GetVertices() const { return _vertices; } + const std::vector& GetVertices() const { return _vertices; } const std::vector& GetIndices() const { return _indices; } - const std::vector& GetNormals() const { return _normals; } - const std::vector& GetUvs(size_t index) const { return _uvs.at(index); } + const std::vector& GetNormals() const { return _normals; } + const std::vector& GetUvs(size_t index) const { return _uvs.at(index); } const std::vector& GetMatrixList() const { return _matrixList; } const std::vector& GetMatrixPalette() const { return _matrixPalette; } - const std::vector& GetWeightList() const { return _weightList; } + const std::vector& GetWeightList() const { return _weightList; } const std::vector& GetColors() const { return _colors; } private: @@ -486,13 +487,13 @@ namespace Donut::P3D uint32_t _numVerts; uint32_t _numIndices; uint32_t _numMatrices; - std::vector _vertices; + std::vector _vertices; std::vector _indices; - std::vector _normals; - std::vector> _uvs; + std::vector _normals; + std::vector> _uvs; std::vector _matrixList; std::vector _matrixPalette; - std::vector _weightList; + std::vector _weightList; std::vector _colors; }; @@ -506,12 +507,12 @@ namespace Donut::P3D static std::unique_ptr Load(const P3DChunk& chunk) { return std::make_unique(chunk); } const uint32_t& GetSize() const { return _size; } - const std::vector& GetPositions() const { return _positions; } + const std::vector& GetPositions() const { return _positions; } private: uint32_t _size; - std::vector _positions; + std::vector _positions; }; @@ -542,12 +543,12 @@ namespace Donut::P3D static std::unique_ptr Load(const P3DChunk& chunk) { return std::make_unique(chunk); } const uint32_t& GetSize() const { return _size; } - const std::vector& GetNormals() const { return _normals; } + const std::vector& GetNormals() const { return _normals; } private: uint32_t _size; - std::vector _normals; + std::vector _normals; }; @@ -561,13 +562,13 @@ namespace Donut::P3D const uint32_t& GetSize() const { return _size; } const uint32_t& GetChannel() const { return _channel; } - const std::vector& GetUvs() const { return _uvs; } + const std::vector& GetUvs() const { return _uvs; } private: uint32_t _size; uint32_t _channel; - std::vector _uvs; + std::vector _uvs; }; @@ -616,12 +617,12 @@ namespace Donut::P3D static std::unique_ptr Load(const P3DChunk& chunk) { return std::make_unique(chunk); } const uint32_t& GetSize() const { return _size; } - const std::vector& GetUvs() const { return _uvs; } + const std::vector& GetUvs() const { return _uvs; } private: uint32_t _size; - std::vector _uvs; + std::vector _uvs; }; @@ -680,7 +681,7 @@ namespace Donut::P3D const int32_t& GetPrimaryAxis() const { return _primaryAxis; } const int32_t& GetSecondaryAxis() const { return _secondaryAxis; } const int32_t& GetTwistAxis() const { return _twistAxis; } - const glm::mat4& GetRestPose() const { return _restPose; } + const Matrix4x4& GetRestPose() const { return _restPose; } const std::unique_ptr& GetMirrorMap() const { return _mirrorMap; } const std::unique_ptr& GetBonePreserve() const { return _bonePreserve; } @@ -693,7 +694,7 @@ namespace Donut::P3D int32_t _primaryAxis; int32_t _secondaryAxis; int32_t _twistAxis; - glm::mat4 _restPose; + Matrix4x4 _restPose; std::unique_ptr _mirrorMap; std::unique_ptr _bonePreserve; @@ -708,12 +709,12 @@ namespace Donut::P3D static std::unique_ptr Load(const P3DChunk& chunk) { return std::make_unique(chunk); } const uint32_t& GetJointIndex() const { return _jointIndex; } - const glm::vec3& GetAxis() const { return _axis; } + const Vector3& GetAxis() const { return _axis; } private: uint32_t _jointIndex; - glm::vec3 _axis; + Vector3 _axis; }; @@ -957,7 +958,7 @@ namespace Donut::P3D const std::string& GetName() const { return _name; } const uint32_t& GetNumChildren() const { return _numChildren; } - const glm::mat4& GetTransform() const { return _transform; } + const Matrix4x4& GetTransform() const { return _transform; } const std::vector>& GetChildren() const { return _children; } const std::vector>& GetDrawables() const { return _drawables; } @@ -965,7 +966,7 @@ namespace Donut::P3D std::string _name; uint32_t _numChildren; - glm::mat4 _transform; + Matrix4x4 _transform; std::vector> _children; std::vector> _drawables; @@ -1212,15 +1213,15 @@ namespace Donut::P3D static std::unique_ptr Load(const P3DChunk& chunk) { return std::make_unique(chunk); } const std::vector& GetIndices() const { return _indices; } - const std::vector& GetPositions() const { return _positions; } - const std::vector& GetNormals() const { return _normals; } + const std::vector& GetPositions() const { return _positions; } + const std::vector& GetNormals() const { return _normals; } const std::unique_ptr& GetBounds() const { return _bounds; } private: std::vector _indices; - std::vector _positions; - std::vector _normals; + std::vector _positions; + std::vector _normals; std::unique_ptr _bounds; }; @@ -1292,16 +1293,16 @@ namespace Donut::P3D const uint32_t& GetVersion() const { return _version; } const std::string& GetName() const { return _name; } const std::string& GetMode() const { return _mode; } - const glm::vec3& GetTranslation() const { return _translation; } + const Vector3& GetTranslation() const { return _translation; } const uint32_t& GetColor() const { return _color; } - const glm::vec2& GetUv0() const { return _uv0; } - const glm::vec2& GetUv1() const { return _uv1; } - const glm::vec2& GetUv2() const { return _uv2; } - const glm::vec2& GetUv3() const { return _uv3; } + const Vector2& GetUv0() const { return _uv0; } + const Vector2& GetUv1() const { return _uv1; } + const Vector2& GetUv2() const { return _uv2; } + const Vector2& GetUv3() const { return _uv3; } const float& GetWidth() const { return _width; } const float& GetHeight() const { return _height; } const float& GetDistance() const { return _distance; } - const glm::vec2& GetUvOffset() const { return _uvOffset; } + const Vector2& GetUvOffset() const { return _uvOffset; } const std::unique_ptr& GetDisplayInfo() const { return _displayInfo; } const std::unique_ptr& GetPerspectiveInfo() const { return _perspectiveInfo; } @@ -1310,16 +1311,16 @@ namespace Donut::P3D uint32_t _version; std::string _name; std::string _mode; - glm::vec3 _translation; + Vector3 _translation; uint32_t _color; - glm::vec2 _uv0; - glm::vec2 _uv1; - glm::vec2 _uv2; - glm::vec2 _uv3; + Vector2 _uv0; + Vector2 _uv1; + Vector2 _uv2; + Vector2 _uv3; float _width; float _height; float _distance; - glm::vec2 _uvOffset; + Vector2 _uvOffset; std::unique_ptr _displayInfo; std::unique_ptr _perspectiveInfo; @@ -1364,18 +1365,18 @@ namespace Donut::P3D static std::unique_ptr Load(const P3DChunk& chunk) { return std::make_unique(chunk); } const uint32_t& GetVersion() const { return _version; } - const glm::quat& GetRotation() const { return _rotation; } + const Quaternion& GetRotation() const { return _rotation; } const std::string& GetCutOffMode() const { return _cutOffMode; } - const glm::vec2& GetUvOffsetRange() const { return _uvOffsetRange; } + const Vector2& GetUvOffsetRange() const { return _uvOffsetRange; } const float& GetSourceRange() const { return _sourceRange; } const float& GetEdgeRange() const { return _edgeRange; } private: uint32_t _version; - glm::quat _rotation; + Quaternion _rotation; std::string _cutOffMode; - glm::vec2 _uvOffsetRange; + Vector2 _uvOffsetRange; float _sourceRange; float _edgeRange; @@ -1849,7 +1850,7 @@ namespace Donut::P3D const uint32_t& GetVersion() const { return _version; } const uint32_t& GetTranslucent() const { return _translucent; } const uint32_t& GetNumPoints() const { return _numPoints; } - const std::vector& GetPoints() const { return _points; } + const std::vector& GetPoints() const { return _points; } const std::vector& GetColors() const { return _colors; } private: @@ -1858,7 +1859,7 @@ namespace Donut::P3D uint32_t _version; uint32_t _translucent; uint32_t _numPoints; - std::vector _points; + std::vector _points; std::vector _colors; }; @@ -1915,15 +1916,15 @@ namespace Donut::P3D const std::string& GetName() const { return _name; } const uint32_t& GetIsRect() const { return _isRect; } - const glm::vec3& GetBounds() const { return _bounds; } - const glm::mat4& GetTransform() const { return _transform; } + const Vector3& GetBounds() const { return _bounds; } + const Matrix4x4& GetTransform() const { return _transform; } private: std::string _name; uint32_t _isRect; - glm::vec3 _bounds; - glm::mat4 _transform; + Vector3 _bounds; + Matrix4x4 _transform; }; @@ -1941,9 +1942,9 @@ namespace Donut::P3D const float& GetAspectRatio() const { return _aspectRatio; } const float& GetNearClip() const { return _nearClip; } const float& GetFarClip() const { return _farClip; } - const glm::vec3& GetPosition() const { return _position; } - const glm::vec3& GetForward() const { return _forward; } - const glm::vec3& GetUp() const { return _up; } + const Vector3& GetPosition() const { return _position; } + const Vector3& GetForward() const { return _forward; } + const Vector3& GetUp() const { return _up; } private: @@ -1953,9 +1954,9 @@ namespace Donut::P3D float _aspectRatio; float _nearClip; float _farClip; - glm::vec3 _position; - glm::vec3 _forward; - glm::vec3 _up; + Vector3 _position; + Vector3 _forward; + Vector3 _up; }; @@ -2078,12 +2079,12 @@ namespace Donut::P3D static std::unique_ptr Load(const P3DChunk& chunk) { return std::make_unique(chunk); } const float& GetRadius() const { return _radius; } - const std::vector& GetVectors() const { return _vectors; } + const std::vector& GetVectors() const { return _vectors; } private: float _radius; - std::vector _vectors; + std::vector _vectors; }; @@ -2098,14 +2099,14 @@ namespace Donut::P3D const float& GetRadius() const { return _radius; } const float& GetLength() const { return _length; } const uint16_t& GetFlatEnd() const { return _flatEnd; } - const std::vector& GetVectors() const { return _vectors; } + const std::vector& GetVectors() const { return _vectors; } private: float _radius; float _length; uint16_t _flatEnd; - std::vector _vectors; + std::vector _vectors; }; @@ -2117,13 +2118,13 @@ namespace Donut::P3D static std::unique_ptr Load(const P3DChunk& chunk) { return std::make_unique(chunk); } - const glm::vec3& GetHalfExtents() const { return _halfExtents; } - const std::vector& GetVectors() const { return _vectors; } + const Vector3& GetHalfExtents() const { return _halfExtents; } + const std::vector& GetVectors() const { return _vectors; } private: - glm::vec3 _halfExtents; - std::vector _vectors; + Vector3 _halfExtents; + std::vector _vectors; }; @@ -2151,11 +2152,11 @@ namespace Donut::P3D static std::unique_ptr Load(const P3DChunk& chunk) { return std::make_unique(chunk); } - const glm::vec3& GetValue() const { return _value; } + const Vector3& GetValue() const { return _value; } private: - glm::vec3 _value; + Vector3 _value; }; @@ -2249,15 +2250,15 @@ namespace Donut::P3D static std::unique_ptr Load(const P3DChunk& chunk) { return std::make_unique(chunk); } - const glm::vec3& GetStart() const { return _start; } - const glm::vec3& GetEnd() const { return _end; } - const glm::vec3& GetNormal() const { return _normal; } + const Vector3& GetStart() const { return _start; } + const Vector3& GetEnd() const { return _end; } + const Vector3& GetNormal() const { return _normal; } private: - glm::vec3 _start; - glm::vec3 _end; - glm::vec3 _normal; + Vector3 _start; + Vector3 _end; + Vector3 _normal; }; @@ -2290,12 +2291,12 @@ namespace Donut::P3D static std::unique_ptr Load(const P3DChunk& chunk) { return std::make_unique(chunk); } const uint32_t& GetNumPoints() const { return _numPoints; } - const std::vector& GetPoints() const { return _points; } + const std::vector& GetPoints() const { return _points; } private: uint32_t _numPoints; - std::vector _points; + std::vector _points; }; @@ -2308,14 +2309,14 @@ namespace Donut::P3D static std::unique_ptr Load(const P3DChunk& chunk) { return std::make_unique(chunk); } const std::string& GetName() const { return _name; } - const glm::vec3& GetPosition() const { return _position; } + const Vector3& GetPosition() const { return _position; } const float& GetRadius() const { return _radius; } const uint32_t& GetTrafficBehaviour() const { return _trafficBehaviour; } private: std::string _name; - glm::vec3 _position; + Vector3 _position; float _radius; uint32_t _trafficBehaviour; @@ -2333,9 +2334,9 @@ namespace Donut::P3D const uint32_t& GetTodo0() const { return _todo0; } const uint32_t& GetLanes() const { return _lanes; } const uint32_t& GetTodo1() const { return _todo1; } - const glm::vec3& GetPosition0() const { return _position0; } - const glm::vec3& GetPosition1() const { return _position1; } - const glm::vec3& GetPosition2() const { return _position2; } + const Vector3& GetPosition0() const { return _position0; } + const Vector3& GetPosition1() const { return _position1; } + const Vector3& GetPosition2() const { return _position2; } private: @@ -2343,9 +2344,9 @@ namespace Donut::P3D uint32_t _todo0; uint32_t _lanes; uint32_t _todo1; - glm::vec3 _position0; - glm::vec3 _position1; - glm::vec3 _position2; + Vector3 _position0; + Vector3 _position1; + Vector3 _position2; }; @@ -2391,15 +2392,15 @@ namespace Donut::P3D const std::string& GetName() const { return _name; } const std::string& GetData() const { return _data; } - const glm::mat4& GetTransform() const { return _transform; } - const glm::mat4& GetTransform2() const { return _transform2; } + const Matrix4x4& GetTransform() const { return _transform; } + const Matrix4x4& GetTransform2() const { return _transform2; } private: std::string _name; std::string _data; - glm::mat4 _transform; - glm::mat4 _transform2; + Matrix4x4 _transform; + Matrix4x4 _transform2; }; @@ -2523,7 +2524,7 @@ namespace Donut::P3D const float& GetYaw() const { return _yaw; } const float& GetPitch() const { return _pitch; } const float& GetDistance() const { return _distance; } - const glm::vec3& GetOffset() const { return _offset; } + const Vector3& GetOffset() const { return _offset; } private: @@ -2531,7 +2532,7 @@ namespace Donut::P3D float _yaw; float _pitch; float _distance; - glm::vec3 _offset; + Vector3 _offset; }; @@ -2577,7 +2578,7 @@ namespace Donut::P3D const float& GetMinAngle() const { return _minAngle; } const float& GetMaxAngle() const { return _maxAngle; } const float& GetDOF() const { return _DOF; } - const glm::vec3& GetVector() const { return _vector; } + const Vector3& GetVector() const { return _vector; } const std::unique_ptr& GetInertiaMatrix() const { return _inertiaMatrix; } private: @@ -2588,7 +2589,7 @@ namespace Donut::P3D float _minAngle; float _maxAngle; float _DOF; - glm::vec3 _vector; + Vector3 _vector; std::unique_ptr _inertiaMatrix; }; @@ -2601,11 +2602,11 @@ namespace Donut::P3D static std::unique_ptr Load(const P3DChunk& chunk) { return std::make_unique(chunk); } - const glm::vec3& GetValue() const { return _value; } + const Vector3& GetValue() const { return _value; } private: - glm::vec3 _value; + Vector3 _value; }; @@ -2617,17 +2618,17 @@ namespace Donut::P3D static std::unique_ptr Load(const P3DChunk& chunk) { return std::make_unique(chunk); } - const glm::vec3& GetPosition() const { return _position; } - const glm::vec3& GetForward() const { return _forward; } - const glm::vec3& GetRight() const { return _right; } - const glm::vec3& GetUp() const { return _up; } + const Vector3& GetPosition() const { return _position; } + const Vector3& GetForward() const { return _forward; } + const Vector3& GetRight() const { return _right; } + const Vector3& GetUp() const { return _up; } private: - glm::vec3 _position; - glm::vec3 _forward; - glm::vec3 _right; - glm::vec3 _up; + Vector3 _position; + Vector3 _forward; + Vector3 _right; + Vector3 _up; }; diff --git a/src/P3D/P3DChunk.cpp b/src/P3D/P3DChunk.cpp index ca2739e..8a13153 100644 --- a/src/P3D/P3DChunk.cpp +++ b/src/P3D/P3DChunk.cpp @@ -23,7 +23,7 @@ ImageDecoder ImageDecoder::Decode(const std::vector& data) void P3DUtil::GetDrawables( const std::unique_ptr& instanceList, std::vector& drawables, - std::vector& transforms) + std::vector& transforms) { if (!instanceList) return; @@ -36,15 +36,15 @@ void P3DUtil::GetDrawables( for (const auto& child : sceneGraphBranch->GetChildren()) { - GetDrawables(child, drawables, transforms, glm::mat4(1.0f)); + GetDrawables(child, drawables, transforms, Matrix4x4::Identity); } } void P3DUtil::GetDrawables( const std::unique_ptr& transform, std::vector& drawables, - std::vector& transforms, - const glm::mat4& parentTransform) + std::vector& transforms, + const Matrix4x4& parentTransform) { if (!transform) return; diff --git a/src/P3D/P3DChunk.h b/src/P3D/P3DChunk.h index b480207..05a589c 100644 --- a/src/P3D/P3DChunk.h +++ b/src/P3D/P3DChunk.h @@ -2,8 +2,10 @@ #pragma once +#include "Core/Math/Matrix4x4.h" +#include "Core/Math/Vector4.h" + #include -#include #include #include @@ -286,19 +288,19 @@ struct P3DUtil static void GetDrawables( const std::unique_ptr&, std::vector&, - std::vector&); + std::vector&); static void GetDrawables( const std::unique_ptr&, std::vector&, - std::vector&, - const glm::mat4&); + std::vector&, + const Matrix4x4&); static std::string GetShaderTexture(const std::unique_ptr&); - static glm::vec4 ConvertColor(uint32_t v) + static Vector4 ConvertColor(uint32_t v) { - return glm::vec4(((v >> 16) & 255) / 255.0f, + return Vector4(((v >> 16) & 255) / 255.0f, ((v >> 8) & 255) / 255.0f, ((v & 255)) / 255.0f, ((v >> 24) & 255) / 255.0f); diff --git a/src/Physics/BulletCast.h b/src/Physics/BulletCast.h index 2aadd77..aaab64f 100644 --- a/src/Physics/BulletCast.h +++ b/src/Physics/BulletCast.h @@ -2,9 +2,11 @@ #pragma once +#include "Core/Math/Matrix4x4.h" +#include "Core/Math/Matrix3x3.h" +#include "Core/Math/Vector3.h" + #include -#include -#include namespace Donut { @@ -12,48 +14,48 @@ template inline T BulletCast(const F& f); template <> -inline glm::vec3 BulletCast(const btVector3& v) +inline Vector3 BulletCast(const btVector3& v) { - return glm::vec3(v.x(), v.y(), v.z()); + return Vector3(v.x(), v.y(), v.z()); } template <> -inline btVector3 BulletCast(const glm::vec3& v) +inline btVector3 BulletCast(const Vector3& v) { - return btVector3(v.x, v.y, v.z); + return btVector3(v.X, v.Y, v.Z); } -template <> +/*template <> inline btVector3 BulletCast(const glm::ivec3& v) { return btVector3(btScalar(v.x), btScalar(v.y), btScalar(v.z)); -} +}*/ template <> -inline glm::quat BulletCast(const btQuaternion& q) +inline Quaternion BulletCast(const btQuaternion& q) { - return glm::quat(q.w(), q.x(), q.y(), q.z()); + return Quaternion(q.w(), q.x(), q.y(), q.z()); } template <> -inline btQuaternion BulletCast(const glm::quat& q) +inline btQuaternion BulletCast(const Quaternion& q) { - return btQuaternion(q.x, q.y, q.z, q.w); + return btQuaternion(q.X, q.Y, q.Z, q.W); } template <> -inline glm::mat3 BulletCast(const btMatrix3x3& m) +inline Matrix3x3 BulletCast(const btMatrix3x3& m) { - return glm::mat3(BulletCast(m[0]), - BulletCast(m[1]), - BulletCast(m[2])); + return Matrix3x3(m[0][0], m[0][1], m[0][2], + m[1][0], m[1][1], m[1][2], + m[2][0], m[2][1], m[2][2]); } template <> -inline btMatrix3x3 BulletCast(const glm::mat3& m) +inline btMatrix3x3 BulletCast(const Matrix3x3& m) { - return btMatrix3x3(m[0][0], m[0][1], m[0][2], - m[1][0], m[1][1], m[1][2], - m[2][0], m[2][1], m[2][2]); + return btMatrix3x3(m.M[0][0], m.M[0][1], m.M[0][2], + m.M[1][0], m.M[1][1], m.M[1][2], + m.M[2][0], m.M[2][1], m.M[2][2]); } } // namespace Donut diff --git a/src/Physics/BulletDebugDraw.cpp b/src/Physics/BulletDebugDraw.cpp index 83a6dec..691ff38 100644 --- a/src/Physics/BulletDebugDraw.cpp +++ b/src/Physics/BulletDebugDraw.cpp @@ -3,6 +3,7 @@ #include "BulletDebugDraw.h" #include "BulletCast.h" +#include "Core/Math/Vector4.h" namespace Donut { @@ -16,10 +17,10 @@ void BulletDebugDraw::drawLine(const btVector3& from, const btVector3& to, const { if (m_lineRenderer == nullptr) return; - glm::vec3 c = BulletCast(color); - glm::vec4 drawColour(c.x, c.y, c.z, 0.75f); + Vector3 c = BulletCast(color); + Vector4 drawColour(c.X, c.Y, c.Z, 0.75f); - m_lineRenderer->DrawLine(BulletCast(from), BulletCast(to), drawColour); + m_lineRenderer->DrawLine(BulletCast(from), BulletCast(to), drawColour); } void BulletDebugDraw::drawContactPoint(const btVector3& PointOnB, const btVector3& normalOnB, btScalar distance, int lifeTime, const btVector3& color) diff --git a/src/Physics/WorldPhysics.cpp b/src/Physics/WorldPhysics.cpp index 9db4c1e..1a9e8e6 100644 --- a/src/Physics/WorldPhysics.cpp +++ b/src/Physics/WorldPhysics.cpp @@ -1,12 +1,12 @@ // Copyright 2019 the donut authors. See AUTHORS.md +#include "P3D/P3D.generated.h" +#include "Physics/BulletCast.h" +#include "Physics/BulletDebugDraw.h" +#include "Physics/BulletFenceShape.h" +#include "Physics/WorldPhysics.h" + #include -#include -#include -#include -#include -#include -#include namespace Donut { @@ -25,7 +25,7 @@ WorldPhysics::WorldPhysics(LineRenderer* lineRenderer) _dynamicsWorld->setDebugDrawer(_debugDraw.get()); _dynamicsWorld->setGravity(btVector3(0.0f, -1.0f, 0.0f)); - // _char = std::make_unique(this, glm::vec3(glm::vec3(229.0f, 4.5f, -182.0f))); + // _char = std::make_unique(this, Vector3(Vector3(229.0f, 4.5f, -182.0f))); } WorldPhysics::~WorldPhysics() @@ -68,12 +68,12 @@ void WorldPhysics::Update(const float dt) const void WorldPhysics::AddIntersect(const P3D::Intersect& intersect) { // copy this shit over first (todo: free it?) - auto verts = new std::vector(intersect.GetPositions()); + auto verts = new std::vector(intersect.GetPositions()); auto indices = new std::vector(intersect.GetIndices()); btIndexedMesh indexedMesh; indexedMesh.m_vertexBase = reinterpret_cast(verts->data()); - indexedMesh.m_vertexStride = sizeof(glm::vec3); + indexedMesh.m_vertexStride = sizeof(Vector3); indexedMesh.m_numVertices = (int)verts->size(); indexedMesh.m_triangleIndexBase = reinterpret_cast(indices->data()); indexedMesh.m_triangleIndexStride = sizeof(uint32_t) * 3; @@ -127,10 +127,11 @@ void WorldPhysics::AddP3DOBBoxVolume(const P3D::CollisionOBBoxVolume& volume) const auto rotY = volume.GetVectors()[2]; const auto rotZ = volume.GetVectors()[3]; - const glm::quat rotation = glm::toQuat(glm::mat3( - rotX.x, rotX.y, rotX.z, - rotY.x, rotY.y, rotY.z, - rotZ.x, rotZ.y, rotZ.z)); + const Quaternion rotation = Matrix3x3( + rotX.X, rotX.Y, rotX.Z, + rotY.X, rotY.Y, rotY.Z, + rotZ.X, rotZ.Y, rotZ.Z + ).Quat(); const auto he = volume.GetHalfExtents(); const auto bulletShape = new btBoxShape(BulletCast(volume.GetHalfExtents())); @@ -170,7 +171,8 @@ void WorldPhysics::AddP3DCylinder(const P3D::CollisionCylinder& cylinder) { const float radius = cylinder.GetRadius(); const float halfLength = cylinder.GetLength(); - const glm::quat rotation = glm::rotation(glm::vec3(0.0f, 1.0f, 0.0f), cylinder.GetVectors()[1]); + const Quaternion rotation = Quaternion(Vector3::Up, cylinder.GetVectors()[1].Y); // todo: not right + //const Quaternion rotation = glm::rotation(Vector3(0.0f, 1.0f, 0.0f), cylinder.GetVectors()[1]); btConvexShape* shape = nullptr; if (cylinder.GetFlatEnd() == 1) @@ -194,19 +196,20 @@ void WorldPhysics::AddP3DCylinder(const P3D::CollisionCylinder& cylinder) void WorldPhysics::AddP3DFence(const P3D::Fence& fence) { - glm::vec3 start = fence.GetStart(); - glm::vec3 end = fence.GetEnd(); - glm::vec3 normal = fence.GetNormal(); - glm::vec3 center = end + (start - end) * 0.5f; + Vector3 start = fence.GetStart(); + Vector3 end = fence.GetEnd(); + Vector3 normal = fence.GetNormal(); + Vector3 center = end + (start - end) * 0.5f; - const float length = glm::distance(start, end); - const glm::quat rotation = glm::rotation(glm::vec3(0.0f, 0.0f, 1.0f), normal); + const float length = Vector3::Distance(start, end); + const Quaternion rotation = Quaternion(Vector3::Forward, normal.X); // todo: wrong + //const Quaternion rotation = glm::rotation(Vector3(0.0f, 0.0f, 1.0f), normal); // tall box, very little thickness, might turn into a plane if possible const btVector3 boxHalfExtents(length / 2, 50.0f, .0125f); auto box = new btBoxShape(boxHalfExtents); - const float angle = atan2f(normal.x, normal.z); + const float angle = atan2f(normal.X, normal.Z); btTransform worldTransform; worldTransform.setIdentity(); diff --git a/src/Physics/WorldPhysics.h b/src/Physics/WorldPhysics.h index 445fb1d..0e24d80 100644 --- a/src/Physics/WorldPhysics.h +++ b/src/Physics/WorldPhysics.h @@ -69,6 +69,6 @@ class WorldPhysics // allocated objects for cleanup std::vector _allocatedCollisionObjects; std::vector*> _allocatedIndexArrays; - std::vector*> _allocatedVertexArrays; + std::vector*> _allocatedVertexArrays; }; } // namespace Donut diff --git a/src/Render/BillboardBatch.cpp b/src/Render/BillboardBatch.cpp index 81aba25..e00fbfb 100644 --- a/src/Render/BillboardBatch.cpp +++ b/src/Render/BillboardBatch.cpp @@ -2,42 +2,42 @@ #include "BillboardBatch.h" -#include -#include -#include -#include -#include -#include -#include -#include +#include "Game.h" +#include "P3D/P3D.generated.h" +#include "Render/OpenGL/IndexBuffer.h" +#include "Render/OpenGL/ShaderProgram.h" +#include "Render/OpenGL/VertexBinding.h" +#include "Render/OpenGL/VertexBuffer.h" +#include "Render/Shader.h" +#include "ResourceManager.h" + #include -#include namespace Donut { struct QuadInstance { - glm::vec3 pos; - glm::vec2 size; - glm::vec4 color; - glm::vec2 uv0; - glm::vec2 uv1; - glm::vec2 uv2; - glm::vec2 uv3; + Vector3 pos; + Vector2 size; + Vector4 color; + Vector2 uv0; + Vector2 uv1; + Vector2 uv2; + Vector2 uv3; }; -static std::array quadVertices = { - glm::vec2(-0.5, 0.5), - glm::vec2(0.5, 0.5), - glm::vec2(0.5, -0.5), - glm::vec2(-0.5, -0.5), +static std::array quadVertices = { + Vector2(-0.5, 0.5), + Vector2(0.5, 0.5), + Vector2(0.5, -0.5), + Vector2(-0.5, -0.5), }; static std::array indices = { 0, 1, 2, 2, 3, 0 }; BillboardBatch::BillboardBatch(const P3D::BillboardQuadGroup& billboardQuadGroup) { - static const size_t vertStride = sizeof(glm::vec2); + static const size_t vertStride = sizeof(Vector2); static const size_t instanceStride = sizeof(QuadInstance); _numQuads = billboardQuadGroup.GetQuadCount(); @@ -48,7 +48,7 @@ BillboardBatch::BillboardBatch(const P3D::BillboardQuadGroup& billboardQuadGroup { quadInstances.push_back(QuadInstance { billboardQuad->GetTranslation(), - glm::vec2(billboardQuad->GetWidth(), billboardQuad->GetHeight()), + Vector2(billboardQuad->GetWidth(), billboardQuad->GetHeight()), P3D::P3DUtil::ConvertColor(billboardQuad->GetColor()), billboardQuad->GetUv0(), billboardQuad->GetUv1(), diff --git a/src/Render/CompositeModel.cpp b/src/Render/CompositeModel.cpp index b4613ea..70a3dab 100644 --- a/src/Render/CompositeModel.cpp +++ b/src/Render/CompositeModel.cpp @@ -54,7 +54,7 @@ CompositeModel::CompositeModel(const ICompositeModel& provider) const auto& textures = provider.GetTextures(); std::map meshNames; - std::map> jointTransforms; + std::map> jointTransforms; for (const auto& meshP3D : meshes) { @@ -68,10 +68,10 @@ CompositeModel::CompositeModel(const ICompositeModel& provider) { const auto& skeletonJoints = skeleton->GetJoints(); - std::vector transforms; + std::vector transforms; transforms.reserve(skeletonJoints.size()); for (const auto& joint : skeletonJoints) - transforms.push_back(transforms.empty() ? glm::mat4(1.0f) : transforms[joint->GetParent()] * joint->GetRestPose()); + transforms.push_back(transforms.empty() ? Matrix4x4::Identity : transforms[joint->GetParent()] * joint->GetRestPose()); const auto& skeletonName = skeleton->GetName(); jointTransforms.insert({ skeletonName, std::move(transforms) }); @@ -118,7 +118,7 @@ std::unique_ptr CompositeModel::LoadP3D(const std::string& filen return std::make_unique(CompositeModel_Chunk(p3d.GetRoot())); } -void CompositeModel::Draw(GL::ShaderProgram& shader, const glm::mat4& viewProj, const glm::mat4& modelMatrix, bool opaque) +void CompositeModel::Draw(GL::ShaderProgram& shader, const Matrix4x4& viewProj, const Matrix4x4& modelMatrix, bool opaque) { for (const auto& prop : _props) { diff --git a/src/Render/CompositeModel.h b/src/Render/CompositeModel.h index d5559b6..b591c38 100644 --- a/src/Render/CompositeModel.h +++ b/src/Render/CompositeModel.h @@ -2,10 +2,10 @@ #pragma once -#include -#include -#include -#include +#include "Render/BillboardBatch.h" +#include "Render/Mesh.h" +#include "ResourceManager.h" + #include namespace Donut @@ -65,19 +65,19 @@ class CompositeModel static std::unique_ptr LoadP3D(const std::string&); - void Draw(GL::ShaderProgram&, const glm::mat4&, const glm::mat4&, bool); + void Draw(GL::ShaderProgram&, const Matrix4x4&, const Matrix4x4&, bool); - void SetTransform(const glm::mat4& transform) { _transform = transform; } - const glm::mat4& GetTransform() const { return _transform; } + void SetTransform(const Matrix4x4& transform) { _transform = transform; } + const Matrix4x4& GetTransform() const { return _transform; } private: struct DrawableProp { size_t meshIndex; - glm::mat4 transform; + Matrix4x4 transform; }; - glm::mat4 _transform; + Matrix4x4 _transform; std::vector> _meshes; std::vector> _billboards; std::vector _props; diff --git a/src/Render/LineRenderer.cpp b/src/Render/LineRenderer.cpp index e7581ba..601b07c 100644 --- a/src/Render/LineRenderer.cpp +++ b/src/Render/LineRenderer.cpp @@ -1,10 +1,12 @@ // Copyright 2019 the donut authors. See AUTHORS.md -#include -#include -#include -#include -#include +#include "LineRenderer.h" + +#include "Skeleton.h" +#include "Core/Math/Math.h" +#include "Core/Math/Quaternion.h" +#include "Core/Math/Vector3.h" +#include "Core/Math/Vector4.h" namespace Donut { @@ -57,7 +59,7 @@ LineRenderer::LineRenderer(size_t maxVertexCount): _shader = std::make_unique(VertSrc, FragSrc); } -void LineRenderer::Flush(glm::mat4& viewProj) +void LineRenderer::Flush(Matrix4x4& viewProj) { if (_vertexCount < 2) return; @@ -74,69 +76,67 @@ void LineRenderer::Flush(glm::mat4& viewProj) _vertexCount = 0; } -void LineRenderer::DrawLine(const glm::vec3& p1, const glm::vec3& p2, const glm::vec4& colour) +void LineRenderer::DrawLine(const Vector3& p1, const Vector3& p2, const Vector4& colour) { BufferVertex(p1, colour); BufferVertex(p2, colour); } -void LineRenderer::DrawBox(const glm::mat4 transform, const glm::vec3& mins, const glm::vec3& maxs, const glm::vec4& colour) +void LineRenderer::DrawBox(const Matrix4x4 transform, const Vector3& mins, const Vector3& maxs, const Vector4& colour) { - DrawLine(transform * glm::vec4(mins.x, mins.y, mins.z, 1), transform * glm::vec4(maxs.x, mins.y, mins.z, 1), colour); - DrawLine(transform * glm::vec4(maxs.x, mins.y, mins.z, 1), transform * glm::vec4(maxs.x, maxs.y, mins.z, 1), colour); - DrawLine(transform * glm::vec4(maxs.x, maxs.y, mins.z, 1), transform * glm::vec4(mins.x, maxs.y, mins.z, 1), colour); - DrawLine(transform * glm::vec4(mins.x, maxs.y, mins.z, 1), transform * glm::vec4(mins.x, mins.y, mins.z, 1), colour); - DrawLine(transform * glm::vec4(mins.x, mins.y, mins.z, 1), transform * glm::vec4(mins.x, mins.y, maxs.z, 1), colour); - DrawLine(transform * glm::vec4(maxs.x, mins.y, mins.z, 1), transform * glm::vec4(maxs.x, mins.y, maxs.z, 1), colour); - DrawLine(transform * glm::vec4(maxs.x, maxs.y, mins.z, 1), transform * glm::vec4(maxs.x, maxs.y, maxs.z, 1), colour); - DrawLine(transform * glm::vec4(mins.x, maxs.y, mins.z, 1), transform * glm::vec4(mins.x, maxs.y, maxs.z, 1), colour); - DrawLine(transform * glm::vec4(mins.x, mins.y, maxs.z, 1), transform * glm::vec4(maxs.x, mins.y, maxs.z, 1), colour); - DrawLine(transform * glm::vec4(maxs.x, mins.y, maxs.z, 1), transform * glm::vec4(maxs.x, maxs.y, maxs.z, 1), colour); - DrawLine(transform * glm::vec4(maxs.x, maxs.y, maxs.z, 1), transform * glm::vec4(mins.x, maxs.y, maxs.z, 1), colour); - DrawLine(transform * glm::vec4(mins.x, maxs.y, maxs.z, 1), transform * glm::vec4(mins.x, mins.y, maxs.z, 1), colour); + //DrawLine(transform * Vector4(mins.X, mins.Y, mins.Z, 1), transform * Vector4(maxs.X, mins.Y, mins.Z, 1), colour); + //DrawLine(transform * Vector4(maxs.X, mins.Y, mins.Z, 1), transform * Vector4(maxs.X, maxs.Y, mins.Z, 1), colour); + //DrawLine(transform * Vector4(maxs.X, maxs.Y, mins.Z, 1), transform * Vector4(mins.X, maxs.Y, mins.Z, 1), colour); + //DrawLine(transform * Vector4(mins.X, maxs.Y, mins.Z, 1), transform * Vector4(mins.X, mins.Y, mins.Z, 1), colour); + //DrawLine(transform * Vector4(mins.X, mins.Y, mins.Z, 1), transform * Vector4(mins.X, mins.Y, maxs.Z, 1), colour); + //DrawLine(transform * Vector4(maxs.X, mins.Y, mins.Z, 1), transform * Vector4(maxs.X, mins.Y, maxs.Z, 1), colour); + //DrawLine(transform * Vector4(maxs.X, maxs.Y, mins.Z, 1), transform * Vector4(maxs.X, maxs.Y, maxs.Z, 1), colour); + //DrawLine(transform * Vector4(mins.X, maxs.Y, mins.Z, 1), transform * Vector4(mins.X, maxs.Y, maxs.Z, 1), colour); + //DrawLine(transform * Vector4(mins.X, mins.Y, maxs.Z, 1), transform * Vector4(maxs.X, mins.Y, maxs.Z, 1), colour); + //DrawLine(transform * Vector4(maxs.X, mins.Y, maxs.Z, 1), transform * Vector4(maxs.X, maxs.Y, maxs.Z, 1), colour); + //DrawLine(transform * Vector4(maxs.X, maxs.Y, maxs.Z, 1), transform * Vector4(mins.X, maxs.Y, maxs.Z, 1), colour); + //DrawLine(transform * Vector4(mins.X, maxs.Y, maxs.Z, 1), transform * Vector4(mins.X, mins.Y, maxs.Z, 1), colour); } -void LineRenderer::DrawAABBox(const glm::vec3& position, const glm::vec3& mins, const glm::vec3& maxs, const glm::vec4& colour) +void LineRenderer::DrawAABBox(const Vector3& position, const Vector3& mins, const Vector3& maxs, const Vector4& colour) { DrawAABBox(position + mins, position + maxs, colour); } -void LineRenderer::DrawAABBox(const glm::vec3& mins, const glm::vec3& maxs, const glm::vec4& colour) +void LineRenderer::DrawAABBox(const Vector3& mins, const Vector3& maxs, const Vector4& colour) { - DrawLine(glm::vec3(mins.x, mins.y, mins.z), glm::vec3(maxs.x, mins.y, mins.z), colour); - DrawLine(glm::vec3(maxs.x, mins.y, mins.z), glm::vec3(maxs.x, maxs.y, mins.z), colour); - DrawLine(glm::vec3(maxs.x, maxs.y, mins.z), glm::vec3(mins.x, maxs.y, mins.z), colour); - DrawLine(glm::vec3(mins.x, maxs.y, mins.z), glm::vec3(mins.x, mins.y, mins.z), colour); - DrawLine(glm::vec3(mins.x, mins.y, mins.z), glm::vec3(mins.x, mins.y, maxs.z), colour); - DrawLine(glm::vec3(maxs.x, mins.y, mins.z), glm::vec3(maxs.x, mins.y, maxs.z), colour); - DrawLine(glm::vec3(maxs.x, maxs.y, mins.z), glm::vec3(maxs.x, maxs.y, maxs.z), colour); - DrawLine(glm::vec3(mins.x, maxs.y, mins.z), glm::vec3(mins.x, maxs.y, maxs.z), colour); - DrawLine(glm::vec3(mins.x, mins.y, maxs.z), glm::vec3(maxs.x, mins.y, maxs.z), colour); - DrawLine(glm::vec3(maxs.x, mins.y, maxs.z), glm::vec3(maxs.x, maxs.y, maxs.z), colour); - DrawLine(glm::vec3(maxs.x, maxs.y, maxs.z), glm::vec3(mins.x, maxs.y, maxs.z), colour); - DrawLine(glm::vec3(mins.x, maxs.y, maxs.z), glm::vec3(mins.x, mins.y, maxs.z), colour); + DrawLine(Vector3(mins.X, mins.Y, mins.Z), Vector3(maxs.X, mins.Y, mins.Z), colour); + DrawLine(Vector3(maxs.X, mins.Y, mins.Z), Vector3(maxs.X, maxs.Y, mins.Z), colour); + DrawLine(Vector3(maxs.X, maxs.Y, mins.Z), Vector3(mins.X, maxs.Y, mins.Z), colour); + DrawLine(Vector3(mins.X, maxs.Y, mins.Z), Vector3(mins.X, mins.Y, mins.Z), colour); + DrawLine(Vector3(mins.X, mins.Y, mins.Z), Vector3(mins.X, mins.Y, maxs.Z), colour); + DrawLine(Vector3(maxs.X, mins.Y, mins.Z), Vector3(maxs.X, mins.Y, maxs.Z), colour); + DrawLine(Vector3(maxs.X, maxs.Y, mins.Z), Vector3(maxs.X, maxs.Y, maxs.Z), colour); + DrawLine(Vector3(mins.X, maxs.Y, mins.Z), Vector3(mins.X, maxs.Y, maxs.Z), colour); + DrawLine(Vector3(mins.X, mins.Y, maxs.Z), Vector3(maxs.X, mins.Y, maxs.Z), colour); + DrawLine(Vector3(maxs.X, mins.Y, maxs.Z), Vector3(maxs.X, maxs.Y, maxs.Z), colour); + DrawLine(Vector3(maxs.X, maxs.Y, maxs.Z), Vector3(mins.X, maxs.Y, maxs.Z), colour); + DrawLine(Vector3(mins.X, maxs.Y, maxs.Z), Vector3(mins.X, mins.Y, maxs.Z), colour); } -void LineRenderer::DrawBox(const glm::vec3& position, const glm::vec3& angles, const glm::vec3& mins, const glm::vec3& maxs, const glm::vec4& colour) +void LineRenderer::DrawBox(const Vector3& position, const Vector3& angles, const Vector3& mins, const Vector3& maxs, const Vector4& colour) { - DrawBox(position, glm::quat(glm::radians(angles)), mins, maxs, colour); + //DrawBox(position, Quaternion(Math::DegreesToRadians(angles)), mins, maxs, colour); } -void LineRenderer::DrawBox(const glm::vec3& position, const glm::quat& angles, const glm::vec3& mins, const glm::vec3& maxs, const glm::vec4& colour) +void LineRenderer::DrawBox(const Vector3& position, const Quaternion& angles, const Vector3& mins, const Vector3& maxs, const Vector4& colour) { - glm::mat4 rot = glm::toMat4(angles); - glm::mat4 trans = glm::translate(glm::mat4(1.0f), position); - glm::mat4 transform = trans * rot; + //Matrix4x4 transform = Matrix4x4::MakeTranslate(position) * angles; - DrawBox(transform, mins, maxs, colour); + DrawBox(Matrix4x4::Identity, mins, maxs, colour); } -void LineRenderer::DrawSphere(const glm::vec3& position, float radius, int thetaSegments, int phiSegments, const glm::vec4& colour) +void LineRenderer::DrawSphere(const Vector3& position, float radius, int thetaSegments, int phiSegments, const Vector4& colour) { int theta = thetaSegments + 1; int phi = phiSegments; - std::vector vertices; + std::vector vertices; vertices.reserve(phi * theta); for (int i = 0; i < phi; ++i) @@ -145,10 +145,10 @@ void LineRenderer::DrawSphere(const glm::vec3& position, float radius, int theta { float u = j / (float)(theta - 1); float v = i / (float)(phi - 1); - float t = glm::two_pi() * u; - float p = glm::pi() * v; + float t = Math::Pi2 * u; + float p = Math::Pi * v; - glm::vec3 vertex(radius * sin(p) * cos(t), + Vector3 vertex(radius * sin(p) * cos(t), radius * cos(p), radius * sin(p) * sin(t)); @@ -170,20 +170,20 @@ void LineRenderer::DrawSphere(const glm::vec3& position, float radius, int theta } } -void LineRenderer::DrawCone(const glm::vec3& position, float radius, float height, std::size_t sides, const glm::vec4& colour) +void LineRenderer::DrawCone(const Vector3& position, float radius, float height, std::size_t sides, const Vector4& colour) { if (sides < 3) return; - std::vector vertices; + std::vector vertices; vertices.reserve(sides + 1); - glm::vec3 topVertex = position + (glm::vec3(0, 1, 0) * height); + Vector3 topVertex = position + (Vector3(0, 1, 0) * height); for (std::size_t i = 0; i <= sides; ++i) { - float r = glm::two_pi() * (i / (float)sides); - glm::vec3 vertex(radius * cos(r), 0.0f, radius * sin(r)); + float r = Math::Pi2 * (i / (float)sides); + Vector3 vertex(radius * cos(r), 0.0f, radius * sin(r)); vertices.push_back(position + vertex); } @@ -194,20 +194,20 @@ void LineRenderer::DrawCone(const glm::vec3& position, float radius, float heigh } } -void LineRenderer::DrawCone(const glm::vec3& position, const glm::quat& rotation, float radius, float height, std::size_t sides, const glm::vec4& colour) +void LineRenderer::DrawCone(const Vector3& position, const Quaternion& rotation, float radius, float height, std::size_t sides, const Vector4& colour) { if (sides < 3) return; - std::vector vertices; + std::vector vertices; vertices.reserve(sides + 1); - glm::vec3 topVertex = position + (rotation * glm::vec3(0, height, 0)); + Vector3 topVertex = position + (rotation * Vector3(0, height, 0)); for (std::size_t i = 0; i <= sides; ++i) { - float r = glm::two_pi() * (i / (float)sides); - glm::vec3 vertex(radius * cos(r), 0.0f, radius * sin(r)); + float r = Math::Pi2 * (i / (float)sides); + Vector3 vertex(radius * cos(r), 0.0f, radius * sin(r)); vertices.push_back(position + (rotation * vertex)); } @@ -218,32 +218,32 @@ void LineRenderer::DrawCone(const glm::vec3& position, const glm::quat& rotation } } -void LineRenderer::DrawSkeleton(const glm::vec3& position, const Skeleton& skeleton) +void LineRenderer::DrawSkeleton(const Vector3& position, const Skeleton& skeleton) { auto const& joints = skeleton.GetJoints(); for (auto const& joint : joints) { auto const& parent = joints[joint.parent]; - glm::mat4 mParent = glm::translate(position) * parent.pose; - glm::mat4 mJoint = glm::translate(position) * joint.pose; + Matrix4x4 mParent = Matrix4x4::MakeTranslate(position) * parent.pose; + Matrix4x4 mJoint = Matrix4x4::MakeTranslate(position) * joint.pose; - const glm::vec4 lineColor(1.0f, 1.0f, 1.0f, 1.0f); - const glm::vec4 sphereColor(0.0f, 1.0f, 0.0f, 1.0f); + const Vector4 lineColor(1.0f, 1.0f, 1.0f, 1.0f); + const Vector4 sphereColor(0.0f, 1.0f, 0.0f, 1.0f); - DrawSphere(glm::vec3(mJoint[3]), 0.025f, 4, 4, sphereColor); - DrawLine(glm::vec3(mParent[3]), glm::vec3(mJoint[3]), lineColor); + DrawSphere(mJoint.Translation(), 0.025f, 4, 4, sphereColor); + DrawLine(mParent.Translation(), mJoint.Translation(), lineColor); } } -void LineRenderer::BufferVertex(const glm::vec3& position, const glm::vec4& colour) +void LineRenderer::BufferVertex(const Vector3& position, const Vector4& colour) { if (_vertexCount >= _maxVertexCount) return; - uint8_t* vertexData = &_buffer[_vertexCount * kVertexSize]; - *(glm::vec3*)(vertexData) = position; - *(glm::vec4*)(vertexData + sizeof(glm::vec3)) = colour; + uint8_t* vertexData = &_buffer[_vertexCount * kVertexSize]; + *(Vector3*)(vertexData) = position; + *(Vector4*)(vertexData + sizeof(Vector3)) = colour; _vertexCount++; } diff --git a/src/Render/LineRenderer.h b/src/Render/LineRenderer.h index 063aec3..d9d3c09 100644 --- a/src/Render/LineRenderer.h +++ b/src/Render/LineRenderer.h @@ -2,10 +2,11 @@ #pragma once -#include -#include -#include -#include +#include "Core/Math/Fwd.h" +#include "Render/OpenGL/ShaderProgram.h" +#include "Render/OpenGL/VertexBinding.h" +#include "Render/OpenGL/VertexBuffer.h" + #include namespace Donut @@ -17,24 +18,24 @@ class LineRenderer public: LineRenderer(std::size_t maxVertexCount); - void DrawLine(const glm::vec3& p1, const glm::vec3& p2, const glm::vec4& colour); - void DrawBox(const glm::mat4 transform, const glm::vec3& mins, const glm::vec3& maxs, const glm::vec4& colour); - void DrawBox(const glm::vec3& position, const glm::vec3& angles, const glm::vec3& mins, const glm::vec3& maxs, const glm::vec4& colour); - void DrawBox(const glm::vec3& position, const glm::quat& angles, const glm::vec3& mins, const glm::vec3& maxs, const glm::vec4& colour); - void DrawAABBox(const glm::vec3& mins, const glm::vec3& maxs, const glm::vec4& colour); - void DrawAABBox(const glm::vec3& position, const glm::vec3& mins, const glm::vec3& maxs, const glm::vec4& colour); - void DrawSphere(const glm::vec3& position, float radius, int thetaSegments, int phiSegments, const glm::vec4& colour); - void DrawCone(const glm::vec3& position, float radius, float height, std::size_t sides, const glm::vec4& colour); - void DrawCone(const glm::vec3& position, const glm::quat& rotation, float radius, float height, std::size_t sides, const glm::vec4& colour); - void DrawSkeleton(const glm::vec3& position, const Skeleton& skeleton); + void DrawLine(const Vector3& p1, const Vector3& p2, const Vector4& colour); + void DrawBox(const Matrix4x4 transform, const Vector3& mins, const Vector3& maxs, const Vector4& colour); + void DrawBox(const Vector3& position, const Vector3& angles, const Vector3& mins, const Vector3& maxs, const Vector4& colour); + void DrawBox(const Vector3& position, const Quaternion& angles, const Vector3& mins, const Vector3& maxs, const Vector4& colour); + void DrawAABBox(const Vector3& mins, const Vector3& maxs, const Vector4& colour); + void DrawAABBox(const Vector3& position, const Vector3& mins, const Vector3& maxs, const Vector4& colour); + void DrawSphere(const Vector3& position, float radius, int thetaSegments, int phiSegments, const Vector4& colour); + void DrawCone(const Vector3& position, float radius, float height, std::size_t sides, const Vector4& colour); + void DrawCone(const Vector3& position, const Quaternion& rotation, float radius, float height, std::size_t sides, const Vector4& colour); + void DrawSkeleton(const Vector3& position, const Skeleton& skeleton); - void Flush(glm::mat4& viewProj); + void Flush(Matrix4x4& viewProj); std::size_t GetVertexCount() const { return _vertexCount; } std::size_t GetMaxVertexCount() const { return _maxVertexCount; } private: - void BufferVertex(const glm::vec3& position, const glm::vec4& colour); + void BufferVertex(const Vector3& position, const Vector4& colour); static inline const std::size_t kVertexSize = 28; std::size_t _vertexCount; diff --git a/src/Render/Mesh.cpp b/src/Render/Mesh.cpp index d0dac04..ca9957a 100644 --- a/src/Render/Mesh.cpp +++ b/src/Render/Mesh.cpp @@ -53,8 +53,8 @@ void Mesh::CreateMeshBuffers(const P3D::Geometry& geometry) { allVerts.push_back(Vertex { verts[i], - glm::vec2(uvs[i].x, 1.0f - uvs[i].y), - hasColors ? P3D::P3DUtil::ConvertColor(colors[i]) : glm::vec4(1.0f, 1.0f, 1.0f, 1.0f), + Vector2(uvs[i].X, 1.0f - uvs[i].Y), + hasColors ? P3D::P3DUtil::ConvertColor(colors[i]) : Vector4(1.0f, 1.0f, 1.0f, 1.0f), }); } @@ -145,7 +145,7 @@ void Mesh::DrawPrimGroup(const PrimGroup& primGroup) reinterpret_cast(primGroup.indicesOffset * 4)); } -MeshInstanced::MeshInstanced(const P3D::Geometry& geometry, const std::vector& transforms): +MeshInstanced::MeshInstanced(const P3D::Geometry& geometry, const std::vector& transforms): Mesh(geometry), _transforms(std::move(transforms)) { @@ -154,7 +154,7 @@ MeshInstanced::MeshInstanced(const P3D::Geometry& geometry, const std::vector(_transforms.data(), _transforms.size(), instanceStride); diff --git a/src/Render/Mesh.h b/src/Render/Mesh.h index 61b4c73..7ceeb7a 100644 --- a/src/Render/Mesh.h +++ b/src/Render/Mesh.h @@ -2,13 +2,14 @@ #pragma once -#include -#include -#include -#include -#include -#include -#include +#include "Core/Math/Fwd.h" +#include "P3D/P3D.generated.h" +#include "Render/OpenGL/IndexBuffer.h" +#include "Render/OpenGL/ShaderProgram.h" +#include "Render/OpenGL/VertexBinding.h" +#include "Render/OpenGL/VertexBuffer.h" +#include "Render/SkinAnimation.h" + #include namespace Donut @@ -36,9 +37,9 @@ class Mesh struct Vertex { - glm::vec3 pos; - glm::vec2 uv; - glm::vec4 co0lor; + Vector3 pos; + Vector2 uv; + Vector4 co0lor; }; void CreateMeshBuffers(const P3D::Geometry& geometry); @@ -53,21 +54,21 @@ class Mesh std::shared_ptr _indexBuffer; std::shared_ptr _vertexBinding; - glm::vec3 _boundingBoxMin; - glm::vec3 _boundingBoxMax; + Vector3 _boundingBoxMin; + Vector3 _boundingBoxMax; }; class MeshInstanced: public Mesh { public: - MeshInstanced(const P3D::Geometry& geometry, const std::vector& transforms); + MeshInstanced(const P3D::Geometry& geometry, const std::vector& transforms); protected: virtual void CreateVertexBinding() override; virtual void DrawPrimGroup(const PrimGroup& primGroup) override; - std::vector _transforms; + std::vector _transforms; std::shared_ptr _instanceBuffer; }; diff --git a/src/Render/OpenGL/GLTexture2D.h b/src/Render/OpenGL/GLTexture2D.h index f795190..839fe9b 100644 --- a/src/Render/OpenGL/GLTexture2D.h +++ b/src/Render/OpenGL/GLTexture2D.h @@ -3,7 +3,6 @@ #pragma once #include "Render/OpenGL/glad/glad.h" -#include namespace Donut::GL { @@ -29,7 +28,6 @@ class GLTexture2D const GLuint GetHandle() const { return _textureID; } const GLsizei GetWidth() const { return _width; } const GLsizei GetHeight() const { return _height; } - const glm::ivec2 GetSize() const { return glm::ivec2(_width, _height); } private: GLuint _textureID; diff --git a/src/Render/OpenGL/ShaderProgram.cpp b/src/Render/OpenGL/ShaderProgram.cpp index 1f5f93b..057ecfb 100644 --- a/src/Render/OpenGL/ShaderProgram.cpp +++ b/src/Render/OpenGL/ShaderProgram.cpp @@ -1,11 +1,18 @@ // Copyright 2019 the donut authors. See AUTHORS.md -#include +#include "ShaderProgram.h" + +#include "Core/Math/Matrix4x4.h" +#include "Core/Math/Matrix3x3.h" +#include "Core/Math/Quaternion.h" +#include "Core/Math/Vector2.h" +#include "Core/Math/Vector3.h" +#include "Core/Math/Vector4.h" + #include #include #include #include -#include namespace Donut::GL { @@ -91,34 +98,34 @@ void ShaderProgram::SetUniformValue(const char* uniformName, float value) glUniform1f(_uniforms[uniformName], value); } -void ShaderProgram::SetUniformValue(const char* uniformName, const glm::vec2& v) +void ShaderProgram::SetUniformValue(const char* uniformName, const Vector2& v) { - glUniform2fv(_uniforms[uniformName], 1, glm::value_ptr(v)); + glUniform2fv(_uniforms[uniformName], 1, &v.X); } -void ShaderProgram::SetUniformValue(const char* uniformName, const glm::vec3& v) +void ShaderProgram::SetUniformValue(const char* uniformName, const Vector3& v) { - glUniform3fv(_uniforms[uniformName], 1, glm::value_ptr(v)); + glUniform3fv(_uniforms[uniformName], 1, &v.X); } -void ShaderProgram::SetUniformValue(const char* uniformName, const glm::vec4& v) +void ShaderProgram::SetUniformValue(const char* uniformName, const Vector4& v) { - glUniform4fv(_uniforms[uniformName], 1, glm::value_ptr(v)); + glUniform4fv(_uniforms[uniformName], 1, &v.X); } -void ShaderProgram::SetUniformValue(const char* uniformName, const glm::mat3& m) +void ShaderProgram::SetUniformValue(const char* uniformName, const Matrix3x3& m) { - glUniformMatrix3fv(_uniforms[uniformName], 1, GL_FALSE, glm::value_ptr(m)); + glUniformMatrix3fv(_uniforms[uniformName], 1, GL_FALSE, &m.M[0][0]); } -void ShaderProgram::SetUniformValue(const char* uniformName, const glm::mat4& m) +void ShaderProgram::SetUniformValue(const char* uniformName, const Matrix4x4& m) { - glUniformMatrix4fv(_uniforms[uniformName], 1, GL_FALSE, glm::value_ptr(m)); + glUniformMatrix4fv(_uniforms[uniformName], 1, GL_FALSE, m.M16); } -void ShaderProgram::SetUniformValue(const char* uniformName, std::size_t count, const glm::mat4* m) +void ShaderProgram::SetUniformValue(const char* uniformName, std::size_t count, const Matrix4x4* m) { - glUniformMatrix4fv(_uniforms[uniformName], (GLsizei)count, GL_FALSE, glm::value_ptr(m[0])); + glUniformMatrix4fv(_uniforms[uniformName], (GLsizei)count, GL_FALSE, &m[0].M[0][0]); } GLuint ShaderProgram::createSubShader(GLenum type, const std::string& source) diff --git a/src/Render/OpenGL/ShaderProgram.h b/src/Render/OpenGL/ShaderProgram.h index 5fa2c7b..766901d 100644 --- a/src/Render/OpenGL/ShaderProgram.h +++ b/src/Render/OpenGL/ShaderProgram.h @@ -2,9 +2,9 @@ #pragma once +#include "Core/Math/Fwd.h" #include "Render/OpenGL/glad/glad.h" -#include #include #include @@ -23,12 +23,12 @@ class ShaderProgram void SetUniformValue(const char* uniformName, int value); void SetUniformValue(const char* uniformName, float value); - void SetUniformValue(const char* uniformName, const glm::vec2& v); - void SetUniformValue(const char* uniformName, const glm::vec3& v); - void SetUniformValue(const char* uniformName, const glm::vec4& v); - void SetUniformValue(const char* uniformName, const glm::mat3& m); - void SetUniformValue(const char* uniformName, const glm::mat4& m); - void SetUniformValue(const char* uniformName, std::size_t count, const glm::mat4* m); + void SetUniformValue(const char* uniformName, const Vector2& v); + void SetUniformValue(const char* uniformName, const Vector3& v); + void SetUniformValue(const char* uniformName, const Vector4& v); + void SetUniformValue(const char* uniformName, const Matrix3x3& m); + void SetUniformValue(const char* uniformName, const Matrix4x4& m); + void SetUniformValue(const char* uniformName, std::size_t count, const Matrix4x4* m); GLuint GetRawHandle() const { return _program; } diff --git a/src/Render/SkinAnimation.cpp b/src/Render/SkinAnimation.cpp index 1bf5473..afc3e5f 100644 --- a/src/Render/SkinAnimation.cpp +++ b/src/Render/SkinAnimation.cpp @@ -2,24 +2,21 @@ #include "SkinAnimation.h" -#include -#include - namespace Donut { -glm::mat4 SkinAnimation::Evaluate(size_t trackIndex, float time) +Matrix4x4 SkinAnimation::Evaluate(size_t trackIndex, float time) { const auto& track = _tracks[trackIndex]; return track->Evaluate(time); } -glm::mat4 SkinAnimation::Track::Evaluate(float time) +Matrix4x4 SkinAnimation::Track::Evaluate(float time) { - glm::mat4 rot = glm::toMat4(_rotationKeys.Evalulate(time, glm::quat(1, 0, 0, 0))); - glm::mat4 trans = glm::translate(glm::mat4(1.0f), _translationKeys.Evalulate(time, glm::vec3(1.0f))); - glm::mat4 final = trans * rot; - - return final; + // Matrix4x4 rot = glm::toMat4(_rotationKeys.Evalulate(time, Quaternion(1, 0, 0, 0))); + // Matrix4x4 trans = glm::translate(Matrix(1.0f), _translationKeys.Evalulate(time, Vector3(1.0f))); + // Matrix4x4 final = trans * rot; + // return final; + return Matrix4x4::Identity; } } // namespace Donut diff --git a/src/Render/SkinAnimation.h b/src/Render/SkinAnimation.h index 5632a2b..0e0e637 100644 --- a/src/Render/SkinAnimation.h +++ b/src/Render/SkinAnimation.h @@ -2,8 +2,10 @@ #pragma once -#include -#include +#include "Core/Math/Matrix4x4.h" +#include "Core/Math/Quaternion.h" +#include "Core/Math/Vector3.h" + #include #include #include @@ -31,27 +33,29 @@ class SkinAnimation T _value; }; - class TranslationKey: public ValueKey + class TranslationKey: public ValueKey { public: - TranslationKey(float time, const glm::vec3& value): + TranslationKey(float time, const Vector3& value): ValueKey(time, value) {} - virtual glm::vec3 Lerp(const glm::vec3& b, float time) override + virtual Vector3 Lerp(const Vector3& b, float time) override { - return glm::mix(_value, b, time); + return b; // TODO + // return glm::mix(_value, b, time); } }; - class RotationKey: public ValueKey + class RotationKey: public ValueKey { public: - RotationKey(float time, const glm::quat& value): + RotationKey(float time, const Quaternion& value): ValueKey(time, value) {} - virtual glm::quat Lerp(const glm::quat& b, float time) override + virtual Quaternion Lerp(const Quaternion& b, float time) override { - return glm::slerp(_value, b, time); + return b; // TODO + // return glm::slerp(_value, b, time); } }; @@ -59,8 +63,8 @@ class SkinAnimation class ValueKeyCurve { public: - void AddTranslationKey(float time, const glm::vec3& value) { _keyValues.push_back(std::make_unique(time, value)); } - void AddRotationKey(float time, const glm::quat& value) { _keyValues.push_back(std::make_unique(time, value)); } + void AddTranslationKey(float time, const Vector3& value) { _keyValues.push_back(std::make_unique(time, value)); } + void AddRotationKey(float time, const Quaternion& value) { _keyValues.push_back(std::make_unique(time, value)); } T Evalulate(float time, T defaultValue) { @@ -86,7 +90,7 @@ class SkinAnimation if (delta > 0.0f) { auto fraction = (time - prevPoint->GetTime()) / delta; - fraction = glm::clamp(fraction, 0.0f, 1.0f); + fraction = Math::Clamp(fraction, 0.0f, 1.0f); return prevPoint->Lerp(nextPoint->GetValue(), fraction); } @@ -141,21 +145,21 @@ class SkinAnimation Track(std::string name): _name(name) {} - glm::mat4 Evaluate(float time); + Matrix4x4 Evaluate(float time); - void AddTranslationKey(float time, const glm::vec3& value) { _translationKeys.AddTranslationKey(time, value); } - void AddRotationKey(float time, const glm::quat& value) { _rotationKeys.AddRotationKey(time, value); } + void AddTranslationKey(float time, const Vector3& value) { _translationKeys.AddTranslationKey(time, value); } + void AddRotationKey(float time, const Quaternion& value) { _rotationKeys.AddRotationKey(time, value); } private: std::string _name; - ValueKeyCurve _translationKeys; - ValueKeyCurve _rotationKeys; + ValueKeyCurve _translationKeys; + ValueKeyCurve _rotationKeys; }; SkinAnimation(std::string name, float length, int32_t frameCount, float frameRate): _name(name), _length(length), _frameCount(frameCount), _frameRate(frameRate) {} - glm::mat4 Evaluate(size_t trackIndex, float time); + Matrix4x4 Evaluate(size_t trackIndex, float time); void AddTrack(std::unique_ptr& track) { _tracks.push_back(std::move(track)); } size_t GetNumTracks() const { return _tracks.size(); } diff --git a/src/Render/SkinModel.cpp b/src/Render/SkinModel.cpp index c5caba7..1f4d5c6 100644 --- a/src/Render/SkinModel.cpp +++ b/src/Render/SkinModel.cpp @@ -32,7 +32,7 @@ void SkinModel::LoadPolySkin(const P3D::PolySkin& polySkin) for (uint32_t i = 0; i < primVerts.size(); i++) { - auto boneIndices = glm::ivec3(0); + auto boneIndices = Vector3Int::Zero; if (primHasBoneIndices) { @@ -42,14 +42,14 @@ void SkinModel::LoadPolySkin(const P3D::PolySkin& polySkin) auto i2 = (m >> 8) & 0xFF; auto i3 = m & 0xFF; - boneIndices = glm::ivec3( + boneIndices = Vector3Int( primMatrixPalette[i0], primMatrixPalette[i1], primMatrixPalette[i2]); } - const auto weight = primHasWeights ? primWeights[i] : glm::vec3(1, 0, 0); - const auto uv = glm::vec2(primUV[i].x, 1.0f - primUV[i].y); // turn that frown upside down :) + const auto weight = primHasWeights ? primWeights[i] : Vector3(1, 0, 0); + const auto uv = Vector2(primUV[i].X, 1.0f - primUV[i].Y); // turn that frown upside down :) vertices.emplace_back(primVerts[i], primNormals[i], uv, weight, boneIndices); } diff --git a/src/Render/SkinModel.h b/src/Render/SkinModel.h index c244198..543d2e8 100644 --- a/src/Render/SkinModel.h +++ b/src/Render/SkinModel.h @@ -2,13 +2,14 @@ #pragma once -#include -#include -#include -#include -#include -#include -#include +#include "Core/Math/Vector3Int.h" +#include "Render/OpenGL/IndexBuffer.h" +#include "Render/OpenGL/TextureBuffer.h" +#include "Render/OpenGL/VertexBinding.h" +#include "Render/OpenGL/VertexBuffer.h" +#include "Render/SkinAnimation.h" +#include "ResourceManager.h" + #include #include #include @@ -30,13 +31,13 @@ class SkinModel { struct Vertex { - glm::vec3 pos; - glm::vec3 normal; - glm::vec2 uv; - glm::vec3 boneWeights; - glm::ivec3 boneIndices; + Vector3 pos; + Vector3 normal; + Vector2 uv; + Vector3 boneWeights; + Vector3Int boneIndices; - Vertex(glm::vec3 pos, glm::vec3 normal, glm::vec2 uv, glm::vec3 boneWeights, glm::ivec3 boneIndices): + Vertex(Vector3 pos, Vector3 normal, Vector2 uv, Vector3 boneWeights, Vector3Int boneIndices): pos(pos), normal(normal), uv(uv), boneWeights(boneWeights), boneIndices(boneIndices) { } diff --git a/src/Render/SpriteBatch.cpp b/src/Render/SpriteBatch.cpp index 2e5d91f..d469634 100644 --- a/src/Render/SpriteBatch.cpp +++ b/src/Render/SpriteBatch.cpp @@ -2,11 +2,12 @@ #include "SpriteBatch.h" -#include -#include -#include -#include -#include +#include "Core/Math/Math.h" +#include "Render/Font.h" +#include "Render/OpenGL/ShaderProgram.h" +#include "Render/OpenGL/VertexBinding.h" +#include "Render/OpenGL/VertexBuffer.h" +#include "Render/Texture.h" namespace Donut { @@ -56,20 +57,20 @@ GL::ShaderProgram& SpriteBatch::GetShader() return *Shader; } -void SpriteBatch::DrawText(const Font* font, const std::string& text, const glm::vec2& position, const glm::vec4& colour) +void SpriteBatch::DrawText(const Font* font, const std::string& text, const Vector2& position, const Vector4& colour) { if (font == nullptr) return; Font::Glyph glyph; - glm::vec2 curPosition = position; + Vector2 curPosition = position; float fontHeight = font->GetHeight(); for (const char& c : text) { if (c == '\n') { - curPosition.x = position.x; - curPosition.y += fontHeight; + curPosition.X = position.X; + curPosition.Y += fontHeight; continue; } @@ -78,22 +79,22 @@ void SpriteBatch::DrawText(const Font* font, const std::string& text, const glm: Texture* glyphTexture = font->GetTexture(glyph.textureId); Draw(glyphTexture, - curPosition + glm::vec2(glyph.leftBearing), - glm::vec2(glyph.bottomLeftX, 1.0f - glyph.topRightY), - glm::vec2(glyph.topRightX, 1.0f - glyph.bottomLeftY), - glm::vec2(glyph.width, fontHeight), + curPosition + Vector2(glyph.leftBearing), + Vector2(glyph.bottomLeftX, 1.0f - glyph.topRightY), + Vector2(glyph.topRightX, 1.0f - glyph.bottomLeftY), + Vector2(glyph.width, fontHeight), colour); - curPosition += glm::vec2(glyph.advance, 0); + curPosition += Vector2(glyph.advance, 0); } } SpriteBatch::Sprite::Sprite( Texture* texture, - const glm::vec2& position, - const glm::vec2& size, + const Vector2& position, + const Vector2& size, float angle, - const glm::vec4& colour): + const Vector4& colour): _texture(texture), _position(position), _size(size), @@ -106,11 +107,11 @@ SpriteBatch::Sprite::Sprite( SpriteBatch::Sprite::Sprite( Texture* texture, - const glm::vec2& position, - const glm::vec2& size, - const glm::vec2& uv1, - const glm::vec2& uv2, - const glm::vec4& colour): + const Vector2& position, + const Vector2& size, + const Vector2& uv1, + const Vector2& uv2, + const Vector4& colour): _texture(texture), _position(position), _size(size), @@ -146,25 +147,25 @@ SpriteBatch::SpriteBatch(size_t maxSpriteCount): void SpriteBatch::Draw( Texture* texture, - const glm::vec2& position, + const Vector2& position, float angle, - const glm::vec4& colour) + const Vector4& colour) { - _spritesToDraw.push_back(Sprite(texture, position, texture->GetSize(), angle, colour)); + _spritesToDraw.push_back(Sprite(texture, position, Vector2(texture->GetSize()), angle, colour)); } void SpriteBatch::Draw( Texture* texture, - const glm::vec2& position, - const glm::vec2& size, - const glm::vec4& colour) + const Vector2& position, + const Vector2& size, + const Vector4& colour) { float u1 = 0.0f; float v1 = 0.0f; float u2 = 1.0f; float v2 = 1.0f; - glm::vec2 newPosition = position; - glm::vec2 newSize = size; + Vector2 newPosition = position; + Vector2 newSize = size; if (_clipping) { @@ -173,23 +174,23 @@ void SpriteBatch::Draw( return; } - float newleft = glm::clamp(position.x, (float)_clippingRect.x, (float)_clippingRect.z); - float newtop = glm::clamp(position.y, (float)_clippingRect.y, (float)_clippingRect.w); - float newright = glm::clamp(position.x + size.x, (float)_clippingRect.x, (float)_clippingRect.z); - float newbottom = glm::clamp(position.y + size.y, (float)_clippingRect.y, (float)_clippingRect.w); + float newleft = Math::Clamp(position.X, (float)_clippingRect.X, (float)_clippingRect.Z); + float newtop = Math::Clamp(position.Y, (float)_clippingRect.Y, (float)_clippingRect.W); + float newright = Math::Clamp(position.X + size.X, (float)_clippingRect.X, (float)_clippingRect.Z); + float newbottom = Math::Clamp(position.Y + size.Y, (float)_clippingRect.Y, (float)_clippingRect.W); - newPosition = glm::vec2(newleft, newtop); - newSize = glm::vec2(newright - newleft, newbottom - newtop); + newPosition = Vector2(newleft, newtop); + newSize = Vector2(newright - newleft, newbottom - newtop); - float difleft = newleft - position.x; - float diftop = newtop - position.y; - float difright = newright - (position.x + size.x); - float difbottom = newbottom - (position.y + size.y); + float difleft = newleft - position.X; + float diftop = newtop - position.Y; + float difright = newright - (position.X + size.X); + float difbottom = newbottom - (position.Y + size.Y); - difleft /= size.x; - diftop /= size.y; - difright /= size.x; - difbottom /= size.y; + difleft /= size.X; + diftop /= size.Y; + difright /= size.X; + difbottom /= size.Y; float uwidth = u2 - u1; float vheight = v2 - v1; @@ -200,33 +201,33 @@ void SpriteBatch::Draw( v2 = v2 + (vheight * difbottom); } - _spritesToDraw.push_back(Sprite(texture, newPosition, newSize, glm::vec2(u1, v1), glm::vec2(u2, v2), colour)); + _spritesToDraw.push_back(Sprite(texture, newPosition, newSize, Vector2(u1, v1), Vector2(u2, v2), colour)); } void SpriteBatch::Draw( Texture* texture, - const glm::vec2& position, - const glm::vec2& size, + const Vector2& position, + const Vector2& size, float angle, - const glm::vec4& colour) + const Vector4& colour) { _spritesToDraw.push_back(Sprite(texture, position, size, angle, colour)); } void SpriteBatch::Draw( Texture* texture, - const glm::vec2& position, - const glm::vec2& uv1, - const glm::vec2& uv2, - const glm::vec2& size, - const glm::vec4& colour) + const Vector2& position, + const Vector2& uv1, + const Vector2& uv2, + const Vector2& size, + const Vector4& colour) { - float u1 = uv1.x; - float v1 = uv1.y; - float u2 = uv2.x; - float v2 = uv2.y; - glm::vec2 newPosition = position; - glm::vec2 newSize = size; + float u1 = uv1.X; + float v1 = uv1.Y; + float u2 = uv2.X; + float v2 = uv2.Y; + Vector2 newPosition = position; + Vector2 newSize = size; if (_clipping) { @@ -235,23 +236,23 @@ void SpriteBatch::Draw( return; } - float newleft = glm::clamp(position.x, (float)_clippingRect.x, (float)_clippingRect.z); - float newtop = glm::clamp(position.y, (float)_clippingRect.y, (float)_clippingRect.w); - float newright = glm::clamp(position.x + size.x, (float)_clippingRect.x, (float)_clippingRect.z); - float newbottom = glm::clamp(position.y + size.y, (float)_clippingRect.y, (float)_clippingRect.w); + float newleft = Math::Clamp(position.X, (float)_clippingRect.X, (float)_clippingRect.Z); + float newtop = Math::Clamp(position.Y, (float)_clippingRect.Y, (float)_clippingRect.W); + float newright = Math::Clamp(position.X + size.X, (float)_clippingRect.X, (float)_clippingRect.Z); + float newbottom = Math::Clamp(position.Y + size.Y, (float)_clippingRect.Y, (float)_clippingRect.W); - newPosition = glm::vec2(newleft, newtop); - newSize = glm::vec2(newright - newleft, newbottom - newtop); + newPosition = Vector2(newleft, newtop); + newSize = Vector2(newright - newleft, newbottom - newtop); - float difleft = newleft - position.x; - float diftop = newtop - position.y; - float difright = newright - (position.x + size.x); - float difbottom = newbottom - (position.y + size.y); + float difleft = newleft - position.X; + float diftop = newtop - position.Y; + float difright = newright - (position.X + size.X); + float difbottom = newbottom - (position.Y + size.Y); - difleft /= size.x; - diftop /= size.y; - difright /= size.x; - difbottom /= size.y; + difleft /= size.X; + diftop /= size.Y; + difright /= size.X; + difbottom /= size.Y; float uwidth = u2 - u1; float vheight = v2 - v1; @@ -262,15 +263,15 @@ void SpriteBatch::Draw( v2 = v2 + (vheight * difbottom); } - _spritesToDraw.push_back(Sprite(texture, newPosition, newSize, glm::vec2(u1, v1), glm::vec2(u2, v2), colour)); + _spritesToDraw.push_back(Sprite(texture, newPosition, newSize, Vector2(u1, v1), Vector2(u2, v2), colour)); } SpriteBatch::NineSliceProperties::NineSliceProperties( - const glm::vec2& topLeftSlicePx, - const glm::vec2& bottomRightSlicePx, - const glm::vec2& glyphSize, - const glm::vec2& drawPosition, - const glm::vec2& drawSize): + const Vector2& topLeftSlicePx, + const Vector2& bottomRightSlicePx, + const Vector2& glyphSize, + const Vector2& drawPosition, + const Vector2& drawSize): _topLeftSlicePx(topLeftSlicePx), _bottomRightSlicePx(bottomRightSlicePx), _topLeftSlice(topLeftSlicePx / glyphSize), @@ -281,123 +282,123 @@ SpriteBatch::NineSliceProperties::NineSliceProperties( { } -void SpriteBatch::DrawSlice(Texture* texture, const SpriteBatch::Slice& slice, const glm::vec4& colour) +void SpriteBatch::DrawSlice(Texture* texture, const SpriteBatch::Slice& slice, const Vector4& colour) { Draw(texture, slice._drawPosition, slice._uv1, slice._uv2, slice._drawSize, colour); } void SpriteBatch::NineSliceProperties::GetTopLeftSlice(Slice& slice) const { - slice._uv1.x = 0.0f; - slice._uv1.y = 1.0f; - slice._uv2.x = _topLeftSlice.x; - slice._uv2.y = 1.0f - _topLeftSlice.y; + slice._uv1.X = 0.0f; + slice._uv1.Y = 1.0f; + slice._uv2.X = _topLeftSlice.X; + slice._uv2.Y = 1.0f - _topLeftSlice.Y; slice._drawPosition = _drawPosition; slice._drawSize = _topLeftSlicePx; } void SpriteBatch::NineSliceProperties::GetTopRightSlice(Slice& slice) const { - slice._uv1.x = 1.0f - _bottomRightSlice.x; - slice._uv1.y = 1.0f; - slice._uv2.x = 1.0f; - slice._uv2.y = 1.0f - _topLeftSlice.y; - slice._drawPosition.x = _drawPosition.x + _drawSize.x - _bottomRightSlicePx.x; - slice._drawPosition.y = _drawPosition.y; - slice._drawSize.x = _bottomRightSlicePx.x; - slice._drawSize.y = _topLeftSlicePx.y; + slice._uv1.X = 1.0f - _bottomRightSlice.X; + slice._uv1.Y = 1.0f; + slice._uv2.X = 1.0f; + slice._uv2.Y = 1.0f - _topLeftSlice.Y; + slice._drawPosition.X = _drawPosition.X + _drawSize.X - _bottomRightSlicePx.X; + slice._drawPosition.Y = _drawPosition.Y; + slice._drawSize.X = _bottomRightSlicePx.X; + slice._drawSize.Y = _topLeftSlicePx.Y; } void SpriteBatch::NineSliceProperties::GetBottomLeftSlice(Slice& slice) const { - slice._uv1.x = 0.0f; - slice._uv1.y = _bottomRightSlice.y; - slice._uv2.x = _topLeftSlice.x; - slice._uv2.y = 0.0f; - slice._drawPosition.x = _drawPosition.x; - slice._drawPosition.y = _drawPosition.y + _drawSize.y - _bottomRightSlicePx.y; - slice._drawSize.x = _topLeftSlicePx.x; - slice._drawSize.y = _bottomRightSlicePx.y; + slice._uv1.X = 0.0f; + slice._uv1.Y = _bottomRightSlice.Y; + slice._uv2.X = _topLeftSlice.X; + slice._uv2.Y = 0.0f; + slice._drawPosition.X = _drawPosition.X; + slice._drawPosition.Y = _drawPosition.Y + _drawSize.Y - _bottomRightSlicePx.Y; + slice._drawSize.X = _topLeftSlicePx.X; + slice._drawSize.Y = _bottomRightSlicePx.Y; } void SpriteBatch::NineSliceProperties::GetBottomRightSlice(Slice& slice) const { - slice._uv1.x = 1.0f - _bottomRightSlice.x; - slice._uv1.y = _bottomRightSlice.y; - slice._uv2.x = 1.0f; - slice._uv2.y = 0.0f; + slice._uv1.X = 1.0f - _bottomRightSlice.X; + slice._uv1.Y = _bottomRightSlice.Y; + slice._uv2.X = 1.0f; + slice._uv2.Y = 0.0f; slice._drawPosition = _drawPosition + _drawSize - _bottomRightSlicePx; slice._drawSize = _bottomRightSlicePx; } void SpriteBatch::NineSliceProperties::GetTopMidSlice(Slice& slice) const { - slice._uv1.x = _topLeftSlice.x; - slice._uv1.y = 1.0f; - slice._uv2.x = 1.0f - _bottomRightSlice.x; - slice._uv2.y = 1.0f - _topLeftSlice.y; - slice._drawPosition.x = _drawPosition.x + _topLeftSlicePx.x; - slice._drawPosition.y = _drawPosition.y; - slice._drawSize.x = _drawSize.x - _topLeftSlicePx.x - _bottomRightSlicePx.x; - slice._drawSize.y = _topLeftSlicePx.y; + slice._uv1.X = _topLeftSlice.X; + slice._uv1.Y = 1.0f; + slice._uv2.X = 1.0f - _bottomRightSlice.X; + slice._uv2.Y = 1.0f - _topLeftSlice.Y; + slice._drawPosition.X = _drawPosition.X + _topLeftSlicePx.X; + slice._drawPosition.Y = _drawPosition.Y; + slice._drawSize.X = _drawSize.X - _topLeftSlicePx.X - _bottomRightSlicePx.X; + slice._drawSize.Y = _topLeftSlicePx.Y; } void SpriteBatch::NineSliceProperties::GetBottomMidSlice(Slice& slice) const { - slice._uv1.x = _topLeftSlice.x; - slice._uv1.y = _bottomRightSlice.y; - slice._uv2.x = 1.0f - _bottomRightSlice.x; - slice._uv2.y = 0.0f; - slice._drawPosition.x = _drawPosition.x + _topLeftSlicePx.x; - slice._drawPosition.y = _drawPosition.y + _drawSize.y - _bottomRightSlicePx.y; - slice._drawSize.x = _drawSize.x - _topLeftSlicePx.x - _bottomRightSlicePx.x; - slice._drawSize.y = _bottomRightSlicePx.y; + slice._uv1.X = _topLeftSlice.X; + slice._uv1.Y = _bottomRightSlice.Y; + slice._uv2.X = 1.0f - _bottomRightSlice.X; + slice._uv2.Y = 0.0f; + slice._drawPosition.X = _drawPosition.X + _topLeftSlicePx.X; + slice._drawPosition.Y = _drawPosition.Y + _drawSize.Y - _bottomRightSlicePx.Y; + slice._drawSize.X = _drawSize.X - _topLeftSlicePx.X - _bottomRightSlicePx.X; + slice._drawSize.Y = _bottomRightSlicePx.Y; } void SpriteBatch::NineSliceProperties::GetLeftMidSlice(Slice& slice) const { - slice._uv1.x = 0.0f; - slice._uv1.y = 1.0f - _topLeftSlice.y; - slice._uv2.x = _topLeftSlice.x; - slice._uv2.y = _bottomRightSlice.y; - slice._drawPosition.x = _drawPosition.x; - slice._drawPosition.y = _drawPosition.y + _topLeftSlicePx.y; - slice._drawSize.x = _topLeftSlicePx.x; - slice._drawSize.y = _drawSize.y - _bottomRightSlicePx.y - _topLeftSlicePx.y; + slice._uv1.X = 0.0f; + slice._uv1.Y = 1.0f - _topLeftSlice.Y; + slice._uv2.X = _topLeftSlice.X; + slice._uv2.Y = _bottomRightSlice.Y; + slice._drawPosition.X = _drawPosition.X; + slice._drawPosition.Y = _drawPosition.Y + _topLeftSlicePx.Y; + slice._drawSize.X = _topLeftSlicePx.X; + slice._drawSize.Y = _drawSize.Y - _bottomRightSlicePx.Y - _topLeftSlicePx.Y; } void SpriteBatch::NineSliceProperties::GetRightMidSlice(Slice& slice) const { - slice._uv1.x = 1.0f - _bottomRightSlice.x; - slice._uv1.y = 1.0f - _topLeftSlice.y; - slice._uv2.x = 1.0f; - slice._uv2.y = _bottomRightSlice.y; - slice._drawPosition.x = _drawPosition.x + _drawSize.x - _bottomRightSlicePx.x; - slice._drawPosition.y = _drawPosition.y + _topLeftSlicePx.y; - slice._drawSize.x = _bottomRightSlicePx.x; - slice._drawSize.y = _drawSize.y - _bottomRightSlicePx.y - _topLeftSlicePx.y; + slice._uv1.X = 1.0f - _bottomRightSlice.X; + slice._uv1.Y = 1.0f - _topLeftSlice.Y; + slice._uv2.X = 1.0f; + slice._uv2.Y = _bottomRightSlice.Y; + slice._drawPosition.X = _drawPosition.X + _drawSize.X - _bottomRightSlicePx.X; + slice._drawPosition.Y = _drawPosition.Y + _topLeftSlicePx.Y; + slice._drawSize.X = _bottomRightSlicePx.X; + slice._drawSize.Y = _drawSize.Y - _bottomRightSlicePx.Y - _topLeftSlicePx.Y; } void SpriteBatch::NineSliceProperties::GetMidSlice(Slice& slice) const { - slice._uv1.x = _topLeftSlice.x; - slice._uv1.y = 1.0f - _topLeftSlice.y; - slice._uv2.x = 1.0f - _bottomRightSlice.x; - slice._uv2.y = _bottomRightSlice.y; + slice._uv1.X = _topLeftSlice.X; + slice._uv1.Y = 1.0f - _topLeftSlice.Y; + slice._uv2.X = 1.0f - _bottomRightSlice.X; + slice._uv2.Y = _bottomRightSlice.Y; slice._drawPosition = _drawPosition + _topLeftSlicePx; slice._drawSize = _drawSize - _topLeftSlicePx - _bottomRightSlicePx; } void SpriteBatch::Draw9Slice( Texture* texture, - const glm::vec2& position, - const glm::vec2& size, - const glm::vec4& margin, - const glm::vec4& colour, + const Vector2& position, + const Vector2& size, + const Vector4& margin, + const Vector4& colour, bool drawCenter) { Slice slice; - const NineSliceProperties properties(glm::vec2(margin.x, margin.y), glm::vec2(margin.z, margin.w), texture->GetSize(), position, size); + const NineSliceProperties properties(Vector2(margin.X, margin.Y), Vector2(margin.Z, margin.W), texture->GetSize(), position, size); properties.GetTopLeftSlice(slice); DrawSlice(texture, slice, colour); @@ -430,12 +431,12 @@ void SpriteBatch::Draw9Slice( } } -void SpriteBatch::TransformUV(glm::vec2& uv, const glm::vec2& glyphSize, const glm::vec2& offset, const glm::vec2& sheetSize) +void SpriteBatch::TransformUV(Vector2& uv, const Vector2& glyphSize, const Vector2& offset, const Vector2& sheetSize) { - uv = glm::vec2(offset.x, sheetSize.y - offset.y - glyphSize.y) / sheetSize + uv * glyphSize / sheetSize; + uv = Vector2(offset.X, sheetSize.Y - offset.Y - glyphSize.Y) / sheetSize + uv * glyphSize / sheetSize; } -void SpriteBatch::TransformUVs(Slice& slice, const glm::vec2& glyphSize, const glm::vec2& offset, const glm::vec2& sheetSize) +void SpriteBatch::TransformUVs(Slice& slice, const Vector2& glyphSize, const Vector2& offset, const Vector2& sheetSize) { TransformUV(slice._uv1, glyphSize, offset, sheetSize); TransformUV(slice._uv2, glyphSize, offset, sheetSize); @@ -443,17 +444,17 @@ void SpriteBatch::TransformUVs(Slice& slice, const glm::vec2& glyphSize, const g void SpriteBatch::Draw9Slice( Texture* texture, - const glm::vec2& position, - const glm::vec2& size, - const glm::vec2& glyphPosition, - const glm::vec2& glyphSize, - const glm::vec4& margin, - const glm::vec4& colour, + const Vector2& position, + const Vector2& size, + const Vector2& glyphPosition, + const Vector2& glyphSize, + const Vector4& margin, + const Vector4& colour, bool drawCenter) { Slice slice; - const glm::vec2& sheetSize = texture->GetSize(); - const NineSliceProperties properties(glm::vec2(margin.x, margin.y), glm::vec2(margin.z, margin.w), glyphSize, position, size); + const Vector2& sheetSize = Vector2(texture->GetWidth(), texture->GetHeight()); + const NineSliceProperties properties(Vector2(margin.X, margin.Y), Vector2(margin.Z, margin.W), glyphSize, position, size); properties.GetTopLeftSlice(slice); TransformUVs(slice, glyphSize, glyphPosition, sheetSize); @@ -495,7 +496,7 @@ void SpriteBatch::Draw9Slice( } } -void SpriteBatch::Flush(const glm::mat4& proj, float scale) +void SpriteBatch::Flush(const Matrix4x4& proj, float scale) { if (_spritesToDraw.empty()) { @@ -546,34 +547,34 @@ void SpriteBatch::Flush(const glm::mat4& proj, float scale) for (size_t j = 0; j < faceVertCount; ++j) { - buffer[4 + vertSize * j] = sprite._colour.x; - buffer[5 + vertSize * j] = sprite._colour.y; - buffer[6 + vertSize * j] = sprite._colour.z; - buffer[7 + vertSize * j] = sprite._colour.w; + buffer[4 + vertSize * j] = sprite._colour.X; + buffer[5 + vertSize * j] = sprite._colour.Y; + buffer[6 + vertSize * j] = sprite._colour.Z; + buffer[7 + vertSize * j] = sprite._colour.W; } - glm::vec2 vertexPositions[4] = { - glm::vec2(-(sprite._size.x / 2), -(sprite._size.y / 2)), - glm::vec2(-(sprite._size.x / 2), sprite._size.y / 2), - glm::vec2(sprite._size.x / 2, sprite._size.y / 2), - glm::vec2(sprite._size.x / 2, -(sprite._size.y / 2)), + Vector2 vertexPositions[4] = { + Vector2(-(sprite._size.X / 2), -(sprite._size.Y / 2)), + Vector2(-(sprite._size.X / 2), sprite._size.Y / 2), + Vector2(sprite._size.X / 2, sprite._size.Y / 2), + Vector2(sprite._size.X / 2, -(sprite._size.Y / 2)), }; - glm::vec2 vertexTextureCoords[4] = { - glm::vec2(sprite._uv1.x, sprite._uv1.y), - glm::vec2(sprite._uv1.x, sprite._uv2.y), - glm::vec2(sprite._uv2.x, sprite._uv2.y), - glm::vec2(sprite._uv2.x, sprite._uv1.y), + Vector2 vertexTextureCoords[4] = { + Vector2(sprite._uv1.X, sprite._uv1.Y), + Vector2(sprite._uv1.X, sprite._uv2.Y), + Vector2(sprite._uv2.X, sprite._uv2.Y), + Vector2(sprite._uv2.X, sprite._uv1.Y), }; for (size_t j = 0; j < 4; ++j) { - float cosAngle = glm::cos(glm::radians(sprite._angle)); - float sinAngle = glm::sin(glm::radians(sprite._angle)); + float cosAngle = Math::Cos(Math::DegreesToRadians(sprite._angle)); + float sinAngle = Math::Sin(Math::DegreesToRadians(sprite._angle)); - glm::vec2 p = vertexPositions[j]; - vertexPositions[j].x = p.x * cosAngle - p.y * sinAngle; - vertexPositions[j].y = p.y * cosAngle + p.x * sinAngle; + Vector2 p = vertexPositions[j]; + vertexPositions[j].X = p.X * cosAngle - p.Y * sinAngle; + vertexPositions[j].Y = p.Y * cosAngle + p.X * sinAngle; vertexPositions[j] += sprite._position + (sprite._size * 0.5f); } @@ -582,10 +583,10 @@ void SpriteBatch::Flush(const glm::mat4& proj, float scale) size_t bufferIndex = j * vertSize; size_t vertexIndex = faceIndices[j]; - buffer[bufferIndex] = vertexPositions[vertexIndex].x * scale; - buffer[bufferIndex + 1] = vertexPositions[vertexIndex].y * scale; - buffer[bufferIndex + 2] = vertexTextureCoords[vertexIndex].x; - buffer[bufferIndex + 3] = vertexTextureCoords[vertexIndex].y; + buffer[bufferIndex] = vertexPositions[vertexIndex].X * scale; + buffer[bufferIndex + 1] = vertexPositions[vertexIndex].Y * scale; + buffer[bufferIndex + 2] = vertexTextureCoords[vertexIndex].X; + buffer[bufferIndex + 3] = vertexTextureCoords[vertexIndex].Y; } } @@ -606,11 +607,11 @@ void SpriteBatch::Flush(const glm::mat4& proj, float scale) _spritesToDraw.clear(); } -bool SpriteBatch::IsSpriteInsideClippingRect(const glm::vec2& position, const glm::vec2& size) +bool SpriteBatch::IsSpriteInsideClippingRect(const Vector2& position, const Vector2& size) { - return !((position.x >= _clippingRect.z) || - (position.x + size.x <= _clippingRect.x) || - (position.y >= _clippingRect.w) || - (position.y + size.y <= _clippingRect.y)); + return !((position.X >= _clippingRect.Z) || + (position.X + size.X <= _clippingRect.X) || + (position.Y >= _clippingRect.W) || + (position.Y + size.Y <= _clippingRect.Y)); } } // namespace Donut diff --git a/src/Render/SpriteBatch.h b/src/Render/SpriteBatch.h index 9f464ed..71c9847 100644 --- a/src/Render/SpriteBatch.h +++ b/src/Render/SpriteBatch.h @@ -2,7 +2,10 @@ #pragma once -#include +#include "Core/Math/Fwd.h" +#include "Core/Math/Vector2.h" +#include "Core/Math/Vector4.h" + #include #include #include @@ -26,17 +29,17 @@ class SpriteBatch public: SpriteBatch(size_t = 1000); - void Flush(const glm::mat4&, float = 1.0f); - void DrawText(const class Font*, const std::string&, const glm::vec2&, const glm::vec4&); - void Draw(Texture*, const glm::vec2&, float, const glm::vec4&); - void Draw(Texture*, const glm::vec2&, const glm::vec2&, const glm::vec4&); - void Draw(Texture*, const glm::vec2&, const glm::vec2&, float, const glm::vec4&); - void Draw(Texture*, const glm::vec2&, const glm::vec2&, const glm::vec2&, const glm::vec2&, const glm::vec4&); - void Draw9Slice(Texture*, const glm::vec2&, const glm::vec2&, const glm::vec4&, const glm::vec4&, bool = true); - void Draw9Slice(Texture*, const glm::vec2&, const glm::vec2&, const glm::vec2&, const glm::vec2&, const glm::vec4&, const glm::vec4&, bool = true); + void Flush(const Matrix4x4&, float = 1.0f); + void DrawText(const class Font*, const std::string&, const Vector2&, const Vector4&); + void Draw(Texture*, const Vector2&, float, const Vector4&); + void Draw(Texture*, const Vector2&, const Vector2&, const Vector4&); + void Draw(Texture*, const Vector2&, const Vector2&, float, const Vector4&); + void Draw(Texture*, const Vector2&, const Vector2&, const Vector2&, const Vector2&, const Vector4&); + void Draw9Slice(Texture*, const Vector2&, const Vector2&, const Vector4&, const Vector4&, bool = true); + void Draw9Slice(Texture*, const Vector2&, const Vector2&, const Vector2&, const Vector2&, const Vector4&, const Vector4&, bool = true); void EnableClipping(bool clipping) { _clipping = clipping; } - void SetClippingRect(const glm::vec4& clippingRect) { _clippingRect = clippingRect; } + void SetClippingRect(const Vector4& clippingRect) { _clippingRect = clippingRect; } size_t GetDrawCallCount() const { return _drawCallCount; } @@ -45,34 +48,34 @@ class SpriteBatch private: struct Sprite { - Sprite(Texture*, const glm::vec2&, const glm::vec2&, float, const glm::vec4&); - Sprite(Texture*, const glm::vec2&, const glm::vec2&, const glm::vec2&, const glm::vec2&, const glm::vec4&); + Sprite(Texture*, const Vector2&, const Vector2&, float, const Vector4&); + Sprite(Texture*, const Vector2&, const Vector2&, const Vector2&, const Vector2&, const Vector4&); Texture* _texture; - glm::vec2 _position; - glm::vec2 _size; - glm::vec2 _uv1; - glm::vec2 _uv2; - glm::vec4 _colour; + Vector2 _position; + Vector2 _size; + Vector2 _uv1; + Vector2 _uv2; + Vector4 _colour; float _angle; }; struct Slice { - glm::vec2 _uv1; - glm::vec2 _uv2; - glm::vec2 _drawPosition; - glm::vec2 _drawSize; + Vector2 _uv1; + Vector2 _uv2; + Vector2 _drawPosition; + Vector2 _drawSize; }; struct NineSliceProperties { NineSliceProperties( - const glm::vec2&, - const glm::vec2&, - const glm::vec2&, - const glm::vec2&, - const glm::vec2&); + const Vector2&, + const Vector2&, + const Vector2&, + const Vector2&, + const Vector2&); void GetTopLeftSlice(Slice&) const; void GetTopRightSlice(Slice&) const; @@ -84,23 +87,23 @@ class SpriteBatch void GetRightMidSlice(Slice&) const; void GetMidSlice(Slice&) const; - const glm::vec2 _glyphSize; - const glm::vec2 _topLeftSlicePx; - const glm::vec2 _bottomRightSlicePx; - const glm::vec2 _topLeftSlice; - const glm::vec2 _bottomRightSlice; - const glm::vec2 _drawPosition; - const glm::vec2 _drawSize; + const Vector2 _glyphSize; + const Vector2 _topLeftSlicePx; + const Vector2 _bottomRightSlicePx; + const Vector2 _topLeftSlice; + const Vector2 _bottomRightSlice; + const Vector2 _drawPosition; + const Vector2 _drawSize; }; - void DrawSlice(Texture*, const Slice&, const glm::vec4&); - static void TransformUV(glm::vec2&, const glm::vec2&, const glm::vec2&, const glm::vec2&); - static void TransformUVs(Slice&, const glm::vec2&, const glm::vec2&, const glm::vec2&); - bool IsSpriteInsideClippingRect(const glm::vec2&, const glm::vec2&); + void DrawSlice(Texture*, const Slice&, const Vector4&); + static void TransformUV(Vector2&, const Vector2&, const Vector2&, const Vector2&); + static void TransformUVs(Slice&, const Vector2&, const Vector2&, const Vector2&); + bool IsSpriteInsideClippingRect(const Vector2&, const Vector2&); std::vector _spritesToDraw; bool _clipping; - glm::vec4 _clippingRect; + Vector4 _clippingRect; size_t _drawCallCount; size_t _maxSpriteCount; std::vector _vertexData; diff --git a/src/Render/Texture.h b/src/Render/Texture.h index 6f34edc..2aee993 100644 --- a/src/Render/Texture.h +++ b/src/Render/Texture.h @@ -1,8 +1,8 @@ #pragma once +#include "Core/Math/Vector2Int.h" #include "OpenGL/glad/glad.h" -#include #include #include @@ -28,7 +28,7 @@ class Texture std::size_t GetWidth() const { return _width; } std::size_t GetHeight() const { return _height; } - glm::ivec2 GetSize() const { return glm::ivec2(_width, _height); } + Vector2Int GetSize() const { return Vector2Int(_width, _height); } // bool HasAlpha() const; GLuint GetOpenGLHandle() const { return _glTexture; } diff --git a/src/Render/WorldSphere.cpp b/src/Render/WorldSphere.cpp index 02463c2..2531958 100644 --- a/src/Render/WorldSphere.cpp +++ b/src/Render/WorldSphere.cpp @@ -1,10 +1,11 @@ // Copyright 2019 the donut authors. See AUTHORS.md -#include -#include -#include -#include -#include +#include "WorldSphere.h" + +#include "Game.h" +#include "Render/LineRenderer.h" +#include "Render/SkinAnimation.h" +#include "Skeleton.h" namespace Donut { @@ -58,8 +59,8 @@ WorldSphere::WorldSphere(const P3D::WorldSphere& worldSphere): auto track = std::make_unique(joint.name); const auto& jointRestPose = joint.rest; - const auto& jointTranslation = jointRestPose[3]; - const auto& jointRotation = glm::quat_cast(jointRestPose); + const auto& jointTranslation = jointRestPose.Translation(); + const auto& jointRotation = jointRestPose.Quat(); if (groupNameIndex.find(joint.name) == groupNameIndex.end()) { @@ -82,7 +83,7 @@ WorldSphere::WorldSphere(const P3D::WorldSphere& worldSphere): for (std::size_t i = 0; i < vector2Channel->GetNumFrames(); ++i) { - track->AddTranslationKey(frames[i], constants + glm::vec3(values[i].x, 0.0f, values[i].y)); + track->AddTranslationKey(frames[i], constants + Vector3(values[i].X, 0.0f, values[i].Y)); } } else if (vector3Channel) @@ -107,8 +108,7 @@ WorldSphere::WorldSphere(const P3D::WorldSphere& worldSphere): for (std::size_t i = 0; i < quaternionChannel->GetNumFrames(); ++i) { - auto q = values[i]; - track->AddRotationKey(frames[i], glm::quat(q.x, q.y, q.z, q.w)); + track->AddRotationKey(frames[i], values[i]); } } else if (compressedQuaternionChannel) @@ -124,7 +124,7 @@ WorldSphere::WorldSphere(const P3D::WorldSphere& worldSphere): float x = (int16_t)((value >> 16) & 0xFFFF) / (float)0x7FFF; float w = (int16_t)(value & 0xFFFF) / (float)0x7FFF; - track->AddRotationKey(frames[i], glm::quat(w, x, y, z)); + track->AddRotationKey(frames[i], Quaternion(w, x, y, z)); } } else @@ -151,7 +151,7 @@ WorldSphere::WorldSphere(const P3D::WorldSphere& worldSphere): // Lens Flare } -void WorldSphere::Draw(GL::ShaderProgram& shader, const glm::mat4& viewProj, bool opaque) const +void WorldSphere::Draw(GL::ShaderProgram& shader, const Matrix4x4& viewProj, bool opaque) const { for (auto const& prop : _props) { @@ -170,7 +170,7 @@ void WorldSphere::Update(double deltatime) if (_animation != nullptr) _skeleton->UpdatePose(*_animation, _animTime); - //Game::GetInstance().GetLineRenderer().DrawSkeleton(glm::vec3(0.0, 0.0, 0.0), *_skeleton); + //Game::GetInstance().GetLineRenderer().DrawSkeleton(Vector3(0.0, 0.0, 0.0), *_skeleton); } } // namespace Donut diff --git a/src/Render/WorldSphere.h b/src/Render/WorldSphere.h index 2c7f972..f35a142 100644 --- a/src/Render/WorldSphere.h +++ b/src/Render/WorldSphere.h @@ -31,7 +31,7 @@ class WorldSphere public: WorldSphere(const P3D::WorldSphere&); - void Draw(GL::ShaderProgram&, const glm::mat4& viewProj, bool opaque) const; + void Draw(GL::ShaderProgram&, const Matrix4x4& viewProj, bool opaque) const; void Update(double deltatime); private: diff --git a/src/Scripting/Commands.h b/src/Scripting/Commands.h index 88ccfd4..cacc8d0 100644 --- a/src/Scripting/Commands.h +++ b/src/Scripting/Commands.h @@ -2,14 +2,14 @@ #pragma once +#include "Core/File.h" #include "GameCommands.h" -#include +#include + #include #include #include -#include -#include #include #include #include diff --git a/src/Skeleton.cpp b/src/Skeleton.cpp index 731106d..0debcf6 100644 --- a/src/Skeleton.cpp +++ b/src/Skeleton.cpp @@ -26,13 +26,13 @@ Skeleton::Skeleton(const P3D::Skeleton& skeleton): // inverse those globals afterwards for (auto i = 0; i < _joints.size(); i++) - _joints[i].restGlobalInverse = glm::inverse(_joints[i].restGlobalInverse); + _joints[i].restGlobalInverse = _joints[i].restGlobalInverse.Inverse(); } void Skeleton::ResetPose() { for (auto& joint : _joints) - joint.pose = glm::mat4(1.0f); + joint.pose = Matrix4x4::Identity; updateJoints(); } @@ -41,7 +41,7 @@ void Skeleton::UpdatePose(SkinAnimation& animation, double time) { time *= animation.GetFrameRate(); - _joints[0].pose = glm::mat4(1.0f); + _joints[0].pose = Matrix4x4::Identity; for (auto i = 0; i < _joints.size(); i++) _joints[i].pose = _joints[_joints[i].parent].pose * animation.Evaluate(i, static_cast(time)); diff --git a/src/Skeleton.h b/src/Skeleton.h index 876228f..86d6035 100644 --- a/src/Skeleton.h +++ b/src/Skeleton.h @@ -2,8 +2,8 @@ #pragma once -#include -#include +#include "Core/Math/Matrix4x4.h" + #include #include #include @@ -25,14 +25,14 @@ class Skeleton { std::string name; int parent; - glm::mat4 rest; - glm::mat4 restGlobalInverse; - glm::mat4 pose; - glm::mat4 finalGlobal; + Matrix4x4 rest; + Matrix4x4 restGlobalInverse; + Matrix4x4 pose; + Matrix4x4 finalGlobal; - Joint(std::string pName, int pParent, glm::mat4 pRest): + Joint(std::string pName, int pParent, Matrix4x4 pRest): name(std::move(pName)), parent(pParent), rest(pRest), - restGlobalInverse(glm::mat4(1.0f)), pose(glm::mat4(1.0f)), finalGlobal(glm::mat4(1.0f)) {} + restGlobalInverse(Matrix4x4::Identity), pose(Matrix4x4::Identity), finalGlobal(Matrix4x4::Identity) {} }; public: