Compare commits
3 Commits
46f6784256
...
camera-bui
| Author | SHA1 | Date | |
|---|---|---|---|
| 987bbe1c98 | |||
| f2d1a892b7 | |||
| 8e37fd8e97 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +0,0 @@
|
|||||||
/target
|
|
||||||
15
Cargo.toml
15
Cargo.toml
@@ -1,10 +1,19 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rustpt"
|
name = "rustpt"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2024"
|
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.9", features = ["small_rng"] }
|
rand = { version = "0.8.5", features = ["small_rng"] }
|
||||||
itertools = { version = "0.14" }
|
itertools = { version = "0.11.0" }
|
||||||
|
eframe = "0.25.0"
|
||||||
|
|||||||
@@ -1,26 +1,33 @@
|
|||||||
mod primitives;
|
#![warn(clippy::all, rust_2018_idioms, rust_2018_compatibility)]
|
||||||
mod renderer;
|
use rustpt::primitives::{
|
||||||
mod scene;
|
Vec2i,
|
||||||
|
Vec3,
|
||||||
|
};
|
||||||
|
use rustpt::scene::{
|
||||||
|
Camera,
|
||||||
|
Scene
|
||||||
|
};
|
||||||
|
|
||||||
use crate::primitives::{Vec2i, Vec3};
|
use rustpt::renderer::{
|
||||||
use crate::scene::{Camera, Scene};
|
Tile,
|
||||||
|
RenderProperties,
|
||||||
use crate::renderer::{RenderProperties, Tile};
|
};
|
||||||
|
|
||||||
use rand::SeedableRng;
|
use rand::SeedableRng;
|
||||||
use rand::rngs::SmallRng;
|
use rand::rngs::SmallRng;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// image
|
// image
|
||||||
let aspect_ratio = 3.0 / 2.0;
|
let aspect_ratio = 3.0 / 2.0;
|
||||||
let image = Vec2i {
|
let image = Vec2i {
|
||||||
x: 400,
|
x: 400,
|
||||||
y: (400.0 / aspect_ratio) as i32,
|
y: (400.0 / aspect_ratio) as i32
|
||||||
};
|
};
|
||||||
|
|
||||||
let render_config = RenderProperties {
|
let render_config = RenderProperties {
|
||||||
samples: 10,
|
samples: 10,
|
||||||
bounces: 50,
|
bounces: 50
|
||||||
};
|
};
|
||||||
|
|
||||||
// random generator
|
// random generator
|
||||||
@@ -37,7 +44,7 @@ fn main() {
|
|||||||
0.1, // aperture
|
0.1, // aperture
|
||||||
10.0, // dist_to_focus
|
10.0, // dist_to_focus
|
||||||
),
|
),
|
||||||
world: Scene::random_world(&mut small_rng),
|
world: Scene::random_world(&mut small_rng)
|
||||||
};
|
};
|
||||||
|
|
||||||
// render
|
// render
|
||||||
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;
|
||||||
@@ -1,10 +1,21 @@
|
|||||||
|
|
||||||
|
use std::ops::{
|
||||||
|
Add,
|
||||||
|
AddAssign,
|
||||||
|
Sub,
|
||||||
|
SubAssign,
|
||||||
|
Mul,
|
||||||
|
MulAssign,
|
||||||
|
Div,
|
||||||
|
DivAssign,
|
||||||
|
Neg,
|
||||||
|
};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
|
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use rand::distr::Uniform;
|
|
||||||
use rand::rngs::SmallRng;
|
use rand::rngs::SmallRng;
|
||||||
|
use rand::distributions::Uniform;
|
||||||
|
|
||||||
pub type Vec2i = Vec2<i32>;
|
pub type Vec2i = Vec2<i32>;
|
||||||
pub type Vec2f = Vec2<f32>;
|
pub type Vec2f = Vec2<f32>;
|
||||||
@@ -15,6 +26,7 @@ pub struct Vec2<T> {
|
|||||||
pub y: T,
|
pub y: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl Vec2<f32> {
|
impl Vec2<f32> {
|
||||||
pub fn zero() -> Vec2<f32> {
|
pub fn zero() -> Vec2<f32> {
|
||||||
Vec2{ x: 0.0, y: 0.0 }
|
Vec2{ x: 0.0, y: 0.0 }
|
||||||
@@ -25,44 +37,33 @@ impl Vec2<f32> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn rand(srng: &mut SmallRng, distrib: Uniform<f32>) -> Vec2<f32> {
|
pub fn rand(srng: &mut SmallRng, distrib: Uniform<f32>) -> Vec2<f32> {
|
||||||
Vec2 {
|
Vec2 { x: srng.sample(distrib), y: srng.sample(distrib) }
|
||||||
x: srng.sample(distrib),
|
|
||||||
y: srng.sample(distrib),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T> Vec2<T>
|
impl <T> Vec2<T>
|
||||||
where
|
where T: std::ops::Mul{
|
||||||
T: std::ops::Mul,
|
|
||||||
{
|
|
||||||
pub fn new(x: T, y: T) -> Vec2<T> {
|
pub fn new(x: T, y: T) -> Vec2<T> {
|
||||||
Vec2{x, y}
|
Vec2{x, y}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T> Add for Vec2 <T>
|
impl <T> Add for Vec2 <T>
|
||||||
where
|
where T: std::ops::Add<Output = T>{
|
||||||
T: std::ops::Add<Output = T>,
|
|
||||||
{
|
|
||||||
type Output = Vec2<T>;
|
type Output = Vec2<T>;
|
||||||
fn add(self, other: Vec2<T>) -> Vec2<T> {
|
fn add(self, other: Vec2<T>) -> Vec2<T> {
|
||||||
Vec2 {
|
Vec2 { x: self.x + other.x, y: self.y + other.y }
|
||||||
x: self.x + other.x,
|
|
||||||
y: self.y + other.y,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T> Mul for Vec2<T>
|
impl <T> Mul for Vec2<T>
|
||||||
where
|
where T: std::ops::Mul<Output = T>{
|
||||||
T: std::ops::Mul<Output = T>,
|
|
||||||
{
|
|
||||||
type Output = Vec2<T>;
|
type Output = Vec2<T>;
|
||||||
fn mul(self, other: Vec2<T>) -> Vec2<T> {
|
fn mul(self, other: Vec2<T>) -> Vec2<T> {
|
||||||
Vec2 {
|
Vec2 {
|
||||||
x: self.x * other.x,
|
x: self.x * other.x,
|
||||||
y: self.y * other.y,
|
y: self.y * other.y
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,7 +73,7 @@ impl Div<f32> for Vec2<f32> {
|
|||||||
fn div(self, other: f32) -> Vec2<f32> {
|
fn div(self, other: f32) -> Vec2<f32> {
|
||||||
Vec2 {
|
Vec2 {
|
||||||
x: 1.0/other * self.x,
|
x: 1.0/other * self.x,
|
||||||
y: 1.0 / other * self.y,
|
y: 1.0/other * self.y
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,30 +83,25 @@ impl Div<i32> for Vec2<i32> {
|
|||||||
fn div(self, other: i32) -> Vec2<i32> {
|
fn div(self, other: i32) -> Vec2<i32> {
|
||||||
Vec2 {
|
Vec2 {
|
||||||
x: self.x / other,
|
x: self.x / other,
|
||||||
y: self.y / other,
|
y: self.y / other
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T> Div<Vec2<T>> for Vec2<T>
|
impl <T> Div<Vec2<T>> for Vec2<T>
|
||||||
where
|
where T: std::ops::Div<Output = T>{
|
||||||
T: std::ops::Div<Output = T>,
|
|
||||||
{
|
|
||||||
type Output = Vec2<T>;
|
type Output = Vec2<T>;
|
||||||
fn div(self, other: Vec2<T>) -> Vec2<T> {
|
fn div(self, other: Vec2<T>) -> Vec2<T> {
|
||||||
Vec2 {
|
Vec2 {
|
||||||
x: self.x / other.x,
|
x: self.x / other.x,
|
||||||
y: self.y / other.y,
|
y: self.y / other.y
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T> Display for Vec2<T>
|
impl <T> Display for Vec2<T>
|
||||||
where
|
where T: Display { // nested type still needs to have Display
|
||||||
T: Display,
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
{
|
|
||||||
// nested type still needs to have Display
|
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
let str = format!("{} {}", self.x, self.y);
|
let str = format!("{} {}", self.x, self.y);
|
||||||
fmt.write_str(&str)
|
fmt.write_str(&str)
|
||||||
}
|
}
|
||||||
@@ -135,7 +131,7 @@ impl Vec3 {
|
|||||||
Vec3 {
|
Vec3 {
|
||||||
x: 1.0,
|
x: 1.0,
|
||||||
y: 1.0,
|
y: 1.0,
|
||||||
z: 1.0,
|
z: 1.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,14 +144,11 @@ impl Vec3 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn rand_in_unit_sphere(srng: &mut SmallRng) -> Vec3 {
|
pub fn rand_in_unit_sphere(srng: &mut SmallRng) -> Vec3 {
|
||||||
let distrib = Uniform::new(-1.0, 1.0).unwrap();
|
let distrib = Uniform::new(-1.0, 1.0);
|
||||||
loop {
|
loop {
|
||||||
let p = Vec3::rand(srng, distrib);
|
let p = Vec3::rand(srng, distrib);
|
||||||
if p.length_squared() >= 1.0 {
|
if p.length_squared() >= 1.0 { continue; }
|
||||||
continue;
|
else { return p; }
|
||||||
} else {
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,20 +156,17 @@ impl Vec3 {
|
|||||||
let distrib = Uniform::new(-1.0, 1.0);
|
let distrib = Uniform::new(-1.0, 1.0);
|
||||||
loop {
|
loop {
|
||||||
let p = Vec3 {
|
let p = Vec3 {
|
||||||
x: srng.sample(distrib.unwrap()),
|
x: srng.sample(distrib),
|
||||||
y: srng.sample(distrib.unwrap()),
|
y: srng.sample(distrib),
|
||||||
z: 0.0,
|
z: 0.0,
|
||||||
};
|
};
|
||||||
if p.length_squared() >= 1.0 {
|
if p.length_squared() >= 1.0 { continue; }
|
||||||
continue;
|
else { return p; }
|
||||||
} else {
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rand_unit_vector(srng: &mut SmallRng) -> Vec3 {
|
pub fn rand_unit_vector(srng: &mut SmallRng) -> Vec3 {
|
||||||
Vec3::as_unit(Vec3::rand_in_unit_sphere(srng))
|
return Vec3::as_unit(Vec3::rand_in_unit_sphere(srng));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn length(&self) -> f32 {
|
pub fn length(&self) -> f32 {
|
||||||
@@ -189,6 +179,7 @@ impl Vec3 {
|
|||||||
|
|
||||||
// roughly equivalent to the `void write_color(...)` in the book
|
// roughly equivalent to the `void write_color(...)` in the book
|
||||||
pub fn print_ppm(&self, samples_per_pixel: u32) -> String {
|
pub fn print_ppm(&self, samples_per_pixel: u32) -> String {
|
||||||
|
|
||||||
let scale = 1.0 / samples_per_pixel as f32;
|
let scale = 1.0 / samples_per_pixel as f32;
|
||||||
|
|
||||||
// now with gamma correction
|
// now with gamma correction
|
||||||
@@ -204,11 +195,14 @@ impl Vec3 {
|
|||||||
|
|
||||||
pub fn near_zero(&self) -> bool {
|
pub fn near_zero(&self) -> bool {
|
||||||
let epsilon: f32 = 1e-4;
|
let epsilon: f32 = 1e-4;
|
||||||
self.x.abs() < epsilon && self.y.abs() < epsilon && self.z.abs() < epsilon
|
return
|
||||||
|
self.x.abs() < epsilon &&
|
||||||
|
self.y.abs() < epsilon &&
|
||||||
|
self.z.abs() < epsilon
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reflect(v: Vec3, n: Vec3) -> Vec3 {
|
pub fn reflect(v: Vec3, n: Vec3) -> Vec3 {
|
||||||
v - n * Vec3::dot(v, n) * 2.0
|
return v - n * Vec3::dot(v, n) * 2.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn refract(uv: Vec3, n: Vec3, etai_over_etat: f32) -> Vec3 {
|
pub fn refract(uv: Vec3, n: Vec3, etai_over_etat: f32) -> Vec3 {
|
||||||
@@ -219,14 +213,16 @@ impl Vec3 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn dot(left: Vec3, right: Vec3) -> f32{
|
pub fn dot(left: Vec3, right: Vec3) -> f32{
|
||||||
left.x * right.x + left.y * right.y + left.z * right.z
|
left.x * right.x +
|
||||||
|
left.y * right.y +
|
||||||
|
left.z * right.z
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cross(u: Vec3, v: Vec3) -> Vec3{
|
pub fn cross(u: Vec3, v: Vec3) -> Vec3{
|
||||||
Vec3{
|
Vec3{
|
||||||
x: u.y * v.z - u.z * v.y,
|
x: u.y * v.z - u.z * v.y,
|
||||||
y: u.z * v.x - u.x * v.z,
|
y: u.z * v.x - u.x * v.z,
|
||||||
z: u.x * v.y - u.y * v.x,
|
z: u.x * v.y - u.y * v.x
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,6 +230,7 @@ impl Vec3 {
|
|||||||
let len = v.length();
|
let len = v.length();
|
||||||
v / len
|
v / len
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
impl Add for Vec3 {
|
impl Add for Vec3 {
|
||||||
type Output = Vec3;
|
type Output = Vec3;
|
||||||
@@ -251,7 +248,7 @@ impl AddAssign for Vec3 {
|
|||||||
*self = Self {
|
*self = Self {
|
||||||
x: self.x + other.x,
|
x: self.x + other.x,
|
||||||
y: self.y + other.y,
|
y: self.y + other.y,
|
||||||
z: self.z + other.z,
|
z: self.z + other.z
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -272,7 +269,7 @@ impl SubAssign for Vec3 {
|
|||||||
*self = Self {
|
*self = Self {
|
||||||
x: self.x - other.x,
|
x: self.x - other.x,
|
||||||
y: self.y - other.y,
|
y: self.y - other.y,
|
||||||
z: self.z - other.z,
|
z: self.z - other.z
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -304,7 +301,7 @@ impl MulAssign<Vec3> for Vec3 {
|
|||||||
*self = Self {
|
*self = Self {
|
||||||
x: self.x * other.x,
|
x: self.x * other.x,
|
||||||
y: self.y * other.y,
|
y: self.y * other.y,
|
||||||
z: self.z * other.z,
|
z: self.z * other.z
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -314,7 +311,7 @@ impl MulAssign<f32> for Vec3 {
|
|||||||
*self = Self {
|
*self = Self {
|
||||||
x: self.x * other,
|
x: self.x * other,
|
||||||
y: self.y * other,
|
y: self.y * other,
|
||||||
z: self.z * other,
|
z: self.z * other
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -346,7 +343,7 @@ impl DivAssign<Vec3> for Vec3 {
|
|||||||
*self = Self {
|
*self = Self {
|
||||||
x: self.x / other.x,
|
x: self.x / other.x,
|
||||||
y: self.y / other.y,
|
y: self.y / other.y,
|
||||||
z: self.z / other.z,
|
z: self.z / other.z
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -356,7 +353,7 @@ impl DivAssign<f32> for Vec3 {
|
|||||||
*self = Self {
|
*self = Self {
|
||||||
x: self.x / other,
|
x: self.x / other,
|
||||||
y: self.y / other,
|
y: self.y / other,
|
||||||
z: self.z / other,
|
z: self.z / other
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -373,10 +370,11 @@ 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(())
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -660,6 +658,7 @@ mod test {
|
|||||||
let refl = Vec3::reflect(ray, normal);
|
let refl = Vec3::reflect(ray, normal);
|
||||||
let expected = Vec3::new(-1.0, 0.0, 0.0);
|
let expected = Vec3::new(-1.0, 0.0, 0.0);
|
||||||
assert!(refl == expected);
|
assert!(refl == expected);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -667,6 +666,7 @@ mod test {
|
|||||||
let ray = Vec3::new(1.0, 0.0, 0.0);
|
let ray = Vec3::new(1.0, 0.0, 0.0);
|
||||||
let normal = Vec3::as_unit(Vec3::new(-1.0, 1.0, 0.0));
|
let normal = Vec3::as_unit(Vec3::new(-1.0, 1.0, 0.0));
|
||||||
|
|
||||||
|
|
||||||
let refl = Vec3::reflect(ray, normal);
|
let refl = Vec3::reflect(ray, normal);
|
||||||
let expected = Vec3::new(0.0, 1.0, 0.0);
|
let expected = Vec3::new(0.0, 1.0, 0.0);
|
||||||
let diff = refl - expected;
|
let diff = refl - expected;
|
||||||
@@ -678,9 +678,12 @@ mod test {
|
|||||||
fn check_lerp(){
|
fn check_lerp(){
|
||||||
let ray = Ray{
|
let ray = Ray{
|
||||||
orig: Vec3::new(0.0, 0.0, 0.0),
|
orig: Vec3::new(0.0, 0.0, 0.0),
|
||||||
dir: Vec3::new(1.0, 1.0, 0.0),
|
dir: Vec3::new(1.0, 1.0, 0.0)
|
||||||
};
|
};
|
||||||
let half = ray.at(0.5);
|
let half = ray.at(0.5);
|
||||||
assert_eq!(half, Vec3::new(0.5, 0.5, 0.0));
|
assert_eq!(
|
||||||
|
half,
|
||||||
|
Vec3::new(0.5, 0.5, 0.0)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,21 @@
|
|||||||
use crate::primitives::{Ray, Rect, Vec2f, Vec2i, Vec3};
|
|
||||||
use crate::scene::{Hittable, Scene};
|
use crate::primitives::{
|
||||||
|
Vec2i,
|
||||||
|
Vec2f,
|
||||||
|
Vec3,
|
||||||
|
Ray,
|
||||||
|
Rect,
|
||||||
|
};
|
||||||
|
use crate::scene::{
|
||||||
|
Hittable,
|
||||||
|
Scene,
|
||||||
|
};
|
||||||
|
|
||||||
use rand::rngs::SmallRng;
|
use rand::rngs::SmallRng;
|
||||||
|
|
||||||
use itertools::{self, Itertools};
|
use itertools::{self, Itertools};
|
||||||
|
|
||||||
const SKY_COLOR: Vec3 = Vec3 {
|
const SKY_COLOR: Vec3 = Vec3 { x: 0.5, y: 0.7, z: 1.0};
|
||||||
x: 0.5,
|
|
||||||
y: 0.7,
|
|
||||||
z: 1.0,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct RenderProperties {
|
pub struct RenderProperties {
|
||||||
pub samples: u32, // samples are averaged results over a pixel
|
pub samples: u32, // samples are averaged results over a pixel
|
||||||
@@ -22,7 +28,10 @@ fn to_uv(coord: Vec2i, img_size: Vec2i) -> Vec2f {
|
|||||||
Vec2f::new(u, v)
|
Vec2f::new(u, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ray_color(r: Ray, surface: &Hittable, depth: u32, rng: &mut SmallRng) -> Vec3 {
|
fn ray_color(
|
||||||
|
r: Ray, surface: &Hittable, depth: u32,
|
||||||
|
rng: &mut SmallRng,
|
||||||
|
) -> Vec3 {
|
||||||
// recursion guard
|
// recursion guard
|
||||||
if depth == 0 {
|
if depth == 0 {
|
||||||
return Vec3::zero();
|
return Vec3::zero();
|
||||||
@@ -35,22 +44,26 @@ fn ray_color(r: Ray, surface: &Hittable, depth: u32, rng: &mut SmallRng) -> Vec3
|
|||||||
dir: Vec3::zero(),
|
dir: Vec3::zero(),
|
||||||
};
|
};
|
||||||
let mut attenuation = Vec3::zero();
|
let mut attenuation = Vec3::zero();
|
||||||
if record
|
if record.material.scatter(
|
||||||
.material
|
r,
|
||||||
.scatter(r, &record, &mut attenuation, &mut scattered, rng)
|
&record,
|
||||||
{
|
&mut attenuation,
|
||||||
return attenuation * ray_color(scattered, surface, depth - 1, rng);
|
&mut scattered,
|
||||||
|
rng
|
||||||
|
) {
|
||||||
|
return attenuation * ray_color(
|
||||||
|
scattered, surface, depth-1, rng
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} // TODO: explicit else block
|
} // TODO: explicit else block
|
||||||
// Rust gets angry about the inner if{} block because it evaluates to ()
|
// 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
|
// when the else path is taken. This is a problem for a function
|
||||||
// that returns Vec3 and not ().
|
// that returns Vec3 and not ().
|
||||||
|
|
||||||
{
|
{ // when nothing is struck, return sky color
|
||||||
// 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);
|
||||||
Vec3::ones() * (1.0 - t) + SKY_COLOR * t
|
return Vec3::ones() * (1.0 - t) + SKY_COLOR * t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,14 +75,18 @@ fn sample_pixel(
|
|||||||
// Supplied by the execution environment (the thread)
|
// Supplied by the execution environment (the thread)
|
||||||
rng: &mut SmallRng,
|
rng: &mut SmallRng,
|
||||||
) -> Vec3{
|
) -> Vec3{
|
||||||
(0..render_props.samples).fold(Vec3::zero(), |color, _sample| -> Vec3 {
|
(0..render_props.samples)
|
||||||
|
.fold(
|
||||||
|
Vec3::zero(),
|
||||||
|
|color, _sample| -> Vec3 {
|
||||||
let uv = to_uv(coord, img_size);
|
let uv = to_uv(coord, img_size);
|
||||||
let ray = scene.camera.get_ray(uv.x, uv.y, rng);
|
let ray = scene.camera.get_ray(uv.x, uv.y, rng);
|
||||||
if ray.dir.x.is_nan() {
|
if ray.dir.x.is_nan() {
|
||||||
panic!("Ray dir.x is NAN");
|
panic!("Ray dir.x is NAN");
|
||||||
}
|
}
|
||||||
color + ray_color(ray, &scene.world, render_props.bounces, rng)
|
color + ray_color(ray, &scene.world, render_props.bounces, rng)
|
||||||
})
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Tile {
|
pub struct Tile {
|
||||||
@@ -85,25 +102,22 @@ impl Tile {
|
|||||||
properties: &RenderProperties, // TODO: Place image size in render properties?
|
properties: &RenderProperties, // TODO: Place image size in render properties?
|
||||||
rng: &mut SmallRng,
|
rng: &mut SmallRng,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let pixel_iter =
|
let pixel_iter = (bounds.y..(bounds.y + bounds.h))
|
||||||
(bounds.y..(bounds.y + bounds.h)).cartesian_product(bounds.x..(bounds.x + bounds.w));
|
.cartesian_product( bounds.x..(bounds.x + bounds.w));
|
||||||
let pixels = pixel_iter
|
let pixels = pixel_iter.map(
|
||||||
.map(|coord| -> Vec3 {
|
|coord| -> Vec3 {
|
||||||
sample_pixel(
|
sample_pixel(
|
||||||
Vec2i {
|
Vec2i{x: coord.1, y: coord.0},
|
||||||
x: coord.1,
|
|
||||||
y: coord.0,
|
|
||||||
},
|
|
||||||
scene,
|
scene,
|
||||||
properties,
|
properties,
|
||||||
img_size,
|
img_size,
|
||||||
rng,
|
rng,
|
||||||
)
|
)
|
||||||
})
|
}
|
||||||
.collect();
|
).collect();
|
||||||
Self {
|
Self {
|
||||||
_bounds: bounds,
|
_bounds: bounds,
|
||||||
pixels,
|
pixels
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn render_line(
|
pub fn render_line(
|
||||||
@@ -114,16 +128,11 @@ impl Tile {
|
|||||||
rng: &mut SmallRng, // rng utils
|
rng: &mut SmallRng, // rng utils
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Tile::render_tile(
|
Tile::render_tile(
|
||||||
Rect {
|
Rect{ x: 0, y, w: img_size.x, h: 1 },
|
||||||
x: 0,
|
|
||||||
y,
|
|
||||||
w: img_size.x,
|
|
||||||
h: 1,
|
|
||||||
},
|
|
||||||
img_size,
|
img_size,
|
||||||
scene,
|
scene,
|
||||||
properties,
|
properties,
|
||||||
rng,
|
rng
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
211
src/scene.rs
211
src/scene.rs
@@ -1,8 +1,9 @@
|
|||||||
use crate::primitives::{Ray, Vec3};
|
|
||||||
|
use crate::primitives::{Vec3, Ray};
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use rand::distr::Uniform;
|
|
||||||
use rand::rngs::SmallRng;
|
use rand::rngs::SmallRng;
|
||||||
|
use rand::distributions::Uniform;
|
||||||
|
|
||||||
pub struct HitRecord{
|
pub struct HitRecord{
|
||||||
pub p: Vec3,
|
pub p: Vec3,
|
||||||
@@ -13,47 +14,34 @@ pub struct HitRecord {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl HitRecord{
|
impl HitRecord{
|
||||||
pub fn set_face_normal(&mut self, r: Ray, outward_normal: Vec3) {
|
pub fn set_face_normal(&mut self, r: Ray, outward_normal: Vec3) -> (){
|
||||||
self.front_face = Vec3::dot(r.dir, outward_normal) < 0.0;
|
self.front_face = Vec3::dot(r.dir, outward_normal) < 0.0;
|
||||||
self.normal = if self.front_face {
|
self.normal = if self.front_face { outward_normal } else { -outward_normal };
|
||||||
outward_normal
|
|
||||||
} else {
|
|
||||||
-outward_normal
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Clone)]
|
#[derive (Clone)]
|
||||||
pub enum Hittable {
|
pub enum Hittable {
|
||||||
Sphere {
|
Sphere { center: Vec3, radius: f32, material: Material },
|
||||||
center: Vec3,
|
HittableList { hittables: Vec<Hittable> }
|
||||||
radius: f32,
|
|
||||||
material: Material,
|
|
||||||
},
|
|
||||||
HittableList {
|
|
||||||
hittables: Vec<Hittable>,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hittable {
|
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 } => hittables
|
Hittable::HittableList { hittables } => {
|
||||||
.iter()
|
hittables.iter()
|
||||||
.map(|obj| -> Option<HitRecord> { obj.hit(r, t_min, t_max) })
|
.map( |obj| -> Option<HitRecord> {
|
||||||
.filter(|obj| obj.is_some())
|
obj.hit(r, t_min, t_max)
|
||||||
|
}).filter(|obj| obj.is_some())
|
||||||
.min_by(|lhs, rhs| {
|
.min_by(|lhs, rhs| {
|
||||||
let lhs = lhs.as_ref().unwrap();
|
let lhs = lhs.as_ref().unwrap();
|
||||||
let rhs = rhs.as_ref().unwrap();
|
let rhs = rhs.as_ref().unwrap();
|
||||||
lhs.t.partial_cmp(&rhs.t).expect("Couldn't compare??")
|
lhs.t.partial_cmp(&rhs.t).expect("Couldn't compare??")
|
||||||
})
|
}).unwrap_or(None)
|
||||||
.unwrap_or(None),
|
}
|
||||||
|
|
||||||
Hittable::Sphere {
|
Hittable::Sphere { center, radius, material } => {
|
||||||
center,
|
|
||||||
radius,
|
|
||||||
material,
|
|
||||||
} => {
|
|
||||||
let oc = r.orig - *center;
|
let oc = r.orig - *center;
|
||||||
let a = r.dir.length_squared();
|
let a = r.dir.length_squared();
|
||||||
let half_b = Vec3::dot(oc, r.dir);
|
let half_b = Vec3::dot(oc, r.dir);
|
||||||
@@ -93,6 +81,7 @@ impl Hittable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
pub enum Material{
|
pub enum Material{
|
||||||
Lambertian { albedo: Vec3 },
|
Lambertian { albedo: Vec3 },
|
||||||
@@ -114,8 +103,7 @@ impl Material {
|
|||||||
let scatter_dir = rec.normal + Vec3::rand_unit_vector(srng);
|
let scatter_dir = rec.normal + Vec3::rand_unit_vector(srng);
|
||||||
// The compiler might be smart enough to compute this ^^^ just once. In which case,
|
// The compiler might be smart enough to compute this ^^^ just once. In which case,
|
||||||
// I don't need to do this weird dance. Oh well. It'll work.
|
// I don't need to do this weird dance. Oh well. It'll work.
|
||||||
let scatter_dir = if scatter_dir.near_zero() {
|
let scatter_dir = if scatter_dir.near_zero() { // if near zero,
|
||||||
// if near zero,
|
|
||||||
rec.normal // replace with normal
|
rec.normal // replace with normal
|
||||||
} else {
|
} else {
|
||||||
scatter_dir // else preserve current
|
scatter_dir // else preserve current
|
||||||
@@ -126,48 +114,44 @@ impl Material {
|
|||||||
// using them at all)
|
// using them at all)
|
||||||
*scattered = Ray{
|
*scattered = Ray{
|
||||||
orig: rec.p,
|
orig: rec.p,
|
||||||
dir: scatter_dir,
|
dir: scatter_dir
|
||||||
};
|
};
|
||||||
*attenuation = *albedo; // deref on both sides? Wacky
|
*attenuation = *albedo; // deref on both sides? Wacky
|
||||||
true
|
return true;
|
||||||
}
|
},
|
||||||
Material::Metal { albedo, fuzz } => {
|
Material::Metal { albedo, fuzz } => {
|
||||||
let reflected = Vec3::reflect(Vec3::as_unit(ray_in.dir), rec.normal);
|
let reflected = Vec3::reflect(
|
||||||
|
Vec3::as_unit(ray_in.dir),
|
||||||
|
rec.normal
|
||||||
|
);
|
||||||
*scattered = Ray{
|
*scattered = Ray{
|
||||||
orig: rec.p,
|
orig: rec.p,
|
||||||
dir: reflected + Vec3::rand_in_unit_sphere(srng) * *fuzz,
|
dir: reflected + Vec3::rand_in_unit_sphere(srng) * *fuzz,
|
||||||
};
|
};
|
||||||
*attenuation = *albedo;
|
*attenuation = *albedo;
|
||||||
Vec3::dot(scattered.dir, rec.normal) > 0.0
|
return Vec3::dot(scattered.dir, rec.normal) > 0.0;
|
||||||
}
|
},
|
||||||
Material::Dielectric { index_refraction } => {
|
Material::Dielectric { index_refraction } => {
|
||||||
*attenuation = Vec3::ones();
|
*attenuation = Vec3::ones();
|
||||||
let refraction_ratio = if rec.front_face {
|
let refraction_ratio = if rec.front_face { 1.0 / index_refraction } else { *index_refraction };
|
||||||
1.0 / index_refraction
|
|
||||||
} else {
|
|
||||||
*index_refraction
|
|
||||||
};
|
|
||||||
|
|
||||||
let unit_direction = Vec3::as_unit(ray_in.dir);
|
let unit_direction = Vec3::as_unit(ray_in.dir);
|
||||||
let cos_theta = Vec3::dot(-unit_direction, rec.normal).min(1.0);
|
let cos_theta = Vec3::dot(-unit_direction, rec.normal).min(1.0);
|
||||||
let sin_theta = (1.0 - cos_theta * cos_theta).sqrt();
|
let sin_theta = (1.0 - cos_theta * cos_theta).sqrt();
|
||||||
|
|
||||||
let cannot_refract = refraction_ratio * sin_theta > 1.0;
|
let cannot_refract = refraction_ratio * sin_theta > 1.0;
|
||||||
let distrib_zero_one = Uniform::new(0.0, 1.0).unwrap();
|
let distrib_zero_one = Uniform::new(0.0, 1.0);
|
||||||
let direction = if cannot_refract
|
let direction = if cannot_refract || Material::reflectance(cos_theta, refraction_ratio) > srng.sample(distrib_zero_one) {
|
||||||
|| Material::reflectance(cos_theta, refraction_ratio)
|
|
||||||
> srng.sample(distrib_zero_one)
|
|
||||||
{
|
|
||||||
Vec3::reflect(unit_direction, rec.normal)
|
Vec3::reflect(unit_direction, rec.normal)
|
||||||
} else {
|
} else {
|
||||||
Vec3::refract(unit_direction, rec.normal, refraction_ratio)
|
Vec3::refract(unit_direction, rec.normal, refraction_ratio)
|
||||||
};
|
};
|
||||||
*scattered = Ray {
|
*scattered = Ray {
|
||||||
orig: rec.p,
|
orig: rec.p,
|
||||||
dir: direction,
|
dir: direction
|
||||||
};
|
};
|
||||||
true
|
return true;
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,7 +159,7 @@ impl Material {
|
|||||||
// Schlick's approximation for reflectance.
|
// Schlick's approximation for reflectance.
|
||||||
let r0 = (1.0 - ref_idx) / (1.0 + ref_idx);
|
let r0 = (1.0 - ref_idx) / (1.0 + ref_idx);
|
||||||
let r0 = r0 * r0;
|
let r0 = r0 * r0;
|
||||||
r0 + (1.0 - r0) * (1.0 - cosine).powf(5.0)
|
return r0 + (1.0 - r0) * (1.0 - cosine).powf(5.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,21 +174,42 @@ pub struct Camera {
|
|||||||
lower_left_corner: Vec3,
|
lower_left_corner: Vec3,
|
||||||
horizontal: Vec3,
|
horizontal: Vec3,
|
||||||
vertical: Vec3,
|
vertical: Vec3,
|
||||||
u: Vec3,
|
u: Vec3, v: Vec3, /*w: Vec3,*/
|
||||||
v: Vec3, /*w: Vec3,*/
|
|
||||||
lens_radius: f32,
|
lens_radius: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
@@ -224,24 +229,12 @@ impl Camera {
|
|||||||
lower_left_corner,
|
lower_left_corner,
|
||||||
horizontal: horiz,
|
horizontal: horiz,
|
||||||
vertical: verti,
|
vertical: verti,
|
||||||
u,
|
u, v, /* w,*/
|
||||||
v, /* w,*/
|
|
||||||
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 struct Scene {
|
||||||
pub camera: Camera,
|
pub camera: Camera,
|
||||||
@@ -250,20 +243,12 @@ pub struct Scene {
|
|||||||
|
|
||||||
impl Scene {
|
impl Scene {
|
||||||
pub fn random_world(srng: &mut SmallRng) -> Hittable {
|
pub fn random_world(srng: &mut SmallRng) -> Hittable {
|
||||||
let mat_ground = Material::Lambertian {
|
let mat_ground = Material::Lambertian { albedo: Vec3::new(0.5, 0.5, 0.5) };
|
||||||
albedo: Vec3::new(0.5, 0.5, 0.5),
|
let mut world = Hittable::HittableList { hittables : Vec::<Hittable>::new() };
|
||||||
};
|
|
||||||
let mut world = Hittable::HittableList {
|
|
||||||
hittables: Vec::<Hittable>::new(),
|
|
||||||
};
|
|
||||||
|
|
||||||
world.push(Hittable::Sphere {
|
world.push( Hittable::Sphere { center: Vec3::new(0.0, -1000.0, 0.0), radius: 1000.0, material: mat_ground });
|
||||||
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).unwrap();
|
let distrib_zero_one = Uniform::new(0.0, 1.0);
|
||||||
for a in -11..11 {
|
for a in -11..11 {
|
||||||
for b in -11..11 {
|
for b in -11..11 {
|
||||||
let choose_mat = srng.sample(distrib_zero_one);
|
let choose_mat = srng.sample(distrib_zero_one);
|
||||||
@@ -273,70 +258,68 @@ impl Scene {
|
|||||||
z: b as f32 + 0.9 * srng.sample(distrib_zero_one),
|
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 (center - Vec3::new(4.0, 0.2, 0.0)).length() > 0.9 {
|
||||||
|
|
||||||
if choose_mat < 0.8 {
|
if choose_mat < 0.8 {
|
||||||
// diffuse
|
// diffuse
|
||||||
let albedo =
|
let albedo = Vec3::rand(srng, distrib_zero_one) * Vec3::rand(srng, distrib_zero_one);
|
||||||
Vec3::rand(srng, distrib_zero_one) * Vec3::rand(srng, distrib_zero_one);
|
|
||||||
let sphere_material = Material::Lambertian { albedo };
|
let sphere_material = Material::Lambertian { albedo };
|
||||||
world.push(Hittable::Sphere {
|
world.push(
|
||||||
|
Hittable::Sphere {
|
||||||
center,
|
center,
|
||||||
radius: 0.2,
|
radius: 0.2,
|
||||||
material: sphere_material,
|
material: sphere_material,
|
||||||
});
|
}
|
||||||
|
);
|
||||||
} else if choose_mat < 0.95 {
|
} else if choose_mat < 0.95 {
|
||||||
// metal
|
// metal
|
||||||
let distr_albedo = Uniform::new(0.5, 1.0).unwrap();
|
let distr_albedo = Uniform::new(0.5, 1.0);
|
||||||
let distr_fuzz = Uniform::new(0.0, 0.5).unwrap();
|
let distr_fuzz = Uniform::new(0.0, 0.5);
|
||||||
|
|
||||||
let albedo = Vec3::rand(srng, distr_albedo);
|
let albedo = Vec3::rand(srng, distr_albedo);
|
||||||
let fuzz = srng.sample(distr_fuzz);
|
let fuzz = srng.sample(distr_fuzz);
|
||||||
let material = Material::Metal { albedo, fuzz };
|
let material = Material::Metal { albedo, fuzz };
|
||||||
world.push(Hittable::Sphere {
|
world.push(
|
||||||
|
Hittable::Sphere {
|
||||||
center,
|
center,
|
||||||
radius: 0.2,
|
radius: 0.2,
|
||||||
material,
|
material: material,
|
||||||
});
|
}
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
// glass
|
// glass
|
||||||
let material = Material::Dielectric {
|
let material = Material::Dielectric { index_refraction: 1.5 };
|
||||||
index_refraction: 1.5,
|
world.push(
|
||||||
};
|
Hittable::Sphere{
|
||||||
world.push(Hittable::Sphere {
|
|
||||||
center,
|
center,
|
||||||
radius: 0.2,
|
radius: 0.2,
|
||||||
material,
|
material: material,
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let material1 = Material::Dielectric {
|
let material1 = Material::Dielectric { index_refraction: 1.5 };
|
||||||
index_refraction: 1.5,
|
|
||||||
};
|
|
||||||
world.push( Hittable::Sphere{
|
world.push( Hittable::Sphere{
|
||||||
center: Vec3::new(0.0, 1.0, 0.0),
|
center: Vec3::new(0.0, 1.0, 0.0),
|
||||||
radius: 1.0,
|
radius: 1.0,
|
||||||
material: material1,
|
material: material1
|
||||||
});
|
});
|
||||||
|
|
||||||
let material2 = Material::Lambertian {
|
let material2 = Material::Lambertian { albedo: Vec3::new(0.4, 0.2, 0.1) };
|
||||||
albedo: Vec3::new(0.4, 0.2, 0.1),
|
|
||||||
};
|
|
||||||
world.push( Hittable::Sphere {
|
world.push( Hittable::Sphere {
|
||||||
center: Vec3::new(-4.0, 1.0, 0.0),
|
center: Vec3::new(-4.0, 1.0, 0.0),
|
||||||
radius: 1.0,
|
radius: 1.0,
|
||||||
material: material2,
|
material: material2
|
||||||
});
|
});
|
||||||
|
|
||||||
let material3 = Material::Metal {
|
let material3 = Material::Metal { albedo: Vec3::new(0.7, 0.6, 0.5), fuzz: 0.0 };
|
||||||
albedo: Vec3::new(0.7, 0.6, 0.5),
|
|
||||||
fuzz: 0.0,
|
|
||||||
};
|
|
||||||
world.push( Hittable::Sphere {
|
world.push( Hittable::Sphere {
|
||||||
center: Vec3::new(4.0, 1.0, 0.0),
|
center: Vec3::new(4.0, 1.0, 0.0),
|
||||||
radius: 1.0,
|
radius: 1.0,
|
||||||
material: material3,
|
material: material3
|
||||||
});
|
});
|
||||||
world
|
world
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user