Compare commits

..

5 Commits

Author SHA1 Message Date
5c5c81325d fix(build): app name 2026-04-02 19:19:26 +02:00
d1211b7157 fix(texture): display inf rec 2026-04-02 19:17:23 +02:00
99bf883eeb fix(flake): default app name 2026-04-02 18:51:15 +02:00
078ad2c401 build: install scenes properly 2026-04-02 18:17:22 +02:00
3a49460fe6 feat(sphere): add texture sphere 2026-04-02 15:10:09 +02:00
7 changed files with 232 additions and 95 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];
};
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;
apps = {
lisp_demo = {
@@ -94,7 +102,7 @@
type = "app";
program = "${self'.packages.lispers}/bin/rt_demo";
};
rt_demo_lisp = {
rt_lisp_demo = {
type = "app";
program = "${self'.packages.lispers}/bin/rt_lisp_demo";
};
@@ -102,11 +110,13 @@
type = "app";
program = "${self'.packages.lispers}/bin/rt_interp";
};
default = self'.apps.rt_demo_lisp;
default = self'.apps.rt_lisp_demo;
};
devShells.default = pkgs.mkShell {
inputsFrom = [self'.packages.lispers];
shellHook = ''
export LISPERS_USE_LOCAL_SCENES=1
'';
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
nativeBuildInputs = [rust-toolchain pkgs.pkg-config pkgs.ffmpeg_4];
BINDGEN_EXTRA_CLANG_ARGS = [

View File

@@ -17,32 +17,7 @@
(color 0 0.6 0)
50 0.25))
(set 's1
(sphere
(point 0 1 0) 1 blue))
(set 's2
(sphere
(point 2 0.5 2) 0.5 green))
(defun spiral-sphere (i n)
(sphere
(progn
(print "Spiral Sphere at: ")
(println (point
(* 2 (cos (/ (* i 6.2) n)))
0.5
(* 2 (sin (/ (* i 6.2) n)))))
)
0.5 red))
(defun spiral (scn i n)
(if (< i n)
(scene-add
(spiral scn (+ i 1) n)
(spiral-sphere i n))
scn))
(set 'mandelbrot
(set 'mandelbrot-red
(mandelbrot-texture
1800.0
(point2 -0.7489967346191402 -0.06952285766601607)
@@ -52,9 +27,27 @@
(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
mandelbrot-red
(point 0 0 0)
(vector 0 1 0)
1.0

View File

@@ -1,37 +1,51 @@
use std::collections::HashMap;
use std::path::Path;
use lispers::raytracer::lisp::mk_raytrace;
use lispers_core::lisp::environment::EnvironmentLayer;
use lispers_core::lisp::prelude::mk_prelude;
use lispers_core::lisp::{eval, Environment};
use lispers_core::parser::ExpressionStream;
const SCENES_DIR: &str = env!("SCENES_DIR");
fn main() {
let programs = [
"(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 'white (material (color 1 1 1) (color 1 1 1) (color 0.6 0.6 0.6) 100 0.5))",
"(set 'black (material (color 0 0 0) (color 0 0 0) (color 0.6 0.6 0.6) 100 0.5))",
"(set 's1 (sphere (point 0 1 0) 1 blue))",
"(set 's2 (sphere (point 2 0.5 2) 0.5 green))",
"(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))",
"(defun spiral (scn i n) (if (< i n) (scene-add (spiral scn (+ i 1) n) (spiral-sphere i n)) scn))",
"(set 'p1 (checkerboard (point 0 0 0) (vector 0 1 0) black white 0.5 (vector 0.5 0 1)))",
"(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)))",
"(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\")",
];
println!("Loading scenes from directory: {}", SCENES_DIR);
let mut scenes = HashMap::new();
for e in std::fs::read_dir(Path::new(SCENES_DIR)).expect("Failed to read scenes directory") {
let e = e.expect("Failed to read scene file");
let t = e.file_type().expect("Failed to read scene file type");
let n = e
.file_name()
.into_string()
.expect("Failed to read scene file name");
if t.is_file() && n.starts_with("demo-") && n.ends_with(".lisp") {
scenes.insert(n, e);
}
}
let mut layer = EnvironmentLayer::new();
mk_prelude(&mut layer);
mk_raytrace(&mut 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 {
Err(err) => {
println!("ParserError: {:?}", err);

View File

@@ -2,6 +2,7 @@ use std::path::PathBuf;
use crate::raytracer::{
scene::Scene,
sphere::TextureSphere,
texture::TextureWrapper,
types::{Light, Point2},
};
@@ -74,6 +75,22 @@ pub fn sphere(
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)]
pub fn plane(
pos: ForeignDataWrapper<Point3>,
@@ -539,6 +556,10 @@ pub fn mk_raytrace(layer: &mut EnvironmentLayer) {
Expression::Function(mandelbrot_texture),
);
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-add".to_string(), Expression::Function(scene_add));
layer.set("camera".to_string(), Expression::Function(camera));

View File

@@ -1,4 +1,7 @@
use super::types::{Intersect, Material, Point3, Ray, Scalar, Vector3};
use super::{
texture::TextureWrapper,
types::{Intersect, Material, Point2, Point3, Ray, Scalar, Vector3},
};
extern crate nalgebra as na;
@@ -13,6 +16,17 @@ pub struct Sphere {
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 {
/// Create a new sphere at `center` with `radius` and `material`.
pub fn new(center: Point3, radius: Scalar, material: Material) -> Sphere {
@@ -27,50 +41,47 @@ impl Sphere {
/// Numerical error tolerance
const EPSILON: Scalar = 1e-5;
impl Intersect for Sphere {
fn intersect(&self, ray: &Ray) -> Option<(Point3, Vector3, Scalar, Material)> {
let co = ray.origin - self.center;
fn intersect(ray: &Ray, center: &Point3, radius: Scalar) -> Option<(Point3, Vector3, Scalar)> {
let co = ray.origin - center;
let a = ray.direction.dot(&ray.direction);
let b = 2.0 * ray.direction.dot(&co);
let c = co.dot(&co) - (self.radius * self.radius);
let d = b * b - 4.0 * a * c;
let a = ray.direction.dot(&ray.direction);
let b = 2.0 * ray.direction.dot(&co);
let c = co.dot(&co) - (radius * radius);
let d = b * b - 4.0 * a * c;
if d >= 0.0 {
let e = d.sqrt();
let t1 = (-b - e) / (2.0 * a);
let t2 = (-b + e) / (2.0 * a);
let mut t = Scalar::MAX;
if d >= 0.0 {
let e = d.sqrt();
let t1 = (-b - e) / (2.0 * a);
let t2 = (-b + e) / (2.0 * a);
let mut t = Scalar::MAX;
if t1 > EPSILON && t1 < t {
t = t1;
}
if t2 > EPSILON && t2 < t {
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(),
));
}
}
if t1 > EPSILON && t1 < t {
t = t1;
}
if t2 > EPSILON && t2 < t {
t = t2;
}
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,
}
}
}
@@ -89,3 +100,46 @@ impl PartialOrd for Sphere {
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
}
}

View File

@@ -14,7 +14,7 @@ pub trait Texture: Display + Debug + AsAny + Sync + Send {
fn material_at(&self, pt: Point2) -> Material;
}
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct TextureWrapper(Arc<dyn Texture>);
impl TextureWrapper {
@@ -25,7 +25,13 @@ impl TextureWrapper {
impl Display for TextureWrapper {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self, f)
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)
}
}
@@ -114,7 +120,7 @@ impl Debug for MandelbrotTexture {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"MandelbrotTexture{{at={}, max_iter={}}}",
"MandelbrotTexture{{at={:?}, max_iter={:?}}}",
self.at, self.max_iter
)
}