Feat: Wide-angle camera
This commit is contained in:
@@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
use crate::vec3::Vec3;
|
use crate::vec3::Vec3;
|
||||||
use crate::ray::Ray;
|
use crate::ray::Ray;
|
||||||
|
use crate::degrees_to_radians;
|
||||||
|
|
||||||
pub struct Camera {
|
pub struct Camera {
|
||||||
pub origin: Vec3,
|
pub origin: Vec3,
|
||||||
@@ -21,10 +22,12 @@ pub struct Camera {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Camera {
|
impl Camera {
|
||||||
pub fn new() -> Camera {
|
pub fn new(vfov: f32, aspect_ratio: f32) -> Camera {
|
||||||
let aspect_ratio = 16.0 / 9.0;
|
let theta = degrees_to_radians(vfov);
|
||||||
let vp_height = 2.0;
|
let h = (theta / 2.0).tan();
|
||||||
|
let vp_height = 2.0 * h;
|
||||||
let vp_width = aspect_ratio * vp_height;
|
let vp_width = aspect_ratio * vp_height;
|
||||||
|
|
||||||
let focal_length = 1.0;
|
let focal_length = 1.0;
|
||||||
|
|
||||||
let horiz = Vec3::new(vp_width, 0.0, 0.0);
|
let horiz = Vec3::new(vp_width, 0.0, 0.0);
|
||||||
|
|||||||
55
src/main.rs
55
src/main.rs
@@ -33,63 +33,36 @@ fn main() {
|
|||||||
|
|
||||||
// world
|
// world
|
||||||
|
|
||||||
let mat_ground = Material::Lambertian{ albedo: Vec3::new(0.8, 0.8, 0.0) };
|
let R = std::f32::consts::FRAC_PI_4.cos();
|
||||||
let mat_center = Material::Lambertian{ albedo: Vec3::new(0.1, 0.2, 0.5) };
|
|
||||||
let mat_left = Material::Dielectric { index_refraction: 1.5 };
|
let mat_left = Material::Lambertian { albedo: Vec3::new(0.0, 0.0, 1.0) };
|
||||||
let mat_right = Material::Metal{ albedo: Vec3::new(0.8, 0.6, 0.2), fuzz: 0.0 };
|
let mat_right = Material::Lambertian { albedo: Vec3::new(1.0, 0.0, 0.0) };
|
||||||
|
|
||||||
let mut world = HittableList::new();
|
let mut world = HittableList::new();
|
||||||
world.add(
|
world.add(
|
||||||
Box::new(
|
Box::new(
|
||||||
Sphere{
|
Sphere {
|
||||||
center: Vec3{ x: 0.0, y: -100.5, z: -1.0 },
|
center: Vec3::new( -R, 0.0, -1.0),
|
||||||
radius: 100.0,
|
radius: R,
|
||||||
material: Some(mat_ground),
|
material: Some(mat_left),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
world.add(
|
world.add(
|
||||||
Box::new(
|
Box::new(
|
||||||
Sphere{
|
Sphere {
|
||||||
center: Vec3{ x: 0.0, y: 0.0, z: -1.0},
|
center: Vec3::new( R, 0.0, -1.0),
|
||||||
radius: 0.5,
|
radius: R,
|
||||||
material: Some(mat_center),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
world.add(
|
|
||||||
Box::new(
|
|
||||||
Sphere{
|
|
||||||
center: Vec3::new(-1.0, 0.0, -1.0),
|
|
||||||
radius: 0.5,
|
|
||||||
material: Some(mat_left), }
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
world.add(
|
|
||||||
Box::new(
|
|
||||||
Sphere{
|
|
||||||
center: Vec3::new(-1.0, 0.0, -1.0),
|
|
||||||
radius: -0.4,
|
|
||||||
material: Some(mat_left), }
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
world.add(
|
|
||||||
Box::new(
|
|
||||||
Sphere{
|
|
||||||
center: Vec3::new( 1.0, 0.0, -1.0),
|
|
||||||
radius: 0.5,
|
|
||||||
material: Some(mat_right),
|
material: Some(mat_right),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
// camera
|
// camera
|
||||||
|
|
||||||
let cam = Camera::new();
|
let cam = Camera::new(90.0, aspect_ratio);
|
||||||
|
|
||||||
// render
|
// render
|
||||||
let mut small_rng = SmallRng::from_entropy();
|
let mut small_rng = SmallRng::from_entropy();
|
||||||
@@ -139,7 +112,7 @@ fn ray_color(r: Ray, world: &dyn Hittable, depth: u32, srng: &mut SmallRng, dist
|
|||||||
return Vec3::ones() * (1.0 - t) + Vec3::new(0.5, 0.7, 1.0) * t
|
return Vec3::ones() * (1.0 - t) + Vec3::new(0.5, 0.7, 1.0) * t
|
||||||
}
|
}
|
||||||
|
|
||||||
fn degrees_to_radians(degrees: f32) -> f32 {
|
pub fn degrees_to_radians(degrees: f32) -> f32 {
|
||||||
degrees * std::f32::consts::PI / 180.0
|
degrees * std::f32::consts::PI / 180.0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user