Skip to content

Commit

Permalink
Added Quaternion and Code restructure
Browse files Browse the repository at this point in the history
Added Quat for new rotations

Placed Engine files in new folder with the same name.
Moved Tangram and T3D to Examples folder.
  • Loading branch information
RicardoEPRodrigues committed Oct 30, 2017
1 parent f6f3e48 commit 74748bf
Show file tree
Hide file tree
Showing 62 changed files with 811 additions and 524 deletions.
2 changes: 2 additions & 0 deletions .idea/dictionaries/ricardo.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

834 changes: 397 additions & 437 deletions .idea/workspace.xml

Large diffs are not rendered by default.

61 changes: 30 additions & 31 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,34 @@ elseif (CMAKE_BUILD_TYPE STREQUAL "Release")
ENDIF ()

set(SOURCE_FILES
src/Debug.h src/Engine.cpp src/Engine.h
src/json.hpp
src/Math/Vector.cpp src/Math/Vector.h
src/Math/Vector2.cpp src/Math/Vector2.h
src/Math/Maths.cpp src/Math/Maths.h
src/Math/Vector4.cpp src/Math/Vector4.h
src/Math/Matrix.cpp src/Math/Matrix.h
src/Math/TMatrix.h
src/Math/Matrix3.cpp src/Math/Matrix3.h
src/Math/Matrix2.cpp src/Math/Matrix2.h
src/Shader/Shader.cpp src/Shader/Shader.h
src/Shader/ShaderProgram.cpp src/Shader/ShaderProgram.h
src/OpenGLUtils.h
src/Tangram/Tangram.cpp src/Tangram/Tangram.h
src/IActor.h
src/Tangram/Triangle.cpp src/Tangram/Triangle.h
src/Tangram/Square.cpp src/Tangram/Square.h
src/Tangram/Parallelogram.cpp src/Tangram/Parallelogram.h
src/Tangram3D/Cube.cpp src/Tangram3D/Cube.h
src/Camera/Camera.cpp src/Camera/Camera.h
src/Tangram3D/Tangram3D.cpp src/Tangram3D/Tangram3D.h
src/Tangram3D/CameraController.cpp src/Tangram3D/CameraController.h
src/Input.cpp src/Input.h
src/Camera/LookAt.cpp src/Camera/LookAt.h
src/Camera/Perspective.cpp src/Camera/Perspective.h
src/Camera/Ortho.cpp src/Camera/Ortho.h
src/Tangram3D/Triangle3D.cpp src/Tangram3D/Triangle3D.h
src/Tangram3D/Parallelogram3D.h src/Tangram3D/Parallelogram3D.cpp)
src/Engine/Debug.h src/Engine/Engine.cpp src/Engine/Engine.h
src/Engine/json.hpp
src/Engine/Math/Vector.cpp src/Engine/Math/Vector.h
src/Engine/Math/Vector2.cpp src/Engine/Math/Vector2.h
src/Engine/Math/Maths.cpp src/Engine/Math/Maths.h
src/Engine/Math/Vector4.cpp src/Engine/Math/Vector4.h
src/Engine/Math/Matrix.cpp src/Engine/Math/Matrix.h
src/Engine/Math/TMatrix.h
src/Engine/Math/Matrix3.cpp src/Engine/Math/Matrix3.h
src/Engine/Math/Matrix2.cpp src/Engine/Math/Matrix2.h
src/Engine/Shader/Shader.cpp src/Engine/Shader/Shader.h
src/Engine/Shader/ShaderProgram.cpp src/Engine/Shader/ShaderProgram.h
src/Engine/OpenGLUtils.h
src/Examples/Tangram/Tangram.cpp src/Examples/Tangram/Tangram.h
src/Engine/IActor.h
src/Examples/Tangram/Triangle.cpp src/Examples/Tangram/Triangle.h
src/Examples/Tangram/Square.cpp src/Examples/Tangram/Square.h
src/Examples/Tangram/Parallelogram.cpp src/Examples/Tangram/Parallelogram.h
src/Examples/Tangram3D/Cube.cpp src/Examples/Tangram3D/Cube.h
src/Engine/Camera/Camera.cpp src/Engine/Camera/Camera.h
src/Examples/Tangram3D/Tangram3D.cpp src/Examples/Tangram3D/Tangram3D.h
src/Examples/Tangram3D/CameraController.cpp src/Examples/Tangram3D/CameraController.h
src/Engine/Input.cpp src/Engine/Input.h
src/Engine/Camera/LookAt.cpp src/Engine/Camera/LookAt.h
src/Engine/Camera/Perspective.cpp src/Engine/Camera/Perspective.h
src/Engine/Camera/Ortho.cpp src/Engine/Camera/Ortho.h
src/Examples/Tangram3D/Triangle3D.cpp src/Examples/Tangram3D/Triangle3D.h
src/Examples/Tangram3D/Parallelogram3D.h src/Examples/Tangram3D/Parallelogram3D.cpp src/Engine/Math/Quat.cpp src/Engine/Math/Quat.h)

set(TESTS_SOURCE_FILES src/Tests/catch.hpp src/Tests/tests.cpp src/Tests/VectorTests.h src/Tests/MatrixTests.h)

Expand All @@ -70,12 +70,11 @@ elseif (UNIX AND NOT APPLE)

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wextra -pedantic -g3 -Og -Wuninitialized")

FIND_PACKAGE(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
find_package(GLEW REQUIRED)

if (NOT (OpenGL_FOUND AND GLUT_FOUND AND GLEW_FOUND))
message(FATAL_ERROR "Some needed libraries were not found. Try running:\n\tsudo apt install freeglut3-dev libglew-dev libxmu-dev libxi-dev")
endif ()
message(STATUS "GLEW and FreeGlut need to be installed.\nRun:\tsudo apt install freeglut3-dev libglew-dev libxmu-dev libxi-dev")
endif ()

include_directories(${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS})
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions src/Camera/LookAt.h → src/Engine/Camera/LookAt.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@

namespace ThreeEngine {

class LookAt : public Matrix{
class LookAt : public Matrix {
public:
Vector eye, center, up, view, v, side, s, u;

LookAt(Vector eye, Vector center, Vector up);
LookAt(Matrix other);

explicit LookAt(Matrix other);

void Set(Vector eye, Vector center, Vector up);

void SetM(Vector eye, Vector v, Vector s, Vector u);
};

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/Debug.h → src/Engine/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ namespace ThreeEngine {
std::unique_ptr<char[]> formatted;
va_list ap;
while (1) {
formatted.reset(
new char[n]); /* Wrap the plain char array into the unique_ptr */
/* Wrap the plain char array into the unique_ptr */
formatted.reset(new char[n]);
#if OS_WIN
strcpy_s(&formatted[0], n, fmt_str.c_str());
#else
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions src/Math/Maths.cpp → src/Engine/Math/Maths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ namespace ThreeEngine {
number Maths::ToRadians(number degrees) {
return degrees * Maths::PI / 180.0f;
}

number Maths::ToDegrees(number radians) {
return radians * 180.0f / Maths::PI;
}
}
6 changes: 4 additions & 2 deletions src/Math/Maths.h → src/Engine/Math/Maths.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ namespace ThreeEngine {
typedef float number;

/// Epsilon is the difference between 1.0 and the next value representable by the floating-point.
const float Epsilon = static_cast<number>(1.0e-5);
const number Epsilon = 1.0e-5f;
//const float Epsilon = std::numeric_limits<number>::epsilon();

/// Units in the Last Place to take into account in a floating-point type.
const float ULP = 3.0f;
const number ULP = 3.0f;

struct Maths {
enum Axis { X, Y, Z, W };
Expand All @@ -32,6 +32,8 @@ namespace ThreeEngine {

static number ToRadians(number degrees);

static number ToDegrees(number radians);

private:
Maths() = default;
};
Expand Down
2 changes: 1 addition & 1 deletion src/Math/Matrix.cpp → src/Engine/Math/Matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace ThreeEngine {
Reset(0.0f);
}

Matrix::Matrix(number in) {
Matrix::Matrix(const number& in) {
Reset(in);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Math/Matrix.h → src/Engine/Math/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace ThreeEngine {

Matrix();

explicit Matrix(number in);
explicit Matrix(const number& in);

explicit Matrix(Matrix2 m);

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
204 changes: 204 additions & 0 deletions src/Engine/Math/Quat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*
* File Quat.cpp in project ThreeEngine
*
* Copyright (C) Ricardo Rodrigues 2017 - All Rights Reserved
*/
#include "Quat.h"

using namespace std;

namespace ThreeEngine {

Quat::Quat() = default;

Quat::~Quat() = default;

Quat::Quat(number InX, number InY, number InZ, number InW) : T(InX), X(InY), Y(InZ), Z(InW) { }

Quat::Quat(const Quat& Q) {
operator=(Q);
}

Quat& Quat::operator=(const Quat& Other) = default;

Quat::Quat(const Vector& v) : T(0), X(v.X), Y(v.Y), Z(v.Z) { }

void Quat::Clean() {
if (abs(T) < Epsilon) { T = 0; }
if (abs(X) < Epsilon) { X = 0; }
if (abs(Y) < Epsilon) { Y = 0; }
if (abs(Z) < Epsilon) { Z = 0; }
}

number Quat::DistSquared() const {
return T * T + X * X + Y * Y + Z * Z;
}

number Quat::Dist() const {
return sqrt(DistSquared());
}

Quat Quat::Normalize() const {
number s = 1 / Dist();
return (*this) * s;
}

Quat Quat::Conjugate() const {
return Quat(T, -X, -Y, -Z);
}

Quat Quat::Inverse() const {
return Conjugate() * (1.0f / DistSquared());
}

Quat Quat::operator*(const number s) const {
return Quat(
T * s,
X * s,
Y * s,
Z * s
);
}

Quat Quat::operator*(const Quat& q) const {
return Quat(
T * q.T - X * q.X - Y * q.Y - Z * q.Z,
T * q.X + X * q.T + Y * q.Z - Z * q.Y,
T * q.Y + Y * q.T + Z * q.X - X * q.Z,
T * q.Z + Z * q.T + X * q.Y - Y * q.X
);
}

Quat Quat::operator+(const Quat& q) const {
return Quat(
T + q.T,
X + q.X,
Y + q.Y,
Z + q.Z
);
}

Quat operator*(const number& value, const Quat& quat) {
return quat * value;
}

bool Quat::operator==(const Quat& q) const {
return (abs(T - q.T) < Epsilon && abs(X - q.X) < Epsilon &&
abs(Y - q.Y) < Epsilon && abs(q.Z - q.Z) < Epsilon);
}

Matrix Quat::ToMatrix() {

Quat qN = Normalize();

number xx = qN.X * qN.X;
number xy = qN.X * qN.Y;
number xz = qN.X * qN.Z;
number xt = qN.X * qN.T;
number yy = qN.Y * qN.Y;
number yz = qN.Y * qN.Z;
number yt = qN.Y * qN.T;
number zz = qN.Z * qN.Z;
number zt = qN.Z * qN.T;

return {
1.0f - 2.0f * (yy + zz), 2.0f * (xy - zt), 2.0f * (xz + yt), 0,
2.0f * (xy + zt), 1.0f - 2.0f * (xx + zz), 2.0f * (yz - xt), 0,
2.0f * (xz - yt), 2.0f * (yz + xt), 1.0f - 2.0f * (xx + yy), 0,
0, 0, 0, 1
};
}

Quat Quat::FromAngleAxis(number theta, Vector axis) {
Vector axisN = Vector::Normalize(axis);

Quat q;
number angle = Maths::ToRadians(theta);
q.T = cos(angle / 2.0f);
number s = sin(angle / 2.0f);
q.X = axisN.X * s;
q.Y = axisN.Y * s;
q.Z = axisN.Z * s;
q.Clean();

return q.Normalize();
}

Quat Quat::FromAngleAxis(number theta, Vector4 axis) {
Vector v = axis;
return FromAngleAxis(theta, v);
}

void Quat::ToAngleAxis(const Quat& q, number& theta, Vector& axis) {
Quat qN = q.Normalize();
theta = Maths::ToDegrees(2.0f * acos(qN.T));

number s = sqrt(1.0f - qN.T * qN.T);
if (s < Epsilon) {
axis.X = 1.0f;
axis.Y = 0.0f;
axis.Z = 0.0f;
} else {
axis.X = qN.X / s;
axis.Y = qN.Y / s;
axis.Z = qN.Z / s;
}
}

void Quat::ToAngleAxis(const Quat& q, number& theta, Vector4& axis) {
Quat qN = q.Normalize();
theta = Maths::ToDegrees(2.0f * acos(qN.T));

number s = sqrt(1.0f - qN.T * qN.T);
if (s < Epsilon) {
axis.X = 1.0f;
axis.Y = 0.0f;
axis.Z = 0.0f;
axis.W = 1.0f;
} else {
axis.X = qN.X / s;
axis.Y = qN.Y / s;
axis.Z = qN.Z / s;
axis.W = 1.0f;
}
}

Quat Quat::Lerp(const Quat& q0, const Quat& q1, number time) {
number cos_angle = q0.X * q1.X + q0.Y * q1.Y + q0.Z * q1.Z + q0.T * q1.T;
number t1 = (cos_angle > 0) ? time : -time;

return ((1 - time) * q0 + t1 * q1).Normalize();
}

Quat Quat::Slerp(const Quat& q0, const Quat& q1, number time) {
number angle = acos(q0.X * q1.X + q0.Y * q1.Y + q0.Z * q1.Z + q0.T * q1.T);
number t0 = sin((1 - time) * angle) / sin(angle);
number t1 = sin(time * angle) / sin(angle);

return (t0 * q0 + t1 * q1);
}

bool Quat::Equals(const Quat& q0, const Quat& q1) {
return q0 == q1;
}

Quat::operator std::string() {
return "(" + to_string(T) + ", " + to_string(X) + ", " + to_string(Y) + ", " +
to_string(Z) + ")";
}

std::string Quat::ToStringAngleAxis() {

float thetaf;
Vector axis_f;
ToAngleAxis(*this, thetaf, axis_f);

return "[ angle = " + to_string(thetaf) + " axis = " + static_cast<string>(axis_f) + " ]";
}

std::ostream& operator<<(std::ostream& os, const Quat& quat) {
os << "(" << quat.T << ", " << quat.X << ", " << quat.Y << ", " << quat.Z << ")";
return os;
}

} /* namespace Divisaction */
Loading

0 comments on commit 74748bf

Please sign in to comment.