diff --git a/include/Orange/Math/Angle.h b/include/Orange/Math/Angle.h new file mode 100644 index 0000000..5e56b30 --- /dev/null +++ b/include/Orange/Math/Angle.h @@ -0,0 +1,77 @@ +#pragma once + +#include +#include + +namespace orange +{ + namespace detail + { + template + class Radian; + + template + class Degree : public Unit + { + public: + Degree() {} + + explicit Degree(T value) + : Unit(value) + { + } + + Degree(Unit value) + : Unit(value) + { + } + + template + explicit Degree(Unit value) + : Unit(value) + { + } + + Degree(Unit value); + }; + + template + class Radian : public Unit + { + public: + Radian() {} + + explicit Radian(T value) + : Unit(value) + { + } + + Radian(Unit value) + : Unit(value) + { + } + template + explicit Radian(Unit value) + : Unit(value) + { + } + + Radian(Unit value); + }; + + template + Degree::Degree(Unit value) + : Unit(T(360) * T(value) / Math::Tau) + { + } + + template + Radian::Radian(Unit value) + : Unit(T(value) * Math::Tau / T(360)) + { + } + } + + using Radian = detail::Radian; + using Degree = detail::Degree; +} \ No newline at end of file diff --git a/include/Orange/Math/Common.h b/include/Orange/Math/Common.h new file mode 100644 index 0000000..1f33c80 --- /dev/null +++ b/include/Orange/Math/Common.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +namespace orange::Math +{ + inline float sin(const Radian& theta) { return sinf(static_cast(theta)); } + inline float cos(const Radian& theta) { return cosf(static_cast(theta)); } + inline float tan(const Radian& theta) { return tanf(static_cast(theta)); } + + template + constexpr T kroneckerDelta(const T& i, const T& j) + { + return i == j ? T{1} : T{0}; + } +} diff --git a/include/Orange/Math/Constants.h b/include/Orange/Math/Constants.h new file mode 100644 index 0000000..31d7305 --- /dev/null +++ b/include/Orange/Math/Constants.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +namespace orange::Math +{ + static constexpr float Epsilon = std::numeric_limits::epsilon(); + + static constexpr float Zero = 0.0f; + static constexpr float One = 1.0f; + static constexpr float Third = 0.33333333f; + static constexpr float TwoThirds = 0.66666667f; + + static constexpr float E = 2.718281828f; + static constexpr float Pi = 3.141592654f; + static constexpr float Tau = 6.283185307f; + + static constexpr float Sqrt2 = 1.414213562f; + static constexpr float Sqrt3 = 1.732050808f; + + static constexpr float GoldenRatio = 1.618033989f; +} diff --git a/include/Orange/Math/Matrix.h b/include/Orange/Math/Matrix.h index afd4d23..6b9ab16 100644 --- a/include/Orange/Math/Matrix.h +++ b/include/Orange/Math/Matrix.h @@ -1,6 +1,7 @@ #pragma once #include +#include namespace orange { @@ -19,6 +20,16 @@ namespace orange } } + constexpr Matrix(const RowVector& scale) + { + for (size_t i = 0; i < Rows; i++) + { + RowVector vector{}; + vector[i] = scale[i]; + data[i] = vector; + } + } + template constexpr Matrix(const Args&... args) : data {{ args... }} @@ -245,4 +256,6 @@ namespace orange return x.TransformResult(y, math::Multiply); } + using mat4 = Matrix; + } diff --git a/include/Orange/Math/Transformation.h b/include/Orange/Math/Transformation.h new file mode 100644 index 0000000..29411b2 --- /dev/null +++ b/include/Orange/Math/Transformation.h @@ -0,0 +1,34 @@ +#pragma once + +#include + +namespace orange +{ + mat4 translate(const vec3& v) + { + mat4 result; + result[3] = vec4{ v[0], v[1], v[2], 1.0f }; + return result; + } + + mat4 scale(const vec3& v) + { + return mat4{vec4{v[0], v[1], v[2], 1.0f}}; + } + + mat4 perspective(const Radian& fovy, float aspect, float zNear, float zFar) + { + Assert(fabsf(aspect - Math::Epsilon) > 0.0f && "Math::perspective `fovy` is 0/inf."); + + const float tanHalfFovy = Math::tan(0.5f * fovy); + + mat4 result{0.0f}; + result[0][0] = 1.0f / (aspect * tanHalfFovy); + result[1][1] = 1.0f / (tanHalfFovy); + result[2][2] = -(zFar + zNear) / (zFar - zNear); + result[2][3] = -1.0f; + result[3][2] = -2.0f * zFar * zNear / (zFar - zNear); + + return result; + } +} diff --git a/include/Orange/Math/Unit.h b/include/Orange/Math/Unit.h new file mode 100644 index 0000000..5beaa80 --- /dev/null +++ b/include/Orange/Math/Unit.h @@ -0,0 +1,113 @@ +#pragma once + +namespace orange +{ + + template