【发布时间】:2014-06-25 08:10:55
【问题描述】:
我正在用 java 创建一个光线追踪器,我只需要打印出我制作的三个球体中的每一个。
我创建了 3 个球体对象并将它们存储在我的 main 中的一个数组列表中,我现在将其传递给我的相机,以便我可以创建一个图像。
为此,我需要弄清楚:
- 如果交点不为空,则获取射线原点与交点之间的距离。
- 如果这是内部循环中的第一个球体,则为该距离设置一个变量 - 如果不查看此距离是否小于先前测量的距离 - 如果是,则保存此距离以与其他球体进行比较并设置最接近被评估球体索引的球体(内循环中使用的计数器通过球体)。
- 在这个内部循环之外,将像素写回缓冲图像 - 但在调用时
localReflectionModel使用最近的球体(使用您在内部循环中保存的最近球体的索引)。
代码:
for(int x = 0; x < filmResolutionX; x++) {
for(int y =0; y < filmResolutionY; y++) {
for (int z = 0; z < s.size(); z++) {
double planeX = -0.5+ x / (double) filmResolutionX;
double planeY = 0.5 - y / (double) filmResolutionY;
double planeZ = 1;
Coord3D planes = new Coord3D(planeX, planeY, planeZ);
Coord3D Origin = new Coord3D(0,0,0);
Ray plane = new Ray(Origin, planes);
Coord3D iPoint = plane.intersectionPoint(s.get(z));
Color colorSphere = new Color(0xD72448);
if (iPoint != null){
double distance = plane.getOrigin().distanceBetween(iPoint);
}
if (z == 0) {
double distanceCheck1 = plane.getOrigin().distanceBetween(iPoint);
} else if (z != 0) {
}
if (iPoint == null) {
newImage.setRGB(x, y, 0);
} else {
colorSphere = s.get(z).localReflectionModel(iPoint, l, plane);
int rgb = colorSphere.getRGB();
newImage.setRGB(x, y, rgb);
}
}
}
}
return newImage;
}
}
我在尝试比较与先前球体的距离时遇到问题。当我尝试引用变量距离或距离检查 1 时,我收到一条错误消息,指出无法将距离解析为变量
【问题讨论】:
标签: java loops distance rgb raytracing