我目前正在使用 C++ 和 OpenGL 编写体素锥体跟踪渲染引擎。一切都进行得相当顺利,除了对于更宽的锥角我得到了相当奇怪的结果。
现在,出于测试的目的,我所做的就是垂直于片段法线射出一个单一的锥体。我只计算“间接光”。作为引用,这是我正在使用的相当简单的片段着色器:
#version 450 core
out vec4 FragColor;
in vec3 pos_fs;
in vec3 nrm_fs;
uniform sampler3D tex3D;
vec3 indirectDiffuse();
vec3 voxelTraceCone(const vec3 from, vec3 direction);
void main()
{
FragColor = vec4(0, 0, 0, 1);
FragColor.rgb += indirectDiffuse();
}
vec3 indirectDiffuse(){
// singular cone in direction of the normal
vec3 ret = voxelTraceCone(pos_fs, nrm);
return ret;
}
vec3 voxelTraceCone(const vec3 origin, vec3 dir) {
float max_dist = 1f;
dir = normalize(dir);
float current_dist = 0.01f;
float apperture_angle = 0.01f; //Angle in Radians.
vec3 color = vec3(0.0f);
float occlusion = 0.0f;
float vox_size = 128.0f; //voxel map size
while(current_dist < max_dist && occlusion < 1) {
//Get cone diameter (tan = cathetus / cathetus)
float current_coneDiameter = 2.0f * current_dist * tan(apperture_angle * 0.5f);
//Get mipmap level which should be sampled according to the cone diameter
float vlevel = log2(current_coneDiameter * vox_size);
vec3 pos_worldspace = origin + dir * current_dist;
vec3 pos_texturespace = (pos_worldspace + vec3(1.0f)) * 0.5f; //[-1,1] Coordinates to [0,1]
vec4 voxel = textureLod(tex3D, pos_texturespace, vlevel); //get voxel
vec3 color_read = voxel.rgb;
float occlusion_read = voxel.a;
color = occlusion*color + (1 - occlusion) * occlusion_read * color_read;
occlusion = occlusion + (1 - occlusion) * occlusion_read;
float dist_factor = 0.3f; //Lower = better results but higher performance hit
current_dist += current_coneDiameter * dist_factor;
}
return color;
}
tex3D 制服是体素 3d 纹理。
作为引用,这是可视化时体素贴图 (tex3D) (128x128x128) 的样子:
现在我们来看看我遇到的实际问题。如果我将上面的着色器应用到场景中,我会得到以下结果:
对于非常小的圆锥角 (apperture_angle=0.01),我大致得到您可能期望的结果:体素化场景基本上垂直“反射”在每个表面上:
现在,如果我将孔径角增加到例如 30 度 (apperture_angle=0.52),我会得到这个非常奇怪的“波浪形”外观结果:
我本以为会得到与之前的结果更相似的结果,只是不那么高光。相反,我得到的主要是每个对象的轮廓以镜面反射的方式反射,轮廓内偶尔有一些像素。考虑到这是场景中的“间接照明”,即使我添加直射光,它看起来也不会很好。
我尝试了 max_dist、current_dist 等的不同值,并尝试了多个锥体而不是一个锥体。结果仍然相似,甚至更糟。
有人知道我在这里做错了什么,以及如何获得实际的远程真实间接光吗?
我怀疑 textureLod 函数会以某种方式为任何高于 0 的 LOD 级别产生错误的结果,但我无法证实这一点。
最佳答案
3D 纹理的 Mipmap 未正确生成。 此外,vlevel 没有硬性限制,导致所有 textureLod 调用返回 #000000 颜色,访问任何高于 1 的 mipmaplevel。
关于c++ - 奇怪的体素锥体追踪结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51688914/