Compare commits
12 Commits
4ce43e12af
...
camera-bui
| Author | SHA1 | Date | |
|---|---|---|---|
| 987bbe1c98 | |||
| f2d1a892b7 | |||
| 8e37fd8e97 | |||
| a701b9407b | |||
| 72f154510f | |||
| 3250f8e580 | |||
| f03c6280a7 | |||
| 60b4407573 | |||
| 7c43c3fb82 | |||
| 4be7ba54bb | |||
| 515f5b866a | |||
| bdc396accf |
@@ -5,6 +5,15 @@ edition = "2021"
|
|||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "rustpt_cli"
|
||||||
|
path = "src/cli_tool.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "rustpt_gui"
|
||||||
|
path = "src/gui_tool.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rand = { version = "0.8.5", features = ["small_rng"] }
|
rand = { version = "0.8.5", features = ["small_rng"] }
|
||||||
itertools = { version = "0.11.0" }
|
itertools = { version = "0.11.0" }
|
||||||
|
eframe = "0.25.0"
|
||||||
|
|||||||
BIN
hitrecord_misordering.png
Normal file
BIN
hitrecord_misordering.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.5 MiB |
75
src/cli_tool.rs
Normal file
75
src/cli_tool.rs
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
#![warn(clippy::all, rust_2018_idioms, rust_2018_compatibility)]
|
||||||
|
use rustpt::primitives::{
|
||||||
|
Vec2i,
|
||||||
|
Vec3,
|
||||||
|
};
|
||||||
|
use rustpt::scene::{
|
||||||
|
Camera,
|
||||||
|
Scene
|
||||||
|
};
|
||||||
|
|
||||||
|
use rustpt::renderer::{
|
||||||
|
Tile,
|
||||||
|
RenderProperties,
|
||||||
|
};
|
||||||
|
|
||||||
|
use rand::SeedableRng;
|
||||||
|
use rand::rngs::SmallRng;
|
||||||
|
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// image
|
||||||
|
let aspect_ratio = 3.0 / 2.0;
|
||||||
|
let image = Vec2i {
|
||||||
|
x: 400,
|
||||||
|
y: (400.0 / aspect_ratio) as i32
|
||||||
|
};
|
||||||
|
|
||||||
|
let render_config = RenderProperties {
|
||||||
|
samples: 10,
|
||||||
|
bounces: 50
|
||||||
|
};
|
||||||
|
|
||||||
|
// random generator
|
||||||
|
let mut small_rng = SmallRng::seed_from_u64(0);
|
||||||
|
|
||||||
|
// Scene (now includes camera)
|
||||||
|
let scene = Scene {
|
||||||
|
camera: Camera::new(
|
||||||
|
Vec3::new(13.0, 2.0, 3.0), // lookfrom
|
||||||
|
Vec3::zero(), // lookat
|
||||||
|
Vec3::new(0.0, 1.0, 0.0), // vup
|
||||||
|
20.0,
|
||||||
|
aspect_ratio,
|
||||||
|
0.1, // aperture
|
||||||
|
10.0, // dist_to_focus
|
||||||
|
),
|
||||||
|
world: Scene::random_world(&mut small_rng)
|
||||||
|
};
|
||||||
|
|
||||||
|
// render
|
||||||
|
// The render loop should now be a job submission mechanism
|
||||||
|
// Iterate lines, submitting them as tasks to the thread.
|
||||||
|
println!("P3\n{} {}\n255", image.x, image.y);
|
||||||
|
// TILE BASED RENDERER
|
||||||
|
// let tile = Tile::render_tile(
|
||||||
|
// Rect { x: 0, y: 0, w: image.x, h: image.y },
|
||||||
|
// image,
|
||||||
|
// &scene,
|
||||||
|
// &render_config,
|
||||||
|
// &mut small_rng
|
||||||
|
// );
|
||||||
|
// for pixel in tile.pixels.iter().rev() {
|
||||||
|
// println!("{}", pixel.print_ppm(render_config.samples));
|
||||||
|
// }
|
||||||
|
|
||||||
|
// LINE BASED RENDERER
|
||||||
|
for row in (0..image.y).rev() {
|
||||||
|
let tile = Tile::render_line(row, image, &scene, &render_config, &mut small_rng);
|
||||||
|
eprintln!("Printing scanline #{}", row);
|
||||||
|
for pixel in tile.pixels {
|
||||||
|
println!("{}", pixel.print_ppm(render_config.samples))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
eprintln!("Done!");
|
||||||
|
}
|
||||||
36
src/gui_tool.rs
Normal file
36
src/gui_tool.rs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#![warn(clippy::all, rust_2018_idioms)]
|
||||||
|
use eframe::{egui, Frame};
|
||||||
|
|
||||||
|
fn main() -> Result<(), eframe::Error> {
|
||||||
|
let options = eframe::NativeOptions {
|
||||||
|
viewport: egui::ViewportBuilder::default()
|
||||||
|
.with_inner_size([800.0, 600.0])
|
||||||
|
.with_min_inner_size([50.0, 50.0])
|
||||||
|
.with_title("RustPT GUI Tool"),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
eframe::run_native(
|
||||||
|
"app name?",
|
||||||
|
options,
|
||||||
|
Box::new(
|
||||||
|
| cc | Box::new(RtApp::new(cc))
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct RtApp;
|
||||||
|
|
||||||
|
impl RtApp {
|
||||||
|
fn new(cc: &eframe::CreationContext<'_>) -> Self {
|
||||||
|
Self::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl eframe::App for RtApp {
|
||||||
|
fn update(&mut self, ctx: &egui::Context, frame: &mut Frame) {
|
||||||
|
egui::SidePanel::left("Render Properties").show(ctx, |ui| {
|
||||||
|
ui.heading("Render Properties");
|
||||||
|
ui.label("")
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
4
src/lib.rs
Normal file
4
src/lib.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#![warn(clippy::all, rust_2018_idioms, rust_2018_compatibility)]
|
||||||
|
pub mod primitives;
|
||||||
|
pub mod scene;
|
||||||
|
pub mod renderer;
|
||||||
216
src/main.rs
216
src/main.rs
@@ -1,216 +0,0 @@
|
|||||||
|
|
||||||
mod primitives;
|
|
||||||
mod renderer;
|
|
||||||
mod scene;
|
|
||||||
|
|
||||||
use crate::primitives::Vec3;
|
|
||||||
use crate::scene::{
|
|
||||||
Camera,
|
|
||||||
Hittable,
|
|
||||||
Material,
|
|
||||||
};
|
|
||||||
use crate::renderer::RenderCommand;
|
|
||||||
|
|
||||||
use rand::{Rng, SeedableRng};
|
|
||||||
use rand::rngs::SmallRng;
|
|
||||||
use rand::distributions::Uniform;
|
|
||||||
|
|
||||||
use std::thread;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
// image
|
|
||||||
let aspect_ratio = 3.0 / 2.0;
|
|
||||||
let image = (
|
|
||||||
1920,
|
|
||||||
(1920.0 / aspect_ratio) as i32
|
|
||||||
);
|
|
||||||
let samples_per_pixel: u32 = 10;
|
|
||||||
let max_depth = 50;
|
|
||||||
|
|
||||||
// random generator
|
|
||||||
let mut small_rng = SmallRng::seed_from_u64(0);
|
|
||||||
|
|
||||||
// world
|
|
||||||
let world = random_scene(&mut small_rng);
|
|
||||||
|
|
||||||
// camera
|
|
||||||
|
|
||||||
let cam = Camera::new(
|
|
||||||
Vec3::new(13.0, 2.0, 3.0), // lookfrom
|
|
||||||
Vec3::zero(), // lookat
|
|
||||||
Vec3::new(0.0, 1.0, 0.0), // vup
|
|
||||||
20.0,
|
|
||||||
aspect_ratio,
|
|
||||||
0.1, // aperture
|
|
||||||
10.0, // dist_to_focus
|
|
||||||
);
|
|
||||||
|
|
||||||
// render
|
|
||||||
// The render loop should now be a job submission mechanism
|
|
||||||
// Iterate lines, submitting them as tasks to the thread.
|
|
||||||
println!("P3\n{} {}\n255", image.0, image.1);
|
|
||||||
let context = renderer::RenderContext {
|
|
||||||
camera: cam,
|
|
||||||
image,
|
|
||||||
max_depth,
|
|
||||||
samples_per_pixel,
|
|
||||||
world,
|
|
||||||
};
|
|
||||||
|
|
||||||
thread::scope(|s| {
|
|
||||||
let (mut dispatcher, scanline_receiver) = renderer::Dispatcher::new(&small_rng, 12);
|
|
||||||
|
|
||||||
s.spawn(move || {
|
|
||||||
for y in (0..image.1).rev() {
|
|
||||||
eprintln!("Submitting scanline: {}", y);
|
|
||||||
let job = RenderCommand::Line { line_num: y, context: context.clone() };
|
|
||||||
dispatcher.submit_job(job).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatcher.submit_job(RenderCommand::Stop).unwrap();
|
|
||||||
// ... also I happen to know there are 4 threads.
|
|
||||||
});
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Store received results in the segments buffer.
|
|
||||||
* Some will land before their previous segments and will need to be held
|
|
||||||
* until the next-to-write arrives.
|
|
||||||
*
|
|
||||||
* Elements are sorted in reverse order so that they can be popped from the
|
|
||||||
* Vec quickly.
|
|
||||||
*
|
|
||||||
* The queue is scanned every single time a new item is received. In the
|
|
||||||
* happy path where the received item is next-up, it'll be buffered, checked
|
|
||||||
* and then printed. In the case where it isn't, it'll get buffered and
|
|
||||||
* stick around for more loops. When the next-to-write finally lands, it
|
|
||||||
* means the n+1 element is up, now. If that element is already in the buffer
|
|
||||||
* we want to write it out. Hence the loop that scans the whole buffer each
|
|
||||||
* receive.
|
|
||||||
*
|
|
||||||
* TODO: There could be an up-front conditional that checks to see if the
|
|
||||||
* received item *is* the next-to-write and skip the buffering step.
|
|
||||||
* But I need to make the concept work at all, first.
|
|
||||||
*/
|
|
||||||
let mut raster_segments = Vec::<renderer::RenderResult>::new();
|
|
||||||
let mut sl_output_index = image.1-1; // scanlines count down, start at image height.
|
|
||||||
while let Ok(scanline) = scanline_receiver.recv() {
|
|
||||||
eprintln!("Received scanline: {}", scanline.line_num);
|
|
||||||
|
|
||||||
raster_segments.push(scanline);
|
|
||||||
raster_segments.sort_by( |a, b| b.cmp(a) );
|
|
||||||
|
|
||||||
loop {
|
|
||||||
if raster_segments.len() == 0 { break; } // can this ever happen? Not while every
|
|
||||||
// single element gets pushed to the
|
|
||||||
// buffer first. With the happy path
|
|
||||||
// short-circuit noted above, it could.
|
|
||||||
|
|
||||||
let last_ind = raster_segments.len() - 1;
|
|
||||||
if raster_segments[last_ind].line_num == sl_output_index{
|
|
||||||
let scanline = raster_segments.pop().unwrap();
|
|
||||||
print_scanline(scanline, samples_per_pixel);
|
|
||||||
sl_output_index -= 1;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
eprintln!("Size of raster_segments at finish: {}", raster_segments.len());
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
// TODO: Dispatcher shutdown mechanism. Right now, we might technically be leaking threads.
|
|
||||||
eprintln!("Done!");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn print_scanline(scanline: renderer::RenderResult, samples_per_pixel: u32){
|
|
||||||
eprintln!("Printing scanline num: {}", scanline.line_num);
|
|
||||||
for color in &scanline.line {
|
|
||||||
println!("{}", color.print_ppm(samples_per_pixel));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn random_scene(srng: &mut SmallRng) -> Hittable {
|
|
||||||
let mat_ground = Material::Lambertian { albedo: Vec3::new(0.5, 0.5, 0.5) };
|
|
||||||
let mut world = Hittable::HittableList { hittables : Vec::<Hittable>::new() };
|
|
||||||
|
|
||||||
world.push( Hittable::Sphere { center: Vec3::new(0.0, -1000.0, 0.0), radius: 1000.0, material: Some(mat_ground) });
|
|
||||||
|
|
||||||
let distrib_zero_one = Uniform::new(0.0, 1.0);
|
|
||||||
for a in -11..11 {
|
|
||||||
for b in -11..11 {
|
|
||||||
let choose_mat = srng.sample(distrib_zero_one);
|
|
||||||
let center = Vec3 {
|
|
||||||
x: a as f32 + 0.9 * srng.sample(distrib_zero_one),
|
|
||||||
y: 0.2,
|
|
||||||
z: b as f32 + 0.9 * srng.sample(distrib_zero_one),
|
|
||||||
};
|
|
||||||
if (center - Vec3::new(4.0, 0.2, 0.0)).length() > 0.9 {
|
|
||||||
|
|
||||||
if choose_mat < 0.8 {
|
|
||||||
// diffuse
|
|
||||||
let albedo = Vec3::rand(srng, distrib_zero_one) * Vec3::rand(srng, distrib_zero_one);
|
|
||||||
let sphere_material = Material::Lambertian { albedo };
|
|
||||||
world.push(
|
|
||||||
Hittable::Sphere {
|
|
||||||
center,
|
|
||||||
radius: 0.2,
|
|
||||||
material: Some(sphere_material),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} else if choose_mat < 0.95 {
|
|
||||||
// metal
|
|
||||||
let distr_albedo = Uniform::new(0.5, 1.0);
|
|
||||||
let distr_fuzz = Uniform::new(0.0, 0.5);
|
|
||||||
|
|
||||||
let albedo = Vec3::rand(srng, distr_albedo);
|
|
||||||
let fuzz = srng.sample(distr_fuzz);
|
|
||||||
let material = Material::Metal { albedo, fuzz };
|
|
||||||
world.push(
|
|
||||||
Hittable::Sphere {
|
|
||||||
center,
|
|
||||||
radius: 0.2,
|
|
||||||
material: Some(material),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
// glass
|
|
||||||
let material = Material::Dielectric { index_refraction: 1.5 };
|
|
||||||
world.push(
|
|
||||||
Hittable::Sphere{
|
|
||||||
center,
|
|
||||||
radius: 0.2,
|
|
||||||
material: Some(material),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let material1 = Material::Dielectric { index_refraction: 1.5 };
|
|
||||||
world.push( Hittable::Sphere{
|
|
||||||
center: Vec3::new(0.0, 1.0, 0.0),
|
|
||||||
radius: 1.0,
|
|
||||||
material: Some(material1)
|
|
||||||
});
|
|
||||||
|
|
||||||
let material2 = Material::Lambertian { albedo: Vec3::new(0.4, 0.2, 0.1) };
|
|
||||||
world.push( Hittable::Sphere {
|
|
||||||
center: Vec3::new(-4.0, 1.0, 0.0),
|
|
||||||
radius: 1.0,
|
|
||||||
material: Some(material2)
|
|
||||||
});
|
|
||||||
|
|
||||||
let material3 = Material::Metal { albedo: Vec3::new(0.7, 0.6, 0.5), fuzz: 0.0 };
|
|
||||||
world.push( Hittable::Sphere {
|
|
||||||
center: Vec3::new(4.0, 1.0, 0.0),
|
|
||||||
radius: 1.0,
|
|
||||||
material: Some(material3)
|
|
||||||
});
|
|
||||||
|
|
||||||
return world;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@@ -17,6 +17,96 @@ use rand::Rng;
|
|||||||
use rand::rngs::SmallRng;
|
use rand::rngs::SmallRng;
|
||||||
use rand::distributions::Uniform;
|
use rand::distributions::Uniform;
|
||||||
|
|
||||||
|
pub type Vec2i = Vec2<i32>;
|
||||||
|
pub type Vec2f = Vec2<f32>;
|
||||||
|
|
||||||
|
#[derive (Clone, Copy, PartialEq, PartialOrd, Debug)]
|
||||||
|
pub struct Vec2<T>{
|
||||||
|
pub x: T,
|
||||||
|
pub y: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Vec2<f32> {
|
||||||
|
pub fn zero() -> Vec2<f32> {
|
||||||
|
Vec2{ x: 0.0, y: 0.0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ones() -> Vec2<f32> {
|
||||||
|
Vec2{ x: 1.0, y: 1.0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rand(srng: &mut SmallRng, distrib: Uniform<f32>) -> Vec2<f32> {
|
||||||
|
Vec2 { x: srng.sample(distrib), y: srng.sample(distrib) }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <T> Vec2<T>
|
||||||
|
where T: std::ops::Mul{
|
||||||
|
pub fn new(x: T, y: T) -> Vec2<T> {
|
||||||
|
Vec2{x, y}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <T> Add for Vec2 <T>
|
||||||
|
where T: std::ops::Add<Output = T>{
|
||||||
|
type Output = Vec2<T>;
|
||||||
|
fn add(self, other: Vec2<T>) -> Vec2<T> {
|
||||||
|
Vec2 { x: self.x + other.x, y: self.y + other.y }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <T> Mul for Vec2<T>
|
||||||
|
where T: std::ops::Mul<Output = T>{
|
||||||
|
type Output = Vec2<T>;
|
||||||
|
fn mul(self, other: Vec2<T>) -> Vec2<T> {
|
||||||
|
Vec2 {
|
||||||
|
x: self.x * other.x,
|
||||||
|
y: self.y * other.y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Div<f32> for Vec2<f32>{
|
||||||
|
type Output = Vec2<f32>;
|
||||||
|
fn div(self, other: f32) -> Vec2<f32> {
|
||||||
|
Vec2 {
|
||||||
|
x: 1.0/other * self.x,
|
||||||
|
y: 1.0/other * self.y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Div<i32> for Vec2<i32>{
|
||||||
|
type Output = Vec2<i32>;
|
||||||
|
fn div(self, other: i32) -> Vec2<i32> {
|
||||||
|
Vec2 {
|
||||||
|
x: self.x / other,
|
||||||
|
y: self.y / other
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <T> Div<Vec2<T>> for Vec2<T>
|
||||||
|
where T: std::ops::Div<Output = T>{
|
||||||
|
type Output = Vec2<T>;
|
||||||
|
fn div(self, other: Vec2<T>) -> Vec2<T> {
|
||||||
|
Vec2 {
|
||||||
|
x: self.x / other.x,
|
||||||
|
y: self.y / other.y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <T> Display for Vec2<T>
|
||||||
|
where T: Display { // nested type still needs to have Display
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
let str = format!("{} {}", self.x, self.y);
|
||||||
|
fmt.write_str(&str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
|
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug)]
|
||||||
pub struct Vec3{
|
pub struct Vec3{
|
||||||
pub x: f32,
|
pub x: f32,
|
||||||
@@ -280,7 +370,7 @@ impl Neg for Vec3{
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Vec3 {
|
impl Display for Vec3 {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
let str = format!("{} {} {}", self.x, self.y, self.z);
|
let str = format!("{} {} {}", self.x, self.y, self.z);
|
||||||
fmt.write_str(&str)?;
|
fmt.write_str(&str)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -308,6 +398,22 @@ pub struct Rect {
|
|||||||
pub h: i32,
|
pub h: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Rect{
|
||||||
|
pub fn pos(&self) -> Vec2i {
|
||||||
|
Vec2i {
|
||||||
|
x: self.x,
|
||||||
|
y: self.y,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn size(&self) -> Vec2i {
|
||||||
|
Vec2i {
|
||||||
|
x: self.w - self.x,
|
||||||
|
y: self.h - self.y,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test{
|
mod test{
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
333
src/renderer.rs
333
src/renderer.rs
@@ -1,275 +1,138 @@
|
|||||||
|
|
||||||
use crate::primitives::{Vec3, Ray, Rect};
|
use crate::primitives::{
|
||||||
|
Vec2i,
|
||||||
|
Vec2f,
|
||||||
|
Vec3,
|
||||||
|
Ray,
|
||||||
|
Rect,
|
||||||
|
};
|
||||||
use crate::scene::{
|
use crate::scene::{
|
||||||
Camera,
|
|
||||||
Hittable,
|
Hittable,
|
||||||
|
Scene,
|
||||||
};
|
};
|
||||||
|
|
||||||
use core::cmp::Ordering;
|
|
||||||
use std::thread;
|
|
||||||
use std::sync::mpsc;
|
|
||||||
use std::ops;
|
|
||||||
use rand::Rng;
|
|
||||||
use rand::rngs::SmallRng;
|
use rand::rngs::SmallRng;
|
||||||
use rand::distributions::Uniform;
|
|
||||||
use itertools::Itertools;
|
|
||||||
|
|
||||||
// =================
|
use itertools::{self, Itertools};
|
||||||
// Description parts
|
|
||||||
// =================
|
|
||||||
|
|
||||||
#[derive (Clone)]
|
const SKY_COLOR: Vec3 = Vec3 { x: 0.5, y: 0.7, z: 1.0};
|
||||||
pub struct RenderContext{
|
|
||||||
pub image: (i32, i32),
|
pub struct RenderProperties {
|
||||||
pub samples_per_pixel: u32,
|
pub samples: u32, // samples are averaged results over a pixel
|
||||||
pub max_depth: u32,
|
pub bounces: u32, // bounces are how far the ray will travel (in hits not total distance)
|
||||||
pub world: Hittable,
|
|
||||||
pub camera: Camera,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DistributionContianer {
|
fn to_uv(coord: Vec2i, img_size: Vec2i) -> Vec2f {
|
||||||
pub distrib_zero_one: Uniform<f32>,
|
let u = (coord.x as f32) / ((img_size.x - 1) as f32);
|
||||||
pub distrib_plusminus_one: Uniform<f32>,
|
let v = (coord.y as f32) / ((img_size.y - 1) as f32);
|
||||||
|
Vec2f::new(u, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DistributionContianer {
|
fn ray_color(
|
||||||
fn new() -> Self {
|
r: Ray, surface: &Hittable, depth: u32,
|
||||||
DistributionContianer {
|
rng: &mut SmallRng,
|
||||||
distrib_zero_one: Uniform::new(0.0, 1.0),
|
) -> Vec3 {
|
||||||
distrib_plusminus_one: Uniform::new(-1.0, 1.0),
|
// recursion guard
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// =============
|
|
||||||
// Drawing Parts
|
|
||||||
// =============
|
|
||||||
|
|
||||||
fn render_line(y: i32, small_rng: &mut SmallRng, context: RenderContext, distr: &DistributionContianer) -> Vec<Vec3> {
|
|
||||||
//TODO: Ensure that the compiler hoists the distribution's out as constants
|
|
||||||
// else, do so manually
|
|
||||||
(0..context.image.0).map(|x| {
|
|
||||||
sample_pixel(x, y, small_rng, &context, distr)
|
|
||||||
}).collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ray_color(r: Ray, world: &Hittable, depth: u32, srng: &mut SmallRng, distrib: Uniform<f32> ) -> Vec3 {
|
|
||||||
// recursion depth guard
|
|
||||||
if depth == 0 {
|
if depth == 0 {
|
||||||
return Vec3::zero();
|
return Vec3::zero();
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(rec) = world.hit(r, 0.001, f32::INFINITY){
|
// cast a ray, interrogate hit record
|
||||||
|
if let Some(record) = surface.hit(r, 0.001, f32::INFINITY){
|
||||||
let mut scattered = Ray {
|
let mut scattered = Ray {
|
||||||
orig: Vec3::zero(),
|
orig: Vec3::zero(),
|
||||||
dir: Vec3::zero()
|
dir: Vec3::zero(),
|
||||||
};
|
};
|
||||||
let mut attenuation = Vec3::zero();
|
let mut attenuation = Vec3::zero();
|
||||||
match rec.material {
|
if record.material.scatter(
|
||||||
Some(mat) => {
|
r,
|
||||||
if mat.scatter(r, rec, &mut attenuation, &mut scattered, srng) {
|
&record,
|
||||||
return attenuation * ray_color(scattered, world, depth-1, srng, distrib);
|
&mut attenuation,
|
||||||
};
|
&mut scattered,
|
||||||
},
|
rng
|
||||||
None => return Vec3::zero(),
|
) {
|
||||||
}
|
return attenuation * ray_color(
|
||||||
|
scattered, surface, depth-1, rng
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
} // TODO: explicit else block
|
||||||
|
// Rust gets angry about the inner if{} block because it evaluates to ()
|
||||||
|
// when the else path is taken. This is a problem for a function
|
||||||
|
// that returns Vec3 and not ().
|
||||||
|
|
||||||
|
{ // when nothing is struck, return sky color
|
||||||
let unitdir = Vec3::as_unit(r.dir);
|
let unitdir = Vec3::as_unit(r.dir);
|
||||||
let t = 0.5 * (unitdir.y + 1.0);
|
let t = 0.5 * (unitdir.y + 1.0);
|
||||||
return Vec3::ones() * (1.0 - t) + Vec3::new(0.5, 0.7, 1.0) * t
|
return Vec3::ones() * (1.0 - t) + SKY_COLOR * t
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sample_pixel(x: i32, y: i32, small_rng: &mut SmallRng, context: &RenderContext, distr: &DistributionContianer) -> Vec3{
|
fn sample_pixel(
|
||||||
(0..context.samples_per_pixel).into_iter().fold(
|
coord: Vec2i, // location in image/screen space
|
||||||
|
scene: &Scene, // scene we're drawing
|
||||||
|
render_props: &RenderProperties,
|
||||||
|
img_size: Vec2i,
|
||||||
|
// Supplied by the execution environment (the thread)
|
||||||
|
rng: &mut SmallRng,
|
||||||
|
) -> Vec3{
|
||||||
|
(0..render_props.samples)
|
||||||
|
.fold(
|
||||||
Vec3::zero(),
|
Vec3::zero(),
|
||||||
|color, _sample| {
|
|color, _sample| -> Vec3 {
|
||||||
let u = ((x as f32) + small_rng.sample(distr.distrib_zero_one)) / ((context.image.0 - 1) as f32);
|
let uv = to_uv(coord, img_size);
|
||||||
let v = ((y as f32) + small_rng.sample(distr.distrib_zero_one)) / ((context.image.1 - 1) as f32);
|
let ray = scene.camera.get_ray(uv.x, uv.y, rng);
|
||||||
let ray = context.camera.get_ray(u, v, small_rng);
|
if ray.dir.x.is_nan() {
|
||||||
color + ray_color(ray, &context.world, context.max_depth, small_rng, distr.distrib_plusminus_one)
|
panic!("Ray dir.x is NAN");
|
||||||
|
}
|
||||||
|
color + ray_color(ray, &scene.world, render_props.bounces, rng)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===============
|
pub struct Tile {
|
||||||
// Execution parts
|
_bounds: Rect,
|
||||||
// ===============
|
pub pixels: Vec<Vec3>,
|
||||||
|
|
||||||
/* Iterable that produces pixels left-to-right, top-to-bottom.
|
|
||||||
* `Tile`s represent the render space, not the finished image.
|
|
||||||
* There is no internal pixel buffer
|
|
||||||
*/
|
|
||||||
|
|
||||||
type TileCursorIter = itertools::Product<ops::Range<i32>, ops::Range<i32>>;
|
|
||||||
|
|
||||||
struct Tile {
|
|
||||||
bounds: Rect,
|
|
||||||
context: RenderContext,
|
|
||||||
small_rng: SmallRng,
|
|
||||||
rand_distr: DistributionContianer,
|
|
||||||
cursor: TileCursorIter,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tile{
|
impl Tile {
|
||||||
fn new(
|
pub fn render_tile(
|
||||||
bounds: Rect,
|
bounds: Rect, // bounds of the region to render
|
||||||
context: RenderContext,
|
img_size: Vec2i, // final image resolution (needed for proper UV mapping)
|
||||||
small_rng: SmallRng,
|
scene: &Scene,
|
||||||
rand_distr: DistributionContianer
|
properties: &RenderProperties, // TODO: Place image size in render properties?
|
||||||
) -> Self
|
rng: &mut SmallRng,
|
||||||
{
|
) -> Self {
|
||||||
Tile { bounds, context, small_rng, rand_distr,
|
let pixel_iter = (bounds.y..(bounds.y + bounds.h))
|
||||||
cursor: (bounds.x..(bounds.x + bounds.w))
|
.cartesian_product( bounds.x..(bounds.x + bounds.w));
|
||||||
.cartesian_product(bounds.y..(bounds.y + bounds.h)
|
let pixels = pixel_iter.map(
|
||||||
|
|coord| -> Vec3 {
|
||||||
|
sample_pixel(
|
||||||
|
Vec2i{x: coord.1, y: coord.0},
|
||||||
|
scene,
|
||||||
|
properties,
|
||||||
|
img_size,
|
||||||
|
rng,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
).collect();
|
||||||
}
|
Self {
|
||||||
}
|
_bounds: bounds,
|
||||||
|
pixels
|
||||||
impl Iterator for Tile {
|
|
||||||
type Item = Vec3;
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
if let Some((x, y)) = self.cursor.next(){
|
|
||||||
Some(sample_pixel(
|
|
||||||
x, y,
|
|
||||||
&mut self.small_rng,
|
|
||||||
&self.context,
|
|
||||||
&self.rand_distr,
|
|
||||||
))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
pub fn render_line(
|
||||||
|
y: i32, // bounding rect and line
|
||||||
|
img_size: Vec2i,
|
||||||
|
scene: &Scene,
|
||||||
#[derive (Clone)]
|
properties: &RenderProperties,
|
||||||
pub enum RenderCommand{
|
rng: &mut SmallRng, // rng utils
|
||||||
Stop,
|
) -> Self {
|
||||||
Line { line_num: i32, context: RenderContext },
|
Tile::render_tile(
|
||||||
}
|
Rect{ x: 0, y, w: img_size.x, h: 1 },
|
||||||
|
img_size,
|
||||||
pub struct RenderResult {
|
scene,
|
||||||
pub line_num: i32,
|
properties,
|
||||||
pub line: Vec<Vec3>,
|
rng
|
||||||
}
|
|
||||||
|
|
||||||
impl Ord for RenderResult {
|
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
|
||||||
if self.line_num > other.line_num {
|
|
||||||
Ordering::Less
|
|
||||||
} else if self.line_num < other.line_num {
|
|
||||||
Ordering::Greater
|
|
||||||
} else {
|
|
||||||
Ordering::Equal
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialOrd for RenderResult {
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
||||||
Some(self.cmp(other))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for RenderResult {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
self.line_num == other.line_num
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Eq for RenderResult {}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* The dispatcher will hold a list of threads, and a list of command input channels to match.
|
|
||||||
* Helper functions exist to input jobs serially, and then dispatch them to an open thread.
|
|
||||||
*
|
|
||||||
* Since receivers can be matched to several senders, the input end of the result channel will
|
|
||||||
* be cloned and given to each of the threads.
|
|
||||||
* TODO: Consider holding a copy of the render_tx end in case threads exit early and need to
|
|
||||||
* be restored.
|
|
||||||
*/
|
|
||||||
pub struct Dispatcher{
|
|
||||||
handles: Vec<thread::JoinHandle<()>>,
|
|
||||||
command_transmitters: Vec<mpsc::SyncSender<RenderCommand>>,
|
|
||||||
next_to_feed: usize, // gonna do a round-robin style dispatch, ig.
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Dispatcher {
|
|
||||||
pub fn new(srng: &SmallRng, num_threads: usize) -> (Dispatcher, mpsc::Receiver<RenderResult> ) {
|
|
||||||
let mut handles = Vec::new();
|
|
||||||
let mut command_transmitters = Vec::<mpsc::SyncSender<RenderCommand>>::new();
|
|
||||||
|
|
||||||
let (render_tx, render_rx) = mpsc::sync_channel::<RenderResult>(1);
|
|
||||||
|
|
||||||
for _ in 0..num_threads {
|
|
||||||
// create new command tx/rx pairs. Store tx in the list, give rx to the thread.
|
|
||||||
let (command_tx, command_rx) = mpsc::sync_channel::<RenderCommand>(1);
|
|
||||||
// TODO: Pick appropriate command queue depth (or make it controllable, even)
|
|
||||||
|
|
||||||
|
|
||||||
let mut srng = srng.clone();
|
|
||||||
let threads_result_tx = render_tx.clone();
|
|
||||||
let distribs = DistributionContianer::new();
|
|
||||||
let thread_handle = thread::spawn(move || {
|
|
||||||
while let Ok(job) = command_rx.recv() {
|
|
||||||
match job {
|
|
||||||
RenderCommand::Stop => {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
RenderCommand::Line { line_num, context } => {
|
|
||||||
let line = render_line(line_num, &mut srng, context, &distribs);
|
|
||||||
let result = RenderResult { line_num, line };
|
|
||||||
threads_result_tx.send(result).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
handles.push(thread_handle);
|
|
||||||
command_transmitters.push(command_tx);
|
|
||||||
}
|
|
||||||
// finally, stash everything in the Dispatcher struct and return.
|
|
||||||
(
|
|
||||||
Dispatcher{
|
|
||||||
handles,
|
|
||||||
command_transmitters,
|
|
||||||
next_to_feed: 0,
|
|
||||||
},
|
|
||||||
render_rx
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Reconsider round-robin dispatch
|
|
||||||
// When passing the message to threads which are still busy, this function
|
|
||||||
// will block (it's a sync_channel). While blocked, other threads could
|
|
||||||
// become available and left idle.
|
|
||||||
pub fn submit_job(&mut self, command: RenderCommand) -> Result<(), mpsc::SendError<RenderCommand>> {
|
|
||||||
// Stop command is special. We'll broadcast it to all threads.
|
|
||||||
if let RenderCommand::Stop = command {
|
|
||||||
for channel in &self.command_transmitters {
|
|
||||||
return channel.send(command.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check that `next_to_feed` is in-bounds, and then insert.
|
|
||||||
// index is post-incremented with this function call.
|
|
||||||
|
|
||||||
// wrap when at length (0-indexed so last valid index is len-1)
|
|
||||||
if self.next_to_feed == self.handles.len() {
|
|
||||||
self.next_to_feed = 0;
|
|
||||||
} else if self.next_to_feed > self.handles.len() {
|
|
||||||
panic!("How the hell did a +=1 skip past the maximum allowed size?");
|
|
||||||
}
|
|
||||||
|
|
||||||
match self.command_transmitters.get(self.next_to_feed){
|
|
||||||
Some(target) => target.send(command).unwrap(),
|
|
||||||
None => panic!("oh god oh fuck"),
|
|
||||||
}
|
|
||||||
self.next_to_feed += 1;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
177
src/scene.rs
177
src/scene.rs
@@ -8,7 +8,7 @@ use rand::distributions::Uniform;
|
|||||||
pub struct HitRecord{
|
pub struct HitRecord{
|
||||||
pub p: Vec3,
|
pub p: Vec3,
|
||||||
pub normal: Vec3,
|
pub normal: Vec3,
|
||||||
pub material: Option<Material>,
|
pub material: Material,
|
||||||
pub t: f32,
|
pub t: f32,
|
||||||
pub front_face: bool,
|
pub front_face: bool,
|
||||||
}
|
}
|
||||||
@@ -22,7 +22,7 @@ impl HitRecord{
|
|||||||
|
|
||||||
#[derive (Clone)]
|
#[derive (Clone)]
|
||||||
pub enum Hittable {
|
pub enum Hittable {
|
||||||
Sphere { center: Vec3, radius: f32, material: Option<Material> },
|
Sphere { center: Vec3, radius: f32, material: Material },
|
||||||
HittableList { hittables: Vec<Hittable> }
|
HittableList { hittables: Vec<Hittable> }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,24 +30,15 @@ impl Hittable {
|
|||||||
pub fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option<HitRecord> {
|
pub fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option<HitRecord> {
|
||||||
match self {
|
match self {
|
||||||
Hittable::HittableList { hittables } => {
|
Hittable::HittableList { hittables } => {
|
||||||
let mut might_return = HitRecord {
|
hittables.iter()
|
||||||
p: Vec3::zero(),
|
.map( |obj| -> Option<HitRecord> {
|
||||||
normal: Vec3::zero(),
|
obj.hit(r, t_min, t_max)
|
||||||
material: None,
|
}).filter(|obj| obj.is_some())
|
||||||
t: t_max,
|
.min_by(|lhs, rhs| {
|
||||||
front_face: false,
|
let lhs = lhs.as_ref().unwrap();
|
||||||
};
|
let rhs = rhs.as_ref().unwrap();
|
||||||
let mut hit_anything = false;
|
lhs.t.partial_cmp(&rhs.t).expect("Couldn't compare??")
|
||||||
|
}).unwrap_or(None)
|
||||||
for item in hittables {
|
|
||||||
if let Some(record) = item.hit(r, t_min, might_return.t){
|
|
||||||
hit_anything = true;
|
|
||||||
might_return = record;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if hit_anything{
|
|
||||||
return Some(might_return);
|
|
||||||
} else { return None; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Hittable::Sphere { center, radius, material } => {
|
Hittable::Sphere { center, radius, material } => {
|
||||||
@@ -102,7 +93,7 @@ impl Material {
|
|||||||
pub fn scatter(
|
pub fn scatter(
|
||||||
&self,
|
&self,
|
||||||
ray_in: Ray,
|
ray_in: Ray,
|
||||||
rec: HitRecord,
|
rec: &HitRecord,
|
||||||
attenuation: &mut Vec3,
|
attenuation: &mut Vec3,
|
||||||
scattered: &mut Ray,
|
scattered: &mut Ray,
|
||||||
srng: &mut SmallRng,
|
srng: &mut SmallRng,
|
||||||
@@ -178,7 +169,6 @@ pub fn degrees_to_radians(degrees: f32) -> f32 {
|
|||||||
degrees * std::f32::consts::PI / 180.0
|
degrees * std::f32::consts::PI / 180.0
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Clone, Copy)]
|
|
||||||
pub struct Camera {
|
pub struct Camera {
|
||||||
origin: Vec3,
|
origin: Vec3,
|
||||||
lower_left_corner: Vec3,
|
lower_left_corner: Vec3,
|
||||||
@@ -189,15 +179,37 @@ pub struct Camera {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Camera {
|
impl Camera {
|
||||||
pub fn new(
|
pub fn new() -> Camera {
|
||||||
lookfrom: Vec3,
|
Self::default()
|
||||||
lookat: Vec3,
|
}
|
||||||
vup: Vec3,
|
|
||||||
vfov: f32,
|
pub fn get_ray(&self, s: f32, t: f32, srng: &mut SmallRng) -> Ray {
|
||||||
aspect_ratio: f32,
|
let rd = Vec3::rand_in_unit_disk(srng) * self.lens_radius;
|
||||||
aperture: f32,
|
let offset = self.u * rd.x + self.v * rd.y;
|
||||||
focus_dist: f32
|
|
||||||
) -> Camera {
|
let dir = self.lower_left_corner
|
||||||
|
+ self.horizontal * s
|
||||||
|
+ self.vertical * t
|
||||||
|
- self.origin - offset;
|
||||||
|
Ray{
|
||||||
|
orig: self.origin + offset,
|
||||||
|
dir,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Camera {
|
||||||
|
fn default() -> Self {
|
||||||
|
// defaults are the same as the hard-coded properties passed from main
|
||||||
|
// ... except for the `lookfrom` position: (13.0, 2.0, 3.0) became (10.0, 0..)
|
||||||
|
let lookfrom = Vec3::new(10.0, 0.0, 0.0);
|
||||||
|
let lookat = Vec3 { x: 1.0, y: 0.0, z: 0.0 };
|
||||||
|
let vup = Vec3::new(0.0, 1.0, 0.0);
|
||||||
|
let vfov = 20.0;
|
||||||
|
let aspect_ratio = 3.0 / 2.0;
|
||||||
|
let aperture = 0.1;
|
||||||
|
let focus_dist = 10.0;
|
||||||
|
|
||||||
let theta = degrees_to_radians(vfov);
|
let theta = degrees_to_radians(vfov);
|
||||||
let h = (theta / 2.0).tan();
|
let h = (theta / 2.0).tan();
|
||||||
let vp_height = 2.0 * h;
|
let vp_height = 2.0 * h;
|
||||||
@@ -221,19 +233,94 @@ impl Camera {
|
|||||||
lens_radius: aperture / 2.0,
|
lens_radius: aperture / 2.0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_ray(&self, s: f32, t: f32, srng: &mut SmallRng) -> Ray {
|
|
||||||
let rd = Vec3::rand_in_unit_disk(srng) * self.lens_radius;
|
|
||||||
let offset = self.u * rd.x + self.v * rd.y;
|
|
||||||
|
|
||||||
let dir = self.lower_left_corner
|
|
||||||
+ self.horizontal * s
|
|
||||||
+ self.vertical * t
|
|
||||||
- self.origin - offset;
|
|
||||||
Ray{
|
|
||||||
orig: self.origin + offset,
|
|
||||||
dir,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub struct Scene {
|
||||||
|
pub camera: Camera,
|
||||||
|
pub world: Hittable,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Scene {
|
||||||
|
pub fn random_world(srng: &mut SmallRng) -> Hittable {
|
||||||
|
let mat_ground = Material::Lambertian { albedo: Vec3::new(0.5, 0.5, 0.5) };
|
||||||
|
let mut world = Hittable::HittableList { hittables : Vec::<Hittable>::new() };
|
||||||
|
|
||||||
|
world.push( Hittable::Sphere { center: Vec3::new(0.0, -1000.0, 0.0), radius: 1000.0, material: mat_ground });
|
||||||
|
|
||||||
|
let distrib_zero_one = Uniform::new(0.0, 1.0);
|
||||||
|
for a in -11..11 {
|
||||||
|
for b in -11..11 {
|
||||||
|
let choose_mat = srng.sample(distrib_zero_one);
|
||||||
|
let center = Vec3 {
|
||||||
|
x: a as f32 + 0.9 * srng.sample(distrib_zero_one),
|
||||||
|
y: 0.2,
|
||||||
|
z: b as f32 + 0.9 * srng.sample(distrib_zero_one),
|
||||||
|
};
|
||||||
|
if (center - Vec3::new(4.0, 0.2, 0.0)).length() > 0.9 {
|
||||||
|
|
||||||
|
if choose_mat < 0.8 {
|
||||||
|
// diffuse
|
||||||
|
let albedo = Vec3::rand(srng, distrib_zero_one) * Vec3::rand(srng, distrib_zero_one);
|
||||||
|
let sphere_material = Material::Lambertian { albedo };
|
||||||
|
world.push(
|
||||||
|
Hittable::Sphere {
|
||||||
|
center,
|
||||||
|
radius: 0.2,
|
||||||
|
material: sphere_material,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else if choose_mat < 0.95 {
|
||||||
|
// metal
|
||||||
|
let distr_albedo = Uniform::new(0.5, 1.0);
|
||||||
|
let distr_fuzz = Uniform::new(0.0, 0.5);
|
||||||
|
|
||||||
|
let albedo = Vec3::rand(srng, distr_albedo);
|
||||||
|
let fuzz = srng.sample(distr_fuzz);
|
||||||
|
let material = Material::Metal { albedo, fuzz };
|
||||||
|
world.push(
|
||||||
|
Hittable::Sphere {
|
||||||
|
center,
|
||||||
|
radius: 0.2,
|
||||||
|
material: material,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// glass
|
||||||
|
let material = Material::Dielectric { index_refraction: 1.5 };
|
||||||
|
world.push(
|
||||||
|
Hittable::Sphere{
|
||||||
|
center,
|
||||||
|
radius: 0.2,
|
||||||
|
material: material,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let material1 = Material::Dielectric { index_refraction: 1.5 };
|
||||||
|
world.push( Hittable::Sphere{
|
||||||
|
center: Vec3::new(0.0, 1.0, 0.0),
|
||||||
|
radius: 1.0,
|
||||||
|
material: material1
|
||||||
|
});
|
||||||
|
|
||||||
|
let material2 = Material::Lambertian { albedo: Vec3::new(0.4, 0.2, 0.1) };
|
||||||
|
world.push( Hittable::Sphere {
|
||||||
|
center: Vec3::new(-4.0, 1.0, 0.0),
|
||||||
|
radius: 1.0,
|
||||||
|
material: material2
|
||||||
|
});
|
||||||
|
|
||||||
|
let material3 = Material::Metal { albedo: Vec3::new(0.7, 0.6, 0.5), fuzz: 0.0 };
|
||||||
|
world.push( Hittable::Sphere {
|
||||||
|
center: Vec3::new(4.0, 1.0, 0.0),
|
||||||
|
radius: 1.0,
|
||||||
|
material: material3
|
||||||
|
});
|
||||||
|
world
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user