finally random colors 🚀
This commit is contained in:
BIN
example.png
BIN
example.png
Binary file not shown.
|
Before Width: | Height: | Size: 784 KiB After Width: | Height: | Size: 2.0 MiB |
38
src/main.rs
38
src/main.rs
@@ -28,15 +28,15 @@ use crate::{
|
|||||||
fn random_scene() -> HittableList {
|
fn random_scene() -> HittableList {
|
||||||
let mut world = HittableList::new_empty();
|
let mut world = HittableList::new_empty();
|
||||||
|
|
||||||
static GROUND_MATERIAL: Lambertian = Lambertian::new(Color::new(0.5, 0.5, 0.5));
|
let ground_material: Lambertian = Lambertian::new(Color::new(0.5, 0.5, 0.5));
|
||||||
world.add(Box::new(Sphere::new(
|
world.add(Box::new(Sphere::new(
|
||||||
Point3::new(0.0, -1000.0, 0.0),
|
Point3::new(0.0, -1000.0, 0.0),
|
||||||
1000.0,
|
1000.0,
|
||||||
&GROUND_MATERIAL,
|
Box::new(ground_material),
|
||||||
)));
|
)));
|
||||||
|
|
||||||
for a in 0..12 {
|
for a in -12..12 {
|
||||||
for b in 0..12 {
|
for b in -12..12 {
|
||||||
let choose_mat = random_f64();
|
let choose_mat = random_f64();
|
||||||
let center = Point3::new(
|
let center = Point3::new(
|
||||||
a as f64 + 0.9 * random_f64(),
|
a as f64 + 0.9 * random_f64(),
|
||||||
@@ -47,38 +47,38 @@ fn random_scene() -> HittableList {
|
|||||||
if (center - Point3::new(4.0, 0.2, 0.0)).length() > 0.9 {
|
if (center - Point3::new(4.0, 0.2, 0.0)).length() > 0.9 {
|
||||||
if choose_mat < 0.8 {
|
if choose_mat < 0.8 {
|
||||||
//let albedo = Color::new(0.3, 0.8, 0.4);
|
//let albedo = Color::new(0.3, 0.8, 0.4);
|
||||||
static SPHERE_MATERIAL: Lambertian = Lambertian::new(Color::new(0.3, 0.8, 0.4));
|
let sphere_material: Lambertian = Lambertian::new(Color::random());
|
||||||
world.add(Box::new(Sphere::new(center, 0.2, &SPHERE_MATERIAL)));
|
world.add(Box::new(Sphere::new(center, 0.2, Box::new(sphere_material))));
|
||||||
} else if choose_mat < 0.95 {
|
} else if choose_mat < 0.95 {
|
||||||
static SPHERE_MATERIAL: Metal = Metal::new(Color::new(0.3, 0.6, 0.2), 0.2);
|
let sphere_material: Metal = Metal::new(Color::random(), 0.2);
|
||||||
world.add(Box::new(Sphere::new(center, 0.2, &SPHERE_MATERIAL)));
|
world.add(Box::new(Sphere::new(center, 0.2, Box::new(sphere_material))));
|
||||||
} else {
|
} else {
|
||||||
static SPHERE_MATERIAL: Dielectric = Dielectric::new(1.5);
|
let sphere_material: Dielectric = Dielectric::new(1.5);
|
||||||
world.add(Box::new(Sphere::new(center, 0.2, &SPHERE_MATERIAL)));
|
world.add(Box::new(Sphere::new(center, 0.2, Box::new(sphere_material))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static MATERIAL1: Dielectric = Dielectric::new(1.5);
|
let material1: Dielectric = Dielectric::new(1.5);
|
||||||
world.add(Box::new(Sphere::new(
|
world.add(Box::new(Sphere::new(
|
||||||
Point3::new(0.0, 1.0, 0.0),
|
Point3::new(0.0, 1.0, 0.0),
|
||||||
1.0,
|
1.0,
|
||||||
&MATERIAL1,
|
Box::new(material1),
|
||||||
)));
|
)));
|
||||||
|
|
||||||
static MATERIAL2: Lambertian = Lambertian::new(Color::new(0.4, 0.2, 0.1));
|
let material2: Lambertian = Lambertian::new(Color::new(0.4, 0.2, 0.1));
|
||||||
world.add(Box::new(Sphere::new(
|
world.add(Box::new(Sphere::new(
|
||||||
Point3::new(-4.0, 1.0, 0.0),
|
Point3::new(-4.0, 1.0, 0.0),
|
||||||
1.0,
|
1.0,
|
||||||
&MATERIAL2,
|
Box::new(material2),
|
||||||
)));
|
)));
|
||||||
|
|
||||||
static MATERIAL3: Metal = Metal::new(Color::new(0.7, 0.6, 0.5), 0.0);
|
let material3: Metal = Metal::new(Color::new(0.7, 0.6, 0.5), 0.0);
|
||||||
world.add(Box::new(Sphere::new(
|
world.add(Box::new(Sphere::new(
|
||||||
Point3::new(4.0, 1.0, 0.0),
|
Point3::new(4.0, 1.0, 0.0),
|
||||||
1.0,
|
1.0,
|
||||||
&MATERIAL3,
|
Box::new(material3),
|
||||||
)));
|
)));
|
||||||
|
|
||||||
world
|
world
|
||||||
@@ -112,10 +112,10 @@ fn ray_color(r: Ray, world: &dyn Hittable, depth: i32) -> Color {
|
|||||||
fn main() {
|
fn main() {
|
||||||
// Image
|
// Image
|
||||||
let aspect_ratio: f64 = 16. / 9.;
|
let aspect_ratio: f64 = 16. / 9.;
|
||||||
let image_width: i32 = 2560;
|
let image_width: i32 = 1920;
|
||||||
let image_height: i32 = (image_width as f64 / aspect_ratio as f64) as i32;
|
let image_height: i32 = (image_width as f64 / aspect_ratio as f64) as i32;
|
||||||
let samples_per_pixel: i32 = 5;
|
let samples_per_pixel: i32 = 200;
|
||||||
let max_depth: i32 = 4;
|
let max_depth: i32 = 5;
|
||||||
|
|
||||||
// World
|
// World
|
||||||
let world = random_scene();
|
let world = random_scene();
|
||||||
|
|||||||
@@ -1,21 +1,24 @@
|
|||||||
|
use std::ops::Deref;
|
||||||
|
|
||||||
use crate::hittable::*;
|
use crate::hittable::*;
|
||||||
use crate::material::Material;
|
use crate::material::Material;
|
||||||
use crate::vec3::*;
|
use crate::vec3::*;
|
||||||
|
use crate::Ray;
|
||||||
|
|
||||||
pub struct Sphere<'a> {
|
pub struct Sphere {
|
||||||
center: Point3,
|
center: Point3,
|
||||||
radius: f64,
|
radius: f64,
|
||||||
mat_ptr: &'a dyn Material,
|
mat_ptr: Box<dyn Material>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sphere<'_> {
|
impl Sphere {
|
||||||
pub fn new<T: Material>(center: Point3, radius: f64, mat_ptr: &'static T) -> Self {
|
pub fn new<T: Material + 'static>(center: Point3, radius: f64, mat_ptr: Box<T>) -> Self {
|
||||||
Sphere {center, radius, mat_ptr}
|
Sphere {center, radius, mat_ptr}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hittable for Sphere<'_> {
|
impl Hittable for Sphere {
|
||||||
fn hit(&self, r: &crate::ray::Ray, t_min: f64, t_max: f64) -> Option<HitRecord<'_>> {
|
fn hit(&self, r: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord<'_>> {
|
||||||
let oc = r.origin() - self.center;
|
let oc = r.origin() - self.center;
|
||||||
let a = r.direction().length_squared();
|
let a = r.direction().length_squared();
|
||||||
let half_b = oc.dot(&r.direction());
|
let half_b = oc.dot(&r.direction());
|
||||||
@@ -51,7 +54,7 @@ impl Hittable for Sphere<'_> {
|
|||||||
p: p,
|
p: p,
|
||||||
normal: outward_normal,
|
normal: outward_normal,
|
||||||
front_face,
|
front_face,
|
||||||
mat_ptr: self.mat_ptr,
|
mat_ptr: self.mat_ptr.deref(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user