ft: 3: vec3 class

This commit is contained in:
2026-04-15 00:17:57 +02:00
parent 1c7460d2f5
commit 8e96a0ba11
5 changed files with 922 additions and 12 deletions

View File

@@ -1,7 +1,13 @@
#![feature(core_float_math)]
mod vec3;
use crate::vec3::Vec3;
use log::info;
use pretty_env_logger;
fn main() {
let v = Vec3::rgb(255., 0., 255.);
pretty_env_logger::init();
let image_width = 256;
@@ -12,16 +18,14 @@ fn main() {
for j in 0..image_height {
info!("{}\tScanlines remaining.", (image_height - j));
for i in 0..image_width {
let r = i as f32 / (image_width - 1) as f32;
let g = j as f32 / (image_height - 1) as f32;
let b = 0.0;
let ir = (255.599 * r) as u8;
let ig = (255.599 * g) as u8;
let ib = (255.599 * b) as u8;
let rgb = Vec3::rgb(
i as f32 / (image_width - 1) as f32,
j as f32 / (image_height - 1) as f32,
0.0,
);
let pixel = imgbuf.get_pixel_mut(i, j);
*pixel = image::Rgb([ir, ig, ib]);
*pixel = rgb.output();
}
}

252
src/vec3.rs Normal file
View File

@@ -0,0 +1,252 @@
use core::f32::math::sqrt;
use std::{
fmt::Display,
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
};
pub struct Vec3 {
r: f32,
g: f32,
b: f32,
}
impl Vec3 {
pub fn rgb(r: f32, g: f32, b: f32) -> Vec3 {
return Self { r: r, g: g, b: b };
}
pub fn length(&self) -> f32 {
return sqrt(self.length_squared());
}
pub fn length_squared(&self) -> f32 {
return (self.r * self.r + self.g * self.g + self.b * self.b) as f32;
}
pub fn dot(self, other: Vec3) -> Vec3 {
Vec3 {
r: self.r * other.r,
g: self.g * other.g,
b: self.b * other.b,
}
}
pub fn cross(self, other: Vec3) -> Vec3 {
Vec3 {
r: self.g * other.b - self.b * other.g,
g: self.b * other.r - self.r * other.b,
b: self.r * other.g - self.g * other.r,
}
}
pub fn unit(mut self) {
self /= self.length();
}
pub fn output(self) -> image::Rgb<u8> {
let ir = (255.599 * self.r) as u8;
let ig = (255.599 * self.g) as u8;
let ib = (255.599 * self.b) as u8;
image::Rgb([ir, ig, ib])
}
}
impl Neg for Vec3 {
type Output = Self;
fn neg(self) -> Self::Output {
Self {
r: -self.r,
g: -self.g,
b: -self.b,
}
}
}
impl Add<Vec3> for Vec3 {
type Output = Self;
fn add(self, _rhs: Self) -> Self {
return Self {
r: self.r + _rhs.r,
g: self.g + _rhs.g,
b: self.b + _rhs.b,
};
}
}
impl Add<f32> for Vec3 {
type Output = Self;
fn add(self, f: f32) -> Self {
return Self {
r: self.r + f,
g: self.g + f,
b: self.b + f,
};
}
}
impl AddAssign<Vec3> for Vec3 {
fn add_assign(&mut self, other: Self) {
*self = Self {
r: self.r + other.r,
g: self.g + other.g,
b: self.b + other.b,
};
}
}
impl AddAssign<f32> for Vec3 {
fn add_assign(&mut self, f: f32) {
*self = Self {
r: self.r + f,
g: self.g + f,
b: self.b + f,
};
}
}
impl Sub<Vec3> for Vec3 {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
return Self {
r: self.r - rhs.r,
g: self.g - rhs.g,
b: self.b - rhs.b,
};
}
}
impl Sub<f32> for Vec3 {
type Output = Self;
fn sub(self, f: f32) -> Self {
return Self {
r: self.r - f,
g: self.g - f,
b: self.b - f,
};
}
}
impl SubAssign<Vec3> for Vec3 {
fn sub_assign(&mut self, rhs: Self) {
*self = Self {
r: self.r - rhs.r,
g: self.g - rhs.g,
b: self.b - rhs.b,
};
}
}
impl SubAssign<f32> for Vec3 {
fn sub_assign(&mut self, f: f32) {
*self = Self {
r: self.r - f,
g: self.g - f,
b: self.b - f,
};
}
}
impl Mul<Vec3> for Vec3 {
type Output = Vec3;
fn mul(self, rhs: Self) -> Self::Output {
Self {
r: self.r * rhs.r,
g: self.g * rhs.g,
b: self.b * rhs.b,
}
}
}
impl Mul<f32> for Vec3 {
type Output = Self;
fn mul(self, f: f32) -> Self::Output {
Self {
r: self.r * f,
g: self.g * f,
b: self.b * f,
}
}
}
impl MulAssign<Vec3> for Vec3 {
fn mul_assign(&mut self, rhs: Self) {
*self = Self {
r: self.r * rhs.r,
g: self.g * rhs.g,
b: self.b * rhs.b,
};
}
}
impl MulAssign<f32> for Vec3 {
fn mul_assign(&mut self, f: f32) {
*self = Self {
r: self.r * f,
g: self.g * f,
b: self.b * f,
};
}
}
impl Div<Vec3> for Vec3 {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self {
r: self.r / rhs.r,
g: self.g / rhs.g,
b: self.b / rhs.b,
}
}
}
impl Div<f32> for Vec3 {
type Output = Self;
fn div(self, f: f32) -> Self::Output {
Self {
r: self.r / f,
g: self.g / f,
b: self.b / f,
}
}
}
impl DivAssign<Vec3> for Vec3 {
fn div_assign(&mut self, rhs: Self) {
*self = Self {
r: self.r / rhs.r,
g: self.g / rhs.g,
b: self.b / rhs.b,
};
}
}
impl DivAssign<f32> for Vec3 {
fn div_assign(&mut self, f: f32) {
*self = Self {
r: self.r / f,
g: self.g / f,
b: self.b / f,
};
}
}
impl PartialEq for Vec3 {
fn eq(&self, other: &Self) -> bool {
self.r == other.r && self.g == other.g && self.b == other.b
}
}
impl Display for Vec3 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({}, {}, {})", self.r, self.g, self.b)
}
}