From 309931b7f62ae6b6e87998fe36f8fe999df3ed10 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Wed, 20 Aug 2025 10:31:03 -0500 Subject: [PATCH] Replace util ctor's with constants I don't need functions to create new vectors, I just want a constant that I can clone. The compiler probably does the same thing in both cases, but this is more semantically accurate to that goal. --- src/main.rs | 6 +++--- src/primitives.rs | 31 +++++++++++++++++-------------- src/renderer.rs | 12 ++++++------ src/scene.rs | 2 +- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5bfcef6..7da13ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,9 +29,9 @@ fn main() { // 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 + Vec3::new(13.0, 2.0, 3.0), // lookfrom + Vec3::ZERO, // lookat + Vec3::UP, // vup 20.0, aspect_ratio, 0.1, // aperture diff --git a/src/primitives.rs b/src/primitives.rs index 7cd37aa..5204126 100644 --- a/src/primitives.rs +++ b/src/primitives.rs @@ -123,21 +123,24 @@ impl Vec3 { Vec3 { x, y, z } } - pub fn zero() -> Vec3 { - Vec3 { - x: 0.0, - y: 0.0, - z: 0.0, - } - } + pub const ZERO: Self = Vec3 { + x: 0.0, + y: 0.0, + z: 0.0, + }; - pub fn ones() -> Vec3 { - Vec3 { - x: 1.0, - y: 1.0, - z: 1.0, - } - } + pub const ONES: Self = Vec3 { + x: 1.0, + y: 1.0, + z: 1.0, + }; + + /// "Up" is considered to be positive-y + pub const UP: Self = Vec3 { + x: 0.0, + y: 1.0, + z: 0.0, + }; pub fn rand(srng: &mut SmallRng, distrib: Uniform) -> Vec3 { Vec3 { diff --git a/src/renderer.rs b/src/renderer.rs index 175fb8e..02bcd2c 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -25,16 +25,16 @@ fn to_uv(coord: Vec2i, img_size: Vec2i) -> Vec2f { fn ray_color(r: Ray, surface: &Hittable, depth: u32, rng: &mut SmallRng) -> Vec3 { // recursion guard if depth == 0 { - return Vec3::zero(); + return Vec3::ZERO; } // cast a ray, interrogate hit record if let Some(record) = surface.hit(r, 0.001, f32::INFINITY) { let mut scattered = Ray { - orig: Vec3::zero(), - dir: Vec3::zero(), + orig: Vec3::ZERO, + dir: Vec3::ZERO, }; - let mut attenuation = Vec3::zero(); + let mut attenuation = Vec3::ZERO; if record .material .scatter(r, &record, &mut attenuation, &mut scattered, rng) @@ -49,7 +49,7 @@ fn ray_color(r: Ray, surface: &Hittable, depth: u32, rng: &mut SmallRng) -> Vec3 // when nothing is struck, return sky color let unitdir = Vec3::as_unit(r.dir); let t = 0.5 * (unitdir.y + 1.0); - Vec3::ones() * (1.0 - t) + SKY_COLOR * t + Vec3::ONES * (1.0 - t) + SKY_COLOR * t } fn sample_pixel( @@ -60,7 +60,7 @@ fn sample_pixel( // Supplied by the execution environment (the thread) rng: &mut SmallRng, ) -> 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 ray = scene.camera.get_ray(uv.x, uv.y, rng); if ray.dir.x.is_nan() { diff --git a/src/scene.rs b/src/scene.rs index c3b3a29..e8b419d 100644 --- a/src/scene.rs +++ b/src/scene.rs @@ -141,7 +141,7 @@ impl Material { Vec3::dot(scattered.dir, rec.normal) > 0.0 } Material::Dielectric { index_refraction } => { - *attenuation = Vec3::ones(); + *attenuation = Vec3::ONES; let refraction_ratio = if rec.front_face { 1.0 / index_refraction } else {