finally random colors 🚀

This commit is contained in:
2026-02-14 04:27:54 +01:00
parent 5bdd7d8121
commit 25a3661fd0
3 changed files with 29 additions and 26 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 784 KiB

After

Width:  |  Height:  |  Size: 2.0 MiB

View File

@@ -28,15 +28,15 @@ use crate::{
fn random_scene() -> HittableList {
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(
Point3::new(0.0, -1000.0, 0.0),
1000.0,
&GROUND_MATERIAL,
Box::new(ground_material),
)));
for a in 0..12 {
for b in 0..12 {
for a in -12..12 {
for b in -12..12 {
let choose_mat = random_f64();
let center = Point3::new(
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 choose_mat < 0.8 {
//let albedo = Color::new(0.3, 0.8, 0.4);
static SPHERE_MATERIAL: Lambertian = Lambertian::new(Color::new(0.3, 0.8, 0.4));
world.add(Box::new(Sphere::new(center, 0.2, &SPHERE_MATERIAL)));
let sphere_material: Lambertian = Lambertian::new(Color::random());
world.add(Box::new(Sphere::new(center, 0.2, Box::new(sphere_material))));
} else if choose_mat < 0.95 {
static SPHERE_MATERIAL: Metal = Metal::new(Color::new(0.3, 0.6, 0.2), 0.2);
world.add(Box::new(Sphere::new(center, 0.2, &SPHERE_MATERIAL)));
let sphere_material: Metal = Metal::new(Color::random(), 0.2);
world.add(Box::new(Sphere::new(center, 0.2, Box::new(sphere_material))));
} else {
static SPHERE_MATERIAL: Dielectric = Dielectric::new(1.5);
world.add(Box::new(Sphere::new(center, 0.2, &SPHERE_MATERIAL)));
let sphere_material: Dielectric = Dielectric::new(1.5);
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(
Point3::new(0.0, 1.0, 0.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(
Point3::new(-4.0, 1.0, 0.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(
Point3::new(4.0, 1.0, 0.0),
1.0,
&MATERIAL3,
Box::new(material3),
)));
world
@@ -112,10 +112,10 @@ fn ray_color(r: Ray, world: &dyn Hittable, depth: i32) -> Color {
fn main() {
// Image
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 samples_per_pixel: i32 = 5;
let max_depth: i32 = 4;
let samples_per_pixel: i32 = 200;
let max_depth: i32 = 5;
// World
let world = random_scene();

View File

@@ -1,21 +1,24 @@
use std::ops::Deref;
use crate::hittable::*;
use crate::material::Material;
use crate::vec3::*;
use crate::Ray;
pub struct Sphere<'a> {
pub struct Sphere {
center: Point3,
radius: f64,
mat_ptr: &'a dyn Material,
mat_ptr: Box<dyn Material>,
}
impl Sphere<'_> {
pub fn new<T: Material>(center: Point3, radius: f64, mat_ptr: &'static T) -> Self {
impl Sphere {
pub fn new<T: Material + 'static>(center: Point3, radius: f64, mat_ptr: Box<T>) -> Self {
Sphere {center, radius, mat_ptr}
}
}
impl Hittable for Sphere<'_> {
fn hit(&self, r: &crate::ray::Ray, t_min: f64, t_max: f64) -> Option<HitRecord<'_>> {
impl Hittable for Sphere {
fn hit(&self, r: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord<'_>> {
let oc = r.origin() - self.center;
let a = r.direction().length_squared();
let half_b = oc.dot(&r.direction());
@@ -51,7 +54,7 @@ impl Hittable for Sphere<'_> {
p: p,
normal: outward_normal,
front_face,
mat_ptr: self.mat_ptr,
mat_ptr: self.mat_ptr.deref(),
})
}
}