ft: 4: ray class + simpl gradient
This commit is contained in:
99
src/vec3.rs
99
src/vec3.rs
@@ -4,22 +4,46 @@ use std::{
|
||||
ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
|
||||
};
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Vec3 {
|
||||
r: f32,
|
||||
g: f32,
|
||||
b: f32,
|
||||
}
|
||||
|
||||
pub type Colour = Vec3;
|
||||
|
||||
impl Vec3 {
|
||||
pub fn rgb(r: f32, g: f32, b: f32) -> Vec3 {
|
||||
return Self { r: r, g: g, b: b };
|
||||
pub fn new(r: f32, g: f32, b: f32) -> Vec3 {
|
||||
Self { r: r, g: g, b: b }
|
||||
}
|
||||
|
||||
pub fn nil() -> Vec3 {
|
||||
Self {
|
||||
r: 0.,
|
||||
g: 0.,
|
||||
b: 0.,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn x(&self) -> &f32 {
|
||||
&self.r
|
||||
}
|
||||
|
||||
pub fn y(&self) -> &f32 {
|
||||
&self.g
|
||||
}
|
||||
|
||||
pub fn z(&self) -> &f32 {
|
||||
&self.b
|
||||
}
|
||||
|
||||
pub fn length(&self) -> f32 {
|
||||
return sqrt(self.length_squared());
|
||||
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;
|
||||
(self.r * self.r + self.g * self.g + self.b * self.b) as f32
|
||||
}
|
||||
|
||||
pub fn dot(self, other: Vec3) -> Vec3 {
|
||||
@@ -38,10 +62,14 @@ impl Vec3 {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unit(mut self) {
|
||||
pub fn make_unit(mut self) {
|
||||
self /= self.length();
|
||||
}
|
||||
|
||||
pub fn get_unit(self) -> Vec3 {
|
||||
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;
|
||||
@@ -49,6 +77,14 @@ impl Vec3 {
|
||||
|
||||
image::Rgb([ir, ig, ib])
|
||||
}
|
||||
|
||||
pub fn clone(&self) -> Vec3 {
|
||||
Vec3 {
|
||||
r: self.r,
|
||||
g: self.g,
|
||||
b: self.b,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Neg for Vec3 {
|
||||
@@ -67,11 +103,11 @@ impl Add<Vec3> for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, _rhs: Self) -> Self {
|
||||
return Self {
|
||||
Self {
|
||||
r: self.r + _rhs.r,
|
||||
g: self.g + _rhs.g,
|
||||
b: self.b + _rhs.b,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,11 +115,11 @@ impl Add<f32> for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, f: f32) -> Self {
|
||||
return Self {
|
||||
Self {
|
||||
r: self.r + f,
|
||||
g: self.g + f,
|
||||
b: self.b + f,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,11 +147,11 @@ impl Sub<Vec3> for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, rhs: Self) -> Self {
|
||||
return Self {
|
||||
Self {
|
||||
r: self.r - rhs.r,
|
||||
g: self.g - rhs.g,
|
||||
b: self.b - rhs.b,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,11 +159,11 @@ impl Sub<f32> for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, f: f32) -> Self {
|
||||
return Self {
|
||||
Self {
|
||||
r: self.r - f,
|
||||
g: self.g - f,
|
||||
b: self.b - f,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,6 +199,19 @@ impl Mul<Vec3> for Vec3 {
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<Vec3> for u32 {
|
||||
type Output = Vec3;
|
||||
|
||||
fn mul(self, rhs: Vec3) -> Self::Output {
|
||||
let f = self as f32;
|
||||
Vec3 {
|
||||
r: rhs.r * f,
|
||||
g: rhs.g * f,
|
||||
b: rhs.b * f,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<f32> for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
@@ -175,6 +224,18 @@ impl Mul<f32> for Vec3 {
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<Vec3> for f32 {
|
||||
type Output = Vec3;
|
||||
|
||||
fn mul(self, rhs: Vec3) -> Self::Output {
|
||||
Vec3 {
|
||||
r: rhs.r * self,
|
||||
g: rhs.g * self,
|
||||
b: rhs.b * self,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MulAssign<Vec3> for Vec3 {
|
||||
fn mul_assign(&mut self, rhs: Self) {
|
||||
*self = Self {
|
||||
@@ -206,6 +267,18 @@ impl Div<Vec3> for Vec3 {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Div<i32> for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
fn div(self, i: i32) -> Self::Output {
|
||||
let f: f32 = i as f32;
|
||||
Self {
|
||||
r: self.r / f,
|
||||
g: self.g / f,
|
||||
b: self.b / f,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Div<f32> for Vec3 {
|
||||
type Output = Self;
|
||||
|
||||
Reference in New Issue
Block a user