Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 890c15c

Browse files
committedAug 9, 2024·
update according to the reviews
Signed-off-by: ZhuohaoHe <zhuohao.he@outlook.com>
1 parent c877b4d commit 890c15c

26 files changed

+276
-297
lines changed
 
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[2024-08-09 15:04:13.936] [simple_renderer.cpp:57 SimpleRenderer] [info] SimpleRenderer init with 1920, 1080
2+
[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
3+
[2024-08-09 15:04:13.960] [simple_renderer.cpp:61 render] [info] render model: obj/utah-teapot/utah-teapot.obj
4+
[2024-08-09 15:04:13.961] [simple_renderer.cpp:197 DrawModel] [info] draw obj/utah-teapot/utah-teapot.obj
5+
[2024-08-09 15:37:12.808] [simple_renderer.cpp:57 SimpleRenderer] [info] SimpleRenderer init with 1920, 1080
6+
[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
7+
[2024-08-09 15:37:12.833] [simple_renderer.cpp:61 render] [info] render model: obj/utah-teapot/utah-teapot.obj
8+
[2024-08-09 15:37:12.835] [simple_renderer.cpp:197 DrawModel] [info] draw obj/utah-teapot/utah-teapot.obj

‎.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ tools/opensbi/build
1414
.idea
1515
3rd
1616
Doxyfile
17-
include/config.h
17+
src/include/config.h

‎CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
# 设置最小 cmake 版本
88
cmake_minimum_required(VERSION 3.27 FATAL_ERROR)
99

10-
set(CMAKE_CXX_STANDARD 20)
11-
set(CMAKE_CXX_STANDARD_REQUIRED True)
1210

1311
# 设置项目名与版本
1412
project(SimpleRenderer
@@ -42,15 +40,15 @@ set(FONT_FILE_PATH "${wqy_font_SOURCE_DIR}/wqy-zenhei.ttc")
4240
include(ProcessorCount)
4341
ProcessorCount(NPROC)
4442
# 日志文件路径
45-
set(LOG_FILE_PATH "${PROJECT_SOURCE_DIR}/build/logs/SimpleRendererLog.log")
43+
set(LOG_FILE_PATH "$EXECUTABLE_OUTPUT_PATH/logs/SimpleRendererLog.log")
4644
# 日志文件大小
4745
set(LOG_FILE_MAX_SIZE 1024*1024*4)
4846
# 日志文件数量
4947
set(LOG_FILE_MAX_COUNT 8)
5048
# 生成配置头文件
5149
configure_file(
5250
"${PROJECT_SOURCE_DIR}/cmake/config.h.in"
53-
"${PROJECT_SOURCE_DIR}/include/config.h"
51+
"${PROJECT_SOURCE_DIR}/src/include/config.h"
5452
)
5553

5654
# 添加要编译的目录

‎CMakePresets.json

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,10 @@
22
"version": 6,
33
"cmakeMinimumRequired": {
44
"major": 3,
5-
"minor": 27,
5+
"minor": 23,
66
"patch": 0
77
},
88
"configurePresets": [
9-
{
10-
"name": "host",
11-
"description": "Linux Only",
12-
"hidden": true,
13-
"condition": {
14-
"type": "equals",
15-
"lhs": "${hostSystemName}",
16-
"rhs": "Linux"
17-
}
18-
},
199
{
2010
"name": "std",
2111
"description": "This preset makes sure the project actually builds with at least the specified standard",
@@ -30,13 +20,10 @@
3020
}
3121
},
3222
{
33-
"name": "configurePresets_base",
23+
"name": "config-base",
3424
"hidden": true,
35-
"inherits": [
36-
"host",
37-
"std"
38-
],
39-
"displayName": "configurePresets_base",
25+
"inherits": [ "std" ],
26+
"displayName": "config-base",
4027
"description": "base configurePresets",
4128
"binaryDir": "${sourceDir}/build",
4229
"cacheVariables": {
@@ -58,14 +45,47 @@
5845
}
5946
}
6047
},
48+
{
49+
"name": "config-mac",
50+
"hidden": true,
51+
"inherits": [ "config-base" ],
52+
"displayName": "config-base",
53+
"description": "macOS configurePresets",
54+
"condition": {
55+
"type": "equals",
56+
"lhs": "${hostSystemName}",
57+
"rhs": "Darwin"
58+
},
59+
"cacheVariables": {
60+
"CMAKE_MACOSX_RPATH": "1",
61+
"CMAKE_INSTALL_RPATH": "/Library/Frameworks",
62+
"CMAKE_BUILD_WITH_INSTALL_RPATH": "TRUE"
63+
}
64+
},
6165
{
6266
"name": "build",
6367
"hidden": false,
6468
"inherits": [
65-
"configurePresets_base"
69+
"config-base"
70+
],
71+
"displayName": "build-base",
72+
"description": "build base configurePresets"
73+
},
74+
{
75+
"name": "build-mac",
76+
"hidden": false,
77+
"inherits": [
78+
"config-mac"
6679
],
67-
"displayName": "build",
68-
"description": "build"
80+
"displayName": "build-mac",
81+
"description": "macOS build configurePresets"
82+
}
83+
],
84+
"buildPresets": [
85+
{
86+
"name": "build",
87+
"displayName": "Build",
88+
"configurePreset": "config-base"
6989
}
7090
]
7191
}

‎cmake/3rd.cmake

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ CPMAddPackage(
5757
"gtest_force_shared_crt ON"
5858
)
5959

60-
# SDL2
61-
60+
# https://github.com/libsdl-org/SDL
6261
CPMAddPackage(
6362
NAME SDL2
6463
GITHUB_REPOSITORY libsdl-org/SDL
@@ -70,22 +69,17 @@ CPMAddPackage(
7069
"SDL_STATIC_PIC ON"
7170
"SDL_WERROR OFF"
7271
)
73-
find_package(SDL2 REQUIRED)
74-
75-
# https://github.com/aminosbh/sdl2-cmake-modules.git
76-
CPMAddPackage(
77-
NAME sdl2-cmake-modules
78-
GIT_REPOSITORY https://github.com/aminosbh/sdl2-cmake-modules.git
79-
GIT_TAG ad006a3daae65a612ed87415037e32188b81071e
80-
DOWNLOAD_ONLY True
81-
)
82-
if (SDL2_ADDED)
83-
add_library(SDL2::SDL2)
84-
endif()
8572

86-
if (sdl2-cmake-modules_ADDED)
87-
list(APPEND CMAKE_MODULE_PATH ${sdl2-cmake-modules_SOURCE_DIR})
88-
endif ()
73+
# # https://github.com/aminosbh/sdl2-cmake-modules.git
74+
# CPMAddPackage(
75+
# NAME sdl2-cmake-modules
76+
# GIT_REPOSITORY https://github.com/aminosbh/sdl2-cmake-modules.git
77+
# GIT_TAG ad006a3daae65a612ed87415037e32188b81071e
78+
# DOWNLOAD_ONLY True
79+
# )
80+
# if (sdl2-cmake-modules_ADDED)
81+
# list(APPEND CMAKE_MODULE_PATH ${sdl2-cmake-modules_SOURCE_DIR})
82+
# endif ()
8983

9084
# https://github.com/tinyobjloader/tinyobjloader.git
9185
CPMAddPackage(
@@ -103,11 +97,16 @@ if (tinyobjloader_ADDED)
10397
)
10498
endif ()
10599

100+
# https://github.com/g-truc/glm
106101
CPMAddPackage(
107102
NAME glm
108103
GITHUB_REPOSITORY g-truc/glm
109104
GIT_TAG 1.0.1
110105
)
106+
if (glm_ADDED)
107+
add_library(glm INTERFACE)
108+
target_include_directories(glm INTERFACE ${glm_SOURCE_DIR})
109+
endif ()
111110

112111
# https://github.com/nothings/stb.git
113112
CPMAddPackage(
@@ -236,12 +235,6 @@ if (NOT LCOV_EXE)
236235
"Following https://github.com/linux-test-project/lcov to install.")
237236
endif ()
238237

239-
find_package(SDL2 REQUIRED)
240-
if (NOT SDL2_FOUND)
241-
message(FATAL_ERROR "sdl2 not found.\n"
242-
"Following https://github.com/libsdl-org/SDL to install.")
243-
endif ()
244-
245238
find_package(OpenMP REQUIRED)
246239
if (NOT OpenMP_FOUND)
247240
message(FATAL_ERROR "OpenMP not found.\n"
@@ -252,10 +245,4 @@ find_package(spdlog REQUIRED)
252245
if (NOT spdlog_FOUND)
253246
message(FATAL_ERROR "spdlog not found.\n"
254247
"Following https://github.com/gabime/spdlog to install.")
255-
endif ()
256-
257-
find_package(glm REQUIRED)
258-
if (NOT glm_FOUND)
259-
message(FATAL_ERROR "glm not found.\n"
260-
"Following https://github.com/g-truc/glm tp install")
261248
endif ()

‎include/config.h

Lines changed: 0 additions & 38 deletions
This file was deleted.

‎include/vector.hpp

Lines changed: 0 additions & 57 deletions
This file was deleted.

‎src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS
1212
add_library(${PROJECT_NAME} STATIC ${SRC_FILES})
1313

1414
target_include_directories(${PROJECT_NAME} PRIVATE
15-
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
15+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src/include>
1616
$<INSTALL_INTERFACE:include>
1717
)
1818

‎src/default_shader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace simple_renderer {
2020

2121
auto DefaultShader::InterpolateColor(
2222
const Color &color0, const Color &color1, const Color &color2,
23-
const glm::vec3 &barycentric_coord) -> Color {
23+
const Vector3f &barycentric_coord) -> Color {
2424
return Color(
2525
static_cast<uint8_t>(static_cast<float>(color0[Color::kColorIndexRed]) *
2626
barycentric_coord.x +
@@ -56,7 +56,8 @@ auto DefaultShader::Vertex(const ShaderVertexIn &shader_vertex_in) const
5656

5757
auto DefaultShader::Fragment(const ShaderFragmentIn &shader_fragment_in) const
5858
-> ShaderFragmentOut {
59-
auto intensity = glm::dot(shader_fragment_in.normal_,shader_fragment_in.light_);
59+
auto intensity =
60+
glm::dot(shader_fragment_in.normal_, shader_fragment_in.light_);
6061
auto is_need_draw = true;
6162
// 光照方向为正,不绘制背面
6263
if (intensity <= 0) {
File renamed without changes.

‎include/default_shader.h renamed to ‎src/include/default_shader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class DefaultShader : public ShaderBase {
6464
*/
6565
static auto InterpolateColor(const Color &color0, const Color &color1,
6666
const Color &color2,
67-
const glm::vec3 &barycentric_coord) -> Color;
67+
const Vector3f &barycentric_coord) -> Color;
6868
};
6969

7070
} // namespace simple_renderer

‎include/light.h renamed to ‎src/include/light.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class Light {
3333
/// 光照名称
3434
std::string name_ = "default light name";
3535
/// 位置
36-
glm::vec3 pos = kDefaultPos;
36+
Vector3f pos = kDefaultPos;
3737
/// 方向
38-
glm::vec3 dir = kDefaultDir;
38+
Vector3f dir = kDefaultDir;
3939
/// 颜色
4040
Color color = kDefaultColor;
4141

@@ -57,9 +57,9 @@ class Light {
5757

5858
private:
5959
/// 默认位置
60-
static const glm::vec3 kDefaultPos;
60+
static const Vector3f kDefaultPos;
6161
/// 默认方向,左手系,x 向右,y 向下,z 正方向为屏幕由内向外
62-
static const glm::vec3 kDefaultDir;
62+
static const Vector3f kDefaultDir;
6363
/// 默认颜色
6464
static const Color kDefaultColor;
6565
};
File renamed without changes.

‎include/matrix.hpp renamed to ‎src/include/matrix.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,22 @@
2323
#include "log_system.h"
2424

2525
namespace simple_renderer {
26-
27-
26+
using Matrix4f = glm::mat4;
2827
} // namespace simple_renderer
2928

3029
/**
3130
* spdlog 输出矩阵实现
3231
*/
3332
template <>
34-
struct fmt::formatter<glm::mat4> : fmt::formatter<std::string> {
35-
auto format(const glm::mat4 &matrix, fmt::format_context &ctx) const -> decltype(ctx.out()) {
36-
// Convert the glm::mat4 to a string using glm::to_string
33+
struct fmt::formatter<simple_renderer::Matrix4f> : fmt::formatter<std::string> {
34+
auto format(const simple_renderer::Matrix4f &matrix,
35+
fmt::format_context &ctx) const -> decltype(ctx.out()) {
36+
// Convert the Matrix4f to a string using glm::to_string
37+
// 转化矩阵为字符串
3738
std::string matrix_str = glm::to_string(matrix);
38-
39+
3940
// Format and output the string
41+
// 输出格式化后的字符串
4042
return fmt::format_to(ctx.out(), "\n{}", matrix_str);
4143
}
4244
};

‎include/model.hpp renamed to ‎src/include/model.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ namespace simple_renderer {
3636
class Model {
3737
public:
3838
/// 顶点坐标
39-
using Coord = glm::vec3;
39+
using Coord = Vector3f;
4040
/// 法向量
41-
using Normal = glm::vec3;
41+
using Normal = Vector3f;
4242
/// 贴图
4343
using TextureCoord = glm::vec2;
4444

@@ -47,11 +47,11 @@ class Model {
4747
/// 反光度
4848
float shininess = 0;
4949
/// 环境光照
50-
glm::vec3 ambient;
50+
Vector3f ambient;
5151
/// 漫反射光照
52-
glm::vec3 diffuse;
52+
Vector3f diffuse;
5353
/// 镜面光照
54-
glm::vec3 specular;
54+
Vector3f specular;
5555

5656
/// @name 默认构造/析构函数
5757
/// @{
@@ -108,7 +108,7 @@ class Model {
108108
* @param tran 要对顶点进行的变换矩阵
109109
* @return 结果
110110
*/
111-
[[nodiscard]] auto operator*(const glm::mat4 &tran) const -> Vertex;
111+
[[nodiscard]] auto operator*(const Matrix4f &tran) const -> Vertex;
112112
};
113113

114114
/// @todo 直接保存太浪费内存了
@@ -148,7 +148,7 @@ class Model {
148148
* @param tran 要对面进行的变换矩阵
149149
* @return 结果
150150
*/
151-
[[nodiscard]] auto operator*(const glm::mat4 &tran) const -> Face;
151+
[[nodiscard]] auto operator*(const Matrix4f &tran) const -> Face;
152152
};
153153

154154
/// obj 文件路径
@@ -179,7 +179,7 @@ class Model {
179179
* @param tran 要对模型进行的变换矩阵
180180
* @return 结果
181181
*/
182-
[[nodiscard]] auto operator*(const glm::mat4 &tran) const -> Model;
182+
[[nodiscard]] auto operator*(const Matrix4f &tran) const -> Model;
183183

184184
/**
185185
* 获取面

‎include/shader_base.h renamed to ‎src/include/shader_base.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ class ShaderVertexOut {
8383
class ShaderFragmentIn {
8484
public:
8585
/// 重心坐标
86-
glm::vec3 barycentric_coord_;
86+
Vector3f barycentric_coord_;
8787
/// 法线方向
88-
glm::vec3 normal_;
88+
Vector3f normal_;
8989
/// 光照方向
90-
glm::vec3 light_;
90+
Vector3f light_;
9191

9292
/// @name 三个顶点的颜色
9393
/// @{
@@ -105,8 +105,8 @@ class ShaderFragmentIn {
105105
* @param color1 顶点 1 颜色
106106
* @param color2 顶点 2 颜色
107107
*/
108-
explicit ShaderFragmentIn(const glm::vec3 &_barycentric_coord,
109-
const glm::vec3 &_normal, const glm::vec3 &_light,
108+
explicit ShaderFragmentIn(const Vector3f &_barycentric_coord,
109+
const Vector3f &_normal, const Vector3f &_light,
110110
const Color &_color0, const Color &_color1,
111111
const Color &_color2);
112112

@@ -160,20 +160,20 @@ class ShaderFragmentOut {
160160
class ShaderData {
161161
public:
162162
/// 模型变换矩阵
163-
glm::mat4 model_matrix_ = glm::mat4(1.0f);
163+
Matrix4f model_matrix_ = Matrix4f(1.0f);
164164
/// 视图变换矩阵
165-
glm::mat4 view_matrix_ = glm::mat4(1.0f);
165+
Matrix4f view_matrix_ = Matrix4f(1.0f);
166166
/// 正交变换矩阵
167-
glm::mat4 project_matrix_ = glm::mat4(1.0f);
167+
Matrix4f project_matrix_ = Matrix4f(1.0f);
168168

169169
/**
170170
* 构造函数
171171
* @param model_matrix 模型变换矩阵
172172
* @param view_matrix 视图变换矩阵
173173
* @param project_matrix 正交变换矩阵
174174
*/
175-
explicit ShaderData(const glm::mat4 &model_matrix, const glm::mat4 &view_matrix,
176-
const glm::mat4 &project_matrix);
175+
explicit ShaderData(const Matrix4f &model_matrix, const Matrix4f &view_matrix,
176+
const Matrix4f &project_matrix);
177177

178178
/// @name 默认构造/析构函数
179179
/// @{

‎include/simple_renderer.h renamed to ‎src/include/simple_renderer.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ class SimpleRenderer {
128128
* weight_B = s
129129
* weight_C = t
130130
*/
131-
static auto GetBarycentricCoord(const glm::vec3 &p0, const glm::vec3 &p1,
132-
const glm::vec3 &p2, const glm::vec3 &pa)
133-
-> std::pair<bool, glm::vec3>;
131+
static auto GetBarycentricCoord(const Vector3f &p0, const Vector3f &p1,
132+
const Vector3f &p2, const Vector3f &pa)
133+
-> std::pair<bool, Vector3f>;
134134

135135
/**
136136
* 深度插值,由重心坐标计算出对应点的深度值
@@ -141,7 +141,7 @@ class SimpleRenderer {
141141
* @return 深度值
142142
*/
143143
static auto InterpolateDepth(float depth0, float depth1, float depth2,
144-
const glm::vec3 &barycentric_coord) -> float;
144+
const Vector3f &barycentric_coord) -> float;
145145
};
146146

147147
} // namespace simple_renderer

‎src/include/vector.hpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
/**
3+
* @file vector.hpp
4+
* @brief 向量模版
5+
* @author Zone.N (Zone.Niuzh@hotmail.com)
6+
* @version 1.0
7+
* @date 2022-06-07
8+
* @copyright MIT LICENSE
9+
* https://github.com/Simple-XX/SimpleRenderer
10+
* @par change log:
11+
* <table>
12+
* <tr><th>Date<th>Author<th>Description
13+
* <tr><td>2022-06-07<td>Zone.N<td>迁移到 doxygen
14+
* </table>
15+
*/
16+
17+
#ifndef SIMPLERENDER_SRC_INCLUDE_VECTOR_HPP_
18+
#define SIMPLERENDER_SRC_INCLUDE_VECTOR_HPP_
19+
20+
#include <glm/glm.hpp>
21+
22+
#define GLM_ENABLE_EXPERIMENTAL
23+
#include <glm/gtx/string_cast.hpp>
24+
25+
#include "log_system.h"
26+
27+
namespace simple_renderer {
28+
using Vector2f = glm::vec2;
29+
using Vector3f = glm::vec3;
30+
using Vector4f = glm::vec4;
31+
} // namespace simple_renderer
32+
33+
/**
34+
* spdlog 输出 Vector3f 实现
35+
*/
36+
template <>
37+
struct fmt::formatter<simple_renderer::Vector3f> : fmt::formatter<std::string> {
38+
auto format(const simple_renderer::Vector3f &vector,
39+
fmt::format_context &ctx) const -> decltype(ctx.out()) {
40+
std::string vector_string = glm::to_string(vector);
41+
42+
// Format and output the string
43+
return fmt::format_to(ctx.out(), "\n{}", vector_string);
44+
}
45+
};
46+
47+
/**
48+
* spdlog 输出 Vector4f 实现
49+
*/
50+
template <>
51+
struct fmt::formatter<simple_renderer::Vector4f> : fmt::formatter<std::string> {
52+
auto format(const simple_renderer::Vector4f &vector,
53+
fmt::format_context &ctx) const -> decltype(ctx.out()) {
54+
std::string vector_string = glm::to_string(vector);
55+
56+
// Format and output the string
57+
return fmt::format_to(ctx.out(), "\n{}", vector_string);
58+
}
59+
};
60+
61+
#endif /* SIMPLERENDER_SRC_INCLUDE_VECTOR_HPP_ */

‎src/light.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
namespace simple_renderer {
2424

25-
const glm::vec3 Light::kDefaultPos = glm::vec3(0, 0, 0);
26-
const glm::vec3 Light::kDefaultDir = glm::vec3(0, 0, -1);
25+
const Vector3f Light::kDefaultPos = Vector3f(0, 0, 0);
26+
const Vector3f Light::kDefaultDir = Vector3f(0, 0, -1);
2727
const Color Light::kDefaultColor = Color::kWhite;
2828

2929
Light::Light(const std::string &name) : name_(name) {

‎src/log_system.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616

1717
#include "log_system.h"
1818

19-
#include <iostream>
20-
2119
#include <spdlog/async.h>
2220
#include <spdlog/sinks/basic_file_sink.h>
2321
#include <spdlog/sinks/rotating_file_sink.h>
2422
#include <spdlog/sinks/stdout_color_sinks.h>
2523
#include <spdlog/spdlog.h>
2624

25+
#include <iostream>
26+
2727
namespace simple_renderer {
2828

2929
LogSystem::LogSystem(const std::string &log_file_path, size_t lig_file_max_size,
3030
size_t log_file_max_count) {
3131
spdlog::init_thread_pool(65536, 1);
32-
// std::string log_file_paths = "./logs/simple_renderer.log";
32+
// std::string log_file_paths = "./logs/simple_renderer.log";
3333
auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
3434
auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
3535
log_file_path, lig_file_max_size, log_file_max_count);

‎src/model.cpp

Lines changed: 66 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616

1717
#include "model.hpp"
18-
#include <glm/gtc/matrix_transform.hpp>
1918

19+
#include <glm/gtc/matrix_transform.hpp>
2020
#include <utility>
2121

2222
#define TINYOBJLOADER_IMPLEMENTATION
@@ -33,30 +33,34 @@ Model::Vertex::Vertex(Coord coord, Normal normal, TextureCoord texture_coord,
3333
texture_coord_(std::move(texture_coord)),
3434
color_(color) {}
3535

36-
auto Model::Vertex::operator*(const glm::mat4 &tran) const -> Model::Vertex {
37-
Vertex vertex(*this);
36+
auto Model::Vertex::operator*(const Matrix4f &tran) const -> Model::Vertex {
37+
Vertex vertex(*this);
3838

39-
// Convert the 3D coordinate to a 4D vector by adding a 1.0 w-component
40-
glm::vec4 res4(coord_, 1.0f);
39+
// Convert the 3D coordinate to a 4D vector by adding a 1.0 w-component
40+
// 将 3D 坐标转换为 4D 向量,通过添加 1.0 w-分量
41+
Vector4f res4(coord_, 1.0f);
4142

42-
// Apply the transformation matrix
43-
glm::vec4 ret4 = tran * res4;
43+
// Apply the transformation matrix
44+
// 应用变换矩阵
45+
Vector4f ret4 = tran * res4;
4446

45-
// Update the vertex coordinates with the transformed values
46-
vertex.coord_ = glm::vec3(ret4);
47+
// Update the vertex coordinates with the transformed values
48+
// 使用变换后的值更新顶点坐标
49+
vertex.coord_ = Vector3f(ret4);
4750

48-
/// @todo 变换法线
51+
/// @todo 变换法线
4952

50-
return vertex;
53+
return vertex;
5154
}
5255

5356
Model::Face::Face(const Model::Vertex &v0, const Model::Vertex &v1,
5457
const Model::Vertex &v2, Material material)
5558
: v0_(v0), v1_(v1), v2_(v2), material_(std::move(material)) {
5659
// 计算法向量
5760
// 如果 obj 内包含法向量,直接使用即可
58-
if (glm::normalize(v0.normal_) != glm::vec3(0.0f) && glm::normalize(v1.normal_) != glm::vec3(0.0f) &&
59-
glm::normalize(v2.normal_) != glm::vec3(0.0f)) {
61+
if (glm::normalize(v0.normal_) != Vector3f(0.0f) &&
62+
glm::normalize(v1.normal_) != Vector3f(0.0f) &&
63+
glm::normalize(v2.normal_) != Vector3f(0.0f)) {
6064
normal_ = glm::normalize((v0.normal_ + v1.normal_ + v2.normal_));
6165
}
6266
// 手动计算
@@ -68,18 +72,18 @@ Model::Face::Face(const Model::Vertex &v0, const Model::Vertex &v1,
6872
}
6973
}
7074

71-
auto Model::Face::operator*(const glm::mat4 &tran) const -> Model::Face {
72-
auto face(*this);
73-
face.v0_ = face.v0_ * tran;
74-
face.v1_ = face.v1_ * tran;
75-
face.v2_ = face.v2_ * tran;
75+
auto Model::Face::operator*(const Matrix4f &tran) const -> Model::Face {
76+
auto face(*this);
77+
face.v0_ = face.v0_ * tran;
78+
face.v1_ = face.v1_ * tran;
79+
face.v2_ = face.v2_ * tran;
7680

77-
// Calculate the transformed normal
78-
glm::vec3 v2v0 = face.v2_.coord_ - face.v0_.coord_;
79-
glm::vec3 v1v0 = face.v1_.coord_ - face.v0_.coord_;
80-
face.normal_ = glm::normalize(glm::cross(v2v0, v1v0));
81+
// Calculate the transformed normal
82+
Vector3f v2v0 = face.v2_.coord_ - face.v0_.coord_;
83+
Vector3f v1v0 = face.v1_.coord_ - face.v0_.coord_;
84+
face.normal_ = glm::normalize(glm::cross(v2v0, v1v0));
8185

82-
return face;
86+
return face;
8387
}
8488

8589
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)
146150

147151
// 如果法线索引存在(即 idx.normal_index >= 0),
148152
// 则构造并保存,否则设置为 0
149-
Normal normal = glm::vec3(0.0f);
153+
Normal normal = Vector3f(0.0f);
150154
if (idx.normal_index >= 0) {
151155
normal = Normal(attrib.normals[3 * size_t(idx.normal_index) + 0],
152156
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)
155159

156160
// 如果贴图索引存在(即 idx.texcoord_index >= 0),
157161
// 则构造并保存,否则设置为 0
158-
TextureCoord texture_coord = glm::vec3(0.0f);
162+
TextureCoord texture_coord = Vector3f(0.0f);
159163
if (idx.texcoord_index >= 0) {
160164
texture_coord = TextureCoord(
161165
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)
175179
auto material = Material();
176180
if (!materials.empty()) {
177181
material.shininess = materials[shape_index].shininess;
178-
material.ambient = glm::vec3(materials[shape_index].ambient[0],
182+
material.ambient = Vector3f(materials[shape_index].ambient[0],
179183
materials[shape_index].ambient[1],
180184
materials[shape_index].ambient[2]);
181-
material.diffuse = glm::vec3(materials[shape_index].diffuse[0],
185+
material.diffuse = Vector3f(materials[shape_index].diffuse[0],
182186
materials[shape_index].diffuse[1],
183187
materials[shape_index].diffuse[2]);
184-
material.specular = glm::vec3(materials[shape_index].specular[0],
188+
material.specular = Vector3f(materials[shape_index].specular[0],
185189
materials[shape_index].specular[1],
186190
materials[shape_index].specular[2]);
187191
}
@@ -193,7 +197,7 @@ Model::Model(const std::string &obj_path, const std::string &mtl_path)
193197
Normalize();
194198
}
195199

196-
auto Model::operator*(const glm::mat4 &tran) const -> Model {
200+
auto Model::operator*(const Matrix4f &tran) const -> Model {
197201
auto model = Model(*this);
198202

199203
for (auto &i : model.faces_) {
@@ -216,23 +220,23 @@ std::pair<Model::Coord, Model::Coord> Model::GetMaxMinXYX() {
216220
std::numeric_limits<float>::max());
217221

218222
for (const auto &i : faces_) {
219-
auto curr_max_x = std::max(i.v0_.coord_.x,
220-
std::max(i.v1_.coord_.x, i.v2_.coord_.x));
221-
auto curr_max_y = std::max(i.v0_.coord_.y,
222-
std::max(i.v1_.coord_.y, i.v2_.coord_.y));
223-
auto curr_max_z = std::max(i.v0_.coord_.z,
224-
std::max(i.v1_.coord_.z, i.v2_.coord_.z));
223+
auto curr_max_x =
224+
std::max(i.v0_.coord_.x, std::max(i.v1_.coord_.x, i.v2_.coord_.x));
225+
auto curr_max_y =
226+
std::max(i.v0_.coord_.y, std::max(i.v1_.coord_.y, i.v2_.coord_.y));
227+
auto curr_max_z =
228+
std::max(i.v0_.coord_.z, std::max(i.v1_.coord_.z, i.v2_.coord_.z));
225229

226230
max.x = curr_max_x > max.x ? curr_max_x : max.x;
227231
max.y = curr_max_y > max.y ? curr_max_y : max.y;
228232
max.z = curr_max_z > max.z ? curr_max_z : max.z;
229233

230-
auto curr_min_x = std::min(i.v0_.coord_.x,
231-
std::min(i.v1_.coord_.x, i.v2_.coord_.x));
232-
auto curr_min_y = std::min(i.v0_.coord_.y,
233-
std::min(i.v1_.coord_.y, i.v2_.coord_.y));
234-
auto curr_min_z = std::min(i.v0_.coord_.z,
235-
std::min(i.v1_.coord_.z, i.v2_.coord_.z));
234+
auto curr_min_x =
235+
std::min(i.v0_.coord_.x, std::min(i.v1_.coord_.x, i.v2_.coord_.x));
236+
auto curr_min_y =
237+
std::min(i.v0_.coord_.y, std::min(i.v1_.coord_.y, i.v2_.coord_.y));
238+
auto curr_min_z =
239+
std::min(i.v0_.coord_.z, std::min(i.v1_.coord_.z, i.v2_.coord_.z));
236240

237241
min.x = curr_min_x < min.x ? curr_min_x : min.x;
238242
min.y = curr_min_y < min.y ? curr_min_y : min.y;
@@ -244,31 +248,34 @@ std::pair<Model::Coord, Model::Coord> Model::GetMaxMinXYX() {
244248
void Model::Normalize() {
245249
auto [max, min] = GetMaxMinXYX();
246250

247-
// Compute the dimensions of the bounding box
248-
auto x = std::abs(max.x) + std::abs(min.x);
249-
auto y = std::abs(max.y) + std::abs(min.y);
250-
auto z = std::abs(max.z) + std::abs(min.z);
251+
// Compute the dimensions of the bounding box
252+
auto x = std::abs(max.x) + std::abs(min.x);
253+
auto y = std::abs(max.y) + std::abs(min.y);
254+
auto z = std::abs(max.z) + std::abs(min.z);
251255

252-
// Calculate the scaling factor
253-
auto scale = 1.0f / std::max(x, std::max(y, z));
256+
// Calculate the scaling factor
257+
auto scale = 1.0f / std::max(x, std::max(y, z));
254258

255-
// Create the scaling matrix
256-
glm::mat4 scale_matrix = glm::scale(glm::mat4(1.0f), glm::vec3(scale, scale, scale));
259+
// Create the scaling matrix
260+
Matrix4f scale_matrix =
261+
glm::scale(Matrix4f(1.0f), Vector3f(scale, scale, scale));
257262

258-
// Calculate the center of the bounding box
259-
glm::vec3 center = glm::vec3((max.x + min.x) / 2.0f, (max.y + min.y) / 2.0f, (max.z + min.z) / 2.0f);
263+
// Calculate the center of the bounding box
264+
Vector3f center = Vector3f((max.x + min.x) / 2.0f, (max.y + min.y) / 2.0f,
265+
(max.z + min.z) / 2.0f);
260266

261-
// Create the translation matrix
262-
glm::mat4 translation_matrix = glm::translate(glm::mat4(1.0f), -center);
267+
// Create the translation matrix
268+
Matrix4f translation_matrix = glm::translate(Matrix4f(1.0f), -center);
263269

264-
// Combine the scaling and translation matrices
265-
glm::mat4 normalization_matrix = scale_matrix * translation_matrix;
270+
// Combine the scaling and translation matrices
271+
Matrix4f normalization_matrix = scale_matrix * translation_matrix;
266272

267-
// Debug output
268-
SPDLOG_DEBUG("normalization_matrix: \n{}", glm::to_string(normalization_matrix));
273+
// Debug output
274+
SPDLOG_DEBUG("normalization_matrix: \n{}",
275+
glm::to_string(normalization_matrix));
269276

270-
// Apply the normalization matrix to the model
271-
*this = *this * normalization_matrix;
277+
// Apply the normalization matrix to the model
278+
*this = *this * normalization_matrix;
272279
}
273280

274281
} // namespace simple_renderer

‎src/shader_base.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ ShaderVertexIn::ShaderVertexIn(const Model::Face &face) : face_(face) {}
2222

2323
ShaderVertexOut::ShaderVertexOut(const Model::Face &face) : face_(face) {}
2424

25-
ShaderFragmentIn::ShaderFragmentIn(const glm::vec3 &barycentric_coord,
26-
const glm::vec3 &normal,
27-
const glm::vec3 &light, const Color &color0,
25+
ShaderFragmentIn::ShaderFragmentIn(const Vector3f &barycentric_coord,
26+
const Vector3f &normal,
27+
const Vector3f &light, const Color &color0,
2828
const Color &color1, const Color &color2)
2929
: barycentric_coord_(barycentric_coord),
3030
normal_(normal),
@@ -37,9 +37,9 @@ ShaderFragmentOut::ShaderFragmentOut(const bool &is_need_draw,
3737
const Color &color)
3838
: is_need_draw_(is_need_draw), color_(color) {}
3939

40-
ShaderData::ShaderData(const glm::mat4 &model_matrix,
41-
const glm::mat4 &view_matrix,
42-
const glm::mat4 &project_matrix)
40+
ShaderData::ShaderData(const Matrix4f &model_matrix,
41+
const Matrix4f &view_matrix,
42+
const Matrix4f &project_matrix)
4343
: model_matrix_(model_matrix),
4444
view_matrix_(view_matrix),
4545
project_matrix_(project_matrix) {}

‎src/simple_renderer.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void SimpleRenderer::DrawTriangle(const ShaderBase &shader, const Light &light,
160160
}
161161
auto [is_inside, barycentric_coord] = GetBarycentricCoord(
162162
v0.coord_, v1.coord_, v2.coord_,
163-
glm::vec3(static_cast<float>(x), static_cast<float>(y), 0));
163+
Vector3f(static_cast<float>(x), static_cast<float>(y), 0));
164164
// 如果点在三角形内再进行下一步
165165
if (!is_inside) {
166166
continue;
@@ -222,37 +222,37 @@ void SimpleRenderer::DrawModel(const ShaderBase &shader, const Light &light,
222222
}
223223

224224
/// @todo 巨大性能开销
225-
auto SimpleRenderer::GetBarycentricCoord(const glm::vec3 &p0, const glm::vec3 &p1,
226-
const glm::vec3 &p2, const glm::vec3 &pa)
227-
-> std::pair<bool, glm::vec3> {
225+
auto SimpleRenderer::GetBarycentricCoord(const Vector3f &p0, const Vector3f &p1,
226+
const Vector3f &p2, const Vector3f &pa)
227+
-> std::pair<bool, Vector3f> {
228228
auto p1p0 = p1 - p0;
229229
auto p2p0 = p2 - p0;
230230
auto pap0 = pa - p0;
231231

232232
auto deno = (p1p0.x * p2p0.y - p1p0.y * p2p0.x);
233233
if (std::abs(deno) < std::numeric_limits<decltype(deno)>::epsilon()) {
234-
return std::pair<bool, const glm::vec3>{false, glm::vec3()};
234+
return std::pair<bool, const Vector3f>{false, Vector3f()};
235235
}
236236

237237
auto s = (p2p0.y * pap0.x - p2p0.x * pap0.y) / deno;
238238
if ((s > 1) || (s < 0)) {
239-
return std::pair<bool, const glm::vec3>{false, glm::vec3()};
239+
return std::pair<bool, const Vector3f>{false, Vector3f()};
240240
}
241241

242242
auto t = (p1p0.x * pap0.y - p1p0.y * pap0.x) / deno;
243243
if ((t > 1) || (t < 0)) {
244-
return std::pair<bool, const glm::vec3>{false, glm::vec3()};
244+
return std::pair<bool, const Vector3f>{false, Vector3f()};
245245
}
246246

247247
if ((1 - s - t > 1) || (1 - s - t < 0)) {
248-
return std::pair<bool, const glm::vec3>{false, glm::vec3()};
248+
return std::pair<bool, const Vector3f>{false, Vector3f()};
249249
}
250250

251-
return std::pair<bool, const glm::vec3>{true, glm::vec3(1 - s - t, s, t)};
251+
return std::pair<bool, const Vector3f>{true, Vector3f(1 - s - t, s, t)};
252252
}
253253

254254
auto SimpleRenderer::InterpolateDepth(float depth0, float depth1, float depth2,
255-
const glm::vec3 &_barycentric_coord)
255+
const Vector3f &_barycentric_coord)
256256
-> float {
257257
auto depth = depth0 * _barycentric_coord.x;
258258
depth += depth1 * _barycentric_coord.y;

‎test/CMakeLists.txt

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ enable_testing()
1414
include(GoogleTest)
1515

1616
include_directories(
17-
${SimpleRenderer_SOURCE_DIR}/include
17+
${SimpleRenderer_SOURCE_DIR}/src/include
1818
)
1919

2020
list(APPEND DEFAULT_TEST_COMPILE_OPTIONS
@@ -38,26 +38,14 @@ link_libraries(
3838
SimpleRenderer
3939
)
4040

41-
# Check if the platform is macOS
42-
if(APPLE)
43-
# Enable RPATH support on macOS
44-
set(CMAKE_MACOSX_RPATH 1)
4541

46-
# Set the RPATH to look for frameworks relative to the executable path
47-
set(CMAKE_INSTALL_RPATH "/Library/Frameworks")
48-
49-
# Ensure that RPATH is used in the build
50-
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
51-
endif()
52-
53-
54-
# add_subdirectory(unit_test)
42+
add_subdirectory(unit_test)
5543
#add_subdirectory(integration_test)
5644
add_subdirectory(system_test)
5745

58-
# add_coverage_target(
59-
# DEPENDS unit_test
60-
# SOURCE_DIR ${SimpleRenderer_SOURCE_DIR}
61-
# BINARY_DIR ${SimpleRenderer_BINARY_DIR}
62-
# EXCLUDE_DIR ${SimpleRenderer_SOURCE_DIR}/3rd/*
63-
# )
46+
add_coverage_target(
47+
DEPENDS unit_test
48+
SOURCE_DIR ${SimpleRenderer_SOURCE_DIR}
49+
BINARY_DIR ${SimpleRenderer_BINARY_DIR}
50+
EXCLUDE_DIR ${SimpleRenderer_SOURCE_DIR}/3rd/*
51+
)

‎test/system_test/camera.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class Camera {
3333
/// 光照名称
3434
std::string name_ = "default light name";
3535
/// 位置
36-
glm::vec3 pos_;
36+
Vector3f pos_;
3737
/// 方向
38-
glm::vec3 dir_;
38+
Vector3f dir_;
3939

4040
/**
4141
* 构造函数

‎test/system_test/main.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,18 @@ int main(int argc, char **argv) {
5959
// objs.emplace_back(obj_path + "/african_head.obj");
6060
objs.emplace_back(obj_path + "/utah-teapot/utah-teapot.obj");
6161

62-
63-
auto matrix =
64-
glm::mat4(1.0f);
65-
glm::mat4 scale_matrix = glm::scale(glm::mat4(1.0f), glm::vec3(500.0f, 500.0f, 500.0f));
66-
67-
// Translation matrix
68-
glm::mat4 translation_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(kWidth / 2.0f, kHeight / 2.0f, 0.0f));
69-
70-
// Combined transformation matrix
71-
matrix = translation_matrix * scale_matrix;
62+
auto matrix = simple_renderer::Matrix4f(1.0f);
63+
simple_renderer::Matrix4f scale_matrix =
64+
glm::scale(simple_renderer::Matrix4f(1.0f),
65+
simple_renderer::Vector3f(500.0f, 500.0f, 500.0f));
66+
67+
// Translation matrix
68+
simple_renderer::Matrix4f translation_matrix = glm::translate(
69+
simple_renderer::Matrix4f(1.0f),
70+
simple_renderer::Vector3f(kWidth / 2.0f, kHeight / 2.0f, 0.0f));
71+
72+
// Combined transformation matrix
73+
matrix = translation_matrix * scale_matrix;
7274

7375
// 矩阵运算的顺序
7476
// 归一化

0 commit comments

Comments
 (0)
Please sign in to comment.