由于某些原因,在我的光线跟踪器中,如果我试图限制光线跟踪器中递归调用的数量,我的反射率就不起作用。
这是我的反射率代码:
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语句,它就能工作,不过如果我放了太多的反射对象,就会耗尽内存。我不确定是什么导致了这一切。
发布于 2015-08-29 14:43:45
问题是您正在使用recursionLevel
作为全局状态,但它实际上应该是局部状态。此外,对于每个对shade()
的递归调用,您会递增它两次,而只递减一次。我将按如下方式重构您的代码:
recursionLevel
全局方法recursionLevel
参数添加到您的shade()
方法shade(..., recursionLevel + 1)
https://stackoverflow.com/questions/29735252
复制