Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ option(OMATH_BUILD_AS_SHARED_LIBRARY "Build Omath as .so or .dll" OFF)
if (OMATH_BUILD_AS_SHARED_LIBRARY)
add_library(omath SHARED source/Vector3.cpp)
else()
add_library(omath STATIC source/Vector3.cpp
include/omath/engines/OpenGL/Constants.hpp
include/omath/engines/OpenGL/Formulas.hpp
include/omath/engines/OpenGL/Camera.hpp)
add_library(omath STATIC source/Matrix.cpp)
endif()

set_target_properties(omath PROPERTIES
Expand Down
4 changes: 2 additions & 2 deletions include/omath/Color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ namespace omath
};


class Color final : public Vector4
class Color final : public Vector4<float>
{
public:
constexpr Color(float r, float g, float b, float a) : Vector4(r,g,b,a)
constexpr Color(const float r, const float g, const float b, const float a) : Vector4(r,g,b,a)
{
Clamp(0.f, 1.f);
}
Expand Down
10 changes: 5 additions & 5 deletions include/omath/Mat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,21 +333,21 @@ namespace omath

template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
[[nodiscard]]
constexpr static Mat<1, 4, Type, St> MatRowFromVector(const Vector3& vector) noexcept
constexpr static Mat<1, 4, Type, St> MatRowFromVector(const Vector3<Type>& vector) noexcept
{
return {{vector.x, vector.y, vector.z, 1}};
}

template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
[[nodiscard]]
constexpr static Mat<4, 1, Type, St> MatColumnFromVector(const Vector3& vector) noexcept
constexpr static Mat<4, 1, Type, St> MatColumnFromVector(const Vector3<Type>& vector) noexcept
{
return {{vector.x}, {vector.y}, {vector.z}, {1}};
}

template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
[[nodiscard]]
constexpr Mat<4, 4, Type, St> MatTranslation(const Vector3& diff) noexcept
constexpr Mat<4, 4, Type, St> MatTranslation(const Vector3<Type>& diff) noexcept
{
return
{
Expand Down Expand Up @@ -399,8 +399,8 @@ namespace omath

template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
[[nodiscard]]
static Mat<4, 4, Type, St> MatCameraView(const Vector3& forward, const Vector3& right, const Vector3& up,
const Vector3& cameraOrigin) noexcept
static Mat<4, 4, Type, St> MatCameraView(const Vector3<Type>& forward, const Vector3<Type>& right,
const Vector3<Type>& up, const Vector3<Type>& cameraOrigin) noexcept
{
return Mat<4, 4, Type, St>
{
Expand Down
6 changes: 3 additions & 3 deletions include/omath/Matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#include <initializer_list>
#include <memory>
#include <string>
#include "Vector3.hpp"

namespace omath
{
class Vector3;

class Matrix final
{
Expand All @@ -19,10 +19,10 @@ namespace omath
static Matrix ToScreenMatrix(float screenWidth, float screenHeight);

[[nodiscard]]
static Matrix TranslationMatrix(const Vector3& diff);
static Matrix TranslationMatrix(const Vector3<float>& diff);

[[nodiscard]]
static Matrix OrientationMatrix(const Vector3& forward, const Vector3& right, const Vector3& up);
static Matrix OrientationMatrix(const Vector3<float>& forward, const Vector3<float>& right, const Vector3<float>& up);

[[nodiscard]]
static Matrix ProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
Expand Down
14 changes: 7 additions & 7 deletions include/omath/Triangle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ namespace omath
{
}

Vector3 m_vertex1;
Vector3 m_vertex2;
Vector3 m_vertex3;
Vector3<float> m_vertex1;
Vector3<float> m_vertex2;
Vector3<float> m_vertex3;

[[nodiscard]]
constexpr Vector3 CalculateNormal() const
constexpr Vector3<float> CalculateNormal() const
{
const auto b = SideBVector();
const auto a = SideAVector();
Expand All @@ -41,7 +41,7 @@ namespace omath
}

[[nodiscard]]
constexpr Vector3 SideAVector() const
constexpr Vector3<float> SideAVector() const
{
return m_vertex1 - m_vertex2;
}
Expand All @@ -61,12 +61,12 @@ namespace omath
return std::abs(sideA*sideA + sideB*sideB - hypot*hypot) <= 0.0001f;
}
[[nodiscard]]
constexpr Vector3 SideBVector() const
constexpr Vector3<float> SideBVector() const
{
return m_vertex3 - m_vertex2;
}
[[nodiscard]]
constexpr Vector3 MidPoint() const
constexpr Vector3<float> MidPoint() const
{
return (m_vertex1 + m_vertex2 + m_vertex3) / 3;
}
Expand Down
36 changes: 19 additions & 17 deletions include/omath/Vector2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

namespace omath
{

template<class Type> requires std::is_arithmetic_v<Type>
class Vector2
{
public:
float x = 0.f;
float y = 0.f;
Type x = static_cast<Type>(0);
Type y = static_cast<Type>(0);

// Constructors
constexpr Vector2() = default;

constexpr Vector2(const float x, const float y) : x(x), y(y) {}
constexpr Vector2(const Type& x, const Type& y) : x(x), y(y) {}

// Equality operators
[[nodiscard]]
Expand Down Expand Up @@ -65,31 +67,31 @@ namespace omath
return *this;
}

constexpr Vector2& operator*=(const float fl)
constexpr Vector2& operator*=(const Type& fl)
{
x *= fl;
y *= fl;

return *this;
}

constexpr Vector2& operator/=(const float fl)
constexpr Vector2& operator/=(const Type& fl)
{
x /= fl;
y /= fl;

return *this;
}

constexpr Vector2& operator+=(const float fl)
constexpr Vector2& operator+=(const Type& fl)
{
x += fl;
y += fl;

return *this;
}

constexpr Vector2& operator-=(const float fl)
constexpr Vector2& operator-=(const Type& fl)
{
x -= fl;
y -= fl;
Expand All @@ -98,45 +100,45 @@ namespace omath
}

// Basic vector operations
[[nodiscard]] float DistTo(const Vector2& vOther) const
[[nodiscard]] Type DistTo(const Vector2& vOther) const
{
return std::sqrt(DistToSqr(vOther));
}

[[nodiscard]] constexpr float DistToSqr(const Vector2& vOther) const
[[nodiscard]] constexpr Type DistToSqr(const Vector2& vOther) const
{
return (x - vOther.x) * (x - vOther.x) + (y - vOther.y) * (y - vOther.y);
}

[[nodiscard]] constexpr float Dot(const Vector2& vOther) const
[[nodiscard]] constexpr Type Dot(const Vector2& vOther) const
{
return x * vOther.x + y * vOther.y;
}

#ifndef _MSC_VER
[[nodiscard]] constexpr float Length() const
[[nodiscard]] constexpr Type& Length() const
{
return std::hypot(x, y);
}

[[nodiscard]] constexpr Vector2 Normalized() const
{
const float len = Length();
const Type len = Length();
return len > 0.f ? *this / len : *this;
}
#else
[[nodiscard]] float Length() const
[[nodiscard]] Type Length() const
{
return std::hypot(x, y);
}

[[nodiscard]] Vector2 Normalized() const
{
const float len = Length();
const Type len = Length();
return len > 0.f ? *this / len : *this;
}
#endif
[[nodiscard]] constexpr float LengthSqr() const
[[nodiscard]] constexpr Type LengthSqr() const
{
return x * x + y * y;
}
Expand Down Expand Up @@ -187,13 +189,13 @@ namespace omath
}

// Sum of elements
[[nodiscard]] constexpr float Sum() const
[[nodiscard]] constexpr Type Sum() const
{
return x + y;
}

[[nodiscard]]
constexpr std::tuple<float, float> AsTuple() const
constexpr std::tuple<Type, Type> AsTuple() const
{
return std::make_tuple(x, y);
}
Expand Down
Loading