From 4c872f9f9114b60432847976af76656c5ee7fb48 Mon Sep 17 00:00:00 2001 From: hal8174 Date: Fri, 27 Sep 2024 22:28:35 +0200 Subject: [PATCH] Initial commit --- .gitignore | 1 + Cargo.lock | 7 ++ Cargo.toml | 4 + ray-tracing-core/Cargo.toml | 6 ++ ray-tracing-core/src/lib.rs | 11 ++ ray-tracing-core/src/material.rs | 1 + ray-tracing-core/src/math.rs | 166 +++++++++++++++++++++++++++++++ ray-tracing-core/src/ray.rs | 8 ++ ray-tracing-core/src/scene.rs | 24 +++++ 9 files changed, 228 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 ray-tracing-core/Cargo.toml create mode 100644 ray-tracing-core/src/lib.rs create mode 100644 ray-tracing-core/src/material.rs create mode 100644 ray-tracing-core/src/math.rs create mode 100644 ray-tracing-core/src/ray.rs create mode 100644 ray-tracing-core/src/scene.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b64043 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..95b5fda --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ray-tracing-core" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..bf36457 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,4 @@ +[workspace] + +members = [ "ray-tracing-core"] +resolver = "2" diff --git a/ray-tracing-core/Cargo.toml b/ray-tracing-core/Cargo.toml new file mode 100644 index 0000000..f235845 --- /dev/null +++ b/ray-tracing-core/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ray-tracing-core" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/ray-tracing-core/src/lib.rs b/ray-tracing-core/src/lib.rs new file mode 100644 index 0000000..fbf7452 --- /dev/null +++ b/ray-tracing-core/src/lib.rs @@ -0,0 +1,11 @@ +pub mod material; +pub mod math; +pub mod ray; +pub mod scene; + +pub mod prelude { + pub type Float = f64; + pub use crate::material::Material; + pub use crate::math::{Dir3, Pos3}; + pub use crate::ray::Ray; +} diff --git a/ray-tracing-core/src/material.rs b/ray-tracing-core/src/material.rs new file mode 100644 index 0000000..a799c9b --- /dev/null +++ b/ray-tracing-core/src/material.rs @@ -0,0 +1 @@ +pub trait Material {} diff --git a/ray-tracing-core/src/math.rs b/ray-tracing-core/src/math.rs new file mode 100644 index 0000000..281f379 --- /dev/null +++ b/ray-tracing-core/src/math.rs @@ -0,0 +1,166 @@ +use crate::prelude::*; +use std::ops::{Add, Div, Mul, Neg, Sub}; + +#[derive(Debug, Clone, Copy)] +pub struct Pos3 { + x: Float, + y: Float, + z: Float, +} + +#[derive(Debug, Clone, Copy)] +pub struct Dir3 { + x: Float, + y: Float, + z: Float, +} + +impl Pos3 { + pub fn new(x: Float, y: Float, z: Float) -> Self { + Self { x, y, z } + } + + pub fn x(self) -> Float { + self.x + } + pub fn y(self) -> Float { + self.y + } + pub fn z(self) -> Float { + self.z + } + + pub fn zero() -> Self { + Self { + x: 0.0, + y: 0.0, + z: 0.0, + } + } +} + +impl Dir3 { + pub fn new(x: Float, y: Float, z: Float) -> Self { + Self { x, y, z } + } + pub fn x(self) -> Float { + self.x + } + pub fn y(self) -> Float { + self.y + } + pub fn z(self) -> Float { + self.z + } + + pub fn zero() -> Self { + Self { + x: 0.0, + y: 0.0, + z: 0.0, + } + } + + pub fn normalize(self) -> Self { + self / self.length() + } + pub fn length(self) -> Float { + Float::sqrt(self.x * self.x + self.y * self.y + self.z * self.z) + } +} + +impl Sub for Pos3 { + type Output = Dir3; + + fn sub(self, rhs: Self) -> Self::Output { + Dir3 { + x: self.x - rhs.x, + y: self.y - rhs.y, + z: self.z - rhs.z, + } + } +} + +impl Add for Dir3 { + type Output = Dir3; + + fn add(self, rhs: Self) -> Self::Output { + Dir3 { + x: self.x + rhs.x, + y: self.y + rhs.y, + z: self.z + rhs.z, + } + } +} + +impl Add for Dir3 { + type Output = Pos3; + + fn add(self, rhs: Pos3) -> Self::Output { + Pos3 { + x: self.x + rhs.x, + y: self.y + rhs.y, + z: self.z + rhs.z, + } + } +} + +impl Add for Pos3 { + type Output = Pos3; + + fn add(self, rhs: Dir3) -> Self::Output { + Pos3 { + x: self.x + rhs.x, + y: self.y + rhs.y, + z: self.z + rhs.z, + } + } +} + +impl Sub for Dir3 { + type Output = Dir3; + + fn sub(self, rhs: Self) -> Self::Output { + Dir3 { + x: self.x - rhs.x, + y: self.y - rhs.y, + z: self.z - rhs.z, + } + } +} + +impl Mul for Dir3 { + type Output = Dir3; + + fn mul(self, rhs: Float) -> Self::Output { + Dir3 { + x: self.x * rhs, + y: self.y * rhs, + z: self.z * rhs, + } + } +} + +impl Div for Dir3 { + type Output = Dir3; + + fn div(self, rhs: Float) -> Self::Output { + Dir3 { + x: self.x / rhs, + y: self.y / rhs, + z: self.z / rhs, + } + } +} + +impl Neg for Dir3 { + type Output = Dir3; + + fn neg(self) -> Self::Output { + Dir3 { + x: -self.x, + y: -self.y, + z: -self.z, + } + } +} diff --git a/ray-tracing-core/src/ray.rs b/ray-tracing-core/src/ray.rs new file mode 100644 index 0000000..ac3a573 --- /dev/null +++ b/ray-tracing-core/src/ray.rs @@ -0,0 +1,8 @@ +use crate::prelude::*; + +#[derive(Debug, Clone, Copy)] +pub struct Ray { + pub start: Pos3, + pub dir: Dir3, + pub time: Float, +} diff --git a/ray-tracing-core/src/scene.rs b/ray-tracing-core/src/scene.rs new file mode 100644 index 0000000..8c28af3 --- /dev/null +++ b/ray-tracing-core/src/scene.rs @@ -0,0 +1,24 @@ +use crate::prelude::*; + +pub trait Scene { + fn intersect(&self, ray: Ray, min: Float, max: Float) -> Option>; +} + +pub struct Intersection<'sc> { + t: Float, + material: &'sc dyn Material, +} + +impl<'sc> Intersection<'sc> { + pub fn new(t: Float, material: &'sc dyn Material) -> Self { + Self { t, material } + } + + pub fn t(&self) -> Float { + self.t + } + + pub fn material(&self) -> &'sc dyn Material { + self.material + } +}