【问题标题】:Ray Tracer, shadow ray produces black circle?Ray Tracer,阴影射线产生黑圈?
【发布时间】:2020-06-21 01:05:39
【问题描述】:

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

这是阴影射线的代码

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;
    }
}

【问题讨论】:

    标签: c++ 3d shadow raytracing


    【解决方案1】:

    好的,知道了。

    对于黑色圆圈,您必须测试阴影射线的距离是否小于点与光源之间的距离。此外,对于距离,不应规范化 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);
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-26
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      • 1970-01-01
      相关资源
      最近更新 更多