-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from petiaccja/examples
examples
- Loading branch information
Showing
10 changed files
with
364 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
add_executable(Examples) | ||
|
||
|
||
target_sources(Examples | ||
PRIVATE | ||
"main.cpp" | ||
"Math.hpp" | ||
"Camera.hpp" | ||
"Camera.cpp" | ||
"Object.hpp" | ||
"Object.cpp" | ||
) | ||
|
||
|
||
target_link_libraries(Examples Mathter) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "Camera.hpp" | ||
|
||
#include <Mathter/Transforms.hpp> | ||
#include <Mathter/Utility.hpp> | ||
|
||
|
||
void Camera::SetEye(const Vec3f& eye) { | ||
m_eye = eye; | ||
} | ||
|
||
|
||
const Vec3f& Camera::GetEye() const { | ||
return m_eye; | ||
} | ||
|
||
|
||
void Camera::SetTarget(const Vec3f& target) { | ||
m_target = target; | ||
} | ||
|
||
|
||
const Vec3f& Camera::GetTarget() const { | ||
return m_target; | ||
} | ||
|
||
|
||
void Camera::SetUp(const Vec3f& up) { | ||
m_up = up; | ||
} | ||
|
||
|
||
const Vec3f& Camera::GetUp() const { | ||
return m_up; | ||
} | ||
|
||
|
||
void Camera::SetFoVH(float fovH) { | ||
m_fovH = fovH; | ||
} | ||
|
||
|
||
float Camera::GetFoVH() const { | ||
return m_fovH; | ||
} | ||
|
||
|
||
void Camera::SetAspect(float aspect) { | ||
m_aspect = aspect; | ||
} | ||
|
||
|
||
float Camera::GetAspect() const { | ||
return m_aspect; | ||
} | ||
|
||
|
||
Mat44f Camera::GetViewTransform() const { | ||
// This creates a left-handed camera space where the Z axis is forward, | ||
// the X axis if right, and the Y axis is up. | ||
return LookAt(m_eye, m_target, m_up, true, false, false); | ||
} | ||
|
||
|
||
Mat44f Camera::GetPerspectiveTransform() const { | ||
// This projects the camera space to normalized device coordinates. | ||
// If the camera space used negative Z forward, the near and far planes should also be negative. | ||
// The normalized device coordinates are X in [-1, 1], Y in [-1, 1], and Z in [0, 1]. | ||
return mathter::Perspective(mathter::Deg2Rad(m_fovH), m_aspect, 0.1f, 100.f, 0.0f, 1.0f); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include "Math.hpp" | ||
|
||
|
||
/// <summary> A camera object that help with view and projective transforms. </summary> | ||
class Camera { | ||
public: | ||
void SetEye(const Vec3f& eye); | ||
const Vec3f& GetEye() const; | ||
|
||
void SetTarget(const Vec3f& target); | ||
const Vec3f& GetTarget() const; | ||
|
||
void SetUp(const Vec3f& eye); | ||
const Vec3f& GetUp() const; | ||
|
||
void SetFoVH(float fovH); | ||
float GetFoVH() const; | ||
|
||
void SetAspect(float aspect); | ||
float GetAspect() const; | ||
|
||
Mat44f GetViewTransform() const; | ||
Mat44f GetPerspectiveTransform() const; | ||
|
||
private: | ||
Vec3f m_eye = { 0, 0, 0 }; | ||
Vec3f m_target = { 1, 0, 0 }; | ||
Vec3f m_up = { 0, 0, 1 }; | ||
float m_fovH = 75.0f; | ||
float m_aspect = 16.0f / 9.0f; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
#include <Mathter/Geometry/Triangle.hpp> | ||
#include <Mathter/Matrix.hpp> | ||
#include <Mathter/Quaternion.hpp> | ||
#include <Mathter/Vector.hpp> | ||
|
||
|
||
// Type aliases for Mathter's types, defining the Example project's conventions. | ||
using Vec2i = mathter::Vector<int, 2, false>; | ||
using Vec2f = mathter::Vector<float, 2, false>; | ||
using Vec3f = mathter::Vector<float, 3, false>; | ||
using Mat44f = mathter::Matrix<float, 4, 4, mathter::eMatrixOrder::FOLLOW_VECTOR, mathter::eMatrixLayout::ROW_MAJOR, false>; | ||
using Quatf = mathter::Quaternion<float, mathter::eQuaternionLayout::SCALAR_FIRST, false>; | ||
|
||
using Triangle3f = mathter::Triangle<float, 3>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "Object.hpp" | ||
|
||
#include "Mathter/Transforms.hpp" | ||
|
||
|
||
Mat44f Object::GetTransform() const { | ||
// The scaling and translation transforms must be converted to Mat44 before they can be used for multiplication. | ||
// Since we defined Mat44 to have FOLLOW_VECTOR multiplication order, we're concatenating | ||
// the transforms from left to right. | ||
return Mat44f(Scale(scale)) * Mat44f(rotation) * Mat44f(Translation(position)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#include "Math.hpp" | ||
|
||
#include <vector> | ||
|
||
|
||
/// <summary> A polygonal 3D geometry consisting of triangles. </summary> | ||
struct Object { | ||
std::vector<Triangle3f> triangles; | ||
|
||
Vec3f scale; | ||
Quatf rotation; | ||
Vec3f position; | ||
|
||
Mat44f GetTransform() const; | ||
}; |
Oops, something went wrong.