c++ - Ray Tracer,阴影射线产生黑圈?

标签 c++ 3d shadow raytracing

enter image description here

正如您在图像中看到的那样,我在球体顶部看到了一个黑色圆圈,并且图像看起来有颗粒感。它应该更清晰,但是有这些小的黑白点。

这是阴影射线的代码

int pos = 0;

float intersect(const ray &r, vector<unique_ptr<object>> &obj)
{
    //gives closest object hit point and position;
    float closest = numeric_limits<float>::max();
    for(int j = 0; j < obj.size(); j++)
    {
        float t = obj[j]->intersect(r);
        if(t > 1e-6 && t < closest)
        {
            closest = t;
            pos = j;
        }
    }
    return closest;
}
vec color(const ray& r, vector<unique_ptr<object>> &shape,  vector<unique_ptr<Light>> &lighting, int depth)
{   
    vec background_color( .678, .847, .902);
    vec total{0.0, 0.0, 0.0};
    vec ambient{0.125, 0.125, 0.125};

    float t_near = intersect(r, shape);

    if(t_near == numeric_limits<float>::max())
            return background_color;
    else
    {
        total += ambient;
        for(int i = 0; i < lighting.size(); i++){
        total += shape[pos]->shade(lighting[i]->position(), t_near, r);//gives specular + diffuse
        vec shadow_dir = unit_vector(lighting[i]->position() - r.p_at_par(t_near));
        ray shadowRay(r.p_at_par(t_near), shadow_dir);
        float dist = shadow_dir.lenght();
        float a = intersect(shadowRay, shape);
        if(a != numeric_limits<float>::max())
                return vec(0.0, 0.0, 0.0);
        }
        return total;
    }
}

最佳答案

enter image description here好,知道了。

对于黑色圆圈,您必须测试阴影射线的距离是否小于点与光源之间的距离。此外,对于距离,不应标准化 shadow_dir。为了处理阴影相交的黑白色点,您必须将 N*bias 添加到偏差为例如 1e-4 的命中点。偏差不应该太小

        vec shadow_dir = lighting[i]->position() - r.p_at_par(t_near);
        float dist = shadow_dir.lenght();
        vec N = unit_vector(shape[pos]->normal(r, t_near));
        shadow_dir = unit_vector(shadow_dir);
        ray shadowRay(r.p_at_par(t_near) + N*1e-4, shadow_dir);
        float a = intersect(shadowRay, shape);
        if(a != numeric_limits<float>::max()){
            float m = shadowRay.p_at_par(a).lenght();
            if(a < dist)
                return vec(0.0, 0.0, 0.0);
            }
        }

关于c++ - Ray Tracer,阴影射线产生黑圈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60595516/

相关文章:

C++ 初学者 - 从命令行读取 3 个连续值的最佳方法?

C++ 翻译单元

C++模板类构造器

c++ - 根据减法运算的结果排序

wpf - 如何在像谷歌按钮这样的 wpf 按钮周围创建阴影效果

c++ - D3DXVec3Unproject 在 C++ 中不准确

r - 如何使用 R 在 3D 中绘制一组密度?

c++ - 使用 OpenGL 进行纹理映射后出现奇怪的结果

c++ - 在 OpenGL 中添加阴影

带阴影的 CSS3 3D 框