From 03fbcc5b5e18dbf62c48849fc8d02cd250aa744e Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20Chilliet?= Date: Mon, 2 Jan 2017 17:15:26 +0100 Subject: [PATCH] Removed lots of unused code from Math folder, renamed Quaternion file to XYZ --- CMakeLists.txt | 5 +- Source/Animation/Animation.hpp | 2 +- Source/Animation/Joint.hpp | 2 +- Source/Animation/Skeleton.hpp | 2 +- Source/Audio/Sounds.hpp | 2 +- Source/Audio/openal_wrapper.cpp | 2 +- Source/Audio/openal_wrapper.hpp | 2 +- Source/Environment/Lights.hpp | 2 +- Source/Environment/Skybox.hpp | 4 +- Source/Environment/Terrain.hpp | 2 +- Source/Globals.cpp | 2 +- Source/Graphic/Decal.hpp | 2 +- Source/Graphic/Models.hpp | 2 +- Source/Graphic/Sprite.hpp | 2 +- Source/Graphic/Text.hpp | 2 +- Source/Level/Campaign.hpp | 2 +- Source/Level/Dialog.hpp | 2 +- Source/Level/Hotspot.hpp | 2 +- Source/Math/PhysicsMath.hpp | 763 ----------------------- Source/Math/Quaternions.cpp | 534 ---------------- Source/Math/XYZ.cpp | 247 ++++++++ Source/Math/{Quaternions.hpp => XYZ.hpp} | 40 -- Source/Objects/Object.hpp | 2 +- Source/Objects/Person.hpp | 2 +- Source/Objects/Weapons.hpp | 2 +- 25 files changed, 270 insertions(+), 1361 deletions(-) delete mode 100644 Source/Math/PhysicsMath.hpp delete mode 100644 Source/Math/Quaternions.cpp create mode 100644 Source/Math/XYZ.cpp rename Source/Math/{Quaternions.hpp => XYZ.hpp} (91%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 62d5bd9..d798d41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,7 +65,7 @@ set(LUGARU_SRCS ${SRCDIR}/Level/Dialog.cpp ${SRCDIR}/Level/Hotspot.cpp ${SRCDIR}/Math/Frustum.cpp - ${SRCDIR}/Math/Quaternions.cpp + ${SRCDIR}/Math/XYZ.cpp ${SRCDIR}/Menu/Menu.cpp ${SRCDIR}/Objects/Object.cpp ${SRCDIR}/Objects/Person.cpp @@ -109,8 +109,7 @@ set(LUGARU_H ${SRCDIR}/Level/Dialog.hpp ${SRCDIR}/Level/Hotspot.hpp ${SRCDIR}/Math/Frustum.hpp - ${SRCDIR}/Math/PhysicsMath.hpp - ${SRCDIR}/Math/Quaternions.hpp + ${SRCDIR}/Math/XYZ.hpp ${SRCDIR}/Math/Random.hpp ${SRCDIR}/Menu/Menu.hpp ${SRCDIR}/Objects/Object.hpp diff --git a/Source/Animation/Animation.hpp b/Source/Animation/Animation.hpp index b58f6ba..912a60e 100644 --- a/Source/Animation/Animation.hpp +++ b/Source/Animation/Animation.hpp @@ -20,7 +20,7 @@ along with Lugaru. If not, see . #ifndef _ANIMATION_HPP_ #define _ANIMATION_HPP_ -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" #include diff --git a/Source/Animation/Joint.hpp b/Source/Animation/Joint.hpp index 4b4f221..114ca07 100644 --- a/Source/Animation/Joint.hpp +++ b/Source/Animation/Joint.hpp @@ -21,7 +21,7 @@ along with Lugaru. If not, see . #ifndef _JOINT_HPP_ #define _JOINT_HPP_ -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" #include diff --git a/Source/Animation/Skeleton.hpp b/Source/Animation/Skeleton.hpp index bbf91ed..0f900f5 100644 --- a/Source/Animation/Skeleton.hpp +++ b/Source/Animation/Skeleton.hpp @@ -27,7 +27,7 @@ along with Lugaru. If not, see . #include "Graphic/gamegl.hpp" #include "Graphic/Models.hpp" #include "Graphic/Sprite.hpp" -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" #include "Objects/Object.hpp" #include "Utils/binio.h" diff --git a/Source/Audio/Sounds.hpp b/Source/Audio/Sounds.hpp index 1ec3139..4ceddec 100644 --- a/Source/Audio/Sounds.hpp +++ b/Source/Audio/Sounds.hpp @@ -20,7 +20,7 @@ along with Lugaru. If not, see . #ifndef _SOUNDS_HPP_ #define _SOUNDS_HPP_ -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" enum sound_types { #define DECLARE_SOUND(id, filename) id, diff --git a/Source/Audio/openal_wrapper.cpp b/Source/Audio/openal_wrapper.cpp index 10c86b9..e8203af 100644 --- a/Source/Audio/openal_wrapper.cpp +++ b/Source/Audio/openal_wrapper.cpp @@ -22,7 +22,7 @@ along with Lugaru. If not, see . #include "Audio/Sounds.hpp" #include "Game.hpp" -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" #include #include diff --git a/Source/Audio/openal_wrapper.hpp b/Source/Audio/openal_wrapper.hpp index 2149732..d122030 100644 --- a/Source/Audio/openal_wrapper.hpp +++ b/Source/Audio/openal_wrapper.hpp @@ -22,7 +22,7 @@ along with Lugaru. If not, see . #define _OPENAL_WRAPPER_HPP_ #include "MacCompatibility.hpp" -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" #ifdef __APPLE__ #include diff --git a/Source/Environment/Lights.hpp b/Source/Environment/Lights.hpp index ca113e0..19c1432 100644 --- a/Source/Environment/Lights.hpp +++ b/Source/Environment/Lights.hpp @@ -22,7 +22,7 @@ along with Lugaru. If not, see . #define _LIGHTS_HPP_ #include "Graphic/gamegl.hpp" -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" class Light { diff --git a/Source/Environment/Skybox.hpp b/Source/Environment/Skybox.hpp index 2fb2d5d..3f72664 100644 --- a/Source/Environment/Skybox.hpp +++ b/Source/Environment/Skybox.hpp @@ -23,8 +23,8 @@ along with Lugaru. If not, see . #include "Graphic/gamegl.hpp" #include "Graphic/Texture.hpp" -#include "Math/Quaternions.hpp" -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" +#include "Math/XYZ.hpp" #include "Utils/ImageIO.hpp" class SkyBox diff --git a/Source/Environment/Terrain.hpp b/Source/Environment/Terrain.hpp index 8cf68e1..884829c 100644 --- a/Source/Environment/Terrain.hpp +++ b/Source/Environment/Terrain.hpp @@ -26,7 +26,7 @@ along with Lugaru. If not, see . #include "Graphic/gamegl.hpp" #include "Graphic/Texture.hpp" #include "Math/Frustum.hpp" -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" #include "Utils/ImageIO.hpp" #define max_terrain_size 256 diff --git a/Source/Globals.cpp b/Source/Globals.cpp index bbe2df2..9054955 100644 --- a/Source/Globals.cpp +++ b/Source/Globals.cpp @@ -19,7 +19,7 @@ along with Lugaru. If not, see . */ #include "Graphic/Stereo.hpp" -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" #include "Objects/Weapons.hpp" #include diff --git a/Source/Graphic/Decal.hpp b/Source/Graphic/Decal.hpp index 094d043..1390bcc 100644 --- a/Source/Graphic/Decal.hpp +++ b/Source/Graphic/Decal.hpp @@ -24,7 +24,7 @@ along with Lugaru. If not, see . class Terrain; class Model; -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" enum decal_type { shadowdecal = 0, diff --git a/Source/Graphic/Models.hpp b/Source/Graphic/Models.hpp index 6b907f2..3756c1e 100644 --- a/Source/Graphic/Models.hpp +++ b/Source/Graphic/Models.hpp @@ -24,7 +24,7 @@ along with Lugaru. If not, see . #include "Environment/Terrain.hpp" #include "Graphic/gamegl.hpp" #include "Graphic/Texture.hpp" -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" #include "Utils/binio.h" #include diff --git a/Source/Graphic/Sprite.hpp b/Source/Graphic/Sprite.hpp index 0eb1d19..6b76ef5 100644 --- a/Source/Graphic/Sprite.hpp +++ b/Source/Graphic/Sprite.hpp @@ -26,7 +26,7 @@ along with Lugaru. If not, see . #include "Graphic/gamegl.hpp" #include "Graphic/Texture.hpp" #include "Math/Frustum.hpp" -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" #include "Objects/Object.hpp" #include "Utils/ImageIO.hpp" diff --git a/Source/Graphic/Text.hpp b/Source/Graphic/Text.hpp index ea23c6f..65c4ca1 100644 --- a/Source/Graphic/Text.hpp +++ b/Source/Graphic/Text.hpp @@ -23,7 +23,7 @@ along with Lugaru. If not, see . #include "Graphic/gamegl.hpp" #include "Graphic/Texture.hpp" -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" #include "Utils/ImageIO.hpp" #include diff --git a/Source/Level/Campaign.hpp b/Source/Level/Campaign.hpp index 5381672..77eb313 100644 --- a/Source/Level/Campaign.hpp +++ b/Source/Level/Campaign.hpp @@ -18,7 +18,7 @@ You should have received a copy of the GNU General Public License along with Lugaru. If not, see . */ -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" #include #include diff --git a/Source/Level/Dialog.hpp b/Source/Level/Dialog.hpp index 43bc31c..618e335 100644 --- a/Source/Level/Dialog.hpp +++ b/Source/Level/Dialog.hpp @@ -21,7 +21,7 @@ along with Lugaru. If not, see . #ifndef _DIALOG_HPP_ #define _DIALOG_HPP_ -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" #include #include diff --git a/Source/Level/Hotspot.hpp b/Source/Level/Hotspot.hpp index 3629a55..c29fb4c 100644 --- a/Source/Level/Hotspot.hpp +++ b/Source/Level/Hotspot.hpp @@ -21,7 +21,7 @@ along with Lugaru. If not, see . #ifndef _HOTSPOT_HPP_ #define _HOTSPOT_HPP_ -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" #include diff --git a/Source/Math/PhysicsMath.hpp b/Source/Math/PhysicsMath.hpp deleted file mode 100644 index 6d1e5a6..0000000 --- a/Source/Math/PhysicsMath.hpp +++ /dev/null @@ -1,763 +0,0 @@ -/* -Copyright (C) 2003, 2010 - Wolfire Games -Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file) - -This file is part of Lugaru. - -Lugaru is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -Lugaru is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with Lugaru. If not, see . -*/ - -#ifndef _PHYSICSMATH_HPP_ -#define _PHYSICSMATH_HPP_ - -#include "MacCompatibility.hpp" - -//------------------------------------------------------------------------// -// Misc. Constants -//------------------------------------------------------------------------// - -float const pi = 3.14159265f; -float const g = -32.174f; // acceleration due to gravity, ft/s^2 -float const rho = 0.0023769f; // desity of air at sea level, slugs/ft^3 -float const tol = 0.0000000001f; // float type tolerance - - -//------------------------------------------------------------------------// -// Misc. Functions -//------------------------------------------------------------------------// -inline float DegreesToRadians(float deg); -inline float RadiansToDegrees(float rad); - -inline float DegreesToRadians(float deg) -{ - return deg * pi / 180.0f; -} - -inline float RadiansToDegrees(float rad) -{ - return rad * 180.0f / pi; -} - -//------------------------------------------------------------------------// -// Vector Class and vector functions -//------------------------------------------------------------------------// -class Vector -{ -public: - float x; - float y; - float z; - - Vector(void); - Vector(float xi, float yi, float zi); - - float Magnitude(void); - void Normalize(void); - void Reverse(void); - - Vector& operator+=(Vector u); // vector addition - Vector& operator-=(Vector u); // vector subtraction - Vector& operator*=(float s); // scalar multiply - Vector& operator/=(float s); // scalar divide - - Vector operator-(void); - -}; - -inline Vector operator+(Vector u, Vector v); -inline Vector operator-(Vector u, Vector v); -inline Vector operator^(Vector u, Vector v); -inline float operator*(Vector u, Vector v); -inline Vector operator*(float s, Vector u); -inline Vector operator*(Vector u, float s); -inline Vector operator/(Vector u, float s); -inline float TripleScalarProduct(Vector u, Vector v, Vector w); -/* -float fast_sqrt2 (register float arg); -float fast_sqrt2 (register float arg) -{ -// Can replace with slower return std::sqrt(arg); -register float result; - -if (arg == 0.0) return 0.0; - -asm { -frsqrte result,arg // Calculate Square root -} - -// Newton Rhapson iterations. -result = result + 0.5 * result * (1.0 - arg * result * result); -result = result + 0.5 * result * (1.0 - arg * result * result); - -return result * arg; -} -*/ -inline Vector::Vector(void) -{ - x = 0; - y = 0; - z = 0; -} - -inline Vector::Vector(float xi, float yi, float zi) -{ - x = xi; - y = yi; - z = zi; -} - -inline float Vector::Magnitude(void) -{ - return (float) sqrt(x * x + y * y + z * z); -} - -inline void Vector::Normalize(void) -{ - float m = (float) sqrt(x * x + y * y + z * z); - if (m <= tol) - m = 1; - x /= m; - y /= m; - z /= m; - - if (fabs(x) < tol) - x = 0.0f; - if (fabs(y) < tol) - y = 0.0f; - if (fabs(z) < tol) - z = 0.0f; -} - -inline void Vector::Reverse(void) -{ - x = -x; - y = -y; - z = -z; -} - -inline Vector& Vector::operator+=(Vector u) -{ - x += u.x; - y += u.y; - z += u.z; - return *this; -} - -inline Vector& Vector::operator-=(Vector u) -{ - x -= u.x; - y -= u.y; - z -= u.z; - return *this; -} - -inline Vector& Vector::operator*=(float s) -{ - x *= s; - y *= s; - z *= s; - return *this; -} - -inline Vector& Vector::operator/=(float s) -{ - x /= s; - y /= s; - z /= s; - return *this; -} - -inline Vector Vector::operator-(void) -{ - return Vector(-x, -y, -z); -} - - -inline Vector operator+(Vector u, Vector v) -{ - return Vector(u.x + v.x, u.y + v.y, u.z + v.z); -} - -inline Vector operator-(Vector u, Vector v) -{ - return Vector(u.x - v.x, u.y - v.y, u.z - v.z); -} - -// Vector cross product (u cross v) -inline Vector operator^(Vector u, Vector v) -{ - return Vector( u.y * v.z - u.z * v.y, - -u.x * v.z + u.z * v.x, - u.x * v.y - u.y * v.x ); -} - -// Vector dot product -inline float operator*(Vector u, Vector v) -{ - return (u.x * v.x + u.y * v.y + u.z * v.z); -} - -inline Vector operator*(float s, Vector u) -{ - return Vector(u.x * s, u.y * s, u.z * s); -} - -inline Vector operator*(Vector u, float s) -{ - return Vector(u.x * s, u.y * s, u.z * s); -} - -inline Vector operator/(Vector u, float s) -{ - return Vector(u.x / s, u.y / s, u.z / s); -} - -// triple scalar product (u dot (v cross w)) -inline float TripleScalarProduct(Vector u, Vector v, Vector w) -{ - return float( (u.x * (v.y * w.z - v.z * w.y)) + - (u.y * (-v.x * w.z + v.z * w.x)) + - (u.z * (v.x * w.y - v.y * w.x)) ); - //return u*(v^w); - -} - - - -//------------------------------------------------------------------------// -// Matrix Class and matrix functions -//------------------------------------------------------------------------// - -class Matrix3x3 -{ -public: - // elements eij: i -> row, j -> column - float e11, e12, e13, e21, e22, e23, e31, e32, e33; - - Matrix3x3(void); - Matrix3x3( float r1c1, float r1c2, float r1c3, - float r2c1, float r2c2, float r2c3, - float r3c1, float r3c2, float r3c3 ); - - float det(void); - Matrix3x3 Transpose(void); - Matrix3x3 Inverse(void); - - Matrix3x3& operator+=(Matrix3x3 m); - Matrix3x3& operator-=(Matrix3x3 m); - Matrix3x3& operator*=(float s); - Matrix3x3& operator/=(float s); -}; - -inline Matrix3x3 operator+(Matrix3x3 m1, Matrix3x3 m2); -inline Matrix3x3 operator-(Matrix3x3 m1, Matrix3x3 m2); -inline Matrix3x3 operator/(Matrix3x3 m, float s); -inline Matrix3x3 operator*(Matrix3x3 m1, Matrix3x3 m2); -inline Matrix3x3 operator*(Matrix3x3 m, float s); -inline Matrix3x3 operator*(float s, Matrix3x3 m); -inline Vector operator*(Matrix3x3 m, Vector u); -inline Vector operator*(Vector u, Matrix3x3 m); - - - - - -inline Matrix3x3::Matrix3x3(void) -{ - e11 = 0; - e12 = 0; - e13 = 0; - e21 = 0; - e22 = 0; - e23 = 0; - e31 = 0; - e32 = 0; - e33 = 0; -} - -inline Matrix3x3::Matrix3x3( float r1c1, float r1c2, float r1c3, - float r2c1, float r2c2, float r2c3, - float r3c1, float r3c2, float r3c3 ) -{ - e11 = r1c1; - e12 = r1c2; - e13 = r1c3; - e21 = r2c1; - e22 = r2c2; - e23 = r2c3; - e31 = r3c1; - e32 = r3c2; - e33 = r3c3; -} - -inline float Matrix3x3::det(void) -{ - return e11 * e22 * e33 - - e11 * e32 * e23 + - e21 * e32 * e13 - - e21 * e12 * e33 + - e31 * e12 * e23 - - e31 * e22 * e13; -} - -inline Matrix3x3 Matrix3x3::Transpose(void) -{ - return Matrix3x3(e11, e21, e31, e12, e22, e32, e13, e23, e33); -} - -inline Matrix3x3 Matrix3x3::Inverse(void) -{ - float d = e11 * e22 * e33 - - e11 * e32 * e23 + - e21 * e32 * e13 - - e21 * e12 * e33 + - e31 * e12 * e23 - - e31 * e22 * e13; - - if (d == 0) - d = 1; - - return Matrix3x3( (e22 * e33 - e23 * e32) / d, - -(e12 * e33 - e13 * e32) / d, - (e12 * e23 - e13 * e22) / d, - -(e21 * e33 - e23 * e31) / d, - (e11 * e33 - e13 * e31) / d, - -(e11 * e23 - e13 * e21) / d, - (e21 * e32 - e22 * e31) / d, - -(e11 * e32 - e12 * e31) / d, - (e11 * e22 - e12 * e21) / d ); -} - -inline Matrix3x3& Matrix3x3::operator+=(Matrix3x3 m) -{ - e11 += m.e11; - e12 += m.e12; - e13 += m.e13; - e21 += m.e21; - e22 += m.e22; - e23 += m.e23; - e31 += m.e31; - e32 += m.e32; - e33 += m.e33; - return *this; -} - -inline Matrix3x3& Matrix3x3::operator-=(Matrix3x3 m) -{ - e11 -= m.e11; - e12 -= m.e12; - e13 -= m.e13; - e21 -= m.e21; - e22 -= m.e22; - e23 -= m.e23; - e31 -= m.e31; - e32 -= m.e32; - e33 -= m.e33; - return *this; -} - -inline Matrix3x3& Matrix3x3::operator*=(float s) -{ - e11 *= s; - e12 *= s; - e13 *= s; - e21 *= s; - e22 *= s; - e23 *= s; - e31 *= s; - e32 *= s; - e33 *= s; - return *this; -} - -inline Matrix3x3& Matrix3x3::operator/=(float s) -{ - e11 /= s; - e12 /= s; - e13 /= s; - e21 /= s; - e22 /= s; - e23 /= s; - e31 /= s; - e32 /= s; - e33 /= s; - return *this; -} - -inline Matrix3x3 operator+(Matrix3x3 m1, Matrix3x3 m2) -{ - return Matrix3x3( m1.e11 + m2.e11, - m1.e12 + m2.e12, - m1.e13 + m2.e13, - m1.e21 + m2.e21, - m1.e22 + m2.e22, - m1.e23 + m2.e23, - m1.e31 + m2.e31, - m1.e32 + m2.e32, - m1.e33 + m2.e33); -} - -inline Matrix3x3 operator-(Matrix3x3 m1, Matrix3x3 m2) -{ - return Matrix3x3( m1.e11 - m2.e11, - m1.e12 - m2.e12, - m1.e13 - m2.e13, - m1.e21 - m2.e21, - m1.e22 - m2.e22, - m1.e23 - m2.e23, - m1.e31 - m2.e31, - m1.e32 - m2.e32, - m1.e33 - m2.e33); -} - -inline Matrix3x3 operator/(Matrix3x3 m, float s) -{ - return Matrix3x3( m.e11 / s, - m.e12 / s, - m.e13 / s, - m.e21 / s, - m.e22 / s, - m.e23 / s, - m.e31 / s, - m.e32 / s, - m.e33 / s); -} - -inline Matrix3x3 operator*(Matrix3x3 m1, Matrix3x3 m2) -{ - return Matrix3x3( m1.e11 * m2.e11 + m1.e12 * m2.e21 + m1.e13 * m2.e31, - m1.e11 * m2.e12 + m1.e12 * m2.e22 + m1.e13 * m2.e32, - m1.e11 * m2.e13 + m1.e12 * m2.e23 + m1.e13 * m2.e33, - m1.e21 * m2.e11 + m1.e22 * m2.e21 + m1.e23 * m2.e31, - m1.e21 * m2.e12 + m1.e22 * m2.e22 + m1.e23 * m2.e32, - m1.e21 * m2.e13 + m1.e22 * m2.e23 + m1.e23 * m2.e33, - m1.e31 * m2.e11 + m1.e32 * m2.e21 + m1.e33 * m2.e31, - m1.e31 * m2.e12 + m1.e32 * m2.e22 + m1.e33 * m2.e32, - m1.e31 * m2.e13 + m1.e32 * m2.e23 + m1.e33 * m2.e33 ); -} - -inline Matrix3x3 operator*(Matrix3x3 m, float s) -{ - return Matrix3x3( m.e11 * s, - m.e12 * s, - m.e13 * s, - m.e21 * s, - m.e22 * s, - m.e23 * s, - m.e31 * s, - m.e32 * s, - m.e33 * s); -} - -inline Matrix3x3 operator*(float s, Matrix3x3 m) -{ - return Matrix3x3( m.e11 * s, - m.e12 * s, - m.e13 * s, - m.e21 * s, - m.e22 * s, - m.e23 * s, - m.e31 * s, - m.e32 * s, - m.e33 * s); -} - -inline Vector operator*(Matrix3x3 m, Vector u) -{ - return Vector( m.e11 * u.x + m.e12 * u.y + m.e13 * u.z, - m.e21 * u.x + m.e22 * u.y + m.e23 * u.z, - m.e31 * u.x + m.e32 * u.y + m.e33 * u.z); -} - -inline Vector operator*(Vector u, Matrix3x3 m) -{ - return Vector( u.x * m.e11 + u.y * m.e21 + u.z * m.e31, - u.x * m.e12 + u.y * m.e22 + u.z * m.e32, - u.x * m.e13 + u.y * m.e23 + u.z * m.e33); -} - -//------------------------------------------------------------------------// -// Quaternion Class and Quaternion functions -//------------------------------------------------------------------------// - -class Quaternion -{ -public: - float n; // number (scalar) part - Vector v; // vector part: v.x, v.y, v.z - - Quaternion(void); - Quaternion(float e0, float e1, float e2, float e3); - - float Magnitude(void); - Vector GetVector(void); - float GetScalar(void); - Quaternion operator+=(Quaternion q); - Quaternion operator-=(Quaternion q); - Quaternion operator*=(float s); - Quaternion operator/=(float s); - Quaternion operator~(void) const { - return Quaternion(n, -v.x, -v.y, -v.z); - } -}; - -inline Quaternion operator+(Quaternion q1, Quaternion q2); -inline Quaternion operator-(Quaternion q1, Quaternion q2); -inline Quaternion operator*(Quaternion q1, Quaternion q2); -inline Quaternion operator*(Quaternion q, float s); -inline Quaternion operator*(float s, Quaternion q); -inline Quaternion operator*(Quaternion q, Vector v); -inline Quaternion operator*(Vector v, Quaternion q); -inline Quaternion operator/(Quaternion q, float s); -inline float QGetAngle(Quaternion q); -inline Vector QGetAxis(Quaternion q); -inline Quaternion QRotate(Quaternion q1, Quaternion q2); -inline Vector QVRotate(Quaternion q, Vector v); -inline Quaternion MakeQFromEulerAngles(float x, float y, float z); -inline Vector MakeEulerAnglesFromQ(Quaternion q); - - -inline Quaternion::Quaternion(void) -{ - n = 0; - v.x = 0; - v.y = 0; - v.z = 0; -} - -inline Quaternion::Quaternion(float e0, float e1, float e2, float e3) -{ - n = e0; - v.x = e1; - v.y = e2; - v.z = e3; -} - -inline float Quaternion::Magnitude(void) -{ - return (float) sqrt(n * n + v.x * v.x + v.y * v.y + v.z * v.z); -} - -inline Vector Quaternion::GetVector(void) -{ - return Vector(v.x, v.y, v.z); -} - -inline float Quaternion::GetScalar(void) -{ - return n; -} - -inline Quaternion Quaternion::operator+=(Quaternion q) -{ - n += q.n; - v.x += q.v.x; - v.y += q.v.y; - v.z += q.v.z; - return *this; -} - -inline Quaternion Quaternion::operator-=(Quaternion q) -{ - n -= q.n; - v.x -= q.v.x; - v.y -= q.v.y; - v.z -= q.v.z; - return *this; -} - -inline Quaternion Quaternion::operator*=(float s) -{ - n *= s; - v.x *= s; - v.y *= s; - v.z *= s; - return *this; -} - -inline Quaternion Quaternion::operator/=(float s) -{ - n /= s; - v.x /= s; - v.y /= s; - v.z /= s; - return *this; -} - -/*inline Quaternion Quaternion::operator~() -{ -return Quaternion(n, -v.x, -v.y, -v.z); -}*/ - -inline Quaternion operator+(Quaternion q1, Quaternion q2) -{ - return Quaternion( q1.n + q2.n, - q1.v.x + q2.v.x, - q1.v.y + q2.v.y, - q1.v.z + q2.v.z); -} - -inline Quaternion operator-(Quaternion q1, Quaternion q2) -{ - return Quaternion( q1.n - q2.n, - q1.v.x - q2.v.x, - q1.v.y - q2.v.y, - q1.v.z - q2.v.z); -} - -inline Quaternion operator*(Quaternion q1, Quaternion q2) -{ - return Quaternion( q1.n * q2.n - q1.v.x * q2.v.x - q1.v.y * q2.v.y - q1.v.z * q2.v.z, - q1.n * q2.v.x + q1.v.x * q2.n + q1.v.y * q2.v.z - q1.v.z * q2.v.y, - q1.n * q2.v.y + q1.v.y * q2.n + q1.v.z * q2.v.x - q1.v.x * q2.v.z, - q1.n * q2.v.z + q1.v.z * q2.n + q1.v.x * q2.v.y - q1.v.y * q2.v.x); -} - -inline Quaternion operator*(Quaternion q, float s) -{ - return Quaternion(q.n * s, q.v.x * s, q.v.y * s, q.v.z * s); -} - -inline Quaternion operator*(float s, Quaternion q) -{ - return Quaternion(q.n * s, q.v.x * s, q.v.y * s, q.v.z * s); -} - -inline Quaternion operator*(Quaternion q, Vector v) -{ - return Quaternion( -(q.v.x * v.x + q.v.y * v.y + q.v.z * v.z), - q.n * v.x + q.v.y * v.z - q.v.z * v.y, - q.n * v.y + q.v.z * v.x - q.v.x * v.z, - q.n * v.z + q.v.x * v.y - q.v.y * v.x); -} - -inline Quaternion operator*(Vector v, Quaternion q) -{ - return Quaternion( -(q.v.x * v.x + q.v.y * v.y + q.v.z * v.z), - q.n * v.x + q.v.z * v.y - q.v.y * v.z, - q.n * v.y + q.v.x * v.z - q.v.z * v.x, - q.n * v.z + q.v.y * v.x - q.v.x * v.y); -} - -inline Quaternion operator/(Quaternion q, float s) -{ - return Quaternion(q.n / s, q.v.x / s, q.v.y / s, q.v.z / s); -} - -inline float QGetAngle(Quaternion q) -{ - return (float) (2 * acosf(q.n)); -} - -inline Vector QGetAxis(Quaternion q) -{ - Vector v; - float m; - - v = q.GetVector(); - m = v.Magnitude(); - - if (m <= tol) - return Vector(); - else - return v / m; -} - -inline Quaternion QRotate(Quaternion q1, Quaternion q2) -{ - return q1 * q2 * (~q1); -} - -inline Vector QVRotate(Quaternion q, Vector v) -{ - Quaternion t; - - - t = q * v * (~q); - - return t.GetVector(); -} - -inline Quaternion MakeQFromEulerAngles(float x, float y, float z) -{ - Quaternion q; - double roll = DegreesToRadians(x); - double pitch = DegreesToRadians(y); - double yaw = DegreesToRadians(z); - - double cyaw, cpitch, croll, syaw, spitch, sroll; - double cyawcpitch, syawspitch, cyawspitch, syawcpitch; - - cyaw = cos(0.5f * yaw); - cpitch = cos(0.5f * pitch); - croll = cos(0.5f * roll); - syaw = sin(0.5f * yaw); - spitch = sin(0.5f * pitch); - sroll = sin(0.5f * roll); - - cyawcpitch = cyaw * cpitch; - syawspitch = syaw * spitch; - cyawspitch = cyaw * spitch; - syawcpitch = syaw * cpitch; - - q.n = (float) (cyawcpitch * croll + syawspitch * sroll); - q.v.x = (float) (cyawcpitch * sroll - syawspitch * croll); - q.v.y = (float) (cyawspitch * croll + syawcpitch * sroll); - q.v.z = (float) (syawcpitch * croll - cyawspitch * sroll); - - return q; -} - -inline Vector MakeEulerAnglesFromQ(Quaternion q) -{ - double r11, r21, r31, r32, r33; - double q00, q11, q22, q33; - double tmp; - Vector u; - - q00 = q.n * q.n; - q11 = q.v.x * q.v.x; - q22 = q.v.y * q.v.y; - q33 = q.v.z * q.v.z; - - r11 = q00 + q11 - q22 - q33; - r21 = 2 * (q.v.x * q.v.y + q.n * q.v.z); - r31 = 2 * (q.v.x * q.v.z - q.n * q.v.y); - r32 = 2 * (q.v.y * q.v.z + q.n * q.v.x); - r33 = q00 - q11 - q22 + q33; - - tmp = fabs(r31); - if (tmp > 0.999999) { - double r12 = 2 * (q.v.x * q.v.y - q.n * q.v.z); - double r13 = 2 * (q.v.x * q.v.z + q.n * q.v.y); - - u.x = RadiansToDegrees(0.0f); //roll - u.y = RadiansToDegrees((float) (-(pi / 2) * r31 / tmp)); // pitch - u.z = RadiansToDegrees((float) atan2(-r12, -r31 * r13)); // yaw - return u; - } - - u.x = RadiansToDegrees((float) atan2(r32, r33)); // roll - u.y = RadiansToDegrees((float) asinf(-r31)); // pitch - u.z = RadiansToDegrees((float) atan2(r21, r11)); // yaw - return u; - - -} - - - - - -#endif diff --git a/Source/Math/Quaternions.cpp b/Source/Math/Quaternions.cpp deleted file mode 100644 index 0feaa70..0000000 --- a/Source/Math/Quaternions.cpp +++ /dev/null @@ -1,534 +0,0 @@ -/* -Copyright (C) 2003, 2010 - Wolfire Games -Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file) - -This file is part of Lugaru. - -Lugaru is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -Lugaru is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with Lugaru. If not, see . -*/ - -#include "Math/Quaternions.hpp" - -// Functions -quaternion Quat_Mult(quaternion q1, quaternion q2) -{ - quaternion QResult; - float a, b, c, d, e, f, g, h; - a = (q1.w + q1.x) * (q2.w + q2.x); - b = (q1.z - q1.y) * (q2.y - q2.z); - c = (q1.w - q1.x) * (q2.y + q2.z); - d = (q1.y + q1.z) * (q2.w - q2.x); - e = (q1.x + q1.z) * (q2.x + q2.y); - f = (q1.x - q1.z) * (q2.x - q2.y); - g = (q1.w + q1.y) * (q2.w - q2.z); - h = (q1.w - q1.y) * (q2.w + q2.z); - QResult.w = b + (-e - f + g + h) / 2; - QResult.x = a - (e + f + g + h) / 2; - QResult.y = c + (e - f + g - h) / 2; - QResult.z = d + (e - f - g + h) / 2; - return QResult; -} - - - -quaternion To_Quat(Matrix_t m) -{ - // From Jason Shankel, (C) 2000. - static quaternion Quat; - - static double Tr = m[0][0] + m[1][1] + m[2][2] + 1.0, fourD; - static double q[4]; - - static int i, j, k; - if (Tr >= 1.0) { - fourD = 2.0 * fast_sqrt(Tr); - q[3] = fourD / 4.0; - q[0] = (m[2][1] - m[1][2]) / fourD; - q[1] = (m[0][2] - m[2][0]) / fourD; - q[2] = (m[1][0] - m[0][1]) / fourD; - } else { - if (m[0][0] > m[1][1]) { - i = 0; - } else { - i = 1; - } - if (m[2][2] > m[i][i]) { - i = 2; - } - j = (i + 1) % 3; - k = (j + 1) % 3; - fourD = 2.0 * fast_sqrt(m[i][i] - m[j][j] - m[k][k] + 1.0); - q[i] = fourD / 4.0; - q[j] = (m[j][i] + m[i][j]) / fourD; - q[k] = (m[k][i] + m[i][k]) / fourD; - q[3] = (m[j][k] - m[k][j]) / fourD; - } - - Quat.x = q[0]; - Quat.y = q[1]; - Quat.z = q[2]; - Quat.w = q[3]; - return Quat; -} -void Quat_2_Matrix(quaternion Quat, Matrix_t m) -{ - // From the GLVelocity site (http://glvelocity.gamedev.net) - float fW = Quat.w; - float fX = Quat.x; - float fY = Quat.y; - float fZ = Quat.z; - float fXX = fX * fX; - float fYY = fY * fY; - float fZZ = fZ * fZ; - m[0][0] = 1.0f - 2.0f * (fYY + fZZ); - m[1][0] = 2.0f * (fX * fY + fW * fZ); - m[2][0] = 2.0f * (fX * fZ - fW * fY); - m[3][0] = 0.0f; - m[0][1] = 2.0f * (fX * fY - fW * fZ); - m[1][1] = 1.0f - 2.0f * (fXX + fZZ); - m[2][1] = 2.0f * (fY * fZ + fW * fX); - m[3][1] = 0.0f; - m[0][2] = 2.0f * (fX * fZ + fW * fY); - m[1][2] = 2.0f * (fX * fZ - fW * fX); - m[2][2] = 1.0f - 2.0f * (fXX + fYY); - m[3][2] = 0.0f; - m[0][3] = 0.0f; - m[1][3] = 0.0f; - m[2][3] = 0.0f; - m[3][3] = 1.0f; -} -quaternion To_Quat(angle_axis Ang_Ax) -{ - // From the Quaternion Powers article on gamedev.net - static quaternion Quat; - - Quat.x = Ang_Ax.x * sin(Ang_Ax.angle / 2); - Quat.y = Ang_Ax.y * sin(Ang_Ax.angle / 2); - Quat.z = Ang_Ax.z * sin(Ang_Ax.angle / 2); - Quat.w = cos(Ang_Ax.angle / 2); - return Quat; -} -angle_axis Quat_2_AA(quaternion Quat) -{ - static angle_axis Ang_Ax; - static float scale, tw; - tw = (float)acosf(Quat.w) * 2; - scale = (float)sin(tw / 2.0); - Ang_Ax.x = Quat.x / scale; - Ang_Ax.y = Quat.y / scale; - Ang_Ax.z = Quat.z / scale; - - Ang_Ax.angle = 2.0 * acosf(Quat.w) / (float)PI * 180; - return Ang_Ax; -} - -quaternion To_Quat(int In_Degrees, euler Euler) -{ - // From the gamasutra quaternion article - static quaternion Quat; - static float cr, cp, cy, sr, sp, sy, cpcy, spsy; - //If we are in Degree mode, convert to Radians - if (In_Degrees) { - Euler.x = Euler.x * (float)PI / 180; - Euler.y = Euler.y * (float)PI / 180; - Euler.z = Euler.z * (float)PI / 180; - } - //Calculate trig identities - //Formerly roll, pitch, yaw - cr = float(cos(Euler.x / 2)); - cp = float(cos(Euler.y / 2)); - cy = float(cos(Euler.z / 2)); - sr = float(sin(Euler.x / 2)); - sp = float(sin(Euler.y / 2)); - sy = float(sin(Euler.z / 2)); - - cpcy = cp * cy; - spsy = sp * sy; - Quat.w = cr * cpcy + sr * spsy; - Quat.x = sr * cpcy - cr * spsy; - Quat.y = cr * sp * cy + sr * cp * sy; - Quat.z = cr * cp * sy - sr * sp * cy; - - return Quat; -} - -quaternion QNormalize(quaternion Quat) -{ - static float norm; - norm = Quat.x * Quat.x + - Quat.y * Quat.y + - Quat.z * Quat.z + - Quat.w * Quat.w; - Quat.x = float(Quat.x / norm); - Quat.y = float(Quat.y / norm); - Quat.z = float(Quat.z / norm); - Quat.w = float(Quat.w / norm); - return Quat; -} - -XYZ Quat2Vector(quaternion Quat) -{ - QNormalize(Quat); - - float fW = Quat.w; - float fX = Quat.x; - float fY = Quat.y; - float fZ = Quat.z; - - XYZ tempvec; - - tempvec.x = 2.0f * (fX * fZ - fW * fY); - tempvec.y = 2.0f * (fY * fZ + fW * fX); - tempvec.z = 1.0f - 2.0f * (fX * fX + fY * fY); - - return tempvec; -} - -bool PointInTriangle(Vector *p, Vector normal, float p11, float p12, float p13, float p21, float p22, float p23, float p31, float p32, float p33) -{ - static float u0, u1, u2; - static float v0, v1, v2; - static float a, b; - static float max; - static int i, j; - static bool bInter; - static float pointv[3]; - static float p1v[3]; - static float p2v[3]; - static float p3v[3]; - static float normalv[3]; - - bInter = 0; - - pointv[0] = p->x; - pointv[1] = p->y; - pointv[2] = p->z; - - - p1v[0] = p11; - p1v[1] = p12; - p1v[2] = p13; - - p2v[0] = p21; - p2v[1] = p22; - p2v[2] = p23; - - p3v[0] = p31; - p3v[1] = p32; - p3v[2] = p33; - - normalv[0] = normal.x; - normalv[1] = normal.y; - normalv[2] = normal.z; - -#define ABS(X) (((X)<0.f)?-(X):(X) ) -#define MAX(A, B) (((A)<(B))?(B):(A)) - max = MAX(MAX(ABS(normalv[0]), ABS(normalv[1])), ABS(normalv[2])); -#undef MAX - if (max == ABS(normalv[0])) { - i = 1; // y, z - j = 2; - } - if (max == ABS(normalv[1])) { - i = 0; // x, z - j = 2; - } - if (max == ABS(normalv[2])) { - i = 0; // x, y - j = 1; - } -#undef ABS - - u0 = pointv[i] - p1v[i]; - v0 = pointv[j] - p1v[j]; - u1 = p2v[i] - p1v[i]; - v1 = p2v[j] - p1v[j]; - u2 = p3v[i] - p1v[i]; - v2 = p3v[j] - p1v[j]; - - if (u1 > -1.0e-05f && u1 < 1.0e-05f) { // == 0.0f) - b = u0 / u2; - if (0.0f <= b && b <= 1.0f) { - a = (v0 - b * v2) / v1; - if ((a >= 0.0f) && (( a + b ) <= 1.0f)) - bInter = 1; - } - } else { - b = (v0 * u1 - u0 * v1) / (v2 * u1 - u2 * v1); - if (0.0f <= b && b <= 1.0f) { - a = (u0 - b * u2) / u1; - if ((a >= 0.0f) && (( a + b ) <= 1.0f )) - bInter = 1; - } - } - - return bInter; -} - -bool LineFacet(Vector p1, Vector p2, Vector pa, Vector pb, Vector pc, Vector *p) -{ - static float d; - static float denom, mu; - static Vector n; - - //Calculate the parameters for the plane - n.x = (pb.y - pa.y) * (pc.z - pa.z) - (pb.z - pa.z) * (pc.y - pa.y); - n.y = (pb.z - pa.z) * (pc.x - pa.x) - (pb.x - pa.x) * (pc.z - pa.z); - n.z = (pb.x - pa.x) * (pc.y - pa.y) - (pb.y - pa.y) * (pc.x - pa.x); - n.Normalize(); - d = - n.x * pa.x - n.y * pa.y - n.z * pa.z; - - //Calculate the position on the line that intersects the plane - denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z); - if (fabs(denom) < 0.0000001) // Line and plane don't intersect - return 0; - mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom; - p->x = p1.x + mu * (p2.x - p1.x); - p->y = p1.y + mu * (p2.y - p1.y); - p->z = p1.z + mu * (p2.z - p1.z); - if (mu < 0 || mu > 1) // Intersection not along line segment - return 0; - - if (!PointInTriangle( p, n, pa.x, pa.y, pa.z, pb.x, pb.y, pb.z, pc.x, pc.y, pc.z)) { - return 0; - } - - return 1; -} - -bool PointInTriangle(XYZ *p, XYZ normal, XYZ *p1, XYZ *p2, XYZ *p3) -{ - static float u0, u1, u2; - static float v0, v1, v2; - static float a, b; - static float max; - static int i, j; - static bool bInter = 0; - static float pointv[3]; - static float p1v[3]; - static float p2v[3]; - static float p3v[3]; - static float normalv[3]; - - bInter = 0; - - pointv[0] = p->x; - pointv[1] = p->y; - pointv[2] = p->z; - - - p1v[0] = p1->x; - p1v[1] = p1->y; - p1v[2] = p1->z; - - p2v[0] = p2->x; - p2v[1] = p2->y; - p2v[2] = p2->z; - - p3v[0] = p3->x; - p3v[1] = p3->y; - p3v[2] = p3->z; - - normalv[0] = normal.x; - normalv[1] = normal.y; - normalv[2] = normal.z; - -#define ABS(X) (((X)<0.f)?-(X):(X) ) -#define MAX(A, B) (((A)<(B))?(B):(A)) - max = MAX(MAX(ABS(normalv[0]), ABS(normalv[1])), ABS(normalv[2])); -#undef MAX - if (max == ABS(normalv[0])) { - i = 1; // y, z - j = 2; - } - if (max == ABS(normalv[1])) { - i = 0; // x, z - j = 2; - } - if (max == ABS(normalv[2])) { - i = 0; // x, y - j = 1; - } -#undef ABS - - u0 = pointv[i] - p1v[i]; - v0 = pointv[j] - p1v[j]; - u1 = p2v[i] - p1v[i]; - v1 = p2v[j] - p1v[j]; - u2 = p3v[i] - p1v[i]; - v2 = p3v[j] - p1v[j]; - - if (u1 > -1.0e-05f && u1 < 1.0e-05f) { // == 0.0f) - b = u0 / u2; - if (0.0f <= b && b <= 1.0f) { - a = (v0 - b * v2) / v1; - if ((a >= 0.0f) && (( a + b ) <= 1.0f)) - bInter = 1; - } - } else { - b = (v0 * u1 - u0 * v1) / (v2 * u1 - u2 * v1); - if (0.0f <= b && b <= 1.0f) { - a = (u0 - b * u2) / u1; - if ((a >= 0.0f) && (( a + b ) <= 1.0f )) - bInter = 1; - } - } - - return bInter; -} - -bool LineFacet(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ *p) -{ - static float d; - static float denom, mu; - static XYZ n; - - //Calculate the parameters for the plane - n.x = (pb.y - pa.y) * (pc.z - pa.z) - (pb.z - pa.z) * (pc.y - pa.y); - n.y = (pb.z - pa.z) * (pc.x - pa.x) - (pb.x - pa.x) * (pc.z - pa.z); - n.z = (pb.x - pa.x) * (pc.y - pa.y) - (pb.y - pa.y) * (pc.x - pa.x); - Normalise(&n); - d = - n.x * pa.x - n.y * pa.y - n.z * pa.z; - - //Calculate the position on the line that intersects the plane - denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z); - if (fabs(denom) < 0.0000001) // Line and plane don't intersect - return 0; - mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom; - p->x = p1.x + mu * (p2.x - p1.x); - p->y = p1.y + mu * (p2.y - p1.y); - p->z = p1.z + mu * (p2.z - p1.z); - if (mu < 0 || mu > 1) // Intersection not along line segment - return 0; - - if (!PointInTriangle( p, n, &pa, &pb, &pc)) { - return 0; - } - - return 1; -} - -float LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ *p) -{ - static float d; - static float denom, mu; - static XYZ n; - - //Calculate the parameters for the plane - n.x = (pb.y - pa.y) * (pc.z - pa.z) - (pb.z - pa.z) * (pc.y - pa.y); - n.y = (pb.z - pa.z) * (pc.x - pa.x) - (pb.x - pa.x) * (pc.z - pa.z); - n.z = (pb.x - pa.x) * (pc.y - pa.y) - (pb.y - pa.y) * (pc.x - pa.x); - Normalise(&n); - d = - n.x * pa.x - n.y * pa.y - n.z * pa.z; - - //Calculate the position on the line that intersects the plane - denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z); - if (fabs(denom) < 0.0000001) // Line and plane don't intersect - return 0; - mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom; - p->x = p1.x + mu * (p2.x - p1.x); - p->y = p1.y + mu * (p2.y - p1.y); - p->z = p1.z + mu * (p2.z - p1.z); - if (mu < 0 || mu > 1) // Intersection not along line segment - return 0; - - if (!PointInTriangle( p, n, &pa, &pb, &pc)) { - return 0; - } - - return 1; -} - -float LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ n, XYZ *p) -{ - static float d; - static float denom, mu; - - //Calculate the parameters for the plane - d = - n.x * pa.x - n.y * pa.y - n.z * pa.z; - - //Calculate the position on the line that intersects the plane - denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z); - if (fabs(denom) < 0.0000001) // Line and plane don't intersect - return 0; - mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom; - p->x = p1.x + mu * (p2.x - p1.x); - p->y = p1.y + mu * (p2.y - p1.y); - p->z = p1.z + mu * (p2.z - p1.z); - if (mu < 0 || mu > 1) // Intersection not along line segment - return 0; - - if (!PointInTriangle( p, n, &pa, &pb, &pc)) { - return 0; - } - return 1; -} - -float LineFacetd(XYZ *p1, XYZ *p2, XYZ *pa, XYZ *pb, XYZ *pc, XYZ *p) -{ - static float d; - static float denom, mu; - static XYZ n; - - //Calculate the parameters for the plane - n.x = (pb->y - pa->y) * (pc->z - pa->z) - (pb->z - pa->z) * (pc->y - pa->y); - n.y = (pb->z - pa->z) * (pc->x - pa->x) - (pb->x - pa->x) * (pc->z - pa->z); - n.z = (pb->x - pa->x) * (pc->y - pa->y) - (pb->y - pa->y) * (pc->x - pa->x); - Normalise(&n); - d = - n.x * pa->x - n.y * pa->y - n.z * pa->z; - - - //Calculate the position on the line that intersects the plane - denom = n.x * (p2->x - p1->x) + n.y * (p2->y - p1->y) + n.z * (p2->z - p1->z); - if (fabs(denom) < 0.0000001) // Line and plane don't intersect - return 0; - mu = - (d + n.x * p1->x + n.y * p1->y + n.z * p1->z) / denom; - p->x = p1->x + mu * (p2->x - p1->x); - p->y = p1->y + mu * (p2->y - p1->y); - p->z = p1->z + mu * (p2->z - p1->z); - if (mu < 0 || mu > 1) // Intersection not along line segment - return 0; - - if (!PointInTriangle( p, n, pa, pb, pc)) { - return 0; - } - return 1; -} - -float LineFacetd(XYZ *p1, XYZ *p2, XYZ *pa, XYZ *pb, XYZ *pc, XYZ *n, XYZ *p) -{ - static float d; - static float denom, mu; - - //Calculate the parameters for the plane - d = - n->x * pa->x - n->y * pa->y - n->z * pa->z; - - //Calculate the position on the line that intersects the plane - denom = n->x * (p2->x - p1->x) + n->y * (p2->y - p1->y) + n->z * (p2->z - p1->z); - if (fabs(denom) < 0.0000001) // Line and plane don't intersect - return 0; - mu = - (d + n->x * p1->x + n->y * p1->y + n->z * p1->z) / denom; - p->x = p1->x + mu * (p2->x - p1->x); - p->y = p1->y + mu * (p2->y - p1->y); - p->z = p1->z + mu * (p2->z - p1->z); - if (mu < 0 || mu > 1) // Intersection not along line segment - return 0; - - if (!PointInTriangle( p, *n, pa, pb, pc)) { - return 0; - } - return 1; -} - - diff --git a/Source/Math/XYZ.cpp b/Source/Math/XYZ.cpp new file mode 100644 index 0000000..ad145ae --- /dev/null +++ b/Source/Math/XYZ.cpp @@ -0,0 +1,247 @@ +/* +Copyright (C) 2003, 2010 - Wolfire Games +Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file) + +This file is part of Lugaru. + +Lugaru is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +Lugaru is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Lugaru. If not, see . +*/ + +#include "Math/XYZ.hpp" + +bool PointInTriangle(XYZ *p, XYZ normal, XYZ *p1, XYZ *p2, XYZ *p3) +{ + static float u0, u1, u2; + static float v0, v1, v2; + static float a, b; + static float max; + static int i, j; + static bool bInter = 0; + static float pointv[3]; + static float p1v[3]; + static float p2v[3]; + static float p3v[3]; + static float normalv[3]; + + bInter = 0; + + pointv[0] = p->x; + pointv[1] = p->y; + pointv[2] = p->z; + + + p1v[0] = p1->x; + p1v[1] = p1->y; + p1v[2] = p1->z; + + p2v[0] = p2->x; + p2v[1] = p2->y; + p2v[2] = p2->z; + + p3v[0] = p3->x; + p3v[1] = p3->y; + p3v[2] = p3->z; + + normalv[0] = normal.x; + normalv[1] = normal.y; + normalv[2] = normal.z; + +#define ABS(X) (((X)<0.f)?-(X):(X) ) +#define MAX(A, B) (((A)<(B))?(B):(A)) + max = MAX(MAX(ABS(normalv[0]), ABS(normalv[1])), ABS(normalv[2])); +#undef MAX + if (max == ABS(normalv[0])) { + i = 1; // y, z + j = 2; + } + if (max == ABS(normalv[1])) { + i = 0; // x, z + j = 2; + } + if (max == ABS(normalv[2])) { + i = 0; // x, y + j = 1; + } +#undef ABS + + u0 = pointv[i] - p1v[i]; + v0 = pointv[j] - p1v[j]; + u1 = p2v[i] - p1v[i]; + v1 = p2v[j] - p1v[j]; + u2 = p3v[i] - p1v[i]; + v2 = p3v[j] - p1v[j]; + + if (u1 > -1.0e-05f && u1 < 1.0e-05f) { // == 0.0f) + b = u0 / u2; + if (0.0f <= b && b <= 1.0f) { + a = (v0 - b * v2) / v1; + if ((a >= 0.0f) && (( a + b ) <= 1.0f)) + bInter = 1; + } + } else { + b = (v0 * u1 - u0 * v1) / (v2 * u1 - u2 * v1); + if (0.0f <= b && b <= 1.0f) { + a = (u0 - b * u2) / u1; + if ((a >= 0.0f) && (( a + b ) <= 1.0f )) + bInter = 1; + } + } + + return bInter; +} + +bool LineFacet(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ *p) +{ + static float d; + static float denom, mu; + static XYZ n; + + //Calculate the parameters for the plane + n.x = (pb.y - pa.y) * (pc.z - pa.z) - (pb.z - pa.z) * (pc.y - pa.y); + n.y = (pb.z - pa.z) * (pc.x - pa.x) - (pb.x - pa.x) * (pc.z - pa.z); + n.z = (pb.x - pa.x) * (pc.y - pa.y) - (pb.y - pa.y) * (pc.x - pa.x); + Normalise(&n); + d = - n.x * pa.x - n.y * pa.y - n.z * pa.z; + + //Calculate the position on the line that intersects the plane + denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z); + if (fabs(denom) < 0.0000001) // Line and plane don't intersect + return 0; + mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom; + p->x = p1.x + mu * (p2.x - p1.x); + p->y = p1.y + mu * (p2.y - p1.y); + p->z = p1.z + mu * (p2.z - p1.z); + if (mu < 0 || mu > 1) // Intersection not along line segment + return 0; + + if (!PointInTriangle( p, n, &pa, &pb, &pc)) { + return 0; + } + + return 1; +} + +float LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ *p) +{ + static float d; + static float denom, mu; + static XYZ n; + + //Calculate the parameters for the plane + n.x = (pb.y - pa.y) * (pc.z - pa.z) - (pb.z - pa.z) * (pc.y - pa.y); + n.y = (pb.z - pa.z) * (pc.x - pa.x) - (pb.x - pa.x) * (pc.z - pa.z); + n.z = (pb.x - pa.x) * (pc.y - pa.y) - (pb.y - pa.y) * (pc.x - pa.x); + Normalise(&n); + d = - n.x * pa.x - n.y * pa.y - n.z * pa.z; + + //Calculate the position on the line that intersects the plane + denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z); + if (fabs(denom) < 0.0000001) // Line and plane don't intersect + return 0; + mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom; + p->x = p1.x + mu * (p2.x - p1.x); + p->y = p1.y + mu * (p2.y - p1.y); + p->z = p1.z + mu * (p2.z - p1.z); + if (mu < 0 || mu > 1) // Intersection not along line segment + return 0; + + if (!PointInTriangle( p, n, &pa, &pb, &pc)) { + return 0; + } + + return 1; +} + +float LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ n, XYZ *p) +{ + static float d; + static float denom, mu; + + //Calculate the parameters for the plane + d = - n.x * pa.x - n.y * pa.y - n.z * pa.z; + + //Calculate the position on the line that intersects the plane + denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z); + if (fabs(denom) < 0.0000001) // Line and plane don't intersect + return 0; + mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom; + p->x = p1.x + mu * (p2.x - p1.x); + p->y = p1.y + mu * (p2.y - p1.y); + p->z = p1.z + mu * (p2.z - p1.z); + if (mu < 0 || mu > 1) // Intersection not along line segment + return 0; + + if (!PointInTriangle( p, n, &pa, &pb, &pc)) { + return 0; + } + return 1; +} + +float LineFacetd(XYZ *p1, XYZ *p2, XYZ *pa, XYZ *pb, XYZ *pc, XYZ *p) +{ + static float d; + static float denom, mu; + static XYZ n; + + //Calculate the parameters for the plane + n.x = (pb->y - pa->y) * (pc->z - pa->z) - (pb->z - pa->z) * (pc->y - pa->y); + n.y = (pb->z - pa->z) * (pc->x - pa->x) - (pb->x - pa->x) * (pc->z - pa->z); + n.z = (pb->x - pa->x) * (pc->y - pa->y) - (pb->y - pa->y) * (pc->x - pa->x); + Normalise(&n); + d = - n.x * pa->x - n.y * pa->y - n.z * pa->z; + + + //Calculate the position on the line that intersects the plane + denom = n.x * (p2->x - p1->x) + n.y * (p2->y - p1->y) + n.z * (p2->z - p1->z); + if (fabs(denom) < 0.0000001) // Line and plane don't intersect + return 0; + mu = - (d + n.x * p1->x + n.y * p1->y + n.z * p1->z) / denom; + p->x = p1->x + mu * (p2->x - p1->x); + p->y = p1->y + mu * (p2->y - p1->y); + p->z = p1->z + mu * (p2->z - p1->z); + if (mu < 0 || mu > 1) // Intersection not along line segment + return 0; + + if (!PointInTriangle( p, n, pa, pb, pc)) { + return 0; + } + return 1; +} + +float LineFacetd(XYZ *p1, XYZ *p2, XYZ *pa, XYZ *pb, XYZ *pc, XYZ *n, XYZ *p) +{ + static float d; + static float denom, mu; + + //Calculate the parameters for the plane + d = - n->x * pa->x - n->y * pa->y - n->z * pa->z; + + //Calculate the position on the line that intersects the plane + denom = n->x * (p2->x - p1->x) + n->y * (p2->y - p1->y) + n->z * (p2->z - p1->z); + if (fabs(denom) < 0.0000001) // Line and plane don't intersect + return 0; + mu = - (d + n->x * p1->x + n->y * p1->y + n->z * p1->z) / denom; + p->x = p1->x + mu * (p2->x - p1->x); + p->y = p1->y + mu * (p2->y - p1->y); + p->z = p1->z + mu * (p2->z - p1->z); + if (mu < 0 || mu > 1) // Intersection not along line segment + return 0; + + if (!PointInTriangle( p, *n, pa, pb, pc)) { + return 0; + } + return 1; +} + + diff --git a/Source/Math/Quaternions.hpp b/Source/Math/XYZ.hpp similarity index 91% rename from Source/Math/Quaternions.hpp rename to Source/Math/XYZ.hpp index 3220545..6892ea1 100644 --- a/Source/Math/Quaternions.hpp +++ b/Source/Math/XYZ.hpp @@ -21,29 +21,10 @@ along with Lugaru. If not, see . #ifndef _QUATERNIONS_HPP_ #define _QUATERNIONS_HPP_ -#include "PhysicsMath.hpp" #include "Graphic/gamegl.hpp" #include -/**> Quaternion Structures <**/ -#define PI 3.14159265355555897932384626 -#define RADIANS 0 -#define DEGREES 1 -#define deg2rad .0174532925 - -//using namespace std; -typedef float Matrix_t [4][4]; -struct euler { - float x, y, z; -}; -struct angle_axis { - float x, y, z, angle; -}; -struct quaternion { - float x, y, z, w; -}; - class XYZ { public: @@ -62,21 +43,9 @@ public: inline void operator*=(XYZ add); inline void operator/=(float add); inline void operator=(float add); - inline void vec(Vector add); inline bool operator==(XYZ add); }; -/*********************> Quaternion Function definition <********/ -quaternion To_Quat(int Degree_Flag, euler Euler); -quaternion To_Quat(angle_axis Ang_Ax); -quaternion To_Quat(Matrix_t m); -angle_axis Quat_2_AA(quaternion Quat); -void Quat_2_Matrix(quaternion Quat, Matrix_t m); -quaternion Normalize(quaternion Quat); -quaternion Quat_Mult(quaternion q1, quaternion q2); -quaternion QNormalize(quaternion Quat); -XYZ Quat2Vector(quaternion Quat); - inline void CrossProduct(XYZ *P, XYZ *Q, XYZ *V); inline void CrossProduct(XYZ P, XYZ Q, XYZ *V); inline void Normalise(XYZ *vectory); @@ -88,8 +57,6 @@ float LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ *p); float LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ n, XYZ *p); float LineFacetd(XYZ *p1, XYZ *p2, XYZ *pa, XYZ *pb, XYZ *pc, XYZ *n, XYZ *p); float LineFacetd(XYZ *p1, XYZ *p2, XYZ *pa, XYZ *pb, XYZ *pc, XYZ *p); -bool PointInTriangle(Vector *p, Vector normal, float p11, float p12, float p13, float p21, float p22, float p23, float p31, float p32, float p33); -bool LineFacet(Vector p1, Vector p2, Vector pa, Vector pb, Vector pc, Vector *p); inline void ReflectVector(XYZ *vel, const XYZ *n); inline void ReflectVector(XYZ *vel, const XYZ &n); inline XYZ DoRotation(XYZ thePoint, float xang, float yang, float zang); @@ -211,13 +178,6 @@ inline void XYZ::operator=(float add) z = add; } -inline void XYZ::vec(Vector add) -{ - x = add.x; - y = add.y; - z = add.z; -} - inline bool XYZ::operator==(XYZ add) { if (x == add.x && y == add.y && z == add.z) diff --git a/Source/Objects/Object.hpp b/Source/Objects/Object.hpp index 11935fe..d1a86a8 100644 --- a/Source/Objects/Object.hpp +++ b/Source/Objects/Object.hpp @@ -28,7 +28,7 @@ along with Lugaru. If not, see . #include "Graphic/Sprite.hpp" #include "Graphic/Texture.hpp" #include "Math/Frustum.hpp" -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" #include "Utils/ImageIO.hpp" #include diff --git a/Source/Objects/Person.hpp b/Source/Objects/Person.hpp index 8ad6530..590bbe4 100644 --- a/Source/Objects/Person.hpp +++ b/Source/Objects/Person.hpp @@ -27,7 +27,7 @@ along with Lugaru. If not, see . #include "Graphic/gamegl.hpp" #include "Graphic/Models.hpp" #include "Graphic/Sprite.hpp" -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" #include "Objects/Weapons.hpp" #include diff --git a/Source/Objects/Weapons.hpp b/Source/Objects/Weapons.hpp index 2897cfb..2f802f7 100644 --- a/Source/Objects/Weapons.hpp +++ b/Source/Objects/Weapons.hpp @@ -27,7 +27,7 @@ along with Lugaru. If not, see . #include "Graphic/Models.hpp" #include "Graphic/Sprite.hpp" #include "Graphic/Texture.hpp" -#include "Math/Quaternions.hpp" +#include "Math/XYZ.hpp" #include "Objects/Person.hpp" #include -- 2.39.5