From cebca182eb312898582c7e224b571adf3811e440 Mon Sep 17 00:00:00 2001 From: ZhuohaoHe Date: Tue, 6 Aug 2024 12:42:31 -0400 Subject: [PATCH 1/9] run on MacOS successfully --- CMakeLists.txt | 5 ++++- cmake/3rd.cmake | 29 +++++++++++++++++++++++++++++ cmake/compile_config.cmake | 2 +- src/log_system.cpp | 9 ++++++++- test/CMakeLists.txt | 27 ++++++++++++++++++++------- 5 files changed, 62 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 31556e8..54766fb 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,9 @@ # 设置最小 cmake 版本 cmake_minimum_required(VERSION 3.27 FATAL_ERROR) +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED True) + # 设置项目名与版本 project(SimpleRenderer VERSION 0.0.1) @@ -39,7 +42,7 @@ set(FONT_FILE_PATH "${wqy_font_SOURCE_DIR}/wqy-zenhei.ttc") include(ProcessorCount) ProcessorCount(NPROC) # 日志文件路径 -set(LOG_FILE_PATH "${EXECUTABLE_OUTPUT_PATH}/logs/SimpleRendererLog.log") +set(LOG_FILE_PATH "${PROJECT_SOURCE_DIR}/build/logs/SimpleRendererLog.log") # 日志文件大小 set(LOG_FILE_MAX_SIZE 1024*1024*4) # 日志文件数量 diff --git a/cmake/3rd.cmake b/cmake/3rd.cmake index 496cbb8..061cf41 100644 --- a/cmake/3rd.cmake +++ b/cmake/3rd.cmake @@ -57,6 +57,21 @@ CPMAddPackage( "gtest_force_shared_crt ON" ) +# SDL2 + +CPMAddPackage( + NAME SDL2 + GITHUB_REPOSITORY libsdl-org/SDL + GIT_TAG release-2.30.6 + OPTIONS + "SDL2_DISABLE_INSTALL ON" + "SDL_SHARED OFF" + "SDL_STATIC ON" + "SDL_STATIC_PIC ON" + "SDL_WERROR OFF" +) +find_package(SDL2 REQUIRED) + # https://github.com/aminosbh/sdl2-cmake-modules.git CPMAddPackage( NAME sdl2-cmake-modules @@ -64,6 +79,10 @@ CPMAddPackage( GIT_TAG ad006a3daae65a612ed87415037e32188b81071e DOWNLOAD_ONLY True ) +if (SDL2_ADDED) + add_library(SDL2::SDL2) +endif() + if (sdl2-cmake-modules_ADDED) list(APPEND CMAKE_MODULE_PATH ${sdl2-cmake-modules_SOURCE_DIR}) endif () @@ -95,6 +114,16 @@ if (tinyobjloader_ADDED) ) endif () +CPMAddPackage( + NAME glm + GITHUB_REPOSITORY g-truc/glm + GIT_TAG 1.0.1 +) +if (glm_ADDED) + add_library(glm INTERFACE IMPORTED) + target_include_directories(glm INTERFACE ${glm_SOURCE_DIR}) +endif () + # https://github.com/nothings/stb.git CPMAddPackage( NAME stb diff --git a/cmake/compile_config.cmake b/cmake/compile_config.cmake index 330a14a..a0a3981 100644 --- a/cmake/compile_config.cmake +++ b/cmake/compile_config.cmake @@ -19,6 +19,6 @@ list(APPEND DEFAULT_LINK_LIB tinyobjloader Eigen ${glog_LIBRARIES} - SDL2::Main + SDL2::SDL2 OpenMP::OpenMP_CXX ) diff --git a/src/log_system.cpp b/src/log_system.cpp index dc8cc66..9f7382e 100755 --- a/src/log_system.cpp +++ b/src/log_system.cpp @@ -16,6 +16,8 @@ #include "log_system.h" +#include + #include #include #include @@ -27,15 +29,20 @@ namespace simple_renderer { LogSystem::LogSystem(const std::string &log_file_path, size_t lig_file_max_size, size_t log_file_max_count) { spdlog::init_thread_pool(65536, 1); +// std::string log_file_paths = "./logs/simple_renderer.log"; + std::cout << "-1: " << log_file_path << std::endl; auto stdout_sink = std::make_shared(); + std::cout << "0: " << log_file_path << std::endl; auto rotating_sink = std::make_shared( log_file_path, lig_file_max_size, log_file_max_count); - + std::cout << "1: " << log_file_path << std::endl; std::vector sinks{stdout_sink, rotating_sink}; logger_ = std::make_shared( "multi_sink", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block); + std::cout << "2: " << log_file_path << std::endl; + // [年-月-日 时:分:秒.毫秒] [文件名:行号] [日志级别以彩色大写输出 8 // 字符右对齐] 内容 logger_->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%s:%# %!] [%^%l%$] %v"); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a568aeb..61bc9ed 100755 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -38,13 +38,26 @@ link_libraries( SimpleRenderer ) -add_subdirectory(unit_test) +# Check if the platform is macOS +if(APPLE) + # Enable RPATH support on macOS + set(CMAKE_MACOSX_RPATH 1) + + # Set the RPATH to look for frameworks relative to the executable path + set(CMAKE_INSTALL_RPATH "/Library/Frameworks") + + # Ensure that RPATH is used in the build + set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) +endif() + + +# add_subdirectory(unit_test) #add_subdirectory(integration_test) add_subdirectory(system_test) -add_coverage_target( - DEPENDS unit_test - SOURCE_DIR ${SimpleRenderer_SOURCE_DIR} - BINARY_DIR ${SimpleRenderer_BINARY_DIR} - EXCLUDE_DIR ${SimpleRenderer_SOURCE_DIR}/3rd/* -) +# add_coverage_target( +# DEPENDS unit_test +# SOURCE_DIR ${SimpleRenderer_SOURCE_DIR} +# BINARY_DIR ${SimpleRenderer_BINARY_DIR} +# EXCLUDE_DIR ${SimpleRenderer_SOURCE_DIR}/3rd/* +# ) From 78ba9f3c16cd5f4783232e33190952735afc0abe Mon Sep 17 00:00:00 2001 From: ZhuohaoHe Date: Tue, 6 Aug 2024 12:43:17 -0400 Subject: [PATCH 2/9] update log_system --- src/log_system.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/log_system.cpp b/src/log_system.cpp index 9f7382e..6c81b14 100755 --- a/src/log_system.cpp +++ b/src/log_system.cpp @@ -30,19 +30,14 @@ LogSystem::LogSystem(const std::string &log_file_path, size_t lig_file_max_size, size_t log_file_max_count) { spdlog::init_thread_pool(65536, 1); // std::string log_file_paths = "./logs/simple_renderer.log"; - std::cout << "-1: " << log_file_path << std::endl; auto stdout_sink = std::make_shared(); - std::cout << "0: " << log_file_path << std::endl; auto rotating_sink = std::make_shared( log_file_path, lig_file_max_size, log_file_max_count); - std::cout << "1: " << log_file_path << std::endl; std::vector sinks{stdout_sink, rotating_sink}; logger_ = std::make_shared( "multi_sink", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block); - std::cout << "2: " << log_file_path << std::endl; - // [年-月-日 时:分:秒.毫秒] [文件名:行号] [日志级别以彩色大写输出 8 // 字符右对齐] 内容 logger_->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%s:%# %!] [%^%l%$] %v"); From cebb0663923cbb9287da7bc17552aebb296e1263 Mon Sep 17 00:00:00 2001 From: ZhuohaoHe Date: Tue, 6 Aug 2024 13:42:15 -0400 Subject: [PATCH 3/9] replace Eigen by glm --- cmake/3rd.cmake | 6 ++ cmake/compile_config.cmake | 2 +- src/default_shader.cpp | 22 +++--- src/include/default_shader.h | 2 +- src/include/light.h | 8 +- src/include/matrix.hpp | 17 +++-- src/include/model.hpp | 18 ++--- src/include/shader_base.h | 20 ++--- src/include/simple_renderer.h | 8 +- src/include/vector.hpp | 37 +++++---- src/light.cpp | 4 +- src/model.cpp | 137 ++++++++++++++++++---------------- src/shader_base.cpp | 12 +-- src/simple_renderer.cpp | 78 +++++++++---------- test/system_test/camera.h | 4 +- test/system_test/main.cpp | 11 ++- 16 files changed, 203 insertions(+), 183 deletions(-) diff --git a/cmake/3rd.cmake b/cmake/3rd.cmake index 061cf41..2be4a15 100644 --- a/cmake/3rd.cmake +++ b/cmake/3rd.cmake @@ -281,3 +281,9 @@ if (NOT spdlog_FOUND) message(FATAL_ERROR "spdlog not found.\n" "Following https://github.com/gabime/spdlog to install.") endif () + +find_package(glm REQUIRED) +if (NOT glm_FOUND) + message(FATAL_ERROR "glm not found.\n" + "Following https://github.com/g-truc/glm tp install") +endif () \ No newline at end of file diff --git a/cmake/compile_config.cmake b/cmake/compile_config.cmake index a0a3981..b82917f 100644 --- a/cmake/compile_config.cmake +++ b/cmake/compile_config.cmake @@ -17,7 +17,7 @@ list(APPEND DEFAULT_LINK_LIB spdlog::spdlog stb tinyobjloader - Eigen + glm::glm ${glog_LIBRARIES} SDL2::SDL2 OpenMP::OpenMP_CXX diff --git a/src/default_shader.cpp b/src/default_shader.cpp index 4a90f8a..82b1732 100644 --- a/src/default_shader.cpp +++ b/src/default_shader.cpp @@ -20,26 +20,26 @@ namespace simple_renderer { auto DefaultShader::InterpolateColor( const Color &color0, const Color &color1, const Color &color2, - const Vector3f &barycentric_coord) -> Color { + const glm::vec3 &barycentric_coord) -> Color { return Color( static_cast(static_cast(color0[Color::kColorIndexRed]) * - barycentric_coord.x() + + barycentric_coord.x + static_cast(color1[Color::kColorIndexRed]) * - barycentric_coord.y() + + barycentric_coord.y + static_cast(color2[Color::kColorIndexRed]) * - barycentric_coord.z()), + barycentric_coord.z), static_cast(static_cast(color0[Color::kColorIndexGreen]) * - barycentric_coord.x() + + barycentric_coord.x + static_cast(color1[Color::kColorIndexGreen]) * - barycentric_coord.y() + + barycentric_coord.y + static_cast(color2[Color::kColorIndexGreen]) * - barycentric_coord.z()), + barycentric_coord.z), static_cast(static_cast(color0[Color::kColorIndexBlue]) * - barycentric_coord.x() + + barycentric_coord.x + static_cast(color1[Color::kColorIndexBlue]) * - barycentric_coord.y() + + barycentric_coord.y + static_cast(color2[Color::kColorIndexBlue]) * - barycentric_coord.z())); + barycentric_coord.z)); } /// @todo 巨大性能开销 @@ -56,7 +56,7 @@ auto DefaultShader::Vertex(const ShaderVertexIn &shader_vertex_in) const auto DefaultShader::Fragment(const ShaderFragmentIn &shader_fragment_in) const -> ShaderFragmentOut { - auto intensity = (shader_fragment_in.normal_.dot(shader_fragment_in.light_)); + auto intensity = glm::dot(shader_fragment_in.normal_,shader_fragment_in.light_); auto is_need_draw = true; // 光照方向为正,不绘制背面 if (intensity <= 0) { diff --git a/src/include/default_shader.h b/src/include/default_shader.h index 717c802..a187577 100644 --- a/src/include/default_shader.h +++ b/src/include/default_shader.h @@ -64,7 +64,7 @@ class DefaultShader : public ShaderBase { */ static auto InterpolateColor(const Color &color0, const Color &color1, const Color &color2, - const Vector3f &barycentric_coord) -> Color; + const glm::vec3 &barycentric_coord) -> Color; }; } // namespace simple_renderer diff --git a/src/include/light.h b/src/include/light.h index f566308..c5307d9 100644 --- a/src/include/light.h +++ b/src/include/light.h @@ -33,9 +33,9 @@ class Light { /// 光照名称 std::string name_ = "default light name"; /// 位置 - Vector3f pos = kDefaultPos; + glm::vec3 pos = kDefaultPos; /// 方向 - Vector3f dir = kDefaultDir; + glm::vec3 dir = kDefaultDir; /// 颜色 Color color = kDefaultColor; @@ -57,9 +57,9 @@ class Light { private: /// 默认位置 - static const Vector3f kDefaultPos; + static const glm::vec3 kDefaultPos; /// 默认方向,左手系,x 向右,y 向下,z 正方向为屏幕由内向外 - static const Vector3f kDefaultDir; + static const glm::vec3 kDefaultDir; /// 默认颜色 static const Color kDefaultColor; }; diff --git a/src/include/matrix.hpp b/src/include/matrix.hpp index 30a1e1b..29b7f67 100755 --- a/src/include/matrix.hpp +++ b/src/include/matrix.hpp @@ -17,13 +17,13 @@ #ifndef SIMPLERENDER_SRC_INCLUDE_MATRIX_HPP_ #define SIMPLERENDER_SRC_INCLUDE_MATRIX_HPP_ -#include +#define GLM_ENABLE_EXPERIMENTAL +#include #include "log_system.h" namespace simple_renderer { -using Matrix4f = Eigen::Matrix4f; } // namespace simple_renderer @@ -31,12 +31,13 @@ using Matrix4f = Eigen::Matrix4f; * spdlog 输出矩阵实现 */ template <> -struct fmt::formatter : fmt::formatter { - auto format(simple_renderer::Matrix4f matrix, format_context &format_context) - const -> decltype(format_context.out()) { - std::stringstream buf; - buf << matrix; - return fmt::format_to(format_context.out(), "\n{}", buf.str()); +struct fmt::formatter : fmt::formatter { + auto format(const glm::mat4 &matrix, fmt::format_context &ctx) const -> decltype(ctx.out()) { + // Convert the glm::mat4 to a string using glm::to_string + std::string matrix_str = glm::to_string(matrix); + + // Format and output the string + return fmt::format_to(ctx.out(), "\n{}", matrix_str); } }; diff --git a/src/include/model.hpp b/src/include/model.hpp index 635436e..0e9e877 100755 --- a/src/include/model.hpp +++ b/src/include/model.hpp @@ -36,22 +36,22 @@ namespace simple_renderer { class Model { public: /// 顶点坐标 - using Coord = Vector3f; + using Coord = glm::vec3; /// 法向量 - using Normal = Vector3f; + using Normal = glm::vec3; /// 贴图 - using TextureCoord = Vector2f; + using TextureCoord = glm::vec2; class Material { public: /// 反光度 float shininess = 0; /// 环境光照 - Vector3f ambient; + glm::vec3 ambient; /// 漫反射光照 - Vector3f diffuse; + glm::vec3 diffuse; /// 镜面光照 - Vector3f specular; + glm::vec3 specular; /// @name 默认构造/析构函数 /// @{ @@ -108,7 +108,7 @@ class Model { * @param tran 要对顶点进行的变换矩阵 * @return 结果 */ - [[nodiscard]] auto operator*(const Matrix4f &tran) const -> Vertex; + [[nodiscard]] auto operator*(const glm::mat4 &tran) const -> Vertex; }; /// @todo 直接保存太浪费内存了 @@ -148,7 +148,7 @@ class Model { * @param tran 要对面进行的变换矩阵 * @return 结果 */ - [[nodiscard]] auto operator*(const Matrix4f &tran) const -> Face; + [[nodiscard]] auto operator*(const glm::mat4 &tran) const -> Face; }; /// obj 文件路径 @@ -179,7 +179,7 @@ class Model { * @param tran 要对模型进行的变换矩阵 * @return 结果 */ - [[nodiscard]] auto operator*(const Matrix4f &tran) const -> Model; + [[nodiscard]] auto operator*(const glm::mat4 &tran) const -> Model; /** * 获取面 diff --git a/src/include/shader_base.h b/src/include/shader_base.h index aa04937..a97e429 100644 --- a/src/include/shader_base.h +++ b/src/include/shader_base.h @@ -83,11 +83,11 @@ class ShaderVertexOut { class ShaderFragmentIn { public: /// 重心坐标 - Vector3f barycentric_coord_; + glm::vec3 barycentric_coord_; /// 法线方向 - Vector3f normal_; + glm::vec3 normal_; /// 光照方向 - Vector3f light_; + glm::vec3 light_; /// @name 三个顶点的颜色 /// @{ @@ -105,8 +105,8 @@ class ShaderFragmentIn { * @param color1 顶点 1 颜色 * @param color2 顶点 2 颜色 */ - explicit ShaderFragmentIn(const Vector3f &_barycentric_coord, - const Vector3f &_normal, const Vector3f &_light, + explicit ShaderFragmentIn(const glm::vec3 &_barycentric_coord, + const glm::vec3 &_normal, const glm::vec3 &_light, const Color &_color0, const Color &_color1, const Color &_color2); @@ -160,11 +160,11 @@ class ShaderFragmentOut { class ShaderData { public: /// 模型变换矩阵 - Matrix4f model_matrix_ = Matrix4f().setIdentity(); + glm::mat4 model_matrix_ = glm::mat4(1.0f); /// 视图变换矩阵 - Matrix4f view_matrix_ = Matrix4f().setIdentity(); + glm::mat4 view_matrix_ = glm::mat4(1.0f); /// 正交变换矩阵 - Matrix4f project_matrix_ = Matrix4f().setIdentity(); + glm::mat4 project_matrix_ = glm::mat4(1.0f); /** * 构造函数 @@ -172,8 +172,8 @@ class ShaderData { * @param view_matrix 视图变换矩阵 * @param project_matrix 正交变换矩阵 */ - explicit ShaderData(const Matrix4f &model_matrix, const Matrix4f &view_matrix, - const Matrix4f &project_matrix); + explicit ShaderData(const glm::mat4 &model_matrix, const glm::mat4 &view_matrix, + const glm::mat4 &project_matrix); /// @name 默认构造/析构函数 /// @{ diff --git a/src/include/simple_renderer.h b/src/include/simple_renderer.h index 9aa587f..05f7a44 100755 --- a/src/include/simple_renderer.h +++ b/src/include/simple_renderer.h @@ -128,9 +128,9 @@ class SimpleRenderer { * weight_B = s * weight_C = t */ - static auto GetBarycentricCoord(const Vector3f &p0, const Vector3f &p1, - const Vector3f &p2, const Vector3f &pa) - -> std::pair; + static auto GetBarycentricCoord(const glm::vec3 &p0, const glm::vec3 &p1, + const glm::vec3 &p2, const glm::vec3 &pa) + -> std::pair; /** * 深度插值,由重心坐标计算出对应点的深度值 @@ -141,7 +141,7 @@ class SimpleRenderer { * @return 深度值 */ static auto InterpolateDepth(float depth0, float depth1, float depth2, - const Vector3f &barycentric_coord) -> float; + const glm::vec3 &barycentric_coord) -> float; }; } // namespace simple_renderer diff --git a/src/include/vector.hpp b/src/include/vector.hpp index eab8995..4b8f96a 100755 --- a/src/include/vector.hpp +++ b/src/include/vector.hpp @@ -17,42 +17,41 @@ #ifndef SIMPLERENDER_SRC_INCLUDE_VECTOR_HPP_ #define SIMPLERENDER_SRC_INCLUDE_VECTOR_HPP_ -#include +#include + +#define GLM_ENABLE_EXPERIMENTAL +#include #include "log_system.h" namespace simple_renderer { -using Vector2f = Eigen::Vector2f; -using Vector3f = Eigen::Vector3f; -using Vector4f = Eigen::Vector4f; - } // namespace simple_renderer /** * spdlog 输出 Vector3f 实现 */ template <> -struct fmt::formatter : fmt::formatter { - auto format(simple_renderer::Vector3f vector, format_context &format_context) - const -> decltype(format_context.out()) { - std::stringstream buf; - buf << vector; - return fmt::format_to(format_context.out(), "\n{}", buf.str()); - } +struct fmt::formatter : fmt::formatter { + auto format(const glm::vec3 &vector, fmt::format_context &ctx) const -> decltype(ctx.out()) { + std::string vector_string = glm::to_string(vector); + + // Format and output the string + return fmt::format_to(ctx.out(), "\n{}", vector_string); + } }; /** * spdlog 输出 Vector4f 实现 */ template <> -struct fmt::formatter : fmt::formatter { - auto format(simple_renderer::Vector4f vector, format_context &format_context) - const -> decltype(format_context.out()) { - std::stringstream buf; - buf << vector; - return fmt::format_to(format_context.out(), "\n{}", buf.str()); - } +struct fmt::formatter : fmt::formatter { + auto format(const glm::vec4 &vector, fmt::format_context &ctx) const -> decltype(ctx.out()) { + std::string vector_string = glm::to_string(vector); + + // Format and output the string + return fmt::format_to(ctx.out(), "\n{}", vector_string); + } }; #endif /* SIMPLERENDER_SRC_INCLUDE_VECTOR_HPP_ */ diff --git a/src/light.cpp b/src/light.cpp index 21c0254..61503d0 100644 --- a/src/light.cpp +++ b/src/light.cpp @@ -22,8 +22,8 @@ namespace simple_renderer { -const Vector3f Light::kDefaultPos = Vector3f(0, 0, 0); -const Vector3f Light::kDefaultDir = Vector3f(0, 0, -1); +const glm::vec3 Light::kDefaultPos = glm::vec3(0, 0, 0); +const glm::vec3 Light::kDefaultDir = glm::vec3(0, 0, -1); const Color Light::kDefaultColor = Color::kWhite; Light::Light(const std::string &name) : name_(name) { diff --git a/src/model.cpp b/src/model.cpp index 8ce949b..19f7423 100755 --- a/src/model.cpp +++ b/src/model.cpp @@ -15,6 +15,7 @@ */ #include "model.hpp" +#include #include @@ -32,19 +33,21 @@ Model::Vertex::Vertex(Coord coord, Normal normal, TextureCoord texture_coord, texture_coord_(std::move(texture_coord)), color_(color) {} -auto Model::Vertex::operator*(const Matrix4f &tran) const -> Model::Vertex { - auto vertex(*this); +auto Model::Vertex::operator*(const glm::mat4 &tran) const -> Model::Vertex { + Vertex vertex(*this); - auto res4 = Vector4f(coord_.x(), coord_.y(), coord_.z(), 1); - auto ret4 = Vector4f(tran * res4); + // Convert the 3D coordinate to a 4D vector by adding a 1.0 w-component + glm::vec4 res4(coord_, 1.0f); - vertex.coord_.x() = ret4.x(); - vertex.coord_.y() = ret4.y(); - vertex.coord_.z() = ret4.z(); + // Apply the transformation matrix + glm::vec4 ret4 = tran * res4; - /// @todo 变换法线 + // Update the vertex coordinates with the transformed values + vertex.coord_ = glm::vec3(ret4); - return vertex; + /// @todo 变换法线 + + return vertex; } Model::Face::Face(const Model::Vertex &v0, const Model::Vertex &v1, @@ -52,31 +55,31 @@ Model::Face::Face(const Model::Vertex &v0, const Model::Vertex &v1, : v0_(v0), v1_(v1), v2_(v2), material_(std::move(material)) { // 计算法向量 // 如果 obj 内包含法向量,直接使用即可 - if (v0.normal_.norm() != 0 && v1.normal_.norm() != 0 && - v2.normal_.norm() != 0) { - normal_ = (v0.normal_ + v1.normal_ + v2.normal_).normalized(); + if (glm::normalize(v0.normal_) != glm::vec3(0.0f) && glm::normalize(v1.normal_) != glm::vec3(0.0f) && + glm::normalize(v2.normal_) != glm::vec3(0.0f)) { + normal_ = glm::normalize((v0.normal_ + v1.normal_ + v2.normal_)); } // 手动计算 else { // 两条相临边的叉积 auto v2v0 = v2.coord_ - v0.coord_; auto v1v0 = v1.coord_ - v0.coord_; - normal_ = (v2v0.cross(v1v0)).normalized(); + normal_ = glm::normalize(glm::cross(v2v0, v1v0)); } } -auto Model::Face::operator*(const Matrix4f &tran) const -> Model::Face { - auto face(*this); - face.v0_ = face.v0_ * tran; - face.v1_ = face.v1_ * tran; - face.v2_ = face.v2_ * tran; +auto Model::Face::operator*(const glm::mat4 &tran) const -> Model::Face { + auto face(*this); + face.v0_ = face.v0_ * tran; + face.v1_ = face.v1_ * tran; + face.v2_ = face.v2_ * tran; - /// @todo 通过矩阵变换法线 - auto v2v0 = face.v2_.coord_ - face.v0_.coord_; - auto v1v0 = face.v1_.coord_ - face.v0_.coord_; - face.normal_ = (v2v0.cross(v1v0)).normalized(); + // Calculate the transformed normal + glm::vec3 v2v0 = face.v2_.coord_ - face.v0_.coord_; + glm::vec3 v1v0 = face.v1_.coord_ - face.v0_.coord_; + face.normal_ = glm::normalize(glm::cross(v2v0, v1v0)); - return face; + return face; } Model::Model(const std::string &obj_path, const std::string &mtl_path) @@ -143,7 +146,7 @@ Model::Model(const std::string &obj_path, const std::string &mtl_path) // 如果法线索引存在(即 idx.normal_index >= 0), // 则构造并保存,否则设置为 0 - Normal normal = Normal::Zero(); + Normal normal = glm::vec3(0.0f); if (idx.normal_index >= 0) { normal = Normal(attrib.normals[3 * size_t(idx.normal_index) + 0], attrib.normals[3 * size_t(idx.normal_index) + 1], @@ -152,7 +155,7 @@ Model::Model(const std::string &obj_path, const std::string &mtl_path) // 如果贴图索引存在(即 idx.texcoord_index >= 0), // 则构造并保存,否则设置为 0 - TextureCoord texture_coord = TextureCoord::Zero(); + TextureCoord texture_coord = glm::vec3(0.0f); if (idx.texcoord_index >= 0) { texture_coord = TextureCoord( attrib.texcoords[2 * size_t(idx.texcoord_index) + 0], @@ -172,13 +175,13 @@ Model::Model(const std::string &obj_path, const std::string &mtl_path) auto material = Material(); if (!materials.empty()) { material.shininess = materials[shape_index].shininess; - material.ambient = Vector3f(materials[shape_index].ambient[0], + material.ambient = glm::vec3(materials[shape_index].ambient[0], materials[shape_index].ambient[1], materials[shape_index].ambient[2]); - material.diffuse = Vector3f(materials[shape_index].diffuse[0], + material.diffuse = glm::vec3(materials[shape_index].diffuse[0], materials[shape_index].diffuse[1], materials[shape_index].diffuse[2]); - material.specular = Vector3f(materials[shape_index].specular[0], + material.specular = glm::vec3(materials[shape_index].specular[0], materials[shape_index].specular[1], materials[shape_index].specular[2]); } @@ -190,7 +193,7 @@ Model::Model(const std::string &obj_path, const std::string &mtl_path) Normalize(); } -auto Model::operator*(const Matrix4f &tran) const -> Model { +auto Model::operator*(const glm::mat4 &tran) const -> Model { auto model = Model(*this); for (auto &i : model.faces_) { @@ -213,27 +216,27 @@ std::pair Model::GetMaxMinXYX() { std::numeric_limits::max()); for (const auto &i : faces_) { - auto curr_max_x = std::max(i.v0_.coord_.x(), - std::max(i.v1_.coord_.x(), i.v2_.coord_.x())); - auto curr_max_y = std::max(i.v0_.coord_.y(), - std::max(i.v1_.coord_.y(), i.v2_.coord_.y())); - auto curr_max_z = std::max(i.v0_.coord_.z(), - std::max(i.v1_.coord_.z(), i.v2_.coord_.z())); - - max.x() = curr_max_x > max.x() ? curr_max_x : max.x(); - max.y() = curr_max_y > max.y() ? curr_max_y : max.y(); - max.z() = curr_max_z > max.z() ? curr_max_z : max.z(); - - auto curr_min_x = std::min(i.v0_.coord_.x(), - std::min(i.v1_.coord_.x(), i.v2_.coord_.x())); - auto curr_min_y = std::min(i.v0_.coord_.y(), - std::min(i.v1_.coord_.y(), i.v2_.coord_.y())); - auto curr_min_z = std::min(i.v0_.coord_.z(), - std::min(i.v1_.coord_.z(), i.v2_.coord_.z())); - - min.x() = curr_min_x < min.x() ? curr_min_x : min.x(); - min.y() = curr_min_y < min.y() ? curr_min_y : min.y(); - min.z() = curr_min_z < min.z() ? curr_min_z : min.z(); + auto curr_max_x = std::max(i.v0_.coord_.x, + std::max(i.v1_.coord_.x, i.v2_.coord_.x)); + auto curr_max_y = std::max(i.v0_.coord_.y, + std::max(i.v1_.coord_.y, i.v2_.coord_.y)); + auto curr_max_z = std::max(i.v0_.coord_.z, + std::max(i.v1_.coord_.z, i.v2_.coord_.z)); + + max.x = curr_max_x > max.x ? curr_max_x : max.x; + max.y = curr_max_y > max.y ? curr_max_y : max.y; + max.z = curr_max_z > max.z ? curr_max_z : max.z; + + auto curr_min_x = std::min(i.v0_.coord_.x, + std::min(i.v1_.coord_.x, i.v2_.coord_.x)); + auto curr_min_y = std::min(i.v0_.coord_.y, + std::min(i.v1_.coord_.y, i.v2_.coord_.y)); + auto curr_min_z = std::min(i.v0_.coord_.z, + std::min(i.v1_.coord_.z, i.v2_.coord_.z)); + + min.x = curr_min_x < min.x ? curr_min_x : min.x; + min.y = curr_min_y < min.y ? curr_min_y : min.y; + min.z = curr_min_z < min.z ? curr_min_z : min.z; } return {max, min}; } @@ -241,25 +244,31 @@ std::pair Model::GetMaxMinXYX() { void Model::Normalize() { auto [max, min] = GetMaxMinXYX(); - auto x = std::abs(max.x()) + std::abs(min.x()); - auto y = std::abs(max.y()) + std::abs(min.y()); - auto z = std::abs(max.z()) + std::abs(min.z()); + // Compute the dimensions of the bounding box + auto x = std::abs(max.x) + std::abs(min.x); + auto y = std::abs(max.y) + std::abs(min.y); + auto z = std::abs(max.z) + std::abs(min.z); + + // Calculate the scaling factor + auto scale = 1.0f / std::max(x, std::max(y, z)); + + // Create the scaling matrix + glm::mat4 scale_matrix = glm::scale(glm::mat4(1.0f), glm::vec3(scale, scale, scale)); - auto scale = 1.0f / std::max(x, std::max(y, z)); - auto scale_matrix = Matrix4f(Matrix4f::Identity()); - scale_matrix.diagonal() << scale, scale, scale, 1; + // Calculate the center of the bounding box + glm::vec3 center = glm::vec3((max.x + min.x) / 2.0f, (max.y + min.y) / 2.0f, (max.z + min.z) / 2.0f); - auto center = Coord((max.x() + min.x()) / 2.f, (max.y() + min.y()) / 2.f, - (max.z() + min.z()) / 2.f); - auto translation_matrix = Matrix4f(Matrix4f::Identity()); - translation_matrix.col(translation_matrix.cols() - 1) << -center.x(), - -center.y(), -center.z(), 1; + // Create the translation matrix + glm::mat4 translation_matrix = glm::translate(glm::mat4(1.0f), -center); - auto matrix = Matrix4f(scale_matrix * translation_matrix); + // Combine the scaling and translation matrices + glm::mat4 normalization_matrix = scale_matrix * translation_matrix; - SPDLOG_DEBUG("matrix: {}", matrix); + // Debug output + SPDLOG_DEBUG("normalization_matrix: \n{}", glm::to_string(normalization_matrix)); - *this = *this * matrix; + // Apply the normalization matrix to the model + *this = *this * normalization_matrix; } } // namespace simple_renderer diff --git a/src/shader_base.cpp b/src/shader_base.cpp index c0e9a83..50a2968 100644 --- a/src/shader_base.cpp +++ b/src/shader_base.cpp @@ -22,9 +22,9 @@ ShaderVertexIn::ShaderVertexIn(const Model::Face &face) : face_(face) {} ShaderVertexOut::ShaderVertexOut(const Model::Face &face) : face_(face) {} -ShaderFragmentIn::ShaderFragmentIn(const Vector3f &barycentric_coord, - const Vector3f &normal, - const Vector3f &light, const Color &color0, +ShaderFragmentIn::ShaderFragmentIn(const glm::vec3 &barycentric_coord, + const glm::vec3 &normal, + const glm::vec3 &light, const Color &color0, const Color &color1, const Color &color2) : barycentric_coord_(barycentric_coord), normal_(normal), @@ -37,9 +37,9 @@ ShaderFragmentOut::ShaderFragmentOut(const bool &is_need_draw, const Color &color) : is_need_draw_(is_need_draw), color_(color) {} -ShaderData::ShaderData(const Matrix4f &model_matrix, - const Matrix4f &view_matrix, - const Matrix4f &project_matrix) +ShaderData::ShaderData(const glm::mat4 &model_matrix, + const glm::mat4 &view_matrix, + const glm::mat4 &project_matrix) : model_matrix_(model_matrix), view_matrix_(view_matrix), project_matrix_(project_matrix) {} diff --git a/src/simple_renderer.cpp b/src/simple_renderer.cpp index c259899..b01b9a7 100755 --- a/src/simple_renderer.cpp +++ b/src/simple_renderer.cpp @@ -135,38 +135,38 @@ void SimpleRenderer::DrawTriangle(const ShaderBase &shader, const Light &light, // 获取三角形的最小 box auto min = v0.coord_; auto max = v1.coord_; - auto max_x = std::max(face.v0_.coord_.x(), - std::max(face.v1_.coord_.x(), face.v2_.coord_.x())); - auto max_y = std::max(face.v0_.coord_.y(), - std::max(face.v1_.coord_.y(), face.v2_.coord_.y())); - max.x() = max_x > max.x() ? max_x : max.x(); - max.y() = max_y > max.y() ? max_y : max.y(); - max.z() = 0; - auto min_x = std::min(face.v0_.coord_.x(), - std::min(face.v1_.coord_.x(), face.v2_.coord_.x())); - auto min_y = std::min(face.v0_.coord_.y(), - std::min(face.v1_.coord_.y(), face.v2_.coord_.y())); - min.x() = min_x < min.x() ? min_x : min.x(); - min.y() = min_y < min.y() ? min_y : min.y(); - min.z() = 0; + auto max_x = std::max(face.v0_.coord_.x, + std::max(face.v1_.coord_.x, face.v2_.coord_.x)); + auto max_y = std::max(face.v0_.coord_.y, + std::max(face.v1_.coord_.y, face.v2_.coord_.y)); + max.x = max_x > max.x ? max_x : max.x; + max.y = max_y > max.y ? max_y : max.y; + max.z = 0; + auto min_x = std::min(face.v0_.coord_.x, + std::min(face.v1_.coord_.x, face.v2_.coord_.x)); + auto min_y = std::min(face.v0_.coord_.y, + std::min(face.v1_.coord_.y, face.v2_.coord_.y)); + min.x = min_x < min.x ? min_x : min.x; + min.y = min_y < min.y ? min_y : min.y; + min.z = 0; #pragma omp parallel for num_threads(kNProc) collapse(2) default(none) \ shared(min, max, v0, v1, v2, shader) firstprivate(normal, light) - for (auto x = int32_t(min.x()); x <= int32_t(max.x()); x++) { - for (auto y = int32_t(min.y()); y <= int32_t(max.y()); y++) { + for (auto x = int32_t(min.x); x <= int32_t(max.x); x++) { + for (auto y = int32_t(min.y); y <= int32_t(max.y); y++) { /// @todo 这里要用裁剪替换掉 if ((unsigned)x >= width_ || (unsigned)y >= height_) { continue; } auto [is_inside, barycentric_coord] = GetBarycentricCoord( v0.coord_, v1.coord_, v2.coord_, - Vector3f(static_cast(x), static_cast(y), 0)); + glm::vec3(static_cast(x), static_cast(y), 0)); // 如果点在三角形内再进行下一步 if (!is_inside) { continue; } // 计算该点的深度,通过重心坐标插值计算 - auto z = InterpolateDepth(v0.coord_.z(), v1.coord_.z(), v2.coord_.z(), + auto z = InterpolateDepth(v0.coord_.z, v1.coord_.z, v2.coord_.z, barycentric_coord); // 深度在已有颜色之上 if (z < depth_buffer_[y * width_ + x]) { @@ -202,12 +202,12 @@ void SimpleRenderer::DrawModel(const ShaderBase &shader, const Light &light, for (const auto &f : model.GetFace()) { /// @todo 巨大性能开销 auto face = shader.Vertex(ShaderVertexIn(f)).face_; - DrawLine(face.v0_.coord_.x(), face.v0_.coord_.y(), face.v1_.coord_.x(), - face.v1_.coord_.y(), Color::kRed); - DrawLine(face.v1_.coord_.x(), face.v1_.coord_.y(), face.v2_.coord_.x(), - face.v2_.coord_.y(), Color::kGreen); - DrawLine(face.v2_.coord_.x(), face.v2_.coord_.y(), face.v0_.coord_.x(), - face.v0_.coord_.y(), Color::kBlue); + DrawLine(face.v0_.coord_.x, face.v0_.coord_.y, face.v1_.coord_.x, + face.v1_.coord_.y, Color::kRed); + DrawLine(face.v1_.coord_.x, face.v1_.coord_.y, face.v2_.coord_.x, + face.v2_.coord_.y, Color::kGreen); + DrawLine(face.v2_.coord_.x, face.v2_.coord_.y, face.v0_.coord_.x, + face.v0_.coord_.y, Color::kBlue); } } if (draw_triangle) { @@ -222,41 +222,41 @@ void SimpleRenderer::DrawModel(const ShaderBase &shader, const Light &light, } /// @todo 巨大性能开销 -auto SimpleRenderer::GetBarycentricCoord(const Vector3f &p0, const Vector3f &p1, - const Vector3f &p2, const Vector3f &pa) - -> std::pair { +auto SimpleRenderer::GetBarycentricCoord(const glm::vec3 &p0, const glm::vec3 &p1, + const glm::vec3 &p2, const glm::vec3 &pa) + -> std::pair { auto p1p0 = p1 - p0; auto p2p0 = p2 - p0; auto pap0 = pa - p0; - auto deno = (p1p0.x() * p2p0.y() - p1p0.y() * p2p0.x()); + auto deno = (p1p0.x * p2p0.y - p1p0.y * p2p0.x); if (std::abs(deno) < std::numeric_limits::epsilon()) { - return std::pair{false, Vector3f()}; + return std::pair{false, glm::vec3()}; } - auto s = (p2p0.y() * pap0.x() - p2p0.x() * pap0.y()) / deno; + auto s = (p2p0.y * pap0.x - p2p0.x * pap0.y) / deno; if ((s > 1) || (s < 0)) { - return std::pair{false, Vector3f()}; + return std::pair{false, glm::vec3()}; } - auto t = (p1p0.x() * pap0.y() - p1p0.y() * pap0.x()) / deno; + auto t = (p1p0.x * pap0.y - p1p0.y * pap0.x) / deno; if ((t > 1) || (t < 0)) { - return std::pair{false, Vector3f()}; + return std::pair{false, glm::vec3()}; } if ((1 - s - t > 1) || (1 - s - t < 0)) { - return std::pair{false, Vector3f()}; + return std::pair{false, glm::vec3()}; } - return std::pair{true, Vector3f(1 - s - t, s, t)}; + return std::pair{true, glm::vec3(1 - s - t, s, t)}; } auto SimpleRenderer::InterpolateDepth(float depth0, float depth1, float depth2, - const Vector3f &_barycentric_coord) + const glm::vec3 &_barycentric_coord) -> float { - auto depth = depth0 * _barycentric_coord.x(); - depth += depth1 * _barycentric_coord.y(); - depth += depth2 * _barycentric_coord.z(); + auto depth = depth0 * _barycentric_coord.x; + depth += depth1 * _barycentric_coord.y; + depth += depth2 * _barycentric_coord.z; return depth; } diff --git a/test/system_test/camera.h b/test/system_test/camera.h index ffaf55b..b661e1f 100644 --- a/test/system_test/camera.h +++ b/test/system_test/camera.h @@ -33,9 +33,9 @@ class Camera { /// 光照名称 std::string name_ = "default light name"; /// 位置 - Vector3f pos_; + glm::vec3 pos_; /// 方向 - Vector3f dir_; + glm::vec3 dir_; /** * 构造函数 diff --git a/test/system_test/main.cpp b/test/system_test/main.cpp index 6000cd2..0a166c3 100755 --- a/test/system_test/main.cpp +++ b/test/system_test/main.cpp @@ -61,9 +61,14 @@ int main(int argc, char **argv) { auto matrix = - simple_renderer::Matrix4f(simple_renderer::Matrix4f::Identity()); - matrix.diagonal() << 500, 500, 500, 1; - matrix.col(matrix.cols() - 1) << kWidth / 2, kHeight / 2, 0, 1; + glm::mat4(1.0f); + glm::mat4 scale_matrix = glm::scale(glm::mat4(1.0f), glm::vec3(500.0f, 500.0f, 500.0f)); + +// Translation matrix + glm::mat4 translation_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(kWidth / 2.0f, kHeight / 2.0f, 0.0f)); + + // Combined transformation matrix + matrix = translation_matrix * scale_matrix; // 矩阵运算的顺序 // 归一化 From 049e985dd684b284c53c30d4ed36da9b96f45cab Mon Sep 17 00:00:00 2001 From: ZhuohaoHe Date: Tue, 6 Aug 2024 14:00:20 -0400 Subject: [PATCH 4/9] update 3rd.cmake --- cmake/3rd.cmake | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/cmake/3rd.cmake b/cmake/3rd.cmake index 2be4a15..3c5b464 100644 --- a/cmake/3rd.cmake +++ b/cmake/3rd.cmake @@ -87,17 +87,6 @@ if (sdl2-cmake-modules_ADDED) list(APPEND CMAKE_MODULE_PATH ${sdl2-cmake-modules_SOURCE_DIR}) endif () -## https://github.com/freetype/freetype -#CPMAddPackage( -# NAME freetype -# GIT_REPOSITORY https://github.com/freetype/freetype.git -# GIT_TAG VER-2-13-0 -# VERSION 2.13.0 -#) -#if (freetype_ADDED) -# add_library(Freetype::Freetype ALIAS freetype) -#endif () - # https://github.com/tinyobjloader/tinyobjloader.git CPMAddPackage( NAME tinyobjloader @@ -119,10 +108,6 @@ CPMAddPackage( GITHUB_REPOSITORY g-truc/glm GIT_TAG 1.0.1 ) -if (glm_ADDED) - add_library(glm INTERFACE IMPORTED) - target_include_directories(glm INTERFACE ${glm_SOURCE_DIR}) -endif () # https://github.com/nothings/stb.git CPMAddPackage( @@ -140,19 +125,6 @@ if (stb_ADDED) ) endif () -# https://gitlab.com/libeigen/eigen.git -CPMAddPackage( - NAME Eigen - GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git - GIT_TAG 3.4.0 - VERSION 3.4.0 - DOWNLOAD_ONLY True -) -if (Eigen_ADDED) - add_library(Eigen INTERFACE IMPORTED) - target_include_directories(Eigen INTERFACE ${Eigen_SOURCE_DIR}) -endif () - # http://wenq.org/wqy2/index.cgi?ZenHei CPMAddPackage( NAME wqy_font From ecdb5423ab7e3e91fd0bf14077daf40a648f022e Mon Sep 17 00:00:00 2001 From: ZhuohaoHe Date: Tue, 6 Aug 2024 16:10:28 -0400 Subject: [PATCH 5/9] update structure --- {src/include => include}/color.h | 0 include/config.h | 38 ++++++++++++++++++++++ {src/include => include}/default_shader.h | 0 {src/include => include}/light.h | 0 {src/include => include}/log_system.h | 0 {src/include => include}/matrix.hpp | 0 {src/include => include}/model.hpp | 0 {src/include => include}/shader_base.h | 0 {src/include => include}/simple_renderer.h | 0 {src/include => include}/vector.hpp | 0 10 files changed, 38 insertions(+) rename {src/include => include}/color.h (100%) create mode 100644 include/config.h rename {src/include => include}/default_shader.h (100%) rename {src/include => include}/light.h (100%) rename {src/include => include}/log_system.h (100%) rename {src/include => include}/matrix.hpp (100%) rename {src/include => include}/model.hpp (100%) rename {src/include => include}/shader_base.h (100%) rename {src/include => include}/simple_renderer.h (100%) rename {src/include => include}/vector.hpp (100%) diff --git a/src/include/color.h b/include/color.h similarity index 100% rename from src/include/color.h rename to include/color.h diff --git a/include/config.h b/include/config.h new file mode 100644 index 0000000..9427427 --- /dev/null +++ b/include/config.h @@ -0,0 +1,38 @@ + +/** + * @file config.h + * @brief 项目配置 + * @author Zone.N (Zone.Niuzh@hotmail.com) + * @version 1.0 + * @date 2023-08-24 + * @copyright MIT LICENSE + * https://github.com/Simple-XX/SimpleRenderer + * @par change log: + * + *
DateAuthorDescription + *
2023-08-24Zone.N创建文件 + *
+ */ + +#ifndef SIMPLERENDER_SRC_INCLUDE_CONFIG_H_ +#define SIMPLERENDER_SRC_INCLUDE_CONFIG_H_ + +#include +#include + +namespace simple_renderer { + +/// 线程数 +static constexpr const size_t kNProc = 8; + +/// 日志文件路径 +static const std::string kLogFilePath = + std::string("/Users/hezhohao/Programming/GitRepo/SimpleRenderer/build/logs/SimpleRendererLog.log"); +/// 日志文件大小 +static constexpr const size_t kLogFileMaxSize = 1024*1024*4; +/// 日志文件数量 +static constexpr const size_t kLogFileMaxCount = 8; + +} // namespace simple_renderer + +#endif /* SIMPLERENDER_SRC_INCLUDE_CONFIG_H_ */ diff --git a/src/include/default_shader.h b/include/default_shader.h similarity index 100% rename from src/include/default_shader.h rename to include/default_shader.h diff --git a/src/include/light.h b/include/light.h similarity index 100% rename from src/include/light.h rename to include/light.h diff --git a/src/include/log_system.h b/include/log_system.h similarity index 100% rename from src/include/log_system.h rename to include/log_system.h diff --git a/src/include/matrix.hpp b/include/matrix.hpp similarity index 100% rename from src/include/matrix.hpp rename to include/matrix.hpp diff --git a/src/include/model.hpp b/include/model.hpp similarity index 100% rename from src/include/model.hpp rename to include/model.hpp diff --git a/src/include/shader_base.h b/include/shader_base.h similarity index 100% rename from src/include/shader_base.h rename to include/shader_base.h diff --git a/src/include/simple_renderer.h b/include/simple_renderer.h similarity index 100% rename from src/include/simple_renderer.h rename to include/simple_renderer.h diff --git a/src/include/vector.hpp b/include/vector.hpp similarity index 100% rename from src/include/vector.hpp rename to include/vector.hpp From c877b4d03eff75814c9fb649bef9933127cdba24 Mon Sep 17 00:00:00 2001 From: ZhuohaoHe Date: Tue, 6 Aug 2024 20:24:04 -0400 Subject: [PATCH 6/9] update CMakeLists.txt --- .gitignore | 2 +- CMakeLists.txt | 2 +- include/config.h | 2 +- src/CMakeLists.txt | 26 ++++++++------------------ src/scene.cpp | 43 ------------------------------------------- test/CMakeLists.txt | 26 +++++++++++++------------- 6 files changed, 24 insertions(+), 77 deletions(-) delete mode 100644 src/scene.cpp diff --git a/.gitignore b/.gitignore index f646857..da25727 100755 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,4 @@ tools/opensbi/build .idea 3rd Doxyfile -src/include/config.h +include/config.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 54766fb..67725f4 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,7 +50,7 @@ set(LOG_FILE_MAX_COUNT 8) # 生成配置头文件 configure_file( "${PROJECT_SOURCE_DIR}/cmake/config.h.in" - "${PROJECT_SOURCE_DIR}/src/include/config.h" + "${PROJECT_SOURCE_DIR}/include/config.h" ) # 添加要编译的目录 diff --git a/include/config.h b/include/config.h index 9427427..5384ffa 100644 --- a/include/config.h +++ b/include/config.h @@ -27,7 +27,7 @@ static constexpr const size_t kNProc = 8; /// 日志文件路径 static const std::string kLogFilePath = - std::string("/Users/hezhohao/Programming/GitRepo/SimpleRenderer/build/logs/SimpleRendererLog.log"); + std::string("/Users/hezhohao/Programming/SimpleRenderer/build/logs/SimpleRendererLog.log"); /// 日志文件大小 static constexpr const size_t kLogFileMaxSize = 1024*1024*4; /// 日志文件数量 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2fbf564..70435ba 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,31 +5,21 @@ # CMakeLists.txt for Simple-XX/SimpleRenderer. # 生成静态库 -add_library(${PROJECT_NAME} STATIC - log_system.cpp - color.cpp - include/vector.hpp - include/matrix.hpp - include/model.hpp - include/shader_base.h - include/default_shader.h - include/light.h - model.cpp - light.cpp - shader_base.cpp - default_shader.cpp - simple_renderer.cpp +file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS + "*.cpp" + "*.c" ) +add_library(${PROJECT_NAME} STATIC ${SRC_FILES}) target_include_directories(${PROJECT_NAME} PRIVATE - $ - $ + $ + $ ) target_compile_options(${PROJECT_NAME} PRIVATE - ${DEFAULT_COMPILE_OPTIONS} + ${DEFAULT_COMPILE_OPTIONS} ) target_link_libraries(${PROJECT_NAME} PRIVATE - ${DEFAULT_LINK_LIB} + ${DEFAULT_LINK_LIB} ) diff --git a/src/scene.cpp b/src/scene.cpp deleted file mode 100644 index 49c48e9..0000000 --- a/src/scene.cpp +++ /dev/null @@ -1,43 +0,0 @@ - -/** - * @file scene.cpp - * @brief 场景抽象 - * @author Zone.N (Zone.Niuzh@hotmail.com) - * @version 1.0 - * @date 2022-12-15 - * @copyright MIT LICENSE - * https://github.com/Simple-XX/SimpleRenderer - * @par change log: - * - *
DateAuthorDescription - *
2022-12-15Zone.N创建文件 - *
- */ - -#include "scene.h" -#include "log_system.h" -#include "matrix.hpp" - -namespace simple_renderer { - -scene_t::scene_t(const std::string &_name, uint64_t _x, uint64_t _y, - uint64_t _z) - : name(_name), x(_x), y(_y), z(_z) {} - -void scene_t::add_model(const model_t &_model, const vector3f_t _pos) { - models.push_back(std::pair(_model, _pos)); - SPDLOG_LOGGER_INFO(SRLOG, "add_model: {} to: {}, total: {}", _model.obj_path, - name, models.size()); -} - -void scene_t::add_light(const light_t &_light) { - lights.push_back(_light); - SPDLOG_LOGGER_INFO(SRLOG, "add_light: {} to: {}", _light.name, name); -} - -void scene_t::add_camera(const SimpleRenderer::camera_t &_camera) { - camera = _camera; - SPDLOG_LOGGER_INFO(SRLOG, "add_camera: {} to: {}", _camera.name, name); -} - -} // namespace SimpleRenderer diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 61bc9ed..4a4b591 100755 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,28 +14,28 @@ enable_testing() include(GoogleTest) include_directories( - ${SimpleRenderer_SOURCE_DIR}/src/include + ${SimpleRenderer_SOURCE_DIR}/include ) list(APPEND DEFAULT_TEST_COMPILE_OPTIONS - ${DEFAULT_COMPILE_OPTIONS} - --coverage + ${DEFAULT_COMPILE_OPTIONS} + --coverage ) list(APPEND DEFAULT_TEST_LINK_OPTIONS - --coverage - $<$: - # -fsanitize=leak - > - # -fsanitize=address - -fno-omit-frame-pointer + --coverage + $<$: + # -fsanitize=leak + > + # -fsanitize=address + -fno-omit-frame-pointer ) link_libraries( - ${DEFAULT_LINK_LIB} - gtest_main - ${glog_LIBRARIES} - SimpleRenderer + ${DEFAULT_LINK_LIB} + gtest_main + ${glog_LIBRARIES} + SimpleRenderer ) # Check if the platform is macOS From 890c15cbf0c87d0a8f977241a157ee65fd0fc28e Mon Sep 17 00:00:00 2001 From: ZhuohaoHe Date: Fri, 9 Aug 2024 15:39:45 -0400 Subject: [PATCH 7/9] update according to the reviews Signed-off-by: ZhuohaoHe --- .../logs/SimpleRendererLog.log | 8 ++ .gitignore | 2 +- CMakeLists.txt | 6 +- CMakePresets.json | 60 ++++++--- cmake/3rd.cmake | 45 +++---- include/config.h | 38 ------ include/vector.hpp | 57 -------- src/CMakeLists.txt | 2 +- src/default_shader.cpp | 5 +- {include => src/include}/color.h | 0 {include => src/include}/default_shader.h | 2 +- {include => src/include}/light.h | 8 +- {include => src/include}/log_system.h | 0 {include => src/include}/matrix.hpp | 14 +- {include => src/include}/model.hpp | 16 +-- {include => src/include}/shader_base.h | 20 +-- {include => src/include}/simple_renderer.h | 8 +- src/include/vector.hpp | 61 +++++++++ src/light.cpp | 4 +- src/log_system.cpp | 6 +- src/model.cpp | 125 +++++++++--------- src/shader_base.cpp | 12 +- src/simple_renderer.cpp | 20 +-- test/CMakeLists.txt | 28 ++-- test/system_test/camera.h | 4 +- test/system_test/main.cpp | 22 +-- 26 files changed, 276 insertions(+), 297 deletions(-) create mode 100644 $EXECUTABLE_OUTPUT_PATH/logs/SimpleRendererLog.log delete mode 100644 include/config.h delete mode 100755 include/vector.hpp rename {include => src/include}/color.h (100%) rename {include => src/include}/default_shader.h (96%) rename {include => src/include}/light.h (90%) rename {include => src/include}/log_system.h (100%) rename {include => src/include}/matrix.hpp (69%) rename {include => src/include}/model.hpp (93%) rename {include => src/include}/shader_base.h (92%) rename {include => src/include}/simple_renderer.h (93%) create mode 100755 src/include/vector.hpp diff --git a/$EXECUTABLE_OUTPUT_PATH/logs/SimpleRendererLog.log b/$EXECUTABLE_OUTPUT_PATH/logs/SimpleRendererLog.log new file mode 100644 index 0000000..0d5deb8 --- /dev/null +++ b/$EXECUTABLE_OUTPUT_PATH/logs/SimpleRendererLog.log @@ -0,0 +1,8 @@ +[2024-08-09 15:04:13.936] [simple_renderer.cpp:57 SimpleRenderer] [info] SimpleRenderer init with 1920, 1080 +[2024-08-09 15:04:13.949] [model.cpp:115 Model] [info] 加载模型: obj/utah-teapot/utah-teapot.obj, 顶点数: 2082, 法线数: 2082, 颜色数: 2082, UV数: 1036, 子模型数: 1, 材质数: 1 +[2024-08-09 15:04:13.960] [simple_renderer.cpp:61 render] [info] render model: obj/utah-teapot/utah-teapot.obj +[2024-08-09 15:04:13.961] [simple_renderer.cpp:197 DrawModel] [info] draw obj/utah-teapot/utah-teapot.obj +[2024-08-09 15:37:12.808] [simple_renderer.cpp:57 SimpleRenderer] [info] SimpleRenderer init with 1920, 1080 +[2024-08-09 15:37:12.821] [model.cpp:118 Model] [info] 加载模型: obj/utah-teapot/utah-teapot.obj, 顶点数: 2082, 法线数: 2082, 颜色数: 2082, UV数: 1036, 子模型数: 1, 材质数: 1 +[2024-08-09 15:37:12.833] [simple_renderer.cpp:61 render] [info] render model: obj/utah-teapot/utah-teapot.obj +[2024-08-09 15:37:12.835] [simple_renderer.cpp:197 DrawModel] [info] draw obj/utah-teapot/utah-teapot.obj diff --git a/.gitignore b/.gitignore index da25727..f646857 100755 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,4 @@ tools/opensbi/build .idea 3rd Doxyfile -include/config.h +src/include/config.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 67725f4..253b3dc 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,8 +7,6 @@ # 设置最小 cmake 版本 cmake_minimum_required(VERSION 3.27 FATAL_ERROR) -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_STANDARD_REQUIRED True) # 设置项目名与版本 project(SimpleRenderer @@ -42,7 +40,7 @@ set(FONT_FILE_PATH "${wqy_font_SOURCE_DIR}/wqy-zenhei.ttc") include(ProcessorCount) ProcessorCount(NPROC) # 日志文件路径 -set(LOG_FILE_PATH "${PROJECT_SOURCE_DIR}/build/logs/SimpleRendererLog.log") +set(LOG_FILE_PATH "$EXECUTABLE_OUTPUT_PATH/logs/SimpleRendererLog.log") # 日志文件大小 set(LOG_FILE_MAX_SIZE 1024*1024*4) # 日志文件数量 @@ -50,7 +48,7 @@ set(LOG_FILE_MAX_COUNT 8) # 生成配置头文件 configure_file( "${PROJECT_SOURCE_DIR}/cmake/config.h.in" - "${PROJECT_SOURCE_DIR}/include/config.h" + "${PROJECT_SOURCE_DIR}/src/include/config.h" ) # 添加要编译的目录 diff --git a/CMakePresets.json b/CMakePresets.json index cdab07d..744f911 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -2,20 +2,10 @@ "version": 6, "cmakeMinimumRequired": { "major": 3, - "minor": 27, + "minor": 23, "patch": 0 }, "configurePresets": [ - { - "name": "host", - "description": "Linux Only", - "hidden": true, - "condition": { - "type": "equals", - "lhs": "${hostSystemName}", - "rhs": "Linux" - } - }, { "name": "std", "description": "This preset makes sure the project actually builds with at least the specified standard", @@ -30,13 +20,10 @@ } }, { - "name": "configurePresets_base", + "name": "config-base", "hidden": true, - "inherits": [ - "host", - "std" - ], - "displayName": "configurePresets_base", + "inherits": [ "std" ], + "displayName": "config-base", "description": "base configurePresets", "binaryDir": "${sourceDir}/build", "cacheVariables": { @@ -58,14 +45,47 @@ } } }, + { + "name": "config-mac", + "hidden": true, + "inherits": [ "config-base" ], + "displayName": "config-base", + "description": "macOS configurePresets", + "condition": { + "type": "equals", + "lhs": "${hostSystemName}", + "rhs": "Darwin" + }, + "cacheVariables": { + "CMAKE_MACOSX_RPATH": "1", + "CMAKE_INSTALL_RPATH": "/Library/Frameworks", + "CMAKE_BUILD_WITH_INSTALL_RPATH": "TRUE" + } + }, { "name": "build", "hidden": false, "inherits": [ - "configurePresets_base" + "config-base" + ], + "displayName": "build-base", + "description": "build base configurePresets" + }, + { + "name": "build-mac", + "hidden": false, + "inherits": [ + "config-mac" ], - "displayName": "build", - "description": "build" + "displayName": "build-mac", + "description": "macOS build configurePresets" + } + ], + "buildPresets": [ + { + "name": "build", + "displayName": "Build", + "configurePreset": "config-base" } ] } \ No newline at end of file diff --git a/cmake/3rd.cmake b/cmake/3rd.cmake index 3c5b464..d22cd05 100644 --- a/cmake/3rd.cmake +++ b/cmake/3rd.cmake @@ -57,8 +57,7 @@ CPMAddPackage( "gtest_force_shared_crt ON" ) -# SDL2 - +# https://github.com/libsdl-org/SDL CPMAddPackage( NAME SDL2 GITHUB_REPOSITORY libsdl-org/SDL @@ -70,22 +69,17 @@ CPMAddPackage( "SDL_STATIC_PIC ON" "SDL_WERROR OFF" ) -find_package(SDL2 REQUIRED) - -# https://github.com/aminosbh/sdl2-cmake-modules.git -CPMAddPackage( - NAME sdl2-cmake-modules - GIT_REPOSITORY https://github.com/aminosbh/sdl2-cmake-modules.git - GIT_TAG ad006a3daae65a612ed87415037e32188b81071e - DOWNLOAD_ONLY True -) -if (SDL2_ADDED) - add_library(SDL2::SDL2) -endif() -if (sdl2-cmake-modules_ADDED) - list(APPEND CMAKE_MODULE_PATH ${sdl2-cmake-modules_SOURCE_DIR}) -endif () +# # https://github.com/aminosbh/sdl2-cmake-modules.git +# CPMAddPackage( +# NAME sdl2-cmake-modules +# GIT_REPOSITORY https://github.com/aminosbh/sdl2-cmake-modules.git +# GIT_TAG ad006a3daae65a612ed87415037e32188b81071e +# DOWNLOAD_ONLY True +# ) +# if (sdl2-cmake-modules_ADDED) +# list(APPEND CMAKE_MODULE_PATH ${sdl2-cmake-modules_SOURCE_DIR}) +# endif () # https://github.com/tinyobjloader/tinyobjloader.git CPMAddPackage( @@ -103,11 +97,16 @@ if (tinyobjloader_ADDED) ) endif () +# https://github.com/g-truc/glm CPMAddPackage( NAME glm GITHUB_REPOSITORY g-truc/glm GIT_TAG 1.0.1 ) +if (glm_ADDED) + add_library(glm INTERFACE) + target_include_directories(glm INTERFACE ${glm_SOURCE_DIR}) +endif () # https://github.com/nothings/stb.git CPMAddPackage( @@ -236,12 +235,6 @@ if (NOT LCOV_EXE) "Following https://github.com/linux-test-project/lcov to install.") endif () -find_package(SDL2 REQUIRED) -if (NOT SDL2_FOUND) - message(FATAL_ERROR "sdl2 not found.\n" - "Following https://github.com/libsdl-org/SDL to install.") -endif () - find_package(OpenMP REQUIRED) if (NOT OpenMP_FOUND) message(FATAL_ERROR "OpenMP not found.\n" @@ -252,10 +245,4 @@ find_package(spdlog REQUIRED) if (NOT spdlog_FOUND) message(FATAL_ERROR "spdlog not found.\n" "Following https://github.com/gabime/spdlog to install.") -endif () - -find_package(glm REQUIRED) -if (NOT glm_FOUND) - message(FATAL_ERROR "glm not found.\n" - "Following https://github.com/g-truc/glm tp install") endif () \ No newline at end of file diff --git a/include/config.h b/include/config.h deleted file mode 100644 index 5384ffa..0000000 --- a/include/config.h +++ /dev/null @@ -1,38 +0,0 @@ - -/** - * @file config.h - * @brief 项目配置 - * @author Zone.N (Zone.Niuzh@hotmail.com) - * @version 1.0 - * @date 2023-08-24 - * @copyright MIT LICENSE - * https://github.com/Simple-XX/SimpleRenderer - * @par change log: - * - *
DateAuthorDescription - *
2023-08-24Zone.N创建文件 - *
- */ - -#ifndef SIMPLERENDER_SRC_INCLUDE_CONFIG_H_ -#define SIMPLERENDER_SRC_INCLUDE_CONFIG_H_ - -#include -#include - -namespace simple_renderer { - -/// 线程数 -static constexpr const size_t kNProc = 8; - -/// 日志文件路径 -static const std::string kLogFilePath = - std::string("/Users/hezhohao/Programming/SimpleRenderer/build/logs/SimpleRendererLog.log"); -/// 日志文件大小 -static constexpr const size_t kLogFileMaxSize = 1024*1024*4; -/// 日志文件数量 -static constexpr const size_t kLogFileMaxCount = 8; - -} // namespace simple_renderer - -#endif /* SIMPLERENDER_SRC_INCLUDE_CONFIG_H_ */ diff --git a/include/vector.hpp b/include/vector.hpp deleted file mode 100755 index 4b8f96a..0000000 --- a/include/vector.hpp +++ /dev/null @@ -1,57 +0,0 @@ - -/** - * @file vector.hpp - * @brief 向量模版 - * @author Zone.N (Zone.Niuzh@hotmail.com) - * @version 1.0 - * @date 2022-06-07 - * @copyright MIT LICENSE - * https://github.com/Simple-XX/SimpleRenderer - * @par change log: - * - *
DateAuthorDescription - *
2022-06-07Zone.N迁移到 doxygen - *
- */ - -#ifndef SIMPLERENDER_SRC_INCLUDE_VECTOR_HPP_ -#define SIMPLERENDER_SRC_INCLUDE_VECTOR_HPP_ - -#include - -#define GLM_ENABLE_EXPERIMENTAL -#include - -#include "log_system.h" - -namespace simple_renderer { - -} // namespace simple_renderer - -/** - * spdlog 输出 Vector3f 实现 - */ -template <> -struct fmt::formatter : fmt::formatter { - auto format(const glm::vec3 &vector, fmt::format_context &ctx) const -> decltype(ctx.out()) { - std::string vector_string = glm::to_string(vector); - - // Format and output the string - return fmt::format_to(ctx.out(), "\n{}", vector_string); - } -}; - -/** - * spdlog 输出 Vector4f 实现 - */ -template <> -struct fmt::formatter : fmt::formatter { - auto format(const glm::vec4 &vector, fmt::format_context &ctx) const -> decltype(ctx.out()) { - std::string vector_string = glm::to_string(vector); - - // Format and output the string - return fmt::format_to(ctx.out(), "\n{}", vector_string); - } -}; - -#endif /* SIMPLERENDER_SRC_INCLUDE_VECTOR_HPP_ */ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 70435ba..d037097 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,7 +12,7 @@ file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS add_library(${PROJECT_NAME} STATIC ${SRC_FILES}) target_include_directories(${PROJECT_NAME} PRIVATE - $ + $ $ ) diff --git a/src/default_shader.cpp b/src/default_shader.cpp index 82b1732..91a8f3d 100644 --- a/src/default_shader.cpp +++ b/src/default_shader.cpp @@ -20,7 +20,7 @@ namespace simple_renderer { auto DefaultShader::InterpolateColor( const Color &color0, const Color &color1, const Color &color2, - const glm::vec3 &barycentric_coord) -> Color { + const Vector3f &barycentric_coord) -> Color { return Color( static_cast(static_cast(color0[Color::kColorIndexRed]) * barycentric_coord.x + @@ -56,7 +56,8 @@ auto DefaultShader::Vertex(const ShaderVertexIn &shader_vertex_in) const auto DefaultShader::Fragment(const ShaderFragmentIn &shader_fragment_in) const -> ShaderFragmentOut { - auto intensity = glm::dot(shader_fragment_in.normal_,shader_fragment_in.light_); + auto intensity = + glm::dot(shader_fragment_in.normal_, shader_fragment_in.light_); auto is_need_draw = true; // 光照方向为正,不绘制背面 if (intensity <= 0) { diff --git a/include/color.h b/src/include/color.h similarity index 100% rename from include/color.h rename to src/include/color.h diff --git a/include/default_shader.h b/src/include/default_shader.h similarity index 96% rename from include/default_shader.h rename to src/include/default_shader.h index a187577..717c802 100644 --- a/include/default_shader.h +++ b/src/include/default_shader.h @@ -64,7 +64,7 @@ class DefaultShader : public ShaderBase { */ static auto InterpolateColor(const Color &color0, const Color &color1, const Color &color2, - const glm::vec3 &barycentric_coord) -> Color; + const Vector3f &barycentric_coord) -> Color; }; } // namespace simple_renderer diff --git a/include/light.h b/src/include/light.h similarity index 90% rename from include/light.h rename to src/include/light.h index c5307d9..f566308 100644 --- a/include/light.h +++ b/src/include/light.h @@ -33,9 +33,9 @@ class Light { /// 光照名称 std::string name_ = "default light name"; /// 位置 - glm::vec3 pos = kDefaultPos; + Vector3f pos = kDefaultPos; /// 方向 - glm::vec3 dir = kDefaultDir; + Vector3f dir = kDefaultDir; /// 颜色 Color color = kDefaultColor; @@ -57,9 +57,9 @@ class Light { private: /// 默认位置 - static const glm::vec3 kDefaultPos; + static const Vector3f kDefaultPos; /// 默认方向,左手系,x 向右,y 向下,z 正方向为屏幕由内向外 - static const glm::vec3 kDefaultDir; + static const Vector3f kDefaultDir; /// 默认颜色 static const Color kDefaultColor; }; diff --git a/include/log_system.h b/src/include/log_system.h similarity index 100% rename from include/log_system.h rename to src/include/log_system.h diff --git a/include/matrix.hpp b/src/include/matrix.hpp similarity index 69% rename from include/matrix.hpp rename to src/include/matrix.hpp index 29b7f67..c9de6a1 100755 --- a/include/matrix.hpp +++ b/src/include/matrix.hpp @@ -23,20 +23,22 @@ #include "log_system.h" namespace simple_renderer { - - +using Matrix4f = glm::mat4; } // namespace simple_renderer /** * spdlog 输出矩阵实现 */ template <> -struct fmt::formatter : fmt::formatter { - auto format(const glm::mat4 &matrix, fmt::format_context &ctx) const -> decltype(ctx.out()) { - // Convert the glm::mat4 to a string using glm::to_string +struct fmt::formatter : fmt::formatter { + auto format(const simple_renderer::Matrix4f &matrix, + fmt::format_context &ctx) const -> decltype(ctx.out()) { + // Convert the Matrix4f to a string using glm::to_string + // 转化矩阵为字符串 std::string matrix_str = glm::to_string(matrix); - + // Format and output the string + // 输出格式化后的字符串 return fmt::format_to(ctx.out(), "\n{}", matrix_str); } }; diff --git a/include/model.hpp b/src/include/model.hpp similarity index 93% rename from include/model.hpp rename to src/include/model.hpp index 0e9e877..fd782cd 100755 --- a/include/model.hpp +++ b/src/include/model.hpp @@ -36,9 +36,9 @@ namespace simple_renderer { class Model { public: /// 顶点坐标 - using Coord = glm::vec3; + using Coord = Vector3f; /// 法向量 - using Normal = glm::vec3; + using Normal = Vector3f; /// 贴图 using TextureCoord = glm::vec2; @@ -47,11 +47,11 @@ class Model { /// 反光度 float shininess = 0; /// 环境光照 - glm::vec3 ambient; + Vector3f ambient; /// 漫反射光照 - glm::vec3 diffuse; + Vector3f diffuse; /// 镜面光照 - glm::vec3 specular; + Vector3f specular; /// @name 默认构造/析构函数 /// @{ @@ -108,7 +108,7 @@ class Model { * @param tran 要对顶点进行的变换矩阵 * @return 结果 */ - [[nodiscard]] auto operator*(const glm::mat4 &tran) const -> Vertex; + [[nodiscard]] auto operator*(const Matrix4f &tran) const -> Vertex; }; /// @todo 直接保存太浪费内存了 @@ -148,7 +148,7 @@ class Model { * @param tran 要对面进行的变换矩阵 * @return 结果 */ - [[nodiscard]] auto operator*(const glm::mat4 &tran) const -> Face; + [[nodiscard]] auto operator*(const Matrix4f &tran) const -> Face; }; /// obj 文件路径 @@ -179,7 +179,7 @@ class Model { * @param tran 要对模型进行的变换矩阵 * @return 结果 */ - [[nodiscard]] auto operator*(const glm::mat4 &tran) const -> Model; + [[nodiscard]] auto operator*(const Matrix4f &tran) const -> Model; /** * 获取面 diff --git a/include/shader_base.h b/src/include/shader_base.h similarity index 92% rename from include/shader_base.h rename to src/include/shader_base.h index a97e429..b846eef 100644 --- a/include/shader_base.h +++ b/src/include/shader_base.h @@ -83,11 +83,11 @@ class ShaderVertexOut { class ShaderFragmentIn { public: /// 重心坐标 - glm::vec3 barycentric_coord_; + Vector3f barycentric_coord_; /// 法线方向 - glm::vec3 normal_; + Vector3f normal_; /// 光照方向 - glm::vec3 light_; + Vector3f light_; /// @name 三个顶点的颜色 /// @{ @@ -105,8 +105,8 @@ class ShaderFragmentIn { * @param color1 顶点 1 颜色 * @param color2 顶点 2 颜色 */ - explicit ShaderFragmentIn(const glm::vec3 &_barycentric_coord, - const glm::vec3 &_normal, const glm::vec3 &_light, + explicit ShaderFragmentIn(const Vector3f &_barycentric_coord, + const Vector3f &_normal, const Vector3f &_light, const Color &_color0, const Color &_color1, const Color &_color2); @@ -160,11 +160,11 @@ class ShaderFragmentOut { class ShaderData { public: /// 模型变换矩阵 - glm::mat4 model_matrix_ = glm::mat4(1.0f); + Matrix4f model_matrix_ = Matrix4f(1.0f); /// 视图变换矩阵 - glm::mat4 view_matrix_ = glm::mat4(1.0f); + Matrix4f view_matrix_ = Matrix4f(1.0f); /// 正交变换矩阵 - glm::mat4 project_matrix_ = glm::mat4(1.0f); + Matrix4f project_matrix_ = Matrix4f(1.0f); /** * 构造函数 @@ -172,8 +172,8 @@ class ShaderData { * @param view_matrix 视图变换矩阵 * @param project_matrix 正交变换矩阵 */ - explicit ShaderData(const glm::mat4 &model_matrix, const glm::mat4 &view_matrix, - const glm::mat4 &project_matrix); + explicit ShaderData(const Matrix4f &model_matrix, const Matrix4f &view_matrix, + const Matrix4f &project_matrix); /// @name 默认构造/析构函数 /// @{ diff --git a/include/simple_renderer.h b/src/include/simple_renderer.h similarity index 93% rename from include/simple_renderer.h rename to src/include/simple_renderer.h index 05f7a44..9aa587f 100755 --- a/include/simple_renderer.h +++ b/src/include/simple_renderer.h @@ -128,9 +128,9 @@ class SimpleRenderer { * weight_B = s * weight_C = t */ - static auto GetBarycentricCoord(const glm::vec3 &p0, const glm::vec3 &p1, - const glm::vec3 &p2, const glm::vec3 &pa) - -> std::pair; + static auto GetBarycentricCoord(const Vector3f &p0, const Vector3f &p1, + const Vector3f &p2, const Vector3f &pa) + -> std::pair; /** * 深度插值,由重心坐标计算出对应点的深度值 @@ -141,7 +141,7 @@ class SimpleRenderer { * @return 深度值 */ static auto InterpolateDepth(float depth0, float depth1, float depth2, - const glm::vec3 &barycentric_coord) -> float; + const Vector3f &barycentric_coord) -> float; }; } // namespace simple_renderer diff --git a/src/include/vector.hpp b/src/include/vector.hpp new file mode 100755 index 0000000..b0045f2 --- /dev/null +++ b/src/include/vector.hpp @@ -0,0 +1,61 @@ + +/** + * @file vector.hpp + * @brief 向量模版 + * @author Zone.N (Zone.Niuzh@hotmail.com) + * @version 1.0 + * @date 2022-06-07 + * @copyright MIT LICENSE + * https://github.com/Simple-XX/SimpleRenderer + * @par change log: + * + *
DateAuthorDescription + *
2022-06-07Zone.N迁移到 doxygen + *
+ */ + +#ifndef SIMPLERENDER_SRC_INCLUDE_VECTOR_HPP_ +#define SIMPLERENDER_SRC_INCLUDE_VECTOR_HPP_ + +#include + +#define GLM_ENABLE_EXPERIMENTAL +#include + +#include "log_system.h" + +namespace simple_renderer { +using Vector2f = glm::vec2; +using Vector3f = glm::vec3; +using Vector4f = glm::vec4; +} // namespace simple_renderer + +/** + * spdlog 输出 Vector3f 实现 + */ +template <> +struct fmt::formatter : fmt::formatter { + auto format(const simple_renderer::Vector3f &vector, + fmt::format_context &ctx) const -> decltype(ctx.out()) { + std::string vector_string = glm::to_string(vector); + + // Format and output the string + return fmt::format_to(ctx.out(), "\n{}", vector_string); + } +}; + +/** + * spdlog 输出 Vector4f 实现 + */ +template <> +struct fmt::formatter : fmt::formatter { + auto format(const simple_renderer::Vector4f &vector, + fmt::format_context &ctx) const -> decltype(ctx.out()) { + std::string vector_string = glm::to_string(vector); + + // Format and output the string + return fmt::format_to(ctx.out(), "\n{}", vector_string); + } +}; + +#endif /* SIMPLERENDER_SRC_INCLUDE_VECTOR_HPP_ */ diff --git a/src/light.cpp b/src/light.cpp index 61503d0..21c0254 100644 --- a/src/light.cpp +++ b/src/light.cpp @@ -22,8 +22,8 @@ namespace simple_renderer { -const glm::vec3 Light::kDefaultPos = glm::vec3(0, 0, 0); -const glm::vec3 Light::kDefaultDir = glm::vec3(0, 0, -1); +const Vector3f Light::kDefaultPos = Vector3f(0, 0, 0); +const Vector3f Light::kDefaultDir = Vector3f(0, 0, -1); const Color Light::kDefaultColor = Color::kWhite; Light::Light(const std::string &name) : name_(name) { diff --git a/src/log_system.cpp b/src/log_system.cpp index 6c81b14..e6e7218 100755 --- a/src/log_system.cpp +++ b/src/log_system.cpp @@ -16,20 +16,20 @@ #include "log_system.h" -#include - #include #include #include #include #include +#include + namespace simple_renderer { LogSystem::LogSystem(const std::string &log_file_path, size_t lig_file_max_size, size_t log_file_max_count) { spdlog::init_thread_pool(65536, 1); -// std::string log_file_paths = "./logs/simple_renderer.log"; + // std::string log_file_paths = "./logs/simple_renderer.log"; auto stdout_sink = std::make_shared(); auto rotating_sink = std::make_shared( log_file_path, lig_file_max_size, log_file_max_count); diff --git a/src/model.cpp b/src/model.cpp index 19f7423..fd2c857 100755 --- a/src/model.cpp +++ b/src/model.cpp @@ -15,8 +15,8 @@ */ #include "model.hpp" -#include +#include #include #define TINYOBJLOADER_IMPLEMENTATION @@ -33,21 +33,24 @@ Model::Vertex::Vertex(Coord coord, Normal normal, TextureCoord texture_coord, texture_coord_(std::move(texture_coord)), color_(color) {} -auto Model::Vertex::operator*(const glm::mat4 &tran) const -> Model::Vertex { - Vertex vertex(*this); +auto Model::Vertex::operator*(const Matrix4f &tran) const -> Model::Vertex { + Vertex vertex(*this); - // Convert the 3D coordinate to a 4D vector by adding a 1.0 w-component - glm::vec4 res4(coord_, 1.0f); + // Convert the 3D coordinate to a 4D vector by adding a 1.0 w-component + // 将 3D 坐标转换为 4D 向量,通过添加 1.0 w-分量 + Vector4f res4(coord_, 1.0f); - // Apply the transformation matrix - glm::vec4 ret4 = tran * res4; + // Apply the transformation matrix + // 应用变换矩阵 + Vector4f ret4 = tran * res4; - // Update the vertex coordinates with the transformed values - vertex.coord_ = glm::vec3(ret4); + // Update the vertex coordinates with the transformed values + // 使用变换后的值更新顶点坐标 + vertex.coord_ = Vector3f(ret4); - /// @todo 变换法线 + /// @todo 变换法线 - return vertex; + return vertex; } Model::Face::Face(const Model::Vertex &v0, const Model::Vertex &v1, @@ -55,8 +58,9 @@ Model::Face::Face(const Model::Vertex &v0, const Model::Vertex &v1, : v0_(v0), v1_(v1), v2_(v2), material_(std::move(material)) { // 计算法向量 // 如果 obj 内包含法向量,直接使用即可 - if (glm::normalize(v0.normal_) != glm::vec3(0.0f) && glm::normalize(v1.normal_) != glm::vec3(0.0f) && - glm::normalize(v2.normal_) != glm::vec3(0.0f)) { + if (glm::normalize(v0.normal_) != Vector3f(0.0f) && + glm::normalize(v1.normal_) != Vector3f(0.0f) && + glm::normalize(v2.normal_) != Vector3f(0.0f)) { normal_ = glm::normalize((v0.normal_ + v1.normal_ + v2.normal_)); } // 手动计算 @@ -68,18 +72,18 @@ Model::Face::Face(const Model::Vertex &v0, const Model::Vertex &v1, } } -auto Model::Face::operator*(const glm::mat4 &tran) const -> Model::Face { - auto face(*this); - face.v0_ = face.v0_ * tran; - face.v1_ = face.v1_ * tran; - face.v2_ = face.v2_ * tran; +auto Model::Face::operator*(const Matrix4f &tran) const -> Model::Face { + auto face(*this); + face.v0_ = face.v0_ * tran; + face.v1_ = face.v1_ * tran; + face.v2_ = face.v2_ * tran; - // Calculate the transformed normal - glm::vec3 v2v0 = face.v2_.coord_ - face.v0_.coord_; - glm::vec3 v1v0 = face.v1_.coord_ - face.v0_.coord_; - face.normal_ = glm::normalize(glm::cross(v2v0, v1v0)); + // Calculate the transformed normal + Vector3f v2v0 = face.v2_.coord_ - face.v0_.coord_; + Vector3f v1v0 = face.v1_.coord_ - face.v0_.coord_; + face.normal_ = glm::normalize(glm::cross(v2v0, v1v0)); - return face; + return face; } Model::Model(const std::string &obj_path, const std::string &mtl_path) @@ -146,7 +150,7 @@ Model::Model(const std::string &obj_path, const std::string &mtl_path) // 如果法线索引存在(即 idx.normal_index >= 0), // 则构造并保存,否则设置为 0 - Normal normal = glm::vec3(0.0f); + Normal normal = Vector3f(0.0f); if (idx.normal_index >= 0) { normal = Normal(attrib.normals[3 * size_t(idx.normal_index) + 0], attrib.normals[3 * size_t(idx.normal_index) + 1], @@ -155,7 +159,7 @@ Model::Model(const std::string &obj_path, const std::string &mtl_path) // 如果贴图索引存在(即 idx.texcoord_index >= 0), // 则构造并保存,否则设置为 0 - TextureCoord texture_coord = glm::vec3(0.0f); + TextureCoord texture_coord = Vector3f(0.0f); if (idx.texcoord_index >= 0) { texture_coord = TextureCoord( attrib.texcoords[2 * size_t(idx.texcoord_index) + 0], @@ -175,13 +179,13 @@ Model::Model(const std::string &obj_path, const std::string &mtl_path) auto material = Material(); if (!materials.empty()) { material.shininess = materials[shape_index].shininess; - material.ambient = glm::vec3(materials[shape_index].ambient[0], + material.ambient = Vector3f(materials[shape_index].ambient[0], materials[shape_index].ambient[1], materials[shape_index].ambient[2]); - material.diffuse = glm::vec3(materials[shape_index].diffuse[0], + material.diffuse = Vector3f(materials[shape_index].diffuse[0], materials[shape_index].diffuse[1], materials[shape_index].diffuse[2]); - material.specular = glm::vec3(materials[shape_index].specular[0], + material.specular = Vector3f(materials[shape_index].specular[0], materials[shape_index].specular[1], materials[shape_index].specular[2]); } @@ -193,7 +197,7 @@ Model::Model(const std::string &obj_path, const std::string &mtl_path) Normalize(); } -auto Model::operator*(const glm::mat4 &tran) const -> Model { +auto Model::operator*(const Matrix4f &tran) const -> Model { auto model = Model(*this); for (auto &i : model.faces_) { @@ -216,23 +220,23 @@ std::pair Model::GetMaxMinXYX() { std::numeric_limits::max()); for (const auto &i : faces_) { - auto curr_max_x = std::max(i.v0_.coord_.x, - std::max(i.v1_.coord_.x, i.v2_.coord_.x)); - auto curr_max_y = std::max(i.v0_.coord_.y, - std::max(i.v1_.coord_.y, i.v2_.coord_.y)); - auto curr_max_z = std::max(i.v0_.coord_.z, - std::max(i.v1_.coord_.z, i.v2_.coord_.z)); + auto curr_max_x = + std::max(i.v0_.coord_.x, std::max(i.v1_.coord_.x, i.v2_.coord_.x)); + auto curr_max_y = + std::max(i.v0_.coord_.y, std::max(i.v1_.coord_.y, i.v2_.coord_.y)); + auto curr_max_z = + std::max(i.v0_.coord_.z, std::max(i.v1_.coord_.z, i.v2_.coord_.z)); max.x = curr_max_x > max.x ? curr_max_x : max.x; max.y = curr_max_y > max.y ? curr_max_y : max.y; max.z = curr_max_z > max.z ? curr_max_z : max.z; - auto curr_min_x = std::min(i.v0_.coord_.x, - std::min(i.v1_.coord_.x, i.v2_.coord_.x)); - auto curr_min_y = std::min(i.v0_.coord_.y, - std::min(i.v1_.coord_.y, i.v2_.coord_.y)); - auto curr_min_z = std::min(i.v0_.coord_.z, - std::min(i.v1_.coord_.z, i.v2_.coord_.z)); + auto curr_min_x = + std::min(i.v0_.coord_.x, std::min(i.v1_.coord_.x, i.v2_.coord_.x)); + auto curr_min_y = + std::min(i.v0_.coord_.y, std::min(i.v1_.coord_.y, i.v2_.coord_.y)); + auto curr_min_z = + std::min(i.v0_.coord_.z, std::min(i.v1_.coord_.z, i.v2_.coord_.z)); min.x = curr_min_x < min.x ? curr_min_x : min.x; min.y = curr_min_y < min.y ? curr_min_y : min.y; @@ -244,31 +248,34 @@ std::pair Model::GetMaxMinXYX() { void Model::Normalize() { auto [max, min] = GetMaxMinXYX(); - // Compute the dimensions of the bounding box - auto x = std::abs(max.x) + std::abs(min.x); - auto y = std::abs(max.y) + std::abs(min.y); - auto z = std::abs(max.z) + std::abs(min.z); + // Compute the dimensions of the bounding box + auto x = std::abs(max.x) + std::abs(min.x); + auto y = std::abs(max.y) + std::abs(min.y); + auto z = std::abs(max.z) + std::abs(min.z); - // Calculate the scaling factor - auto scale = 1.0f / std::max(x, std::max(y, z)); + // Calculate the scaling factor + auto scale = 1.0f / std::max(x, std::max(y, z)); - // Create the scaling matrix - glm::mat4 scale_matrix = glm::scale(glm::mat4(1.0f), glm::vec3(scale, scale, scale)); + // Create the scaling matrix + Matrix4f scale_matrix = + glm::scale(Matrix4f(1.0f), Vector3f(scale, scale, scale)); - // Calculate the center of the bounding box - glm::vec3 center = glm::vec3((max.x + min.x) / 2.0f, (max.y + min.y) / 2.0f, (max.z + min.z) / 2.0f); + // Calculate the center of the bounding box + Vector3f center = Vector3f((max.x + min.x) / 2.0f, (max.y + min.y) / 2.0f, + (max.z + min.z) / 2.0f); - // Create the translation matrix - glm::mat4 translation_matrix = glm::translate(glm::mat4(1.0f), -center); + // Create the translation matrix + Matrix4f translation_matrix = glm::translate(Matrix4f(1.0f), -center); - // Combine the scaling and translation matrices - glm::mat4 normalization_matrix = scale_matrix * translation_matrix; + // Combine the scaling and translation matrices + Matrix4f normalization_matrix = scale_matrix * translation_matrix; - // Debug output - SPDLOG_DEBUG("normalization_matrix: \n{}", glm::to_string(normalization_matrix)); + // Debug output + SPDLOG_DEBUG("normalization_matrix: \n{}", + glm::to_string(normalization_matrix)); - // Apply the normalization matrix to the model - *this = *this * normalization_matrix; + // Apply the normalization matrix to the model + *this = *this * normalization_matrix; } } // namespace simple_renderer diff --git a/src/shader_base.cpp b/src/shader_base.cpp index 50a2968..c0e9a83 100644 --- a/src/shader_base.cpp +++ b/src/shader_base.cpp @@ -22,9 +22,9 @@ ShaderVertexIn::ShaderVertexIn(const Model::Face &face) : face_(face) {} ShaderVertexOut::ShaderVertexOut(const Model::Face &face) : face_(face) {} -ShaderFragmentIn::ShaderFragmentIn(const glm::vec3 &barycentric_coord, - const glm::vec3 &normal, - const glm::vec3 &light, const Color &color0, +ShaderFragmentIn::ShaderFragmentIn(const Vector3f &barycentric_coord, + const Vector3f &normal, + const Vector3f &light, const Color &color0, const Color &color1, const Color &color2) : barycentric_coord_(barycentric_coord), normal_(normal), @@ -37,9 +37,9 @@ ShaderFragmentOut::ShaderFragmentOut(const bool &is_need_draw, const Color &color) : is_need_draw_(is_need_draw), color_(color) {} -ShaderData::ShaderData(const glm::mat4 &model_matrix, - const glm::mat4 &view_matrix, - const glm::mat4 &project_matrix) +ShaderData::ShaderData(const Matrix4f &model_matrix, + const Matrix4f &view_matrix, + const Matrix4f &project_matrix) : model_matrix_(model_matrix), view_matrix_(view_matrix), project_matrix_(project_matrix) {} diff --git a/src/simple_renderer.cpp b/src/simple_renderer.cpp index b01b9a7..cee8747 100755 --- a/src/simple_renderer.cpp +++ b/src/simple_renderer.cpp @@ -160,7 +160,7 @@ void SimpleRenderer::DrawTriangle(const ShaderBase &shader, const Light &light, } auto [is_inside, barycentric_coord] = GetBarycentricCoord( v0.coord_, v1.coord_, v2.coord_, - glm::vec3(static_cast(x), static_cast(y), 0)); + Vector3f(static_cast(x), static_cast(y), 0)); // 如果点在三角形内再进行下一步 if (!is_inside) { continue; @@ -222,37 +222,37 @@ void SimpleRenderer::DrawModel(const ShaderBase &shader, const Light &light, } /// @todo 巨大性能开销 -auto SimpleRenderer::GetBarycentricCoord(const glm::vec3 &p0, const glm::vec3 &p1, - const glm::vec3 &p2, const glm::vec3 &pa) - -> std::pair { +auto SimpleRenderer::GetBarycentricCoord(const Vector3f &p0, const Vector3f &p1, + const Vector3f &p2, const Vector3f &pa) + -> std::pair { auto p1p0 = p1 - p0; auto p2p0 = p2 - p0; auto pap0 = pa - p0; auto deno = (p1p0.x * p2p0.y - p1p0.y * p2p0.x); if (std::abs(deno) < std::numeric_limits::epsilon()) { - return std::pair{false, glm::vec3()}; + return std::pair{false, Vector3f()}; } auto s = (p2p0.y * pap0.x - p2p0.x * pap0.y) / deno; if ((s > 1) || (s < 0)) { - return std::pair{false, glm::vec3()}; + return std::pair{false, Vector3f()}; } auto t = (p1p0.x * pap0.y - p1p0.y * pap0.x) / deno; if ((t > 1) || (t < 0)) { - return std::pair{false, glm::vec3()}; + return std::pair{false, Vector3f()}; } if ((1 - s - t > 1) || (1 - s - t < 0)) { - return std::pair{false, glm::vec3()}; + return std::pair{false, Vector3f()}; } - return std::pair{true, glm::vec3(1 - s - t, s, t)}; + return std::pair{true, Vector3f(1 - s - t, s, t)}; } auto SimpleRenderer::InterpolateDepth(float depth0, float depth1, float depth2, - const glm::vec3 &_barycentric_coord) + const Vector3f &_barycentric_coord) -> float { auto depth = depth0 * _barycentric_coord.x; depth += depth1 * _barycentric_coord.y; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 4a4b591..8872e67 100755 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,7 +14,7 @@ enable_testing() include(GoogleTest) include_directories( - ${SimpleRenderer_SOURCE_DIR}/include + ${SimpleRenderer_SOURCE_DIR}/src/include ) list(APPEND DEFAULT_TEST_COMPILE_OPTIONS @@ -38,26 +38,14 @@ link_libraries( SimpleRenderer ) -# Check if the platform is macOS -if(APPLE) - # Enable RPATH support on macOS - set(CMAKE_MACOSX_RPATH 1) - # Set the RPATH to look for frameworks relative to the executable path - set(CMAKE_INSTALL_RPATH "/Library/Frameworks") - - # Ensure that RPATH is used in the build - set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) -endif() - - -# add_subdirectory(unit_test) +add_subdirectory(unit_test) #add_subdirectory(integration_test) add_subdirectory(system_test) -# add_coverage_target( -# DEPENDS unit_test -# SOURCE_DIR ${SimpleRenderer_SOURCE_DIR} -# BINARY_DIR ${SimpleRenderer_BINARY_DIR} -# EXCLUDE_DIR ${SimpleRenderer_SOURCE_DIR}/3rd/* -# ) +add_coverage_target( + DEPENDS unit_test + SOURCE_DIR ${SimpleRenderer_SOURCE_DIR} + BINARY_DIR ${SimpleRenderer_BINARY_DIR} + EXCLUDE_DIR ${SimpleRenderer_SOURCE_DIR}/3rd/* +) diff --git a/test/system_test/camera.h b/test/system_test/camera.h index b661e1f..ffaf55b 100644 --- a/test/system_test/camera.h +++ b/test/system_test/camera.h @@ -33,9 +33,9 @@ class Camera { /// 光照名称 std::string name_ = "default light name"; /// 位置 - glm::vec3 pos_; + Vector3f pos_; /// 方向 - glm::vec3 dir_; + Vector3f dir_; /** * 构造函数 diff --git a/test/system_test/main.cpp b/test/system_test/main.cpp index 0a166c3..63cb649 100755 --- a/test/system_test/main.cpp +++ b/test/system_test/main.cpp @@ -59,16 +59,18 @@ int main(int argc, char **argv) { // objs.emplace_back(obj_path + "/african_head.obj"); objs.emplace_back(obj_path + "/utah-teapot/utah-teapot.obj"); - - auto matrix = - glm::mat4(1.0f); - glm::mat4 scale_matrix = glm::scale(glm::mat4(1.0f), glm::vec3(500.0f, 500.0f, 500.0f)); - -// Translation matrix - glm::mat4 translation_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(kWidth / 2.0f, kHeight / 2.0f, 0.0f)); - - // Combined transformation matrix - matrix = translation_matrix * scale_matrix; + auto matrix = simple_renderer::Matrix4f(1.0f); + simple_renderer::Matrix4f scale_matrix = + glm::scale(simple_renderer::Matrix4f(1.0f), + simple_renderer::Vector3f(500.0f, 500.0f, 500.0f)); + + // Translation matrix + simple_renderer::Matrix4f translation_matrix = glm::translate( + simple_renderer::Matrix4f(1.0f), + simple_renderer::Vector3f(kWidth / 2.0f, kHeight / 2.0f, 0.0f)); + + // Combined transformation matrix + matrix = translation_matrix * scale_matrix; // 矩阵运算的顺序 // 归一化 From b502e5cce0128f2ab09e828a30c0718142c8a62e Mon Sep 17 00:00:00 2001 From: ZhuohaoHe Date: Fri, 9 Aug 2024 16:00:35 -0400 Subject: [PATCH 8/9] update 3rd.cmake to pass the CI Signed-off-by: ZhuohaoHe --- CMakePresets.json | 15 ++++----------- cmake/3rd.cmake | 4 ---- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/CMakePresets.json b/CMakePresets.json index 744f911..d01acbd 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -46,7 +46,7 @@ } }, { - "name": "config-mac", + "name": "config-macos", "hidden": true, "inherits": [ "config-base" ], "displayName": "config-base", @@ -72,20 +72,13 @@ "description": "build base configurePresets" }, { - "name": "build-mac", + "name": "build-macos", "hidden": false, "inherits": [ - "config-mac" + "config-macos" ], - "displayName": "build-mac", + "displayName": "build-macos", "description": "macOS build configurePresets" } - ], - "buildPresets": [ - { - "name": "build", - "displayName": "Build", - "configurePreset": "config-base" - } ] } \ No newline at end of file diff --git a/cmake/3rd.cmake b/cmake/3rd.cmake index d22cd05..2cf4f9e 100644 --- a/cmake/3rd.cmake +++ b/cmake/3rd.cmake @@ -103,10 +103,6 @@ CPMAddPackage( GITHUB_REPOSITORY g-truc/glm GIT_TAG 1.0.1 ) -if (glm_ADDED) - add_library(glm INTERFACE) - target_include_directories(glm INTERFACE ${glm_SOURCE_DIR}) -endif () # https://github.com/nothings/stb.git CPMAddPackage( From b380f8f946b418be2c3d6057c0c43e9df0506fcf Mon Sep 17 00:00:00 2001 From: ZhuohaoHe Date: Fri, 9 Aug 2024 16:25:27 -0400 Subject: [PATCH 9/9] update the CMakeLists.txt to make the log path correct Signed-off-by: ZhuohaoHe --- $EXECUTABLE_OUTPUT_PATH/logs/SimpleRendererLog.log | 8 -------- CMakeLists.txt | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) delete mode 100644 $EXECUTABLE_OUTPUT_PATH/logs/SimpleRendererLog.log diff --git a/$EXECUTABLE_OUTPUT_PATH/logs/SimpleRendererLog.log b/$EXECUTABLE_OUTPUT_PATH/logs/SimpleRendererLog.log deleted file mode 100644 index 0d5deb8..0000000 --- a/$EXECUTABLE_OUTPUT_PATH/logs/SimpleRendererLog.log +++ /dev/null @@ -1,8 +0,0 @@ -[2024-08-09 15:04:13.936] [simple_renderer.cpp:57 SimpleRenderer] [info] SimpleRenderer init with 1920, 1080 -[2024-08-09 15:04:13.949] [model.cpp:115 Model] [info] 加载模型: obj/utah-teapot/utah-teapot.obj, 顶点数: 2082, 法线数: 2082, 颜色数: 2082, UV数: 1036, 子模型数: 1, 材质数: 1 -[2024-08-09 15:04:13.960] [simple_renderer.cpp:61 render] [info] render model: obj/utah-teapot/utah-teapot.obj -[2024-08-09 15:04:13.961] [simple_renderer.cpp:197 DrawModel] [info] draw obj/utah-teapot/utah-teapot.obj -[2024-08-09 15:37:12.808] [simple_renderer.cpp:57 SimpleRenderer] [info] SimpleRenderer init with 1920, 1080 -[2024-08-09 15:37:12.821] [model.cpp:118 Model] [info] 加载模型: obj/utah-teapot/utah-teapot.obj, 顶点数: 2082, 法线数: 2082, 颜色数: 2082, UV数: 1036, 子模型数: 1, 材质数: 1 -[2024-08-09 15:37:12.833] [simple_renderer.cpp:61 render] [info] render model: obj/utah-teapot/utah-teapot.obj -[2024-08-09 15:37:12.835] [simple_renderer.cpp:197 DrawModel] [info] draw obj/utah-teapot/utah-teapot.obj diff --git a/CMakeLists.txt b/CMakeLists.txt index 253b3dc..0d7a959 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,7 +40,7 @@ set(FONT_FILE_PATH "${wqy_font_SOURCE_DIR}/wqy-zenhei.ttc") include(ProcessorCount) ProcessorCount(NPROC) # 日志文件路径 -set(LOG_FILE_PATH "$EXECUTABLE_OUTPUT_PATH/logs/SimpleRendererLog.log") +set(LOG_FILE_PATH "${EXECUTABLE_OUTPUT_PATH}/logs/SimpleRendererLog.log") # 日志文件大小 set(LOG_FILE_MAX_SIZE 1024*1024*4) # 日志文件数量