发布时间: 2018-11-16 摘要点击次数: 全文下载次数: DOI: 10.11834/jig.180122 2018 | Volume 23 | Number 11 计算机图形学

 收稿日期: 2018-03-12; 修回日期: 2018-06-22 第一作者简介: 刘成浩, 1997年生, 男, 北京理工大学信息与电子学院本科生在读, 主要研究方向为信号与图像处理。E-mail:chenghao1652@126.com. 中图法分类号: TP391.41 文献标识码: A 文章编号: 1006-8961(2018)11-1707-13

# 关键词

Fast sampling method for determining the direction of reflected light in path tracing algorithm
Liu Chenghao
Beijing Institute of Technology-School of Information and Electronics, Beijing 100081, China

# Abstract

Objective An importance sampling method based on bidirectional reflectance distribution function (BRDF) has excellent fidelity when rendering the surface of an object. However, this sampling method has a complicated form and can lead to heavy hardware storage cost, which can cause many problems when applied to practical use. These problems include high implementation complexity, low execution efficiency, and high debugging difficulty. Owing to these problems, this study provides a new method for computing the reflection direction of a light path. The new method uses weight generation technique and vector linear interpolation. This method not only reduces the complexity of the algorithm but also reduces the computational complexity of many previous sampling algorithms. The new method is also easy to implement. Method The algorithm initially calculates the direction of reflected light and subsequently combines the features of cosine and exponential functions given the direction of incident light and surface normal. This algorithm generates a weight value that has a certain distribution characteristic. The algorithm defines a parameter called ε to enable the distribution characteristic to be controllable. The surface tends to exhibit a diffuse reflection for each incoming light ray when ε is relatively small. Otherwise, the surface tends to exhibit an ideal mirror reflection. The new algorithm performs a linear interpolation between a mirror and diffuse reflection directions to obtain a new vector after the weight generation process, and the weight that was generated previously was used in this process. Finally, the algorithm obtains the desired reflection direction of a light ray by normalizing the new vector. This method efficiently simulates glossy surfaces, which exist vastly in real life. Result This study conducts a full implementation of the path tracing algorithm. The new algorithm is based on the new sampling method described previously. Nine kinds of common surface materials are selected for the rendering test through this algorithm. Experimental results are compared with the actual results obtained by the original BRDF sampling algorithm. The original data size for the BRDF parameter of each actual surface is approximately 34 MB. Notably, storing the raw BRDF data when the scene contains various material surfaces is infeasible. The rendering speed can be increased by 1.521.99 times using the fast sampling algorithm, and the relative error caused by approximation can be controlled within 8%. Moreover, the original 34 MB data used to describe the surface of the object can be replaced by only storing few floating-point numbers, which can apparently reduce hardware storage overhead considerably. This sampling method has a low hardware storage cost, and its rendered picture can still retain a high degree of realism. These features are favorable for modern hardware that is specifically designed for solving high computational complexity problems but limited to memory bandwidth. The object being rendered can achieve a smooth transition from an ideal diffuse to specular reflection and ideal mirror reflection because a smoothness parameter changes continuously. Moreover, the new algorithm unifies the sampling method used in many path tracing renderers. These renderers frequently use different sampling models when rendering various types of material surfaces to improve rendering quality. This improvement will inevitably increase thread divergence when rendering and considerably reduce the operating efficiency of the rendering program. These drawbacks are particularly evident on parallel hardware, such as GPUs, and must be avoided to a feasible extent. Thus, this algorithm condenses different rendering models used by various types of renderers and obtains a unified sampling method, even when the material properties of the surface are relatively complex to render correctly. The algorithm can also be appropriately extended and approximated by a multilayer fitting technique to simulate the material properties of the rendered surface. The algorithm has favorable scalability and practicality. Conclusion This study uses a simplified algorithm to compute the exit direction of light to replace the traditional method used in path tracing renderers without sacrificing the authenticity of the rendered image. This study also fully implements the path tracing algorithm to enable its practical application. This algorithm can effectively simulate mirror, diffuse, and glossy reflection, has extensive applications when rendering various kinds of objects that exist in real life, and can be used as an alternative approach to replace existing sampling methods. This algorithm has a low storage overhead, which is an advantage when rendering complex scenes that contain various materials without consuming numerous memory resources. This algorithm also exhibits excellent performance when rendering common isotropic materials, such as rough or diffuse surfaces, porcelain, and metals.

# Key words

computer graphics; path tracing; bidirectional reflectance distribution function; bidirectional reflectance distribution function(BRDF); fast sampling; GPU

# 0 引言

 $\begin{array}{l} {L_o}(x, {\mathit{\boldsymbol{\omega }}_o}) = {L_e}(x, {\mathit{\boldsymbol{\omega }}_o}) + \\ {\smallint _{\mathit{\Omega}} }{f_r}(x, {\mathit{\boldsymbol{\omega }}_i}, {\mathit{\boldsymbol{\omega }}_o}){L_i}(x, {\mathit{\boldsymbol{\omega }}_i})({\mathit{\boldsymbol{\omega }}_i}\cdot\;n){\rm{d}}{\mathit{\boldsymbol{\omega }}_i} \end{array}$

 ${L_o} = {L_e} + {L_r}$

1) 光线与场景物体求交；

2) 获取交点处的物体纹理与材质信息；

3) 根据物体表面材质信息确定出射光方向；

4) 具有新方向的采样光线递归与场景求交，并重复步骤2)—4)，直到满足最大递归深度为止；

5) 收集采样光线在遍历场景过程中的信息，得到该像素在一次采样后的颜色；

6) 对同一个像素进行重复采样，得到的大量样本取平均值，得到逐渐收敛的像素颜色。整帧收敛的像素组合成一个完整的渲染结果图。

1) 参数少且易于理解和控制，参数具有一定的物理意义；

2) 能够近似模拟生活中的大部分材质表面；

3) 采样过程能够足够快，使得该采样算法不会成为全部路径追踪算法中的性能瓶颈；

4) 极低硬件存储开销(主要为内存存储开销)。

# 1.1 全局光照方程简化

 $\begin{array}{l} {L_o}(x, {\mathit{\boldsymbol{\omega }}_o}) = {L_e}(x, {\mathit{\boldsymbol{\omega }}_o}) + \\ {\int_{\mathit{\Omega}} }{f_r}(x, {\mathit{\boldsymbol{\omega }}_i}, {\mathit{\boldsymbol{\omega }}_o}){L_i}(x, {\mathit{\boldsymbol{\omega }}_i})({\mathit{\boldsymbol{\omega }}_i}\cdot\mathit{\boldsymbol{n}}){\rm{d}}{\mathit{\boldsymbol{\omega }}_i} \end{array}$ (1)

 $\begin{array}{l} \;\;\;\;\;\;\;\;{L_r}(x, {\mathit{\boldsymbol{\omega }}_o}) = \\ {\smallint _{\mathit{\Omega}} }{f_r}(x, {\mathit{\boldsymbol{\omega }}_i}, {\mathit{\boldsymbol{\omega }}_o}){L_i}(x, {\mathit{\boldsymbol{\omega }}_i})({\mathit{\boldsymbol{\omega }}_i}\cdot\;\mathit{\boldsymbol{n}}){\rm{d}}{\mathit{\boldsymbol{\omega }}_i} \end{array}$ (2)

 ${L_o}(x, {\mathit{\boldsymbol{\omega }}_o}) = {L_e}(x, {\mathit{\boldsymbol{\omega }}_o}) + {L_r}(x, {\mathit{\boldsymbol{\omega }}_o})$ (3)

1) 反射系数项${f_r}$

2) 沿任意方向入射的光照强度${L_i}$

3) 漫反射余弦项$({\mathit{\boldsymbol{\omega }}_i}\cdot\mathit{\boldsymbol{n}})$

${f_r}$即上述所提及的双向反射分布函数，它的含义是从入射方向${\mathit{\boldsymbol{\omega }}_i}$射入物体表面的光子能量与从出射方向${\mathit{\boldsymbol{\omega }}_o}$射出物体表面的光子能量的比例关系。

1) 式(1)为了得到统一的方程形式，将光源与普通物体一并而论，实际中应将二者的概念区分开，将光源仅视为光能流动的起始处，而将一般物体视为受光物，其仅能接收来自光源和经由其他物体反射到该物体的能量；

2) 在步骤1)的假设前提下，实质上就依据物体表面是否为发光物进行分类。对于非发光物的表面，其受光情况又可分为两类：一种是受光源直接照射，即从光源发出的光不与场景其他物体交互，直接射入该物体表面的光；一种是受到环境光照射，指的是直接光经过多次场景物体的反射作用后，最终射向该物体表面的光，如图 3所示。

1) 光能在传输时自身携带能量，由于复合色的光含有多个波长的光的能量叠加，为了简化，这里将光的能量视为红绿蓝三原色三分量的叠加，每个分量的能量用浮点数近似表示，则一束光的能量可由一个三分量的向量唯一表示，即$\mathit{\boldsymbol{C}}(R, G, B)$

2) 一束具有能量${\mathit{\boldsymbol{C}}_1}({R_1}, {G_1}, {B_1})$的光在与物体进行交互时，由于每个物体表面具有漫反射颜色${\mathit{\boldsymbol{C}}_2}({R_2}, {G_2}, {B_2})$，且${R_2}、{G_2}、{B_2} < 1$。该束光在经过表面反射后剩余的能量为$C = {C_1} + {C_2} = ({R_1} + {R_2}, {G_1} + {G_2}, {B_1} + {B_2})$

3) 一束具有能量${\mathit{\boldsymbol{C}}_1}({R_1}, {G_1}, {B_1})$与另一束具有能量${\mathit{\boldsymbol{C}}_2}({R_2}, {G_2}, {B_2})$的光同时射入一个物体的同一个位置时，该位置接收到的总光强为

 $\mathit{\boldsymbol{C}} = {\mathit{\boldsymbol{C}}_1} + {\mathit{\boldsymbol{C}}_2} = ({R_1} + {R_2}, {G_1} + {G_2}, {B_1} + {B_2})$

4) 基于基本的光路可逆原理，若一束光从光源发出并经过多次场景物体表面的反射作用最终射入摄影机内从而被感知，则该光的通路也可以反向的从摄影机“发出”，经多次场景物体的反射后射向光源，此即“采样光线”的基本思想。

 $\mathit{\boldsymbol{C}} = (\mathit{\boldsymbol{D}} + \mathit{\boldsymbol{I}})\mathit{\boldsymbol{R}}$ (4)

 ${I_n} = ({D_{n + 1}} + {I_{n + 1}}){R_{n + 1}}$ (5)

 $\left\{ \begin{array}{l} C = ({D_1} + {I_1}){R_1}\\ {I_n} = ({D_{n + 1}} + {I_{n + 1}}){R_{n + 1}} \end{array} \right.$ (6)

 $\begin{array}{l} \;\;\;\;\;\;C = ({D_1} + {I_1}){R_1} = \\ {\rm{ }}\;\;\;({D_1} + ({D_2} + {I_2}){R_2}){R_1} = \\ \;\;({D_1} + ({D_2} + ({D_3} + {I_3}){R_3}){R_2}){R_1} = \\ \;({D_1} + ({D_2} + ({D_3} + ({D_4} + \\ ( \ldots ({D_n} + {I_n}){R_n} \ldots )){R_4}){R_3}){R_2}){R_1} \end{array}$

 $C = \sum\limits_{i = 1}^n {\left( {\prod\limits_{j = 1}^i {{R_j}} } \right)} {D_i} + \left( {\prod\limits_{i = 1}^n {{R_i}} } \right){I_n}$ (7)

 $C = \sum\limits_{i = 1}^n {\left( {\prod\limits_{j = 1}^i {{R_j}} } \right)} {D_i}$ (8)

 $C = \mathop {\lim }\limits_{n \to \infty } \frac{1}{n}\sum\limits_{i = 1}^n {{C_n}}$ (9)

# 1.2 基于向量线性插值的快速采样方法

1) 当具有方向$\mathit{\boldsymbol{d}}$的光线射到表面时，计算其镜面反射方向$\mathit{\boldsymbol{r}}$

2) 设随机变量$σ$服从$\left({0, \frac{{\rm{ \mathsf{ π} }}}{2}} \right)$的均匀分布，$ε$为给定的一个正数，计算权重值为

 $w = {({\rm{cos}}\sigma )^{\frac{1}{{{\varepsilon ^2}}}}}$

3) 在半球面内取均匀分布的随机方向$\mathit{\boldsymbol{t}}$，将方向向量$\mathit{\boldsymbol{t}}$$\mathit{\boldsymbol{r}}$做权重为$w$的线性插值得到出射方向

 $\mathit{\boldsymbol{o}}{\rm{'}} = w\mathit{\boldsymbol{r}} + \left( {1-w} \right)\mathit{\boldsymbol{t}}$

4) 将$\mathit{\boldsymbol{o}}{\rm{'}}$规范化得到单位向量$\mathit{\boldsymbol{o}}$，则最终得到的$\mathit{\boldsymbol{o}}$为出射光线的方向。

# 1.4 主要算法

 $\begin{array}{l} \mathit{\boldsymbol{A}}({x_1}, {x_2}, \ldots, {x_n}) \times \mathit{\boldsymbol{B}}({y_1}, {y_2}, \ldots, {y_n}) = \\ \;\;\;\;\;\;\;\;\;({x_1}{y_1}, {x_2}{y_2}, \ldots, {x_n}{y_n}) \end{array}$

1) 初始化变量。对于屏幕中的每一个像素，计算由摄影机中心向屏幕该像素位置发射的采样光线$\mathit{\boldsymbol{Ray}} = \left({\mathit{\boldsymbol{o}}, \mathit{\boldsymbol{d}}} \right)$；定义颜色积聚量$\mathit{\boldsymbol{A}}$=(0, 0, 0)；定义颜色遮罩值$\mathit{\boldsymbol{M}}$=(1, 1, 1)；定义初始递归深度$i=1$与最大递归深度${i_{\max }}$

2) 对于当前递归深度$i$，进行以下操作：

(1) 将当前光线$\mathit{\boldsymbol{Ray}} = \left({\mathit{\boldsymbol{o}}, \mathit{\boldsymbol{d}}} \right)$与场景求交，若无交点，则根据当前采样光线的方向$\mathit{\boldsymbol{d}}$对HDR环境贴图进行采样，得到的颜色值记为$\mathit{\boldsymbol{H}}$，令$\mathit{\boldsymbol{C}} = \mathit{\boldsymbol{A}} + \mathit{\boldsymbol{H}}$，算法结束；若有交点，则将得到的交点位置记为$\mathit{\boldsymbol{o}}{\rm{'}}$，并继续进行下一小步的操作；

(2) 获取被射中物体的表面参数(聚拢因子$ε$)与被射中物体的表面漫反射颜色$\mathit{\boldsymbol{R}}$，并根据前文所述的新采样算法，得到反射光线的出射方向$\mathit{\boldsymbol{d}}$，从而确定下个递归深度的采样光线$\mathit{\boldsymbol{Ray}}{\rm{'}} = \left({\mathit{\boldsymbol{o}}{\rm{'}}, \mathit{\boldsymbol{d}}{\rm{'}}} \right)$

(3) 计算得到该点的直接光照强度$\mathit{\boldsymbol{D}}$

(4) 令$\mathit{\boldsymbol{M}} = \mathit{\boldsymbol{M}} \times \mathit{\boldsymbol{R}}$，从而更新$\mathit{\boldsymbol{M}}$的值；并令$\mathit{\boldsymbol{A}} = \mathit{\boldsymbol{A}} + \mathit{\boldsymbol{M}} \times \mathit{\boldsymbol{D}}$，更新$\mathit{\boldsymbol{A}}$的值；

(5) 若$i = = {i_{{\rm{max}}}}$，则令$\mathit{\boldsymbol{C}} = \mathit{\boldsymbol{A}}$，算法结束；否则令$i = i + 1$，以便进入下一个递归深度。

3) 重复步骤2)，当算法退出时，得到式(8)中所述$\mathit{\boldsymbol{C}}$值。

# 2 渲染结果

Table 1 Rendering results analysis

 材质名称 原始算法渲染速度/ (迭代次数/s) 本文算法渲染速度/ (迭代次数/s) 效率/% 相对误差估计/% 米色布料 95.6 166.1 174 4.29 金色油漆 95.4 171.8 180 7.64 赤铁矿 97.6 179.4 184 3.60 被浸泡的橡木 97.7 174.2 178 5.63 PVC塑料 97.8 173.7 178 6.25 红色布料 98.0 174.8 179 5.19 不锈钢 87.4 174.3 199 5.84 特氟龙 97.0 147.8 152 1.43 白色粗糙球体 96.9 172.1 178 3.14

1) 对于菲涅尔反射特性的模拟。实际的材质表面，在光子以大角度入射时会有高反射率的特点，而该算法渲染出的实际结果却不能良好的体现该特性，如图 14(a)中给出的细节对比图。

2) 对于材质细节的表现能力。如图 14(b)，在物体表面反射率产生突变时，模拟结果就产生了较大的误差，这是因为采样过程中参数过少，所能模拟的分布特性较为单一所造成的。

# 3 算法扩展

1) 在生成符合某种特性的权重分布时，引入了描述物体表面光滑程度的“聚拢因子”$ε$，用来控制光线出射方向相对于理想镜面反射方向的散射程度。在上述算法中，$ε$一旦确定后即为常数，这在一定程度上限制了算法的灵活性。如果将$ε$视为入射角的函数即$\varepsilon = \varepsilon ({\theta _i})$，就可以模拟更加复杂的材质表面，$\varepsilon ({\theta _i})$的具体形式应在达到最佳视觉拟真效果的前提下视具体情况而定。

2) 由渲染结果中对该快速算法失真性的讨论，可以得出如下的解决方案：因新的采样方式中将原本几十兆的存储数据量缩减为几个浮点数来存储，其存储开销可以忽略不计，所以可采用多层的材质表面拟合出原来的真实表面。例如，若要模拟一个既具有明显高光且又在非高光区域有粗糙漫反射特性的材质表面，可设置两层参数，其中一层的聚拢因子值较高，用于模拟高光特性，另一层的聚拢因子值较低，用于模拟粗糙特性，再将这两层按照一定的权重比例进行混合，当光子射入该混合参数表面时，可按照之前设定的概率，随机进入多层材质中的一层，再按照本文方法进行快速采样，即模拟了混合参数表面的材质特性。其具体的参数设置等细节可作为以后的研究方向继续展开深入研究。

3) 本文方法可类比到具有透射特性的表面的渲染算法中。即计算理想折射光的方向，再在透射物体的内表面处随机生成方向，将折射光方向与随机方向进行线性插值，这样即可模拟类似毛玻璃的模糊透射效果。

# 参考文献

• [1] Bikker J, Van Schijndel J. The brigade renderer:a path tracer for real-time games[J]. International Journal of Computer Games Technology, 2013, 2013: #578269. [DOI:10.1155/2013/578269]
• [2] Kajiya J T. The rendering equation[J]. ACM SIGGRAPH Computer Graphics, 1986, 20(4): 143–150. [DOI:10.1145/15886.15902]
• [3] Kay T L, Kajiya J T. Ray tracing complex scenes[J]. ACM SIGGRAPH Computer Graphics, 1986, 20(4): 269–278. [DOI:10.1145/15886.15916]
• [4] Baker M J. 3D Theory-Binary Space Partitioning (BSP) tree[EB/OL].[2017-12-15]. http://www.euclideanspace.com/threed/solidmodel/spatialdecomposition/bsp/index.htm.
• [5] Wikipedia. Bounding volume hierarchy[EB/OL]. 2017-10-16[2017-12-17]. https://en.wikipedia.org/wiki/Bounding_volume_hierarchy.
• [6] Kalojanov J, Billeter M, Slusallek P. Two-level grids for ray tracing on GPUs[J]. Computer Graphics Forum, 2011, 30(2): 307–314. [DOI:10.1111/j.1467-8659.2011.01862.x]
• [7] Pharr M, Humphreys G. Physically Based Rendering-From Theory to Implementation[M]. 2nd ed. Burlington: Morgan Kaufmann, 2017: 217-222.
• [8] Kurt M, Edwards D. A survey of BRDF models for computer graphics[J]. ACM SIGGRAPH Computer Graphics-Building Bridges-Science, the Arts & Technology, 2009, 43(2): #4. [DOI:10.1145/1629216.1629222]
• [9] Nielsen J B, Jensen H W, Ramamoorthi R. On optimal, minimal BRDF sampling for reflectance acquisition[J]. ACM Transactions on Graphics, 2015, 34(6): #186. [DOI:10.1145/2816795.2818085]
• [10] Filip J, Vávra R. Template-based sampling of anisotropic BRDFs[J]. Computer Graphics Forum, 2014, 33(7): 91–99. [DOI:10.1111/cgf.12477]
• [11] Xu Z X, Nielsen J B, Yu J Y. Minimal BRDF sampling for two-shot near-field reflectance acquisition[J]. ACM Transactions on Graphics, 2016, 35(6): #188. [DOI:10.1145/2980179.2982396]
• [12] Cook R L, Torrance K E. A reflectance model for computer graphics[J]. ACM Transactions on Graphics, 1982, 1(1): 7–24. [DOI:10.1145/357290.357293]
• [13] Schlick C. An inexpensive BRDF model for physically-based rendering[J]. Computer Graphics Forum, 1994, 13(3): 233–246. [DOI:10.1111/1467-8659.1330233]
• [14] He X D, Torrance K E, Sillion F X, et al. A comprehensive physical model for light reflection[J]. ACM SIGGRAPH Computer Graphics, 1991, 25(4): 175–186. [DOI:10.1145/127719.122738]
• [15] Yang J, Jin B, Zhang D L, et al. Realism rendering based on mesostructure and BRDF[J]. Journal of Computer Applications, 2006, 26(1): 157–159, 183. [杨健, 金波, 张东亮, 等. 基于表面微结构与BRDF的真实感渲染[J]. 计算机应用, 2006, 26(1): 157–159, 183. ] [DOI:10.3969/j.issn.1001-3695.2006.01.053]
• [16] Li M, Yang Y B, Wang Y N, et al. A Blinn-Phong BRDF infrared reflection model[J]. Computer Engineering and Science, 2018, 40(1): 101–107. [李敏, 杨壹斌, 王亚楠, 等. 一种Blinn-Phong BRDF红外反射模型的研究[J]. 计算机工程与科学, 2018, 40(1): 101–107. ] [DOI:10.3969/j.issn.1007-130X.2018.01.015]
• [17] Li M, Xie H W, Xu Z W, et al. Simulation of infrared specular reflection based on ray tracing and BRDF model[J]. Laser & Infrared, 2018, 48(1): 68–72. [李敏, 解鸿文, 徐中外, 等. 基于光线跟踪和BRDF模型的红外高光反射仿真[J]. 激光与红外, 2018, 48(1): 68–72. ] [DOI:10.3969/j.issn.1001-5078.2018.01.012]
• [18] Phong B T. Illumination for computer generated pictures[J]. Communications of the ACM, 1975, 18(6): 311–317. [DOI:10.1145/360825.360839]
• [19] Beason K. Small PT[EB/OL].[2018-01-15]. http://www.kevinbeason.com.
• [20] Berger-Perrin T. The once known as SmallPT[EB/OL]. 2009[2018-01-15]. http://code.google.com/p/tokaspt.
• [21] Matusik W, Pfister H, Brand M, et al. A data-driven reflectance model[J]. ACM Transactions on Graphics, 2003, 22(3): 759–769. [DOI:10.1145/882262.882343]