Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganization: GLM Integration, SDL2 on macOS Build Fix, and Structural Enhancements #11

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# 设置最小 cmake 版本
cmake_minimum_required(VERSION 3.27 FATAL_ERROR)


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这两个在 CMakePresets.json 里面设置过了,可以删掉

Copy link
Contributor Author

@ZzzhHe ZzzhHe Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

但是删掉之后,就会在std::span的地方报错

In file included from /Users/hezhohao/Programming/GitRepo/SimpleRenderer/src/color.cpp:22:
/Users/hezhohao/Programming/GitRepo/SimpleRenderer/src/include/log_system.h:75:25: warning: unused parameter 'args' [-Wunused-parameter]
   75 |   void debug(TARGS &&...args) {
      |                         ^
/Users/hezhohao/Programming/GitRepo/SimpleRenderer/src/color.cpp:33:24: error: no member named 'span' in namespace 'std'
   33 |   auto data_ptr = std::span(reinterpret_cast<uint8_t *>(&data), 4);
      |                   ~~~~~^
/Users/hezhohao/Programming/GitRepo/SimpleRenderer/src/color.cpp:101:23: error: no member named 'span' in namespace 'std'
  101 |   auto ret_ptr = std::span(reinterpret_cast<uint8_t *>(&ret), 4);
      |                  ~~~~~^
1 warning and 2 errors generated.
make[2]: *** [src/CMakeFiles/SimpleRenderer.dir/color.cpp.o] Error 1
make[1]: *** [src/CMakeFiles/SimpleRenderer.dir/all] Error 2
make: *** [all] Error 2

参考这个链接

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我发现了,是因为CMakePresets.json这个文件我没设置好,导致在跑命令cmake --list-presets 的时候没有输出列表,然后就没办法用这个preset,我简单调整了一下preset的代码,现在可以正常使用preset跑了。

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)

&

set(LOG_FILE_PATH "${PROJECT_SOURCE_DIR}/build/logs/SimpleRendererLog.log")

这两个地方都可以按照原本的代码直接跑,我下个commit会删掉我的更改

# 设置项目名与版本
project(SimpleRenderer
VERSION 0.0.1)
Expand Down
53 changes: 33 additions & 20 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand All @@ -58,14 +45,40 @@
}
}
},
{
"name": "config-macos",
"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-macos",
"hidden": false,
"inherits": [
"config-macos"
],
"displayName": "build",
"description": "build"
"displayName": "build-macos",
"description": "macOS build configurePresets"
}
]
}
66 changes: 28 additions & 38 deletions cmake/3rd.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,29 @@ CPMAddPackage(
"gtest_force_shared_crt ON"
)

# https://github.com/aminosbh/sdl2-cmake-modules.git
# https://github.com/libsdl-org/SDL
CPMAddPackage(
NAME sdl2-cmake-modules
GIT_REPOSITORY https://github.com/aminosbh/sdl2-cmake-modules.git
GIT_TAG ad006a3daae65a612ed87415037e32188b81071e
DOWNLOAD_ONLY True
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"
)
if (sdl2-cmake-modules_ADDED)
list(APPEND CMAKE_MODULE_PATH ${sdl2-cmake-modules_SOURCE_DIR})
endif ()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前的习惯是 CPMAddPackage 放在一起,find_package 放在一起,最好统一格式,改成旧有的分开或者改成按模块

## 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/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(
Expand All @@ -95,6 +97,13 @@ if (tinyobjloader_ADDED)
)
endif ()

# https://github.com/g-truc/glm
CPMAddPackage(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加一个 github 链接

NAME glm
GITHUB_REPOSITORY g-truc/glm
GIT_TAG 1.0.1
)

# https://github.com/nothings/stb.git
CPMAddPackage(
NAME stb
Expand All @@ -111,19 +120,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
Expand Down Expand Up @@ -235,12 +231,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"
Expand All @@ -251,4 +241,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 ()
endif ()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里 glm 的 CPMAddPackage 和 find_package 又分开写了,注意统一哈

4 changes: 2 additions & 2 deletions cmake/compile_config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ list(APPEND DEFAULT_LINK_LIB
spdlog::spdlog
stb
tinyobjloader
Eigen
glm::glm
${glog_LIBRARIES}
SDL2::Main
SDL2::SDL2
OpenMP::OpenMP_CXX
)
26 changes: 8 additions & 18 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src/include>
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src/include>
$<INSTALL_INTERFACE:include>
)

target_compile_options(${PROJECT_NAME} PRIVATE
${DEFAULT_COMPILE_OPTIONS}
${DEFAULT_COMPILE_OPTIONS}
)

target_link_libraries(${PROJECT_NAME} PRIVATE
${DEFAULT_LINK_LIB}
${DEFAULT_LINK_LIB}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmakelist 的对齐你是用的什么?我是 clion 默认的对齐方案,这样比较省事,如果你有好用的工具可以推荐下

)
21 changes: 11 additions & 10 deletions src/default_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ auto DefaultShader::InterpolateColor(
const Vector3f &barycentric_coord) -> Color {
return Color(
static_cast<uint8_t>(static_cast<float>(color0[Color::kColorIndexRed]) *
barycentric_coord.x() +
barycentric_coord.x +
static_cast<float>(color1[Color::kColorIndexRed]) *
barycentric_coord.y() +
barycentric_coord.y +
static_cast<float>(color2[Color::kColorIndexRed]) *
barycentric_coord.z()),
barycentric_coord.z),
static_cast<uint8_t>(static_cast<float>(color0[Color::kColorIndexGreen]) *
barycentric_coord.x() +
barycentric_coord.x +
static_cast<float>(color1[Color::kColorIndexGreen]) *
barycentric_coord.y() +
barycentric_coord.y +
static_cast<float>(color2[Color::kColorIndexGreen]) *
barycentric_coord.z()),
barycentric_coord.z),
static_cast<uint8_t>(static_cast<float>(color0[Color::kColorIndexBlue]) *
barycentric_coord.x() +
barycentric_coord.x +
static_cast<float>(color1[Color::kColorIndexBlue]) *
barycentric_coord.y() +
barycentric_coord.y +
static_cast<float>(color2[Color::kColorIndexBlue]) *
barycentric_coord.z()));
barycentric_coord.z));
}

/// @todo 巨大性能开销
Expand All @@ -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 = (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) {
Expand Down
21 changes: 12 additions & 9 deletions src/include/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,29 @@
#ifndef SIMPLERENDER_SRC_INCLUDE_MATRIX_HPP_
#define SIMPLERENDER_SRC_INCLUDE_MATRIX_HPP_

#include <Eigen/Dense>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/string_cast.hpp>

#include "log_system.h"

namespace simple_renderer {

using Matrix4f = Eigen::Matrix4f;

using Matrix4f = glm::mat4;
} // namespace simple_renderer

/**
* spdlog 输出矩阵实现
*/
template <>
struct fmt::formatter<simple_renderer::Matrix4f> : fmt::formatter<std::string> {
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());
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);
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/include/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Model {
/// 法向量
using Normal = Vector3f;
/// 贴图
using TextureCoord = Vector2f;
using TextureCoord = glm::vec2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们不是有 Vector2f 嘛,直接用呗


class Material {
public:
Expand Down
6 changes: 3 additions & 3 deletions src/include/shader_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ class ShaderFragmentOut {
class ShaderData {
public:
/// 模型变换矩阵
Matrix4f model_matrix_ = Matrix4f().setIdentity();
Matrix4f model_matrix_ = Matrix4f(1.0f);
/// 视图变换矩阵
Matrix4f view_matrix_ = Matrix4f().setIdentity();
Matrix4f view_matrix_ = Matrix4f(1.0f);
/// 正交变换矩阵
Matrix4f project_matrix_ = Matrix4f().setIdentity();
Matrix4f project_matrix_ = Matrix4f(1.0f);

/**
* 构造函数
Expand Down
Loading
Loading