116 lines
3.4 KiB
Rust
116 lines
3.4 KiB
Rust
use std::time::Instant;
|
|
|
|
use anyhow::Result;
|
|
use structopt::StructOpt;
|
|
|
|
use rtchallenge::prelude::*;
|
|
|
|
use rtchallenge::{
|
|
camera::RenderStrategy,
|
|
float::consts::PI,
|
|
patterns::{BLACK_PAT, WHITE_PAT},
|
|
WHITE,
|
|
};
|
|
|
|
/// Experiment with transparency.
|
|
#[derive(StructOpt, Debug)]
|
|
#[structopt(name = "glass")]
|
|
struct Opt {
|
|
/// Strategy for casting rays into image.
|
|
#[structopt(long, default_value = "rayon")]
|
|
render_strategy: RenderStrategy,
|
|
/// Number of samples per pixel. 0 renders from the center of the pixel, 1 or more samples N
|
|
/// times randomly across the pixel.
|
|
#[structopt(short, long, default_value = "0")]
|
|
samples: usize,
|
|
/// Rendered image width in pixels.
|
|
#[structopt(short, long, default_value = "2560")]
|
|
width: usize,
|
|
/// Rendered image height in pixels.
|
|
#[structopt(short, long, default_value = "1440")]
|
|
height: usize,
|
|
}
|
|
|
|
fn main() -> Result<()> {
|
|
let start = Instant::now();
|
|
let opt = Opt::from_args();
|
|
|
|
let light1 = PointLightBuilder::default()
|
|
.position(point(-5., 5., -5.))
|
|
.intensity(WHITE)
|
|
.build()?;
|
|
let light2 = PointLightBuilder::default()
|
|
.position(point(5., 5., -5.))
|
|
.intensity([0.2, 0.2, 0.6])
|
|
.build()?;
|
|
let light3 = PointLightBuilder::default()
|
|
.position(point(0., 2., -5.))
|
|
.intensity([0.2, 0.2, 0.1])
|
|
.build()?;
|
|
|
|
let from = point(0., 0., -10.);
|
|
let to = point(0., 0., 0.);
|
|
let up = point(0., 1., 0.);
|
|
let camera = CameraBuilder::default()
|
|
.hsize(opt.width)
|
|
.vsize(opt.height)
|
|
.field_of_view(PI / 4.)
|
|
.transform(view_transform(from, to, up))
|
|
.render_strategy(opt.render_strategy)
|
|
.samples_per_pixel(opt.samples)
|
|
.build()?;
|
|
|
|
let floor = plane()
|
|
.transform(rotation_x(PI / 2.))
|
|
.material(
|
|
MaterialBuilder::default()
|
|
.color(checkers_pattern(BLACK_PAT, WHITE_PAT).build()?)
|
|
.reflective(0.5)
|
|
.build()?,
|
|
)
|
|
.build()?;
|
|
let ceiling = plane()
|
|
.transform(translation(0., 0., -20.) * rotation_x(PI / 2.))
|
|
.material(MaterialBuilder::default().color([0.2, 0.2, 0.8]).build()?)
|
|
.build()?;
|
|
|
|
let mut objects = Vec::new();
|
|
for x in 0..5 {
|
|
for y in 0..5 {
|
|
let trans = y as Float / 5.;
|
|
let index = 1. + x as Float / 5.;
|
|
dbg!(x, y, trans, index);
|
|
objects.push(
|
|
sphere()
|
|
.transform(
|
|
translation(x as Float - 2., y as Float - 2., -2.) * scaling(0.5, 0.5, 0.5),
|
|
)
|
|
.material(
|
|
MaterialBuilder::default()
|
|
.color([0.5, 0.5, 0.5])
|
|
.transparency(trans)
|
|
.refractive_index(index)
|
|
.build()?,
|
|
)
|
|
.build()?,
|
|
);
|
|
}
|
|
}
|
|
|
|
objects.push(floor);
|
|
objects.push(ceiling);
|
|
|
|
let world = WorldBuilder::default()
|
|
.lights(vec![light1, light2, light3])
|
|
.objects(objects)
|
|
.build()?;
|
|
|
|
let image = camera.render(&world);
|
|
|
|
let path = "/tmp/glass.png";
|
|
println!("saving output to {}", path);
|
|
image.write_to_file(path)?;
|
|
println!("Render time {:.3} seconds", start.elapsed().as_secs_f32());
|
|
Ok(())
|
|
}
|