编写 Raytracer,无法使图像正确居中?

Writing a Raytracer, and cant get the image to be centred properly?

我正在 Java 中编写 Raytracer,我已经到了可以创建对象、光线、测试交点和颜色像素的地步。我还完成了一些基本的抗锯齿。我的问题是,如果创建一个球体,它应该位于世界的中心(即 0.00.00.0),然后绘制图像,我最终会得到一个这样的图片。

当红色圆圈应该在图像的中间时。

主要方法

public static void main(String[] args) {
    System.out.println("Rendering...");
    long start = System.nanoTime();

    // Setting up the size of the image to be rendered
    world = new World(1920, 1080, 1.0);
    image = new Image("image.png");
    sampler = new SimpleSampler(4);
    projector = new OrthographicProjector();

    // Main loop of program, goes through each pixel in image and assigns a colour value
    for (int y = 0; y < world.viewPlane.height; y++) {
        for (int x = 0; x < world.viewPlane.width; x++) {
            // Render pixel colour
            trace(x, y);
        }
    }

    image.saveImage("PNG");

    long end = System.nanoTime();

    System.out.print("Loop Time = " + ((end - start)/1000000000.0f));
}

追踪方法

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());
}

World.java

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)));

    }
}

SimpleSampler.java

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;
    }
}

OrthographicProjector.java

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;
    }
}

我感觉我在某处混合了 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);

两个坐标都使用 width。也许你应该使用 widthheight.