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.
This commit is contained in:
2025-08-20 10:31:03 -05:00
parent 83fa670d35
commit 309931b7f6
4 changed files with 27 additions and 24 deletions

View File

@@ -30,8 +30,8 @@ fn main() {
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::ZERO, // lookat
Vec3::UP, // vup
20.0,
aspect_ratio,
0.1, // aperture

View File

@@ -123,21 +123,24 @@ impl Vec3 {
Vec3 { x, y, z }
}
pub fn zero() -> Vec3 {
Vec3 {
pub const ZERO: Self = Vec3 {
x: 0.0,
y: 0.0,
z: 0.0,
}
}
};
pub fn ones() -> Vec3 {
Vec3 {
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<f32>) -> Vec3 {
Vec3 {

View File

@@ -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() {

View File

@@ -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 {