Compare commits

..

4 Commits

Author SHA1 Message Date
5605ad0901 fix: environment warnings 2026-04-01 01:45:35 +02:00
3e5f23a3bf feat(rt-lisp): add more math overloads 2026-04-01 01:43:50 +02:00
5aeaf72af1 feat(macro): show invalid args 2026-04-01 01:24:47 +02:00
6e6a3e8a27 feat: update demo 2026-04-01 00:56:36 +02:00
5 changed files with 158 additions and 35 deletions

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

@@ -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)

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

@@ -232,6 +232,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 +267,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 +295,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 +303,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 +311,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 +319,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 +348,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 +356,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 +364,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,12 +372,98 @@ 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.
@@ -373,7 +493,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));
} }