gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:14:59 26 4
gpt4 key购买 nike

我正在用 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());
}

世界.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);

两个坐标都使用宽度。也许您应该使用 widthheight

关于java - 编写 Raytracer,无法使图像正确居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29493930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com