Compare commits
No commits in common. "bfaf3c2cb6695e75dfc619e9101c2d9f64c02d22" and "48abb194648ef51ecd39cd7a80cdfbd5973c7a64" have entirely different histories.
bfaf3c2cb6
...
48abb19464
8 changed files with 27 additions and 298 deletions
13
Cargo.lock
generated
13
Cargo.lock
generated
|
@ -20,18 +20,6 @@ version = "1.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "enum_dispatch"
|
|
||||||
version = "0.3.13"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "aa18ce2bc66555b3218614519ac839ddb759a7d6720732f979ef8d13be147ecd"
|
|
||||||
dependencies = [
|
|
||||||
"once_cell",
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "glam"
|
name = "glam"
|
||||||
version = "0.30.2"
|
version = "0.30.2"
|
||||||
|
@ -64,7 +52,6 @@ name = "mechthild_core"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytemuck",
|
"bytemuck",
|
||||||
"enum_dispatch",
|
|
||||||
"glam",
|
"glam",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,6 @@ repository.workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bytemuck = { version = "1.22.0", optional = true }
|
bytemuck = { version = "1.22.0", optional = true }
|
||||||
enum_dispatch = "0.3.13"
|
|
||||||
glam = "0.30.2"
|
glam = "0.30.2"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
use crate::geometry::{Point, Ray, Transform, Vector};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct Camera {
|
|
||||||
/// camera to world transform
|
|
||||||
transform: Transform,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Camera {
|
|
||||||
pub fn new(eye: Point, center: Point, up: Vector, fov: f32) -> Self {
|
|
||||||
let f = (center - eye).length();
|
|
||||||
let tan = (fov / 2.0).to_radians().tan();
|
|
||||||
let transform =
|
|
||||||
Transform::look_at(eye, center, up).inverse() * Transform::scale(tan * f, tan * f, f);
|
|
||||||
|
|
||||||
Self { transform }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn ray(&self, x: f32, y: f32) -> Ray {
|
|
||||||
let origin = self.transform * Point::ORIGIN;
|
|
||||||
let direction = self.transform * Vector::new(x, y, 1.0);
|
|
||||||
Ray::new(origin, direction)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,24 +1,9 @@
|
||||||
use std::{
|
use std::ops::{Add, Div, Mul, Sub};
|
||||||
fmt,
|
|
||||||
ops::{Add, Deref, Div, Mul, Sub},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub mod shapes;
|
|
||||||
|
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct Vector(glam::Vec3A);
|
pub struct Vector(glam::Vec3A);
|
||||||
|
|
||||||
impl fmt::Debug for Vector {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
f.debug_tuple(stringify!(Vector))
|
|
||||||
.field(&self.0.x)
|
|
||||||
.field(&self.0.y)
|
|
||||||
.field(&self.0.z)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Vector> for glam::Vec3 {
|
impl From<Vector> for glam::Vec3 {
|
||||||
fn from(value: Vector) -> Self {
|
fn from(value: Vector) -> Self {
|
||||||
value.0.into()
|
value.0.into()
|
||||||
|
@ -31,14 +16,6 @@ impl From<(f32, f32, f32)> for Vector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for Vector {
|
|
||||||
type Target = glam::Vec3A;
|
|
||||||
#[inline]
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Vector {
|
impl Vector {
|
||||||
pub const ZERO: Self = Self(glam::Vec3A::ZERO);
|
pub const ZERO: Self = Self(glam::Vec3A::ZERO);
|
||||||
pub const I: Self = Self(glam::Vec3A::X);
|
pub const I: Self = Self(glam::Vec3A::X);
|
||||||
|
@ -53,14 +30,6 @@ impl Vector {
|
||||||
self.0.length()
|
self.0.length()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn length_squared(self) -> f32 {
|
|
||||||
self.0.length_squared()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn normalize(self) -> Vector {
|
|
||||||
Self(self.0.normalize())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn dot(self, other: Vector) -> f32 {
|
pub fn dot(self, other: Vector) -> f32 {
|
||||||
self.0.dot(other.0)
|
self.0.dot(other.0)
|
||||||
}
|
}
|
||||||
|
@ -103,19 +72,9 @@ impl Add for Vector {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct Point(glam::Vec3A);
|
pub struct Point(glam::Vec3A);
|
||||||
|
|
||||||
impl fmt::Debug for Point {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
f.debug_tuple(stringify!(Point))
|
|
||||||
.field(&self.0.x)
|
|
||||||
.field(&self.0.y)
|
|
||||||
.field(&self.0.z)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Point> for glam::Vec3 {
|
impl From<Point> for glam::Vec3 {
|
||||||
fn from(value: Point) -> Self {
|
fn from(value: Point) -> Self {
|
||||||
value.0.into()
|
value.0.into()
|
||||||
|
@ -134,10 +93,6 @@ impl Point {
|
||||||
pub fn new(x: f32, y: f32, z: f32) -> Self {
|
pub fn new(x: f32, y: f32, z: f32) -> Self {
|
||||||
Self(glam::vec3a(x, y, z))
|
Self(glam::vec3a(x, y, z))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_vector(self) -> Vector {
|
|
||||||
Vector(self.0)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Add<Vector> for Point {
|
impl Add<Vector> for Point {
|
||||||
|
@ -156,19 +111,10 @@ impl Sub for Point {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
pub struct Transform(glam::Affine3A);
|
pub struct Transform(glam::Affine3A);
|
||||||
|
|
||||||
impl fmt::Debug for Transform {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
f.debug_struct(stringify!(Transform))
|
|
||||||
.field(stringify!(matrix3), &self.0.matrix3)
|
|
||||||
.field(stringify!(translation), &self.0.translation)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Transform {
|
impl Transform {
|
||||||
pub fn scale(x: f32, y: f32, z: f32) -> Self {
|
pub fn scale(x: f32, y: f32, z: f32) -> Self {
|
||||||
Self(glam::Affine3A::from_scale(glam::vec3(x, y, z)))
|
Self(glam::Affine3A::from_scale(glam::vec3(x, y, z)))
|
||||||
|
@ -203,15 +149,6 @@ impl Mul<Vector> for Transform {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Mul<Point> for Transform {
|
|
||||||
type Output = Point;
|
|
||||||
|
|
||||||
fn mul(self, rhs: Point) -> Self::Output {
|
|
||||||
Point(self.0.transform_point3a(rhs.0))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Ray {
|
pub struct Ray {
|
||||||
origin: Point,
|
origin: Point,
|
||||||
direction: Vector,
|
direction: Vector,
|
||||||
|
|
|
@ -1,91 +0,0 @@
|
||||||
use std::fmt;
|
|
||||||
|
|
||||||
use enum_dispatch::enum_dispatch;
|
|
||||||
|
|
||||||
use super::{Point, Ray, Vector};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) struct Hit {
|
|
||||||
pub point: Point,
|
|
||||||
pub normal: Vector,
|
|
||||||
pub t: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[enum_dispatch]
|
|
||||||
pub(crate) trait Hittable {
|
|
||||||
fn intersect(&self, ray: &Ray, t_max: f32) -> Option<Hit>;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct Sphere {
|
|
||||||
center: Point,
|
|
||||||
radius: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Hittable for Sphere {
|
|
||||||
fn intersect(&self, ray: &Ray, t_max: f32) -> Option<Hit> {
|
|
||||||
let oc = self.center - ray.origin;
|
|
||||||
let a = ray.direction.length_squared();
|
|
||||||
let h = ray.direction.dot(oc);
|
|
||||||
let c = oc.length_squared() - self.radius * self.radius;
|
|
||||||
let d = h * h - a * c;
|
|
||||||
|
|
||||||
(d > 0.0)
|
|
||||||
.then(|| (h - d.sqrt()) / a)
|
|
||||||
.filter(|t| *t < t_max && *t > 0.0)
|
|
||||||
.map(|t| {
|
|
||||||
let point = ray.at(t);
|
|
||||||
let normal = point - self.center;
|
|
||||||
Hit { point, normal, t }
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct Plane {
|
|
||||||
normal: Vector,
|
|
||||||
d: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Hittable for Plane {
|
|
||||||
fn intersect(&self, ray: &Ray, t_max: f32) -> Option<Hit> {
|
|
||||||
let n_dot_r = self.normal.dot(ray.direction);
|
|
||||||
(n_dot_r.abs() > f32::EPSILON)
|
|
||||||
.then(|| {
|
|
||||||
let n_dot_o = self.normal.dot(ray.origin.to_vector());
|
|
||||||
-(n_dot_o - self.d) / n_dot_r
|
|
||||||
})
|
|
||||||
.filter(|t| *t < t_max && *t > 0.0)
|
|
||||||
.map(|t| Hit {
|
|
||||||
point: ray.at(t),
|
|
||||||
normal: self.normal,
|
|
||||||
t,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[enum_dispatch(Hittable)]
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub enum Shape {
|
|
||||||
Sphere,
|
|
||||||
Plane,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Debug for Shape {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
Self::Sphere(s) => s.fmt(f),
|
|
||||||
Self::Plane(p) => p.fmt(f),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Shape {
|
|
||||||
pub fn sphere(center: Point, radius: f32) -> Shape {
|
|
||||||
Shape::from(Sphere { center, radius })
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn plane(normal: Vector, d: f32) -> Shape {
|
|
||||||
Shape::from(Plane { normal, d })
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2,63 +2,38 @@
|
||||||
pub mod impl_bytemuck;
|
pub mod impl_bytemuck;
|
||||||
|
|
||||||
pub mod color;
|
pub mod color;
|
||||||
|
|
||||||
mod camera;
|
|
||||||
mod geometry;
|
mod geometry;
|
||||||
|
|
||||||
use core::f32;
|
|
||||||
|
|
||||||
pub use camera::Camera;
|
|
||||||
pub use geometry::{Point, Transform, Vector, shapes::Shape};
|
|
||||||
|
|
||||||
use color::Rgba;
|
use color::Rgba;
|
||||||
use geometry::{
|
|
||||||
Ray,
|
|
||||||
shapes::{Hit, Hittable},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct Scene {
|
pub struct Scene;
|
||||||
camera: Camera,
|
|
||||||
contents: Vec<Shape>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Scene {
|
impl Scene {
|
||||||
pub fn new(camera: Camera, contents: Vec<Shape>) -> Self {
|
pub fn new() -> Self {
|
||||||
Self { camera, contents }
|
Self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&self, width: usize, height: usize) -> Box<[Rgba]> {
|
pub fn render(&self, width: usize, height: usize) -> Box<[Rgba]> {
|
||||||
let ray = self.camera.ray(0.0, 0.0);
|
|
||||||
dbg!(&ray);
|
|
||||||
dbg!(self.intersect(ray));
|
|
||||||
let mut data = Box::new_uninit_slice(width * height);
|
let mut data = Box::new_uninit_slice(width * height);
|
||||||
let w = width as f32;
|
|
||||||
let h = height as f32;
|
|
||||||
let aspect_ratio = h / w;
|
|
||||||
|
|
||||||
for (i, pixel) in data.iter_mut().enumerate() {
|
for (i, pixel) in data.iter_mut().enumerate() {
|
||||||
let x = ((i % width) as f32 * 2.0 / w) - 1.0;
|
let x = (i % width) as f32;
|
||||||
let y = (1.0 - (i / width) as f32 * 2.0 / h) * aspect_ratio;
|
let y = (i / width) as f32;
|
||||||
|
|
||||||
let rgb = self
|
pixel.write(Rgba::new(
|
||||||
.intersect(self.camera.ray(x, y))
|
x / (width - 1) as f32,
|
||||||
.map(|h| h.normal.normalize() * 0.5 + Vector::new(0.5, 0.5, 0.5))
|
y / (height - 1) as f32,
|
||||||
.unwrap_or(Vector::ZERO);
|
0.5,
|
||||||
|
1.0,
|
||||||
pixel.write(Rgba::new(rgb.x, rgb.y, rgb.z, 1.0));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe { data.assume_init() }
|
unsafe { data.assume_init() }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn intersect(&self, ray: Ray) -> Option<Hit> {
|
impl Default for Scene {
|
||||||
let mut hit: Option<Hit> = None;
|
fn default() -> Self {
|
||||||
for shape in &self.contents {
|
Self::new()
|
||||||
let t_max = hit.as_ref().map(|h| h.t).unwrap_or(f32::INFINITY);
|
|
||||||
if let Some(h) = shape.intersect(&ray, t_max) {
|
|
||||||
hit = Some(h);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
hit
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
19
examples/basic.py
Executable file → Normal file
19
examples/basic.py
Executable file → Normal file
|
@ -1,20 +1,11 @@
|
||||||
#!python
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from mechthild import Scene, Shape, Camera
|
from mechthild import Scene
|
||||||
|
|
||||||
WIDTH = 800
|
WIDTH = 800
|
||||||
HEIGHT = 600
|
HEIGHT = 600
|
||||||
|
|
||||||
s1 = Shape.sphere((0.75, 1.0, 1.0), 1.0)
|
render_result = Scene().render(WIDTH, HEIGHT)
|
||||||
s2 = Shape.sphere((-1.5, 2.0, -2.0), 2.0)
|
rgba = bytes([int(v * 255) for v in render_result])
|
||||||
ground = Shape.plane((0.0, 1.0, 0.0), 0.0)
|
|
||||||
|
|
||||||
camera = Camera((0.0, 1.5, 5.0), (0.0, 1.0, 0.0), (0.0, 1.0, 0.0), 80.0)
|
img = Image.frombuffer('RGBA', (WIDTH, HEIGHT), rgba)
|
||||||
|
img.save("test.png")
|
||||||
scene = Scene(camera, [s1, s2, ground])
|
|
||||||
|
|
||||||
render = scene.render(WIDTH, HEIGHT)
|
|
||||||
rgba = bytes([int(v * 255) for v in render])
|
|
||||||
|
|
||||||
image = Image.frombuffer('RGBA', (WIDTH, HEIGHT), rgba)
|
|
||||||
image.save("test.png")
|
|
||||||
|
|
|
@ -1,58 +1,15 @@
|
||||||
use bytemuck::allocation::cast_slice_box;
|
use bytemuck::allocation::cast_slice_box;
|
||||||
use mechthild_core::{Camera, Scene, Shape, color::Rgba};
|
use mechthild_core::{Scene, color::Rgba};
|
||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
|
|
||||||
#[pyclass(name = "Camera")]
|
|
||||||
#[derive(Clone)]
|
|
||||||
struct PyCamera(Camera);
|
|
||||||
|
|
||||||
#[pymethods]
|
|
||||||
impl PyCamera {
|
|
||||||
#[new]
|
|
||||||
pub fn new(
|
|
||||||
eye: (f32, f32, f32),
|
|
||||||
center: (f32, f32, f32),
|
|
||||||
up: (f32, f32, f32),
|
|
||||||
fov: f32,
|
|
||||||
) -> Self {
|
|
||||||
Self(Camera::new(eye.into(), center.into(), up.into(), fov))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn __str__(&self) -> PyResult<String> {
|
|
||||||
Ok(format!("{:#?}", self.0))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[pyclass(name = "Shape")]
|
|
||||||
#[derive(Clone)]
|
|
||||||
struct PyShape(Shape);
|
|
||||||
|
|
||||||
#[pymethods]
|
|
||||||
impl PyShape {
|
|
||||||
#[staticmethod]
|
|
||||||
pub fn sphere(center: (f32, f32, f32), radius: f32) -> Self {
|
|
||||||
Self(Shape::sphere(center.into(), radius))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[staticmethod]
|
|
||||||
pub fn plane(normal: (f32, f32, f32), d: f32) -> Self {
|
|
||||||
Self(Shape::plane(normal.into(), d))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn __str__(&self) -> PyResult<String> {
|
|
||||||
Ok(format!("{:?}", self.0))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[pyclass(name = "Scene")]
|
#[pyclass(name = "Scene")]
|
||||||
struct PyScene(Scene);
|
struct PyScene(Scene);
|
||||||
|
|
||||||
#[pymethods]
|
#[pymethods]
|
||||||
impl PyScene {
|
impl PyScene {
|
||||||
#[new]
|
#[new]
|
||||||
pub fn new(camera: &PyCamera, contents: Vec<PyShape>) -> Self {
|
pub fn new() -> Self {
|
||||||
let contents = contents.into_iter().map(|s| s.0).collect();
|
Self(Scene::new())
|
||||||
Self(Scene::new(camera.0.clone(), contents))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&self, width: usize, height: usize) -> Vec<f32> {
|
pub fn render(&self, width: usize, height: usize) -> Vec<f32> {
|
||||||
|
@ -65,8 +22,6 @@ impl PyScene {
|
||||||
/// A Python module implemented in Rust.
|
/// A Python module implemented in Rust.
|
||||||
#[pymodule]
|
#[pymodule]
|
||||||
fn mechthild(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
fn mechthild(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
||||||
m.add_class::<PyCamera>()?;
|
|
||||||
m.add_class::<PyShape>()?;
|
|
||||||
m.add_class::<PyScene>()?;
|
m.add_class::<PyScene>()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue