diff --git a/CMakeLists.txt b/CMakeLists.txt index 21cf4db..26f9093 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -120,9 +120,10 @@ include(GoogleTest) add_subdirectory(data_type) add_subdirectory(sdl_wrapper) add_subdirectory(glsl_reflector) +add_subdirectory(logos) add_executable(SDL_TEST main.cpp) -target_link_libraries(SDL_TEST PRIVATE imgui sdl_wrapper stb) +target_link_libraries(SDL_TEST PRIVATE imgui sdl_wrapper stb logos) include(GNUInstallDirs) install(TARGETS SDL_TEST diff --git a/README.md b/README.md index 2dde53b..b105afd 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ This is a personal learning project following [Learn OpenGL](https://learnopengl - [x] Hello Triangle - [x] Shaders - [x] Textures - - [ ] Transformations + - [x] Transformations - [ ] Coordinate Systems - [ ] Camera - [ ] Lighting diff --git a/glsl_reflector/CMakeLists.txt b/glsl_reflector/CMakeLists.txt index 5e0ec26..8631e09 100644 --- a/glsl_reflector/CMakeLists.txt +++ b/glsl_reflector/CMakeLists.txt @@ -16,6 +16,5 @@ add_executable(glslr test/test.cpp) target_link_libraries(glslr PUBLIC glsl_reflector gtest gtest_main) -add_test(NAME unit_tests COMMAND glslr) gtest_discover_tests(glslr DISCOVERY_TIMEOUT 60) diff --git a/logos/CMakeLists.txt b/logos/CMakeLists.txt new file mode 100644 index 0000000..1e24b8e --- /dev/null +++ b/logos/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.30) +project(logos LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 23) +add_library(logos STATIC) + +target_sources(logos + PUBLIC + FILE_SET cxx_modules TYPE CXX_MODULES FILES + modules/logos.ixx +) diff --git a/logos/modules/logos.ixx b/logos/modules/logos.ixx new file mode 100644 index 0000000..a0c52f9 --- /dev/null +++ b/logos/modules/logos.ixx @@ -0,0 +1,127 @@ +// logos.ixx +// Created by wsqsy on 11/26/2025. +// +module; +#include +#include +#include +export module logos; + +namespace sopho +{ + export template + class Mat + { + std::array, Row> m_data{}; + + public: + TScalar& operator()(std::uint8_t row, std::uint8_t col) { return m_data[row][col]; } + const TScalar& operator()(std::uint8_t row, std::uint8_t col) const { return m_data[row][col]; } + TScalar& operator()(std::uint8_t row) + requires(Col == 1) + { + return m_data[row][0]; + } + TScalar* data() { return m_data[0].data(); } + template + Mat operator*(const Mat& rhs) const + { + Mat result{}; + + for (std::uint8_t r = 0; r < Row; ++r) + { + for (std::uint8_t c = 0; c < OtherCol; ++c) + { + TScalar sum{}; + for (std::uint8_t k = 0; k < Col; ++k) + { + sum += (*this)(r, k) * rhs(k, c); + } + result(r, c) = sum; + } + } + + return result; + } + }; + + export Mat MakeRotationY(float yaw) + { + float cy = std::cos(yaw); + float sy = std::sin(yaw); + + Mat R{}; + + R(0, 0) = cy; + R(0, 1) = 0; + R(0, 2) = sy; + R(0, 3) = 0; + R(1, 0) = 0; + R(1, 1) = 1; + R(1, 2) = 0; + R(1, 3) = 0; + R(2, 0) = -sy; + R(2, 1) = 0; + R(2, 2) = cy; + R(2, 3) = 0; + R(3, 0) = 0; + R(3, 1) = 0; + R(3, 2) = 0; + R(3, 3) = 1; + + return R; + } + + export Mat MakeRotationX(float pitch) + { + float cp = std::cos(pitch); + float sp = std::sin(pitch); + + Mat R{}; + + R(0, 0) = 1; + R(0, 1) = 0; + R(0, 2) = 0; + R(0, 3) = 0; + R(1, 0) = 0; + R(1, 1) = cp; + R(1, 2) = sp; + R(1, 3) = 0; + R(2, 0) = 0; + R(2, 1) = -sp; + R(2, 2) = cp; + R(2, 3) = 0; + R(3, 0) = 0; + R(3, 1) = 0; + R(3, 2) = 0; + R(3, 3) = 1; + + return R; + } + + export Mat Scale(float scale_size) + { + + Mat R{}; + + R(0, 0) = scale_size; + R(0, 1) = 0; + R(0, 2) = 0; + R(0, 3) = 0; + R(1, 0) = 0; + R(1, 1) = scale_size; + R(1, 2) = 0; + R(1, 3) = 0; + R(2, 0) = 0; + R(2, 1) = 0; + R(2, 2) = scale_size; + R(2, 3) = 0; + R(3, 0) = 0; + R(3, 1) = 0; + R(3, 2) = 0; + R(3, 3) = 1; + + return R; + } + +} // namespace sopho diff --git a/main.cpp b/main.cpp index 81eef1a..0283d20 100644 --- a/main.cpp +++ b/main.cpp @@ -26,16 +26,7 @@ import data_type; import glsl_reflector; import sdl_wrapper; - -/** - * @brief Structure to hold camera transformation matrix data. - * - * Contains a 4x4 matrix (16 elements) representing the camera's view transformation. - */ -struct CameraUniform -{ - std::array m{}; ///< 4x4 transformation matrix as a flat array -}; +import logos; /** * @brief Loads image data from the test texture file. @@ -82,8 +73,6 @@ class UserApp : public sopho::App float yaw = 0.0f; float pitch = 0.0f; - CameraUniform cam{}; - std::string vertex_source = R"WSQ(#version 460 @@ -182,15 +171,6 @@ void main() .m_render_procedural = std::make_shared(std::move(pw_result.value())), .m_render_data = std::move(render_data.value())}); - // 6. Initialize camera matrix to identity. - { - cam.m.fill(0.0F); - cam.m[0] = 1.0F; - cam.m[5] = 1.0F; - cam.m[10] = 1.0F; - cam.m[15] = 1.0F; - } - // 7. Setup Dear ImGui context. IMGUI_CHECKVERSION(); ImGui::CreateContext(); @@ -464,34 +444,9 @@ void main() SDL_BindGPUGraphicsPipeline(renderPass, m_renderable->procedural()->data()); // Compute camera matrix and upload as a vertex uniform. - { - float cy = std::cos(yaw); - float sy = std::sin(yaw); - float cp = std::cos(pitch); - float sp = std::sin(pitch); - - std::array Ry = {cy, 0.0F, sy, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, - -sy, 0.0F, cy, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F}; - - std::array Rx = {1.0F, 0.0F, 0.0F, 0.0F, 0.0F, cp, sp, 0.0F, - 0.0F, -sp, cp, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F}; - - auto mulMat4 = [](const float* A, const float* B, float* C) - { - for (int col = 0; col < 4; ++col) - { - for (int row = 0; row < 4; ++row) - { - C[col * 4 + row] = A[0 * 4 + row] * B[col * 4 + 0] + A[1 * 4 + row] * B[col * 4 + 1] + - A[2 * 4 + row] * B[col * 4 + 2] + A[3 * 4 + row] * B[col * 4 + 3]; - } - } - }; - - // uView = Rx * Ry - mulMat4(Rx.data(), Ry.data(), cam.m.data()); - SDL_PushGPUVertexUniformData(commandBuffer, 0, cam.m.data(), static_cast(sizeof(cam.m))); - } + SDL_PushGPUVertexUniformData( + commandBuffer, 0, (sopho::MakeRotationY(yaw) * sopho::MakeRotationX(-pitch) * sopho::Scale(0.1F)).data(), + sizeof(sopho::Mat)); SDL_BindGPUVertexBuffers(renderPass, 0, m_renderable->data()->get_vertex_buffer_binding().data(), m_renderable->data()->get_vertex_buffer_binding().size());