Java 光线跟踪器中的递归反射不工作
由于我的光线跟踪器中的某些原因,如果我试图限制光线跟踪器中递归调用的数量,我的反射将不起作用 这是我的代码:Java 光线跟踪器中的递归反射不工作,java,recursion,reflection,raytracing,Java,Recursion,Reflection,Raytracing,由于我的光线跟踪器中的某些原因,如果我试图限制光线跟踪器中递归调用的数量,我的反射将不起作用 这是我的代码: public static int recursionLevel; public int maxRecursionLevel; public Colour shade(Intersection intersection, ArrayList<Light> lights, Ray incidenceRay) { recursionLevel++; if(recu
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);
}
}
公共静态int递归级别;
公共整数maxRecursionLevel;
公共颜色阴影(交叉路口、阵列列表灯、光线入射){
递归级别++;
如果(recursionLevel问题是您使用的是recursionLevel
作为全局状态,但它实际上应该是本地状态。此外,每次递归调用shade()
,您都会将其递增两次,只递减一次。我将按照以下方式重构您的代码:
- 删除
递归级别
全局
- 将
recursionLevel
参数添加到shade()
方法中
- 保持
if(recursionLevel
检查
- 删除递归调用
shade()
- 修改对
shade()
的递归调用,使其调用shade(…,递归级别+1)