Reflection and refraction impossible without recursive ray tracing?(没有递归光线追踪就不可能反射和折射?)
问题描述
我正在使用 GLSL 计算着色器编写基于 GPU 的实时光线追踪渲染器.到目前为止,它运行得非常好,但是当涉及同时具有反射和折射时,我偶然发现了一个看似无法解决的问题.
我的逻辑告诉我,为了在物体(例如玻璃)上进行反射和折射,光线必须一分为二,一条光线从表面反射,另一条光线通过表面折射.这些光线的最终颜色将根据某个函数进行组合,并最终用作光线源自的像素的颜色.我的问题是我不能在着色器代码中分割光线,因为我必须使用递归来做到这一点.根据我的理解,着色器中的函数不能递归,因为由于与旧 GPU 硬件的兼容性问题,所有 GLSL 函数都类似于 C++ 中的内联函数.
是否可以在着色器代码中模拟或伪造递归,或者我什至可以在不使用递归的情况下同时实现反射和折射?我看不出没有递归怎么会发生,但我可能是错的.
我设法将 back-raytracing 转换为适用于 GLSL 的迭代过程评论.它离优化还很远,我还没有实现所有的物理东西(没有斯内尔定律等......),但作为概念证明它已经可以工作了.我在片段着色器和 CPU 侧代码中做所有的事情,只是以 32 位非钳位浮点纹理 的形式发送 uniforms
常量和场景GL_LUMINANCE32F_ARB
渲染只是一个QUAD
覆盖整个屏幕.
- 穿越场景
我决定将场景存储在纹理中,这样每个光线/片段都可以直接访问整个场景.纹理是 2D 但它用作 32 位浮点数的线性列表.我决定采用这种格式:
您可以添加/更改任何类型的对象.此示例仅包含单个半透明蓝色四面体.您还可以为材料属性等添加变换矩阵更多系数......
- 架构
顶点着色器只是初始化视图的角光线(开始位置和方向),它被插值,所以每个片段代表反向光线追踪过程的开始光线.
迭代反向光线追踪
所以我创建了一个静态"光线列表并使用起始光线对其进行初始化.Iteration 分两步完成,首先是反向光线追踪:
- 从第一个列表中循环遍历所有光线
- 找到最近的路口与场景...
将位置、表面法线和材料属性存储到ray struct
- 如果找到交集并且不是最后一次递归"图层添加反射/折射光线以列在最后.
还将它们的索引存储到处理过的光线 struct
现在您的光线应该包含重建颜色所需的所有交叉信息.要做到这一点:
- 向后遍历所有递归级别
- 对于匹配实际递归层的每条光线
- 计算光线颜色
所以使用你想要的光照方程.如果光线包含子元素,则根据材料属性(反射和折射系数...)将其颜色添加到结果中
现在第一条光线应该包含你想要输出的颜色.
使用的制服:
tm_eye
查看相机矩阵
aspect
查看ys/xs纵横比
n0
空空间折射指数(尚未使用)
focal_length
相机焦距
fac_siz
场景方块纹理的分辨率
fac_num
场景纹理中实际使用的浮点数
fac_txr
场景纹理的纹理单元
预览:
片段着色器包含我的调试打印,因此如果使用,您还需要纹理,请参阅 QA:
这个版本解决了一些几何、精度、域问题和错误.我实现了反射和折射,如测试射线的调试绘图所示:
在调试视图中,只有立方体是透明的,最后一条没有击中任何物体的光线被忽略.所以你可以看到光线分裂......由于全反射角度,光线在立方体内部结束并且出于速度原因我禁用了物体内部的所有反射.
用于交叉路口检测的 32 位
floats
与距离有关,因此您可以使用 64 位doubles
代替,但在这种情况下速度会显着下降.另一种选择是重写方程以使用在这种情况下更精确的相对坐标.这里是
float
着色器源:顶点:
片段:
代码尚未优化,但我想让物理首先正常工作.仍然没有实现 Fresnell,而是使用了
refl,refr
材料系数.你也可以忽略调试打印的东西(它们被
#define
封装).我为几何纹理构建了一个小类,以便我可以轻松设置场景对象.这是为预览启动场景的方式:
重要的是计算出的法线面向对象,因为它用于检测内部/外部对象交叉.
附言
如果你感兴趣,这里是我的立体 3D 背光线追踪器:
- 如何在考虑性能的情况下最好地用 C 编写体素引擎
- 此处为低代表用户存档
这里是这个Mesh"的新版本支持半球对象的光线追踪器:
- 光线追踪半球
I am writing a GPU-based real-time raytracing renderer using a GLSL compute shader. So far, it works really well, but I have stumbled into a seemingly unsolvable problem when it comes to having both reflections and refractions simultaneously.
My logic tells me that in order to have reflections and refractions on an object, such as glass, the ray would have to split into two, one ray reflects off the surface, and the other refracts through the surface. The ultimate colours of these rays would then be combined based on some function and ultimately used as the colour of the pixel the ray originated from. The problem I have is that I can't split the rays in shader code, as I would have to use recursion to do so. From my understanding, functions in a shader cannot be recursive because all GLSL functions are like inline functions in C++ due to compatibility issues with older GPU hardware.
Is it possible to simulate or fake recursion in shader code, or can I even achieve reflection and refraction simultaneously without using recursion at all? I can't see how it can happen without recursion, but I might be wrong.
解决方案I manage to convert back-raytracing to iterative process suitable for GLSL with the method suggested in my comment. It is far from optimized and I do not have all the physical stuff implemented (no Snell's law etc ...) yet but as a proof of concept it works already. I do all the stuff in fragment shader and CPU side code just send the
uniforms
constants and scene in form of 32 bit non-clamped float textureGL_LUMINANCE32F_ARB
The rendering is just singleQUAD
covering whole screen.- passing the scene
I decided to store the scene in texture so each ray/fragment has direct access to whole scene. The texture is 2D but it is used as linear list of 32 bit floats. I decided this format:
You can add/change any type of object. This example holds just single semi transparent bluish tetrahedron. You could also add transform matrices more coefficients for material properties etc ...
- Architecture
the Vertex shader just initialize corner Rays of the view (start position and direction) which is interpolated so each fragment represents start ray of back ray tracing process.
Iterative back ray tracing
So I created a "static" list of rays and init it with the start ray. The Iteration is done in two steps first the back ray tracing:
- Loop through all rays in a list from the first
- Find closest intersection with scene...
store the position, surface normal and material properties into ray
struct
- If intersection found and not last "recursion" layer add reflect/refract rays to list at the end.
also store their indexes to the processed ray
struct
Now your rays should hold all the intersection info you need to reconstruct the color. To do that:
- loop through all the recursion levels backwards
- for each of the rays matching actual recursion layer
- compute ray color
so use lighting equations you want. If the ray contains children add their color to the result based on material properties (reflective and refractive coefficients ...)
Now the first ray should contain the color you want to output.
Uniforms used:
tm_eye
view camera matrix
aspect
view ys/xs aspect ratio
n0
empty space refraction index (unused yet)
focal_length
camera focal length
fac_siz
resolution of the scene square texture
fac_num
number of floats actually used in the scene texture
fac_txr
texture unit for the scene texturePreview:
The fragment shader contains my debug prints so you will need also the texture if used see the QA:
- GLSL debug prints
ToDo:
add matrices for objects, camera etc.
add material properties (shininess, reflection/refraction coefficient)
Snell's law right now the direction of new rays are wrong ...
may be separate R,G,B to 3 start rays and combine at the end
fake SSS Subsurface scattering based on ray lengths
better implement lights (right now they are constants in a code)
implement more primitives (right now only triangles are supported)[Edit1] code debug and upgrade
I removed old source code to fit inside 30KB limit. If you need it then dig it from edit history. Had some time for more advanced debugging for this and here the result:
this version got resolved some geometrical,accuracy,domain problems and bugs. I got implemented both reflections and refractions as is shown on this debug draw for test ray:
In the debug view only the cube is transparent and last ray that does not hit anything is ignored. So as you can see the ray split ... The ray ended inside cube due to total reflection angle And I disable all reflections inside objects for speed reasons.
The 32bit
floats
for intersection detection are a bit noisy with distances so you can use 64bitdoubles
instead but the speed drops considerably in such case. Another option is to rewrite the equation to use relative coordinates which are more precise in this case of use.Here the
float
shaders source:Vertex:
Fragment:
The code is not optimized yet I wanted to have the physics working correctly first. There are still not Fresnells implemented but
refl,refr
coefficients of material are used instead.Also you can ignore the debug prints stuff (they are encapsulated by
#define
).I build a small class for the geometry texture so I can easily set up scene objects. This is how the scene was initiated for the preview:
It is important so computed normals are facing out of objects because that is used for detecting inside/outside object crossings.
P.S.
If you're interested here is my volumetric 3D back ray tracer:
- How to best write a voxel engine in C with performance in mind
- here archive for low rep users
Here newer version of this "Mesh" Raytracer supporting hemisphere objects:
- Ray tracing a Hemisphere
这篇关于没有递归光线追踪就不可能反射和折射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!