首页 > 解决方案 > 奇怪的体素锥追踪结果

问题描述

我目前正在使用 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 纹理。

在常规的 Phong 着色器(在其下计算体素值)下,场景如下所示: 场景

作为参考,这是体素图 (tex3D) (128x128x128) 在可视化时的样子:体素图

现在我们来解决我遇到的实际问题。如果我将上面的着色器应用于场景,我会得到以下结果:

对于非常小的锥角(apperture_angle=0.01),我大致得到了您可能期望的结果:体素化场景基本上在每个表面上垂直“反射”: 小光圈

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

大胃口

我本来希望得到与之前的结果更相似的结果,只是不那么高光。相反,我得到的主要是每个对象的轮廓以镜面反射的方式反射,轮廓内偶尔会有一些像素。考虑到这是场景中的“间接照明”,即使我添加了直接照明,它看起来也不会很好。

我为 max_dist、current_dist 等尝试了不同的值,并拍摄了几个圆锥而不是一个。结果仍然相似,如果不是更糟的话。

有人知道我在这里做错了什么,以及如何获得实际的远程逼真的间接光吗?

我怀疑 textureLod 函数会以某种方式对任何高于 0 的 LOD 级别产生错误的结果,但我无法确认这一点。

标签: c++openglglslshadervoxel

解决方案


3D 纹理的 Mipmap 未正确生成。此外,在 vlevel 上没有硬帽,导致所有 textureLod 调用都返回 #000000 颜色,访问任何高于 1 的 mipmaplevel。


推荐阅读