Condensing the primitives

Group the informational types together. `vec3.rs` was renamed, and
the Ray implementation was copied into it. The Rect (and possibly a
Point) struct will be moved in, next. It's bad to have a `misc` or
`util` section, but "primitives" doesn't really do it for me, either.

My thought is that the sections will be:
- Primitives
- Renderer
- Scene Components

The renderer section would cover the image description and generation.
Image size and pixel sample count, but also things like the tile size,
render command dispatching, and file write-out. Anything to do with
producing the render.

The scene components section covers anything that goes in the render.
Obvious parts are the spheres and their materials, but this will also
include the camera. After all, it exists "in the world", and there could
be multiple.
This commit is contained in:
2023-08-19 19:28:43 -05:00
parent 809d7b678b
commit f5eae46f17
6 changed files with 39 additions and 60 deletions

View File

@@ -1,6 +1,5 @@
use crate::vec3::Vec3;
use crate::ray::Ray;
use crate::primitives::{Vec3, Ray};
use crate::degrees_to_radians;
use rand::rngs::SmallRng;

View File

@@ -1,6 +1,5 @@
use crate::vec3::Vec3;
use crate::ray::Ray;
use crate::primitives::{Vec3, Ray};
use crate::material::Material;
pub struct HitRecord{

View File

@@ -1,13 +1,11 @@
mod vec3;
mod ray;
mod primitives;
mod camera;
mod material;
mod hittable;
mod thread_utils;
use crate::vec3::Vec3;
use crate::ray::Ray;
use crate::primitives::{Vec3, Ray};
use crate::hittable::Hittable;
use crate::material::Material;
use crate::camera::Camera;

View File

@@ -1,8 +1,6 @@
use crate::ray::Ray;
use crate::primitives::{Vec3, Ray};
use crate::hittable::HitRecord;
use crate::vec3;
use crate::vec3::Vec3;
use rand::Rng;
use rand::rngs::SmallRng;
@@ -62,7 +60,7 @@ impl Material {
let refraction_ratio = if rec.front_face { 1.0 / index_refraction } else { *index_refraction };
let unit_direction = Vec3::as_unit(ray_in.dir);
let cos_theta = vec3::min(Vec3::dot(-unit_direction, rec.normal), 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 cannot_refract = refraction_ratio * sin_theta > 1.0;

View File

@@ -97,9 +97,9 @@ impl Vec3{
let g = (self.y * scale).sqrt();
let b = (self.z * scale).sqrt();
let ir = (clamp(r, 0.0, 0.999) * 256.0) as i32;
let ig = (clamp(g, 0.0, 0.999) * 256.0) as i32;
let ib = (clamp(b, 0.0, 0.999) * 256.0) as i32;
let ir = (r.clamp( 0.0, 0.999) * 256.0) as i32;
let ig = (g.clamp( 0.0, 0.999) * 256.0) as i32;
let ib = (b.clamp( 0.0, 0.999) * 256.0) as i32;
format!("{} {} {}", ir, ig, ib)
}
@@ -116,7 +116,7 @@ impl Vec3{
}
pub fn refract(uv: Vec3, n: Vec3, etai_over_etat: f32) -> Vec3 {
let cos_theta = min(Vec3::dot(-uv, n), 1.0);
let cos_theta = Vec3::dot(-uv, n).min(1.0);
let r_out_perp = (uv + n * cos_theta) * etai_over_etat;
let r_out_parallel = n * -(1.0 - r_out_perp.length_squared()).abs().sqrt();
r_out_perp + r_out_parallel
@@ -288,18 +288,6 @@ impl Display for Vec3 {
}
}
pub fn clamp(input: f32, lower: f32, upper: f32) -> f32 {
min(max(input, lower), upper)
}
pub fn min(a: f32, b: f32) -> f32 {
if a < b { a } else { b }
}
pub fn max(a: f32, b: f32) -> f32 {
if a > b { a } else { b }
}
#[cfg(test)]
mod test{
use super::*;
@@ -561,3 +549,32 @@ mod test{
}
}
#[derive(Copy, Clone)]
pub struct Ray{
pub orig: Vec3,
pub dir: Vec3,
}
impl Ray{
pub fn at(&self, t: f32) -> Vec3 {
self.orig + self.dir*t
}
}
#[cfg(test)]
mod test{
use super::*;
#[test]
fn check_lerp(){
let ray = Ray{
orig: Vec3::new(0.0, 0.0, 0.0),
dir: Vec3::new(1.0, 1.0, 0.0)
};
let half = ray.at(0.5);
assert_eq!(
half,
Vec3::new(0.5, 0.5, 0.0)
);
}
}

View File

@@ -1,32 +0,0 @@
use crate::vec3::Vec3;
#[derive(Copy, Clone)]
pub struct Ray{
pub orig: Vec3,
pub dir: Vec3,
}
impl Ray{
pub fn at(&self, t: f32) -> Vec3 {
self.orig + self.dir*t
}
}
#[cfg(test)]
mod test{
use super::*;
#[test]
fn check_lerp(){
let ray = Ray{
orig: Vec3::new(0.0, 0.0, 0.0),
dir: Vec3::new(1.0, 1.0, 0.0)
};
let half = ray.at(0.5);
assert_eq!(
half,
Vec3::new(0.5, 0.5, 0.0)
);
}
}