parent
0f1195ba08
commit
33da339432
After Width: | Height: | Size: 590 KiB |
@ -0,0 +1,52 @@ |
||||
local unpack = table.unpack or _G.unpack |
||||
math.randomseed(os.time()) |
||||
|
||||
local function randab(a, b) |
||||
return math.random() * (b-a) + a |
||||
end |
||||
|
||||
local function randtable(n, a, b) |
||||
local t = {} |
||||
for i=1, n do |
||||
t[i] = randab(a, b) |
||||
end |
||||
return t |
||||
end |
||||
|
||||
local function choice(list) |
||||
return list[math.random(1, #list)] |
||||
end |
||||
|
||||
local function randomsphere() |
||||
local pos = vec3.new(unpack(randtable(3, -2, 2))) |
||||
local radius = randab(0, 0.5) |
||||
local emission = colorvec.new(unpack(randtable(4, 0, 1))) |
||||
local reflection = colormat.new(unpack(randtable(16, 0, 1))) |
||||
local surfacetype = choice({surfacetype.DIFFUSE, surfacetype.REFLECTIVE, surfacetype.STOP}) |
||||
|
||||
return obj.withmaterial( |
||||
obj.sphere(pos, radius), |
||||
material.new(emission, reflection, surfacetype) |
||||
) |
||||
end |
||||
|
||||
local function union(objs) |
||||
local n = #objs |
||||
if n == 1 then return objs[1] end |
||||
|
||||
local mid = n//2 |
||||
local left, right = {}, {} |
||||
for i=1, mid do |
||||
left[i] = objs[i] |
||||
end |
||||
for i=mid+1, n do |
||||
right[i-mid] = objs[i] |
||||
end |
||||
return obj.union(union(left), union(right)) |
||||
end |
||||
|
||||
local spheres = {} |
||||
for i=1, 100 do |
||||
spheres[i] = randomsphere() |
||||
end |
||||
return union(spheres) |
@ -0,0 +1,47 @@ |
||||
use crate::structs::{ColorVec, ColorMat}; |
||||
use rlua::{UserData, Context, Table}; |
||||
|
||||
impl UserData for ColorVec {} |
||||
impl UserData for ColorMat {} |
||||
|
||||
pub fn color_vec(ctx: Context, _: ()) -> rlua::Result<Table> { |
||||
let module = ctx.create_table()?; |
||||
|
||||
module.set("new", ctx.create_function( |
||||
|ctx, (r, g, b, u)| Ok(ColorVec::new([r, g, b, u])) |
||||
)?)?; |
||||
|
||||
module.set("ZERO", ColorVec::new_zero())?; |
||||
|
||||
Ok(module) |
||||
} |
||||
|
||||
pub fn color_mat(ctx: Context, _: ()) -> rlua::Result<Table> { |
||||
let module = ctx.create_table()?; |
||||
|
||||
module.set("new", ctx.create_function( |
||||
|ctx, ( |
||||
a, b, c, d, |
||||
e, f, g, h, |
||||
i, j, k, l, |
||||
m, n, o, p |
||||
)| Ok(ColorMat::new([ |
||||
[a, b, c, d], |
||||
[e, f, g, h], |
||||
[i, j, k, l], |
||||
[m, n, o, p] |
||||
])) |
||||
)?)?; |
||||
|
||||
module.set("newfromvec", ctx.create_function( |
||||
|ctx, vec: ColorVec| Ok(ColorMat::new_from_diagonal(vec)) |
||||
)?)?; |
||||
|
||||
module.set("newfromdiagonal", ctx.create_function( |
||||
|ctx, (r, g, b, u)| Ok(ColorMat::new_from_diagonal(ColorVec::new([r, g, b, u]))) |
||||
)?)?; |
||||
|
||||
module.set("ZERO", ColorMat::new_zero())?; |
||||
|
||||
Ok(module) |
||||
} |
@ -0,0 +1,14 @@ |
||||
use crate::light::Light; |
||||
use rlua::{UserData, Context, Table}; |
||||
|
||||
impl UserData for Light {} |
||||
|
||||
pub fn light(ctx: Context, _: ()) -> rlua::Result<Table> { |
||||
let module = ctx.create_table()?; |
||||
|
||||
module.set("new", ctx.create_function( |
||||
|ctx, (pos, color)| Ok(Light::new(pos, color)) |
||||
)?)?; |
||||
|
||||
Ok(module) |
||||
} |
@ -0,0 +1,43 @@ |
||||
use crate::structs::{Mat3, I3, O3}; |
||||
use rlua::{UserData, UserDataMethods, Context, MetaMethod, Table}; |
||||
|
||||
impl UserData for Mat3 { |
||||
fn add_methods<'lua, T: UserDataMethods<'lua, Self>>(methods: &mut T) { |
||||
methods.add_meta_method(MetaMethod::Add, |ctx, a: &Self, b: Self| Ok(*a+b)); |
||||
methods.add_meta_method(MetaMethod::Sub, |ctx, a: &Self, b: Self| Ok(*a-b)); |
||||
methods.add_meta_method(MetaMethod::Mul, |ctx, a: &Self, b: Self| Ok(*a*b)); |
||||
methods.add_meta_method(MetaMethod::Unm, |ctx, a: &Self, b: ()| Ok(-*a)); |
||||
methods.add_meta_method(MetaMethod::Mul, |ctx, a: &Self, b: f64| Ok(*a*b)); |
||||
methods.add_meta_method(MetaMethod::Div, |ctx, a: &Self, b: f64| Ok(*a/b)); |
||||
|
||||
methods.add_method("a", |ctx, x, ()| Ok(x.a())); |
||||
methods.add_method("b", |ctx, x, ()| Ok(x.b())); |
||||
methods.add_method("c", |ctx, x, ()| Ok(x.c())); |
||||
methods.add_method("d", |ctx, x, ()| Ok(x.d())); |
||||
methods.add_method("e", |ctx, x, ()| Ok(x.e())); |
||||
methods.add_method("f", |ctx, x, ()| Ok(x.f())); |
||||
methods.add_method("g", |ctx, x, ()| Ok(x.g())); |
||||
methods.add_method("h", |ctx, x, ()| Ok(x.h())); |
||||
methods.add_method("i", |ctx, x, ()| Ok(x.i())); |
||||
|
||||
methods.add_method("det", |ctx, x, ()| Ok(x.det())); |
||||
methods.add_method("trans",|ctx, x, ()| Ok(x.trans())); |
||||
methods.add_method("minor", |ctx, x, ()| Ok(x.minor())); |
||||
methods.add_method("invert", |ctx, x, ()| Ok(x.invert())); |
||||
|
||||
methods.add_method("membermul", |ctx, x, y| Ok(x.member_mul(y))); |
||||
} |
||||
} |
||||
|
||||
pub fn mat3(ctx: Context, _: ()) -> rlua::Result<Table> { |
||||
let module = ctx.create_table()?; |
||||
|
||||
module.set("new", ctx.create_function( |
||||
|ctx, (a, b, c, d, e, f, g, h, i)| Ok(Mat3::new(a, b, c, d, e, f, g, h, i)) |
||||
)?)?; |
||||
|
||||
module.set("I", I3)?; |
||||
module.set("O", O3)?; |
||||
|
||||
Ok(module) |
||||
} |
@ -0,0 +1,30 @@ |
||||
use crate::material::{SurfaceType, Material}; |
||||
use rlua::{UserData, Context, Table}; |
||||
|
||||
impl UserData for SurfaceType {} |
||||
impl UserData for Material {} |
||||
|
||||
pub fn surface_type(ctx: Context, _: ()) -> rlua::Result<Table> { |
||||
let module = ctx.create_table()?; |
||||
|
||||
module.set("DIFFUSE", SurfaceType::Diffuse)?; |
||||
module.set("REFLECTIVE", SurfaceType::Reflective)?; |
||||
module.set("STOP", SurfaceType::Stop)?; |
||||
|
||||
Ok(module) |
||||
} |
||||
|
||||
pub fn material(ctx: Context, _: ()) -> rlua::Result<Table> { |
||||
let module = ctx.create_table()?; |
||||
|
||||
module.set("new", ctx.create_function( |
||||
|ctx, (emission, reflection, surface)| Ok(Material::new(emission, reflection, surface)) |
||||
)?)?; |
||||
|
||||
module.set("newfromdiagonal", ctx.create_function( |
||||
|ctx, (emission, reflection, surface)| Ok(Material::new_from_diagonal(emission, reflection, surface)) |
||||
)?)?; |
||||
|
||||
Ok(module) |
||||
} |
||||
|
@ -0,0 +1,43 @@ |
||||
use rlua::{Context, Table, Lua, Function}; |
||||
|
||||
mod obj; |
||||
mod vec3; |
||||
mod mat3; |
||||
mod color; |
||||
mod material; |
||||
mod light; |
||||
|
||||
pub use obj::{LuaObject, obj}; |
||||
pub use vec3::vec3; |
||||
pub use mat3::mat3; |
||||
pub use color::{color_vec, color_mat}; |
||||
pub use material::{surface_type, material}; |
||||
pub use light::light; |
||||
|
||||
pub fn env(ctx: Context, _: ()) -> rlua::Result<Table> { |
||||
let module = ctx.create_table()?; |
||||
|
||||
module.set("obj", obj(ctx, ())?)?; |
||||
module.set("vec3", vec3(ctx, ())?)?; |
||||
module.set("mat3", mat3(ctx, ())?)?; |
||||
module.set("colorvec", color_vec(ctx, ())?)?; |
||||
module.set("colormat", color_mat(ctx, ())?)?; |
||||
module.set("surfacetype", surface_type(ctx, ())?)?; |
||||
module.set("material", material(ctx, ())?)?; |
||||
module.set("light", light(ctx, ())?)?; |
||||
|
||||
Ok(module) |
||||
} |
||||
|
||||
pub fn scene_from_file(file: String) -> rlua::Result<LuaObject> { |
||||
Lua::new().context(|ctx| { |
||||
let env = env(ctx, ())?; |
||||
let meta = ctx.create_table()?; |
||||
meta.set("__index", ctx.globals())?; |
||||
env.set_metatable(Some(meta)); |
||||
|
||||
let loadfile: Function = ctx.globals().get("loadfile")?; |
||||
let chunk: Function = loadfile.call((file, "t", env))?; |
||||
ctx.unpack(chunk.call(())?) |
||||
}) |
||||
} |
@ -0,0 +1,84 @@ |
||||
use crate::object::*; |
||||
use crate::structs::{Vec3, Mat3}; |
||||
use std::sync::Arc; |
||||
use crate::material::Material; |
||||
use crate::light::Light; |
||||
use rlua::{UserData, Context, Table}; |
||||
|
||||
#[derive(Clone)] |
||||
pub struct LuaObject(Arc<Scene>); |
||||
|
||||
impl LuaObject { |
||||
fn new<T: 'static + Obj + Clone>(obj: T) -> rlua::Result<LuaObject> { |
||||
Ok(LuaObject(Arc::new(Scene::new(obj)))) |
||||
} |
||||
fn get(self) -> Scene { |
||||
(*self.0).clone() |
||||
} |
||||
} |
||||
|
||||
impl Obj for LuaObject { |
||||
fn distance_to(&self, point: Vec3) -> f64 { self.0.distance_to(point) } |
||||
fn normal_at(&self, point: Vec3) -> Vec3 { self.0.normal_at(point) } |
||||
fn material_at(&self, point: Vec3) -> Material { self.0.material_at(point) } |
||||
fn get_lights(&self) -> Vec<Light> { self.0.get_lights() } |
||||
fn node_count(&self) -> u32 { 1 + self.0.node_count() } |
||||
} |
||||
|
||||
impl UserData for LuaObject {} |
||||
|
||||
pub fn obj(ctx: Context, _: ()) -> rlua::Result<Table> { |
||||
let module = ctx.create_table()?; |
||||
|
||||
module.set("cuboid", ctx.create_function( |
||||
|ctx, (pos, radius): (Vec3, Vec3)| LuaObject::new(Cuboid::new(pos, radius)) |
||||
)?)?; |
||||
|
||||
module.set("cylinder", ctx.create_function( |
||||
|ctx, (pos, radius): (Vec3, f64)| LuaObject::new(Cylinder::new(pos, radius)) |
||||
)?)?; |
||||
|
||||
module.set("exclusion", ctx.create_function( |
||||
|ctx, (a, b): (LuaObject, LuaObject)| LuaObject::new(Exclusion::new(a.get(), b.get())) |
||||
)?)?; |
||||
|
||||
module.set("intersection", ctx.create_function( |
||||
|ctx, (a, b): (LuaObject, LuaObject)| LuaObject::new(Intersection::new(a.get(), b.get())) |
||||
)?)?; |
||||
|
||||
module.set("plane", ctx.create_function( |
||||
|ctx, (normal, offset): (Vec3, f64)| LuaObject::new(Plane::new(normal, offset)) |
||||
)?)?; |
||||
|
||||
module.set("sphere", ctx.create_function( |
||||
|ctx, (pos, radius): (Vec3, f64)| LuaObject::new(Sphere::new(pos, radius)) |
||||
)?)?; |
||||
|
||||
module.set("torus", ctx.create_function( |
||||
|ctx, (center, radius, thickness): (Vec3, f64, f64)| LuaObject::new(Torus::new(center, radius, thickness)) |
||||
)?)?; |
||||
|
||||
module.set("affinetransform", ctx.create_function( |
||||
|ctx, (obj, trans, disp): (LuaObject, Mat3, Vec3)| LuaObject::new(AffineTransform::new(obj.get(), trans, disp)) |
||||
)?)?; |
||||
|
||||
module.set("union", ctx.create_function( |
||||
|ctx, (a, b): (LuaObject, LuaObject)| LuaObject::new(Union::new(a.get(), b.get())) |
||||
)?)?; |
||||
|
||||
module.set("waves", ctx.create_function( |
||||
|ctx, ()| LuaObject::new(Waves::new()) |
||||
)?)?; |
||||
|
||||
module.set("withlights", ctx.create_function( |
||||
|ctx, (obj, light): (LuaObject, Light)| LuaObject::new(WithLights::new_one(obj.get(), light)) |
||||
)?)?; |
||||
|
||||
module.set("withmaterial", ctx.create_function( |
||||
|ctx, (obj, material): (LuaObject, Material)| LuaObject::new(WithMaterial::new(obj.get(), material)) |
||||
)?)?; |
||||
|
||||
//TODO add the rest of the objects
|
||||
|
||||
Ok(module) |
||||
} |
@ -0,0 +1,40 @@ |
||||
use crate::structs::{Vec3, X, Y, Z, O}; |
||||
use rlua::{UserData, UserDataMethods, MetaMethod, Context, Table}; |
||||
|
||||
impl UserData for Vec3 { |
||||
fn add_methods<'lua, T: UserDataMethods<'lua, Self>>(methods: &mut T) { |
||||
methods.add_meta_method(MetaMethod::Add, |ctx, a: &Self, b: Self| Ok(*a+b)); |
||||
methods.add_meta_method(MetaMethod::Sub, |ctx, a: &Self, b: Self| Ok(*a-b)); |
||||
methods.add_meta_method(MetaMethod::Mul, |ctx, a: &Self, b: Self| Ok(*a*b)); |
||||
methods.add_meta_method(MetaMethod::Pow, |ctx, a: &Self, b: Self| Ok(*a^b)); |
||||
methods.add_meta_method(MetaMethod::Unm, |ctx, a: &Self, b: ()| Ok(-*a)); |
||||
methods.add_meta_method(MetaMethod::Mul, |ctx, a: &Self, b: f64| Ok(*a*b)); |
||||
methods.add_meta_method(MetaMethod::Div, |ctx, a: &Self, b: f64| Ok(*a/b)); |
||||
|
||||
methods.add_method("x", |ctx, x, ()| Ok(x.x())); |
||||
methods.add_method("y", |ctx, x, ()| Ok(x.y())); |
||||
methods.add_method("z", |ctx, x, ()| Ok(x.z())); |
||||
|
||||
methods.add_method("magnitude", |ctx, x, ()| Ok(x.magnitude())); |
||||
methods.add_method("unit", |ctx, x, ()| Ok(x.unit())); |
||||
|
||||
methods.add_method("distanceto", |ctx, x, y| Ok(x.distance_to(y))); |
||||
methods.add_method("angleto", |ctx, x, y| Ok(x.angle_to(y))); |
||||
methods.add_method("membermul", |ctx, x, y| Ok(x.member_mul(y))); |
||||
} |
||||
} |
||||
|
||||
pub fn vec3(ctx: Context, _: ()) -> rlua::Result<Table> { |
||||
let module = ctx.create_table()?; |
||||
|
||||
module.set("new", ctx.create_function( |
||||
|ctx, (x, y, z)| Ok(Vec3::new(x, y, z)) |
||||
)?)?; |
||||
|
||||
module.set("X", X)?; |
||||
module.set("Y", Y)?; |
||||
module.set("Z", Z)?; |
||||
module.set("O", O)?; |
||||
|
||||
Ok(module) |
||||
} |
Loading…
Reference in new issue