Allow scenes to specify their camera

This commit is contained in:
max.nuding 2022-07-07 13:06:46 +02:00
parent d69fd86f8b
commit 19107f20c9
Failed to extract signature

View File

@ -30,7 +30,14 @@ mod perlin;
mod noise;
mod image_texture;
fn earth() -> HittableList {
// Image
const ASPECT_RATIO: f64 = 3.0 / 2.0;
const IMAGE_WIDTH: usize = 400;
const IMAGE_HEIGHT: usize = (IMAGE_WIDTH as f64 / ASPECT_RATIO) as usize;
const SAMPLES_PER_PIXEL: i32 = 100;
const MAX_DEPTH: i32 = 50;
fn earth() -> (HittableList, Camera) {
let mut world:HittableList = Vec::new();
let earth_texture = ImageTexture::new("textures/earthmap.jpg");
let earth_material = Arc::new(
@ -40,10 +47,26 @@ fn earth() -> HittableList {
radius: 2.0,
material: earth_material
}));
world
let look_from = Point3::new(13.0, 2.0, 3.0);
let look_at = Point3::new(0.0, 0.0, 0.0);
let focus_dist = 2.0;
let cam = Camera::new(
look_from,
look_at,
Vec3::new(0.0, 1.0, 0.0),
ASPECT_RATIO,
20.0,
0.0,
focus_dist,
0.0,
1.0);
(world, cam)
}
fn two_spheres() -> HittableList {
fn two_spheres() -> (HittableList, Camera) {
let mut world:HittableList = Vec::new();
let checker = CheckerTexture::colored(
Color::new(0.2, 0.3, 0.1),
@ -59,9 +82,25 @@ fn two_spheres() -> HittableList {
radius: 10.0,
material: checker_material
}));
world
let look_from = Point3::new(13.0, 2.0, 3.0);
let look_at = Point3::new(0.0, 0.0, 0.0);
let focus_dist = 2.0;
let cam = Camera::new(
look_from,
look_at,
Vec3::new(0.0, 1.0, 0.0),
ASPECT_RATIO,
20.0,
0.0,
focus_dist,
0.0,
1.0);
(world, cam)
}
fn two_perlin_spheres() -> HittableList {
fn two_perlin_spheres() -> (HittableList, Camera) {
let mut world:HittableList = Vec::new();
let noise = NoiseTexture { noise: Perlin::new(), scale: 4.0 };
let noise_material = Arc::new(Material::Lambertian(Lambertian::textured(Arc::new(noise))));
@ -75,10 +114,26 @@ fn two_perlin_spheres() -> HittableList {
radius: 2.0,
material: noise_material
}));
world
let look_from = Point3::new(13.0, 2.0, 3.0);
let look_at = Point3::new(0.0, 0.0, 0.0);
let focus_dist = 2.0;
let cam = Camera::new(
look_from,
look_at,
Vec3::new(0.0, 1.0, 0.0),
ASPECT_RATIO,
20.0,
0.0,
focus_dist,
0.0,
1.0);
(world, cam)
}
fn random_scene() -> HittableList {
fn random_scene() -> (HittableList, Camera) {
let mut world: HittableList = Vec::new();
let checker = CheckerTexture::colored(
@ -155,22 +210,10 @@ fn random_scene() -> HittableList {
material: material3
}));
world
}
fn main() {
// Image
const ASPECT_RATIO: f64 = 3.0 / 2.0;
const IMAGE_WIDTH: usize = 400;
const IMAGE_HEIGHT: usize = (IMAGE_WIDTH as f64 / ASPECT_RATIO) as usize;
const SAMPLES_PER_PIXEL: i32 = 1;
const MAX_DEPTH: i32 = 50;
let look_from = Point3::new(13.0, 2.0, 3.0);
let look_at = Point3::new(0.0, 0.0, 0.0);
let focus_dist = 2.0;
// Camera
let cam = Camera::new(
look_from,
look_at,
@ -179,12 +222,16 @@ fn main() {
20.0,
0.0,
focus_dist,
0.0,
1.0);
0.0,
1.0);
(world, cam)
}
fn main() {
// World
let scene: u8 = 2;
let world = match scene {
let (world, cam) = match scene {
0 => two_spheres(),
1 => two_perlin_spheres(),
2 => earth(),