Java 正在写入光线跟踪器,并且无法使图像正确居中?
我正在用Java编写一个光线跟踪器,我已经到了可以创建对象、光线、测试交点然后给像素上色的地步。我还完成了一些基本的抗锯齿处理。我的问题是,如果a创建一个球体,它应该位于世界的中心(即Java 正在写入光线跟踪器,并且无法使图像正确居中?,java,raytracing,Java,Raytracing,我正在用Java编写一个光线跟踪器,我已经到了可以创建对象、光线、测试交点然后给像素上色的地步。我还完成了一些基本的抗锯齿处理。我的问题是,如果a创建一个球体,它应该位于世界的中心(即0.0,0.0,0.0),然后绘制图像,我会得到这样的图片 当红圈在图像中间时, 主要方法 Orthographic Projector.java 我有一种感觉,在这个过程中,我将x和y混合在一起,这使图像旋转,但我一直无法找到问题所在。如果您想看到更多我的代码,我很乐意展示。在SimpleSampler.jav
0.0
,0.0
,0.0
),然后绘制图像,我会得到这样的图片
当红圈在图像中间时,
主要方法 Orthographic Projector.java我有一种感觉,在这个过程中,我将x和y混合在一起,这使图像旋转,但我一直无法找到问题所在。如果您想看到更多我的代码,我很乐意展示。在SimpleSampler.java中:
Point2D point = new Point2D(
x - RayTracer.world.viewPlane.width / 2 + (col + 0.5) / samples,
y - RayTracer.world.viewPlane.width / 2 + (row + 0.5) / samples);
两个坐标都使用宽度。也许你应该使用宽度和高度我向上帝发誓,我已经盯着这个代码好几天了。非常感谢你,我觉得自己太笨了。这并不比我不得不调试一段代码的时候更糟糕。代码中的两个变量I(大写I)和l(小写l)错误地混合在一起。
public static void trace(int x, int y) {
Colour colour = new Colour();
//int colour = RayTracer.world.backgroundColour.toInteger();
for (int col = 0; col < sampler.samples; col++) {
for (int row = 0; row < sampler.samples; row++) {
Point2D point = sampler.sample(row, col, x, y);
Ray ray = projector.createRay(point);
double min = Double.MAX_VALUE;
Colour tempColour = new Colour();
for (int i = 0; i < world.worldObjects.size(); i++) {
double temp = world.worldObjects.get(i).intersect(ray);
if (temp != 0 && temp < min) {
min = temp;
tempColour = world.worldObjects.get(i).colour;
}
}
colour.add(tempColour);
}
}
colour.divide(sampler.samples*sampler.samples);
image.buffer.setRGB(x, y, colour.toInteger());
}
public class World {
public ViewPlane viewPlane;
public ArrayList<Renderable> worldObjects;
public Colour backgroundColour;
public World(int width, int height, double size) {
viewPlane = new ViewPlane(width, height, size);
backgroundColour = new Colour(0.0f, 0.0f, 0.0f);
worldObjects = new ArrayList<Renderable>();
worldObjects.add(new Sphere(new Point3D(0.0, 0.0, 0.0), 50, new Colour(1.0f, 0.0f, 0.0f)));
//worldObjects.add(new Sphere(new Point3D(-150.0, 0.0, 0.0), 50, new Colour(1.0f, 0.0f, 0.0f)));
//worldObjects.add(new Sphere(new Point3D(0.0, -540.0, 0.0), 50, new Colour(0.0f, 1.0f, 0.0f)));
}
}
public class SimpleSampler extends Sampler {
public SimpleSampler(int samples) {
this.samples = samples;
}
public Point2D sample(int row, int col, int x, int y) {
Point2D point = new Point2D(
x - RayTracer.world.viewPlane.width / 2 + (col + 0.5) / samples,
y - RayTracer.world.viewPlane.width / 2 + (row + 0.5) / samples);
return point;
}
}
public class OrthographicProjector extends Projector{
public Ray createRay(Point2D point) {
Ray ray = new Ray();
ray.origin = new Point3D(
RayTracer.world.viewPlane.size * point.x,
RayTracer.world.viewPlane.size * point.y,
100);
ray.direction = new Vector3D(0.0, 0.0, -1.0);
return ray;
}
}
Point2D point = new Point2D(
x - RayTracer.world.viewPlane.width / 2 + (col + 0.5) / samples,
y - RayTracer.world.viewPlane.width / 2 + (row + 0.5) / samples);