diff --git a/CMakeLists.txt b/CMakeLists.txt index 7182f1d..36b1c8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,7 @@ endif() # glm include_directories(external/glm) +add_compile_definitions(GLM_FORCE_INTRINSICS) # fmt #add_subdirectory(external/fmt EXCLUDE_FROM_ALL) diff --git a/openVulkanoCpp/Base/UI/BaseWindow.hpp b/openVulkanoCpp/Base/UI/BaseWindow.hpp index 698417d..67948ec 100644 --- a/openVulkanoCpp/Base/UI/BaseWindow.hpp +++ b/openVulkanoCpp/Base/UI/BaseWindow.hpp @@ -25,68 +25,8 @@ namespace openVulkanoCpp return windowConfig; } - void GetSize(int* width, int* height) override = 0; - - void GetSize(uint32_t* width, uint32_t* height) override - { - int w, h; - GetSize(&w, &h); - *width = w; - *height = h; - } - - uint32_t GetWidth() override - { - uint32_t width, height; - GetSize(&width, &height); - return width; - } - - uint32_t GetHeight() override - { - uint32_t width, height; - GetSize(&width, &height); - return height; - } - - glm::ivec2 GetSize() override - { - glm::ivec2 size; - this->GetSize(&size.x, &size.y); - return size; - } - void SetSize(uint32_t width, uint32_t height) override = 0; - void SetSize(glm::ivec2 size) override - { - SetSize(size.x, size.y); - } - - void GetPosition(int* x, int* y) override = 0; - - int GetPositionX() override - { - int x, y; - GetPosition(&x, &y); - return x; - } - - int GetPositionY() override - { - int x, y; - GetPosition(&x, &y); - return y; - } - - - glm::ivec2 GetPosition() override - { - glm::ivec2 position; - GetPosition(&position.x, &position.y); - return position; - } - bool HasTitle() override { return WindowMode::WINDOWED == GetWindowMode(); @@ -104,8 +44,6 @@ namespace openVulkanoCpp void SetPosition(int posX, int posY) override = 0; - void SetPosition(glm::ivec2 pos) override { SetPosition(pos.x, pos.y); } - void Show() override = 0; void Hide() override = 0; diff --git a/openVulkanoCpp/Base/UI/IWindow.hpp b/openVulkanoCpp/Base/UI/IWindow.hpp index 5f4bd23..0f8d172 100644 --- a/openVulkanoCpp/Base/UI/IWindow.hpp +++ b/openVulkanoCpp/Base/UI/IWindow.hpp @@ -6,7 +6,7 @@ #pragma once -#include +#include "../../Math/Math.hpp" #include "../PlatformEnums.hpp" #include "../ICloseable.hpp" #include @@ -28,8 +28,8 @@ namespace openVulkanoCpp struct WindowConfiguration { - uint32_t width = 1280, height = 720; - int posX = 0, posY = 0; + Math::Vector2ui size{1280, 720}; + Math::Vector2i position{0, 0}; std::string title = "Window Title"; WindowMode windowMode = WINDOWED; }; @@ -47,25 +47,20 @@ namespace openVulkanoCpp virtual WindowMode GetWindowMode() = 0; virtual void SetWindowMode(WindowMode) = 0; - virtual void SetFullscreen() { SetWindowMode(FULLSCREEN); } - virtual void SetWindowed() { SetWindowMode(WINDOWED); } + inline void SetFullscreen() { SetWindowMode(FULLSCREEN); } + inline void SetWindowed() { SetWindowMode(WINDOWED); } virtual const WindowConfiguration& GetWindowConfiguration() = 0; - virtual uint32_t GetWidth() = 0; - virtual uint32_t GetHeight() = 0; - virtual void GetSize(int* width, int* height) = 0; - virtual void GetSize(uint32_t* width, uint32_t* height) = 0; - virtual glm::ivec2 GetSize() = 0; + inline uint32_t GetWidth() { return GetSize().x; } + inline uint32_t GetHeight() { return GetSize().y; } + virtual Math::Vector2ui GetSize() = 0; virtual void SetSize(uint32_t width, uint32_t height) = 0; - virtual void SetSize(glm::ivec2 size) { SetSize(size.x, size.y); } + inline void SetSize(Math::Vector2ui size) { SetSize(size.x, size.y); } virtual void SetSizeLimits(int minWidth, int minHeight, int maxWidth, int maxHeight) = 0; - virtual int GetPositionX() = 0; - virtual int GetPositionY() = 0; - virtual void GetPosition(int* x, int* y) = 0; - virtual glm::ivec2 GetPosition() = 0; + virtual Math::Vector2i GetPosition() = 0; virtual void SetPosition(int posX, int posY) = 0; - virtual void SetPosition(glm::ivec2 pos) = 0; + inline void SetPosition(Math::Vector2i pos) { SetPosition(pos.x, pos.y); }; virtual void Show() = 0; virtual void Hide() = 0; diff --git a/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp b/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp index 204a3ce..a9146ae 100644 --- a/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp +++ b/openVulkanoCpp/ExampleApps/CubesExampleApp.cpp @@ -9,8 +9,7 @@ #include "../Scene/Shader.hpp" #include "../Input/InputManager.hpp" #include "../Host/GraphicsAppManager.hpp" -#include -#include +#include "../Math/Math.hpp" #pragma clang diagnostic push #pragma ide diagnostic ignored "cert-msc50-cpp" @@ -18,6 +17,7 @@ using namespace openVulkanoCpp::Scene; using namespace openVulkanoCpp::Input; +using namespace openVulkanoCpp::Math; uint32_t GEOS = 3000, OBJECTS = 10000, DYNAMIC = 1000; @@ -46,13 +46,13 @@ public: scene.Init(); cam.Init(70, 16, 9, 0.1f, 100); scene.SetCamera(&cam); - cam.SetMatrix(glm::translate(glm::mat4(1), glm::vec3(0,0,-10))); + cam.SetMatrix(Utils::translate(Matrix4f(1), Vector3f_SIMD(0,0,-10))); shader.Init("Shader/basic", "Shader/basic"); drawablesPool.resize(GEOS); for(int i = 0; i < GEOS; i++) { Geometry* geo = new Geometry(); - geo->InitCube(std::rand() % 1000 / 1000.0f + 0.01f, std::rand() % 1000 / 1000.0f + 0.01f, std::rand() % 1000 / 1000.0f + 0.01f, glm::vec4((std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, 1)); + geo->InitCube(std::rand() % 1000 / 1000.0f + 0.01f, std::rand() % 1000 / 1000.0f + 0.01f, std::rand() % 1000 / 1000.0f + 0.01f, Vector4f((std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, (std::rand() % 255) / 255.0f, 1)); drawablesPool[i].Init(geo, &mat); } nodesPool.resize(OBJECTS); @@ -62,7 +62,7 @@ public: scene.GetRoot()->AddChild(&nodesPool[i]); if (i < DYNAMIC) nodesPool[i].SetUpdateFrequency(UpdateFrequency::Always); nodesPool[i].AddDrawable(&drawablesPool[std::rand() % GEOS]); - nodesPool[i].SetMatrix(glm::translate(glm::mat4x4(1), glm::vec3((std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5))); + nodesPool[i].SetMatrix(Utils::translate(glm::mat4x4(1), Vector3f((std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5, (std::rand() % 10000) / 1000.0f - 5))); } scene.shader = &shader; @@ -88,7 +88,7 @@ public: } float yaw = 0, pitch = 0; - glm::vec3 position = {0,0,-10}; + Vector3f_SIMD position = {0,0,-10}; void Tick() override { @@ -98,10 +98,10 @@ public: } auto input = InputManager::GetInstace(); - glm::vec3 vec(input->GetAxis(actionSide), 0, -input->GetAxis(actionForward)); - if(glm::length2(vec) > 1) + Vector3f_SIMD vec(input->GetAxis(actionSide), 0, -input->GetAxis(actionForward)); + if(Utils::length2(vec) > 1.0f) { - vec = glm::normalize(vec); + vec = Utils::normalize(vec); } float tiemScale = 0.004f; @@ -111,12 +111,12 @@ public: yaw += input->GetAxis(actionLookSide) * tiemScale * 0.5f; pitch += input->GetAxis(actionLookUp) * tiemScale * 0.5f; - glm::quat rot(glm::vec3(pitch, yaw, 0)); - glm::mat4 rotMat = glm::toMat4(rot); + QuaternionF rot(Vector3f(pitch, yaw, 0)); + Matrix4f rotMat = Utils::toMat4(rot); - vec = glm::vec3(rot * glm::vec4(vec, 1)); + vec = Vector3f_SIMD(rotMat * Vector4f(vec, 1)); position += vec; - cam.SetMatrix(glm::translate(glm::mat4(1), position) * rotMat); + cam.SetMatrix(Utils::translate(Matrix4f(1), position) * rotMat); } void Close() override{} diff --git a/openVulkanoCpp/Host/GLFW/WindowGLFW.cpp b/openVulkanoCpp/Host/GLFW/WindowGLFW.cpp index 7059435..a4efc63 100644 --- a/openVulkanoCpp/Host/GLFW/WindowGLFW.cpp +++ b/openVulkanoCpp/Host/GLFW/WindowGLFW.cpp @@ -30,8 +30,8 @@ namespace openVulkanoCpp::GLFW { int posX, posY, sizeX, sizeY; glfwGetMonitorWorkarea(monitors[i], &posX, &posY, &sizeX, &sizeY); - if (windowConfig.posX >= posX && windowConfig.posX < posX + sizeX && - windowConfig.posY >= posY && windowConfig.posY < posY + sizeY) + if (windowConfig.position.x >= posX && windowConfig.position.x < posX + sizeX && + windowConfig.position.y >= posY && windowConfig.position.y < posY + sizeY) { return monitors[i]; } @@ -53,7 +53,7 @@ namespace openVulkanoCpp::GLFW { glfwWindowHint(GLFW_DECORATED, (~windowConfig.windowMode) & 1); //TODO handle full screen resolutions - window = glfwCreateWindow(windowConfig.width, windowConfig.height, windowConfig.title.c_str(), GetTargetMonitor(), nullptr); + window = glfwCreateWindow(windowConfig.size.x, windowConfig.size.y, windowConfig.title.c_str(), GetTargetMonitor(), nullptr); if (!window) return; glfwSetWindowUserPointer(window, this); RegisterCallbacks(); @@ -142,8 +142,8 @@ namespace openVulkanoCpp::GLFW void WindowGLFW::SetSize(uint32_t width, uint32_t height) { - windowConfig.width = width; - windowConfig.height = height; + windowConfig.size.x = width; + windowConfig.size.y = height; if (window) { glfwSetWindowSize(window, width, height); @@ -152,14 +152,28 @@ namespace openVulkanoCpp::GLFW void WindowGLFW::SetPosition(int posX, int posY) { - windowConfig.posX = posX; - windowConfig.posY = posY; + windowConfig.position.x = posX; + windowConfig.position.y = posY; if (window) { glfwSetWindowPos(window, posX, posY); } } + Math::Vector2ui WindowGLFW::GetSize() + { + Math::Vector2i size; + glfwGetWindowSize(window, &size.x, &size.y); + return size; + } + + Math::Vector2i WindowGLFW::GetPosition() + { + Math::Vector2i pos; + glfwGetWindowPos(window, &pos.x, &pos.y); + return pos; + } + void WindowGLFW::SetSizeLimits(int minWidth, int minHeight, int maxWidth, int maxHeight) { minWidth = (minWidth < 0) ? GLFW_DONT_CARE : minWidth; @@ -185,10 +199,10 @@ namespace openVulkanoCpp::GLFW glfwWindowHint(GLFW_DECORATED, (~windowMode) & 1); if (windowMode == WINDOWED || windowMode == BORDERLESS) { - sizeX = windowConfig.width; - sizeY = windowConfig.height; - posX = windowConfig.posX; - posY = windowConfig.posY; + sizeX = windowConfig.size.x; + sizeY = windowConfig.size.y; + posX = windowConfig.position.x; + posY = windowConfig.position.y; monitor = nullptr; if (windowMode == WINDOWED) { @@ -201,7 +215,7 @@ namespace openVulkanoCpp::GLFW } else { // Fullscreen - GetPosition(&windowConfig.posX, &windowConfig.posY); // Backup current window position + windowConfig.position = GetPosition(); // Backup current window position monitor = GetCurrentMonitor(); if (!monitor) monitor = GetPrimaryMonitor(); if (!monitor) return; // We don't have a monitor to set the fullscreen window to @@ -229,16 +243,6 @@ namespace openVulkanoCpp::GLFW this->handler = handler; } - void WindowGLFW::GetSize(int* width, int* height) - { - glfwGetWindowSize(window, width, height); - } - - void WindowGLFW::GetPosition(int* x, int* y) - { - glfwGetWindowPos(window, x, y); - } - vk::SurfaceKHR WindowGLFW::CreateSurface(const vk::Instance& instance, const vk::AllocationCallbacks* pAllocator) { VkSurfaceKHR rawSurface; @@ -286,8 +290,8 @@ namespace openVulkanoCpp::GLFW Logger::WINDOW->debug("Window (id: {0}) moved (x: {1}, y: {2})", GetWindowId(), posX, posY); if (windowConfig.windowMode == WINDOWED || windowConfig.windowMode == BORDERLESS) { // Don't save window position for fullscreen - windowConfig.posX = posX; - windowConfig.posY = posY; + windowConfig.position.x = posX; + windowConfig.position.y = posY; } handler->OnWindowMove(this, posX, posY); } diff --git a/openVulkanoCpp/Host/GLFW/WindowGLFW.hpp b/openVulkanoCpp/Host/GLFW/WindowGLFW.hpp index ae7488d..0a4c924 100644 --- a/openVulkanoCpp/Host/GLFW/WindowGLFW.hpp +++ b/openVulkanoCpp/Host/GLFW/WindowGLFW.hpp @@ -72,9 +72,9 @@ namespace openVulkanoCpp::GLFW IOpenGlWindow* GetOpenGlWindow() override { return this; }; // Status getter - void GetSize(int* width, int* height) override; + Math::Vector2ui GetSize() override; - void GetPosition(int* x, int* y) override; + Math::Vector2i GetPosition() override; IWindowHandler* GetWindowHandler() override { return handler; } diff --git a/openVulkanoCpp/Input/InputDeviceMouse.hpp b/openVulkanoCpp/Input/InputDeviceMouse.hpp index a44fd34..8515fdd 100644 --- a/openVulkanoCpp/Input/InputDeviceMouse.hpp +++ b/openVulkanoCpp/Input/InputDeviceMouse.hpp @@ -7,7 +7,7 @@ #pragma once #include "InputDevice.hpp" -#include "glm/vec2.hpp" +#include "../Math/Math.hpp" namespace openVulkanoCpp { @@ -100,7 +100,7 @@ namespace openVulkanoCpp return GetAxis(InputKey::Mouse::Axis::AXIS_WHEEL_Y); } - [[nodiscard]] glm::vec2 GetMousePosition() const + [[nodiscard]] Math::Vector2d GetMousePosition() const { return { mousePosX, mousePosY }; } @@ -111,7 +111,7 @@ namespace openVulkanoCpp posY = mousePosY; } - glm::vec2 GetMousePosition(const IWindow* window) const + [[nodiscard]] Math::Vector2d GetMousePosition(const IWindow* window) const { if (window == lastWindow) { diff --git a/openVulkanoCpp/Math/Math.hpp b/openVulkanoCpp/Math/Math.hpp new file mode 100644 index 0000000..dba88d0 --- /dev/null +++ b/openVulkanoCpp/Math/Math.hpp @@ -0,0 +1,138 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#define GLM_FORECE_DEPTH_ZERO_TO_ONE // TODO handle this better +#include +#include +#include +#include + +namespace openVulkanoCpp::Math +{ + namespace Utils + { + using namespace glm; + } + + template using Matrix2_SIMD = glm::tmat2x2; + template using Matrix3_SIMD = glm::tmat3x3; + template using Matrix4_SIMD = glm::tmat4x4; + template using Matrix2 = glm::tmat2x2; + template using Matrix3 = glm::tmat3x3; + template using Matrix4 = Matrix4_SIMD; + //template using Matrix4 = glm::tmat4x4; + + typedef Matrix2 Matrix2f; + typedef Matrix2 Matrix2d; + typedef Matrix2 Matrix2i; + typedef Matrix2 Matrix2l; + + typedef Matrix3 Matrix3f; + typedef Matrix3 Matrix3d; + typedef Matrix3 Matrix3i; + typedef Matrix3 Matrix3l; + + typedef Matrix4 Matrix4f; + typedef Matrix4 Matrix4d; + typedef Matrix4 Matrix4i; + typedef Matrix4 Matrix4l; + + typedef Matrix2_SIMD Matrix2f_SIMD; + typedef Matrix2_SIMD Matrix2d_SIMD; + typedef Matrix2_SIMD Matrix2i_SIMD; + typedef Matrix2_SIMD Matrix2l_SIMD; + + typedef Matrix3_SIMD Matrix3f_SIMD; + typedef Matrix3_SIMD Matrix3d_SIMD; + typedef Matrix3_SIMD Matrix3i_SIMD; + typedef Matrix3_SIMD Matrix3l_SIMD; + + typedef Matrix4_SIMD Matrix4f_SIMD; + typedef Matrix4_SIMD Matrix4d_SIMD; + typedef Matrix4_SIMD Matrix4i_SIMD; + typedef Matrix4_SIMD Matrix4l_SIMD; + + template using Vector2_SIMD = glm::tvec2; + template using Vector3_SIMD = glm::tvec3; + template using Vector4_SIMD = glm::tvec4; + template using Vector2 = glm::tvec2; + template using Vector3 = glm::tvec3; + template using Vector4 = Vector4_SIMD; + //template using Vector4 = glm::tvec4; + + typedef Vector2 Vector2f; + typedef Vector2 Vector2d; + typedef Vector2 Vector2c; + typedef Vector2 Vector2s; + typedef Vector2 Vector2i; + typedef Vector2 Vector2l; + typedef Vector2 Vector2uc; + typedef Vector2 Vector2us; + typedef Vector2 Vector2ui; + typedef Vector2 Vector2ul; + + typedef Vector3 Vector3f; + typedef Vector3 Vector3d; + typedef Vector3 Vector3c; + typedef Vector3 Vector3s; + typedef Vector3 Vector3i; + typedef Vector3 Vector3l; + typedef Vector3 Vector3uc; + typedef Vector3 Vector3us; + typedef Vector3 Vector3ui; + typedef Vector3 Vector3ul; + + typedef Vector4 Vector4f; + typedef Vector4 Vector4d; + typedef Vector4 Vector4c; + typedef Vector4 Vector4s; + typedef Vector4 Vector4i; + typedef Vector4 Vector4l; + typedef Vector4 Vector4uc; + typedef Vector4 Vector4us; + typedef Vector4 Vector4ui; + typedef Vector4 Vector4ul; + + typedef Vector2_SIMD Vector2f_SIMD; + typedef Vector2_SIMD Vector2d_SIMD; + typedef Vector2_SIMD Vector2c_SIMD; + typedef Vector2_SIMD Vector2s_SIMD; + typedef Vector2_SIMD Vector2i_SIMD; + typedef Vector2_SIMD Vector2l_SIMD; + typedef Vector2_SIMD Vector2uc_SIMD; + typedef Vector2_SIMD Vector2us_SIMD; + typedef Vector2_SIMD Vector2ui_SIMD; + typedef Vector2_SIMD Vector2ul_SIMD; + + typedef Vector3_SIMD Vector3f_SIMD; + typedef Vector3_SIMD Vector3d_SIMD; + typedef Vector3_SIMD Vector3c_SIMD; + typedef Vector3_SIMD Vector3s_SIMD; + typedef Vector3_SIMD Vector3i_SIMD; + typedef Vector3_SIMD Vector3l_SIMD; + typedef Vector3_SIMD Vector3uc_SIMD; + typedef Vector3_SIMD Vector3us_SIMD; + typedef Vector3_SIMD Vector3ui_SIMD; + typedef Vector3_SIMD Vector3ul_SIMD; + + typedef Vector4_SIMD Vector4f_SIMD; + typedef Vector4_SIMD Vector4d_SIMD; + typedef Vector4_SIMD Vector4c_SIMD; + typedef Vector4_SIMD Vector4s_SIMD; + typedef Vector4_SIMD Vector4i_SIMD; + typedef Vector4_SIMD Vector4l_SIMD; + typedef Vector4_SIMD Vector4uc_SIMD; + typedef Vector4_SIMD Vector4us_SIMD; + typedef Vector4_SIMD Vector4ui_SIMD; + typedef Vector4_SIMD Vector4ul_SIMD; + + template using Quaternion = glm::tquat; + typedef Quaternion QuaternionF; + typedef Quaternion QuaternionD; + typedef Quaternion QuaternionI; +} diff --git a/openVulkanoCpp/Scene/AABB.hpp b/openVulkanoCpp/Scene/AABB.hpp index d6a8135..18f8c2f 100644 --- a/openVulkanoCpp/Scene/AABB.hpp +++ b/openVulkanoCpp/Scene/AABB.hpp @@ -1,5 +1,12 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + #pragma once -#include + +#include "../Math/Math.hpp" #include "../Base/IInitable.hpp" namespace openVulkanoCpp @@ -11,7 +18,7 @@ namespace openVulkanoCpp */ class AABB final : public virtual IInitable { - glm::vec3 min, max; + Math::Vector3f min, max; public: AABB() : min(INFINITY), max(-INFINITY) {} @@ -22,15 +29,15 @@ namespace openVulkanoCpp */ void Init() override { - min = glm::vec3(INFINITY); - max = glm::vec3(-INFINITY); + min = Math::Vector3f(INFINITY); + max = Math::Vector3f(-INFINITY); } /** * \brief Initiates the AABB to a single point (min=max=point) * \param point The point that should be used as min and max of the AABB */ - void Init(const glm::vec3& point) + void Init(const Math::Vector3f& point) { min = max = point; } @@ -45,33 +52,33 @@ namespace openVulkanoCpp max = other.GetMax(); } - const glm::vec3& GetMin() const { return min; } + [[nodiscard]] inline const Math::Vector3f& GetMin() const { return min; } - const glm::vec3& GetMax() const { return max; } + [[nodiscard]] inline const Math::Vector3f& GetMax() const { return max; } - void Grow(const glm::vec3& point) + void Grow(const Math::Vector3f& point) { - min = glm::min(min, point); - max = glm::max(max, point); + min = Math::Utils::min(min, point); + max = Math::Utils::max(max, point); } void Grow(const AABB& otherAABB) { - min = glm::min(min, otherAABB.GetMin()); - max = glm::max(max, otherAABB.GetMax()); + min = Math::Utils::min(min, otherAABB.GetMin()); + max = Math::Utils::max(max, otherAABB.GetMax()); } - void Grow(const AABB& otherAABB, glm::mat4x4 transformation) + void Grow(const AABB& otherAABB, Math::Matrix4f transformation) { //TODO } - glm::vec3 GetDiagonal() const + [[nodiscard]] Math::Vector3f GetDiagonal() const { return max - min; } - glm::vec3 GetCenter() const + [[nodiscard]] Math::Vector3f GetCenter() const { return min + (GetDiagonal() * 0.5f); } @@ -81,7 +88,7 @@ namespace openVulkanoCpp * \param other The other AABB that should be checked * \return true if the AABB overlaps with the other, false if not */ - bool IsOverlapping(const AABB& other) const + [[nodiscard]] bool IsOverlapping(const AABB& other) const { return !(other.min.x > max.x || other.max.x < min.x || other.min.y > max.y || other.max.y < min.y || other.min.z > max.z || other.max.z < min.z); } diff --git a/openVulkanoCpp/Scene/Camera.hpp b/openVulkanoCpp/Scene/Camera.hpp index 63db48c..a0a17b0 100644 --- a/openVulkanoCpp/Scene/Camera.hpp +++ b/openVulkanoCpp/Scene/Camera.hpp @@ -6,11 +6,8 @@ #pragma once -#define _USE_MATH_DEFINES -#include -#include #include "Node.hpp" -#define GLM_FORECE_DEPTH_ZERO_TO_ONE +#include "../Math/Math.hpp" namespace openVulkanoCpp { @@ -21,7 +18,7 @@ namespace openVulkanoCpp protected: float width, height, nearPlane, farPlane; - glm::mat4x4 projection, view, viewProjection; + Math::Matrix4f projection, view, viewProjection; Camera() : width(0), height(0), nearPlane(0), farPlane(0), projection(1), view(1), viewProjection(1) {} @@ -76,17 +73,17 @@ namespace openVulkanoCpp void UpdateViewProjectionMatrix() { // In vulkan the screen space is defined as y=0=top and y=1=bottom and thus the coordinate have to be flipped - viewProjection = projection * glm::mat4x4(1,0,0,0,0,-1,0,0,0,0,1,0,0,0,0,1) * view; + viewProjection = projection * Math::Matrix4f(1,0,0,0,0,-1,0,0,0,0,1,0,0,0,0,1) * view; } - void UpdateWorldMatrix(const glm::mat4x4& parentWorldMat) override + void UpdateWorldMatrix(const Math::Matrix4f& parentWorldMat) override { Node::UpdateWorldMatrix(parentWorldMat); - view = glm::inverse(GetWorldMatrix()); + view = Math::Utils::inverse(GetWorldMatrix()); UpdateViewProjectionMatrix(); } - [[nodiscard]] const glm::mat4x4& GetViewProjectionMatrix() const + [[nodiscard]] const Math::Matrix4f& GetViewProjectionMatrix() const { return viewProjection; } @@ -109,7 +106,7 @@ namespace openVulkanoCpp void Init(float fovDegrees, float width, float height, float nearPlane, float farPlane) { - this->fov = glm::radians(fovDegrees); + this->fov = Math::Utils::radians(fovDegrees); aspect = width / height; Camera::Init(width, height, nearPlane, farPlane); } @@ -138,7 +135,7 @@ namespace openVulkanoCpp void SetFov(const float fov) { - SetFovRad(glm::radians(fov)); + SetFovRad(Math::Utils::radians(fov)); } void SetFovRad(const float fov) @@ -148,7 +145,7 @@ namespace openVulkanoCpp [[nodiscard]] float GetFov() const { - return glm::degrees(fov); + return Math::Utils::degrees(fov); } [[nodiscard]] float GetFovX() const @@ -168,7 +165,7 @@ namespace openVulkanoCpp void UpdateProjectionMatrix() override { - projection = glm::perspectiveLH_ZO(fov, aspect, nearPlane, farPlane); + projection = Math::Utils::perspectiveLH_ZO(fov, aspect, nearPlane, farPlane); UpdateViewProjectionMatrix(); } }; @@ -179,7 +176,7 @@ namespace openVulkanoCpp void UpdateProjectionMatrix() override { const float widthHalf = width * 0.5f, heightHalf = height * 0.5f; - projection = glm::orthoLH_ZO(-widthHalf, widthHalf, -heightHalf, heightHalf, nearPlane, farPlane); + projection = Math::Utils::orthoLH_ZO(-widthHalf, widthHalf, -heightHalf, heightHalf, nearPlane, farPlane); UpdateViewProjectionMatrix(); } }; diff --git a/openVulkanoCpp/Scene/Geometry.hpp b/openVulkanoCpp/Scene/Geometry.hpp index 968f108..70eecce 100644 --- a/openVulkanoCpp/Scene/Geometry.hpp +++ b/openVulkanoCpp/Scene/Geometry.hpp @@ -119,7 +119,7 @@ namespace openVulkanoCpp //TODO load materials } - void InitCube(float x = 1, float y = 1, float z = 1, glm::vec4 color = glm::vec4(1)) + void InitCube(float x = 1, float y = 1, float z = 1, Math::Vector4f color = Math::Vector4f(1)) { Init(24, 36); SetIndices(new uint32_t[indexCount]{ @@ -196,7 +196,7 @@ namespace openVulkanoCpp void Free() { - if(vertices) delete[] vertices; + delete[] vertices; free(indices); vertices = nullptr; indices = nullptr; diff --git a/openVulkanoCpp/Scene/Node.cpp b/openVulkanoCpp/Scene/Node.cpp index 83c3b17..cce71be 100644 --- a/openVulkanoCpp/Scene/Node.cpp +++ b/openVulkanoCpp/Scene/Node.cpp @@ -25,7 +25,7 @@ namespace openVulkanoCpp::Scene void Node::Init() { if (parent || scene || !children.empty() || !drawables.empty()) throw std::runtime_error("Node already initialized"); - localMat = worldMat = glm::mat4x4(1); + localMat = worldMat = Math::Matrix4f(1); enabled = true; } @@ -79,13 +79,13 @@ namespace openVulkanoCpp::Scene Utils::Remove(drawables, drawable); } - void Node::SetMatrix(glm::mat4x4 mat) + void Node::SetMatrix(const Math::Matrix4f& mat) { localMat = mat; - UpdateWorldMatrix(parent ? parent->GetWorldMatrix() : glm::mat4x4(1)); + UpdateWorldMatrix(parent ? parent->GetWorldMatrix() : Math::Matrix4f(1)); } - void Node::UpdateWorldMatrix(const glm::mat4x4& parentWorldMat) + void Node::UpdateWorldMatrix(const Math::Matrix4f& parentWorldMat) { worldMat = parentWorldMat * localMat; for (const auto& node : children) diff --git a/openVulkanoCpp/Scene/Node.hpp b/openVulkanoCpp/Scene/Node.hpp index 7515d9e..3279921 100644 --- a/openVulkanoCpp/Scene/Node.hpp +++ b/openVulkanoCpp/Scene/Node.hpp @@ -7,8 +7,8 @@ #pragma once #include -#include #include "../Base/ICloseable.hpp" +#include "../Math/Math.hpp" #include "Drawable.hpp" namespace openVulkanoCpp @@ -22,12 +22,12 @@ namespace openVulkanoCpp Always, Sometimes, Never }; - struct Node : virtual IInitable, virtual ICloseable + class Node : public virtual IInitable, public virtual ICloseable { friend Scene; public: - glm::mat4x4 localMat, worldMat; + Math::Matrix4f localMat, worldMat; bool enabled = true; Node* parent = nullptr; Scene* scene = nullptr; @@ -57,11 +57,11 @@ namespace openVulkanoCpp void RemoveDrawable(Drawable* drawable); - void SetMatrix(glm::mat4x4 mat); + void SetMatrix(const Math::Matrix4f& mat); - [[nodiscard]] const glm::mat4x4& GetMatrix() const { return localMat; } + [[nodiscard]] const Math::Matrix4f& GetMatrix() const { return localMat; } - [[nodiscard]] const glm::mat4x4& GetWorldMatrix() const { return worldMat; } + [[nodiscard]] const Math::Matrix4f& GetWorldMatrix() const { return worldMat; } [[nodiscard]] bool IsEnabled() const { return enabled; } @@ -84,7 +84,7 @@ namespace openVulkanoCpp } protected: - virtual void UpdateWorldMatrix(const glm::mat4x4& parentWorldMat); + virtual void UpdateWorldMatrix(const Math::Matrix4f& parentWorldMat); private: void SetParent(Node* parent); diff --git a/openVulkanoCpp/Scene/Vertex.hpp b/openVulkanoCpp/Scene/Vertex.hpp index e22fd90..2ee4480 100644 --- a/openVulkanoCpp/Scene/Vertex.hpp +++ b/openVulkanoCpp/Scene/Vertex.hpp @@ -1,5 +1,12 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + #pragma once -#include + +#include "../Math/Math.hpp" #include #include #include @@ -8,8 +15,8 @@ namespace openVulkanoCpp { struct Vertex { - glm::vec3 position, normal, tangent, biTangent, textureCoordinates; - glm::vec4 color; + Math::Vector3f position, normal, tangent, biTangent, textureCoordinates; + Math::Vector4f color; Vertex() = default; @@ -20,11 +27,11 @@ namespace openVulkanoCpp : position({ x, y, z }), normal({ nx, ny, nz }), tangent(), biTangent(), textureCoordinates({ u, v, 0 }), color() {} - Vertex(const glm::vec3& position, const glm::vec3& normal, const glm::vec3& tangent, const glm::vec3& biTangent, const glm::vec2& textureCoordinates) + Vertex(const Math::Vector3f& position, const Math::Vector3f& normal, const Math::Vector3f& tangent, const Math::Vector3f& biTangent, const Math::Vector2f& textureCoordinates) : position(position), normal(normal), tangent(tangent), biTangent(biTangent), textureCoordinates(textureCoordinates, 0), color() {} - Vertex(const glm::vec3 & position, const glm::vec3 & normal, const glm::vec3 & tangent, const glm::vec3 & biTangent, const glm::vec3 & textureCoordinates) + Vertex(const Math::Vector3f & position, const Math::Vector3f & normal, const Math::Vector3f & tangent, const Math::Vector3f & biTangent, const Math::Vector3f & textureCoordinates) : position(position), normal(normal), tangent(tangent), biTangent(biTangent), textureCoordinates(textureCoordinates), color() {} @@ -35,7 +42,7 @@ namespace openVulkanoCpp position = { x, y, z }; } - void Set(const glm::vec3& position) + void Set(const Math::Vector3f& position) { this->position = position; } @@ -52,14 +59,14 @@ namespace openVulkanoCpp this->textureCoordinates = { u, v, 0 }; } - void Set(const glm::vec3& position, const glm::vec3& normal, const glm::vec2& textureCoordinates) + void Set(const Math::Vector3f& position, const Math::Vector3f& normal, const Math::Vector2f& textureCoordinates) { this->position = position; this->normal = normal; this->textureCoordinates = { textureCoordinates, 0 }; } - void Set(const glm::vec3& position, const glm::vec3& normal, const glm::vec3& tangent, const glm::vec3& biTangent, const glm::vec2& textureCoordinates) + void Set(const Math::Vector3f& position, const Math::Vector3f& normal, const Math::Vector3f& tangent, const Math::Vector3f& biTangent, const Math::Vector2f& textureCoordinates) { this->position = position; this->normal = normal; @@ -73,7 +80,7 @@ namespace openVulkanoCpp this->normal = { nx, ny, nz }; } - void SetNormal(const glm::vec3& normal) + void SetNormal(const Math::Vector3f& normal) { this->normal = normal; } @@ -88,7 +95,7 @@ namespace openVulkanoCpp this->tangent = { tx, ty, tz }; } - void SetTangent(const glm::vec3& tangent) + void SetTangent(const Math::Vector3f& tangent) { this->tangent = tangent; } @@ -104,7 +111,7 @@ namespace openVulkanoCpp this->tangent = { tx, ty, tz }; } - void SetTangentAndBiTangent(const glm::vec3& tangent, const glm::vec3& biTangent) + void SetTangentAndBiTangent(const Math::Vector3f& tangent, const Math::Vector3f& biTangent) { this->biTangent = biTangent; this->tangent = tangent; @@ -121,7 +128,7 @@ namespace openVulkanoCpp this->biTangent = { bx, by, bz }; } - void SetBiTangent(const glm::vec3& biTangent) + void SetBiTangent(const Math::Vector3f& biTangent) { this->biTangent = biTangent; } @@ -141,12 +148,12 @@ namespace openVulkanoCpp this->textureCoordinates = { u, v, w }; } - void SetTextureCoordinates(const glm::vec2& textureCoordinates) + void SetTextureCoordinates(const Math::Vector2f& textureCoordinates) { this->textureCoordinates = { textureCoordinates, 0 }; } - void SetTextureCoordinates(const glm::vec3& textureCoordinates) + void SetTextureCoordinates(const Math::Vector3f& textureCoordinates) { this->textureCoordinates = textureCoordinates; } @@ -166,7 +173,7 @@ namespace openVulkanoCpp color = { r,g,b,a }; } - void SetColor(const glm::vec4& color) + void SetColor(const Math::Vector4f& color) { this->color = color; } diff --git a/openVulkanoCpp/Vulkan/Resources/ResourceManager.cpp b/openVulkanoCpp/Vulkan/Resources/ResourceManager.cpp index 9d4c61f..939fa9c 100644 --- a/openVulkanoCpp/Vulkan/Resources/ResourceManager.cpp +++ b/openVulkanoCpp/Vulkan/Resources/ResourceManager.cpp @@ -112,7 +112,7 @@ namespace openVulkanoCpp::Vulkan UniformBuffer* uBuffer = new UniformBuffer(); ManagedBuffer* buffer; VulkanNode* vkNode; - const vk::DeviceSize allocSize = Utils::Align(sizeof(glm::mat4x4), uniformBufferAlignment); + const vk::DeviceSize allocSize = Utils::Align(sizeof(Math::Matrix4f), uniformBufferAlignment); if (node->GetUpdateFrequency() != Scene::UpdateFrequency::Never) { vkNode = new VulkanNodeDynamic(); @@ -123,7 +123,7 @@ namespace openVulkanoCpp::Vulkan else { vkNode = new VulkanNode(); - buffer = CreateDeviceOnlyBufferWithData(sizeof(glm::mat4), vk::BufferUsageFlagBits::eUniformBuffer, &node->worldMat); + buffer = CreateDeviceOnlyBufferWithData(sizeof(Math::Matrix4f), vk::BufferUsageFlagBits::eUniformBuffer, &node->worldMat); } uBuffer->Init(buffer, allocSize, &context->pipeline.descriptorSetLayout, context->pipeline.pipelineLayout); vkNode->Init(node, uBuffer); diff --git a/openVulkanoCpp/Vulkan/Scene/VulkanNode.hpp b/openVulkanoCpp/Vulkan/Scene/VulkanNode.hpp index 92200bb..9840265 100644 --- a/openVulkanoCpp/Vulkan/Scene/VulkanNode.hpp +++ b/openVulkanoCpp/Vulkan/Scene/VulkanNode.hpp @@ -42,7 +42,7 @@ namespace openVulkanoCpp if(bufferId != lastUpdate) { lastUpdate = bufferId; - buffer->Update(&node->worldMat, sizeof(glm::mat4x4), bufferId); + buffer->Update(&node->worldMat, sizeof(Math::Matrix4f), bufferId); } buffer->Record(cmdBuffer, bufferId); }