Compare commits

...

11 Commits

15 changed files with 801 additions and 116 deletions

39
build.rs Normal file
View File

@@ -0,0 +1,39 @@
use std::path::Path;
fn copy_dir(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> {
std::fs::create_dir_all(&dst)?;
for entry in std::fs::read_dir(src)? {
let entry = entry?;
if entry.file_type()?.is_dir() {
copy_dir(entry.path(), dst.as_ref().join(entry.file_name()))?;
} else {
std::fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}
fn main() {
let use_local_scenes = std::env::var("LISPERS_USE_LOCAL_SCENES").unwrap_or_default() == "1";
let no_copy = std::env::var("LISPERS_DONT_COPY_SCENES").unwrap_or_default() == "1";
let out_dir = match std::env::var("LISPERS_OUT_DIR") {
Ok(val) => val,
Err(_) => std::env::var("OUT_DIR").unwrap(),
};
let mut scenes_dir = Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap())
.canonicalize()
.unwrap()
.join("scenes");
if !use_local_scenes {
let tgt_scenes_dir = Path::new(&out_dir).join("scenes");
if !no_copy {
copy_dir(&scenes_dir, &tgt_scenes_dir).expect("Failed to copy scenes directory");
}
scenes_dir = tgt_scenes_dir;
}
println!("cargo:rustc-env=SCENES_DIR={}", scenes_dir.display());
}

View File

@@ -79,7 +79,15 @@
overlays = [rust-overlay.overlays.default]; overlays = [rust-overlay.overlays.default];
}; };
packages.lispers = cargoNix.workspaceMembers.lispers.build; packages.lispers = cargoNix.workspaceMembers.lispers.build.overrideAttrs (attrs: {
preConfigure = ''
export LISPERS_OUT_DIR="$out"
export LISPERS_DONT_COPY_SCENES=1
'';
postInstall = ''
cp -r $src/scenes $out/scenes
'';
});
packages.default = self'.packages.lispers; packages.default = self'.packages.lispers;
apps = { apps = {
lisp_demo = { lisp_demo = {
@@ -94,7 +102,7 @@
type = "app"; type = "app";
program = "${self'.packages.lispers}/bin/rt_demo"; program = "${self'.packages.lispers}/bin/rt_demo";
}; };
rt_demo_lisp = { rt_lisp_demo = {
type = "app"; type = "app";
program = "${self'.packages.lispers}/bin/rt_lisp_demo"; program = "${self'.packages.lispers}/bin/rt_lisp_demo";
}; };
@@ -102,11 +110,13 @@
type = "app"; type = "app";
program = "${self'.packages.lispers}/bin/rt_interp"; program = "${self'.packages.lispers}/bin/rt_interp";
}; };
default = self'.apps.rt_demo_lisp; default = self'.apps.rt_lisp_demo;
}; };
devShells.default = pkgs.mkShell { devShells.default = pkgs.mkShell {
inputsFrom = [self'.packages.lispers]; shellHook = ''
export LISPERS_USE_LOCAL_SCENES=1
'';
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib"; LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
nativeBuildInputs = [rust-toolchain pkgs.pkg-config pkgs.ffmpeg_4]; nativeBuildInputs = [rust-toolchain pkgs.pkg-config pkgs.ffmpeg_4];
BINDGEN_EXTRA_CLANG_ARGS = [ BINDGEN_EXTRA_CLANG_ARGS = [

View File

@@ -1,5 +1,5 @@
use super::{expression::Expression, prelude::mk_prelude}; use super::{expression::Expression, prelude::mk_prelude};
use std::{cell::RefCell, collections::HashMap, env, path::Path, rc::Rc}; use std::{cell::RefCell, collections::HashMap, rc::Rc};
#[derive(PartialEq, Clone, Debug)] #[derive(PartialEq, Clone, Debug)]
/// A Environment is a stack of `EnvironmentLayer`s. Each `EnvironmentLayer` is a mapping from /// A Environment is a stack of `EnvironmentLayer`s. Each `EnvironmentLayer` is a mapping from
@@ -64,7 +64,7 @@ impl<'a> Environment<'a> {
} }
/// Construct a new `Environment` with `self` as the outer `Environment`. /// Construct a new `Environment` with `self` as the outer `Environment`.
pub fn mk_inner(&self) -> Environment { pub fn mk_inner(&'a self) -> Environment<'a> {
Environment { Environment {
layer: EnvironmentLayer::new(), layer: EnvironmentLayer::new(),
outer: Some(self), outer: Some(self),

View File

@@ -121,7 +121,7 @@ impl Display for ForeignDataStore {
} }
} }
#[derive(Clone, Debug, PartialEq, PartialOrd)] #[derive(Clone, Debug)]
/// A sum type of all possible lisp expressions. /// A sum type of all possible lisp expressions.
pub enum Expression { pub enum Expression {
/// The classic lisp cons cell aka (a . b) used to construct expressions. /// The classic lisp cons cell aka (a . b) used to construct expressions.
@@ -151,6 +151,60 @@ pub enum Expression {
Nil, Nil,
} }
impl PartialEq for Expression {
fn eq(&self, other: &Self) -> bool {
use Expression::*;
match (self, other) {
(Cell(a1, b1), Cell(a2, b2)) => PartialEq::eq(a1, a2) && PartialEq::eq(b1, b2),
(
AnonymousFunction {
argument_symbols: args1,
body: body1,
},
AnonymousFunction {
argument_symbols: args2,
body: body2,
},
) => PartialEq::eq(args1, args2) && PartialEq::eq(body1, body2),
(ForeignExpression(f1), ForeignExpression(f2)) => PartialEq::eq(f1, f2),
(Quote(e1), Quote(e2)) => PartialEq::eq(e1, e2),
(Symbol(s1), Symbol(s2)) => PartialEq::eq(s1, s2),
(Float(f1), Float(f2)) => PartialEq::eq(f1, f2),
(Nil, Nil) => true,
(True, True) => true,
_ => false,
}
}
}
impl PartialOrd for Expression {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
use Expression::*;
match (self, other) {
(Cell(a1, b1), Cell(a2, b2)) => a1.partial_cmp(a2).or_else(|| b1.partial_cmp(b2)),
(
AnonymousFunction {
argument_symbols: args1,
body: body1,
},
AnonymousFunction {
argument_symbols: args2,
body: body2,
},
) => args1
.partial_cmp(args2)
.or_else(|| body1.partial_cmp(body2)),
(ForeignExpression(f1), ForeignExpression(f2)) => f1.partial_cmp(f2),
(Quote(e1), Quote(e2)) => e1.partial_cmp(e2),
(Symbol(s1), Symbol(s2)) => s1.partial_cmp(s2),
(Float(f1), Float(f2)) => f1.partial_cmp(f2),
(Nil, Nil) => Some(std::cmp::Ordering::Equal),
(True, True) => Some(std::cmp::Ordering::Equal),
_ => None,
}
}
}
impl<T: ForeignData> From<ForeignDataWrapper<T>> for Expression { impl<T: ForeignData> From<ForeignDataWrapper<T>> for Expression {
fn from(value: ForeignDataWrapper<T>) -> Expression { fn from(value: ForeignDataWrapper<T>) -> Expression {
Expression::ForeignExpression(ForeignDataStore::new(value.0)) Expression::ForeignExpression(ForeignDataStore::new(value.0))

View File

@@ -186,7 +186,7 @@ pub fn native_lisp_function_proxy(item: TokenStream) -> TokenStream {
#(#try_apply_statements)* #(#try_apply_statements)*
Err(EvalError::TypeError(format!("No applicable method found for {}", #fname_str).to_string())) Err(EvalError::TypeError(format!("Could not call {} with arguments {} ", #fname_str, expr).to_string()))
} }
} }
.into() .into()

View File

@@ -9,8 +9,8 @@
(set 'mirror-dome (set 'mirror-dome
(sphere (sphere
(point 0 -30 0) (point 0 -17 0)
100 dark-mirror)) 30 dark-mirror))
(defun spiral-sphere (i n t) (defun spiral-sphere (i n t)
(sphere (sphere
@@ -57,11 +57,11 @@
(up . (vector 0 1 0)) (up . (vector 0 1 0))
(fovy . 80) (fovy . 80)
(pct . (/ t 300.0))) (pct . (/ t 300.0)))
(let '((tpos . (vadd pos (vmul (vsub to pos) pct))) (let '((tpos . (+ pos (* (- to pos) pct)))
(tfovy . (+ fovy (* 40 pct))) (tfovy . (+ fovy (* 40 pct)))
) )
(camera-reposition c tpos cnt up tfovy) (camera-reposition c tpos cnt up tfovy)
) )
)) ))
(render-animation cam "demo-animation.mp4" scene-fn cam-fn 400 30 4 2) (render-animation cam "demo-animation.mp4" scene-fn cam-fn 400 30 7 2)

69
scenes/demo-3.lisp Normal file
View File

@@ -0,0 +1,69 @@
(set 'red
(material
(color 1 0 0)
(color 1 0 0)
(color 0.5 0 0)
50 0.25))
(set 'blue
(material
(color 0 0 1)
(color 0 0 1)
(color 0 0 0.6)
50 0.25))
(set 'green
(material
(color 0 1 0)
(color 0 1 0)
(color 0 0.6 0)
50 0.25))
(set 'mandelbrot-red
(mandelbrot-texture
1800.0
(point2 -0.7489967346191402 -0.06952285766601607)
1000
(color 0.3 0 0)
(color 0.3 0 0)
(color 0.3 0 0)
))
(set 'mandelbrot-blue
(mandelbrot-texture
1800.0
(point2 -0.7489967346191402 -0.06952285766601607)
1000
(color 0 0 0.3)
(color 0 0 0.3)
(color 0 0 0.3)
))
(set 's1
(sphere
(point 0 1 0) 1 blue))
(set 's2
(sphere
(point 2 0.5 2) 0.5 green))
(set 'p1
(texture-plane
mandelbrot-red
(point 0 0 0)
(vector 0 1 0)
1.0
(vector 1 0 0)))
(set 'l1 (light (point 3 10 5) (color 1 1 1)))
(set 'l2 (light (point 2 10 5) (color 1 1 1)))
(set 'scn (scene
(color 0.1 0.1 0.1)
'(s1 s2 p1)
'(l1 l2)))
(println (cons "Final Scene:" scn))
(set 'cam (camera (point 0 3 6) (point 0 0 0) (vector 0 1 0) 40 1920 1080))
(render cam scn 5 4 "demo-3.png")

View File

@@ -21,17 +21,17 @@
(color 1 1 1) (color 1 1 1)
(color 1 1 1) (color 1 1 1)
(color 0.6 0.6 0.6) (color 0.6 0.6 0.6)
100 0.5)) 100 0.4))
(set 'black (set 'black
(material (material
(color 0 0 0) (color 0 0 0)
(color 0 0 0) (color 0 0 0)
(color 0.6 0.6 0.6) (color 0.6 0.6 0.6)
100 0.5)) 100 0.4))
(set 'dark-mirror (set 'dark-mirror
(material (material
(color 0 0 0) (color 0.01 0.05 0.15)
(color 0 0 0) (color 0.01 0.05 0.15)
(color 0.2 0.2 0.2) (color 0.01 0.05 0.15)
20 0.6)) 20 0.7))

View File

@@ -1,37 +1,51 @@
use std::collections::HashMap;
use std::path::Path;
use lispers::raytracer::lisp::mk_raytrace; use lispers::raytracer::lisp::mk_raytrace;
use lispers_core::lisp::environment::EnvironmentLayer; use lispers_core::lisp::environment::EnvironmentLayer;
use lispers_core::lisp::prelude::mk_prelude; use lispers_core::lisp::prelude::mk_prelude;
use lispers_core::lisp::{eval, Environment}; use lispers_core::lisp::{eval, Environment};
use lispers_core::parser::ExpressionStream; use lispers_core::parser::ExpressionStream;
const SCENES_DIR: &str = env!("SCENES_DIR");
fn main() { fn main() {
let programs = [ println!("Loading scenes from directory: {}", SCENES_DIR);
"(set 'red (material (color 1 0 0) (color 1 0 0) (color 0.5 0 0) 50 0.25))",
"(set 'blue (material (color 0 0 1) (color 0 0 1) (color 0 0 0.6) 50 0.25))", let mut scenes = HashMap::new();
"(set 'green (material (color 0 1 0) (color 0 1 0) (color 0 0.6 0) 50 0.25))", for e in std::fs::read_dir(Path::new(SCENES_DIR)).expect("Failed to read scenes directory") {
"(set 'white (material (color 1 1 1) (color 1 1 1) (color 0.6 0.6 0.6) 100 0.5))", let e = e.expect("Failed to read scene file");
"(set 'black (material (color 0 0 0) (color 0 0 0) (color 0.6 0.6 0.6) 100 0.5))", let t = e.file_type().expect("Failed to read scene file type");
"(set 's1 (sphere (point 0 1 0) 1 blue))", let n = e
"(set 's2 (sphere (point 2 0.5 2) 0.5 green))", .file_name()
"(defun spiral-sphere (i n) (sphere (print (point (* 2 (cos (/ (* i 6.2) n))) 0.5 (* 2 (sin (/ (* i 6.2) n))))) 0.5 red))", .into_string()
"(defun spiral (scn i n) (if (< i n) (scene-add (spiral scn (+ i 1) n) (spiral-sphere i n)) scn))", .expect("Failed to read scene file name");
"(set 'p1 (checkerboard (point 0 0 0) (vector 0 1 0) black white 0.5 (vector 0.5 0 1)))", if t.is_file() && n.starts_with("demo-") && n.ends_with(".lisp") {
"(set 'l1 (light (point 3 10 5) (color 1 1 1)))", scenes.insert(n, e);
"(set 'l2 (light (point 2 10 5) (color 1 1 1)))", }
"(set 'scn (scene (color 0.1 0.1 0.1) '(s1 s2 p1) '(l1 l2)))", }
"(set 'scn (spiral scn 0.0 10.0))",
"(print scn)",
"(set 'cam (camera (point 0 3 6) (point 0 0 0) (vector 0 1 0) 40 1920 1080))",
"(render cam scn 5 4 \"rt-lisp-demo.png\")",
];
let mut layer = EnvironmentLayer::new(); let mut layer = EnvironmentLayer::new();
mk_prelude(&mut layer); mk_prelude(&mut layer);
mk_raytrace(&mut layer); mk_raytrace(&mut layer);
let environment = Environment::from_layer(layer); let environment = Environment::from_layer(layer);
for r in ExpressionStream::from_char_stream(programs.iter().map(|p| p.chars()).flatten()) { let args: Vec<_> = std::env::args().collect();
if args.len() != 2 {
println!("Usage: {} <scene-file.lisp>", args[0]);
println!("Available scene files:");
for name in scenes.keys() {
println!(" {}", name);
}
return;
}
for r in ExpressionStream::from_char_stream(
std::fs::read_to_string(scenes.get(&args[1]).expect("Scene file not found").path())
.expect("Failed to read scene file")
.chars(),
) {
match r { match r {
Err(err) => { Err(err) => {
println!("ParserError: {:?}", err); println!("ParserError: {:?}", err);

View File

@@ -1,6 +1,11 @@
use std::path::PathBuf; use std::path::PathBuf;
use crate::raytracer::{scene::Scene, types::Light}; use crate::raytracer::{
scene::Scene,
sphere::TextureSphere,
texture::TextureWrapper,
types::{Light, Point2},
};
use lispers_macro::{native_lisp_function, native_lisp_function_proxy}; use lispers_macro::{native_lisp_function, native_lisp_function_proxy};
@@ -13,8 +18,9 @@ use lispers_core::lisp::{
use super::{ use super::{
camera::Camera, camera::Camera,
plane::{Checkerboard, Plane}, plane::{Checkerboard, Plane, TexturePlane},
sphere::Sphere, sphere::Sphere,
texture::MandelbrotTexture,
types::{Color, Material, Point3, RTObjectWrapper, Vector3}, types::{Color, Material, Point3, RTObjectWrapper, Vector3},
RTError, RTError,
}; };
@@ -24,6 +30,11 @@ pub fn point(x: f64, y: f64, z: f64) -> Result<ForeignDataWrapper<Point3>, EvalE
Ok(ForeignDataWrapper::new(Point3::new(x, y, z))) Ok(ForeignDataWrapper::new(Point3::new(x, y, z)))
} }
#[native_lisp_function(eval)]
pub fn point2(x: f64, y: f64) -> Result<ForeignDataWrapper<Point2>, EvalError> {
Ok(ForeignDataWrapper::new(Point2::new(x, y)))
}
#[native_lisp_function(eval)] #[native_lisp_function(eval)]
pub fn vector(x: f64, y: f64, z: f64) -> Result<ForeignDataWrapper<Vector3>, EvalError> { pub fn vector(x: f64, y: f64, z: f64) -> Result<ForeignDataWrapper<Vector3>, EvalError> {
Ok(ForeignDataWrapper::new(Vector3::new(x, y, z))) Ok(ForeignDataWrapper::new(Vector3::new(x, y, z)))
@@ -64,6 +75,22 @@ pub fn sphere(
Ok(ForeignDataWrapper::new(RTObjectWrapper::from(Sphere::new(*pos, rad, *mat))).into()) Ok(ForeignDataWrapper::new(RTObjectWrapper::from(Sphere::new(*pos, rad, *mat))).into())
} }
#[native_lisp_function(eval)]
pub fn texture_sphere(
pos: ForeignDataWrapper<Point3>,
rad: f64,
tex: ForeignDataWrapper<TextureWrapper>,
) -> Result<ForeignDataWrapper<RTObjectWrapper>, EvalError> {
Ok(
ForeignDataWrapper::new(RTObjectWrapper::from(TextureSphere::new(
*pos,
rad,
tex.clone(),
)))
.into(),
)
}
#[native_lisp_function(eval)] #[native_lisp_function(eval)]
pub fn plane( pub fn plane(
pos: ForeignDataWrapper<Point3>, pos: ForeignDataWrapper<Point3>,
@@ -90,6 +117,47 @@ pub fn checkerboard(
) )
} }
#[native_lisp_function(eval)]
pub fn texture_plane(
texture: ForeignDataWrapper<TextureWrapper>,
pos: ForeignDataWrapper<Point3>,
norm: ForeignDataWrapper<Vector3>,
sca: f64,
up: ForeignDataWrapper<Vector3>,
) -> Result<ForeignDataWrapper<RTObjectWrapper>, EvalError> {
Ok(
ForeignDataWrapper::new(RTObjectWrapper::from(TexturePlane::new(
*pos,
*norm,
texture.clone(),
sca,
*up,
)))
.into(),
)
}
#[native_lisp_function(eval)]
pub fn mandelbrot_texture(
scale: f64,
at: ForeignDataWrapper<Point2>,
max_iter: i64,
ambient_color: ForeignDataWrapper<Color>,
diffuse_color: ForeignDataWrapper<Color>,
specular_color: ForeignDataWrapper<Color>,
) -> Result<ForeignDataWrapper<TextureWrapper>, EvalError> {
Ok(ForeignDataWrapper::new(TextureWrapper::new(
MandelbrotTexture::new(
scale,
*at,
max_iter as u32,
*ambient_color,
*diffuse_color,
*specular_color,
),
)))
}
pub fn scene(env: &Environment, expr: Expression) -> Result<Expression, EvalError> { pub fn scene(env: &Environment, expr: Expression) -> Result<Expression, EvalError> {
let [amb, objs, lgts]: [Expression; 3] = expr.try_into()?; let [amb, objs, lgts]: [Expression; 3] = expr.try_into()?;
@@ -232,6 +300,16 @@ pub fn cos(x: f64) -> Result<f64, EvalError> {
Ok(x.cos()) Ok(x.cos())
} }
#[native_lisp_function(eval)]
pub fn add_i(x: i64, y: i64) -> Result<i64, EvalError> {
Ok(x + y)
}
#[native_lisp_function(eval)]
pub fn add_f(x: f64, y: f64) -> Result<f64, EvalError> {
Ok(x + y)
}
#[native_lisp_function] #[native_lisp_function]
pub fn vadd_vv( pub fn vadd_vv(
a: ForeignDataWrapper<Vector3>, a: ForeignDataWrapper<Vector3>,
@@ -257,15 +335,27 @@ pub fn vadd_pv(
} }
native_lisp_function_proxy!( native_lisp_function_proxy!(
fname = vadd, fname = add,
eval, eval,
dispatch = add_i,
dispatch = add_f,
dispatch = vadd_vv, dispatch = vadd_vv,
dispatch = vadd_vp, dispatch = vadd_vp,
dispatch = vadd_pv dispatch = vadd_pv
); );
#[native_lisp_function(eval)]
pub fn sub_i(x: i64, y: i64) -> Result<i64, EvalError> {
Ok(x - y)
}
#[native_lisp_function(eval)]
pub fn sub_f(x: f64, y: f64) -> Result<f64, EvalError> {
Ok(x - y)
}
#[native_lisp_function] #[native_lisp_function]
pub fn vsub_vv( pub fn sub_vv(
a: ForeignDataWrapper<Vector3>, a: ForeignDataWrapper<Vector3>,
b: ForeignDataWrapper<Vector3>, b: ForeignDataWrapper<Vector3>,
) -> Result<ForeignDataWrapper<Vector3>, EvalError> { ) -> Result<ForeignDataWrapper<Vector3>, EvalError> {
@@ -273,7 +363,7 @@ pub fn vsub_vv(
} }
#[native_lisp_function] #[native_lisp_function]
pub fn vsub_vp( pub fn sub_vp(
a: ForeignDataWrapper<Vector3>, a: ForeignDataWrapper<Vector3>,
b: ForeignDataWrapper<Point3>, b: ForeignDataWrapper<Point3>,
) -> Result<ForeignDataWrapper<Point3>, EvalError> { ) -> Result<ForeignDataWrapper<Point3>, EvalError> {
@@ -281,7 +371,7 @@ pub fn vsub_vp(
} }
#[native_lisp_function] #[native_lisp_function]
pub fn vsub_pv( pub fn sub_pv(
a: ForeignDataWrapper<Point3>, a: ForeignDataWrapper<Point3>,
b: ForeignDataWrapper<Vector3>, b: ForeignDataWrapper<Vector3>,
) -> Result<ForeignDataWrapper<Point3>, EvalError> { ) -> Result<ForeignDataWrapper<Point3>, EvalError> {
@@ -289,7 +379,7 @@ pub fn vsub_pv(
} }
#[native_lisp_function] #[native_lisp_function]
pub fn vsub_pp( pub fn sub_pp(
a: ForeignDataWrapper<Point3>, a: ForeignDataWrapper<Point3>,
b: ForeignDataWrapper<Point3>, b: ForeignDataWrapper<Point3>,
) -> Result<ForeignDataWrapper<Vector3>, EvalError> { ) -> Result<ForeignDataWrapper<Vector3>, EvalError> {
@@ -297,16 +387,28 @@ pub fn vsub_pp(
} }
native_lisp_function_proxy!( native_lisp_function_proxy!(
fname = vsub, fname = sub,
eval, eval,
dispatch = vsub_vv, dispatch = sub_i,
dispatch = vsub_vp, dispatch = sub_f,
dispatch = vsub_pv, dispatch = sub_vv,
dispatch = vsub_pp dispatch = sub_vp,
dispatch = sub_pv,
dispatch = sub_pp
); );
#[native_lisp_function(eval)]
pub fn mul_i(x: i64, y: i64) -> Result<i64, EvalError> {
Ok(x * y)
}
#[native_lisp_function(eval)]
pub fn mul_f(x: f64, y: f64) -> Result<f64, EvalError> {
Ok(x * y)
}
#[native_lisp_function] #[native_lisp_function]
pub fn vmul_vs( pub fn mul_vs(
a: ForeignDataWrapper<Vector3>, a: ForeignDataWrapper<Vector3>,
b: f64, b: f64,
) -> Result<ForeignDataWrapper<Vector3>, EvalError> { ) -> Result<ForeignDataWrapper<Vector3>, EvalError> {
@@ -314,7 +416,7 @@ pub fn vmul_vs(
} }
#[native_lisp_function] #[native_lisp_function]
pub fn vmul_sv( pub fn mul_sv(
a: f64, a: f64,
b: ForeignDataWrapper<Vector3>, b: ForeignDataWrapper<Vector3>,
) -> Result<ForeignDataWrapper<Vector3>, EvalError> { ) -> Result<ForeignDataWrapper<Vector3>, EvalError> {
@@ -322,7 +424,7 @@ pub fn vmul_sv(
} }
#[native_lisp_function] #[native_lisp_function]
pub fn vmul_ps( pub fn mul_ps(
a: ForeignDataWrapper<Point3>, a: ForeignDataWrapper<Point3>,
b: f64, b: f64,
) -> Result<ForeignDataWrapper<Point3>, EvalError> { ) -> Result<ForeignDataWrapper<Point3>, EvalError> {
@@ -330,7 +432,7 @@ pub fn vmul_ps(
} }
#[native_lisp_function] #[native_lisp_function]
pub fn vmul_sp( pub fn mul_sp(
a: f64, a: f64,
b: ForeignDataWrapper<Point3>, b: ForeignDataWrapper<Point3>,
) -> Result<ForeignDataWrapper<Point3>, EvalError> { ) -> Result<ForeignDataWrapper<Point3>, EvalError> {
@@ -338,17 +440,104 @@ pub fn vmul_sp(
} }
native_lisp_function_proxy!( native_lisp_function_proxy!(
fname = vmul, fname = mul,
eval, eval,
dispatch = vmul_vs, dispatch = mul_i,
dispatch = vmul_sv, dispatch = mul_f,
dispatch = vmul_ps, dispatch = mul_vs,
dispatch = vmul_sp dispatch = mul_sv,
dispatch = mul_ps,
dispatch = mul_sp
);
#[native_lisp_function(eval)]
pub fn div_i(x: i64, y: i64) -> Result<f64, EvalError> {
Ok(x as f64 / y as f64)
}
#[native_lisp_function(eval)]
pub fn div_f(x: f64, y: f64) -> Result<f64, EvalError> {
Ok(x / y)
}
#[native_lisp_function]
pub fn div_vs(
a: ForeignDataWrapper<Vector3>,
b: f64,
) -> Result<ForeignDataWrapper<Vector3>, EvalError> {
Ok(ForeignDataWrapper::new(*a / b))
}
#[native_lisp_function]
pub fn div_sv(
a: f64,
b: ForeignDataWrapper<Vector3>,
) -> Result<ForeignDataWrapper<Vector3>, EvalError> {
Ok(ForeignDataWrapper::new(*b / a))
}
#[native_lisp_function]
pub fn div_ps(
a: ForeignDataWrapper<Point3>,
b: f64,
) -> Result<ForeignDataWrapper<Point3>, EvalError> {
Ok(ForeignDataWrapper::new(*a / b))
}
#[native_lisp_function]
pub fn div_sp(
a: f64,
b: ForeignDataWrapper<Point3>,
) -> Result<ForeignDataWrapper<Point3>, EvalError> {
Ok(ForeignDataWrapper::new(*b / a))
}
native_lisp_function_proxy!(
fname = div,
eval,
dispatch = div_i,
dispatch = div_f,
dispatch = div_vs,
dispatch = div_sv,
dispatch = div_ps,
dispatch = div_sp
);
#[native_lisp_function(eval)]
pub fn dot(
a: ForeignDataWrapper<Vector3>,
b: ForeignDataWrapper<Vector3>,
) -> Result<f64, EvalError> {
Ok(a.dot(&b))
}
#[native_lisp_function]
pub fn abs_i(a: i64) -> Result<i64, EvalError> {
Ok(a.abs())
}
#[native_lisp_function]
pub fn abs_f(a: f64) -> Result<f64, EvalError> {
Ok(a.abs())
}
#[native_lisp_function]
pub fn abs_v(a: ForeignDataWrapper<Vector3>) -> Result<f64, EvalError> {
Ok(a.dot(&a).sqrt())
}
native_lisp_function_proxy!(
fname = abs,
eval,
dispatch = abs_i,
dispatch = abs_f,
dispatch = abs_v
); );
/// Adds the raytracing functions to the given environment layer. /// Adds the raytracing functions to the given environment layer.
pub fn mk_raytrace(layer: &mut EnvironmentLayer) { pub fn mk_raytrace(layer: &mut EnvironmentLayer) {
layer.set("point".to_string(), Expression::Function(point)); layer.set("point".to_string(), Expression::Function(point));
layer.set("point2".to_string(), Expression::Function(point2));
layer.set("vector".to_string(), Expression::Function(vector)); layer.set("vector".to_string(), Expression::Function(vector));
layer.set("color".to_string(), Expression::Function(color)); layer.set("color".to_string(), Expression::Function(color));
layer.set("light".to_string(), Expression::Function(light)); layer.set("light".to_string(), Expression::Function(light));
@@ -358,7 +547,19 @@ pub fn mk_raytrace(layer: &mut EnvironmentLayer) {
"checkerboard".to_string(), "checkerboard".to_string(),
Expression::Function(checkerboard), Expression::Function(checkerboard),
); );
layer.set(
"texture-plane".to_string(),
Expression::Function(texture_plane),
);
layer.set(
"mandelbrot-texture".to_string(),
Expression::Function(mandelbrot_texture),
);
layer.set("sphere".to_string(), Expression::Function(sphere)); layer.set("sphere".to_string(), Expression::Function(sphere));
layer.set(
"texture-sphere".to_string(),
Expression::Function(texture_sphere),
);
layer.set("scene".to_string(), Expression::Function(scene)); layer.set("scene".to_string(), Expression::Function(scene));
layer.set("scene-add".to_string(), Expression::Function(scene_add)); layer.set("scene-add".to_string(), Expression::Function(scene_add));
layer.set("camera".to_string(), Expression::Function(camera)); layer.set("camera".to_string(), Expression::Function(camera));
@@ -373,7 +574,10 @@ pub fn mk_raytrace(layer: &mut EnvironmentLayer) {
); );
layer.set("sin".to_string(), Expression::Function(sin)); layer.set("sin".to_string(), Expression::Function(sin));
layer.set("cos".to_string(), Expression::Function(cos)); layer.set("cos".to_string(), Expression::Function(cos));
layer.set("vadd".to_string(), Expression::Function(vadd)); layer.set("+".to_string(), Expression::Function(add));
layer.set("vsub".to_string(), Expression::Function(vsub)); layer.set("-".to_string(), Expression::Function(sub));
layer.set("vmul".to_string(), Expression::Function(vmul)); layer.set("*".to_string(), Expression::Function(mul));
layer.set("/".to_string(), Expression::Function(div));
layer.set("dot".to_string(), Expression::Function(dot));
layer.set("abs".to_string(), Expression::Function(abs));
} }

View File

@@ -3,6 +3,7 @@ pub mod lisp;
pub mod plane; pub mod plane;
pub mod scene; pub mod scene;
pub mod sphere; pub mod sphere;
mod texture;
pub mod types; pub mod types;
mod vec; mod vec;

View File

@@ -1,4 +1,7 @@
use super::types::{Intersect, Material, Point3, Scalar, Vector3}; use super::{
texture::TextureWrapper,
types::{Intersect, Material, Point3, Scalar, Vector3},
};
extern crate nalgebra as na; extern crate nalgebra as na;
@@ -26,6 +29,21 @@ pub struct Checkerboard {
projection_matrix: na::Matrix2x3<Scalar>, projection_matrix: na::Matrix2x3<Scalar>,
} }
/// Define a plane using a 2D texture function
#[derive(Clone)]
pub struct TexturePlane {
/// The position of the plane.
position: Point3,
/// The normal of the plane.
normal: Vector3,
/// The scale of the plane (factor for x,y passed to material function)
scale: f64,
/// A projection matrix to map 3D points to the 2D plane space.
projection_matrix: na::Matrix2x3<Scalar>,
/// The texture to use.
texture: TextureWrapper,
}
impl Plane { impl Plane {
/// Create a new plane. /// Create a new plane.
/// - `position` is the position of the plane. /// - `position` is the position of the plane.
@@ -66,6 +84,49 @@ impl Checkerboard {
} }
} }
impl TexturePlane {
/// Create a new Function Plane.
/// - `position` is the position of the plane.
/// - `normal` is the normal of the plane.
/// - `texture` the texture to use
/// - `scale` is the side-length of each square.
/// - `up` is "y" direction on the plane in 3D-Space.
pub fn new(
position: Point3,
normal: Vector3,
texture: TextureWrapper,
scale: f64,
up: Vector3,
) -> TexturePlane {
let right = up.cross(&normal).normalize();
TexturePlane {
position,
normal,
scale,
projection_matrix: na::Matrix3x2::from_columns(&[right, up]).transpose(),
texture,
}
}
}
fn plane_intersect(
position: Point3,
normal: Vector3,
ray: &super::types::Ray,
) -> Option<(Point3, Vector3, super::types::Scalar)> {
let denom = normal.dot(&ray.direction);
if denom != 0.0 {
let d = normal.dot(&position.coords);
let t = (d - normal.dot(&ray.origin.coords)) / denom;
if t > 1e-5 {
let point = ray.origin + ray.direction * t;
return Some((point, normal, t));
}
}
None
}
impl Intersect for Plane { impl Intersect for Plane {
fn intersect( fn intersect(
&self, &self,
@@ -76,17 +137,11 @@ impl Intersect for Plane {
super::types::Scalar, super::types::Scalar,
super::types::Material, super::types::Material,
)> { )> {
let denom = self.normal.dot(&ray.direction); if let Some((point, normal, t)) = plane_intersect(self.position, self.normal, ray) {
if denom != 0.0 { Some((point, normal, t, self.material.clone()))
let d = self.normal.dot(&self.position.coords); } else {
let t = (d - self.normal.dot(&ray.origin.coords)) / denom; None
if t > 1e-5 {
let point = ray.origin + ray.direction * t;
return Some((point, self.normal, t, self.material.clone()));
}
} }
None
} }
} }
@@ -117,6 +172,29 @@ impl Intersect for Checkerboard {
} }
} }
impl Intersect for TexturePlane {
fn intersect(
&self,
ray: &super::types::Ray,
) -> Option<(
Point3,
Vector3,
super::types::Scalar,
super::types::Material,
)> {
if let Some((point, normal, t)) = plane_intersect(self.position, self.normal, ray) {
let v3 = point - self.position;
let v2 = self.projection_matrix * v3;
let material = self
.texture
.material_at(na::Point2::new(v2.x / self.scale, v2.y / self.scale));
Some((point, normal, t, material))
} else {
None
}
}
}
impl std::fmt::Display for Plane { impl std::fmt::Display for Plane {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!( write!(
@@ -137,6 +215,26 @@ impl std::fmt::Display for Checkerboard {
} }
} }
impl std::fmt::Debug for TexturePlane {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"(function-plane position: {:?}, normal: {:?}, scale: {:?})",
self.position, self.normal, self.scale,
)
}
}
impl std::fmt::Display for TexturePlane {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"(function-plane position: {}, normal: {}, scale: {})",
self.position, self.normal, self.scale,
)
}
}
impl PartialOrd for Plane { impl PartialOrd for Plane {
fn partial_cmp(&self, _other: &Self) -> Option<std::cmp::Ordering> { fn partial_cmp(&self, _other: &Self) -> Option<std::cmp::Ordering> {
None None
@@ -148,3 +246,18 @@ impl PartialOrd for Checkerboard {
None None
} }
} }
impl PartialEq for TexturePlane {
fn eq(&self, other: &Self) -> bool {
self.normal == other.normal
&& self.position == other.position
&& self.projection_matrix == other.projection_matrix
&& self.scale == other.scale
}
}
impl PartialOrd for TexturePlane {
fn partial_cmp(&self, _other: &Self) -> Option<std::cmp::Ordering> {
None
}
}

View File

@@ -1,6 +1,7 @@
use nalgebra::distance; use super::{
texture::TextureWrapper,
use super::types::{Intersect, Material, Point3, Ray, Scalar, Vector3}; types::{Intersect, Material, Point2, Point3, Ray, Scalar, Vector3},
};
extern crate nalgebra as na; extern crate nalgebra as na;
@@ -15,6 +16,17 @@ pub struct Sphere {
material: Material, material: Material,
} }
/// A sphere in 3D space
#[derive(PartialEq, Clone, Debug)]
pub struct TextureSphere {
/// Center of the sphere
center: Point3,
/// Radius of the sphere
radius: Scalar,
/// texture of the sphere
texture: TextureWrapper,
}
impl Sphere { impl Sphere {
/// Create a new sphere at `center` with `radius` and `material`. /// Create a new sphere at `center` with `radius` and `material`.
pub fn new(center: Point3, radius: Scalar, material: Material) -> Sphere { pub fn new(center: Point3, radius: Scalar, material: Material) -> Sphere {
@@ -29,50 +41,47 @@ impl Sphere {
/// Numerical error tolerance /// Numerical error tolerance
const EPSILON: Scalar = 1e-5; const EPSILON: Scalar = 1e-5;
impl Intersect for Sphere { fn intersect(ray: &Ray, center: &Point3, radius: Scalar) -> Option<(Point3, Vector3, Scalar)> {
fn intersect(&self, ray: &Ray) -> Option<(Point3, Vector3, Scalar, Material)> { let co = ray.origin - center;
let co = ray.origin - self.center;
let a = ray.direction.dot(&ray.direction); let a = ray.direction.dot(&ray.direction);
let b = 2.0 * ray.direction.dot(&co); let b = 2.0 * ray.direction.dot(&co);
let c = co.dot(&co) - (self.radius * self.radius); let c = co.dot(&co) - (radius * radius);
let d = b * b - 4.0 * a * c; let d = b * b - 4.0 * a * c;
if d >= 0.0 { if d >= 0.0 {
let e = d.sqrt(); let e = d.sqrt();
let t1 = (-b - e) / (2.0 * a); let t1 = (-b - e) / (2.0 * a);
let t2 = (-b + e) / (2.0 * a); let t2 = (-b + e) / (2.0 * a);
let mut t = Scalar::MAX; let mut t = Scalar::MAX;
if t1 > EPSILON && t1 < t { if t1 > EPSILON && t1 < t {
t = t1; t = t1;
} }
if t2 > EPSILON && t2 < t { if t2 > EPSILON && t2 < t {
t = t2; t = t2;
}
if t < Scalar::MAX {
let isect_pt: Point3 = ray.origin + ray.direction * t;
if c >= 0.0 {
return Some((
isect_pt,
(isect_pt - self.center) / self.radius,
t,
self.material.clone(),
));
} else {
return Some((
isect_pt,
-(isect_pt - self.center) / self.radius,
t,
self.material.clone(),
));
}
}
} }
None if t < Scalar::MAX {
let isect_pt: Point3 = ray.origin + ray.direction * t;
if c >= 0.0 {
return Some((isect_pt, (isect_pt - center) / radius, t));
} else {
return Some((isect_pt, -(isect_pt - center) / radius, t));
}
}
}
None
}
impl Intersect for Sphere {
fn intersect(&self, ray: &Ray) -> Option<(Point3, Vector3, Scalar, Material)> {
match intersect(ray, &self.center, self.radius) {
Some((isect_pt, normal, t)) => Some((isect_pt, normal, t, self.material.clone())),
None => None,
}
} }
} }
@@ -91,3 +100,46 @@ impl PartialOrd for Sphere {
None None
} }
} }
impl TextureSphere {
/// Create a new sphere at `center` with `radius` and `texture`.
pub fn new(center: Point3, radius: Scalar, texture: TextureWrapper) -> TextureSphere {
TextureSphere {
center,
radius,
texture,
}
}
}
impl Intersect for TextureSphere {
fn intersect(&self, ray: &Ray) -> Option<(Point3, Vector3, Scalar, Material)> {
match intersect(ray, &self.center, self.radius) {
Some((isect_pt, normal, t)) => {
let n_isect_pt = (isect_pt - self.center) / self.radius;
let uv: Point2 = Point2::new(
0.5 + (n_isect_pt.z.atan2(n_isect_pt.x) / (2.0 * std::f64::consts::PI)),
0.5 - (n_isect_pt.y).asin() / std::f64::consts::PI,
);
Some((isect_pt, normal, t, self.texture.material_at(uv)))
}
None => None,
}
}
}
impl std::fmt::Display for TextureSphere {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"(sphere center: {}, radius: {}, texture: {})",
self.center, self.radius, self.texture
)
}
}
impl PartialOrd for TextureSphere {
fn partial_cmp(&self, _other: &Self) -> Option<std::cmp::Ordering> {
None
}
}

127
src/raytracer/texture.rs Normal file
View File

@@ -0,0 +1,127 @@
use std::fmt::Debug;
use std::fmt::Display;
use std::sync::Arc;
use as_any::AsAny;
use nalgebra as na;
use super::types::Color;
use super::types::Material;
use super::types::Point2;
use super::types::Scalar;
pub trait Texture: Display + Debug + AsAny + Sync + Send {
fn material_at(&self, pt: Point2) -> Material;
}
#[derive(Clone)]
pub struct TextureWrapper(Arc<dyn Texture>);
impl TextureWrapper {
pub fn new<T: Texture>(texture: T) -> Self {
Self(Arc::new(texture))
}
}
impl Display for TextureWrapper {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.0, f)
}
}
impl Debug for TextureWrapper {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.0, f)
}
}
impl TextureWrapper {
pub fn material_at(&self, pt: Point2) -> Material {
self.0.material_at(pt)
}
}
impl PartialEq for TextureWrapper {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.0, &other.0)
}
}
impl PartialOrd for TextureWrapper {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
PartialOrd::partial_cmp(&Arc::as_ptr(&self.0).addr(), &Arc::as_ptr(&other.0).addr())
}
}
pub struct MandelbrotTexture {
scale: Scalar,
at: Point2,
max_iter: u32,
ambient_color: Color,
diffuse_color: Color,
specular_color: Color,
}
impl MandelbrotTexture {
pub fn new(
scale: Scalar,
at: Point2,
max_iter: u32,
ambient_color: Color,
diffuse_color: Color,
specular_color: Color,
) -> Self {
Self {
scale,
at,
max_iter,
ambient_color,
diffuse_color,
specular_color,
}
}
}
impl Texture for MandelbrotTexture {
fn material_at(&self, pt: Point2) -> Material {
let x = (pt.x / self.scale) + self.at.x;
let y = (pt.y / self.scale) + self.at.y;
let mut z = na::Vector2::new(0.0, 0.0);
let mut n = 0;
while z.norm() < 2.0 && n < self.max_iter {
let xtemp = z.x * z.x - z.y * z.y + x;
z.y = 2.0 * z.x * z.y + y;
z.x = xtemp;
n += 1;
}
let c = n as f64 / self.max_iter as f64;
Material {
ambient_color: self.ambient_color * c,
diffuse_color: self.diffuse_color * c,
specular_color: self.specular_color * c,
shininess: (1.0 - c) * 10.0,
mirror: 1.0 - c,
}
}
}
impl Display for MandelbrotTexture {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"MandelbrotTexture{{at={}, max_iter={}}}",
self.at, self.max_iter
)
}
}
impl Debug for MandelbrotTexture {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"MandelbrotTexture{{at={:?}, max_iter={:?}}}",
self.at, self.max_iter
)
}
}

View File

@@ -10,6 +10,8 @@ pub type Scalar = f64;
pub type Vector3 = na::Vector3<Scalar>; pub type Vector3 = na::Vector3<Scalar>;
/// The Point3 type to use for raytracing /// The Point3 type to use for raytracing
pub type Point3 = na::Point3<Scalar>; pub type Point3 = na::Point3<Scalar>;
/// The Point2 type to use for texture lookups
pub type Point2 = na::Point2<Scalar>;
/// The Color type to use for raytracing /// The Color type to use for raytracing
pub type Color = Vector3; pub type Color = Vector3;