33
44#include " config.h"
55#include " shader.hpp"
6+ #include " vertex.hpp"
67
78namespace simple_renderer {
89
@@ -15,21 +16,80 @@ class Rasterizer {
1516 auto operator =(Rasterizer&& rasterizer) -> Rasterizer& = default ;
1617 ~Rasterizer () = default ;
1718
19+ /* *
20+ * @brief 构造具有指定尺寸的光栅化器
21+ * @param width 光栅化器宽度
22+ * @param height 光栅化器高度
23+ */
1824 Rasterizer (size_t width, size_t height);
1925
26+ /* *
27+ * @brief 光栅化三角形,生成片段列表
28+ * @param v0 三角形第一个顶点
29+ * @param v1 三角形第二个顶点
30+ * @param v2 三角形第三个顶点
31+ * @return 生成的片段向量
32+ */
2033 std::vector<Fragment> Rasterize (const Vertex& v0, const Vertex& v1,
2134 const Vertex& v2);
2235
36+ /* *
37+ * @brief 非分配版本:将片段直接写入调用方提供的容器
38+ *
39+ * 可选的裁剪区域为半开区间 [x0, x1) × [y0, y1)
40+ * 用于 TBR:将光栅化限制在 tile 边界内,便于复用外部 scratch 容器
41+ *
42+ * @param v0 三角形第一个顶点
43+ * @param v1 三角形第二个顶点
44+ * @param v2 三角形第三个顶点
45+ * @param x0 裁剪区域左边界(包含)
46+ * @param y0 裁剪区域上边界(包含)
47+ * @param x1 裁剪区域右边界(不包含)
48+ * @param y1 裁剪区域下边界(不包含)
49+ * @param out 输出片段容器
50+ */
51+ void RasterizeTo (const Vertex& v0, const Vertex& v1, const Vertex& v2,
52+ int x0, int y0, int x1, int y1,
53+ std::vector<Fragment>& out);
54+
55+ /* *
56+ * @brief SoA 版本:按顶点索引从 SoA 读取三角形三顶点
57+ * @param soa 结构体数组格式的顶点数据
58+ * @param i0 三角形第一个顶点索引
59+ * @param i1 三角形第二个顶点索引
60+ * @param i2 三角形第三个顶点索引
61+ * @param x0 裁剪区域左边界(包含)
62+ * @param y0 裁剪区域上边界(包含)
63+ * @param x1 裁剪区域右边界(不包含)
64+ * @param y1 裁剪区域下边界(不包含)
65+ * @param out 输出片段容器
66+ */
67+ void RasterizeTo (const VertexSoA& soa, size_t i0, size_t i1, size_t i2,
68+ int x0, int y0, int x1, int y1,
69+ std::vector<Fragment>& out);
70+
2371 private:
2472 size_t width_, height_;
2573
74+ // 透视矫正结果
75+ struct PerspectiveCorrectionResult {
76+ Vector3f corrected_barycentric;
77+ float interpolated_z;
78+ };
79+
80+ // 透视矫正helper函数
81+ PerspectiveCorrectionResult PerformPerspectiveCorrection (
82+ float w0, float w1, float w2,
83+ float z0, float z1, float z2,
84+ const Vector3f& original_barycentric) const ;
85+
2686 template <typename T>
2787 T Interpolate (const T& v0, const T& v1, const T& v2,
28- const Vector3f& barycentric_coord);
88+ const Vector3f& barycentric_coord) const ;
2989
3090 Color InterpolateColor (const Color& color0, const Color& color1,
3191 const Color& color2,
32- const Vector3f& barycentric_coord);
92+ const Vector3f& barycentric_coord) const ;
3393
3494 std::pair<bool , Vector3f> GetBarycentricCoord (const Vector3f& p0,
3595 const Vector3f& p1,
0 commit comments