Skip to content

Commit

Permalink
make camera more configurable and try to implement some more shapes
Browse files Browse the repository at this point in the history
The extra shapes are in various states of "not working" at the moment.

Box works the best, but needs to be positioned to exist at the offset, while the cylinder rendering seeems very broken, even though the effect of the cylinder existing somehow shows on the ground sphere.

The rounded box looks like a regular box, but a lil weird.

Will work on these more later. For now planning to continue with "the next week"
  • Loading branch information
ChristopherBiscardi committed Aug 31, 2023
1 parent 5cf7f32 commit 81e1850
Show file tree
Hide file tree
Showing 18 changed files with 1,015 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -1 +1,4 @@
/target
*.ppm
.DS_Store
the-dump
2 changes: 2 additions & 0 deletions README.md
@@ -0,0 +1,2 @@
# Raytracing in Rust

Binary file added examples/all-materials-spheres.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions examples/all-materials-spheres.rs
@@ -0,0 +1,67 @@
use glam::DVec3;
use raytracer::{
camera::Camera, material::Material,
shapes::sphere::Sphere,
};
use std::io;

fn main() -> io::Result<()> {
let mut world = vec![];

let material_ground = Material::Lambertian {
albedo: DVec3::new(0.8, 0.8, 0.0),
};
let material_center = Material::Lambertian {
albedo: DVec3::new(0.1, 0.2, 0.5),
};
let material_left = Material::Dielectric {
index_of_refraction: 1.5,
};
let material_right = Material::Metal {
albedo: DVec3::new(0.8, 0.6, 0.2),
fuzz: 0.0,
};

world.push(Sphere {
center: DVec3::new(0.0, -100.5, -1.0),
radius: 100.0,
material: material_ground,
});
world.push(Sphere {
center: DVec3::new(0.0, 0.0, -1.0),
radius: 0.5,
material: material_center,
});
world.push(Sphere {
center: DVec3::new(-1.0, 0.0, -1.0),
radius: 0.5,
material: material_left.clone(),
});
world.push(Sphere {
center: DVec3::new(-1.0, 0.0, -1.0),
radius: -0.4,
material: material_left,
});
world.push(Sphere {
center: DVec3::new(1.0, 0.0, -1.0),
radius: 0.5,
material: material_right,
});

let camera = Camera::init()
.image_width(800)
.aspect_ratio(16.0 / 9.0)
.look_from(DVec3::new(-2., 2., 1.))
.look_at(DVec3::NEG_Z)
.vup(DVec3::Y)
// .focus_dist(10.0)
// .defocus_angle(0.0)
.samples_per_pixel(500)
.max_depth(50)
.build();

camera
.render_to_disk("all-materials-spheres", world)?;

Ok(())
}
71 changes: 71 additions & 0 deletions examples/all-shapes.rs
@@ -0,0 +1,71 @@
use glam::DVec3;
use raytracer::{
camera::Camera,
material::Material,
shapes::{
a_box, cylinder::Cylinder, sphere::Sphere, Shapes,
},
};
use std::io;

fn main() -> io::Result<()> {
let mut world = vec![];

let material_ground = Material::Lambertian {
albedo: DVec3::new(0.8, 0.8, 0.0),
};
let material_center = Material::Lambertian {
albedo: DVec3::new(0.1, 0.2, 0.5),
};
let material_left = Material::Dielectric {
index_of_refraction: 1.5,
};
let material_right = Material::Metal {
albedo: DVec3::new(0.8, 0.6, 0.2),
fuzz: 0.0,
};

world.push(Shapes::Sphere(Sphere {
center: DVec3::new(0.0, -100.5, -1.0),
radius: 100.0,
material: material_ground,
}));
world.push(Shapes::Box(a_box::Box {
center: DVec3::new(0.0, 0.0, -1.0),
size: DVec3::splat(0.2),
material: material_center,
}));
world.push(Shapes::Sphere(Sphere {
center: DVec3::new(-1.0, 0.0, -1.0),
radius: 0.5,
material: material_left.clone(),
}));
world.push(Shapes::Sphere(Sphere {
center: DVec3::new(-1.0, 0.0, -1.0),
radius: -0.4,
material: material_left,
}));
world.push(Shapes::Cylinder(Cylinder {
start: DVec3::splat(-1.),
end: DVec3::splat(-2.),
radius: 0.5,
material: material_right,
}));

let camera = Camera::init()
.image_width(800)
.aspect_ratio(16.0 / 9.0)
.look_from(DVec3::new(-3., 4., 2.))
.look_at(DVec3::NEG_Z)
.vup(DVec3::Y)
// .focus_dist(10.0)
// .defocus_angle(0.0)
.samples_per_pixel(500)
.max_depth(50)
.vfov(90.)
.build();

camera.render_to_disk("all-shapes", world)?;

Ok(())
}
48 changes: 48 additions & 0 deletions examples/box.rs
@@ -0,0 +1,48 @@
use glam::DVec3;
use raytracer::{
camera::Camera,
material::Material,
shapes::{
a_box, rounded_box::RoundedBox, sphere::Sphere,
Shapes,
},
};
use std::io;

fn main() -> io::Result<()> {
let mut world: Vec<Shapes> = vec![];

let material_ground = Material::Lambertian {
albedo: DVec3::new(0.8, 0.8, 0.0),
};
let material_center = Material::Lambertian {
albedo: DVec3::new(0.1, 0.2, 0.5),
};

world.push(Shapes::Sphere(Sphere {
center: DVec3::new(0.0, -100.5, -1.0),
radius: 100.0,
material: material_ground,
}));
world.push(Shapes::Box(a_box::Box {
center: DVec3::new(3.0, 0.0, -1.0),
size: DVec3::ONE * 9.,
material: material_center,
}));

let camera = Camera::init()
.image_width(600)
.aspect_ratio(16.0 / 9.0)
.look_from(DVec3::new(100., 120., 100.))
.look_at(DVec3::NEG_Z)
.vup(DVec3::Y)
// .focus_dist(10.0)
// .defocus_angle(0.0)
.samples_per_pixel(500)
.max_depth(50)
.build();

camera.render_to_disk("a-box", world)?;

Ok(())
}
49 changes: 49 additions & 0 deletions examples/cylinder.rs
@@ -0,0 +1,49 @@
use glam::DVec3;
use raytracer::{
camera::Camera,
material::Material,
shapes::{
cylinder::Cylinder, rounded_box::RoundedBox,
sphere::Sphere, Shapes,
},
};
use std::io;

fn main() -> io::Result<()> {
let mut world: Vec<Shapes> = vec![];

let material_ground = Material::Lambertian {
albedo: DVec3::new(0.8, 0.8, 0.0),
};
let material_center = Material::Lambertian {
albedo: DVec3::new(0., 0., 1.),
};

world.push(Shapes::Sphere(Sphere {
center: DVec3::new(0.0, -100.5, -1.0),
radius: 100.0,
material: material_ground,
}));
world.push(Shapes::Cylinder(Cylinder {
start: DVec3::new(0.1, 0.1, -1.),
end: DVec3::new(0.1, -0.1, -1.),
radius: 10.,
material: material_center,
}));

let camera = Camera::init()
.image_width(600)
.aspect_ratio(16.0 / 9.0)
.look_from(DVec3::new(1., 1., 10.))
.look_at(DVec3::NEG_Z)
.vup(DVec3::Y)
// .focus_dist(10.0)
// .defocus_angle(0.0)
.samples_per_pixel(100)
.max_depth(50)
.build();

camera.render_to_disk("cylinder", world)?;

Ok(())
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
125 changes: 125 additions & 0 deletions examples/raytracing-in-one-weekend-final-scene-more.rs
@@ -0,0 +1,125 @@
use glam::DVec3;
use itertools::Itertools;
use rand::prelude::*;
use raytracer::{
camera::Camera,
material::Material,
shapes::{self, a_box, sphere::Sphere, Shapes},
};
use std::io;

fn main() -> io::Result<()> {
let mut rng = rand::thread_rng();

let mut world = vec![];

world.push(Shapes::Sphere(Sphere {
center: DVec3::new(0., -1000., 0.),
radius: 1000.,
material: Material::Lambertian {
albedo: DVec3::new(0.5, 0.5, 0.5),
},
}));

for (a, b) in
(-11..11).cartesian_product(-11..11).into_iter()
{
let choose_mat = rng.gen::<f64>();
let center = DVec3::new(
a as f64 + 0.9 * rng.gen::<f64>(),
0.2,
b as f64 + 0.9 * rng.gen::<f64>(),
);

if (center - DVec3::new(4., 0.2, 0.)).length() > 0.9
{
let material = if choose_mat < 0.8 {
// diffuse
let albedo = DVec3::new(
rng.gen_range(0f64..1.),
rng.gen_range(0f64..1.),
rng.gen_range(0f64..1.),
) * DVec3::new(
rng.gen_range(0f64..1.),
rng.gen_range(0f64..1.),
rng.gen_range(0f64..1.),
);
Material::Lambertian { albedo: albedo }
} else if choose_mat < 0.95 {
// metal
let albedo = DVec3::new(
rng.gen_range(0.5..1.),
rng.gen_range(0.5..1.),
rng.gen_range(0.5..1.),
);
let fuzz = rng.gen_range(0f64..0.5);

Material::Metal { albedo, fuzz }
} else {
// glass
Material::Dielectric {
index_of_refraction: 1.5,
}
};

// if choose_mat % 0.2 == 0. {
// world.push(Shapes::Sphere(Sphere {
// center,
// radius: 0.2,
// material,
// }));
// } else {
world.push(Shapes::Box(a_box::Box {
center,
size: DVec3::splat(0.2),
material,
}))
// }
}
}

world.push(Shapes::Sphere(Sphere {
center: DVec3::new(0., 1., 0.),
radius: 1.0,
material: Material::Dielectric {
index_of_refraction: 1.5,
},
}));

world.push(Shapes::Sphere(Sphere {
center: DVec3::new(-4., 1., 0.),
radius: 1.0,
material: Material::Lambertian {
albedo: DVec3::new(0.4, 0.2, 0.1),
},
}));

world.push(Shapes::Sphere(Sphere {
center: DVec3::new(4., 1., 0.),
radius: 1.0,
material: Material::Metal {
albedo: DVec3::new(0.7, 0.6, 0.5),
fuzz: 0.0,
},
}));

let camera = Camera::init()
.image_width(400)
.aspect_ratio(16.0 / 9.0)
.look_from(DVec3::new(13., 2., 3.))
.look_at(DVec3::ZERO)
.vup(DVec3::Y)
.focus_dist(10.0)
.defocus_angle(0.0)
.samples_per_pixel(500)
.max_depth(50)
.vfov(20.)
.build();

camera.render_to_disk(
"raytracing-in-one-weekend-final-scene-more",
world,
)?;

Ok(())
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions src/main.rs → .../raytracing-in-one-weekend-final-scene.rs
Expand Up @@ -95,7 +95,7 @@ fn main() -> io::Result<()> {
});

let camera = Camera::init()
.image_width(1200)
.image_width(400)
.aspect_ratio(16.0 / 9.0)
.look_from(DVec3::new(13., 2., 3.))
.look_at(DVec3::ZERO)
Expand All @@ -104,9 +104,13 @@ fn main() -> io::Result<()> {
.defocus_angle(0.0)
.samples_per_pixel(500)
.max_depth(50)
.vfov(20.)
.build();

camera.render_to_disk(world)?;
camera.render_to_disk(
"raytracing-in-one-weekend-final-scene",
world,
)?;

Ok(())
}

0 comments on commit 81e1850

Please sign in to comment.