diff --git a/cmake/Findglm.cmake b/cmake/Findglm.cmake deleted file mode 100644 index 7b78c8da..00000000 --- a/cmake/Findglm.cmake +++ /dev/null @@ -1,9 +0,0 @@ -set(GLM_DIR "${PROJECT_SOURCE_DIR}/third_party/GLM/") - -find_library( - glm - NAMES glm glm_shared - HINTS ${GLM_DIR}/build/glm -) - -set(glm_INCLUDE_DIR "${GLM_DIR}/include") \ No newline at end of file diff --git a/dependencies.dep b/dependencies.dep index 2b406c56..5cbc91d4 100644 --- a/dependencies.dep +++ b/dependencies.dep @@ -1,8 +1,3 @@ -[GLM] -git_url = https://github.com/g-truc/glm.git -tag = 0.9.9.8 -custom_commands = cd GLM && mkdir build && cd build && cmake .. -D BUILD_SHARED_LIBS=ON && make && cd .. && mkdir include && mv glm include - [GLFW] git_url = https://github.com/glfw/glfw.git tag = 3.3.8 diff --git a/graphics/CMakeLists.txt b/graphics/CMakeLists.txt index bb5740d8..3ebd1b29 100644 --- a/graphics/CMakeLists.txt +++ b/graphics/CMakeLists.txt @@ -1,7 +1,6 @@ find_package(Vulkan REQUIRED) find_package(vma REQUIRED) find_package(glfw3 REQUIRED) -find_package(glm REQUIRED) find_package(Vulkan COMPONENTS glslc) find_program(glslc_executable NAMES glslc HINTS ${Vulkan_GLSLC_EXECUTABLE}/..) @@ -43,24 +42,23 @@ ${CMAKE_SOURCE_DIR} ${Vulkan_INCLUDE_DIR} ${vma_INCLUDE_DIR} ${glfw3_INCLUDE_DIR} - ${glm_INCLUDE_DIR} ) message("Vulkan_INCLUDE_DIR: ${Vulkan_INCLUDE_DIR}") target_sources(rse_graphics PRIVATE - src/window.cpp - src/vulkan_base.cpp - src/vulkan_pipeline.cpp - src/vulkan_buffers.cpp - src/vulkan_descriptors.cpp - src/rse_graphics.cpp - src/mesh_controller.cpp - src/vulkan_commons.cpp +src/window.cpp +src/vulkan_base.cpp +src/vulkan_pipeline.cpp +src/vulkan_buffers.cpp +src/vulkan_descriptors.cpp +src/rse_graphics.cpp +src/rse_math.c +src/mesh_controller.cpp +src/vulkan_commons.cpp ) target_link_libraries(rse_graphics PRIVATE rse_utilities ${Vulkan_LIBRARIES} ${glfw} - ${glm} ) diff --git a/graphics/rse_graphics.hpp b/graphics/rse_graphics.hpp index e5889df2..0f74dc20 100644 --- a/graphics/rse_graphics.hpp +++ b/graphics/rse_graphics.hpp @@ -2,11 +2,10 @@ #define RSE_GRAPHICS_HPP #include + +struct VulkanState; namespace rse::graphics { -namespace vulkanbase { - struct VulkanState; -}; class RseGraphics { public: RseGraphics(); @@ -24,7 +23,7 @@ class RseGraphics { private: /* Hold vulkan state. It is modified by function initializing and communicating with Vulkan API */ - vulkanbase::VulkanState* vulkan_state; + VulkanState* vulkan_state; }; diff --git a/graphics/src/locale_vulkan.hpp b/graphics/src/locale_vulkan.h similarity index 93% rename from graphics/src/locale_vulkan.hpp rename to graphics/src/locale_vulkan.h index a45e6ea6..d337ad0a 100644 --- a/graphics/src/locale_vulkan.hpp +++ b/graphics/src/locale_vulkan.h @@ -9,15 +9,10 @@ * */ -#pragma once +#ifndef LOCALE_VULKAN_H +#define LOCALE_VULKAN_H -#include -#include - -#include "utilities/localization.hpp" - -namespace rse::locale::vulkan -{ +#include "utilities/localization.h" #define vulkanErrorMesssages vulkanErrorMesssages_nolocale[SELECTED_LANGUAGE] @@ -53,7 +48,7 @@ enum Errors LAST_MESSAGE }; -inline std::array, rse::utils::locale::LAST_LANGUAGE> vulkanErrorMesssages_nolocale = { +const char vulkanErrorMesssages_nolocale[LAST_LANGUAGE][LAST_MESSAGE][MAX_MESSAGE_LENGHT] = { {/* EN_US */ /* VULKAN_LAYER_NOT_SUPPORTED */ "Selected layer is not supported: ", @@ -112,4 +107,4 @@ inline std::array, rse::utils::locale::LAS } }; -} // namespace rse::locale::vulkan \ No newline at end of file +#endif /* LOCALE_VULKAN_H */ \ No newline at end of file diff --git a/graphics/src/locale_window.hpp b/graphics/src/locale_window.h similarity index 67% rename from graphics/src/locale_window.hpp rename to graphics/src/locale_window.h index 5f3c0dff..42d919f8 100644 --- a/graphics/src/locale_window.hpp +++ b/graphics/src/locale_window.h @@ -13,11 +13,7 @@ #include -#include "utilities/localization.hpp" - - -namespace rse::locale::window -{ +#include "utilities/localization.h" #define windowErrorMesssages windowErrorMesssages_nolocale[SELECTED_LANGUAGE] @@ -29,15 +25,13 @@ enum Errors LAST_MESSAGE }; -std::array, LAST_MESSAGE> windowErrorMesssages_nolocale = { +const char windowErrorMesssages_nolocale[LAST_LANGUAGE][LAST_MESSAGE][MAX_MESSAGE_LENGHT] = { {/* EN_US */ /* WINDOW_GLFW_INIT_FAILED */ - "GLFW init failed!" + "GLFW init failed!", /* WINDOW_VULKAN_NOT_SUPPORTED */ - "Unable to load Vulkan" + "Unable to load Vulkan", /*WINDOW_NOT_CREATED*/ "Window context creation failed" } -}; - -} // namespace rse::locale::window \ No newline at end of file +}; \ No newline at end of file diff --git a/graphics/src/mesh_controller.cpp b/graphics/src/mesh_controller.cpp index 5492f3ce..deae2e49 100644 --- a/graphics/src/mesh_controller.cpp +++ b/graphics/src/mesh_controller.cpp @@ -1,20 +1,16 @@ -#include "mesh_controller.hpp" - -#include -#include -#include +#include "mesh_controller.h" #include "vulkan_buffers.hpp" -namespace rse::graphics::mesh -{ +#include +#include std::map g_meshes; std::queue free_ids; std::map> instance_free_ids; -uint16_t create_mesh(std::vector vertices, +uint16_t create_mesh(std::vector vertices, std::vector indices) { static uint16_t last_id = 0U; @@ -72,12 +68,10 @@ std::vector get_instance_data_for_mesh(uint16_t mesh_id) { } -std::vector get_mesh_vertices(uint16_t mesh_id) { +std::vector get_mesh_vertices(uint16_t mesh_id) { return g_meshes[mesh_id].vertices; } std::vector get_mesh_indices(uint16_t mesh_id) { return g_meshes[mesh_id].indices; -} - -} // namespace rse::graphics::mesh +} \ No newline at end of file diff --git a/graphics/src/mesh_controller.hpp b/graphics/src/mesh_controller.h similarity index 73% rename from graphics/src/mesh_controller.hpp rename to graphics/src/mesh_controller.h index 60a4bf21..07d0d102 100644 --- a/graphics/src/mesh_controller.hpp +++ b/graphics/src/mesh_controller.h @@ -1,5 +1,5 @@ /** - * @file mesh_controller.hpp + * @file mesh_controller.h * @author Piotr Krygier (piotr.krygier@meshsystems.com) * @brief * @version 0.1 @@ -9,19 +9,15 @@ * */ -#ifndef RSE_MESH_CONTROLLER_HPP -#define RSE_MESH_CONTROLLER_HPP +#ifndef RSE_MESH_CONTROLLER_H +#define RSE_MESH_CONTROLLER_H +#include "vulkan_commons.h" + +#include +#include #include #include -#include -#include - -#include "vulkan_commons.hpp" - -namespace rse::graphics::mesh -{ - /* Forward declarations */ struct MeshInstance; @@ -34,7 +30,7 @@ struct MeshInstance struct Mesh { uint32_t unique_id; - const std::vector vertices; + const std::vector vertices; std::vector indices; std::map> instances; }; @@ -47,7 +43,7 @@ struct Mesh * @param indices indices for mesh * @return uint16_t Mesh identifier. Can be useful for getting vertices, indices and instances */ -uint16_t create_mesh(std::vector vertices, +uint16_t create_mesh(std::vector vertices, std::vector indices); /** @@ -59,13 +55,9 @@ uint16_t create_mesh(std::vector vertices, void create_mesh_instance(uint16_t mesh_id, InstanceData instance_data); -std::vector get_mesh_vertices(uint16_t mesh_id); +std::vector get_mesh_vertices(uint16_t mesh_id); std::vector get_mesh_indices(uint16_t mesh_id); std::vector get_instance_data_for_mesh(uint16_t mesh_id); -} // namespace rse::graphics::mesh - - - -#endif \ No newline at end of file +#endif /* RSE_MESH_CONTROLLER_H */ \ No newline at end of file diff --git a/graphics/src/rse_graphics.cpp b/graphics/src/rse_graphics.cpp index 66b8cd1c..f166893b 100644 --- a/graphics/src/rse_graphics.cpp +++ b/graphics/src/rse_graphics.cpp @@ -2,14 +2,16 @@ #include "window.hpp" #include "vulkan_base.hpp" -#include "mesh_controller.hpp" -#include "vulkan_commons.hpp" +#include "mesh_controller.h" +#include "vulkan_commons.h" + +#include "rse_math.h" namespace rse::graphics { RseGraphics::RseGraphics() { - vulkan_state = new vulkanbase::VulkanState; + vulkan_state = new VulkanState; vulkan_state->graphicsQueue = VK_NULL_HANDLE; vulkan_state->physicalDevice = VK_NULL_HANDLE; vulkan_state->currentFrame = 0U; @@ -44,11 +46,11 @@ uint8_t RseGraphics::graphics_run(void) return -1; // FIXME: Temporary array of vertices, for testing purposes - const std::vector vertices = {{{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 1.0f}}, + const std::vector vertices = {{{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 1.0f}}, {{0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 1.0f}}, {{0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}}, {{-0.5f, 0.5f, 0.0f}, {1.0f, 0.0f, 1.0f}}}; - const std::vector vertices2 = {{{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 1.0f}}, + const std::vector vertices2 = {{{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 1.0f}}, {{0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}}, {{0.5f, 0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}}, @@ -57,23 +59,23 @@ uint8_t RseGraphics::graphics_run(void) // FIXME: Temporary array of vertices, for testing purposes std::vector indices = {0, 1, 2, 2, 3, 0}; - uint16_t mesh_id = rse::graphics::mesh::create_mesh(vertices, indices); - rse::graphics::mesh::create_mesh_instance(mesh_id, rse::graphics::mesh::InstanceData( - glm::vec3(0.0f, 0.0f, 0.0f), - glm::vec3(0.0f, 0.0f, glm::radians(45.0f)), + uint16_t mesh_id = create_mesh(vertices, indices); + create_mesh_instance(mesh_id, InstanceData( + {0.0f, 0.0f, 0.0f}, + {0.0f, 0.0f, rse_math_deg_to_radians(45.0f)}, 1.0f )); - rse::graphics::mesh::create_mesh_instance(mesh_id, rse::graphics::mesh::InstanceData( - glm::vec3(-1.0f, 0.0f, 0.0f), - glm::vec3(0.0f, 0.0f, 0.0f), + create_mesh_instance(mesh_id, InstanceData( + {-1.0f, 0.0f, 0.0f}, + {0.0f, 0.0f, 0.0f}, 1.0f )); - mesh_id = rse::graphics::mesh::create_mesh(vertices, indices); - rse::graphics::mesh::create_mesh_instance(mesh_id, rse::graphics::mesh::InstanceData( - glm::vec3(0.0f, 1.0f, 0.0f), - glm::vec3(0.0f, 0.0f, glm::radians(0.0f)), + mesh_id = create_mesh(vertices, indices); + create_mesh_instance(mesh_id, InstanceData( + {0.0f, 1.0f, 0.0f}, + {0.0f, 0.0f, rse_math_deg_to_radians(0.0f)}, 1.0f )); diff --git a/graphics/src/rse_math.c b/graphics/src/rse_math.c new file mode 100644 index 00000000..a70735c1 --- /dev/null +++ b/graphics/src/rse_math.c @@ -0,0 +1,126 @@ +#include "rse_math.h" + +#include + +#define PI 3.14159265359f + +static struct vec3_t substract_vec3(const struct vec3_t* vec1, const struct vec3_t* vec2) +{ + struct vec3_t result; + + result.x = vec1->x - vec2->x; + result.y = vec1->y - vec2->y; + result.z = vec1->z - vec2->z; + + return result; +} + +static float length_vec3(const struct vec3_t* vec) +{ + return sqrt((vec->x * vec->x) + +(vec->y * vec->y) + +(vec->z * vec->z)); +} + +static float scalar_cross_vec3(const struct vec3_t* vec1, const struct vec3_t* vec2) +{ + return (vec1->x * vec2->x) + +(vec1->y * vec2->y) + +(vec1->z * vec2->z); +} + +float rse_math_deg_to_radians(float degrees) +{ + return degrees * (PI/180.0f); +} + +struct vec3_t rse_math_cross_vec3(const struct vec3_t* vec1, const struct vec3_t* vec2) +{ + struct vec3_t result; + + result.x = (vec1->y * vec2->z) - (vec1->z * vec2->y); + result.y = (vec1->z * vec2->x) - (vec1->x * vec2->z); + result.z = (vec1->x * vec2->y) - (vec1->y * vec2->x); + return result; +} + +struct vec3_t rse_math_normalize_vec3(const struct vec3_t* vec) +{ + struct vec3_t result = {0}; + float vec_length = 0.0f; + + vec_length = length_vec3(vec); + + result.x = vec->x / vec_length; + result.y = vec->y / vec_length; + result.z = vec->z / vec_length; + + return result; +} + +struct mat4_t rse_math_uniform_mat4() +{ + struct mat4_t result= {0.0f}; + result.x_1 = 1.0f; + result.y_2 = 1.0f; + result.z_3 = 1.0f; + result.w_4 = 1.0f; + + return result; +} + +struct mat4_t rse_math_perspective(float fovy, float aspect_ratio, float z_near, float z_far) +{ + float tan_half_fovy = 0; + struct mat4_t result = {0}; + + tan_half_fovy = tan(fovy / 2.0f); + + result.x_1 = -1.0f / (aspect_ratio * tan_half_fovy); + result.y_2 = 1.0f / (tan_half_fovy); + result.z_3 = - (z_far + z_near) / (z_far - z_near); + result.w_3 = -1.0f; + result.z_4 = - (2.0f * z_far * z_near) / (z_far - z_near); + + return result; +} + +struct mat4_t rse_math_look_at(const struct vec3_t* eye, const struct vec3_t* center, const struct vec3_t* up) +{ + struct mat4_t result = {0.0f}; + struct vec3_t forward_vec = {0}; + struct vec3_t right_vec = {0}; + struct vec3_t up_vec = {0}; + + struct vec3_t tmp = substract_vec3(eye, center); + + forward_vec = rse_math_normalize_vec3(&tmp); + + tmp = rse_math_cross_vec3(up, &forward_vec); + right_vec = rse_math_normalize_vec3(&tmp); + + up_vec = rse_math_cross_vec3(&forward_vec, &right_vec); + + result.x_1 = right_vec.x; + result.x_2 = right_vec.y; + result.x_3 = right_vec.z; + + result.y_1 = up_vec.x; + result.y_2 = up_vec.y; + result.y_3 = up_vec.z; + + result.z_1 = forward_vec.x; + result.z_2 = forward_vec.y; + result.z_3 = forward_vec.z; + + result.x_4 = -scalar_cross_vec3(&right_vec, eye); + result.y_4 = -scalar_cross_vec3(&up_vec, eye); + result.z_4 = -scalar_cross_vec3(&forward_vec, eye); + + result.w_1 = 0; + result.w_2 = 0; + result.w_3 = 0; + result.w_4 = 1; + + return result; +} \ No newline at end of file diff --git a/graphics/src/rse_math.h b/graphics/src/rse_math.h new file mode 100644 index 00000000..89f3f15a --- /dev/null +++ b/graphics/src/rse_math.h @@ -0,0 +1,120 @@ +/** + * @file rse_math.h + * @author Piotr Krygier (piotr.krygier@) + * @brief RedScarfEngine math operations + * @version 0.1 + * @date 2023-08-07 + * + * @copyright Copyright (c) 2023 + * + */ + +#ifndef RSE_MATH_H +#define RSE_MATH_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Simple representation of vector + * + */ +struct vec3_t { + float x; + float y; + float z; +}; + +/* 4x4 matrix. + * [x_1 | x_2 | x_3 | x_4] + * [y_1 | y_2 | y_3 | y_4] + * [z_1 | x_2 | z_3 | z_4] + * [z_1 | w_2 | w_3 | w_4] + * + * Vulkan and OpenGL expects matrix to be passed by columns, this is why members are created in this order. + * */ +struct mat4_t { + /* First column */ + float x_1; + float y_1; + float z_1; + float w_1; + + /* Second column */ + float x_2; + float y_2; + float z_2; + float w_2; + + /* Third column */ + float x_3; + float y_3; + float z_3; + float w_3; + + /* Fourth column */ + float x_4; + float y_4; + float z_4; + float w_4; +}; + +/** + * @brief Calculate radians from provided degree + * + * @param degrees degrees + * @return float radians + */ +float rse_math_deg_to_radians(float degrees); + +/** + * @brief Create an uniform matrix, the one with ones on diagonal + * + * @return struct mat4_t Diagonal matrix + */ +struct mat4_t rse_math_uniform_mat4(); + +/** + * @brief Normalizes provided vector. Changes the values inside the vector to range 0.0...1.0 + * + * @param vec Vector to normalize + * @return struct vec3_t Normalized vector + */ +struct vec3_t rse_math_normalize_vec3(const struct vec3_t* vec); + +/** + * @brief Creates vector cross product, a vector, using right hand rule. Note, that order of vectors is important + * + * @param vec1 Input vector + * @param vec2 Input vector + * @return struct vec3_t Resulting cross product vector + */ +struct vec3_t rse_math_cross_vec3(const struct vec3_t* vec1, const struct vec3_t* vec2); + +/** + * @brief Calculates transformation matrix for viewing object from and eye cooridinates + * + * @param eye Location of the camera or the eye + * @param center Where are we looking at + * @param up Where "up" is located. Usually (0, 1, 0), since Y is up in most cases + * @return struct mat4_t Resulting lootAtMatrix + */ +struct mat4_t rse_math_look_at(const struct vec3_t* eye, const struct vec3_t* center, const struct vec3_t* up); + +/** + * @brief Creates perspective matrix, used in projection + * + * @param fovy_rad Field of view, in radians + * @param aspect_ratio Aspect ratio (usally screen width / height) + * @param z_near How near objects we can see + * @param z_far How far objects we can see + * @return struct mat4_t Resulting perspective matrix + */ +struct mat4_t rse_math_perspective(float fovy_rad, float aspect_ratio, float z_near, float z_far); + +#ifdef __cplusplus +} +#endif + +#endif /* RSE_MATH_H */ \ No newline at end of file diff --git a/graphics/src/vulkan_base.cpp b/graphics/src/vulkan_base.cpp index 90b4a940..36356994 100644 --- a/graphics/src/vulkan_base.cpp +++ b/graphics/src/vulkan_base.cpp @@ -11,7 +11,7 @@ #include "vulkan_base.hpp" -#include "vulkan_errors.hpp" +#include "vulkan_errors.h" #include #pragma GCC diagnostic push @@ -27,11 +27,11 @@ #include "vk_mem_alloc.h" #pragma GCC diagnostic pop -#include "locale_vulkan.hpp" +#include "locale_vulkan.h" #include "utilities/logger.hpp" #include "window.hpp" -#include "vulkan_commons.hpp" +#include "vulkan_commons.h" #include "vulkan_buffers.hpp" #include "vulkan_descriptors.hpp" #include "vulkan_pipeline.hpp" @@ -39,8 +39,6 @@ namespace rse::graphics::vulkanbase { -using namespace locale::vulkan; - #define APPLICATION_NAME "RedScarfEngine PoC" #define ENGINE_NAME "RedScarf Engine" @@ -394,17 +392,21 @@ uint8_t createDevice() { size_t numberOfQueues = 0; for (size_t familyIdx = 0; familyIdx < queueFamiliesPropertyCount; ++familyIdx) { + LOGI << "Checking queue: " << familyIdx; if (pQueueFamilyProperties[familyIdx].queueFlags & VK_QUEUE_GRAPHICS_BIT) { if (pQueueFamilyProperties[familyIdx].queueCount > numberOfQueues) { graphicsFamilyIdx = familyIdx; + LOGI << "Setting graphics family idx to " << familyIdx; numberOfQueues = pQueueFamilyProperties[familyIdx].queueCount; } } + /* Check for family with surface support */ VkBool32 presentSupport = false; vkGetPhysicalDeviceSurfaceSupportKHR(g_vulkan_state.physicalDevice, familyIdx, g_vulkan_state.surface, &presentSupport); if (presentSupport) { presentationFamiliyIdx = familyIdx; + LOGI << "Setting present family idx to " << familyIdx; } if (graphicsFamilyIdx > 0 && presentationFamiliyIdx > 0) { diff --git a/graphics/src/vulkan_base.hpp b/graphics/src/vulkan_base.hpp index da3e867e..997b4671 100644 --- a/graphics/src/vulkan_base.hpp +++ b/graphics/src/vulkan_base.hpp @@ -17,7 +17,7 @@ #include -#include "vulkan_commons.hpp" +#include "vulkan_commons.h" namespace rse::graphics::vulkanbase { diff --git a/graphics/src/vulkan_buffers.cpp b/graphics/src/vulkan_buffers.cpp index 66b38b46..94d8155d 100644 --- a/graphics/src/vulkan_buffers.cpp +++ b/graphics/src/vulkan_buffers.cpp @@ -3,21 +3,17 @@ #include -#define GLM_FORCE_RADIANS -#include - -#include "vulkan_errors.hpp" -#include "locale_vulkan.hpp" +#include "vulkan_errors.h" +#include "locale_vulkan.h" #include "utilities/logger.hpp" -#include "mesh_controller.hpp" +#include "mesh_controller.h" +#include "rse_math.h" namespace rse::graphics::vulkanbase { #define MAX_VERTEX_BUFFER_SIZE 33554432 /* 32 MB*/ -using namespace locale::vulkan; - // // FIXME: Temporary array of vertices, for testing purposes // const std::vector vertices = {{{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 1.0f}}, // {{0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 1.0f}}, @@ -320,19 +316,32 @@ uint8_t allocateCommandBuffers() */ void updateUniformBuffer() { + struct vec3_t eye = {0}; + struct vec3_t center = {0}; + struct vec3_t up = {0}; + float fovy; + float z_near; + float z_far; UniformBufferObject ubo{}; - // ubo.model = glm::translate(glm::mat4(1.0f), glm::vec3(1.0f, 0.0f, 0.0f)) * glm::rotate(glm::mat4(1.0f), glm::radians(45.0f), glm::vec3(0.0f, 0.0f, 1.0f)) * glm::scale(glm::mat4(1.0f), glm::vec3(2, 1, 1)); - ubo.model = glm::mat4(1.0f); - ubo.view = glm::lookAt(glm::vec3(0.0f, 0.0f, 5.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); - ubo.proj = glm::perspective(glm::radians(45.0f), - g_vulkan_state.swapchainExtent.width / (float)g_vulkan_state.swapchainExtent.height, - 0.1f, - 20.0f); - ubo.proj[1][1] *= -1; /* GLM was developed for OpenGL, where Y coordinate of the clip is inverted */ + + eye.z = 5.0f; + + up.y = 1.0f; + + fovy = rse_math_deg_to_radians(45.0f); + z_near = 0.1f; + z_far = 20.0f; + + ubo.model = rse_math_uniform_mat4(); + ubo.view = rse_math_look_at(&eye, ¢er, &up); + ubo.proj = rse_math_perspective(fovy, + g_vulkan_state.swapchainExtent.width / (float)g_vulkan_state.swapchainExtent.height, + z_near, + z_far); memcpy(g_vulkan_state.uniformBuffers[g_vulkan_state.currentFrame].allocationInfo.pMappedData, &ubo, sizeof(ubo)); } -uint8_t update_mesh_buffers(const std::vector& vertices, +uint8_t update_mesh_buffers(const std::vector& vertices, const std::vector& indices) { // void* mapped_data; @@ -375,7 +384,7 @@ uint8_t update_mesh_buffers(const std::vector return VULKAN_ERROR_NO_ERROR; } -uint8_t update_mesh_instances(rse::graphics::mesh::InstanceData& instance_data) +uint8_t update_mesh_instances(InstanceData& instance_data) { Buffer staging_buffer; @@ -460,8 +469,8 @@ uint8_t recordCommandBuffer(uint32_t imageIndex) VkBuffer vertexBuffers[] = {gVertexBuffers[i].buffer}; VkBuffer indexBuffer = {gIndexBuffers[i].buffer}; VkBuffer instanceBuffers[] = {gInstanceBuffers[i].buffer}; - auto indices = rse::graphics::mesh::get_mesh_indices(0); - auto instances = rse::graphics::mesh::get_instance_data_for_mesh(0); + auto indices = get_mesh_indices(0); + auto instances = get_instance_data_for_mesh(0); /* TODO: Figure out the offset */ vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets); diff --git a/graphics/src/vulkan_buffers.hpp b/graphics/src/vulkan_buffers.hpp index 2a65fc14..27732190 100644 --- a/graphics/src/vulkan_buffers.hpp +++ b/graphics/src/vulkan_buffers.hpp @@ -7,7 +7,7 @@ #include "vk_mem_alloc.h" -#include "vulkan_commons.hpp" +#include "vulkan_commons.h" namespace rse::graphics::vulkanbase { @@ -27,10 +27,10 @@ uint8_t create_buffers(); */ void updateUniformBuffer(); -uint8_t update_mesh_buffers(const std::vector& vertices, +uint8_t update_mesh_buffers(const std::vector& vertices, const std::vector& indices); -uint8_t update_mesh_instances(rse::graphics::mesh::InstanceData& instance_data); +uint8_t update_mesh_instances(InstanceData& instance_data); /** * @brief Record commands for given image index * diff --git a/graphics/src/vulkan_commons.cpp b/graphics/src/vulkan_commons.cpp index bfa38333..7a55ba98 100644 --- a/graphics/src/vulkan_commons.cpp +++ b/graphics/src/vulkan_commons.cpp @@ -1,7 +1,3 @@ -#include "vulkan_commons.hpp" +#include "vulkan_commons.h" -namespace rse::graphics::vulkanbase { - -VulkanState g_vulkan_state; - -} \ No newline at end of file +VulkanState g_vulkan_state; \ No newline at end of file diff --git a/graphics/src/vulkan_commons.hpp b/graphics/src/vulkan_commons.h similarity index 84% rename from graphics/src/vulkan_commons.hpp rename to graphics/src/vulkan_commons.h index a8fac754..a22af076 100644 --- a/graphics/src/vulkan_commons.hpp +++ b/graphics/src/vulkan_commons.h @@ -1,5 +1,5 @@ /** - * @file vulkan_global.hpp + * @file vulkan_global.h * @author Piotr Krygier (piotr.krygier@meshsystems.com) * @brief Declaration of a structure holding Vulkan variables needed by different parts of the code * @version 0.1 @@ -13,25 +13,20 @@ #define RSE_VULKAN_GLOBAL_HPP #include -#pragma GCC diagnostic ignored "-Wvolatile" -#include -#pragma GCC diagnostic pop +#include -#include "vk_mem_alloc.h" +#include "rse_math.h" #define SWAP_BUFFER_COUNT 2U -namespace rse::graphics::vulkanbase -{ - /** * @brief Represents single vertex on the screen * */ struct Vertex { - glm::vec3 pos; - glm::vec3 color; + struct vec3_t pos; + struct vec3_t color; static VkVertexInputBindingDescription getBindingDescription() { VkVertexInputBindingDescription bindingDescription; @@ -75,9 +70,9 @@ struct Buffer struct UniformBufferObject { - alignas(16) glm::mat4 model; - alignas(16) glm::mat4 view; - alignas(16) glm::mat4 proj; + alignas(16) mat4_t model; + alignas(16) mat4_t view; + alignas(16) mat4_t proj; }; struct VulkanState @@ -105,16 +100,10 @@ struct VulkanState extern VulkanState g_vulkan_state; -} // namespace rse::graphics::vulkanbase - -namespace rse::graphics::mesh -{ struct InstanceData { - glm::vec3 pos; - glm::vec3 rot; + struct vec3_t pos; + struct vec3_t rot; float scale; }; -} // namespace rse::graphics::mesh - #endif diff --git a/graphics/src/vulkan_descriptors.cpp b/graphics/src/vulkan_descriptors.cpp index b946891e..aa9ef63c 100644 --- a/graphics/src/vulkan_descriptors.cpp +++ b/graphics/src/vulkan_descriptors.cpp @@ -1,14 +1,12 @@ #include "vulkan_descriptors.hpp" -#include "locale_vulkan.hpp" +#include "locale_vulkan.h" #include "utilities/logger.hpp" -#include "vulkan_errors.hpp" +#include "vulkan_errors.h" namespace rse::graphics::vulkanbase { -using namespace locale::vulkan; - VkDescriptorPool gDescriptorPool = {}; static uint8_t createDescriptorPool(); diff --git a/graphics/src/vulkan_descriptors.hpp b/graphics/src/vulkan_descriptors.hpp index 43a8dec7..2ee89a13 100644 --- a/graphics/src/vulkan_descriptors.hpp +++ b/graphics/src/vulkan_descriptors.hpp @@ -5,7 +5,7 @@ #include #include -#include "vulkan_commons.hpp" +#include "vulkan_commons.h" namespace rse::graphics::vulkanbase { diff --git a/graphics/src/vulkan_errors.hpp b/graphics/src/vulkan_errors.h similarity index 96% rename from graphics/src/vulkan_errors.hpp rename to graphics/src/vulkan_errors.h index d0f98099..76470c43 100644 --- a/graphics/src/vulkan_errors.hpp +++ b/graphics/src/vulkan_errors.h @@ -1,5 +1,5 @@ -#ifndef VULKAN_ERRORS_HPP -#define VULKAN_ERRORS_HPP +#ifndef VULKAN_ERRORS_H +#define VULKAN_ERRORS_H /* Macro for checking status of function execution in initVulkan. Created to avoid writing boilerplate code */ #define STATUS_CHECK(FUNC) \ diff --git a/graphics/src/vulkan_pipeline.cpp b/graphics/src/vulkan_pipeline.cpp index 891d89c8..be56e2a3 100644 --- a/graphics/src/vulkan_pipeline.cpp +++ b/graphics/src/vulkan_pipeline.cpp @@ -1,10 +1,10 @@ #include "vulkan_pipeline.hpp" -#include "locale_vulkan.hpp" +#include "locale_vulkan.h" #include "utilities/file_utils.hpp" #include "utilities/logger.hpp" -#include "vulkan_errors.hpp" -#include "mesh_controller.hpp" +#include "vulkan_errors.h" +#include "mesh_controller.h" #include #include @@ -14,8 +14,6 @@ namespace rse::graphics::vulkanbase { -using namespace locale::vulkan; - enum class ShaderStage { VERTEX, @@ -505,7 +503,7 @@ void fillVertexInputInfo(VkPipelineVertexInputStateCreateInfo* vertexInputInfo) bindingDescriptions[0] = vertexBindingDescription; bindingDescriptions[1].binding = 1; - bindingDescriptions[1].stride = sizeof(rse::graphics::mesh::InstanceData); //TODO: change + bindingDescriptions[1].stride = sizeof(InstanceData); //TODO: change bindingDescriptions[1].inputRate = VK_VERTEX_INPUT_RATE_INSTANCE; auto attributeDescriptions = new VkVertexInputAttributeDescription[5]; @@ -514,17 +512,17 @@ void fillVertexInputInfo(VkPipelineVertexInputStateCreateInfo* vertexInputInfo) attributeDescriptions[2].binding = 1; attributeDescriptions[2].location = 2; attributeDescriptions[2].format = VK_FORMAT_R32G32B32_SFLOAT; - attributeDescriptions[2].offset = offsetof(rse::graphics::mesh::InstanceData, pos); + attributeDescriptions[2].offset = offsetof(InstanceData, pos); attributeDescriptions[3].binding = 1; attributeDescriptions[3].location = 3; attributeDescriptions[3].format = VK_FORMAT_R32G32B32_SFLOAT; - attributeDescriptions[3].offset = offsetof(rse::graphics::mesh::InstanceData, rot); + attributeDescriptions[3].offset = offsetof(InstanceData, rot); attributeDescriptions[4].binding = 1; attributeDescriptions[4].location = 4; attributeDescriptions[4].format = VK_FORMAT_R32_SFLOAT; - attributeDescriptions[4].offset = offsetof(rse::graphics::mesh::InstanceData, scale); + attributeDescriptions[4].offset = offsetof(InstanceData, scale); // ************************ vertexInputInfo->sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; diff --git a/graphics/src/vulkan_pipeline.hpp b/graphics/src/vulkan_pipeline.hpp index 2f102145..762c97e2 100644 --- a/graphics/src/vulkan_pipeline.hpp +++ b/graphics/src/vulkan_pipeline.hpp @@ -4,7 +4,7 @@ #include #include -#include "vulkan_commons.hpp" +#include "vulkan_commons.h" namespace rse::graphics::vulkanbase { diff --git a/graphics/src/window.cpp b/graphics/src/window.cpp index 32512050..dc9aa040 100644 --- a/graphics/src/window.cpp +++ b/graphics/src/window.cpp @@ -11,15 +11,13 @@ #include "window.hpp" -#include "locale_window.hpp" +#include "locale_window.h" #include "utilities/logger.hpp" #include "vulkan_base.hpp" namespace rse::graphics::window { -using namespace locale::window; - /* Error codes */ typedef enum { WINDOW_ERROR_NO_ERROR = 0U, diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3b6851b8..3e9d8c9c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -3,10 +3,10 @@ find_package(Criterion) add_executable(tests src/rse_math_tests.c) - - target_include_directories(tests PRIVATE -${criterion_INCLUDE_DIR}) +${criterion_INCLUDE_DIR} +${CMAKE_SOURCE_DIR}) target_link_libraries(tests PRIVATE -${criterion}) +${criterion} +rse_graphics) diff --git a/tests/src/rse_math_tests.c b/tests/src/rse_math_tests.c index 66425db1..c287fd51 100644 --- a/tests/src/rse_math_tests.c +++ b/tests/src/rse_math_tests.c @@ -1,8 +1,179 @@ -#include - #include "criterion/criterion.h" +#include "graphics/src/rse_math.h" -Test(sampe, test) + +#include +#include + + + +/*-----------------RSE MATH TEST------------------------*/ + +TestSuite(rse_math); + +Test(rse_math, normalize) { - cr_expect(strlen("Tests") == 4, "Expected \"Test\" to have lenght of 4"); + float res_x = 0.8017f; + float res_y = 0.2672f; + float res_z = 0.5345f; + struct vec3_t test_vec = {3.0f, 1.0f, 2.0f}; + + struct vec3_t result = rse_math_normalize_vec3(&test_vec); + + cr_expect_float_eq(result.x, res_x, 0.0001, "Expected %f, got %f", res_x, result.x); + cr_expect_float_eq(result.y, res_y, 0.0001, "Expected %f, got %f", res_y, result.y); + cr_expect_float_eq(result.z, res_z, 0.0001, "Expected %f, got %f", res_z, result.z); +} + +Test(rse_math, cross) +{ + float res_x = -3.0f; + float res_y = 6.0f; + float res_z = -3.0f; + struct vec3_t test_vec1 = {2.0f, 3.0f, 4.0f}; + struct vec3_t test_vec2 = {5.0f, 6.0f, 7.0f}; + + struct vec3_t result = rse_math_cross_vec3(&test_vec1, &test_vec2); + + cr_expect_float_eq(result.x, res_x, 0.0001, "Expected %f, got %f", res_x, result.x); + cr_expect_float_eq(result.y, res_y, 0.0001, "Expected %f, got %f", res_y, result.y); + cr_expect_float_eq(result.z, res_z, 0.0001, "Expected %f, got %f", res_z, result.z); +} + +Test(rse_math, look_at) +{ + struct vec3_t eye; + struct vec3_t center; + struct vec3_t up; + struct mat4_t result; + struct mat4_t expected; + + eye.x = 0.0f; + eye.y = 0.0f; + eye.z = 5.0f; + + center.x = 0.0f; + center.y = 0.0f; + center.z = 0.0f; + + up.x = 0.0f; + up.y = 0.1f; + up.z = 0.0f; + + expected.x_1 = 1.0f; + expected.y_1 = 0.0f; + expected.z_1 = 0.0f; + expected.w_1 = 0.0f; + + expected.x_2 = 0.0f; + expected.y_2 = 1.0f; + expected.z_2 = 0.0f; + expected.w_2 = 0.0f; + + expected.x_3 = 0.0f; + expected.y_3 = 0.0f; + expected.z_3 = 1.0f; + expected.w_3 = 0.0f; + + expected.x_4 = 0.0f; + expected.y_4 = 0.0f; + expected.z_4 = -5.0f; + expected.w_4 = 1.0f; + + result = rse_math_look_at(&eye, ¢er, &up); + + cr_expect_float_eq(result.x_1, expected.x_1, 0.0001, "Expected x_1 %f, got %f", expected.x_1, result.x_1); + cr_expect_float_eq(result.y_1, expected.y_1, 0.0001, "Expected y_1 %f, got %f", expected.y_1, result.y_1); + cr_expect_float_eq(result.z_1, expected.z_1, 0.0001, "Expected z_1 %f, got %f", expected.z_1, result.z_1); + cr_expect_float_eq(result.w_1, expected.w_1, 0.0001, "Expected w_1 %f, got %f", expected.w_1, result.w_1); + + cr_expect_float_eq(result.x_2, expected.x_2, 0.0001, "Expected x_2 %f, got %f", expected.x_2, result.x_2); + cr_expect_float_eq(result.y_2, expected.y_2, 0.0001, "Expected y_2 %f, got %f", expected.y_2, result.y_2); + cr_expect_float_eq(result.z_2, expected.z_2, 0.0001, "Expected z_2 %f, got %f", expected.z_2, result.z_2); + cr_expect_float_eq(result.w_2, expected.w_2, 0.0001, "Expected w_2 %f, got %f", expected.w_2, result.w_2); + + cr_expect_float_eq(result.x_3, expected.x_3, 0.0001, "Expected x_3 %f, got %f", expected.x_3, result.x_3); + cr_expect_float_eq(result.y_3, expected.y_3, 0.0001, "Expected y_3 %f, got %f", expected.y_3, result.y_3); + cr_expect_float_eq(result.z_3, expected.z_3, 0.0001, "Expected z_3 %f, got %f", expected.z_3, result.z_3); + cr_expect_float_eq(result.w_3, expected.w_3, 0.0001, "Expected w_3 %f, got %f", expected.w_3, result.w_3); + + cr_expect_float_eq(result.x_4, expected.x_4, 0.0001, "Expected x_4 %f, got %f", expected.x_4, result.x_4); + cr_expect_float_eq(result.y_4, expected.y_4, 0.0001, "Expected y_4 %f, got %f", expected.y_4, result.y_4); + cr_expect_float_eq(result.z_4, expected.z_4, 0.0001, "Expected z_4 %f, got %f", expected.z_4, result.z_4); + cr_expect_float_eq(result.w_4, expected.w_4, 0.0001, "Expected w_4 %f, got %f", expected.w_4, result.w_4); + +} + +Test(rse_math, radians) +{ + float degrees; + float radians; + float expected_radians; + + degrees = 45.0f; + expected_radians = 0.7853; + + radians = rse_math_deg_to_radians(degrees); + + cr_expect_float_eq(radians, expected_radians, 0.0001, "Expected %f, got %f", expected_radians, radians); +} + +Test(rse_math, perspective) +{ + float fovy; + float width; + float height; + float z_near; + float z_far; + struct mat4_t result; + struct mat4_t expected; + + fovy = rse_math_deg_to_radians(45.0f); + width = 640.0f; + height = 480.0f; + z_near = 0.1f; + z_far = 20.0f; + + expected.x_1 = -1.81066012; + expected.y_1 = 0; + expected.z_1 = 0; + expected.w_1 = 0; + + expected.x_2 = 0; + expected.y_2 = 2.41421342; + expected.z_2 = 0; + expected.w_2 = 0; + + expected.x_3 = 0; + expected.y_3 = 0; + expected.z_3 = -1.0100503; + expected.w_3 = -1; + + expected.x_4 = 0; + expected.y_4 = 0; + expected.z_4 = -0.201005027; + expected.w_4 = 0; + + result = rse_math_perspective(fovy, width / height, z_near, z_far); + + cr_expect_float_eq(result.x_1, expected.x_1, 0.0001, "Expected x_1 %f, got %f", expected.x_1, result.x_1); + cr_expect_float_eq(result.y_1, expected.y_1, 0.0001, "Expected y_1 %f, got %f", expected.y_1, result.y_1); + cr_expect_float_eq(result.z_1, expected.z_1, 0.0001, "Expected z_1 %f, got %f", expected.z_1, result.z_1); + cr_expect_float_eq(result.w_1, expected.w_1, 0.0001, "Expected w_1 %f, got %f", expected.w_1, result.w_1); + + cr_expect_float_eq(result.x_2, expected.x_2, 0.0001, "Expected x_2 %f, got %f", expected.x_2, result.x_2); + cr_expect_float_eq(result.y_2, expected.y_2, 0.0001, "Expected y_2 %f, got %f", expected.y_2, result.y_2); + cr_expect_float_eq(result.z_2, expected.z_2, 0.0001, "Expected z_2 %f, got %f", expected.z_2, result.z_2); + cr_expect_float_eq(result.w_2, expected.w_2, 0.0001, "Expected w_2 %f, got %f", expected.w_2, result.w_2); + + cr_expect_float_eq(result.x_3, expected.x_3, 0.0001, "Expected x_3 %f, got %f", expected.x_3, result.x_3); + cr_expect_float_eq(result.y_3, expected.y_3, 0.0001, "Expected y_3 %f, got %f", expected.y_3, result.y_3); + cr_expect_float_eq(result.z_3, expected.z_3, 0.0001, "Expected z_3 %f, got %f", expected.z_3, result.z_3); + cr_expect_float_eq(result.w_3, expected.w_3, 0.0001, "Expected w_3 %f, got %f", expected.w_3, result.w_3); + + cr_expect_float_eq(result.x_4, expected.x_4, 0.0001, "Expected x_4 %f, got %f", expected.x_4, result.x_4); + cr_expect_float_eq(result.y_4, expected.y_4, 0.0001, "Expected y_4 %f, got %f", expected.y_4, result.y_4); + cr_expect_float_eq(result.z_4, expected.z_4, 0.0001, "Expected z_4 %f, got %f", expected.z_4, result.z_4); + cr_expect_float_eq(result.w_4, expected.w_4, 0.0001, "Expected w_4 %f, got %f", expected.w_4, result.w_4); + } \ No newline at end of file diff --git a/utilities/localization.hpp b/utilities/localization.h similarity index 63% rename from utilities/localization.hpp rename to utilities/localization.h index 943fa60a..eee731f7 100644 --- a/utilities/localization.hpp +++ b/utilities/localization.h @@ -1,5 +1,5 @@ /** - * @file localization.hpp + * @file localization.h * @author Piotr Krygier (everyonencancode@gmail.com) * @brief Simple localization header * @version 0.1 @@ -9,11 +9,11 @@ * */ -#pragma once -#define SELECTED_LANGUAGE rse::utils::locale::EN_US +#ifndef LOCALIZATION_H +#define LOCALIZATION_H -namespace rse::utils::locale -{ +#define SELECTED_LANGUAGE EN_US +#define MAX_MESSAGE_LENGHT 128 /** * @brief Available languages @@ -25,4 +25,4 @@ enum Languages_t LAST_LANGUAGE }; -} // namespace rse::locale +#endif /* LOCALIZATION_H */ \ No newline at end of file