mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 23:13:26 +00:00
Compare commits
146 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 69b9049fb0 | |||
| 2734b58bdd | |||
| d7f1f49165 | |||
| 94b1453cae | |||
| 3e67d8a99c | |||
| 8c00ab3d9d | |||
| d14cb1e93e | |||
| c692cf39e1 | |||
| 9c934c5d9c | |||
|
|
f8202b116d | ||
| 8bf0bb8e0d | |||
| a340766348 | |||
| 254674a62e | |||
| 97c2da893b | |||
| 0ce30a7038 | |||
| 492ddfd566 | |||
| baf7ee8f88 | |||
| 9fde11733f | |||
| 0069b8bd96 | |||
| 127bae0b78 | |||
| bed204a663 | |||
| 3f6ea010dc | |||
| 592a98f38c | |||
| 7873047550 | |||
| 1601f3cbc8 | |||
| 2180f8ab97 | |||
| b613ff9ef1 | |||
| 145eadfffa | |||
| 14acebad5f | |||
| 4a7a631932 | |||
| e08c22f604 | |||
| 1b47f45af9 | |||
| 466d8f7bec | |||
| 3631c5d698 | |||
| b58956efe3 | |||
| fc1e0c62b8 | |||
|
|
8e861b8a85 | ||
|
|
55085604fd | ||
|
|
7b712ed960 | ||
| 138c996393 | |||
| 0f2a858306 | |||
| ea6c1cc929 | |||
| eeb6e40909 | |||
| d72ad663cd | |||
| 3e75d32f59 | |||
| a8ce5cbaa0 | |||
| 79f76a0755 | |||
| 5773cc7798 | |||
| b6b0184523 | |||
| a797dd134a | |||
| dc43411bd2 | |||
| 8959161904 | |||
| 5d5bd215b2 | |||
| a48a257648 | |||
| ffba4e256a | |||
| d9219cdddb | |||
| 5a1014a239 | |||
| 74f2241bcf | |||
| 8977557a61 | |||
| 481d7b85df | |||
| 2b59fb6aa2 | |||
| 4f037a1952 | |||
| ef11183c3f | |||
| 326d8baaae | |||
| e46067b0b9 | |||
| 68ac7f7b3d | |||
| 17de6d407c | |||
| 59d686e252 | |||
| 713af1b772 | |||
| a8922230b3 | |||
| b8d79eb8b4 | |||
| 5acd166d8f | |||
| c7dda0ff10 | |||
| 2688d977a9 | |||
|
|
b9ac44a901 | ||
| 35658b1f6d | |||
| 8f4b61319f | |||
| 832c2ea5ef | |||
| 50c336e044 | |||
|
|
e80e22bd5b | ||
| 27576ed761 | |||
| f85243e892 | |||
| 9b6d0beb03 | |||
| ee9829af22 | |||
| cd452b0397 | |||
| 2fa0c500a7 | |||
| 0740d0778c | |||
| f5c271cfa6 | |||
| 064a31f527 | |||
| e5d0adf247 | |||
| 8fc107ec0f | |||
| 6dd72d2448 | |||
| c76f6e91b0 | |||
| d1d06c24ca | |||
| 169011e238 | |||
| 934ca0da0a | |||
| 4200ef63a6 | |||
| 58e2c3b5b7 | |||
|
|
c3202a3bc3 | ||
|
|
39ab9d065d | ||
|
|
ed372a1c78 | ||
| 70313f5ae0 | |||
|
|
978656b573 | ||
|
|
a0f746b84c | ||
|
|
6d0d267743 | ||
| 9ba3bc754a | |||
| 3d1844fa0e | |||
| f21d29c6c2 | |||
| 28a35d5bc9 | |||
| d9684ff73f | |||
| 900501f37e | |||
| 5639cd0eb5 | |||
| 244d01c313 | |||
| e31ffac103 | |||
| ae87257adf | |||
| 906f5099d1 | |||
| 96e4e1c9d6 | |||
| 872dbe146f | |||
| e0dcb65e3f | |||
| d0c532df39 | |||
| 31907ceca3 | |||
| afcfed4834 | |||
| 835fd110ba | |||
| 29629a737d | |||
| 6a324e8c0e | |||
| 42c84f2523 | |||
| 9a4fb67289 | |||
| 824b301d40 | |||
| dcf466316c | |||
| dc0bca14c5 | |||
| b2db06a739 | |||
| cdce07f2a5 | |||
| 931937d010 | |||
| af880be056 | |||
| c3bfd12971 | |||
| 9ad8ec755b | |||
| 409cd18de6 | |||
| 001c6a8a47 | |||
| 30bc3f2c6d | |||
| 232f456a4d | |||
| 2b9575311e | |||
| e497308349 | |||
| 390e584aee | |||
| 93082d6a8d | |||
| c8c3c4e5fc | |||
|
|
24ad4ac698 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
/cmake-build/
|
||||
/.idea
|
||||
/out
|
||||
*.DS_Store
|
||||
8
.idea/modules.xml
generated
8
.idea/modules.xml
generated
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/uml.iml" filepath="$PROJECT_DIR$/.idea/uml.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
2
.idea/uml.iml
generated
2
.idea/uml.iml
generated
@@ -1,2 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
||||
8
.idea/vcs.xml
generated
8
.idea/vcs.xml
generated
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/extlibs/glm" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/extlibs/googletest" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -1,30 +1,81 @@
|
||||
cmake_minimum_required(VERSION 3.26)
|
||||
|
||||
project(omath VERSION 1.0.0)
|
||||
project(omath VERSION 1.0.1 LANGUAGES CXX)
|
||||
|
||||
include(CMakePackageConfigHelpers)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 26)
|
||||
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
||||
|
||||
option(OMATH_BUILD_TESTS "Build unit tests" ON)
|
||||
option(OMATH_BUILD_TESTS "Build unit tests" OFF)
|
||||
option(OMATH_THREAT_WARNING_AS_ERROR "Set highest level of warnings and force compiler to treat them as errors" ON)
|
||||
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)
|
||||
option(OMATH_SUPRESS_SAFETY_CHECKS "Supress some safety checks in release build to improve general performance" ON)
|
||||
option(OMATH_USE_UNITY_BUILD "Will enable unity build to speed up compilation" ON)
|
||||
|
||||
if (OMATH_BUILD_AS_SHARED_LIBRARY)
|
||||
add_library(omath SHARED source/Vector3.cpp)
|
||||
add_library(omath SHARED source/matrix.cpp)
|
||||
else()
|
||||
add_library(omath STATIC source/Vector3.cpp)
|
||||
add_library(omath STATIC source/matrix.cpp)
|
||||
endif()
|
||||
|
||||
target_compile_definitions(omath PUBLIC OMATH_EXPORT)
|
||||
message(STATUS "Building on ${CMAKE_HOST_SYSTEM_NAME}")
|
||||
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()
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
||||
target_compile_options(omath PRIVATE /Zc:static_assert-)
|
||||
endif()
|
||||
|
||||
if (OMATH_USE_AVX2)
|
||||
target_compile_definitions(omath PUBLIC OMATH_USE_AVX2)
|
||||
endif()
|
||||
|
||||
if (OMATH_SUPRESS_SAFETY_CHECKS)
|
||||
target_compile_definitions(omath PUBLIC OMATH_SUPRESS_SAFETY_CHECKS)
|
||||
endif()
|
||||
|
||||
set_target_properties(omath PROPERTIES
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
CXX_STANDARD 23
|
||||
CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
if (OMATH_USE_UNITY_BUILD)
|
||||
set_target_properties(omath PROPERTIES
|
||||
UNITY_BUILD ON
|
||||
UNITY_BUILD_BATCH_SIZE 20)
|
||||
endif()
|
||||
|
||||
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()
|
||||
|
||||
target_compile_features(omath PUBLIC cxx_std_23)
|
||||
|
||||
|
||||
add_subdirectory(source)
|
||||
|
||||
if(OMATH_BUILD_TESTS)
|
||||
@@ -32,6 +83,10 @@ if(OMATH_BUILD_TESTS)
|
||||
add_subdirectory(tests)
|
||||
endif()
|
||||
|
||||
if (OMATH_BUILD_EXAMPLES)
|
||||
add_subdirectory(examples)
|
||||
endif()
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND OMATH_THREAT_WARNING_AS_ERROR)
|
||||
target_compile_options(omath PRIVATE /W4 /WX)
|
||||
elseif(OMATH_THREAT_WARNING_AS_ERROR)
|
||||
|
||||
@@ -64,6 +64,38 @@
|
||||
"cacheVariables": {
|
||||
"CMAKE_BUILD_TYPE": "Release"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "darwin-base",
|
||||
"hidden": true,
|
||||
"generator": "Ninja",
|
||||
"binaryDir": "${sourceDir}/cmake-build/build/${presetName}",
|
||||
"installDir": "${sourceDir}/cmake-build/install/${presetName}",
|
||||
"cacheVariables": {
|
||||
"CMAKE_C_COMPILER": "clang",
|
||||
"CMAKE_CXX_COMPILER": "clang++"
|
||||
},
|
||||
"condition": {
|
||||
"type": "equals",
|
||||
"lhs": "${hostSystemName}",
|
||||
"rhs": "Darwin"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "darwin-debug",
|
||||
"displayName": "Darwin Debug",
|
||||
"inherits": "darwin-base",
|
||||
"cacheVariables": {
|
||||
"CMAKE_BUILD_TYPE": "Debug"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "darwin-release",
|
||||
"displayName": "Darwin Release",
|
||||
"inherits": "darwin-debug",
|
||||
"cacheVariables": {
|
||||
"CMAKE_BUILD_TYPE": "Release"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
11
CREDITS.md
Normal file
11
CREDITS.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# OMATH CREDITS
|
||||
|
||||
Thanks to everyone who made this possible, including:
|
||||
|
||||
- Saikari aka luadebug for VCPKG port.
|
||||
|
||||
And a big hand to everyone else who has contributed over the past!
|
||||
|
||||
THANKS! <3
|
||||
|
||||
-- Orange++ <orange-cpp@yandex.ru>
|
||||
63
README.md
63
README.md
@@ -1,6 +1,6 @@
|
||||
<div align = center>
|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
@@ -11,21 +11,52 @@
|
||||
|
||||
Oranges's Math Library (omath) is a comprehensive, open-source library aimed at providing efficient, reliable, and versatile mathematical functions and algorithms. Developed primarily in C++, this library is designed to cater to a wide range of mathematical operations essential in scientific computing, engineering, and academic research.
|
||||
|
||||
## Features
|
||||
- **Efficiency**: Optimized for performance, ensuring quick computations.
|
||||
## 👁🗨 Features
|
||||
- **Efficiency**: Optimized for performance, ensuring quick computations using AVX2.
|
||||
- **Versatility**: Includes a wide array of mathematical functions and algorithms.
|
||||
- **Ease of Use**: Simplified interface for convenient integration into various projects.
|
||||
- **Projectile Prediction**: Projectile prediction engine with O(N) algo complexity, that can power you projectile aim-bot.
|
||||
- **3D Projection**: No need to find view-projection matrix anymore you can make your own projection pipeline.
|
||||
- **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!
|
||||
|
||||
## Getting Started
|
||||
## Supported Render Pipelines
|
||||
| ENGINE | SUPPORT |
|
||||
|----------|---------|
|
||||
| Source | ✅YES |
|
||||
| Unity | ✅YES |
|
||||
| IWEngine | ✅YES |
|
||||
| Unreal | ❌NO |
|
||||
|
||||
## Supported Operating Systems
|
||||
|
||||
| OS | SUPPORT |
|
||||
|----------------|---------|
|
||||
| Windows 10/11 | ✅YES |
|
||||
| Linux | ✅YES |
|
||||
| Darwin (MacOS) | ✅YES |
|
||||
|
||||
## ⏬ Getting Started
|
||||
### Prerequisites
|
||||
- C++ Compiler
|
||||
- CMake (for building the project)
|
||||
|
||||
### Installation
|
||||
### vcpkg
|
||||
**Note**: Support vcpkg for package management
|
||||
1. Install vcpkg (https://github.com/microsoft/vcpkg)
|
||||
2. Run the following command to install the orange-math package:
|
||||
```
|
||||
vcpkg install orange-math
|
||||
```
|
||||
CMakeLists.txt
|
||||
```cmake
|
||||
find_package(omath CONFIG REQUIRED)
|
||||
target_link_libraries(main PRIVATE omath::omath)
|
||||
```
|
||||
For detailed commands on installing different versions and more information, please refer to Microsoft's official instructions (https://learn.microsoft.com/en-us/vcpkg/get_started/overview)
|
||||
### Build from source
|
||||
1. Clone the repository:
|
||||
```
|
||||
git clone https://github.com/orange-cpp/omath.git
|
||||
@@ -37,10 +68,10 @@ Oranges's Math Library (omath) is a comprehensive, open-source library aimed at
|
||||
3. Build the project using CMake:
|
||||
```
|
||||
cmake --preset windows-release -S .
|
||||
cmake --build cmake-build/build/windows-release --target server -j 6
|
||||
cmake --build cmake-build/build/windows-release --target omath -j 6
|
||||
```
|
||||
Use **\<platform\>-\<build configuration\>** preset to build siutable version for yourself. Like **windows-release** or **linux-release**.
|
||||
## Usage
|
||||
## ❔ Usage
|
||||
Simple world to screen function
|
||||
```c++
|
||||
TEST(UnitTestProjection, IsPointOnScreen)
|
||||
@@ -51,22 +82,26 @@ TEST(UnitTestProjection, IsPointOnScreen)
|
||||
EXPECT_TRUE(proj.has_value());
|
||||
}
|
||||
```
|
||||
|
||||
## Showcase
|
||||
<details>
|
||||
<summary>OMATH for making cheats</summary>
|
||||
<summary>OMATH for making cheats (click to open)</summary>
|
||||
|
||||
With `omath/projection` module you can achieve simple ESP hack for powered by Source/Unreal/Unity engine games, like [Apex Legends](https://store.steampowered.com/app/1172470/Apex_Legends/).
|
||||
|
||||

|
||||

|
||||
|
||||
Or for InfinityWard Engine based games. Like Call of Duty Black Ops 2!
|
||||

|
||||
Or create simple trigger bot with embeded traceline from omath::collision::LineTrace
|
||||

|
||||
Or even advanced projectile aimbot
|
||||
[Watch Video](https://youtu.be/lM_NJ1yCunw?si=5E87OrQMeypxSJ3E)
|
||||
</details>
|
||||
|
||||
## Contributing
|
||||
## 🫵🏻 Contributing
|
||||
Contributions to `omath` are welcome! Please read `CONTRIBUTING.md` for details on our code of conduct and the process for submitting pull requests.
|
||||
|
||||
## License
|
||||
## 📜 License
|
||||
This project is licensed under the MIT - see the `LICENSE` file for details.
|
||||
|
||||
## Acknowledgments
|
||||
- Orange | [Telegram](https://t.me/orange_cpp)
|
||||
## 💘 Acknowledgments
|
||||
- [All contributors](https://github.com/orange-cpp/omath/graphs/contributors)
|
||||
|
||||
@@ -4,3 +4,4 @@ include(CMakeFindDependencyMacro)
|
||||
|
||||
# Load the targets for the omath library
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/omathTargets.cmake")
|
||||
check_required_components(omath)
|
||||
|
||||
4
examples/CMakeLists.txt
Normal file
4
examples/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
project(examples)
|
||||
|
||||
add_executable(ExampleProjectionMatrixBuilder example_proj_mat_builder.cpp)
|
||||
target_link_libraries(ExampleProjectionMatrixBuilder PRIVATE omath::omath)
|
||||
40
examples/example_proj_mat_builder.cpp
Normal file
40
examples/example_proj_mat_builder.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
//
|
||||
// Created by Vlad on 3/19/2025.
|
||||
//
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <print>
|
||||
#include <omath/engines/opengl_engine/formulas.hpp>
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
std::println("OMATH Projection Matrix Builder");
|
||||
|
||||
float fov = 0;
|
||||
float near = 0;
|
||||
float far = 0;
|
||||
float viewPortWidth = 0;
|
||||
float viewPortHeight = 0;
|
||||
|
||||
std::print("Enter camera fov: ");
|
||||
std::cin >> fov;
|
||||
|
||||
std::print("Enter camera z near: ");
|
||||
std::cin >> near;
|
||||
|
||||
std::print("Enter camera z far: ");
|
||||
std::cin >> far;
|
||||
|
||||
std::print("Enter camera screen width: ");
|
||||
std::cin >> viewPortWidth;
|
||||
|
||||
std::print("Enter camera screen height: ");
|
||||
std::cin >> viewPortHeight;
|
||||
|
||||
const auto mat =
|
||||
omath::opengl_engine::CalcPerspectiveProjectionMatrix(fov, viewPortWidth / viewPortHeight, near, far);
|
||||
|
||||
std::print("{}", mat.ToString());
|
||||
};
|
||||
17
include/omath/3d_primitives/box.hpp
Normal file
17
include/omath/3d_primitives/box.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by Vlad on 4/18/2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include "omath/triangle.hpp"
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
|
||||
namespace omath::primitives
|
||||
{
|
||||
[[nodiscard]]
|
||||
std::array<Triangle<Vector3<float>>, 12> CreateBox(const Vector3<float>& top, const Vector3<float>& bottom,
|
||||
const Vector3<float>& dirForward, const Vector3<float>& dirRight,
|
||||
float ratio = 4.f);
|
||||
}
|
||||
@@ -1,123 +0,0 @@
|
||||
//
|
||||
// Created by vlad on 2/4/24.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "omath/Vector3.hpp"
|
||||
#include <cstdint>
|
||||
#include "omath/Vector4.hpp"
|
||||
#include "omath/omath_export.hpp"
|
||||
|
||||
namespace omath
|
||||
{
|
||||
struct OMATH_API HSV
|
||||
{
|
||||
float m_hue{};
|
||||
float m_saturation{};
|
||||
float m_value{};
|
||||
};
|
||||
|
||||
|
||||
class OMATH_API Color final : public Vector4
|
||||
{
|
||||
public:
|
||||
constexpr Color(float r, float g, float b, float a) : Vector4(r,g,b,a)
|
||||
{
|
||||
Clamp(0.f, 1.f);
|
||||
}
|
||||
|
||||
constexpr explicit Color() : Vector4()
|
||||
{
|
||||
|
||||
}
|
||||
[[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(float hue, float saturation, 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)
|
||||
{
|
||||
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;
|
||||
|
||||
|
||||
if (delta == 0.f)
|
||||
hsvData.m_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);
|
||||
|
||||
if (hsvData.m_hue < 0.f)
|
||||
hsvData.m_hue += 360.f;
|
||||
|
||||
hsvData.m_hue /= 360.f;
|
||||
hsvData.m_saturation = max == 0.f ? 0.f : delta / max;
|
||||
hsvData.m_value = max;
|
||||
|
||||
return hsvData;
|
||||
}
|
||||
|
||||
constexpr explicit Color(const Vector4& vec) : Vector4(vec)
|
||||
{
|
||||
Clamp(0.f, 1.f);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Color Blend(const Color& other, float ratio) const
|
||||
{
|
||||
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};
|
||||
}
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
#pragma once
|
||||
#include <initializer_list>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "omath/omath_export.hpp"
|
||||
|
||||
namespace omath
|
||||
{
|
||||
class Vector3;
|
||||
|
||||
class Matrix final
|
||||
{
|
||||
public:
|
||||
OMATH_API Matrix();
|
||||
OMATH_API Matrix(size_t rows, size_t columns);
|
||||
|
||||
OMATH_API Matrix(const std::initializer_list<std::initializer_list<float>>& rows);
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API static Matrix ToScreenMatrix(float screenWidth, float screenHeight);
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API static Matrix TranslationMatrix(const Vector3& diff);
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API static Matrix OrientationMatrix(const Vector3& forward, const Vector3& right, const Vector3& up);
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API static Matrix ProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
|
||||
|
||||
OMATH_API Matrix(const Matrix& other);
|
||||
|
||||
OMATH_API Matrix(size_t rows, size_t columns, const float* pRaw);
|
||||
|
||||
OMATH_API Matrix(Matrix&& other) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API size_t RowCount() const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API size_t ColumnsCount() const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API std::pair<size_t, size_t> Size() const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API float& At(size_t iRow, size_t iCol);
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API float Sum();
|
||||
|
||||
OMATH_API void SetDataFromRaw(const float* pRawMatrix);
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API Matrix Transpose() const;
|
||||
|
||||
OMATH_API void Set(float val);
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API const float& At(size_t iRow, size_t iCol) const;
|
||||
|
||||
OMATH_API Matrix operator*(const Matrix& other) const;
|
||||
|
||||
OMATH_API Matrix& operator*=(const Matrix& other);
|
||||
|
||||
OMATH_API Matrix operator*(float f) const;
|
||||
|
||||
OMATH_API Matrix& operator*=(float f);
|
||||
|
||||
OMATH_API Matrix& operator/=(float f);
|
||||
|
||||
OMATH_API void Clear();
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API Matrix Strip(size_t row, size_t column) const;
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API float Minor(size_t i, size_t j) const;
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API float AlgComplement(size_t i, size_t j) const;
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API float Determinant() const;
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API const float* Raw() const;
|
||||
|
||||
OMATH_API Matrix& operator=(const Matrix& other);
|
||||
|
||||
OMATH_API Matrix& operator=(Matrix&& other) noexcept;
|
||||
|
||||
OMATH_API Matrix operator/(float f) const;
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API std::string ToString() const;
|
||||
|
||||
OMATH_API ~Matrix();
|
||||
|
||||
private:
|
||||
size_t m_rows;
|
||||
size_t m_columns;
|
||||
std::unique_ptr<float[]> m_data;
|
||||
};
|
||||
} // namespace omath
|
||||
@@ -1,34 +0,0 @@
|
||||
//
|
||||
// Created by Orange on 11/13/2024.
|
||||
//
|
||||
#pragma once
|
||||
#include "omath/Vector3.hpp"
|
||||
#include "omath/omath_export.hpp"
|
||||
|
||||
namespace omath
|
||||
{
|
||||
class OMATH_API Triangle3d final
|
||||
{
|
||||
public:
|
||||
Triangle3d(const Vector3& vertex1, const Vector3& vertex2, const Vector3& vertex3);
|
||||
|
||||
Vector3 m_vertex1;
|
||||
Vector3 m_vertex2;
|
||||
Vector3 m_vertex3;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3 CalculateNormal() const;
|
||||
|
||||
[[nodiscard]]
|
||||
float SideALength() const;
|
||||
|
||||
[[nodiscard]]
|
||||
float SideBLength() const;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3 SideAVector() const;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3 SideBVector() const;
|
||||
};
|
||||
}
|
||||
@@ -1,243 +0,0 @@
|
||||
//
|
||||
// Created by vlad on 10/28/23.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include "omath/Vector2.hpp"
|
||||
#include "omath/omath_export.hpp"
|
||||
|
||||
namespace omath
|
||||
{
|
||||
class OMATH_API Vector3 : public Vector2
|
||||
{
|
||||
public:
|
||||
float z = 0.f;
|
||||
constexpr Vector3(const float x, const float y, const float z) : Vector2(x, y), z(z) { }
|
||||
constexpr Vector3() : Vector2() {};
|
||||
|
||||
[[nodiscard]] constexpr bool operator==(const Vector3& src) const
|
||||
{
|
||||
return Vector2::operator==(src) && (src.z == z);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool operator!=(const Vector3& src) const
|
||||
{
|
||||
return !(*this == src);
|
||||
}
|
||||
|
||||
constexpr Vector3& operator+=(const Vector3& v)
|
||||
{
|
||||
Vector2::operator+=(v);
|
||||
z += v.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator-=(const Vector3& v)
|
||||
{
|
||||
Vector2::operator-=(v);
|
||||
z -= v.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator*=(const float fl)
|
||||
{
|
||||
Vector2::operator*=(fl);
|
||||
z *= fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator*=(const Vector3& v)
|
||||
{
|
||||
Vector2::operator*=(v);
|
||||
z *= v.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator/=(const Vector3& v)
|
||||
{
|
||||
Vector2::operator/=(v);
|
||||
z /= v.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator+=(const float fl)
|
||||
{
|
||||
Vector2::operator+=(fl);
|
||||
z += fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator/=(const float fl)
|
||||
{
|
||||
Vector2::operator/=(fl);
|
||||
z /= fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator-=(const float fl)
|
||||
{
|
||||
Vector2::operator-=(fl);
|
||||
z -= fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& Abs()
|
||||
{
|
||||
Vector2::Abs();
|
||||
z = z < 0.f ? -z : z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr float DistToSqr(const Vector3& vOther) const
|
||||
{
|
||||
return (*this - vOther).LengthSqr();
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr float Dot(const Vector3& vOther) const
|
||||
{
|
||||
return Vector2::Dot(vOther) + z * vOther.z;
|
||||
}
|
||||
|
||||
#ifndef _MSC_VER
|
||||
[[nodiscard]] constexpr float Length() const
|
||||
{
|
||||
return std::hypot(x, y, z);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr float Length2D() const
|
||||
{
|
||||
return Vector2::Length();
|
||||
}
|
||||
[[nodiscard]] float DistTo(const Vector3& vOther) const
|
||||
{
|
||||
return (*this - vOther).Length();
|
||||
}
|
||||
[[nodiscard]] constexpr Vector3 Normalized() const
|
||||
{
|
||||
const float length = this->Length();
|
||||
|
||||
return length != 0 ? *this / length : *this;
|
||||
}
|
||||
#else
|
||||
[[nodiscard]] float Length() const
|
||||
{
|
||||
return std::hypot(x, y, z);
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector3 Normalized() const
|
||||
{
|
||||
const float length = this->Length();
|
||||
|
||||
return length != 0 ? *this / length : *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] float Length2D() const
|
||||
{
|
||||
return Vector2::Length();
|
||||
}
|
||||
|
||||
[[nodiscard]] float DistTo(const Vector3& vOther) const
|
||||
{
|
||||
return (*this - vOther).Length();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
[[nodiscard]] constexpr float LengthSqr() const
|
||||
{
|
||||
return Vector2::LengthSqr() + z * z;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator-() const
|
||||
{
|
||||
return {-x, -y, -z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator+(const Vector3& v) const
|
||||
{
|
||||
return {x + v.x, y + v.y, z + v.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator-(const Vector3& v) const
|
||||
{
|
||||
return {x - v.x, y - v.y, z - v.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator*(const float fl) const
|
||||
{
|
||||
return {x * fl, y * fl, z * fl};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator*(const Vector3& v) const
|
||||
{
|
||||
return {x * v.x, y * v.y, z * v.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator/(const float fl) const
|
||||
{
|
||||
return {x / fl, y / fl, z / fl};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator/(const Vector3& v) const
|
||||
{
|
||||
return {x / v.x, y / v.y, z / v.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 Cross(const Vector3 &v) const
|
||||
{
|
||||
return
|
||||
{
|
||||
y * v.z - z * v.y,
|
||||
z * v.x - x * v.z,
|
||||
x * v.y - y * v.x
|
||||
};
|
||||
}
|
||||
[[nodiscard]] constexpr float Sum() const
|
||||
{
|
||||
return Sum2D() + z;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr float Sum2D() const
|
||||
{
|
||||
return Vector2::Sum();
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector3 ViewAngleTo(const Vector3& other) const;
|
||||
|
||||
[[nodiscard]] std::tuple<float, float, float> AsTuple() const
|
||||
{
|
||||
return std::make_tuple(x, y, z);
|
||||
}
|
||||
};
|
||||
}
|
||||
// ReSharper disable once CppRedundantNamespaceDefinition
|
||||
namespace std
|
||||
{
|
||||
template<>
|
||||
struct hash<omath::Vector3>
|
||||
{
|
||||
std::size_t operator()(const omath::Vector3& vec) const noexcept
|
||||
{
|
||||
std::size_t hash = 0;
|
||||
constexpr std::hash<float> hasher;
|
||||
|
||||
hash ^= hasher(vec.x) + 0x9e3779b9 + (hash<<6) + (hash>>2);
|
||||
hash ^= hasher(vec.y) + 0x9e3779b9 + (hash<<6) + (hash>>2);
|
||||
hash ^= hasher(vec.z) + 0x9e3779b9 + (hash<<6) + (hash>>2);
|
||||
|
||||
return hash;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,155 +0,0 @@
|
||||
//
|
||||
// Vector4.h
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include <omath/Vector3.hpp>
|
||||
#include <algorithm>
|
||||
#include "omath/omath_export.hpp"
|
||||
|
||||
namespace omath
|
||||
{
|
||||
class OMATH_API Vector4 : public Vector3
|
||||
{
|
||||
public:
|
||||
float w;
|
||||
|
||||
constexpr Vector4(const float x, const float y, const float z, const float w) : Vector3(x, y, z), w(w) {}
|
||||
constexpr Vector4() : Vector3(), w(0.f) {};
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr bool operator==(const Vector4& src) const
|
||||
{
|
||||
return Vector3::operator==(src) && w == src.w;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr bool operator!=(const Vector4& src) const
|
||||
{
|
||||
return !(*this == src);
|
||||
}
|
||||
|
||||
constexpr Vector4& operator+=(const Vector4& v)
|
||||
{
|
||||
Vector3::operator+=(v);
|
||||
w += v.w;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector4& operator-=(const Vector4& v)
|
||||
{
|
||||
Vector3::operator-=(v);
|
||||
w -= v.w;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector4& operator*=(const float scalar)
|
||||
{
|
||||
Vector3::operator*=(scalar);
|
||||
w *= scalar;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector4& operator*=(const Vector4& v)
|
||||
{
|
||||
Vector3::operator*=(v);
|
||||
w *= v.w;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector4& operator/=(const float scalar)
|
||||
{
|
||||
Vector3::operator/=(scalar);
|
||||
w /= scalar;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector4& operator/=(const Vector4& v)
|
||||
{
|
||||
Vector3::operator/=(v);
|
||||
w /= v.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr float LengthSqr() const
|
||||
{
|
||||
return Vector3::LengthSqr() + w * w;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr float Dot(const Vector4& vOther) const
|
||||
{
|
||||
return Vector3::Dot(vOther) + w * vOther.w;
|
||||
}
|
||||
|
||||
[[nodiscard]] float Length() const;
|
||||
|
||||
constexpr Vector4& Abs()
|
||||
{
|
||||
Vector3::Abs();
|
||||
w = w < 0.f ? -w : w;
|
||||
|
||||
return *this;
|
||||
}
|
||||
constexpr Vector4& Clamp(const float min, const float max)
|
||||
{
|
||||
x = std::clamp(x, min, max);
|
||||
y = std::clamp(y, min, max);
|
||||
z = std::clamp(z, min, max);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator-() const
|
||||
{
|
||||
return {-x, -y, -z, -w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator+(const Vector4& v) const
|
||||
{
|
||||
return {x + v.x, y + v.y, z + v.z, w + v.w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator-(const Vector4& v) const
|
||||
{
|
||||
return {x - v.x, y - v.y, z - v.z, w - v.w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator*(const float scalar) const
|
||||
{
|
||||
return {x * scalar, y * scalar, z * scalar, w * scalar};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator*(const Vector4& v) const
|
||||
{
|
||||
return {x * v.x, y * v.y, z * v.z, w * v.w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator/(const float scalar) const
|
||||
{
|
||||
return {x / scalar, y / scalar, z / scalar, w / scalar};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator/(const Vector4& v) const
|
||||
{
|
||||
return {x / v.x, y / v.y, z / v.z, w / v.w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr float Sum() const
|
||||
{
|
||||
return Vector3::Sum() + w;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -3,10 +3,10 @@
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "omath/Angles.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include "omath/omath_export.hpp"
|
||||
#include <utility>
|
||||
#include "omath/angles.hpp"
|
||||
|
||||
|
||||
namespace omath
|
||||
{
|
||||
@@ -17,11 +17,11 @@ namespace omath
|
||||
};
|
||||
|
||||
template<class Type = float, Type min = Type(0), Type max = Type(360), AngleFlags flags = AngleFlags::Normalized>
|
||||
requires std::is_arithmetic_v<Type>
|
||||
class OMATH_API Angle
|
||||
requires std::is_arithmetic_v<Type>
|
||||
class Angle
|
||||
{
|
||||
Type m_angle;
|
||||
constexpr Angle(const Type& degrees)
|
||||
constexpr explicit Angle(const Type& degrees)
|
||||
{
|
||||
if constexpr (flags == AngleFlags::Normalized)
|
||||
m_angle = angles::WrapAngle(degrees, min, max);
|
||||
@@ -34,20 +34,20 @@ namespace omath
|
||||
std::unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
[[nodiscard]]
|
||||
constexpr static Angle FromDegrees(const Type& degrees)
|
||||
{
|
||||
return {degrees};
|
||||
return Angle{degrees};
|
||||
}
|
||||
constexpr Angle() : m_angle(0)
|
||||
{
|
||||
|
||||
}
|
||||
[[nodiscard]]
|
||||
constexpr static Angle FromRadians(const Type& degrees)
|
||||
{
|
||||
return {angles::RadiansToDegrees<Type>(degrees)};
|
||||
return Angle{angles::RadiansToDegrees<Type>(degrees)};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
@@ -98,7 +98,6 @@ namespace omath
|
||||
return Cos() / Sin();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Angle& operator+=(const Angle& other)
|
||||
{
|
||||
if constexpr (flags == AngleFlags::Normalized)
|
||||
@@ -118,7 +117,6 @@ namespace omath
|
||||
[[nodiscard]]
|
||||
constexpr std::partial_ordering operator<=>(const Angle& other) const = default;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Angle& operator-=(const Angle& other)
|
||||
{
|
||||
return operator+=(-other);
|
||||
@@ -148,7 +146,7 @@ namespace omath
|
||||
[[nodiscard]]
|
||||
constexpr Angle operator-() const
|
||||
{
|
||||
return {-m_angle};
|
||||
return Angle{-m_angle};
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace omath
|
||||
@@ -3,8 +3,8 @@
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include <numbers>
|
||||
#include <cmath>
|
||||
#include <numbers>
|
||||
|
||||
namespace omath::angles
|
||||
{
|
||||
@@ -1,39 +0,0 @@
|
||||
//
|
||||
// Created by Orange on 11/13/2024.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include "omath/Vector3.hpp"
|
||||
#include "omath/Triangle3d.hpp"
|
||||
#include "omath/omath_export.hpp"
|
||||
|
||||
namespace omath::collision
|
||||
{
|
||||
class OMATH_API Ray
|
||||
{
|
||||
public:
|
||||
Vector3 start;
|
||||
Vector3 end;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3 DirectionVector() const;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3 DirectionVectorNormalized() const;
|
||||
};
|
||||
class OMATH_API LineTracer
|
||||
{
|
||||
public:
|
||||
LineTracer() = delete;
|
||||
|
||||
|
||||
[[nodiscard]]
|
||||
static bool CanTraceLine(const Ray& ray, const Triangle3d& triangle);
|
||||
|
||||
|
||||
// Realization of Möller–Trumbore intersection algorithm
|
||||
// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
|
||||
[[nodiscard]]
|
||||
static Vector3 GetRayHitPoint(const Ray& ray, const Triangle3d& triangle);
|
||||
};
|
||||
}
|
||||
38
include/omath/collision/line_tracer.hpp
Normal file
38
include/omath/collision/line_tracer.hpp
Normal file
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// Created by Orange on 11/13/2024.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include "omath/triangle.hpp"
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
namespace omath::collision
|
||||
{
|
||||
class Ray
|
||||
{
|
||||
public:
|
||||
Vector3<float> start;
|
||||
Vector3<float> end;
|
||||
bool infinite_length = false;
|
||||
[[nodiscard]]
|
||||
Vector3<float> DirectionVector() const;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> DirectionVectorNormalized() const;
|
||||
};
|
||||
class LineTracer
|
||||
{
|
||||
public:
|
||||
LineTracer() = delete;
|
||||
|
||||
|
||||
[[nodiscard]]
|
||||
static bool CanTraceLine(const Ray& ray, const Triangle<Vector3<float>>& triangle);
|
||||
|
||||
|
||||
// Realization of Möller–Trumbore intersection algorithm
|
||||
// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
|
||||
[[nodiscard]]
|
||||
static Vector3<float> GetRayHitPoint(const Ray& ray, const Triangle<Vector3<float>>& triangle);
|
||||
};
|
||||
}
|
||||
173
include/omath/color.hpp
Normal file
173
include/omath/color.hpp
Normal file
@@ -0,0 +1,173 @@
|
||||
//
|
||||
// Created by vlad on 2/4/24.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include "omath/vector3.hpp"
|
||||
#include "omath/vector4.hpp"
|
||||
|
||||
#ifdef max
|
||||
#undef max
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef min
|
||||
#undef min
|
||||
#endif
|
||||
|
||||
namespace omath
|
||||
{
|
||||
struct HSV
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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 static Color FromHSV(const HSV& hsv)
|
||||
{
|
||||
return FromHSV(hsv.hue, hsv.saturation, hsv.value);
|
||||
}
|
||||
|
||||
[[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;
|
||||
|
||||
|
||||
if (delta == 0.f)
|
||||
hsvData.hue = 0.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.hue < 0.f)
|
||||
hsvData.hue += 360.f;
|
||||
|
||||
hsvData.hue /= 360.f;
|
||||
hsvData.saturation = max == 0.f ? 0.f : delta / max;
|
||||
hsvData.value = max;
|
||||
|
||||
return hsvData;
|
||||
}
|
||||
|
||||
constexpr explicit Color(const Vector4& vec) : Vector4(vec)
|
||||
{
|
||||
Clamp(0.f, 1.f);
|
||||
}
|
||||
constexpr void SetHue(const float hue)
|
||||
{
|
||||
auto hsv = ToHSV();
|
||||
hsv.hue = hue;
|
||||
|
||||
*this = FromHSV(hsv);
|
||||
}
|
||||
|
||||
constexpr void SetSaturation(const float saturation)
|
||||
{
|
||||
auto hsv = ToHSV();
|
||||
hsv.saturation = saturation;
|
||||
|
||||
*this = FromHSV(hsv);
|
||||
}
|
||||
|
||||
constexpr 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};
|
||||
}
|
||||
};
|
||||
} // namespace omath
|
||||
@@ -1,20 +0,0 @@
|
||||
//
|
||||
// Created by Orange on 12/4/2024.
|
||||
//
|
||||
#pragma once
|
||||
#include "Constants.h"
|
||||
#include "omath/projection/Camera.hpp"
|
||||
#include "omath/omath_export.hpp"
|
||||
|
||||
namespace omath::source
|
||||
{
|
||||
class OMATH_API Camera final : public projection::Camera<Mat4x4, ViewAngles>
|
||||
{
|
||||
public:
|
||||
Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
|
||||
void LookAt(const Vector3& target) override;
|
||||
[[nodiscard]] Mat4x4 GetViewMatrix() const override;
|
||||
[[nodiscard]] Mat4x4 GetProjectionMatrix() const override;
|
||||
};
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
//
|
||||
// Created by Orange on 12/4/2024.
|
||||
//
|
||||
#pragma once
|
||||
#include "Constants.h"
|
||||
|
||||
namespace omath::source
|
||||
{
|
||||
[[nodiscard]]
|
||||
inline Vector3 ForwardVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsForward);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
inline Vector3 RightVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsRight);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
inline Vector3 UpVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsUp);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
|
||||
[[nodiscard]] inline Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3& cam_origin)
|
||||
{
|
||||
return MatCameraView(ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin);
|
||||
}
|
||||
|
||||
|
||||
[[nodiscard]]
|
||||
inline Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
|
||||
const float far)
|
||||
{
|
||||
// NOTE: Needed tp make thing draw normal, since source is wierd
|
||||
// and use tricky projection matrix formula.
|
||||
constexpr auto kMultiplyFactor = 0.75f;
|
||||
|
||||
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f) * kMultiplyFactor;
|
||||
|
||||
return {
|
||||
{1.f / (aspectRatio * fovHalfTan), 0, 0, 0},
|
||||
{0, 1.f / (fovHalfTan), 0, 0},
|
||||
{0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)},
|
||||
{0, 0, 1, 0},
|
||||
|
||||
};
|
||||
}
|
||||
} // namespace omath::source
|
||||
21
include/omath/engines/iw_engine/camera.hpp
Normal file
21
include/omath/engines/iw_engine/camera.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by Vlad on 3/17/2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "omath/engines/iw_engine/constants.hpp"
|
||||
#include "omath/projection/camera.hpp"
|
||||
|
||||
namespace omath::iw_engine
|
||||
{
|
||||
class Camera final : public projection::Camera<Mat4x4, ViewAngles>
|
||||
{
|
||||
public:
|
||||
Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
|
||||
void LookAt(const Vector3<float>& target) override;
|
||||
protected:
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix() const override;
|
||||
[[nodiscard]] Mat4x4 CalcProjectionMatrix() const override;
|
||||
};
|
||||
}
|
||||
25
include/omath/engines/iw_engine/constants.hpp
Normal file
25
include/omath/engines/iw_engine/constants.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Created by Vlad on 3/17/2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include <omath/vector3.hpp>
|
||||
#include <omath/mat.hpp>
|
||||
#include <omath/angle.hpp>
|
||||
#include <omath/view_angles.hpp>
|
||||
|
||||
namespace omath::iw_engine
|
||||
{
|
||||
constexpr Vector3<float> kAbsUp = {0, 0, 1};
|
||||
constexpr Vector3<float> kAbsRight = {0, -1, 0};
|
||||
constexpr Vector3<float> kAbsForward = {1, 0, 0};
|
||||
|
||||
using Mat4x4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat3x3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat1x3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>;
|
||||
using PitchAngle = Angle<float, -89.f, 89.f, AngleFlags::Clamped>;
|
||||
using YawAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
|
||||
using RollAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
|
||||
|
||||
using ViewAngles = omath::ViewAngles<PitchAngle, YawAngle, RollAngle>;
|
||||
}
|
||||
26
include/omath/engines/iw_engine/formulas.hpp
Normal file
26
include/omath/engines/iw_engine/formulas.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by Vlad on 3/17/2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "omath/engines/iw_engine/constants.hpp"
|
||||
|
||||
namespace omath::iw_engine
|
||||
{
|
||||
[[nodiscard]]
|
||||
Vector3<float> ForwardVector(const ViewAngles& angles);
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> RightVector(const ViewAngles& angles);
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> UpVector(const ViewAngles& angles);
|
||||
|
||||
[[nodiscard]]
|
||||
Mat4x4 RotationMatrix(const ViewAngles& angles);
|
||||
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
|
||||
|
||||
[[nodiscard]]
|
||||
Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
|
||||
} // namespace omath::iw_engine
|
||||
19
include/omath/engines/opengl_engine/camera.hpp
Normal file
19
include/omath/engines/opengl_engine/camera.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by Orange on 12/23/2024.
|
||||
//
|
||||
#pragma once
|
||||
#include "omath/engines/opengl_engine/constants.hpp"
|
||||
#include "omath/projection/camera.hpp"
|
||||
|
||||
namespace omath::opengl_engine
|
||||
{
|
||||
class Camera final : public projection::Camera<Mat4x4, ViewAngles>
|
||||
{
|
||||
public:
|
||||
Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
|
||||
void LookAt(const Vector3<float>& target) override;
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix() const override;
|
||||
[[nodiscard]] Mat4x4 CalcProjectionMatrix() const override;
|
||||
};
|
||||
}
|
||||
26
include/omath/engines/opengl_engine/constants.hpp
Normal file
26
include/omath/engines/opengl_engine/constants.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by Orange on 12/23/2024.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include <omath/angle.hpp>
|
||||
#include <omath/mat.hpp>
|
||||
#include <omath/vector3.hpp>
|
||||
#include <omath/view_angles.hpp>
|
||||
|
||||
namespace omath::opengl_engine
|
||||
{
|
||||
constexpr Vector3<float> kAbsUp = {0, 1, 0};
|
||||
constexpr Vector3<float> kAbsRight = {1, 0, 0};
|
||||
constexpr Vector3<float> kAbsForward = {0, 0, -1};
|
||||
|
||||
using Mat4x4 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
|
||||
using Mat3x3 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
|
||||
using Mat1x3 = Mat<1, 3, float, MatStoreType::COLUMN_MAJOR>;
|
||||
using PitchAngle = Angle<float, -90.f, 90.f, AngleFlags::Clamped>;
|
||||
using YawAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
|
||||
using RollAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
|
||||
|
||||
|
||||
using ViewAngles = omath::ViewAngles<PitchAngle, YawAngle, RollAngle>;
|
||||
}
|
||||
26
include/omath/engines/opengl_engine/formulas.hpp
Normal file
26
include/omath/engines/opengl_engine/formulas.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by Orange on 12/23/2024.
|
||||
//
|
||||
#pragma once
|
||||
#include "omath/engines/opengl_engine/constants.hpp"
|
||||
|
||||
|
||||
namespace omath::opengl_engine
|
||||
{
|
||||
[[nodiscard]]
|
||||
Vector3<float> ForwardVector(const ViewAngles& angles);
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> RightVector(const ViewAngles& angles);
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> UpVector(const ViewAngles& angles);
|
||||
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
|
||||
|
||||
[[nodiscard]]
|
||||
Mat4x4 RotationMatrix(const ViewAngles& angles);
|
||||
|
||||
[[nodiscard]]
|
||||
Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
|
||||
} // namespace omath::opengl_engine
|
||||
20
include/omath/engines/source_engine/camera.hpp
Normal file
20
include/omath/engines/source_engine/camera.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by Orange on 12/4/2024.
|
||||
//
|
||||
#pragma once
|
||||
#include "omath/engines/source_engine/constants.hpp"
|
||||
#include "omath/projection/camera.hpp"
|
||||
|
||||
namespace omath::source_engine
|
||||
{
|
||||
class Camera final : public projection::Camera<Mat4x4, ViewAngles>
|
||||
{
|
||||
public:
|
||||
Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
|
||||
void LookAt(const Vector3<float>& target) override;
|
||||
protected:
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix() const override;
|
||||
[[nodiscard]] Mat4x4 CalcProjectionMatrix() const override;
|
||||
};
|
||||
}
|
||||
@@ -3,15 +3,16 @@
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include <omath/Vector3.hpp>
|
||||
#include <omath/Mat.hpp>
|
||||
#include <omath/Angle.hpp>
|
||||
#include <omath/ViewAngles.hpp>
|
||||
namespace omath::source
|
||||
#include <omath/vector3.hpp>
|
||||
#include <omath/mat.hpp>
|
||||
#include <omath/angle.hpp>
|
||||
#include <omath/view_angles.hpp>
|
||||
|
||||
namespace omath::source_engine
|
||||
{
|
||||
constexpr Vector3 kAbsUp = {0, 0, 1};
|
||||
constexpr Vector3 kAbsRight = {0, -1, 0};
|
||||
constexpr Vector3 kAbsForward = {1, 0, 0};
|
||||
constexpr Vector3<float> kAbsUp = {0, 0, 1};
|
||||
constexpr Vector3<float> kAbsRight = {0, -1, 0};
|
||||
constexpr Vector3<float> kAbsForward = {1, 0, 0};
|
||||
|
||||
using Mat4x4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat3x3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
25
include/omath/engines/source_engine/formulas.hpp
Normal file
25
include/omath/engines/source_engine/formulas.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Created by Orange on 12/4/2024.
|
||||
//
|
||||
#pragma once
|
||||
#include "omath/engines/source_engine/constants.hpp"
|
||||
|
||||
namespace omath::source_engine
|
||||
{
|
||||
[[nodiscard]]
|
||||
Vector3<float> ForwardVector(const ViewAngles& angles);
|
||||
|
||||
[[nodiscard]]
|
||||
Mat4x4 RotationMatrix(const ViewAngles& angles);
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> RightVector(const ViewAngles& angles);
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> UpVector(const ViewAngles& angles);
|
||||
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
|
||||
|
||||
[[nodiscard]]
|
||||
Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
|
||||
} // namespace omath::source
|
||||
21
include/omath/engines/unity_engine/camera.hpp
Normal file
21
include/omath/engines/unity_engine/camera.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by Vlad on 3/22/2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "omath/engines/unity_engine/constants.hpp"
|
||||
#include "omath/projection/camera.hpp"
|
||||
|
||||
namespace omath::unity_engine
|
||||
{
|
||||
class Camera final : public projection::Camera<Mat4x4, ViewAngles>
|
||||
{
|
||||
public:
|
||||
Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
|
||||
void LookAt(const Vector3<float>& target) override;
|
||||
protected:
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix() const override;
|
||||
[[nodiscard]] Mat4x4 CalcProjectionMatrix() const override;
|
||||
};
|
||||
}
|
||||
26
include/omath/engines/unity_engine/constants.hpp
Normal file
26
include/omath/engines/unity_engine/constants.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by Vlad on 3/22/2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <omath/vector3.hpp>
|
||||
#include <omath/mat.hpp>
|
||||
#include <omath/angle.hpp>
|
||||
#include <omath/view_angles.hpp>
|
||||
|
||||
namespace omath::unity_engine
|
||||
{
|
||||
constexpr Vector3<float> kAbsUp = {0, 1, 0};
|
||||
constexpr Vector3<float> kAbsRight = {1, 0, 0};
|
||||
constexpr Vector3<float> kAbsForward = {0, 0, 1};
|
||||
|
||||
using Mat4x4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat3x3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat1x3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>;
|
||||
using PitchAngle = Angle<float, -90.f, 90.f, AngleFlags::Clamped>;
|
||||
using YawAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
|
||||
using RollAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
|
||||
|
||||
using ViewAngles = omath::ViewAngles<PitchAngle, YawAngle, RollAngle>;
|
||||
} // namespace omath::source
|
||||
26
include/omath/engines/unity_engine/formulas.hpp
Normal file
26
include/omath/engines/unity_engine/formulas.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by Vlad on 3/22/2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "omath/engines/unity_engine/constants.hpp"
|
||||
|
||||
namespace omath::unity_engine
|
||||
{
|
||||
[[nodiscard]]
|
||||
Vector3<float> ForwardVector(const ViewAngles& angles);
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> RightVector(const ViewAngles& angles);
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> UpVector(const ViewAngles& angles);
|
||||
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
|
||||
|
||||
[[nodiscard]]
|
||||
Mat4x4 RotationMatrix(const ViewAngles& angles);
|
||||
|
||||
[[nodiscard]]
|
||||
Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
|
||||
} // namespace omath::source
|
||||
@@ -4,15 +4,26 @@
|
||||
#pragma once
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
#include "Vector3.hpp"
|
||||
#include "omath/omath_export.hpp"
|
||||
#include "omath/vector3.hpp"
|
||||
#include <numeric>
|
||||
|
||||
|
||||
#ifdef near
|
||||
#undef near
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef far
|
||||
#undef far
|
||||
#endif
|
||||
|
||||
namespace omath
|
||||
{
|
||||
struct OMATH_API MatSize
|
||||
struct MatSize
|
||||
{
|
||||
size_t rows, columns;
|
||||
};
|
||||
@@ -23,20 +34,29 @@ namespace omath
|
||||
COLUMN_MAJOR
|
||||
};
|
||||
|
||||
|
||||
template<typename M1, typename M2>
|
||||
concept MatTemplateEqual =
|
||||
(M1::rows == M2::rows) && (M1::columns == M2::columns) &&
|
||||
std::is_same_v<typename M1::value_type, typename M2::value_type> &&
|
||||
(M1::store_type == M2::store_type);
|
||||
|
||||
template<size_t Rows = 0, size_t Columns = 0, class Type = float, MatStoreType StoreType = MatStoreType::ROW_MAJOR>
|
||||
requires std::is_arithmetic_v<Type>
|
||||
class Mat final
|
||||
{
|
||||
public:
|
||||
OMATH_API constexpr Mat()
|
||||
constexpr Mat() noexcept
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
OMATH_API constexpr static MatStoreType GetStoreOrdering()
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr static MatStoreType GetStoreOrdering() noexcept
|
||||
{
|
||||
return StoreType;
|
||||
}
|
||||
OMATH_API constexpr Mat(const std::initializer_list<std::initializer_list<Type>>& rows)
|
||||
constexpr Mat(const std::initializer_list<std::initializer_list<Type>>& rows)
|
||||
{
|
||||
if (rows.size() != Rows)
|
||||
throw std::invalid_argument("Initializer list rows size does not match template parameter Rows");
|
||||
@@ -56,44 +76,58 @@ namespace omath
|
||||
}
|
||||
}
|
||||
|
||||
OMATH_API constexpr explicit Mat(const Type* rawData)
|
||||
constexpr explicit Mat(const Type* rawData)
|
||||
{
|
||||
std::copy_n(rawData, Rows * Columns, m_data.begin());
|
||||
}
|
||||
|
||||
OMATH_API constexpr Mat(const Mat& other) noexcept
|
||||
constexpr Mat(const Mat& other) noexcept
|
||||
{
|
||||
m_data = other.m_data;
|
||||
}
|
||||
|
||||
OMATH_API constexpr Mat(Mat&& other) noexcept
|
||||
[[nodiscard]]
|
||||
constexpr Type& operator[](const size_t row, const size_t col)
|
||||
{
|
||||
return At(row, col);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Type& operator[](const size_t row, const size_t col) const
|
||||
{
|
||||
return At(row, col);
|
||||
}
|
||||
|
||||
constexpr Mat(Mat&& other) noexcept
|
||||
{
|
||||
m_data = std::move(other.m_data);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API static constexpr size_t RowCount() noexcept
|
||||
static constexpr size_t RowCount() noexcept
|
||||
{
|
||||
return Rows;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API static constexpr size_t ColumnsCount() noexcept
|
||||
static constexpr size_t ColumnsCount() noexcept
|
||||
{
|
||||
return Columns;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API static consteval MatSize Size() noexcept
|
||||
static consteval MatSize Size() noexcept
|
||||
{
|
||||
return {Rows, Columns};
|
||||
}
|
||||
|
||||
[[nodiscard]] OMATH_API constexpr const Type& At(const size_t rowIndex, const size_t columnIndex) const
|
||||
[[nodiscard]]
|
||||
constexpr const Type& At(const size_t rowIndex, const size_t columnIndex) const
|
||||
{
|
||||
#if !defined(NDEBUG) && defined(OMATH_SUPRESS_SAFETY_CHECKS)
|
||||
if (rowIndex >= Rows || columnIndex >= Columns)
|
||||
throw std::out_of_range("Index out of range");
|
||||
|
||||
#endif
|
||||
if constexpr (StoreType == MatStoreType::ROW_MAJOR)
|
||||
return m_data[rowIndex * Columns + columnIndex];
|
||||
|
||||
@@ -107,36 +141,32 @@ namespace omath
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] OMATH_API constexpr Type& At(const size_t rowIndex, const size_t columnIndex)
|
||||
[[nodiscard]] constexpr Type& At(const size_t rowIndex, const size_t columnIndex)
|
||||
{
|
||||
return const_cast<Type&>(std::as_const(*this).At(rowIndex, columnIndex));
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API constexpr Type Sum() const noexcept
|
||||
constexpr Type Sum() const noexcept
|
||||
{
|
||||
Type sum = 0;
|
||||
for (size_t i = 0; i < Rows; ++i)
|
||||
for (size_t j = 0; j < Columns; ++j)
|
||||
sum += At(i, j);
|
||||
|
||||
return sum;
|
||||
return std::accumulate(m_data.begin(), m_data.end(), Type(0));
|
||||
}
|
||||
|
||||
OMATH_API constexpr void Clear() noexcept
|
||||
constexpr void Clear() noexcept
|
||||
{
|
||||
Set(0);
|
||||
}
|
||||
|
||||
OMATH_API constexpr void Set(const Type& value) noexcept
|
||||
constexpr void Set(const Type& value) noexcept
|
||||
{
|
||||
std::ranges::fill(m_data, value);
|
||||
}
|
||||
|
||||
// Operator overloading for multiplication with another Mat
|
||||
template<size_t OtherColumns>
|
||||
[[nodiscard]]
|
||||
constexpr Mat<Rows, OtherColumns, Type, StoreType>
|
||||
OMATH_API operator*(const Mat<Columns, OtherColumns, Type, StoreType>& other) const
|
||||
operator*(const Mat<Columns, OtherColumns, Type, StoreType>& other) const
|
||||
{
|
||||
Mat<Rows, OtherColumns, Type, StoreType> result;
|
||||
|
||||
@@ -151,67 +181,59 @@ namespace omath
|
||||
return result;
|
||||
}
|
||||
|
||||
OMATH_API constexpr Mat& operator*=(const Type& f) noexcept
|
||||
constexpr Mat& operator*=(const Type& f) noexcept
|
||||
{
|
||||
for (size_t i = 0; i < Rows; ++i)
|
||||
for (size_t j = 0; j < Columns; ++j)
|
||||
At(i, j) *= f;
|
||||
std::ranges::for_each(m_data, [&f](auto& val) {val *= f;});
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<size_t OtherColumns>
|
||||
constexpr Mat<Rows, OtherColumns, Type, StoreType>
|
||||
OMATH_API operator*=(const Mat<Columns, OtherColumns, Type, StoreType>& other)
|
||||
operator*=(const Mat<Columns, OtherColumns, Type, StoreType>& other)
|
||||
{
|
||||
return *this = *this * other;
|
||||
}
|
||||
|
||||
OMATH_API constexpr Mat operator*(const Type& f) const noexcept
|
||||
[[nodiscard]]
|
||||
constexpr Mat operator*(const Type& value) const noexcept
|
||||
{
|
||||
Mat result(*this);
|
||||
result *= f;
|
||||
result *= value;
|
||||
return result;
|
||||
}
|
||||
|
||||
OMATH_API constexpr Mat& operator/=(const Type& f) noexcept
|
||||
constexpr Mat& operator/=(const Type& value) noexcept
|
||||
{
|
||||
for (size_t i = 0; i < Rows; ++i)
|
||||
for (size_t j = 0; j < Columns; ++j)
|
||||
At(i, j) /= f;
|
||||
std::ranges::for_each(m_data, [&value](auto& val) {val /= value;});
|
||||
return *this;
|
||||
}
|
||||
|
||||
OMATH_API constexpr Mat operator/(const Type& f) const noexcept
|
||||
[[nodiscard]]
|
||||
constexpr Mat operator/(const Type& value) const noexcept
|
||||
{
|
||||
Mat result(*this);
|
||||
result /= f;
|
||||
result /= value;
|
||||
return result;
|
||||
}
|
||||
|
||||
OMATH_API constexpr Mat& operator=(const Mat& other) noexcept
|
||||
constexpr Mat& operator=(const Mat& other) noexcept
|
||||
{
|
||||
if (this == &other)
|
||||
return *this;
|
||||
for (size_t i = 0; i < Rows; ++i)
|
||||
for (size_t j = 0; j < Columns; ++j)
|
||||
At(i, j) = other.At(i, j);
|
||||
if (this != &other)
|
||||
m_data = other.m_data;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
OMATH_API constexpr Mat& operator=(Mat&& other) noexcept
|
||||
constexpr Mat& operator=(Mat&& other) noexcept
|
||||
{
|
||||
if (this == &other)
|
||||
return *this;
|
||||
|
||||
for (size_t i = 0; i < Rows; ++i)
|
||||
for (size_t j = 0; j < Columns; ++j)
|
||||
At(i, j) = other.At(i, j);
|
||||
if (this != &other)
|
||||
m_data = std::move(other.m_data);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API constexpr Mat<Columns, Rows, Type, StoreType> Transposed() const noexcept
|
||||
constexpr Mat<Columns, Rows, Type, StoreType> Transposed() const noexcept
|
||||
{
|
||||
Mat<Columns, Rows, Type, StoreType> transposed;
|
||||
for (size_t i = 0; i < Rows; ++i)
|
||||
@@ -222,14 +244,14 @@ namespace omath
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API constexpr Type Determinant() const
|
||||
constexpr Type Determinant() const
|
||||
{
|
||||
static_assert(Rows == Columns, "Determinant is only defined for square matrices.");
|
||||
|
||||
if constexpr (Rows == 1)
|
||||
return At(0, 0);
|
||||
|
||||
else if constexpr (Rows == 2)
|
||||
if constexpr (Rows == 2)
|
||||
return At(0, 0) * At(1, 1) - At(0, 1) * At(1, 0);
|
||||
else
|
||||
{
|
||||
@@ -244,7 +266,7 @@ namespace omath
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API constexpr Mat<Rows - 1, Columns - 1, Type, StoreType> Minor(const size_t row, const size_t column) const
|
||||
constexpr Mat<Rows - 1, Columns - 1, Type, StoreType> Minor(const size_t row, const size_t column) const
|
||||
{
|
||||
Mat<Rows - 1, Columns - 1, Type, StoreType> result;
|
||||
for (size_t i = 0, m = 0; i < Rows; ++i)
|
||||
@@ -264,49 +286,54 @@ namespace omath
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API constexpr const std::array<Type, Rows * Columns>& RawArray() const
|
||||
constexpr const std::array<Type, Rows * Columns>& RawArray() const
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API constexpr std::array<Type, Rows * Columns>& RawArray()
|
||||
constexpr std::array<Type, Rows * Columns>& RawArray()
|
||||
{
|
||||
return const_cast<std::array<Type, Rows * Columns>>(std::as_const(*this).RawArray());
|
||||
return m_data;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API std::string ToString() const noexcept
|
||||
std::string ToString() const noexcept
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "[[";
|
||||
|
||||
for (size_t i = 0; i < Rows; ++i)
|
||||
{
|
||||
if (i > 0)
|
||||
oss << " [";
|
||||
|
||||
for (size_t j = 0; j < Columns; ++j)
|
||||
{
|
||||
oss << At(i, j);
|
||||
oss << std::setw(9) << std::fixed << std::setprecision(3) << At(i, j);
|
||||
if (j != Columns - 1)
|
||||
oss << ' ';
|
||||
oss << ", ";
|
||||
}
|
||||
oss << '\n';
|
||||
oss << (i == Rows - 1 ? "]]" : "]\n");
|
||||
}
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API bool operator==(const Mat& mat) const
|
||||
bool operator==(const Mat& mat) const
|
||||
{
|
||||
return m_data == mat.m_data;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API bool operator!=(const Mat& mat) const
|
||||
bool operator!=(const Mat& mat) const
|
||||
{
|
||||
return !operator==(mat);
|
||||
}
|
||||
|
||||
// Static methods that return fixed-size matrices
|
||||
[[nodiscard]]
|
||||
OMATH_API constexpr static Mat<4, 4> ToScreenMat(const Type& screenWidth, const Type& screenHeight) noexcept
|
||||
constexpr static Mat<4, 4> ToScreenMat(const Type& screenWidth, const Type& screenHeight) noexcept
|
||||
{
|
||||
return {
|
||||
{screenWidth / 2, 0, 0, 0},
|
||||
@@ -322,21 +349,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]]
|
||||
OMATH_API constexpr Mat<4, 4, Type, St> MatTranslation(const Vector3& diff) noexcept
|
||||
constexpr Mat<4, 4, Type, St> MatTranslation(const Vector3<Type>& diff) noexcept
|
||||
{
|
||||
return
|
||||
{
|
||||
@@ -349,7 +376,7 @@ namespace omath
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class Angle>
|
||||
[[nodiscard]]
|
||||
OMATH_API Mat<4, 4, Type, St> MatRotationAxisX(const Angle& angle) noexcept
|
||||
Mat<4, 4, Type, St> MatRotationAxisX(const Angle& angle) noexcept
|
||||
{
|
||||
return
|
||||
{
|
||||
@@ -362,7 +389,7 @@ namespace omath
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class Angle>
|
||||
[[nodiscard]]
|
||||
OMATH_API Mat<4, 4, Type, St> MatRotationAxisY(const Angle& angle) noexcept
|
||||
Mat<4, 4, Type, St> MatRotationAxisY(const Angle& angle) noexcept
|
||||
{
|
||||
return
|
||||
{
|
||||
@@ -375,7 +402,7 @@ namespace omath
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class Angle>
|
||||
[[nodiscard]]
|
||||
OMATH_API Mat<4, 4, Type, St> MatRotationAxisZ(const Angle& angle) noexcept
|
||||
Mat<4, 4, Type, St> MatRotationAxisZ(const Angle& angle) noexcept
|
||||
{
|
||||
return
|
||||
{
|
||||
@@ -388,8 +415,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>
|
||||
{
|
||||
@@ -401,10 +428,29 @@ namespace omath
|
||||
} * MatTranslation<Type, St>(-cameraOrigin);
|
||||
}
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class ViewAngles>
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
|
||||
[[nodiscard]]
|
||||
OMATH_API Mat<4, 4, Type, St> MatRotation(const ViewAngles& angles) noexcept
|
||||
Mat<4, 4, Type, St> MatPerspectiveLeftHanded(const float fieldOfView, const float aspectRatio, const float near,
|
||||
const float far) noexcept
|
||||
{
|
||||
return MatRotationAxisZ(angles.yaw) * MatRotationAxisY(angles.pitch) * MatRotationAxisX(angles.roll);
|
||||
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
|
||||
|
||||
return {{1.f / (aspectRatio * fovHalfTan), 0.f, 0.f, 0.f},
|
||||
{0.f, 1.f / fovHalfTan, 0.f, 0.f},
|
||||
{0.f, 0.f, (far + near) / (far - near), -(2.f * near * far) / (far - near)},
|
||||
{0.f, 0.f, 1.f, 0.f}};
|
||||
}
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
|
||||
[[nodiscard]]
|
||||
Mat<4, 4, Type, St> MatPerspectiveRightHanded(const float fieldOfView, const float aspectRatio, const float near,
|
||||
const float far) noexcept
|
||||
{
|
||||
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
|
||||
|
||||
return {{1.f / (aspectRatio * fovHalfTan), 0.f, 0.f, 0.f},
|
||||
{0.f, 1.f / fovHalfTan, 0.f, 0.f},
|
||||
{0.f, 0.f, -(far + near) / (far - near), -(2.f * near * far) / (far - near)},
|
||||
{0.f, 0.f, -1.f, 0.f}};
|
||||
}
|
||||
} // namespace omath
|
||||
108
include/omath/matrix.hpp
Normal file
108
include/omath/matrix.hpp
Normal file
@@ -0,0 +1,108 @@
|
||||
#pragma once
|
||||
#include <initializer_list>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
namespace omath
|
||||
{
|
||||
|
||||
class Matrix final
|
||||
{
|
||||
public:
|
||||
Matrix();
|
||||
Matrix(size_t rows, size_t columns);
|
||||
|
||||
Matrix(const std::initializer_list<std::initializer_list<float>>& rows);
|
||||
|
||||
[[nodiscard]]
|
||||
static Matrix ToScreenMatrix(float screenWidth, float screenHeight);
|
||||
|
||||
[[nodiscard]]
|
||||
static Matrix TranslationMatrix(const Vector3<float>& diff);
|
||||
|
||||
[[nodiscard]]
|
||||
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);
|
||||
|
||||
Matrix(const Matrix& other);
|
||||
|
||||
Matrix(size_t rows, size_t columns, const float* pRaw);
|
||||
|
||||
Matrix(Matrix&& other) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
size_t RowCount() const noexcept;
|
||||
|
||||
|
||||
[[nodiscard]]
|
||||
float& operator[](size_t row, size_t column);
|
||||
|
||||
[[nodiscard]]
|
||||
size_t ColumnsCount() const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
std::pair<size_t, size_t> Size() const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
float& At(size_t iRow, size_t iCol);
|
||||
|
||||
[[nodiscard]]
|
||||
float Sum();
|
||||
|
||||
void SetDataFromRaw(const float* pRawMatrix);
|
||||
|
||||
[[nodiscard]]
|
||||
Matrix Transpose() const;
|
||||
|
||||
void Set(float val);
|
||||
|
||||
[[nodiscard]]
|
||||
const float& At(size_t iRow, size_t iCol) const;
|
||||
|
||||
Matrix operator*(const Matrix& other) const;
|
||||
|
||||
Matrix& operator*=(const Matrix& other);
|
||||
|
||||
Matrix operator*(float f) const;
|
||||
|
||||
Matrix& operator*=(float f);
|
||||
|
||||
Matrix& operator/=(float f);
|
||||
|
||||
void Clear();
|
||||
|
||||
[[nodiscard]]
|
||||
Matrix Strip(size_t row, size_t column) const;
|
||||
|
||||
[[nodiscard]]
|
||||
float Minor(size_t i, size_t j) const;
|
||||
|
||||
[[nodiscard]]
|
||||
float AlgComplement(size_t i, size_t j) const;
|
||||
|
||||
[[nodiscard]]
|
||||
float Determinant() const;
|
||||
|
||||
[[nodiscard]]
|
||||
const float* Raw() const;
|
||||
|
||||
Matrix& operator=(const Matrix& other);
|
||||
|
||||
Matrix& operator=(Matrix&& other) noexcept;
|
||||
|
||||
Matrix operator/(float f) const;
|
||||
|
||||
[[nodiscard]]
|
||||
std::string ToString() const;
|
||||
|
||||
~Matrix();
|
||||
|
||||
private:
|
||||
size_t m_rows;
|
||||
size_t m_columns;
|
||||
std::unique_ptr<float[]> m_data;
|
||||
};
|
||||
} // namespace omath
|
||||
@@ -1,24 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/* Export prefix for functions */
|
||||
#ifdef _MSC_VER
|
||||
/* MSVC */
|
||||
# define OMATH_API_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
/* GCC/Clang */
|
||||
# define OMATH_API_EXPORT __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
/* Import prefix for functions */
|
||||
#ifdef _MSC_VER
|
||||
# define OMATH_API_IMPORT __declspec(dllimport)
|
||||
#else
|
||||
# define OMATH_API_IMPORT extern
|
||||
#endif
|
||||
|
||||
/* Resolve import/export */
|
||||
#ifdef OMATH_EXPORT
|
||||
# define OMATH_API OMATH_API_EXPORT
|
||||
#else
|
||||
# define OMATH_API OMATH_API_IMPORT
|
||||
#endif
|
||||
@@ -1,20 +0,0 @@
|
||||
//
|
||||
// Created by Vlad on 28.07.2024.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include "NavigationMesh.hpp"
|
||||
#include "omath/Vector3.hpp"
|
||||
#include "omath/omath_export.hpp"
|
||||
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
class OMATH_API Astar final
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
static std::vector<Vector3> FindPath(const Vector3& start, const Vector3& end, const NavigationMesh& navMesh);
|
||||
|
||||
};
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
//
|
||||
// Created by Vlad on 28.07.2024.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "omath/Vector3.hpp"
|
||||
#include <expected>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "omath/omath_export.hpp"
|
||||
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
|
||||
enum Error
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
class NavigationMesh final
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API std::expected<Vector3, std::string> GetClosestVertex(const Vector3& point) const;
|
||||
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API const std::vector<Vector3>& GetNeighbors(const Vector3& vertex) const;
|
||||
|
||||
[[nodiscard]]
|
||||
OMATH_API bool Empty() const;
|
||||
[[nodiscard]] std::vector<uint8_t> Serialize() const;
|
||||
OMATH_API void Deserialize(const std::vector<uint8_t>& raw);
|
||||
|
||||
std::unordered_map<Vector3, std::vector<Vector3>> m_verTextMap;
|
||||
};
|
||||
}
|
||||
30
include/omath/pathfinding/a_star.hpp
Normal file
30
include/omath/pathfinding/a_star.hpp
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by Vlad on 28.07.2024.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include "omath/pathfinding/navigation_mesh.hpp"
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
struct PathNode;
|
||||
class Astar final
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
static std::vector<Vector3<float>> FindPath(const Vector3<float>& start, const Vector3<float>& end,
|
||||
const NavigationMesh& navMesh);
|
||||
|
||||
private:
|
||||
[[nodiscard]]
|
||||
static std::vector<Vector3<float>>
|
||||
ReconstructFinalPath(const std::unordered_map<Vector3<float>, PathNode>& closedList,
|
||||
const Vector3<float>& current);
|
||||
|
||||
[[nodiscard]]
|
||||
static auto GetPerfectNode(const std::unordered_map<Vector3<float>, PathNode>& openList,
|
||||
const Vector3<float>& endVertex);
|
||||
};
|
||||
} // namespace omath::pathfinding
|
||||
38
include/omath/pathfinding/navigation_mesh.hpp
Normal file
38
include/omath/pathfinding/navigation_mesh.hpp
Normal file
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// Created by Vlad on 28.07.2024.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
|
||||
enum Error
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
class NavigationMesh final
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
std::expected<Vector3<float>, std::string> GetClosestVertex(const Vector3<float>& point) const;
|
||||
|
||||
[[nodiscard]]
|
||||
const std::vector<Vector3<float>>& GetNeighbors(const Vector3<float>& vertex) const;
|
||||
|
||||
[[nodiscard]]
|
||||
bool Empty() const;
|
||||
|
||||
[[nodiscard]] std::vector<uint8_t> Serialize() const;
|
||||
|
||||
void Deserialize(const std::vector<uint8_t>& raw);
|
||||
|
||||
std::unordered_map<Vector3<float>, std::vector<Vector3<float>>> m_verTextMap;
|
||||
};
|
||||
} // namespace omath::pathfinding
|
||||
@@ -1,39 +0,0 @@
|
||||
//
|
||||
// Created by Vlad on 6/9/2024.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include "omath/Vector3.hpp"
|
||||
#include "omath/prediction/Projectile.hpp"
|
||||
#include "omath/prediction/Target.hpp"
|
||||
#include "omath/omath_export.hpp"
|
||||
|
||||
namespace omath::prediction
|
||||
{
|
||||
class OMATH_API Engine final
|
||||
{
|
||||
public:
|
||||
explicit Engine(float gravityConstant, float simulationTimeStep,
|
||||
float maximumSimulationTime, float distanceTolerance);
|
||||
|
||||
[[nodiscard]]
|
||||
std::optional<Vector3> MaybeCalculateAimPoint(const Projectile& projectile, const Target& target) const;
|
||||
|
||||
private:
|
||||
const float m_gravityConstant;
|
||||
const float m_simulationTimeStep;
|
||||
const float m_maximumSimulationTime;
|
||||
const float m_distanceTolerance;
|
||||
|
||||
[[nodiscard]]
|
||||
std::optional<float> MaybeCalculateProjectileLaunchPitchAngle(const Projectile& projectile,
|
||||
const Vector3& targetPosition) const;
|
||||
|
||||
|
||||
[[nodiscard]]
|
||||
bool IsProjectileReachedTarget(const Vector3& targetPosition, const Projectile& projectile, float pitch, float time) const;
|
||||
|
||||
};
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
//
|
||||
// Created by Vlad on 6/9/2024.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "omath/Vector3.hpp"
|
||||
#include "omath/omath_export.hpp"
|
||||
|
||||
namespace omath::prediction
|
||||
{
|
||||
class OMATH_API Projectile final
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3 PredictPosition(float pitch, float yaw, float time, float gravity) const;
|
||||
|
||||
Vector3 m_origin;
|
||||
float m_launchSpeed{};
|
||||
float m_gravityScale{};
|
||||
};
|
||||
}
|
||||
20
include/omath/projectile_prediction/proj_pred_engine.hpp
Normal file
20
include/omath/projectile_prediction/proj_pred_engine.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by Vlad on 2/23/2025.
|
||||
//
|
||||
#pragma once
|
||||
#include "omath/projectile_prediction/projectile.hpp"
|
||||
#include "omath/projectile_prediction/target.hpp"
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
|
||||
namespace omath::projectile_prediction
|
||||
{
|
||||
class ProjPredEngine
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
virtual std::optional<Vector3<float>> MaybeCalculateAimPoint(const Projectile& projectile,
|
||||
const Target& target) const = 0;
|
||||
virtual ~ProjPredEngine() = default;
|
||||
};
|
||||
} // namespace omath::projectile_prediction
|
||||
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// Created by Vlad on 2/23/2025.
|
||||
//
|
||||
#pragma once
|
||||
#include "omath/projectile_prediction/proj_pred_engine.hpp"
|
||||
|
||||
namespace omath::projectile_prediction
|
||||
{
|
||||
class ProjPredEngineAVX2 final : public ProjPredEngine
|
||||
{
|
||||
public:
|
||||
[[nodiscard]] std::optional<Vector3<float>> MaybeCalculateAimPoint(const Projectile& projectile,
|
||||
const Target& target) const override;
|
||||
|
||||
|
||||
ProjPredEngineAVX2(float gravityConstant, float simulationTimeStep, float maximumSimulationTime);
|
||||
~ProjPredEngineAVX2() override = default;
|
||||
|
||||
private:
|
||||
[[nodiscard]] static std::optional<float> CalculatePitch(const Vector3<float>& projOrigin, const Vector3<float>& targetPos,
|
||||
float bulletGravity, float v0, float time);
|
||||
const float m_gravityConstant;
|
||||
const float m_simulationTimeStep;
|
||||
const float m_maximumSimulationTime;
|
||||
};
|
||||
} // namespace omath::projectile_prediction
|
||||
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// Created by Vlad on 6/9/2024.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include "omath/projectile_prediction/proj_pred_engine.hpp"
|
||||
#include "omath/projectile_prediction/projectile.hpp"
|
||||
#include "omath/projectile_prediction/target.hpp"
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
|
||||
namespace omath::projectile_prediction
|
||||
{
|
||||
class ProjPredEngineLegacy final : public ProjPredEngine
|
||||
{
|
||||
public:
|
||||
explicit ProjPredEngineLegacy(float gravityConstant, float simulationTimeStep, float maximumSimulationTime,
|
||||
float distanceTolerance);
|
||||
|
||||
[[nodiscard]]
|
||||
std::optional<Vector3<float>> MaybeCalculateAimPoint(const Projectile& projectile,
|
||||
const Target& target) const override;
|
||||
|
||||
private:
|
||||
const float m_gravityConstant;
|
||||
const float m_simulationTimeStep;
|
||||
const float m_maximumSimulationTime;
|
||||
const float m_distanceTolerance;
|
||||
|
||||
[[nodiscard]]
|
||||
std::optional<float> MaybeCalculateProjectileLaunchPitchAngle(const Projectile& projectile,
|
||||
const Vector3<float>& targetPosition) const;
|
||||
|
||||
|
||||
[[nodiscard]]
|
||||
bool IsProjectileReachedTarget(const Vector3<float>& targetPosition, const Projectile& projectile, float pitch,
|
||||
float time) const;
|
||||
};
|
||||
} // namespace omath::projectile_prediction
|
||||
21
include/omath/projectile_prediction/projectile.hpp
Normal file
21
include/omath/projectile_prediction/projectile.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by Vlad on 6/9/2024.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
namespace omath::projectile_prediction
|
||||
{
|
||||
class Projectile final
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> PredictPosition(float pitch, float yaw, float time, float gravity) const;
|
||||
|
||||
Vector3<float> m_origin;
|
||||
float m_launchSpeed{};
|
||||
float m_gravityScale{};
|
||||
};
|
||||
}
|
||||
@@ -3,17 +3,16 @@
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "omath/Vector3.hpp"
|
||||
#include "omath/omath_export.hpp"
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
namespace omath::prediction
|
||||
namespace omath::projectile_prediction
|
||||
{
|
||||
class OMATH_API Target final
|
||||
class Target final
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector3 PredictPosition(const float time, const float gravity) const
|
||||
constexpr Vector3<float> PredictPosition(const float time, const float gravity) const
|
||||
{
|
||||
auto predicted = m_origin + m_velocity * time;
|
||||
|
||||
@@ -23,8 +22,8 @@ namespace omath::prediction
|
||||
return predicted;
|
||||
}
|
||||
|
||||
Vector3 m_origin;
|
||||
Vector3 m_velocity;
|
||||
Vector3<float> m_origin;
|
||||
Vector3<float> m_velocity;
|
||||
bool m_isAirborne{};
|
||||
};
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
//
|
||||
// Created by Vlad on 27.08.2024.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <expected>
|
||||
#include <omath/Mat.hpp>
|
||||
#include <omath/Vector3.hpp>
|
||||
#include "ErrorCodes.hpp"
|
||||
#include <omath/Angle.hpp>
|
||||
#include <type_traits>
|
||||
#include "omath/omath_export.hpp"
|
||||
|
||||
namespace omath::projection
|
||||
{
|
||||
class OMATH_API ViewPort final
|
||||
{
|
||||
public:
|
||||
float m_width;
|
||||
float m_height;
|
||||
|
||||
[[nodiscard]] constexpr float AspectRatio() const
|
||||
{
|
||||
return m_width / m_height;
|
||||
}
|
||||
};
|
||||
using FieldOfView = const Angle<float, 0.f, 180.f, AngleFlags::Clamped>;
|
||||
|
||||
template<class Mat4x4Type, class ViewAnglesType>
|
||||
class OMATH_API Camera
|
||||
{
|
||||
|
||||
public:
|
||||
virtual ~Camera() = default;
|
||||
Camera(const Vector3& position, const ViewAnglesType& viewAngles, const ViewPort& viewPort,
|
||||
const FieldOfView& fov, const float near, const float far) :
|
||||
m_viewPort(viewPort), m_fieldOfView(fov), m_farPlaneDistance(far), m_nearPlaneDistance(near),
|
||||
m_viewAngles(viewAngles), m_origin(position)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
virtual void LookAt(const Vector3& target) = 0;
|
||||
|
||||
[[nodiscard]] virtual Mat4x4Type GetViewMatrix() const = 0;
|
||||
|
||||
[[nodiscard]] virtual Mat4x4Type GetProjectionMatrix() const = 0;
|
||||
|
||||
[[nodiscard]] Mat4x4Type GetViewProjectionMatrix()
|
||||
{
|
||||
return GetProjectionMatrix() * GetViewMatrix();
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<Vector3, Error> WorldToScreen(const Mat4x4Type& viewProj, const Vector3& worldPosition) const
|
||||
{
|
||||
auto projected = viewProj * MatColumnFromVector<float, Mat4x4Type::GetStoreOrdering()>(worldPosition);
|
||||
|
||||
if (projected.At(3, 0) == 0.0f)
|
||||
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
|
||||
|
||||
projected /= projected.At(3, 0);
|
||||
|
||||
if (IsNdcOutOfBounds(projected))
|
||||
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
|
||||
|
||||
return Vector3{(projected.At(0,0)+1) / 2 * m_viewPort.m_width , (-projected.At(1,0)+1) / 2 * m_viewPort.m_height, projected.At(2,0)};
|
||||
}
|
||||
|
||||
protected:
|
||||
ViewPort m_viewPort{};
|
||||
Angle<float, 0.f, 180.f, AngleFlags::Clamped> m_fieldOfView;
|
||||
|
||||
float m_farPlaneDistance;
|
||||
float m_nearPlaneDistance;
|
||||
|
||||
|
||||
ViewAnglesType m_viewAngles;
|
||||
Vector3 m_origin;
|
||||
|
||||
private:
|
||||
template<class Type>
|
||||
[[nodiscard]]
|
||||
constexpr static bool IsNdcOutOfBounds(const Type& ndc)
|
||||
{
|
||||
return std::ranges::any_of( ndc.RawArray(), [](const auto& val) { return val < -1 || val > 1; });
|
||||
}
|
||||
};
|
||||
} // namespace omath::projection
|
||||
181
include/omath/projection/camera.hpp
Normal file
181
include/omath/projection/camera.hpp
Normal file
@@ -0,0 +1,181 @@
|
||||
//
|
||||
// Created by Vlad on 27.08.2024.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <expected>
|
||||
#include <omath/angle.hpp>
|
||||
#include <omath/mat.hpp>
|
||||
#include <omath/vector3.hpp>
|
||||
#include <type_traits>
|
||||
#include "omath/projection/error_codes.hpp"
|
||||
|
||||
namespace omath::projection
|
||||
{
|
||||
class ViewPort final
|
||||
{
|
||||
public:
|
||||
float m_width;
|
||||
float m_height;
|
||||
|
||||
[[nodiscard]] constexpr float AspectRatio() const
|
||||
{
|
||||
return m_width / m_height;
|
||||
}
|
||||
};
|
||||
using FieldOfView = Angle<float, 0.f, 180.f, AngleFlags::Clamped>;
|
||||
|
||||
template<class Mat4x4Type, class ViewAnglesType>
|
||||
class Camera
|
||||
{
|
||||
public:
|
||||
virtual ~Camera() = default;
|
||||
Camera(const Vector3<float>& position, const ViewAnglesType& viewAngles, const ViewPort& viewPort,
|
||||
const FieldOfView& fov, const float near, const float far) :
|
||||
m_viewPort(viewPort), m_fieldOfView(fov), m_farPlaneDistance(far), m_nearPlaneDistance(near),
|
||||
m_viewAngles(viewAngles), m_origin(position)
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void LookAt(const Vector3<float>& target) = 0;
|
||||
|
||||
[[nodiscard]] virtual Mat4x4Type CalcViewMatrix() const = 0;
|
||||
|
||||
[[nodiscard]] virtual Mat4x4Type CalcProjectionMatrix() const = 0;
|
||||
|
||||
[[nodiscard]] Mat4x4Type CalcViewProjectionMatrix() const
|
||||
{
|
||||
return CalcProjectionMatrix() * CalcViewMatrix();
|
||||
}
|
||||
|
||||
public:
|
||||
[[nodiscard]] const Mat4x4Type& GetViewProjectionMatrix() const
|
||||
{
|
||||
if (!m_viewProjectionMatrix.has_value())
|
||||
m_viewProjectionMatrix = CalcViewProjectionMatrix();
|
||||
|
||||
return m_viewProjectionMatrix.value();
|
||||
}
|
||||
|
||||
void SetFieldOfView(const FieldOfView& fov)
|
||||
{
|
||||
m_fieldOfView = fov;
|
||||
m_viewProjectionMatrix = std::nullopt;
|
||||
}
|
||||
|
||||
void SetNearPlane(const float near)
|
||||
{
|
||||
m_nearPlaneDistance = near;
|
||||
m_viewProjectionMatrix = std::nullopt;
|
||||
}
|
||||
|
||||
void SetFarPlane(const float far)
|
||||
{
|
||||
m_farPlaneDistance = far;
|
||||
m_viewProjectionMatrix = std::nullopt;
|
||||
}
|
||||
|
||||
void SetViewAngles(const ViewAnglesType& viewAngles)
|
||||
{
|
||||
m_viewAngles = viewAngles;
|
||||
m_viewProjectionMatrix = std::nullopt;
|
||||
}
|
||||
|
||||
void SetOrigin(const Vector3<float>& origin)
|
||||
{
|
||||
m_origin = origin;
|
||||
m_viewProjectionMatrix = std::nullopt;
|
||||
}
|
||||
|
||||
void SetViewPort(const ViewPort& viewPort)
|
||||
{
|
||||
m_viewPort = viewPort;
|
||||
m_viewProjectionMatrix = std::nullopt;
|
||||
}
|
||||
|
||||
[[nodiscard]] const FieldOfView& GetFieldOfView() const
|
||||
{
|
||||
return m_fieldOfView;
|
||||
}
|
||||
|
||||
[[nodiscard]] const float& GetNearPlane() const
|
||||
{
|
||||
return m_nearPlaneDistance;
|
||||
}
|
||||
|
||||
[[nodiscard]] const float& GetFarPlane() const
|
||||
{
|
||||
return m_farPlaneDistance;
|
||||
}
|
||||
|
||||
[[nodiscard]] const ViewAnglesType& GetViewAngles() const
|
||||
{
|
||||
return m_viewAngles;
|
||||
}
|
||||
|
||||
[[nodiscard]] const Vector3<float>& GetOrigin() const
|
||||
{
|
||||
return m_origin;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<Vector3<float>, Error> WorldToScreen(const Vector3<float>& worldPosition) const
|
||||
{
|
||||
auto normalizedCords = WorldToViewPort(worldPosition);
|
||||
|
||||
if (!normalizedCords.has_value())
|
||||
return std::unexpected{normalizedCords.error()};
|
||||
|
||||
|
||||
return NdcToScreenPosition(*normalizedCords);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<Vector3<float>, Error> WorldToViewPort(const Vector3<float>& worldPosition) const
|
||||
{
|
||||
auto projected = GetViewProjectionMatrix() *
|
||||
MatColumnFromVector<float, Mat4x4Type::GetStoreOrdering()>(worldPosition);
|
||||
|
||||
if (projected.At(3, 0) == 0.0f)
|
||||
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
|
||||
|
||||
projected /= projected.At(3, 0);
|
||||
|
||||
if (IsNdcOutOfBounds(projected))
|
||||
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
|
||||
|
||||
return Vector3<float>{projected.At(0, 0), projected.At(1, 0), projected.At(2, 0)};
|
||||
}
|
||||
|
||||
protected:
|
||||
ViewPort m_viewPort{};
|
||||
Angle<float, 0.f, 180.f, AngleFlags::Clamped> m_fieldOfView;
|
||||
|
||||
mutable std::optional<Mat4x4Type> m_viewProjectionMatrix;
|
||||
|
||||
float m_farPlaneDistance;
|
||||
float m_nearPlaneDistance;
|
||||
|
||||
|
||||
ViewAnglesType m_viewAngles;
|
||||
Vector3<float> m_origin;
|
||||
|
||||
private:
|
||||
template<class Type>
|
||||
[[nodiscard]]
|
||||
constexpr static bool IsNdcOutOfBounds(const Type& ndc)
|
||||
{
|
||||
return std::ranges::any_of(ndc.RawArray(), [](const auto& val) { return val < -1 || val > 1; });
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector3<float> NdcToScreenPosition(const Vector3<float>& ndc) const
|
||||
{
|
||||
return
|
||||
{
|
||||
(ndc.x + 1.f) / 2.f * m_viewPort.m_width,
|
||||
(1.f - ndc.y) / 2.f * m_viewPort.m_height,
|
||||
ndc.z
|
||||
};
|
||||
}
|
||||
};
|
||||
} // namespace omath::projection
|
||||
84
include/omath/triangle.hpp
Normal file
84
include/omath/triangle.hpp
Normal file
@@ -0,0 +1,84 @@
|
||||
//
|
||||
// Created by Orange on 11/13/2024.
|
||||
//
|
||||
#pragma once
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
namespace omath
|
||||
{
|
||||
/*
|
||||
|\
|
||||
| \
|
||||
a | \ hypot
|
||||
| \
|
||||
-----
|
||||
b
|
||||
*/
|
||||
|
||||
|
||||
template<class Vector>
|
||||
class Triangle final
|
||||
{
|
||||
public:
|
||||
constexpr Triangle() = default;
|
||||
constexpr Triangle(const Vector& vertex1, const Vector& vertex2, const Vector& vertex3)
|
||||
: m_vertex1(vertex1), m_vertex2(vertex2), m_vertex3(vertex3)
|
||||
{
|
||||
}
|
||||
|
||||
Vector3<float> m_vertex1;
|
||||
Vector3<float> m_vertex2;
|
||||
Vector3<float> m_vertex3;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector3<float> CalculateNormal() const
|
||||
{
|
||||
const auto b = SideBVector();
|
||||
const auto a = SideAVector();
|
||||
return b.Cross(a).Normalized();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
float SideALength() const
|
||||
{
|
||||
return m_vertex1.DistTo(m_vertex2);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
float SideBLength() const
|
||||
{
|
||||
return m_vertex3.DistTo(m_vertex2);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector3<float> SideAVector() const
|
||||
{
|
||||
return m_vertex1 - m_vertex2;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr float Hypot() const
|
||||
{
|
||||
return m_vertex1.DistTo(m_vertex3);
|
||||
}
|
||||
[[nodiscard]]
|
||||
constexpr bool IsRectangular() const
|
||||
{
|
||||
const auto sideA = SideALength();
|
||||
const auto sideB = SideBLength();
|
||||
const auto hypot = Hypot();
|
||||
|
||||
return std::abs(sideA*sideA + sideB*sideB - hypot*hypot) <= 0.0001f;
|
||||
}
|
||||
[[nodiscard]]
|
||||
constexpr Vector3<float> SideBVector() const
|
||||
{
|
||||
return m_vertex3 - m_vertex2;
|
||||
}
|
||||
[[nodiscard]]
|
||||
constexpr Vector3<float> MidPoint() const
|
||||
{
|
||||
return (m_vertex1 + m_vertex2 + m_vertex3) / 3;
|
||||
}
|
||||
};
|
||||
} // namespace omath
|
||||
@@ -3,22 +3,31 @@
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include <tuple>
|
||||
#include <cmath>
|
||||
#include "omath/omath_export.hpp"
|
||||
#include <tuple>
|
||||
|
||||
#ifdef OMATH_IMGUI_INTEGRATION
|
||||
#include <imgui.h>
|
||||
#endif
|
||||
|
||||
|
||||
namespace omath
|
||||
{
|
||||
class OMATH_API Vector2
|
||||
|
||||
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]]
|
||||
@@ -66,7 +75,7 @@ namespace omath
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2& operator*=(const float fl)
|
||||
constexpr Vector2& operator*=(const Type& fl)
|
||||
{
|
||||
x *= fl;
|
||||
y *= fl;
|
||||
@@ -74,7 +83,7 @@ namespace omath
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2& operator/=(const float fl)
|
||||
constexpr Vector2& operator/=(const Type& fl)
|
||||
{
|
||||
x /= fl;
|
||||
y /= fl;
|
||||
@@ -82,7 +91,7 @@ namespace omath
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2& operator+=(const float fl)
|
||||
constexpr Vector2& operator+=(const Type& fl)
|
||||
{
|
||||
x += fl;
|
||||
y += fl;
|
||||
@@ -90,7 +99,7 @@ namespace omath
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2& operator-=(const float fl)
|
||||
constexpr Vector2& operator-=(const Type& fl)
|
||||
{
|
||||
x -= fl;
|
||||
y -= fl;
|
||||
@@ -99,68 +108,57 @@ 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);
|
||||
return std::hypot(this->x, this->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;
|
||||
}
|
||||
|
||||
constexpr Vector2& Abs()
|
||||
{
|
||||
//FIXME: Replace with std::abs, if it will become constexprable
|
||||
// FIXME: Replace with std::abs, if it will become constexprable
|
||||
x = x < 0 ? -x : x;
|
||||
y = y < 0 ? -y : y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class type>
|
||||
[[nodiscard]] constexpr const type& As() const
|
||||
{
|
||||
return *reinterpret_cast<const type*>(this);
|
||||
}
|
||||
template<class type>
|
||||
[[nodiscard]] constexpr type& As()
|
||||
{
|
||||
return *reinterpret_cast<type*>(this);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector2 operator-() const
|
||||
{
|
||||
return {-x, -y};
|
||||
@@ -187,16 +185,24 @@ namespace omath
|
||||
return {x / fl, y / fl};
|
||||
}
|
||||
|
||||
// Sum of elements
|
||||
[[nodiscard]] constexpr float Sum() const
|
||||
// Sum of elements
|
||||
[[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);
|
||||
}
|
||||
|
||||
#ifdef OMATH_IMGUI_INTEGRATION
|
||||
[[nodiscard]]
|
||||
ImVec2 ToImVec2() const
|
||||
{
|
||||
return {static_cast<float>(this->x), static_cast<float>(this->y)};
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
} // namespace omath
|
||||
281
include/omath/vector3.hpp
Normal file
281
include/omath/vector3.hpp
Normal file
@@ -0,0 +1,281 @@
|
||||
//
|
||||
// Created by vlad on 10/28/23.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <functional>
|
||||
#include "omath/angle.hpp"
|
||||
#include "omath/vector2.hpp"
|
||||
|
||||
namespace omath
|
||||
{
|
||||
|
||||
enum class Vector3Error
|
||||
{
|
||||
IMPOSSIBLE_BETWEEN_ANGLE,
|
||||
};
|
||||
|
||||
template<class Type> requires std::is_arithmetic_v<Type>
|
||||
class Vector3 : public Vector2<Type>
|
||||
{
|
||||
public:
|
||||
Type z = static_cast<Type>(0);
|
||||
constexpr Vector3(const Type& x, const Type& y, const Type& z) : Vector2<Type>(x, y), z(z) { }
|
||||
constexpr Vector3() : Vector2<Type>() {};
|
||||
|
||||
[[nodiscard]] constexpr bool operator==(const Vector3& src) const
|
||||
{
|
||||
return Vector2<Type>::operator==(src) && (src.z == z);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool operator!=(const Vector3& src) const
|
||||
{
|
||||
return !(*this == src);
|
||||
}
|
||||
|
||||
constexpr Vector3& operator+=(const Vector3& v)
|
||||
{
|
||||
Vector2<Type>::operator+=(v);
|
||||
z += v.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator-=(const Vector3& v)
|
||||
{
|
||||
Vector2<Type>::operator-=(v);
|
||||
z -= v.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator*=(const float fl)
|
||||
{
|
||||
Vector2<Type>::operator*=(fl);
|
||||
z *= fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator*=(const Vector3& v)
|
||||
{
|
||||
Vector2<Type>::operator*=(v);
|
||||
z *= v.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator/=(const Vector3& v)
|
||||
{
|
||||
Vector2<Type>::operator/=(v);
|
||||
z /= v.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator+=(const float fl)
|
||||
{
|
||||
Vector2<Type>::operator+=(fl);
|
||||
z += fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator/=(const float fl)
|
||||
{
|
||||
Vector2<Type>::operator/=(fl);
|
||||
z /= fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator-=(const float fl)
|
||||
{
|
||||
Vector2<Type>::operator-=(fl);
|
||||
z -= fl;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& Abs()
|
||||
{
|
||||
Vector2<Type>::Abs();
|
||||
z = z < 0.f ? -z : z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type DistToSqr(const Vector3& vOther) const
|
||||
{
|
||||
return (*this - vOther).LengthSqr();
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type Dot(const Vector3& vOther) const
|
||||
{
|
||||
return Vector2<Type>::Dot(vOther) + z * vOther.z;
|
||||
}
|
||||
|
||||
#ifndef _MSC_VER
|
||||
[[nodiscard]] constexpr Type Length() const
|
||||
{
|
||||
return std::hypot(this->x, this->y, z);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type Length2D() const
|
||||
{
|
||||
return Vector2<Type>::Length();
|
||||
}
|
||||
[[nodiscard]] Type DistTo(const Vector3& vOther) const
|
||||
{
|
||||
return (*this - vOther).Length();
|
||||
}
|
||||
[[nodiscard]] constexpr Vector3 Normalized() const
|
||||
{
|
||||
const Type length = this->Length();
|
||||
|
||||
return length != 0 ? *this / length : *this;
|
||||
}
|
||||
#else
|
||||
[[nodiscard]] Type Length() const
|
||||
{
|
||||
return std::hypot(this->x, this->y, z);
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector3 Normalized() const
|
||||
{
|
||||
const Type length = this->Length();
|
||||
|
||||
return length != 0 ? *this / length : *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] Type Length2D() const
|
||||
{
|
||||
return Vector2<Type>::Length();
|
||||
}
|
||||
|
||||
[[nodiscard]] Type DistTo(const Vector3& vOther) const
|
||||
{
|
||||
return (*this - vOther).Length();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
[[nodiscard]] constexpr Type LengthSqr() const
|
||||
{
|
||||
return Vector2<Type>::LengthSqr() + z * z;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator-() const
|
||||
{
|
||||
return {-this->x, -this->y, -z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator+(const Vector3& v) const
|
||||
{
|
||||
return {this->x + v.x, this->y + v.y, z + v.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator-(const Vector3& v) const
|
||||
{
|
||||
return {this->x - v.x, this->y - v.y, z - v.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator*(const float fl) const
|
||||
{
|
||||
return {this->x * fl, this->y * fl, z * fl};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator*(const Vector3& v) const
|
||||
{
|
||||
return {this->x * v.x, this->y * v.y, z * v.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator/(const float fl) const
|
||||
{
|
||||
return {this->x / fl, this->y / fl, z / fl};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator/(const Vector3& v) const
|
||||
{
|
||||
return {this->x / v.x, this->y / v.y, z / v.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 Cross(const Vector3 &v) const
|
||||
{
|
||||
return
|
||||
{
|
||||
this->y * v.z - z * v.y,
|
||||
z * v.x - this->x * v.z,
|
||||
this->x * v.y - this->y * v.x
|
||||
};
|
||||
}
|
||||
[[nodiscard]] constexpr Type Sum() const
|
||||
{
|
||||
return Sum2D() + z;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<Angle<float, 0.f, 180.f, AngleFlags::Clamped>, Vector3Error>
|
||||
AngleBetween(const Vector3& other) const
|
||||
{
|
||||
const auto bottom = Length() * other.Length();
|
||||
|
||||
if (bottom == 0.f)
|
||||
return std::unexpected(Vector3Error::IMPOSSIBLE_BETWEEN_ANGLE);
|
||||
|
||||
return Angle<float, 0.f, 180.f, AngleFlags::Clamped>::FromRadians(std::acos(Dot(other) / bottom));
|
||||
}
|
||||
|
||||
[[nodiscard]] bool IsPerpendicular(const Vector3& other) const
|
||||
{
|
||||
if (const auto angle = AngleBetween(other))
|
||||
return angle->AsDegrees() == 90.f;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type Sum2D() const
|
||||
{
|
||||
return Vector2<Type>::Sum();
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr std::tuple<Type, Type, Type> AsTuple() const
|
||||
{
|
||||
return std::make_tuple(this->x, this->y, z);
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector3 ViewAngleTo(const Vector3 &other) const
|
||||
{
|
||||
const float distance = DistTo(other);
|
||||
const auto delta = other - *this;
|
||||
|
||||
return
|
||||
{
|
||||
angles::RadiansToDegrees(std::asin(delta.z / distance)),
|
||||
angles::RadiansToDegrees(std::atan2(delta.y, delta.x)),
|
||||
0
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
// ReSharper disable once CppRedundantNamespaceDefinition
|
||||
namespace std
|
||||
{
|
||||
template<>
|
||||
struct hash<omath::Vector3<float>>
|
||||
{
|
||||
std::size_t operator()(const omath::Vector3<float>& vec) const noexcept
|
||||
{
|
||||
std::size_t hash = 0;
|
||||
constexpr std::hash<float> hasher;
|
||||
|
||||
hash ^= hasher(vec.x) + 0x9e3779b9 + (hash<<6) + (hash>>2);
|
||||
hash ^= hasher(vec.y) + 0x9e3779b9 + (hash<<6) + (hash>>2);
|
||||
hash ^= hasher(vec.z) + 0x9e3779b9 + (hash<<6) + (hash>>2);
|
||||
|
||||
return hash;
|
||||
}
|
||||
};
|
||||
}
|
||||
173
include/omath/vector4.hpp
Normal file
173
include/omath/vector4.hpp
Normal file
@@ -0,0 +1,173 @@
|
||||
//
|
||||
// Vector4.h
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <omath/vector3.hpp>
|
||||
|
||||
|
||||
namespace omath
|
||||
{
|
||||
template <class Type>
|
||||
class Vector4 : public Vector3<Type>
|
||||
{
|
||||
public:
|
||||
Type w;
|
||||
|
||||
constexpr Vector4(const Type& x, const Type& y, const Type& z, const Type& w) : Vector3<Type>(x, y, z), w(w) {}
|
||||
constexpr Vector4() : Vector3<Type>(), w(0) {};
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr bool operator==(const Vector4& src) const
|
||||
{
|
||||
return Vector3<Type>::operator==(src) && w == src.w;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr bool operator!=(const Vector4& src) const
|
||||
{
|
||||
return !(*this == src);
|
||||
}
|
||||
|
||||
constexpr Vector4& operator+=(const Vector4& v)
|
||||
{
|
||||
Vector3<Type>::operator+=(v);
|
||||
w += v.w;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector4& operator-=(const Vector4& v)
|
||||
{
|
||||
Vector3<Type>::operator-=(v);
|
||||
w -= v.w;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector4& operator*=(const float scalar)
|
||||
{
|
||||
Vector3<Type>::operator*=(scalar);
|
||||
w *= scalar;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector4& operator*=(const Vector4& v)
|
||||
{
|
||||
Vector3<Type>::operator*=(v);
|
||||
w *= v.w;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector4& operator/=(const float scalar)
|
||||
{
|
||||
Vector3<Type>::operator/=(scalar);
|
||||
w /= scalar;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector4& operator/=(const Vector4& v)
|
||||
{
|
||||
Vector3<Type>::operator/=(v);
|
||||
w /= v.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type LengthSqr() const
|
||||
{
|
||||
return Vector3<Type>::LengthSqr() + w * w;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type Dot(const Vector4& vOther) const
|
||||
{
|
||||
return Vector3<Type>::Dot(vOther) + w * vOther.w;
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector3<Type> Length() const
|
||||
{
|
||||
return std::sqrt(LengthSqr());
|
||||
}
|
||||
|
||||
constexpr Vector4& Abs()
|
||||
{
|
||||
Vector3<Type>::Abs();
|
||||
w = w < 0.f ? -w : w;
|
||||
|
||||
return *this;
|
||||
}
|
||||
constexpr Vector4& Clamp(const Type& min, const Type& max)
|
||||
{
|
||||
this->x = std::clamp(this->x, min, max);
|
||||
this->y = std::clamp(this->y, min, max);
|
||||
this->z = std::clamp(this->z, min, max);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator-() const
|
||||
{
|
||||
return {-this->x, -this->y, -this->z, -w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator+(const Vector4& v) const
|
||||
{
|
||||
return {this->x + v.x, this->y + v.y, this->z + v.z, w + v.w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator-(const Vector4& v) const
|
||||
{
|
||||
return {this->x - v.x, this->y - v.y, this->z - v.z, w - v.w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator*(const Type& scalar) const
|
||||
{
|
||||
return {this->x * scalar, this->y * scalar, this->z * scalar, w * scalar};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator*(const Vector4& v) const
|
||||
{
|
||||
return {this->x * v.x, this->y * v.y, this->z * v.z, w * v.w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator/(const Type& scalar) const
|
||||
{
|
||||
return {this->x / scalar, this->y / scalar, this->z / scalar, w / scalar};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator/(const Vector4& v) const
|
||||
{
|
||||
return {this->x / v.x, this->y / v.y, this->z / v.z, w / v.w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Type Sum() const
|
||||
{
|
||||
return Vector3<Type>::Sum() + w;
|
||||
}
|
||||
|
||||
#ifdef OMATH_IMGUI_INTEGRATION
|
||||
[[nodiscard]]
|
||||
ImVec4 ToImVec4() const
|
||||
{
|
||||
return
|
||||
{
|
||||
static_cast<float>(this->x),
|
||||
static_cast<float>(this->y),
|
||||
static_cast<float>(this->z),
|
||||
static_cast<float>(w),
|
||||
};
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
@@ -3,12 +3,10 @@
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include "omath/omath_export.hpp"
|
||||
|
||||
namespace omath
|
||||
{
|
||||
template<class PitchType, class YawType, class RollType>
|
||||
struct OMATH_API ViewAngles
|
||||
struct ViewAngles
|
||||
{
|
||||
PitchType pitch;
|
||||
YawType yaw;
|
||||
1
source/3d_primitives/CMakeLists.txt
Normal file
1
source/3d_primitives/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
target_sources(omath PRIVATE box.cpp)
|
||||
56
source/3d_primitives/box.cpp
Normal file
56
source/3d_primitives/box.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// Created by Vlad on 4/18/2025.
|
||||
//
|
||||
#include "omath/3d_primitives/box.hpp"
|
||||
|
||||
|
||||
namespace omath::primitives
|
||||
{
|
||||
std::array<Triangle<Vector3<float>>, 12> CreateBox(const Vector3<float>& top, const Vector3<float>& bottom,
|
||||
const Vector3<float>& dirForward,
|
||||
const Vector3<float>& dirRight,
|
||||
const float ratio)
|
||||
{
|
||||
const auto height = top.DistTo(bottom);
|
||||
const auto sideSize = height / ratio;
|
||||
|
||||
// corner layout (0‑3 bottom, 4‑7 top)
|
||||
std::array<Vector3<float>, 8> p;
|
||||
p[0] = bottom + (dirForward + dirRight) * sideSize; // front‑right‑bottom
|
||||
p[1] = bottom + (dirForward - dirRight) * sideSize; // front‑left‑bottom
|
||||
p[2] = bottom + (-dirForward + dirRight) * sideSize; // back‑right‑bottom
|
||||
p[3] = bottom + (-dirForward - dirRight) * sideSize; // back‑left‑bottom
|
||||
p[4] = top + (dirForward + dirRight) * sideSize; // front‑right‑top
|
||||
p[5] = top + (dirForward - dirRight) * sideSize; // front‑left‑top
|
||||
p[6] = top + (-dirForward + dirRight) * sideSize; // back‑right‑top
|
||||
p[7] = top + (-dirForward - dirRight) * sideSize; // back‑left‑top
|
||||
|
||||
std::array<Triangle<Vector3<float>>, 12> poly;
|
||||
|
||||
// bottom face (+Y up ⇒ wind CW when viewed from above)
|
||||
poly[0] = {p[0], p[2], p[3]};
|
||||
poly[1] = {p[0], p[3], p[1]};
|
||||
|
||||
// top face
|
||||
poly[2] = {p[4], p[7], p[6]};
|
||||
poly[3] = {p[4], p[5], p[7]};
|
||||
|
||||
// front face
|
||||
poly[4] = {p[0], p[5], p[1]};
|
||||
poly[5] = {p[0], p[4], p[5]};
|
||||
|
||||
// right face
|
||||
poly[6] = {p[0], p[6], p[2]};
|
||||
poly[7] = {p[0], p[4], p[6]};
|
||||
|
||||
// back face
|
||||
poly[8] = {p[2], p[7], p[3]};
|
||||
poly[9] = {p[2], p[6], p[7]};
|
||||
|
||||
// left face
|
||||
poly[10] = {p[1], p[7], p[5]};
|
||||
poly[11] = {p[1], p[3], p[7]};
|
||||
|
||||
return poly;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,11 @@
|
||||
target_sources(omath PRIVATE
|
||||
Vector3.cpp
|
||||
Matrix.cpp
|
||||
matrix.cpp
|
||||
color.cpp
|
||||
Vector4.cpp
|
||||
Vector2.cpp
|
||||
Triangle3d.cpp
|
||||
)
|
||||
|
||||
add_subdirectory(prediction)
|
||||
add_subdirectory(projectile_prediction)
|
||||
add_subdirectory(pathfinding)
|
||||
add_subdirectory(projection)
|
||||
add_subdirectory(collision)
|
||||
add_subdirectory(engines)
|
||||
add_subdirectory(engines)
|
||||
add_subdirectory(3d_primitives)
|
||||
@@ -1,36 +0,0 @@
|
||||
#include "omath/Triangle3d.hpp"
|
||||
|
||||
|
||||
namespace omath
|
||||
{
|
||||
Triangle3d::Triangle3d(const Vector3 &vertex1, const Vector3 &vertex2, const Vector3 &vertex3)
|
||||
: m_vertex1(vertex1), m_vertex2(vertex2), m_vertex3(vertex3)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Vector3 Triangle3d::CalculateNormal() const
|
||||
{
|
||||
return (m_vertex1 - m_vertex2).Cross(m_vertex3 - m_vertex1).Normalized();
|
||||
}
|
||||
|
||||
float Triangle3d::SideALength() const
|
||||
{
|
||||
return m_vertex1.DistTo(m_vertex2);
|
||||
}
|
||||
|
||||
float Triangle3d::SideBLength() const
|
||||
{
|
||||
return m_vertex3.DistTo(m_vertex2);
|
||||
}
|
||||
|
||||
Vector3 Triangle3d::SideAVector() const
|
||||
{
|
||||
return m_vertex1 - m_vertex2;
|
||||
}
|
||||
|
||||
Vector3 Triangle3d::SideBVector() const
|
||||
{
|
||||
return m_vertex3 - m_vertex2;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
//
|
||||
// Created by Vlad on 02.09.2024.
|
||||
//
|
||||
#include "omath/Vector2.hpp"
|
||||
#include <cmath>
|
||||
|
||||
|
||||
namespace omath
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
//
|
||||
// Created by vlad on 10/28/23.
|
||||
//
|
||||
|
||||
#include <omath/Vector3.hpp>
|
||||
#include <cmath>
|
||||
#include <omath/Angles.hpp>
|
||||
|
||||
namespace omath
|
||||
{
|
||||
Vector3 Vector3::ViewAngleTo(const Vector3 &other) const
|
||||
{
|
||||
const float distance = DistTo(other);
|
||||
const auto delta = other - *this;
|
||||
|
||||
return
|
||||
{
|
||||
angles::RadiansToDegrees(std::asin(delta.z / distance)),
|
||||
angles::RadiansToDegrees(std::atan2(delta.y, delta.x)),
|
||||
0.f
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
//
|
||||
// Vector4.cpp
|
||||
//
|
||||
|
||||
#include "omath/Vector4.hpp"
|
||||
#include <cmath>
|
||||
|
||||
|
||||
namespace omath
|
||||
{
|
||||
|
||||
float Vector4::Length() const
|
||||
{
|
||||
return std::sqrt(LengthSqr());
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
target_sources(omath PRIVATE
|
||||
LineTracer.cpp
|
||||
line_tracer.cpp
|
||||
)
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
//
|
||||
// Created by Orange on 11/13/2024.
|
||||
//
|
||||
#include "omath/collision/LineTracer.hpp"
|
||||
#include "omath/collision/line_tracer.hpp"
|
||||
|
||||
namespace omath::collision
|
||||
{
|
||||
bool LineTracer::CanTraceLine(const Ray &ray, const Triangle3d &triangle)
|
||||
bool LineTracer::CanTraceLine(const Ray& ray, const Triangle<Vector3<float>>& triangle)
|
||||
{
|
||||
return GetRayHitPoint(ray, triangle) == ray.end;
|
||||
}
|
||||
Vector3 Ray::DirectionVector() const
|
||||
Vector3<float> Ray::DirectionVector() const
|
||||
{
|
||||
return end - start;
|
||||
}
|
||||
|
||||
Vector3 Ray::DirectionVectorNormalized() const
|
||||
Vector3<float> Ray::DirectionVectorNormalized() const
|
||||
{
|
||||
return DirectionVector().Normalized();
|
||||
}
|
||||
|
||||
Vector3 LineTracer::GetRayHitPoint(const Ray &ray, const Triangle3d &triangle)
|
||||
Vector3<float> LineTracer::GetRayHitPoint(const Ray& ray, const Triangle<Vector3<float>>& triangle)
|
||||
{
|
||||
constexpr float kEpsilon = std::numeric_limits<float>::epsilon();
|
||||
|
||||
@@ -41,7 +41,7 @@ namespace omath::collision
|
||||
const auto u = t.Dot(p) * invDet;
|
||||
|
||||
|
||||
if ((u < 0 && std::abs(u) > kEpsilon) || (u > 1 && std::abs(u-1) > kEpsilon))
|
||||
if ((u < 0 && std::abs(u) > kEpsilon) || (u > 1 && std::abs(u - 1) > kEpsilon))
|
||||
return ray.end;
|
||||
|
||||
const auto q = t.Cross(sideA);
|
||||
@@ -54,9 +54,14 @@ namespace omath::collision
|
||||
const auto tHit = sideB.Dot(q) * invDet;
|
||||
|
||||
|
||||
if (tHit <= kEpsilon)
|
||||
if (ray.infinite_length)
|
||||
{
|
||||
if (tHit <= kEpsilon)
|
||||
return ray.end;
|
||||
}
|
||||
else if (tHit <= kEpsilon || tHit > 1.0f - kEpsilon)
|
||||
return ray.end;
|
||||
|
||||
return ray.start + rayDir * tHit;
|
||||
}
|
||||
}
|
||||
} // namespace omath::collision
|
||||
@@ -2,7 +2,7 @@
|
||||
// Created by vlad on 2/4/24.
|
||||
//
|
||||
|
||||
#include "omath/Color.hpp"
|
||||
#include "omath/color.hpp"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
add_subdirectory(Source)
|
||||
add_subdirectory(source_engine)
|
||||
add_subdirectory(opengl_engine)
|
||||
add_subdirectory(iw_engine)
|
||||
add_subdirectory(unity_engine)
|
||||
@@ -1 +0,0 @@
|
||||
target_sources(omath PRIVATE Camera.cpp)
|
||||
1
source/engines/iw_engine/CMakeLists.txt
Normal file
1
source/engines/iw_engine/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
target_sources(omath PRIVATE camera.cpp formulas.cpp)
|
||||
34
source/engines/iw_engine/camera.cpp
Normal file
34
source/engines/iw_engine/camera.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// Created by Vlad on 3/17/2025.
|
||||
//
|
||||
#include "omath/engines/iw_engine/camera.hpp"
|
||||
#include "omath/engines/iw_engine/formulas.hpp"
|
||||
|
||||
namespace omath::iw_engine
|
||||
{
|
||||
|
||||
Camera::Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, const float near, const float far) :
|
||||
projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far)
|
||||
{
|
||||
}
|
||||
void Camera::LookAt([[maybe_unused]] const Vector3<float>& target)
|
||||
{
|
||||
const float distance = m_origin.DistTo(target);
|
||||
const auto delta = target - m_origin;
|
||||
|
||||
|
||||
m_viewAngles.pitch = PitchAngle::FromRadians(std::asin(delta.z / distance));
|
||||
m_viewAngles.yaw = -YawAngle::FromRadians(std::atan2(delta.y, delta.x));
|
||||
m_viewAngles.roll = RollAngle::FromRadians(0.f);
|
||||
}
|
||||
Mat4x4 Camera::CalcViewMatrix() const
|
||||
{
|
||||
return iw_engine::CalcViewMatrix(m_viewAngles, m_origin);
|
||||
}
|
||||
Mat4x4 Camera::CalcProjectionMatrix() const
|
||||
{
|
||||
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance,
|
||||
m_farPlaneDistance);
|
||||
}
|
||||
} // namespace omath::openg
|
||||
54
source/engines/iw_engine/formulas.cpp
Normal file
54
source/engines/iw_engine/formulas.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// Created by Vlad on 3/19/2025.
|
||||
//
|
||||
#include "omath/engines/iw_engine/formulas.hpp"
|
||||
|
||||
|
||||
namespace omath::iw_engine
|
||||
{
|
||||
|
||||
Vector3<float> ForwardVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsForward);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
|
||||
Vector3<float> RightVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsRight);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
Vector3<float> UpVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsUp);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
Mat4x4 RotationMatrix(const ViewAngles& angles)
|
||||
{
|
||||
return MatRotationAxisZ(angles.yaw) * MatRotationAxisY(angles.pitch) * MatRotationAxisX(angles.roll);
|
||||
}
|
||||
|
||||
Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
|
||||
{
|
||||
return MatCameraView(ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin);
|
||||
}
|
||||
|
||||
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
|
||||
const float far)
|
||||
{
|
||||
// NOTE: Need magic number to fix fov calculation, since IW engine inherit Quake proj matrix calculation
|
||||
constexpr auto kMultiplyFactor = 0.75f;
|
||||
|
||||
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f) * kMultiplyFactor;
|
||||
|
||||
return {
|
||||
{1.f / (aspectRatio * fovHalfTan), 0, 0, 0},
|
||||
{0, 1.f / (fovHalfTan), 0, 0},
|
||||
{0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)},
|
||||
{0, 0, 1, 0},
|
||||
};
|
||||
};
|
||||
} // namespace omath::iw_engine
|
||||
1
source/engines/opengl_engine/CMakeLists.txt
Normal file
1
source/engines/opengl_engine/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
target_sources(omath PRIVATE camera.cpp formulas.cpp)
|
||||
35
source/engines/opengl_engine/camera.cpp
Normal file
35
source/engines/opengl_engine/camera.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// Created by Orange on 12/23/2024.
|
||||
//
|
||||
#include "omath/engines/opengl_engine/camera.hpp"
|
||||
#include "omath/engines/opengl_engine/formulas.hpp"
|
||||
|
||||
|
||||
namespace omath::opengl_engine
|
||||
{
|
||||
|
||||
Camera::Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, const float near, const float far) :
|
||||
projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far)
|
||||
{
|
||||
}
|
||||
void Camera::LookAt([[maybe_unused]] const Vector3<float>& target)
|
||||
{
|
||||
const float distance = m_origin.DistTo(target);
|
||||
const auto delta = target - m_origin;
|
||||
|
||||
|
||||
m_viewAngles.pitch = PitchAngle::FromRadians(std::asin(delta.z / distance));
|
||||
m_viewAngles.yaw = -YawAngle::FromRadians(std::atan2(delta.y, delta.x));
|
||||
m_viewAngles.roll = RollAngle::FromRadians(0.f);
|
||||
}
|
||||
Mat4x4 Camera::CalcViewMatrix() const
|
||||
{
|
||||
return opengl_engine::CalcViewMatrix(m_viewAngles, m_origin);
|
||||
}
|
||||
Mat4x4 Camera::CalcProjectionMatrix() const
|
||||
{
|
||||
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance,
|
||||
m_farPlaneDistance);
|
||||
}
|
||||
} // namespace omath::opengl
|
||||
51
source/engines/opengl_engine/formulas.cpp
Normal file
51
source/engines/opengl_engine/formulas.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// Created by Vlad on 3/19/2025.
|
||||
//
|
||||
#include "omath/engines/opengl_engine/formulas.hpp"
|
||||
|
||||
|
||||
namespace omath::opengl_engine
|
||||
{
|
||||
|
||||
Vector3<float> ForwardVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector<float, MatStoreType::COLUMN_MAJOR>(kAbsForward);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
Vector3<float> RightVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector<float, MatStoreType::COLUMN_MAJOR>(kAbsRight);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
Vector3<float> UpVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector<float, MatStoreType::COLUMN_MAJOR>(kAbsUp);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
|
||||
{
|
||||
return MatCameraView<float, MatStoreType::COLUMN_MAJOR>(-ForwardVector(angles), RightVector(angles),
|
||||
UpVector(angles), cam_origin);
|
||||
}
|
||||
Mat4x4 RotationMatrix(const ViewAngles& angles)
|
||||
{
|
||||
return MatRotationAxisX<float, MatStoreType::COLUMN_MAJOR>(-angles.pitch) *
|
||||
MatRotationAxisY<float, MatStoreType::COLUMN_MAJOR>(-angles.yaw) *
|
||||
MatRotationAxisZ<float, MatStoreType::COLUMN_MAJOR>(angles.roll);
|
||||
}
|
||||
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
|
||||
const float far)
|
||||
{
|
||||
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
|
||||
|
||||
return {
|
||||
{1.f / (aspectRatio * fovHalfTan), 0, 0, 0},
|
||||
{0, 1.f / (fovHalfTan), 0, 0},
|
||||
{0, 0, -(far + near) / (far - near), -(2.f * far * near) / (far - near)},
|
||||
{0, 0, -1, 0},
|
||||
};
|
||||
}
|
||||
} // namespace omath::opengl_engine
|
||||
1
source/engines/source_engine/CMakeLists.txt
Normal file
1
source/engines/source_engine/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
target_sources(omath PRIVATE camera.cpp formulas.cpp)
|
||||
@@ -1,19 +1,19 @@
|
||||
//
|
||||
// Created by Orange on 12/4/2024.
|
||||
//
|
||||
#include "omath/engines/Source/Camera.hpp"
|
||||
#include "omath/engines/Source/Formulas.hpp"
|
||||
#include "omath/engines/source_engine/camera.hpp"
|
||||
#include "omath/engines/source_engine/formulas.hpp"
|
||||
|
||||
|
||||
namespace omath::source
|
||||
namespace omath::source_engine
|
||||
{
|
||||
|
||||
Camera::Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
Camera::Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const projection::FieldOfView& fov, const float near, const float far) :
|
||||
projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far)
|
||||
{
|
||||
}
|
||||
void Camera::LookAt(const Vector3& target)
|
||||
void Camera::LookAt(const Vector3<float>& target)
|
||||
{
|
||||
const float distance = m_origin.DistTo(target);
|
||||
const auto delta = target - m_origin;
|
||||
@@ -24,13 +24,14 @@ namespace omath::source
|
||||
m_viewAngles.roll = RollAngle::FromRadians(0.f);
|
||||
}
|
||||
|
||||
Mat4x4 Camera::GetViewMatrix() const
|
||||
Mat4x4 Camera::CalcViewMatrix() const
|
||||
{
|
||||
return CalcViewMatrix(m_viewAngles, m_origin);
|
||||
return source_engine::CalcViewMatrix(m_viewAngles, m_origin);
|
||||
}
|
||||
|
||||
Mat4x4 Camera::GetProjectionMatrix() const
|
||||
Mat4x4 Camera::CalcProjectionMatrix() const
|
||||
{
|
||||
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance, m_farPlaneDistance);
|
||||
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance,
|
||||
m_farPlaneDistance);
|
||||
}
|
||||
} // namespace omath::source
|
||||
54
source/engines/source_engine/formulas.cpp
Normal file
54
source/engines/source_engine/formulas.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// Created by Vlad on 3/19/2025.
|
||||
//
|
||||
#include <omath/engines/source_engine/formulas.hpp>
|
||||
|
||||
|
||||
namespace omath::source_engine
|
||||
{
|
||||
Vector3<float> ForwardVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsForward);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
|
||||
Mat4x4 RotationMatrix(const ViewAngles& angles)
|
||||
{
|
||||
return MatRotationAxisZ(angles.yaw) * MatRotationAxisY(angles.pitch) * MatRotationAxisX(angles.roll);
|
||||
}
|
||||
|
||||
Vector3<float> RightVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsRight);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
Vector3<float> UpVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsUp);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
|
||||
Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
|
||||
{
|
||||
return MatCameraView(ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin);
|
||||
}
|
||||
|
||||
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
|
||||
const float far)
|
||||
{
|
||||
// NOTE: Need magic number to fix fov calculation, since source inherit Quake proj matrix calculation
|
||||
constexpr auto kMultiplyFactor = 0.75f;
|
||||
|
||||
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f) * kMultiplyFactor;
|
||||
|
||||
return {
|
||||
{1.f / (aspectRatio * fovHalfTan), 0, 0, 0},
|
||||
{0, 1.f / (fovHalfTan), 0, 0},
|
||||
{0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)},
|
||||
{0, 0, 1, 0},
|
||||
};
|
||||
}
|
||||
} // namespace omath::source_engine
|
||||
1
source/engines/unity_engine/CMakeLists.txt
Normal file
1
source/engines/unity_engine/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
target_sources(omath PRIVATE formulas.cpp camera.cpp)
|
||||
28
source/engines/unity_engine/camera.cpp
Normal file
28
source/engines/unity_engine/camera.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created by Vlad on 3/22/2025.
|
||||
//
|
||||
#include <omath/engines/unity_engine/camera.hpp>
|
||||
#include <omath/engines/unity_engine/formulas.hpp>
|
||||
|
||||
|
||||
namespace omath::unity_engine
|
||||
{
|
||||
Camera::Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const projection::FieldOfView& fov, const float near, const float far) :
|
||||
projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far)
|
||||
{
|
||||
}
|
||||
void Camera::LookAt([[maybe_unused]] const Vector3<float>& target)
|
||||
{
|
||||
throw std::runtime_error("Not implemented");
|
||||
}
|
||||
Mat4x4 Camera::CalcViewMatrix() const
|
||||
{
|
||||
return unity_engine::CalcViewMatrix(m_viewAngles, m_origin);
|
||||
}
|
||||
Mat4x4 Camera::CalcProjectionMatrix() const
|
||||
{
|
||||
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance,
|
||||
m_farPlaneDistance);
|
||||
}
|
||||
} // namespace omath::unity_engine
|
||||
51
source/engines/unity_engine/formulas.cpp
Normal file
51
source/engines/unity_engine/formulas.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// Created by Vlad on 3/22/2025.
|
||||
//
|
||||
#include "omath/engines/unity_engine/formulas.hpp"
|
||||
|
||||
|
||||
|
||||
namespace omath::unity_engine
|
||||
{
|
||||
Vector3<float> ForwardVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsForward);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
Vector3<float> RightVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsRight);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
Vector3<float> UpVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsUp);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
|
||||
{
|
||||
return MatCameraView<float, MatStoreType::ROW_MAJOR>(ForwardVector(angles), -RightVector(angles),
|
||||
UpVector(angles), cam_origin);
|
||||
}
|
||||
Mat4x4 RotationMatrix(const ViewAngles& angles)
|
||||
{
|
||||
return MatRotationAxisX<float, MatStoreType::ROW_MAJOR>(angles.pitch) *
|
||||
MatRotationAxisY<float, MatStoreType::ROW_MAJOR>(angles.yaw) *
|
||||
MatRotationAxisZ<float, MatStoreType::ROW_MAJOR>(angles.roll);
|
||||
}
|
||||
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
|
||||
const float far)
|
||||
{
|
||||
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
|
||||
|
||||
return {
|
||||
{1.f / (aspectRatio * fovHalfTan), 0, 0, 0},
|
||||
{0, 1.f / (fovHalfTan), 0, 0},
|
||||
{0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)},
|
||||
{0, 0, -1.f, 0},
|
||||
};
|
||||
}
|
||||
} // namespace omath::unity_engine
|
||||
@@ -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>
|
||||
@@ -74,6 +74,11 @@ namespace omath
|
||||
{
|
||||
return m_rows;
|
||||
}
|
||||
|
||||
float& Matrix::operator[](const size_t row, const size_t column)
|
||||
{
|
||||
return At(row, column);
|
||||
}
|
||||
|
||||
Matrix::Matrix(Matrix&& other) noexcept
|
||||
{
|
||||
@@ -310,7 +315,7 @@ namespace omath
|
||||
};
|
||||
}
|
||||
|
||||
Matrix Matrix::TranslationMatrix(const Vector3& diff)
|
||||
Matrix Matrix::TranslationMatrix(const Vector3<float>& diff)
|
||||
{
|
||||
return {
|
||||
{1.f, 0.f, 0.f, 0.f},
|
||||
@@ -320,7 +325,7 @@ namespace omath
|
||||
};
|
||||
}
|
||||
|
||||
Matrix Matrix::OrientationMatrix(const Vector3& forward, const Vector3& right, const Vector3& up)
|
||||
Matrix Matrix::OrientationMatrix(const Vector3<float>& forward, const Vector3<float>& right, const Vector3<float>& up)
|
||||
{
|
||||
return {
|
||||
{right.x, up.x, forward.x, 0.f},
|
||||
@@ -1,62 +0,0 @@
|
||||
//
|
||||
// Created by Vlad on 28.07.2024.
|
||||
//
|
||||
#include "omath/pathfinding/Astar.hpp"
|
||||
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
struct PathNode final
|
||||
{
|
||||
std::optional<Vector3> cameFrom;
|
||||
float gCost = 0.f;
|
||||
};
|
||||
|
||||
|
||||
std::vector<Vector3> Astar::FindPath(const Vector3 &start, const Vector3 &end, const NavigationMesh &navMesh)
|
||||
{
|
||||
std::unordered_map<Vector3, PathNode> closedList;
|
||||
std::unordered_map<Vector3, PathNode> openList;
|
||||
|
||||
const auto startVertex = navMesh.GetClosestVertex(start).value();
|
||||
const auto endVertex = navMesh.GetClosestVertex(end).value();
|
||||
|
||||
openList.emplace(startVertex, PathNode{std::nullopt, 0.f});
|
||||
|
||||
while (!openList.empty())
|
||||
{
|
||||
const auto perfectVertex = *std::ranges::min_element(openList,
|
||||
[&endVertex](const auto& a, const auto& b) -> bool
|
||||
{
|
||||
const auto aCost = a.second.gCost + a.first.DistTo(endVertex);
|
||||
const auto bCost = b.second.gCost + b.first.DistTo(endVertex);
|
||||
return aCost < bCost;
|
||||
});
|
||||
|
||||
closedList.emplace(perfectVertex);
|
||||
openList.erase(perfectVertex.first);
|
||||
|
||||
for (const auto& neighbor : navMesh.GetNeighbors(perfectVertex.first))
|
||||
if (!closedList.contains(neighbor))
|
||||
openList.emplace(neighbor, PathNode{perfectVertex.first, neighbor.DistTo(perfectVertex.first) + perfectVertex.second.gCost});
|
||||
|
||||
|
||||
if (perfectVertex.first != endVertex)
|
||||
continue;
|
||||
|
||||
std::vector<Vector3> path = {};
|
||||
|
||||
for (std::optional current = perfectVertex.first; current; current = closedList.at(*current).cameFrom )
|
||||
path.push_back(current.value());
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
target_sources(omath PRIVATE NavigationMesh.cpp Astar.cpp)
|
||||
target_sources(omath PRIVATE navigation_mesh.cpp a_star.cpp)
|
||||
100
source/pathfinding/a_star.cpp
Normal file
100
source/pathfinding/a_star.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
//
|
||||
// Created by Vlad on 28.07.2024.
|
||||
//
|
||||
#include "omath/pathfinding/a_star.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
struct PathNode final
|
||||
{
|
||||
std::optional<Vector3<float>> cameFrom;
|
||||
float gCost = 0.f;
|
||||
};
|
||||
|
||||
|
||||
std::vector<Vector3<float>> Astar::ReconstructFinalPath(const std::unordered_map<Vector3<float>, PathNode>& closedList,
|
||||
const Vector3<float>& current)
|
||||
{
|
||||
std::vector<Vector3<float>> path;
|
||||
std::optional currentOpt = current;
|
||||
|
||||
while (currentOpt)
|
||||
{
|
||||
path.push_back(*currentOpt);
|
||||
|
||||
auto it = closedList.find(*currentOpt);
|
||||
|
||||
if (it == closedList.end())
|
||||
break;
|
||||
|
||||
currentOpt = it->second.cameFrom;
|
||||
}
|
||||
|
||||
std::ranges::reverse(path);
|
||||
return path;
|
||||
}
|
||||
auto Astar::GetPerfectNode(const std::unordered_map<Vector3<float>, PathNode>& openList, const Vector3<float>& endVertex)
|
||||
{
|
||||
return std::ranges::min_element(openList,
|
||||
[&endVertex](const auto& a, const auto& b)
|
||||
{
|
||||
const float fA = a.second.gCost + a.first.DistTo(endVertex);
|
||||
const float fB = b.second.gCost + b.first.DistTo(endVertex);
|
||||
return fA < fB;
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<Vector3<float>> Astar::FindPath(const Vector3<float>& start, const Vector3<float>& end, const NavigationMesh& navMesh)
|
||||
{
|
||||
std::unordered_map<Vector3<float>, PathNode> closedList;
|
||||
std::unordered_map<Vector3<float>, PathNode> openList;
|
||||
|
||||
auto maybeStartVertex = navMesh.GetClosestVertex(start);
|
||||
auto maybeEndVertex = navMesh.GetClosestVertex(end);
|
||||
|
||||
if (!maybeStartVertex || !maybeEndVertex)
|
||||
return {};
|
||||
|
||||
const auto startVertex = maybeStartVertex.value();
|
||||
const auto endVertex = maybeEndVertex.value();
|
||||
|
||||
|
||||
openList.emplace(startVertex, PathNode{std::nullopt, 0.f});
|
||||
|
||||
while (!openList.empty())
|
||||
{
|
||||
auto currentIt = GetPerfectNode(openList, endVertex);
|
||||
|
||||
const auto current = currentIt->first;
|
||||
const auto currentNode = currentIt->second;
|
||||
|
||||
if (current == endVertex)
|
||||
return ReconstructFinalPath(closedList, current);
|
||||
|
||||
|
||||
closedList.emplace(current, currentNode);
|
||||
openList.erase(currentIt);
|
||||
|
||||
for (const auto& neighbor: navMesh.GetNeighbors(current))
|
||||
{
|
||||
if (closedList.contains(neighbor))
|
||||
continue;
|
||||
|
||||
const float tentativeGCost = currentNode.gCost + neighbor.DistTo(current);
|
||||
|
||||
const auto openIt = openList.find(neighbor);
|
||||
|
||||
if (openIt == openList.end() || tentativeGCost < openIt->second.gCost)
|
||||
openList[neighbor] = PathNode{current, tentativeGCost};
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
} // namespace omath::pathfinding
|
||||
@@ -1,13 +1,13 @@
|
||||
//
|
||||
// Created by Vlad on 28.07.2024.
|
||||
//
|
||||
#include "omath/pathfinding/NavigationMesh.hpp"
|
||||
#include "omath/pathfinding/navigation_mesh.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
std::expected<Vector3, std::string> NavigationMesh::GetClosestVertex(const Vector3 &point) const
|
||||
std::expected<Vector3<float>, std::string> NavigationMesh::GetClosestVertex(const Vector3<float> &point) const
|
||||
{
|
||||
const auto res = std::ranges::min_element(m_verTextMap,
|
||||
[&point](const auto& a, const auto& b)
|
||||
@@ -21,7 +21,7 @@ namespace omath::pathfinding
|
||||
return res->first;
|
||||
}
|
||||
|
||||
const std::vector<Vector3>& NavigationMesh::GetNeighbors(const Vector3 &vertex) const
|
||||
const std::vector<Vector3<float>>& NavigationMesh::GetNeighbors(const Vector3<float> &vertex) const
|
||||
{
|
||||
return m_verTextMap.at(vertex);
|
||||
}
|
||||
@@ -73,18 +73,18 @@ namespace omath::pathfinding
|
||||
|
||||
while (offset < raw.size())
|
||||
{
|
||||
Vector3 vertex;
|
||||
Vector3<float> vertex;
|
||||
loadFromVector(raw, offset, vertex);
|
||||
|
||||
uint16_t neighborsCount;
|
||||
loadFromVector(raw, offset, neighborsCount);
|
||||
|
||||
std::vector<Vector3> neighbors;
|
||||
std::vector<Vector3<float>> neighbors;
|
||||
neighbors.reserve(neighborsCount);
|
||||
|
||||
for (size_t i = 0; i < neighborsCount; ++i)
|
||||
{
|
||||
Vector3 neighbor;
|
||||
Vector3<float> neighbor;
|
||||
loadFromVector(raw, offset, neighbor);
|
||||
neighbors.push_back(neighbor);
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
target_sources(omath PRIVATE Engine.cpp Projectile.cpp Target.cpp)
|
||||
@@ -1,21 +0,0 @@
|
||||
//
|
||||
// Created by Vlad on 6/9/2024.
|
||||
//
|
||||
|
||||
#include "omath/prediction/Projectile.hpp"
|
||||
#include <cmath>
|
||||
#include <omath/engines/Source/Formulas.hpp>
|
||||
|
||||
namespace omath::prediction
|
||||
{
|
||||
Vector3 Projectile::PredictPosition(const float pitch, const float yaw, const float time, const float gravity) const
|
||||
{
|
||||
auto currentPos = m_origin + source::ForwardVector({source::PitchAngle::FromDegrees(-pitch),
|
||||
source::YawAngle::FromDegrees(yaw),
|
||||
source::RollAngle::FromDegrees(0)}) *
|
||||
m_launchSpeed * time;
|
||||
currentPos.z -= (gravity * m_gravityScale) * (time * time) * 0.5f;
|
||||
|
||||
return currentPos;
|
||||
}
|
||||
} // namespace omath::prediction
|
||||
1
source/projectile_prediction/CMakeLists.txt
Normal file
1
source/projectile_prediction/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
target_sources(omath PRIVATE proj_pred_engine_legacy.cpp projectile.cpp target.cpp proj_pred_engine_avx2.cpp proj_pred_engine.cpp)
|
||||
10
source/projectile_prediction/proj_pred_engine.cpp
Normal file
10
source/projectile_prediction/proj_pred_engine.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
//
|
||||
// Created by Vlad on 2/23/2025.
|
||||
//
|
||||
#include "omath/projectile_prediction/proj_pred_engine.hpp"
|
||||
|
||||
|
||||
namespace omath::projectile_prediction
|
||||
{
|
||||
|
||||
} // namespace omath::projectile_prediction
|
||||
152
source/projectile_prediction/proj_pred_engine_avx2.cpp
Normal file
152
source/projectile_prediction/proj_pred_engine_avx2.cpp
Normal file
@@ -0,0 +1,152 @@
|
||||
//
|
||||
// Created by Vlad on 2/23/2025.
|
||||
//
|
||||
#include "omath/projectile_prediction/proj_pred_engine_avx2.hpp"
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
|
||||
#if defined(OMATH_USE_AVX2) && defined(__i386__) && defined(__x86_64__)
|
||||
#include <immintrin.h>
|
||||
#else
|
||||
#include <format>
|
||||
#endif
|
||||
|
||||
namespace omath::projectile_prediction
|
||||
{
|
||||
std::optional<Vector3<float>>
|
||||
ProjPredEngineAVX2::MaybeCalculateAimPoint([[maybe_unused]] const Projectile& projectile,
|
||||
[[maybe_unused]] const Target& target) const
|
||||
{
|
||||
#if defined(OMATH_USE_AVX2) && defined(__i386__) && defined(__x86_64__)
|
||||
const float bulletGravity = m_gravityConstant * projectile.m_gravityScale;
|
||||
const float v0 = projectile.m_launchSpeed;
|
||||
const float v0Sqr = v0 * v0;
|
||||
const Vector3 projOrigin = projectile.m_origin;
|
||||
|
||||
constexpr int SIMD_FACTOR = 8;
|
||||
float currentTime = m_simulationTimeStep;
|
||||
|
||||
for (; currentTime <= m_maximumSimulationTime; currentTime += m_simulationTimeStep * SIMD_FACTOR)
|
||||
{
|
||||
const __m256 times =
|
||||
_mm256_setr_ps(currentTime, currentTime + m_simulationTimeStep,
|
||||
currentTime + m_simulationTimeStep * 2, currentTime + m_simulationTimeStep * 3,
|
||||
currentTime + m_simulationTimeStep * 4, currentTime + m_simulationTimeStep * 5,
|
||||
currentTime + m_simulationTimeStep * 6, currentTime + m_simulationTimeStep * 7);
|
||||
|
||||
const __m256 targetX =
|
||||
_mm256_fmadd_ps(_mm256_set1_ps(target.m_velocity.x), times, _mm256_set1_ps(target.m_origin.x));
|
||||
const __m256 targetY =
|
||||
_mm256_fmadd_ps(_mm256_set1_ps(target.m_velocity.y), times, _mm256_set1_ps(target.m_origin.y));
|
||||
const __m256 timesSq = _mm256_mul_ps(times, times);
|
||||
const __m256 targetZ = _mm256_fmadd_ps(_mm256_set1_ps(target.m_velocity.z), times,
|
||||
_mm256_fnmadd_ps(_mm256_set1_ps(0.5f * m_gravityConstant), timesSq,
|
||||
_mm256_set1_ps(target.m_origin.z)));
|
||||
|
||||
const __m256 deltaX = _mm256_sub_ps(targetX, _mm256_set1_ps(projOrigin.x));
|
||||
const __m256 deltaY = _mm256_sub_ps(targetY, _mm256_set1_ps(projOrigin.y));
|
||||
const __m256 deltaZ = _mm256_sub_ps(targetZ, _mm256_set1_ps(projOrigin.z));
|
||||
|
||||
const __m256 dSqr = _mm256_add_ps(_mm256_mul_ps(deltaX, deltaX), _mm256_mul_ps(deltaY, deltaY));
|
||||
|
||||
const __m256 bgTimesSq = _mm256_mul_ps(_mm256_set1_ps(bulletGravity), timesSq);
|
||||
const __m256 term = _mm256_add_ps(deltaZ, _mm256_mul_ps(_mm256_set1_ps(0.5f), bgTimesSq));
|
||||
const __m256 termSq = _mm256_mul_ps(term, term);
|
||||
const __m256 numerator = _mm256_add_ps(dSqr, termSq);
|
||||
const __m256 denominator = _mm256_add_ps(timesSq, _mm256_set1_ps(1e-8f)); // Avoid division by zero
|
||||
const __m256 requiredV0Sqr = _mm256_div_ps(numerator, denominator);
|
||||
|
||||
const __m256 v0SqrVec = _mm256_set1_ps(v0Sqr + 1e-3f);
|
||||
const __m256 mask = _mm256_cmp_ps(requiredV0Sqr, v0SqrVec, _CMP_LE_OQ);
|
||||
|
||||
const unsigned validMask = _mm256_movemask_ps(mask);
|
||||
|
||||
if (!validMask)
|
||||
continue;
|
||||
|
||||
alignas(32) float validTimes[SIMD_FACTOR];
|
||||
_mm256_store_ps(validTimes, times);
|
||||
|
||||
for (int i = 0; i < SIMD_FACTOR; ++i)
|
||||
{
|
||||
if (!(validMask & (1 << i)))
|
||||
continue;
|
||||
|
||||
const float candidateTime = validTimes[i];
|
||||
|
||||
if (candidateTime > m_maximumSimulationTime)
|
||||
continue;
|
||||
|
||||
// Fine search around candidate time
|
||||
for (float fineTime = candidateTime - m_simulationTimeStep * 2;
|
||||
fineTime <= candidateTime + m_simulationTimeStep * 2; fineTime += m_simulationTimeStep)
|
||||
{
|
||||
if (fineTime < 0)
|
||||
continue;
|
||||
|
||||
const Vector3 targetPos = target.PredictPosition(fineTime, m_gravityConstant);
|
||||
const auto pitch = CalculatePitch(projOrigin, targetPos, bulletGravity, v0, fineTime);
|
||||
if (!pitch)
|
||||
continue;
|
||||
|
||||
const Vector3 delta = targetPos - projOrigin;
|
||||
const float d = std::sqrt(delta.x * delta.x + delta.y * delta.y);
|
||||
const float height = d * std::tan(angles::DegreesToRadians(*pitch));
|
||||
return Vector3(targetPos.x, targetPos.y, projOrigin.z + height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback scalar processing for remaining times
|
||||
for (; currentTime <= m_maximumSimulationTime; currentTime += m_simulationTimeStep)
|
||||
{
|
||||
const Vector3 targetPos = target.PredictPosition(currentTime, m_gravityConstant);
|
||||
const auto pitch = CalculatePitch(projOrigin, targetPos, bulletGravity, v0, currentTime);
|
||||
if (!pitch)
|
||||
continue;
|
||||
|
||||
const Vector3 delta = targetPos - projOrigin;
|
||||
const float d = std::sqrt(delta.x * delta.x + delta.y * delta.y);
|
||||
const float height = d * std::tan(angles::DegreesToRadians(*pitch));
|
||||
return Vector3(targetPos.x, targetPos.y, projOrigin.z + height);
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
ProjPredEngineAVX2::ProjPredEngineAVX2(const float gravityConstant, const float simulationTimeStep,
|
||||
const float maximumSimulationTime) :
|
||||
m_gravityConstant(gravityConstant), m_simulationTimeStep(simulationTimeStep),
|
||||
m_maximumSimulationTime(maximumSimulationTime)
|
||||
{
|
||||
}
|
||||
std::optional<float> ProjPredEngineAVX2::CalculatePitch(const Vector3<float>& projOrigin,
|
||||
const Vector3<float>& targetPos, const float bulletGravity,
|
||||
const float v0, const float time)
|
||||
{
|
||||
if (time <= 0.0f)
|
||||
return std::nullopt;
|
||||
|
||||
const Vector3 delta = targetPos - projOrigin;
|
||||
const float dSqr = delta.x * delta.x + delta.y * delta.y;
|
||||
const float h = delta.z;
|
||||
|
||||
const float term = h + 0.5f * bulletGravity * time * time;
|
||||
const float requiredV0Sqr = (dSqr + term * term) / (time * time);
|
||||
const float v0Sqr = v0 * v0;
|
||||
|
||||
if (requiredV0Sqr > v0Sqr + 1e-3f)
|
||||
return std::nullopt;
|
||||
|
||||
if (dSqr == 0.0f)
|
||||
return term >= 0.0f ? 90.0f : -90.0f;
|
||||
|
||||
|
||||
const float d = std::sqrt(dSqr);
|
||||
const float tanTheta = term / d;
|
||||
return angles::RadiansToDegrees(std::atan(tanTheta));
|
||||
#else
|
||||
throw std::runtime_error(
|
||||
std::format("{} AVX2 feature is not enabled!", std::source_location::current().function_name()));
|
||||
#endif
|
||||
}
|
||||
} // namespace omath::projectile_prediction
|
||||
@@ -1,25 +1,18 @@
|
||||
//
|
||||
// Created by Vlad on 6/9/2024.
|
||||
//
|
||||
|
||||
|
||||
#include "omath/prediction/Engine.hpp"
|
||||
#include "omath/projectile_prediction/proj_pred_engine_legacy.hpp"
|
||||
#include <cmath>
|
||||
#include <omath/Angles.hpp>
|
||||
#include <omath/angles.hpp>
|
||||
|
||||
|
||||
namespace omath::prediction
|
||||
namespace omath::projectile_prediction
|
||||
{
|
||||
Engine::Engine(const float gravityConstant, const float simulationTimeStep,
|
||||
const float maximumSimulationTime, const float distanceTolerance)
|
||||
: m_gravityConstant(gravityConstant),
|
||||
m_simulationTimeStep(simulationTimeStep),
|
||||
m_maximumSimulationTime(maximumSimulationTime),
|
||||
m_distanceTolerance(distanceTolerance)
|
||||
ProjPredEngineLegacy::ProjPredEngineLegacy(const float gravityConstant, const float simulationTimeStep,
|
||||
const float maximumSimulationTime, const float distanceTolerance) :
|
||||
m_gravityConstant(gravityConstant), m_simulationTimeStep(simulationTimeStep),
|
||||
m_maximumSimulationTime(maximumSimulationTime), m_distanceTolerance(distanceTolerance)
|
||||
{
|
||||
}
|
||||
|
||||
std::optional<Vector3> Engine::MaybeCalculateAimPoint(const Projectile &projectile, const Target &target) const
|
||||
std::optional<Vector3<float>> ProjPredEngineLegacy::MaybeCalculateAimPoint(const Projectile& projectile,
|
||||
const Target& target) const
|
||||
{
|
||||
for (float time = 0.f; time < m_maximumSimulationTime; time += m_simulationTimeStep)
|
||||
{
|
||||
@@ -28,7 +21,7 @@ namespace omath::prediction
|
||||
const auto projectilePitch = MaybeCalculateProjectileLaunchPitchAngle(projectile, predictedTargetPosition);
|
||||
|
||||
if (!projectilePitch.has_value()) [[unlikely]]
|
||||
continue;
|
||||
continue;
|
||||
|
||||
if (!IsProjectileReachedTarget(predictedTargetPosition, projectile, projectilePitch.value(), time))
|
||||
continue;
|
||||
@@ -41,8 +34,9 @@ namespace omath::prediction
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<float> Engine::MaybeCalculateProjectileLaunchPitchAngle(const Projectile &projectile,
|
||||
const Vector3 &targetPosition) const
|
||||
std::optional<float>
|
||||
ProjPredEngineLegacy::MaybeCalculateProjectileLaunchPitchAngle(const Projectile& projectile,
|
||||
const Vector3<float>& targetPosition) const
|
||||
{
|
||||
const auto bulletGravity = m_gravityConstant * projectile.m_gravityScale;
|
||||
const auto delta = targetPosition - projectile.m_origin;
|
||||
@@ -51,11 +45,11 @@ namespace omath::prediction
|
||||
const auto distance2dSqr = distance2d * distance2d;
|
||||
const auto launchSpeedSqr = projectile.m_launchSpeed * projectile.m_launchSpeed;
|
||||
|
||||
float root = launchSpeedSqr * launchSpeedSqr - bulletGravity * (bulletGravity *
|
||||
distance2dSqr + 2.0f * delta.z * launchSpeedSqr);
|
||||
float root = launchSpeedSqr * launchSpeedSqr -
|
||||
bulletGravity * (bulletGravity * distance2dSqr + 2.0f * delta.z * launchSpeedSqr);
|
||||
|
||||
if (root < 0.0f) [[unlikely]]
|
||||
return std::nullopt;
|
||||
return std::nullopt;
|
||||
|
||||
root = std::sqrt(root);
|
||||
const float angle = std::atan((launchSpeedSqr - root) / (bulletGravity * distance2d));
|
||||
@@ -63,12 +57,12 @@ namespace omath::prediction
|
||||
return angles::RadiansToDegrees(angle);
|
||||
}
|
||||
|
||||
bool Engine::IsProjectileReachedTarget(const Vector3 &targetPosition, const Projectile &projectile,
|
||||
const float pitch, const float time) const
|
||||
bool ProjPredEngineLegacy::IsProjectileReachedTarget(const Vector3<float>& targetPosition, const Projectile& projectile,
|
||||
const float pitch, const float time) const
|
||||
{
|
||||
const auto yaw = projectile.m_origin.ViewAngleTo(targetPosition).y;
|
||||
const auto projectilePosition = projectile.PredictPosition(pitch, yaw, time, m_gravityConstant);
|
||||
|
||||
return projectilePosition.DistTo(targetPosition) <= m_distanceTolerance;
|
||||
}
|
||||
}
|
||||
} // namespace omath::projectile_prediction
|
||||
21
source/projectile_prediction/projectile.cpp
Normal file
21
source/projectile_prediction/projectile.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by Vlad on 6/9/2024.
|
||||
//
|
||||
|
||||
#include "omath/projectile_prediction/projectile.hpp"
|
||||
|
||||
#include <omath/engines/source_engine/formulas.hpp>
|
||||
|
||||
namespace omath::projectile_prediction
|
||||
{
|
||||
Vector3<float> Projectile::PredictPosition(const float pitch, const float yaw, const float time, const float gravity) const
|
||||
{
|
||||
auto currentPos = m_origin + source_engine::ForwardVector({source_engine::PitchAngle::FromDegrees(-pitch),
|
||||
source_engine::YawAngle::FromDegrees(yaw),
|
||||
source_engine::RollAngle::FromDegrees(0)}) *
|
||||
m_launchSpeed * time;
|
||||
currentPos.z -= (gravity * m_gravityScale) * (time * time) * 0.5f;
|
||||
|
||||
return currentPos;
|
||||
}
|
||||
} // namespace omath::prediction
|
||||
@@ -2,7 +2,7 @@
|
||||
// Created by Vlad on 6/9/2024.
|
||||
//
|
||||
|
||||
#include "omath/prediction/Target.hpp"
|
||||
#include "omath/projectile_prediction/projectile.hpp"
|
||||
|
||||
|
||||
namespace omath::prediction
|
||||
@@ -1 +1 @@
|
||||
target_sources(omath PRIVATE Camera.cpp)
|
||||
target_sources(omath PRIVATE camera.cpp)
|
||||
@@ -1,7 +1,7 @@
|
||||
//
|
||||
// Created by Vlad on 27.08.2024.
|
||||
//
|
||||
#include "omath/projection/Camera.hpp"
|
||||
#include "omath/projection/camera.hpp"
|
||||
|
||||
|
||||
namespace omath::projection
|
||||
@@ -1,30 +1,41 @@
|
||||
enable_testing()
|
||||
|
||||
project(unit-tests)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
||||
|
||||
include(GoogleTest)
|
||||
add_executable(unit-tests
|
||||
general/UnitTestPrediction.cpp
|
||||
general/UnitTestMatrix.cpp
|
||||
general/UnitTestMat.cpp
|
||||
general/UnitTestAstar.cpp
|
||||
general/UnitTestProjection.cpp
|
||||
general/UnitTestVector3.cpp
|
||||
general/UnitTestVector2.cpp
|
||||
general/UnitTestColor.cpp
|
||||
general/UnitTestVector4.cpp
|
||||
general/UnitTestLineTrace.cpp
|
||||
general/UnitTestAngles.cpp
|
||||
general/UnitTestViewAngles.cpp
|
||||
general/UnitTestAngle.cpp
|
||||
general/unit_test_prediction.cpp
|
||||
general/unit_test_matrix.cpp
|
||||
general/unit_test_mat.cpp
|
||||
general/unit_test_a_star.cpp
|
||||
general/unit_test_projection.cpp
|
||||
general/unit_test_vector3.cpp
|
||||
general/unit_test_vector2.cpp
|
||||
general/unit_test_color.cpp
|
||||
general/unit_test_vector4.cpp
|
||||
general/unit_test_line_trace.cpp
|
||||
general/unit_test_angles.cpp
|
||||
general/unit_test_view_angles.cpp
|
||||
general/unit_test_angle.cpp
|
||||
general/unit_test_triangle.cpp
|
||||
general/unit_test_box_primitive.cpp
|
||||
|
||||
engines/UnitTestOpenGL.cpp
|
||||
engines/UnitTestUnityEngine.cpp
|
||||
engines/UnitTestSourceEngine.cpp
|
||||
engines/unit_test_open_gl.cpp
|
||||
engines/unit_test_unity_engine.cpp
|
||||
engines/unit_test_source_engine.cpp
|
||||
engines/unit_test_iw_engine.cpp
|
||||
|
||||
)
|
||||
|
||||
target_link_libraries(unit-tests PRIVATE gtest gtest_main omath)
|
||||
set_target_properties(unit-tests PROPERTIES
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
UNITY_BUILD ON
|
||||
UNITY_BUILD_BATCH_SIZE 20
|
||||
CXX_STANDARD 23
|
||||
CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
|
||||
target_link_libraries(unit-tests PRIVATE gtest gtest_main omath::omath)
|
||||
|
||||
gtest_discover_tests(unit-tests)
|
||||
@@ -1,37 +0,0 @@
|
||||
//
|
||||
// Created by Orange on 11/23/2024.
|
||||
//
|
||||
#include <complex>
|
||||
#include <gtest/gtest.h>
|
||||
#include <omath/Matrix.hpp>
|
||||
#include <print>
|
||||
|
||||
// #include <glm/glm.hpp>
|
||||
|
||||
// #include "glm/ext/matrix_clip_space.hpp"
|
||||
// #include "glm/ext/matrix_transform.hpp"
|
||||
|
||||
|
||||
TEST(UnitTestOpenGL, Projection)
|
||||
{
|
||||
|
||||
/*const auto proj_glm = glm::perspective(glm::radians(90.f), 16.f / 9.f, 0.1f, 1000.f);
|
||||
// const auto proj_glm2 = glm::perspectiveLH_NO(glm::radians(90.f), 16.f / 9.f, 0.1f, 1000.f);
|
||||
// const auto proj_omath = omath::Mat<4, 4, float, omath::MatStoreType::COLUMN_MAJOR>((const float*)&proj_glm);
|
||||
// EXPECT_EQ(omath::opengl::PerspectiveProjectionMatrix(90, 16.f / 9.f, 0.1f, 1000.f), proj_omath);
|
||||
|
||||
|
||||
glm::vec4 ndc_glm2 = proj_glm * glm::vec4(300.f, 0.f, -1000.f, 1.f);
|
||||
ndc_glm2 /= ndc_glm2.w;
|
||||
const omath::Mat<4, 1, float, omath::MatStoreType::COLUMN_MAJOR> cords_omath =
|
||||
{
|
||||
{0},
|
||||
{0},
|
||||
{-0.2f},
|
||||
{1}
|
||||
};
|
||||
|
||||
//auto ndc_omath = proj_omath * cords_omath;
|
||||
// ndc_omath /= ndc_omath.At(3, 0);
|
||||
*/
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
//
|
||||
// Created by Orange on 11/23/2024.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
#include <omath/engines/Source/Camera.hpp>
|
||||
#include <omath/engines/Source/Constants.h>
|
||||
#include <omath/engines/Source/Formulas.hpp>
|
||||
|
||||
|
||||
TEST(UnitTestSourceEngine, ForwardVector)
|
||||
{
|
||||
const auto forward = omath::source::ForwardVector({});
|
||||
|
||||
EXPECT_EQ(forward, omath::source::kAbsForward);
|
||||
}
|
||||
|
||||
TEST(UnitTestSourceEngine, RightVector)
|
||||
{
|
||||
const auto right = omath::source::RightVector({});
|
||||
|
||||
EXPECT_EQ(right, omath::source::kAbsRight);
|
||||
}
|
||||
|
||||
TEST(UnitTestSourceEngine, UpVector)
|
||||
{
|
||||
const auto up = omath::source::UpVector({});
|
||||
EXPECT_EQ(up, omath::source::kAbsUp);
|
||||
}
|
||||
|
||||
TEST(UnitTestSourceEngine, PerpectiveProjectionAtCenter)
|
||||
{
|
||||
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);
|
||||
auto cam = omath::source::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||
|
||||
|
||||
const auto viewProjMatrix = cam.GetViewProjectionMatrix();
|
||||
|
||||
for (float distance = 0.02f; distance < 1000.f; distance += 0.01f)
|
||||
{
|
||||
const auto projected = cam.WorldToScreen(viewProjMatrix, {distance, 0, 0});
|
||||
|
||||
EXPECT_TRUE(projected.has_value());
|
||||
|
||||
if (!projected.has_value())
|
||||
continue;
|
||||
|
||||
EXPECT_NEAR(projected->x, 960, 0.00001f);
|
||||
EXPECT_NEAR(projected->y, 540, 0.00001f);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user