我目前正在使用C++和OpenGL编写一个体素圆锥体跟踪渲染引擎。一切都很顺利,除了我在更宽的圆锥角上得到了相当奇怪的结果。
现在,为了测试的目的,我所做的就是垂直于碎片法线射出一个奇异的圆锥体。我只是在计算“间接光”。作为参考,下面是我正在使用的相当简单的Fragment Shader:
#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纹理。
在常规Phong着色器(在其下计算体素值)下,场景看起来如下所示:

作为参考,以下是可视化时体素贴图(tex3D) (128x128x128)的外观:

现在我们来看看我遇到的实际问题。如果我将上面的着色器应用到场景中,我会得到以下结果:
对于非常小的圆锥角(apperture_angle=0.01),我得到了大致你可能期望的结果:体素化的场景本质上是垂直于每个表面的“反射”:

现在,如果我将光圈角度增加到例如30度(apperture_angle=0.52),我会得到这个看起来非常奇怪的“波浪状”结果:

我希望得到的结果与之前的结果更相似,只是镜面反射较少。相反,我得到的主要是每个物体的轮廓,以镜面反射的方式反射,轮廓内部偶尔会有一些像素。考虑到这意味着场景中的“间接照明”,即使我添加了直接光,它也不会看起来很好。
我已经尝试了max_dist,current_dist等不同的值,以及拍摄多个圆锥体而不是只拍摄一个。结果即使不是更糟,也是相似的。
有没有人知道我在这里做错了什么,以及如何获得真正的远程逼真间接光?
我怀疑textureLod函数对高于0的任何LOD级别都会产生错误的结果,但我还无法确认这一点。
发布于 2018-11-08 23:43:40
未正确生成3D纹理的Mipmap。此外,在vlevel上没有硬性限制,导致所有textureLod调用都会返回#000000颜色,以访问高于1的任何mipmaplevel。
https://stackoverflow.com/questions/51688914
复制相似问题