Dandelion 1.1.2
A light-weight 3D builder for educational usage
载入中...
搜索中...
未找到
graphics_interface.h
浏览该文件的文档.
1#ifndef DANDELION_RENDER_GRAPHICS_INTERFACE_H
2#define DANDELION_RENDER_GRAPHICS_INTERFACE_H
3
4#include <memory>
5#include <functional>
6#include <queue>
7
8#include <Eigen/Core>
9#include <Eigen/Geometry>
10#include <spdlog/spdlog.h>
11
12#include "../scene/scene.h"
13
14/*!
15 * \file render/graphics_interface.h
16 * \ingroup rendering
17 * \~chinese
18 * \brief 一些公用的渲染管线接口。
19 */
20
21/*!
22 * \ingroup rendering
23 * \~chinese
24 * \brief 顶点着色器的输入和输出单位。
25 */
27{
28 /*! \~chinese 顶点世界坐标系坐标 */
29 Eigen::Vector4f world_position;
30 /*! \~chinese 顶点视口坐标系坐标 */
31 Eigen::Vector4f viewport_position;
32 /*! \~chinese 顶点法线 */
33 Eigen::Vector3f normal;
34};
35
36/*!
37 * \ingroup rendering
38 * \~chinese
39 * \brief 片元着色器的输入单位。
40 *
41 * “片元”相当于未着色的像素,每个片元数据中均包含为该片元着色所需的局部信息(全局信息则位于
42 * `Uniforms` 中)。
43 */
45{
46 /*! \~chinese 世界坐标系下的位置 */
47 Eigen::Vector3f world_pos;
48 /*! \~chinese 世界坐标系下的法向量 */
49 Eigen::Vector3f world_normal;
50 /*! \~chinese 屏幕空间坐标 */
51 int x, y;
52 /*! \~chinese 当前片元的深度 */
53 float depth;
54 /*! \~chinese 当前片元的颜色 */
55 Eigen::Vector3f color;
56};
57
58/*!
59 * \ingroup rendering
60 * \~chinese
61 * \brief 计算顶点的各项属性几何变化。
62 *
63 * 首先是将顶点坐标变换到投影平面,再进行视口变换;
64 * 其次是将法线向量变换到世界坐标系
65 *
66 * \param payload 输入时顶点和法线均为模型坐标系
67 * 输出时顶点经过视口变换变换到了屏幕空间,法线向量则为世界坐标系
68 */
70
71/*!
72 * \ingroup rendering
73 * \~chinese
74 * \brief 使用 Blinn Phong 着色模型计算每个片元(像素)的颜色。
75 *
76 * \param payload 装的是相机坐标系下的片元位置以及法向量
77 * \param material 当前片元所在物体的材质,包括环境光、漫反射和镜面反射系数等
78 * \param lights 当前场景中的所有光源
79 * \param camera 离线渲染所用的相机
80 *
81 */
82Eigen::Vector3f phong_fragment_shader(
83 const FragmentShaderPayload& payload, const GL::Material& material,
84 const std::list<Light>& lights, const Camera& camera
85);
86
87/*!
88 * \ingroup rendering
89 * \~chinese
90 * \brief 用于选择 buffer 的类型
91 */
92enum class BufferType
93{
94 Color = 1,
95 Depth = 2
96};
97
98inline BufferType operator|(BufferType a, BufferType b)
99{
100 return BufferType((int)a | (int)b);
101}
102
103inline BufferType operator&(BufferType a, BufferType b)
104{
105 return BufferType((int)a & (int)b);
106}
107
108/*!
109 * \ingroup utils
110 * \~chinese
111 * \brief 自旋锁
112 *
113 * 一个比较高效的自旋锁实现,做了局部自旋和主动退避优化。
114 */
116{
117public:
118
119 /*! \~chinese 加锁。 */
120 void lock();
121 /*! \~chinese 解锁。 */
122 void unlock();
123
124private:
125
126 /*! \~chinese 内部用于实现锁的原子变量。 */
127 std::atomic_flag locked;
128};
129
130/*!
131 * \ingroup rendering
132 * \~chinese
133 * \brief 一个最简化的 Frame Buffer 。
134 *
135 * 要渲染一帧,通常至少需要一个 color buffer 用于输出颜色、一个 depth buffer 用于记录深度。
136 */
138{
139public:
140
141 /*! \~chinese 初始化一个 frame buffer 。 */
142 FrameBuffer(int width, int height);
143
144 /*! \~chinese Frame buffer 对应图像的宽度和高度 */
145 int width, height;
146
147 /*!
148 * \~chinese
149 * \brief 设置当前像素点的颜色
150 *
151 * 根据计算出的 index,对 frame buffer 中的 color buffer 和
152 * depth buffer 进行填充
153 *
154 * \param index 填充位置对应的一维索引
155 * \param depth 当前着色点计算出的深度
156 * \param color 当前着色点计算出的颜色值
157 */
158 void set_pixel(int index, float depth, const Eigen::Vector3f& color);
159
160 /*!
161 * \~chinese
162 * \brief 初始化指定类型的 buffer。
163 *
164 * color buffer 最后会传递给 rendering_res,所以可以直接初始化为背景颜色
165 * depth buffer 则可以初始化为最大值
166 *
167 * \param buff 根据传入的 buffer 类型,初始化相应类型的 buffer
168 */
169 void clear(BufferType buff);
170
171 /*! \~chinese color buffer 的存储元素为 Eigen::Vector3f,范围在 [0,255],三个分量分别表示 (R,G,B)
172 */
173 std::vector<Eigen::Vector3f> color_buffer;
174 /*! \~chinese depth buffer 也可以叫做 z-buffer,用于判断像素点相较于观察点的前后关系 */
175 std::vector<float> depth_buffer;
176
177private:
178
179 /*! \~chinese 用于在深度测试和着色时对相应位置的像素加锁 */
180 std::vector<SpinLock> spin_locks;
181};
182
183/*!
184 * \ingroup rendering
185 * \~chinese
186 * \brief 用于存储 RasterizerRenderer 所需的全局变量。
187 */
189{
190 /*! \~chinese 当前渲染物体的 MVP 矩阵 */
191 static Eigen::Matrix4f MVP;
192 /*! \~chinese 当前渲染物体的model.inverse().transpose() */
193 static Eigen::Matrix4f inv_trans_M;
194 ///@{
195 /*! \~chinese 当前成像平面的长和宽 */
196 static int width;
197 static int height;
198 ///@}
199
200 /*! \~chinese 渲染物体的材质 */
202 /*! \~chinese 场景内的光源 */
203 static std::list<Light>& lights;
204 /*! \~chinese 当前渲染视角的相机 */
205 static Camera& camera;
206};
207
208/*!
209 * \ingroup rendering
210 * \~chinese
211 * \brief 存放实现渲染管线所需的一些全局数据。
212 */
214{
215 /*! \~chinese 保护队列的互斥锁 */
216 static std::mutex vertex_queue_mutex;
217 /*! \~chinese 保护队列的互斥锁 */
218 static std::mutex rasterizer_queue_mutex;
219 /*! \~chinese vertex shader的输出队列 */
220 static std::queue<VertexShaderPayload> vertex_shader_output_queue;
221 /*! \~chinese rasterizer的输出队列 */
222 static std::queue<FragmentShaderPayload> rasterizer_output_queue;
223
224 /*! \~chinese 标识顶点着色器是否全部执行完毕。 */
225 volatile static bool vertex_finish;
226 /*! \~chinese 标识三角形是否全部被光栅化。 */
227 volatile static bool rasterizer_finish;
228 /*! \~chinese 标识片元着色器是否全部执行完毕。 */
229 volatile static bool fragment_finish;
230
231 /*! \~chinese 渲染使用的 frame buffer 。 */
233};
234
235#endif // DANDELION_RENDER_GRAPHICS_INTERFACE_H
一个最简化的 Frame Buffer 。
定义 graphics_interface.h:138
void set_pixel(int index, float depth, const Eigen::Vector3f &color)
设置当前像素点的颜色
定义 render_engine.cpp:52
std::vector< float > depth_buffer
定义 graphics_interface.h:175
FrameBuffer(int width, int height)
定义 rasterizer_renderer.cpp:47
std::vector< Eigen::Vector3f > color_buffer
定义 graphics_interface.h:173
int width
定义 graphics_interface.h:145
void clear(BufferType buff)
初始化指定类型的 buffer。
定义 render_engine.cpp:42
std::vector< SpinLock > spin_locks
定义 graphics_interface.h:180
自旋锁
定义 graphics_interface.h:116
std::atomic_flag locked
定义 graphics_interface.h:127
void unlock()
定义 render_engine.cpp:37
void lock()
定义 render_engine.cpp:26
Eigen::Vector3f phong_fragment_shader(const FragmentShaderPayload &payload, const GL::Material &material, const std::list< Light > &lights, const Camera &camera)
使用 Blinn Phong 着色模型计算每个片元(像素)的颜色。
定义 render/shader.cpp:38
BufferType
用于选择 buffer 的类型
定义 graphics_interface.h:93
VertexShaderPayload vertex_shader(const VertexShaderPayload &payload)
计算顶点的各项属性几何变化。
定义 render/shader.cpp:14
包含场景的类。
表示观察点的相机,既可以用于预览视角,也可以用于渲染视角。
定义 camera.h:24
存放实现渲染管线所需的一些全局数据。
定义 graphics_interface.h:214
static volatile bool fragment_finish
定义 graphics_interface.h:229
static std::mutex vertex_queue_mutex
定义 graphics_interface.h:216
static std::queue< VertexShaderPayload > vertex_shader_output_queue
定义 graphics_interface.h:220
static std::mutex rasterizer_queue_mutex
定义 graphics_interface.h:218
static std::queue< FragmentShaderPayload > rasterizer_output_queue
定义 graphics_interface.h:222
static volatile bool vertex_finish
定义 graphics_interface.h:225
static volatile bool rasterizer_finish
定义 graphics_interface.h:227
static FrameBuffer frame_buffer
定义 graphics_interface.h:232
片元着色器的输入单位。
定义 graphics_interface.h:45
Eigen::Vector3f color
定义 graphics_interface.h:55
float depth
定义 graphics_interface.h:53
int x
定义 graphics_interface.h:51
Eigen::Vector3f world_normal
定义 graphics_interface.h:49
Eigen::Vector3f world_pos
定义 graphics_interface.h:47
物体材质。
定义 gl.hpp:227
用于存储 RasterizerRenderer 所需的全局变量。
定义 graphics_interface.h:189
static int width
定义 graphics_interface.h:196
static Eigen::Matrix4f inv_trans_M
定义 graphics_interface.h:193
static Eigen::Matrix4f MVP
定义 graphics_interface.h:191
static std::list< Light > & lights
定义 graphics_interface.h:203
static int height
定义 graphics_interface.h:197
static Camera & camera
定义 graphics_interface.h:205
static GL::Material & material
定义 graphics_interface.h:201
顶点着色器的输入和输出单位。
定义 graphics_interface.h:27
Eigen::Vector4f world_position
定义 graphics_interface.h:29
Eigen::Vector4f viewport_position
定义 graphics_interface.h:31
Eigen::Vector3f normal
定义 graphics_interface.h:33