Compare commits

..

9 Commits

Author SHA1 Message Date
eeb6e40909 Merge pull request #35 from orange-cpp/u/orange-cpp/imrpoved-color
U/orange cpp/imrpoved color
2025-03-29 17:43:36 +03:00
d72ad663cd added new methods 2025-03-29 05:41:55 +03:00
3e75d32f59 fixed style 2025-03-29 04:00:35 +03:00
a8ce5cbaa0 added vcpkg imgui package auto link 2025-03-29 01:56:09 +03:00
79f76a0755 added new option 2025-03-29 01:53:04 +03:00
5773cc7798 improved imgui handling 2025-03-24 21:48:51 +03:00
b6b0184523 fixed for clang 2025-03-24 06:48:51 +03:00
a797dd134a Update README.md 2025-03-24 06:46:02 +03:00
dc43411bd2 fixed unity view matrix building 2025-03-24 06:30:09 +03:00
8 changed files with 170 additions and 97 deletions

View File

@@ -11,6 +11,7 @@ option(OMATH_BUILD_AS_SHARED_LIBRARY "Build Omath as .so or .dll" OFF)
option(OMATH_USE_AVX2 "Omath will use AVX2 to boost performance" ON)
option(OMATH_IMGUI_INTEGRATION "Omath will define method to convert omath types to imgui types" OFF)
option(OMATH_BUILD_EXAMPLES "Build example projects with you can learn & play" OFF)
option(OMATH_STATIC_MSVC_RUNTIME_LIBRARY "Force Omath to link static runtime" OFF)
if (OMATH_BUILD_AS_SHARED_LIBRARY)
add_library(omath SHARED source/Matrix.cpp)
@@ -22,6 +23,21 @@ add_library(omath::omath ALIAS omath)
if (OMATH_IMGUI_INTEGRATION)
target_compile_definitions(omath PUBLIC OMATH_IMGUI_INTEGRATION)
# IMGUI is being linked as submodule
if(TARGET imgui)
target_link_libraries(omath PUBLIC imgui)
install(TARGETS imgui
EXPORT omathTargets
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)
else()
# Assume that IMGUI linked via VCPKG.
find_package(imgui CONFIG REQUIRED)
target_link_libraries(omath PUBLIC imgui::imgui)
endif()
endif()
if (OMATH_USE_AVX2)
@@ -36,6 +52,12 @@ set_target_properties(omath PROPERTIES
CXX_STANDARD 23
CXX_STANDARD_REQUIRED ON)
if (OMATH_STATIC_MSVC_RUNTIME_LIBRARY)
set_target_properties(omath PROPERTIES
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
)
endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(omath PRIVATE -mavx2 -mfma)
endif()

View File

@@ -20,6 +20,14 @@ Oranges's Math Library (omath) is a comprehensive, open-source library aimed at
- **Collision Detection**: Production ready code to handle collision detection by using simple interfaces.
- **No Additional Dependencies**: No additional dependencies need to use OMath except unit test execution
- **Ready for meta-programming**: Omath use templates for common types like Vectors, Matrixes etc, to handle all types!
|ENGINE |SUPPORT|
|--------|-------|
|Source |✅YES |
|Unity |✅YES |
|IWEngine|✅YES |
|Unreal |❌NO |
## ⏬ Getting Started
### Prerequisites
- C++ Compiler

View File

@@ -12,111 +12,153 @@ namespace omath
{
struct HSV
{
float m_hue{};
float m_saturation{};
float m_value{};
float hue{};
float saturation{};
float value{};
};
class Color final : public Vector4<float>
{
public:
constexpr Color(const float r, const float g, const float b, const float a) : Vector4(r,g,b,a)
public:
constexpr Color(const float r, const float g, const float b, const float a) : Vector4(r, g, b, a)
{
Clamp(0.f, 1.f);
}
constexpr explicit Color() = default;
[[nodiscard]]
constexpr static Color FromRGBA(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a)
{
return Color{Vector4(r, g, b, a) / 255.f};
}
[[nodiscard]]
constexpr static Color FromHSV(float hue, const float saturation, const float value)
{
float r{}, g{}, b{};
hue = std::clamp(hue, 0.f, 1.f);
const int i = static_cast<int>(hue * 6.f);
const float f = hue * 6 - i;
const float p = value * (1 - saturation);
const float q = value * (1 - f * saturation);
const float t = value * (1 - (1 - f) * saturation);
switch (i % 6)
{
Clamp(0.f, 1.f);
case 0:
r = value, g = t, b = p;
break;
case 1:
r = q, g = value, b = p;
break;
case 2:
r = p, g = value, b = t;
break;
case 3:
r = p, g = q, b = value;
break;
case 4:
r = t, g = p, b = value;
break;
case 5:
r = value, g = p, b = q;
break;
default:
return {0.f, 0.f, 0.f, 0.f};
}
constexpr explicit Color() : Vector4()
{
return {r, g, b, 1.f};
}
}
[[nodiscard]]
constexpr static Color FromRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
return Color{Vector4(r, g, b, a) / 255.f};
}
[[nodiscard]]
constexpr static Color FromHSV(const HSV& hsv)
{
return FromHSV(hsv.hue, hsv.saturation, hsv.value);
}
[[nodiscard]]
constexpr static Color FromHSV(float hue, float saturation, float value)
{
float r{}, g{}, b{};
[[nodiscard]]
constexpr HSV ToHSV() const
{
HSV hsvData;
hue = std::clamp(hue, 0.f, 1.f);
const float& red = x;
const float& green = y;
const float& blue = z;
const int i = static_cast<int>(hue * 6.f);
const float f = hue * 6 - i;
const float p = value * (1 - saturation);
const float q = value * (1 - f * saturation);
const float t = value * (1 - (1 - f) * saturation);
switch (i % 6)
{
case 0: r = value, g = t, b = p; break;
case 1: r = q, g = value, b = p; break;
case 2: r = p, g = value, b = t; break;
case 3: r = p, g = q, b = value; break;
case 4: r = t, g = p, b = value; break;
case 5: r = value, g = p, b = q; break;
default: return {0.f, 0.f, 0.f, 0.f};
}
return {r, g, b, 1.f};
}
[[nodiscard]]
constexpr HSV ToHSV() const
{
HSV hsvData;
const float& red = x;
const float& green = y;
const float& blue = z;
const float max = std::max({red, green, blue});
const float min = std::min({red, green, blue});
const float delta = max - min;
const float max = std::max({red, green, blue});
const float min = std::min({red, green, blue});
const float delta = max - min;
if (delta == 0.f)
hsvData.m_hue = 0.f;
if (delta == 0.f)
hsvData.hue = 0.f;
else if (max == red)
hsvData.m_hue = 60.f * (std::fmodf(((green - blue) / delta), 6.f));
else if (max == green)
hsvData.m_hue = 60.f * (((blue - red) / delta) + 2.f);
else if (max == blue)
hsvData.m_hue = 60.f * (((red - green) / delta) + 4.f);
else if (max == red)
hsvData.hue = 60.f * (std::fmodf(((green - blue) / delta), 6.f));
else if (max == green)
hsvData.hue = 60.f * (((blue - red) / delta) + 2.f);
else if (max == blue)
hsvData.hue = 60.f * (((red - green) / delta) + 4.f);
if (hsvData.m_hue < 0.f)
hsvData.m_hue += 360.f;
if (hsvData.hue < 0.f)
hsvData.hue += 360.f;
hsvData.m_hue /= 360.f;
hsvData.m_saturation = max == 0.f ? 0.f : delta / max;
hsvData.m_value = max;
hsvData.hue /= 360.f;
hsvData.saturation = max == 0.f ? 0.f : delta / max;
hsvData.value = max;
return hsvData;
}
return hsvData;
}
constexpr explicit Color(const Vector4& vec) : Vector4(vec)
{
Clamp(0.f, 1.f);
}
constexpr explicit Color(const Vector4& vec) : Vector4(vec)
{
Clamp(0.f, 1.f);
}
consteval void SetHue(const float hue)
{
auto hsv = ToHSV();
hsv.hue = hue;
[[nodiscard]]
constexpr Color Blend(const Color& other, float ratio) const
{
return Color( (*this * (1.f - ratio)) + (other * ratio) );
}
*this = FromHSV(hsv);
}
[[nodiscard]] static constexpr Color Red() {return {1.f, 0.f, 0.f, 1.f};}
[[nodiscard]] static constexpr Color Green() {return {0.f, 1.f, 0.f, 1.f};}
[[nodiscard]] static constexpr Color Blue() {return {0.f, 0.f, 1.f, 1.f};}
consteval void SetSaturation(const float saturation)
{
auto hsv = ToHSV();
hsv.saturation = saturation;
*this = FromHSV(hsv);
}
consteval void SetValue(const float value)
{
auto hsv = ToHSV();
hsv.value = value;
*this = FromHSV(hsv);
}
[[nodiscard]]
constexpr Color Blend(const Color& other, float ratio) const
{
ratio = std::clamp(ratio, 0.f, 1.f);
return Color(*this * (1.f - ratio) + other * ratio);
}
[[nodiscard]] static constexpr Color Red()
{
return {1.f, 0.f, 0.f, 1.f};
}
[[nodiscard]] static constexpr Color Green()
{
return {0.f, 1.f, 0.f, 1.f};
}
[[nodiscard]] static constexpr Color Blue()
{
return {0.f, 0.f, 1.f, 1.f};
}
};
[[nodiscard]]
constexpr Color Blend(const Color& first, const Color& second, float ratio)
{
return Color{first * (1.f - std::clamp(ratio, 0.f, 1.f)) + second * ratio};
}
}
} // namespace omath

View File

@@ -2,7 +2,7 @@
// Created by vlad on 2/4/24.
//
#include "omath/Color.hpp"
#include "omath/color.hpp"
#include <algorithm>
#include <cmath>

View File

@@ -7,7 +7,7 @@
namespace omath::unity_engine
{
Vector3<float> unity_engine::ForwardVector(const ViewAngles& angles)
Vector3<float> ForwardVector(const ViewAngles& angles)
{
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsForward);
@@ -27,7 +27,7 @@ namespace omath::unity_engine
}
Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
{
return MatCameraView<float, MatStoreType::ROW_MAJOR>(ForwardVector(angles), RightVector(angles),
return MatCameraView<float, MatStoreType::ROW_MAJOR>(ForwardVector(angles), -RightVector(angles),
UpVector(angles), cam_origin);
}
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,

View File

@@ -1,6 +1,6 @@
#include "omath/Matrix.hpp"
#include "omath/Angles.hpp"
#include "omath/Vector3.hpp"
#include "omath/matrix.hpp"
#include "omath/angles.hpp"
#include "omath/vector3.hpp"
#include <complex>

View File

@@ -51,7 +51,8 @@ TEST(UnitTestUnityEngine, Project)
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(60.f);
const auto cam = omath::unity_engine::Camera({0, 0, 0}, {}, {1280.f, 720.f}, fov, 0.03f, 1000.f);
const auto proj = cam.WorldToScreen({0.f, 2.f, 10.f});
const auto proj = cam.WorldToScreen({5.f, 3, 10.f});
std::println("{} {}", proj->x, proj->y);
}
TEST(UnitTestUnityEngine, CameraSetAndGetFov)

View File

@@ -63,9 +63,9 @@ TEST_F(UnitTestColor, FromHSV)
TEST_F(UnitTestColor, ToHSV)
{
HSV hsv = color1.ToHSV(); // Red color
EXPECT_FLOAT_EQ(hsv.m_hue, 0.0f);
EXPECT_FLOAT_EQ(hsv.m_saturation, 1.0f);
EXPECT_FLOAT_EQ(hsv.m_value, 1.0f);
EXPECT_FLOAT_EQ(hsv.hue, 0.0f);
EXPECT_FLOAT_EQ(hsv.saturation, 1.0f);
EXPECT_FLOAT_EQ(hsv.value, 1.0f);
}
// Test color blending
@@ -106,7 +106,7 @@ TEST_F(UnitTestColor, BlendVector3)
{
constexpr Color v1(1.0f, 0.0f, 0.0f, 1.f); // Red
constexpr Color v2(0.0f, 1.0f, 0.0f, 1.f); // Green
constexpr Color blended = Blend(v1, v2, 0.5f);
constexpr Color blended = v1.Blend(v2, 0.5f);
EXPECT_FLOAT_EQ(blended.x, 0.5f);
EXPECT_FLOAT_EQ(blended.y, 0.5f);
EXPECT_FLOAT_EQ(blended.z, 0.0f);