Initial commit

This commit is contained in:
hal8174 2024-09-27 22:28:35 +02:00
commit 4c872f9f91
9 changed files with 228 additions and 0 deletions

View file

@ -0,0 +1,6 @@
[package]
name = "ray-tracing-core"
version = "0.1.0"
edition = "2021"
[dependencies]

View file

@ -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;
}

View file

@ -0,0 +1 @@
pub trait Material {}

View file

@ -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<Pos3> 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<Dir3> 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<Float> 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<Float> 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,
}
}
}

View file

@ -0,0 +1,8 @@
use crate::prelude::*;
#[derive(Debug, Clone, Copy)]
pub struct Ray {
pub start: Pos3,
pub dir: Dir3,
pub time: Float,
}

View file

@ -0,0 +1,24 @@
use crate::prelude::*;
pub trait Scene {
fn intersect(&self, ray: Ray, min: Float, max: Float) -> Option<Intersection<'_>>;
}
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
}
}