【问题标题】:Recursive reflectance in ray tracer not working光线追踪器中的递归反射率不起作用
【发布时间】:2015-08-29 22:43:45
【问题描述】:

由于某种原因在我的光线追踪器中,如果我尝试限制光线追踪器中的递归调用次数,我的反射率不起作用。

这是我的反射率代码:

public static int recursionLevel;
public int maxRecursionLevel;
public Colour shade(Intersection intersection, ArrayList<Light> lights, Ray incidenceRay) {
    recursionLevel++;
    if(recursionLevel<maxRecursionLevel){
        Vector3D reflectedDirection = incidenceRay.direction.subtractNormal(intersection.normal.multiply(2).multiply(incidenceRay.direction.dot(intersection.normal)));

        Ray reflectiveRay = new Ray(intersection.point, reflectedDirection);

        double min = Double.MAX_VALUE;
        Colour tempColour = new Colour();

        for(int i = 0; i<RayTracer.world.worldObjects.size(); i++){
            Intersection reflectiveRayIntersection = RayTracer.world.worldObjects.get(i).intersect(reflectiveRay);
            if (reflectiveRayIntersection != null && reflectiveRayIntersection.distance<min){
                min = reflectiveRayIntersection.distance;
                recursionLevel++;
                tempColour = RayTracer.world.worldObjects.get(i).material.shade(reflectiveRayIntersection, lights, reflectiveRay);
                recursionLevel--;
            }

        }

        return tempColour;
    }else{
        return new Colour(1.0f,1.0f,1.0f);
    }

}

如果我摆脱 if 语句,它会起作用,但如果我放置太多反射对象,我会耗尽内存。我不确定是什么原因造成的。

【问题讨论】:

    标签: java recursion reflection raytracing


    【解决方案1】:

    问题是您使用recursionLevel 作为全局状态,但它确实应该是本地状态。此外,每次递归调用 shade() 时,您都会将其递增两次,而只会递减一次。我将重构您的代码如下:

    • 删除recursionLevel全局
    • recursionLevel 参数添加到您的shade() 方法中
    • 保留您的if(recursionLevel &lt; maxRecursionLevel) 支票
    • 删除围绕对shade() 的递归调用的recursionLevel 递增和递减
    • 修改对shade()的递归调用,使其调用shade(..., recursionLevel + 1)

    【讨论】:

      猜你喜欢
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 2016-06-14
      • 2017-05-03
      相关资源
      最近更新 更多