Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ tools/opensbi/build
.idea
3rd
Doxyfile
src/include/config.h
include/config.h

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

为啥要把 include 放在 src 外面呢?
我的理解是 src 下包括源码+头文件,include 与 src 同级的话感觉会与 tools/cmake 等辅助文件搞混

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

收到🫡

7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# 设置最小 cmake 版本
cmake_minimum_required(VERSION 3.27 FATAL_ERROR)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

@ZzzhHe ZzzhHe Aug 9, 2024

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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 Expand Up @@ -39,15 +42,15 @@ 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")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

EXECUTABLE_OUTPUT_PATH 这个值同样在 CMakePresets.json 中,如果构建目录不在 build 下可能有问题,建议使用 CMakePresets.json 下的值

# 日志文件大小
set(LOG_FILE_MAX_SIZE 1024*1024*4)
# 日志文件数量
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"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

这里还是 include 目录的问题,后面不再专门说了~

)

# 添加要编译的目录
Expand Down
55 changes: 31 additions & 24 deletions cmake/3rd.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,36 @@ 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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

239 行还有一个 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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

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
Expand All @@ -95,6 +103,12 @@ if (tinyobjloader_ADDED)
)
endif ()

CPMAddPackage(

Copy link
Copy Markdown
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 +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
Expand Down Expand Up @@ -252,3 +253,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 ()

Copy link
Copy Markdown
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
)
File renamed without changes.
38 changes: 38 additions & 0 deletions include/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

生成的文件加到 gitignore 里比较好

/**
* @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:
* <table>
* <tr><th>Date<th>Author<th>Description
* <tr><td>2023-08-24<td>Zone.N<td>创建文件
* </table>
*/

#ifndef SIMPLERENDER_SRC_INCLUDE_CONFIG_H_
#define SIMPLERENDER_SRC_INCLUDE_CONFIG_H_

#include <string>
#include <string_view>

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_ */
2 changes: 1 addition & 1 deletion src/include/default_shader.h → include/default_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

之前的 Vector3f 是

using Vector2f = Eigen::Vector2f;
using Vector3f = Eigen::Vector3f;
using Vector4f = Eigen::Vector4f;

直接修改一下即可,不需要在源码里改这么多

};

} // namespace simple_renderer
Expand Down
8 changes: 4 additions & 4 deletions src/include/light.h → include/light.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
};
Expand Down
File renamed without changes.
17 changes: 9 additions & 8 deletions src/include/matrix.hpp → include/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,27 @@
#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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

所以为啥不用 using,要删掉呢


} // 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());
struct fmt::formatter<glm::mat4> : fmt::formatter<std::string> {
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);
}
};

Expand Down
18 changes: 9 additions & 9 deletions src/include/model.hpp → include/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Copy link
Copy Markdown
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:
/// 反光度
float shininess = 0;
/// 环境光照
Vector3f ambient;
glm::vec3 ambient;
/// 漫反射光照
Vector3f diffuse;
glm::vec3 diffuse;
/// 镜面光照
Vector3f specular;
glm::vec3 specular;

/// @name 默认构造/析构函数
/// @{
Expand Down Expand Up @@ -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 直接保存太浪费内存了
Expand Down Expand Up @@ -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 文件路径
Expand Down Expand Up @@ -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;

/**
* 获取面
Expand Down
20 changes: 10 additions & 10 deletions src/include/shader_base.h → include/shader_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 三个顶点的颜色
/// @{
Expand All @@ -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);

Expand Down Expand Up @@ -160,20 +160,20 @@ 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);

/**
* 构造函数
* @param model_matrix 模型变换矩阵
* @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 默认构造/析构函数
/// @{
Expand Down
8 changes: 4 additions & 4 deletions src/include/simple_renderer.h → include/simple_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool, Vector3f>;
static auto GetBarycentricCoord(const glm::vec3 &p0, const glm::vec3 &p1,
const glm::vec3 &p2, const glm::vec3 &pa)
-> std::pair<bool, glm::vec3>;

/**
* 深度插值,由重心坐标计算出对应点的深度值
Expand All @@ -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
Expand Down
Loading