play with camera settings and fix handedness of camera
This commit is contained in:
parent
82b38d4501
commit
94231f6a5b
@ -3,6 +3,7 @@ package eu.jonahbauer.raytracing;
|
||||
import eu.jonahbauer.raytracing.material.DielectricMaterial;
|
||||
import eu.jonahbauer.raytracing.material.LambertianMaterial;
|
||||
import eu.jonahbauer.raytracing.material.MetallicMaterial;
|
||||
import eu.jonahbauer.raytracing.math.Vec3;
|
||||
import eu.jonahbauer.raytracing.render.Camera;
|
||||
import eu.jonahbauer.raytracing.render.Color;
|
||||
import eu.jonahbauer.raytracing.render.ImageFormat;
|
||||
@ -15,12 +16,17 @@ import java.nio.file.Path;
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException {
|
||||
var scene = new Scene(
|
||||
new Sphere(0, -100.5, 1, 100, new LambertianMaterial(new Color(0.8, 0.8, 0.0))),
|
||||
new Sphere(0, 0, 1.2, 0.5, new LambertianMaterial(new Color(0.1, 0.2, 0.5))),
|
||||
new Sphere(-1, 0, 1, 0.5, new DielectricMaterial(0.75)),
|
||||
new Sphere(1, 0, 1, 0.5, new MetallicMaterial(new Color(0.8, 0.6, 0.2), 1.0))
|
||||
new Sphere(0, -100.5, - 1, 100, new LambertianMaterial(new Color(0.8, 0.8, 0.0))),
|
||||
new Sphere(0, 0, - 1.2, 0.5, new LambertianMaterial(new Color(0.1, 0.2, 0.5))),
|
||||
new Sphere(-1, 0, - 1, 0.5, new DielectricMaterial(1.5)),
|
||||
new Sphere(-1, 0, - 1, 0.4, new DielectricMaterial(1 / 1.5)),
|
||||
new Sphere(1, 0, - 1, 0.5, new MetallicMaterial(new Color(0.8, 0.6, 0.2), 1.0))
|
||||
);
|
||||
var camera = new Camera(
|
||||
800, 450,
|
||||
16d / 9 * 2, 2d,
|
||||
Vec3.ZERO.plus(Vec3.UNIT_Z).plus(Vec3.UNIT_Y).minus(Vec3.UNIT_X), new Vec3(0.25, -0.5, - 1)
|
||||
);
|
||||
var camera = new Camera(512, 2, 16 / 9d);
|
||||
|
||||
var image = camera.render(scene);
|
||||
ImageFormat.PNG.write(image, Path.of("scene-" + System.currentTimeMillis() + ".png"));
|
||||
|
@ -44,6 +44,12 @@ public record Vec3(double x, double y, double z) {
|
||||
return Optional.of(rOutPerp.plus(rOutParallel));
|
||||
}
|
||||
|
||||
public static @NotNull Vec3 rotate(@NotNull Vec3 vec, @NotNull Vec3 axis, double angle) {
|
||||
Vec3 vxp = axis.cross(vec);
|
||||
Vec3 vxvxp = axis.cross(vxp);
|
||||
return vec.plus(vxp.times(Math.sin(angle))).plus(vxvxp.times(1 - Math.cos(angle)));
|
||||
}
|
||||
|
||||
public @NotNull Vec3 plus(@NotNull Vec3 b) {
|
||||
return new Vec3(this.x + b.x, this.y + b.y, this.z + b.z);
|
||||
}
|
||||
@ -60,6 +66,10 @@ public record Vec3(double x, double y, double z) {
|
||||
return new Vec3(this.x * b, this.y * b, this.z * b);
|
||||
}
|
||||
|
||||
public @NotNull Vec3 neg() {
|
||||
return new Vec3(-x, -y, -z);
|
||||
}
|
||||
|
||||
public @NotNull Vec3 cross(@NotNull Vec3 b) {
|
||||
return new Vec3(
|
||||
this.y() * b.z() - b.y() * this.z(),
|
||||
|
@ -49,7 +49,7 @@ public final class Camera {
|
||||
* @param viewportHeight the viewport height
|
||||
*/
|
||||
public Camera(int width, int height, double viewportWidth, double viewportHeight) {
|
||||
this(width, height, viewportWidth, viewportHeight, Vec3.ZERO, Vec3.UNIT_Z);
|
||||
this(width, height, viewportWidth, viewportHeight, Vec3.ZERO, Vec3.UNIT_Z.neg());
|
||||
}
|
||||
|
||||
public Camera(
|
||||
@ -73,11 +73,11 @@ public final class Camera {
|
||||
var d = direction.unit();
|
||||
var dXZ = direction.withY(0).unit();
|
||||
|
||||
var viewportU = new Vec3(dXZ.z(), 0, - dXZ.x()); // perpendicular to dXZ in xz-plane
|
||||
var viewportU = new Vec3(- dXZ.z(), 0, dXZ.x()); // perpendicular to dXZ in xz-plane
|
||||
var viewportV = d.cross(viewportU); // perpendicular to viewportU and direction
|
||||
|
||||
viewportU = viewportU.times(viewportWidth); // vector along the width of the viewport
|
||||
viewportV = viewportV.times(- viewportHeight); // vector along the height of the viewport
|
||||
viewportV = viewportV.times(viewportHeight); // vector along the height of the viewport
|
||||
|
||||
this.pixelU = viewportU.div(width);
|
||||
this.pixelV = viewportV.div(height);
|
||||
|
Loading…
x
Reference in New Issue
Block a user