This commit is contained in:
STP
2023-11-26 07:56:36 -05:00
parent 2e70fc9a68
commit 8ab9be8056
11 changed files with 334 additions and 35 deletions

213
Cargo.lock generated
View File

@@ -133,6 +133,12 @@ version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb"
[[package]]
name = "bit_field"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
@@ -163,6 +169,12 @@ version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "374d28ec25809ee0e23827c2ab573d729e293f281dfe393500e7ad618baa61c6" checksum = "374d28ec25809ee0e23827c2ab573d729e293f281dfe393500e7ad618baa61c6"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]] [[package]]
name = "calloop" name = "calloop"
version = "0.10.6" version = "0.10.6"
@@ -247,6 +259,12 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "color_quant"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]] [[package]]
name = "com-rs" name = "com-rs"
version = "0.2.1" version = "0.2.1"
@@ -334,6 +352,39 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "crossbeam-deque"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset 0.9.0",
"scopeguard",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294"
dependencies = [
"cfg-if",
]
[[package]] [[package]]
name = "crossfont" name = "crossfont"
version = "0.5.1" version = "0.5.1"
@@ -450,6 +501,12 @@ dependencies = [
"wio", "wio",
] ]
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]] [[package]]
name = "env_logger" name = "env_logger"
version = "0.10.1" version = "0.10.1"
@@ -495,6 +552,22 @@ dependencies = [
"pkg-config", "pkg-config",
] ]
[[package]]
name = "exr"
version = "1.71.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "832a761f35ab3e6664babfbdc6cef35a4860e816ec3916dcfd0882954e98a8a8"
dependencies = [
"bit_field",
"flume",
"half",
"lebe",
"miniz_oxide",
"rayon-core",
"smallvec",
"zune-inflate",
]
[[package]] [[package]]
name = "fdeflate" name = "fdeflate"
version = "0.3.1" version = "0.3.1"
@@ -514,6 +587,15 @@ dependencies = [
"miniz_oxide", "miniz_oxide",
] ]
[[package]]
name = "flume"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55ac459de2512911e4b674ce33cf20befaba382d05b62b008afc1c8b57cbf181"
dependencies = [
"spin",
]
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@@ -595,6 +677,16 @@ dependencies = [
"wasi", "wasi",
] ]
[[package]]
name = "gif"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80792593675e051cf94a4b111980da2ba60d4a83e43e0048c5693baab3977045"
dependencies = [
"color_quant",
"weezl",
]
[[package]] [[package]]
name = "gimli" name = "gimli"
version = "0.28.0" version = "0.28.0"
@@ -665,6 +757,15 @@ dependencies = [
"bitflags 2.4.1", "bitflags 2.4.1",
] ]
[[package]]
name = "half"
version = "2.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b4af3693f1b705df946e9fe5631932443781d0aabb423b62fcd4d73f6d2fd0"
dependencies = [
"crunchy",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.12.3" version = "0.12.3"
@@ -720,6 +821,25 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "image"
version = "0.24.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f3dfdbdd72063086ff443e297b61695500514b1e41095b6fb9a5ab48a70a711"
dependencies = [
"bytemuck",
"byteorder",
"color_quant",
"exr",
"gif",
"jpeg-decoder",
"num-rational",
"num-traits",
"png",
"qoi",
"tiff",
]
[[package]] [[package]]
name = "imgui" name = "imgui"
version = "0.11.0" version = "0.11.0"
@@ -817,6 +937,15 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130"
[[package]]
name = "jpeg-decoder"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc0000e42512c92e31c2252315bda326620a4e034105e900c98ec492fa077b3e"
dependencies = [
"rayon",
]
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.65" version = "0.3.65"
@@ -843,6 +972,12 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lebe"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.150" version = "0.2.150"
@@ -934,6 +1069,15 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "memoffset"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "metal" name = "metal"
version = "0.24.0" version = "0.24.0"
@@ -1096,7 +1240,7 @@ dependencies = [
"bitflags 1.3.2", "bitflags 1.3.2",
"cfg-if", "cfg-if",
"libc", "libc",
"memoffset", "memoffset 0.6.5",
] ]
[[package]] [[package]]
@@ -1109,7 +1253,7 @@ dependencies = [
"bitflags 1.3.2", "bitflags 1.3.2",
"cfg-if", "cfg-if",
"libc", "libc",
"memoffset", "memoffset 0.6.5",
] ]
[[package]] [[package]]
@@ -1321,6 +1465,15 @@ version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f89dff0959d98c9758c88826cc002e2c3d0b9dfac4139711d1f30de442f1139b" checksum = "f89dff0959d98c9758c88826cc002e2c3d0b9dfac4139711d1f30de442f1139b"
[[package]]
name = "qoi"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f6d64c71eb498fe9eae14ce4ec935c555749aef511cca85b5568910d6e48001"
dependencies = [
"bytemuck",
]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.33" version = "1.0.33"
@@ -1387,6 +1540,26 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
[[package]]
name = "rayon"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c27db03db7734835b3f53954b534c91069375ce6ccaa2e065441e07d9b6cdb1"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ce3fb6ad83f861aac485e76e1985cd109d9a3713802152be56c3b1f0e0658ed"
dependencies = [
"crossbeam-deque",
"crossbeam-utils",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.4.1" version = "0.4.1"
@@ -1472,6 +1645,7 @@ dependencies = [
"cfg-if", "cfg-if",
"env_logger", "env_logger",
"error-iter", "error-iter",
"image",
"imgui", "imgui",
"imgui-wgpu", "imgui-wgpu",
"imgui-winit-support", "imgui-winit-support",
@@ -1658,6 +1832,15 @@ dependencies = [
"wayland-protocols", "wayland-protocols",
] ]
[[package]]
name = "spin"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
dependencies = [
"lock_api",
]
[[package]] [[package]]
name = "spirv" name = "spirv"
version = "0.2.0+1.5.4" version = "0.2.0+1.5.4"
@@ -1731,6 +1914,17 @@ dependencies = [
"syn 2.0.39", "syn 2.0.39",
] ]
[[package]]
name = "tiff"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d172b0f4d3fba17ba89811858b9d3d97f928aece846475bbda076ca46736211"
dependencies = [
"flate2",
"jpeg-decoder",
"weezl",
]
[[package]] [[package]]
name = "tiny-keccak" name = "tiny-keccak"
version = "2.0.2" version = "2.0.2"
@@ -1982,6 +2176,12 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "weezl"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9193164d4de03a926d909d3bc7c30543cecb35400c02114792c2cae20d5e2dbb"
[[package]] [[package]]
name = "wgpu" name = "wgpu"
version = "0.16.3" version = "0.16.3"
@@ -2409,3 +2609,12 @@ dependencies = [
"quote", "quote",
"syn 2.0.39", "syn 2.0.39",
] ]
[[package]]
name = "zune-inflate"
version = "0.2.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73ab332fe2f6680068f3582b16a24f90ad7096d5d39b974d1c0aff0125116f02"
dependencies = [
"simd-adler32",
]

View File

@@ -22,3 +22,4 @@ pixels = "0.13"
error-iter = "0.4.1" error-iter = "0.4.1"
rhai = {version = "1.16.3"} rhai = {version = "1.16.3"}
rand = "0.8.5" rand = "0.8.5"
image = "0.24.7"

View File

@@ -1,19 +1,32 @@
let scene = Scene(); let scene = Scene();
let material = Material(V(0.5,0.5,0.5), V(0.8, 0.8, 0.8), 25.0); let material = Material(V(0.2,0.2,0.2), V(0.2, 0.8, 0.8), 10.0);
//let ambient = Light(P(10.0,0.0,0.0), V(1.0,1.0,1.0), V(0.0, 0.0, 0.0)); //let ambient = Light(P(10.0,0.0,0.0), V(1.0,1.0,1.0), V(0.0, 0.0, 0.0));
//scene.addLight(ambient); //scene.addLight(ambient);
let light = Light(P(3.0,3.0,1.0), V(0.0,1.0,1.0), V(0.1, 0.01, 0.001)); let light = Light(P(0.0,7.0,0.0), V(0.0,0.0,1.0), V(0.1, 0.01, 0.001));
scene.addLight(light);
let light = Light(P(2.0,7.0,0.0), V(0.0,1.0,0.0), V(0.1, 0.01, 0.001));
scene.addLight(light);
let light = Light(P(-2.0,7.0,0.0), V(1.0,0.0,0.0), V(0.1, 0.01, 0.001));
scene.addLight(light);
let light = Ambient(V(0.1,0.1,0.1));
scene.addLight(light); scene.addLight(light);
let sphere = Sphere(P(0.0,0.0,0.0), 1.0, material); let sphere = Sphere(P(0.0,0.0,0.0), 1.0, material);
let sphere_node = Node(sphere); let sphere_node = Node(sphere);
scene.addNode(sphere_node);
for i in 0..6 {
let sphere = Sphere(P(0.0,0.0,0.0), 2.0, material);
let sphere_node = Node(sphere);
sphere_node.translate(V(2.0*cos(i.to_float()), -4.0, 2.0*sin(i.to_float())));
scene.addNode(sphere_node);
}
// let child = sphere_node.child(sphere); // let child = sphere_node.child(sphere);
// child.translate(V(1.0,1.0,1.0)); // child.translate(V(1.0,1.0,1.0));
// scene.addNode(sphere_node);
//scene.addNode(child); //scene.addNode(child);
// let cube = CubeUnit(material); // let cube = CubeUnit(material);
@@ -24,10 +37,10 @@ let sphere_node = Node(sphere);
// let gnonom_node = Node(gnonom); // let gnonom_node = Node(gnonom);
// scene.addNode(gnonom_node); // scene.addNode(gnonom_node);
let cylinder = Cylinder(1.0,1.0, material); // let cylinder = Cylinder(10.0,1.0, material);
let cylinder_node = Node(cylinder); // let cylinder_node = Node(cylinder);
cylinder_node.scale(V(4.0,0.3,0.3)); // //cylinder_node.scale(V(4.0,0.3,0.3));
cylinder_node.translate(V(0.0,-4.0,1.0)); // cylinder_node.translate(V(0.0,-4.0,0.0));
scene.addNode(cylinder_node); // scene.addNode(cylinder_node);
scene scene

View File

@@ -3,7 +3,7 @@ use crate::{
light::Light, light::Light,
primitive::*, primitive::*,
scene::{Node, Scene}, scene::{Node, Scene},
state::INIT_FILE, state::{INIT_FILE, SAVE_FILE},
UP_VECTOR_F32, ZERO_VECTOR_F32, UP_VECTOR_F32, ZERO_VECTOR_F32,
}; };
use imgui::*; use imgui::*;
@@ -12,13 +12,13 @@ use pixels::{wgpu, PixelsContext};
use rhai::Engine; use rhai::Engine;
use std::time::Instant; use std::time::Instant;
const BUFFER_PROPORTION_INIT: f32 = 1.0; const BUFFER_PROPORTION_INIT: f32 = 0.2;
const BUFFER_PROPORTION_MIN: f32 = 0.5; const BUFFER_PROPORTION_MIN: f32 = 0.1;
const BUFFER_PROPORTION_MAX: f32 = 1.0; const BUFFER_PROPORTION_MAX: f32 = 1.0;
const RAYS_INIT: i32 = 1000; const RAYS_INIT: i32 = 1000;
const RAYS_MIN: i32 = 100; const RAYS_MIN: i32 = 100;
const RAYS_MAX: i32 = 100000; const RAYS_MAX: i32 = 30000;
const CAMERA_MIN_FOV: f32 = 10.0; const CAMERA_MIN_FOV: f32 = 10.0;
const CAMERA_MAX_FOV: f32 = 160.0; const CAMERA_MAX_FOV: f32 = 160.0;
@@ -29,6 +29,7 @@ pub enum GuiEvent {
BufferResize(f32, f32), BufferResize(f32, f32),
CameraUpdate(Camera), CameraUpdate(Camera),
SceneLoad(Scene), SceneLoad(Scene),
SaveImage(String),
} }
pub struct Gui { pub struct Gui {
@@ -53,6 +54,8 @@ pub struct Gui {
camera_target: [f32; 3], camera_target: [f32; 3],
camera_up: [f32; 3], camera_up: [f32; 3],
camera_fov: f32, camera_fov: f32,
image_filename: String,
} }
impl Gui { impl Gui {
@@ -115,6 +118,8 @@ impl Gui {
camera_target: ZERO_VECTOR_F32.into(), camera_target: ZERO_VECTOR_F32.into(),
camera_up: UP_VECTOR_F32.into(), camera_up: UP_VECTOR_F32.into(),
camera_fov: 110.0, camera_fov: 110.0,
image_filename: String::from(SAVE_FILE),
} }
} }
@@ -216,11 +221,25 @@ impl Gui {
Err(e) => println!("{e}"), Err(e) => println!("{e}"),
} }
} }
if ui.button("Save script") {
match std::fs::write(&self.script_filename, &self.script) {
Ok(_) => println!("Script saved successfully"),
Err(e) => println!("{}", e),
}
}
//Script block //Script block
ui.input_text_multiline("script", &mut self.script, [600., 1500.]) ui.input_text_multiline("script", &mut self.script, [600., 1500.])
.build(); .build();
} }
if CollapsingHeader::new("Image").build(ui) {
ui.input_text("Image file", &mut self.image_filename)
.build();
if ui.button("Save Image") {
self.event = Some(GuiEvent::SaveImage(self.image_filename.clone()));
}
}
// Render Dear ImGui with WGPU // Render Dear ImGui with WGPU
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("imgui"), label: Some("imgui"),
@@ -270,7 +289,9 @@ pub fn init_engine() -> Engine {
.register_type::<Scene>() .register_type::<Scene>()
.register_fn("Scene", Scene::empty) .register_fn("Scene", Scene::empty)
.register_fn("addNode", Scene::add_node) .register_fn("addNode", Scene::add_node)
.register_fn("addLight", Scene::add_light); .register_fn("addLight", Scene::add_light)
.register_fn("addCamera", Scene::add_camera)
.register_fn("addMaterial", Scene::add_material);
engine engine
.register_type::<Node>() .register_type::<Node>()
@@ -281,7 +302,8 @@ pub fn init_engine() -> Engine {
.register_fn("child", Node::child); .register_fn("child", Node::child);
engine engine
.register_type::<Light>() .register_type::<Light>()
.register_fn("Light", Light::new); .register_fn("Light", Light::new)
.register_fn("Ambient", Light::ambient);
engine engine
.register_type::<Material>() .register_type::<Material>()
.register_fn("Material", Material::new) .register_fn("Material", Material::new)
@@ -319,6 +341,9 @@ pub fn init_engine() -> Engine {
engine engine
.register_type::<Torus>() .register_type::<Torus>()
.register_fn("Torus", Torus::new); .register_fn("Torus", Torus::new);
engine
.register_type::<Mesh>()
.register_fn("Mesh", Mesh::from_file);
engine engine
.register_type::<Gnonom>() .register_type::<Gnonom>()
.register_fn("Gnonom", Gnonom::new); .register_fn("Gnonom", Gnonom::new);

View File

@@ -5,6 +5,7 @@ pub struct Light {
pub position: Point3<f64>, pub position: Point3<f64>,
pub colour: Vector3<f64>, pub colour: Vector3<f64>,
pub falloff: Vector3<f64>, pub falloff: Vector3<f64>,
pub ambient: bool,
} }
impl Light { impl Light {
@@ -13,11 +14,15 @@ impl Light {
position, position,
colour, colour,
falloff, falloff,
ambient: false,
} }
} }
pub fn white(position: Point3<f64>) -> Self { pub fn ambient(colour: Vector3<f64>) -> Self {
let colour = Vector3::new(1.0, 1.0, 1.0); Light {
let falloff = Vector3::new(1.0, 0.0, 0.0); position: Point3::new(0.0, 0.0, 0.0),
Light::new(position, colour, falloff) colour,
falloff: Vector3::new(0.0, 0.0, 0.0),
ambient: true,
}
} }
} }

View File

@@ -5,7 +5,6 @@ const EPSILON: f64 = 1e-6;
const INFINITY: f64 = f64::MAX; const INFINITY: f64 = f64::MAX;
const EPSILON_VECTOR: Vector3<f64> = Vector3::new(EPSILON, EPSILON, EPSILON); const EPSILON_VECTOR: Vector3<f64> = Vector3::new(EPSILON, EPSILON, EPSILON);
static ZERO_VECTOR: Vector3<f64> = Vector3::new(0.0, 0.0, 0.0); static ZERO_VECTOR: Vector3<f64> = Vector3::new(0.0, 0.0, 0.0);
static UP_VECTOR: Vector3<f64> = Vector3::new(0.0, 1.0, 0.0);
static ZERO_VECTOR_F32: Vector3<f32> = Vector3::new(0.0, 0.0, 0.0); static ZERO_VECTOR_F32: Vector3<f32> = Vector3::new(0.0, 0.0, 0.0);
static UP_VECTOR_F32: Vector3<f32> = Vector3::new(0.0, 1.0, 0.0); static UP_VECTOR_F32: Vector3<f32> = Vector3::new(0.0, 1.0, 0.0);

View File

@@ -73,6 +73,7 @@ impl Intersection {
// BOUNDING BOX ----------------------------------------------------------------- // BOUNDING BOX -----------------------------------------------------------------
#[derive(Clone)] #[derive(Clone)]
struct BoundingBox { struct BoundingBox {
bln: Point3<f64>, bln: Point3<f64>,
trf: Point3<f64>, trf: Point3<f64>,
@@ -97,6 +98,7 @@ impl BoundingBox {
None None
} }
} }
#[allow(dead_code)]
fn get_centroid(&self) -> Point3<f64> { fn get_centroid(&self) -> Point3<f64> {
self.bln + (self.trf - self.bln) / 2.0 self.bln + (self.trf - self.bln) / 2.0
} }
@@ -689,7 +691,8 @@ impl Primitive for Cube {
// TRIANGLE ----------------------------------------------------------------- // TRIANGLE -----------------------------------------------------------------
#[derive(Clone)] #[derive(Clone)]
struct Triangle { #[allow(dead_code)]
pub struct Triangle {
u: Point3<f64>, u: Point3<f64>,
v: Point3<f64>, v: Point3<f64>,
w: Point3<f64>, w: Point3<f64>,
@@ -699,7 +702,7 @@ struct Triangle {
} }
impl Triangle { impl Triangle {
fn new( pub fn new(
u: Point3<f64>, u: Point3<f64>,
v: Point3<f64>, v: Point3<f64>,
w: Point3<f64>, w: Point3<f64>,
@@ -720,6 +723,7 @@ impl Triangle {
bounding_box, bounding_box,
}) })
} }
#[allow(dead_code)]
pub fn unit(material: Arc<Material>) -> Arc<dyn Primitive> { pub fn unit(material: Arc<Material>) -> Arc<dyn Primitive> {
let u = Point3::new(-1.0, 0.0, -1.0); let u = Point3::new(-1.0, 0.0, -1.0);
let v = Point3::new(0.0, 0.0, 1.0); let v = Point3::new(0.0, 0.0, 1.0);
@@ -780,14 +784,15 @@ impl Primitive for Triangle {
// MESH ----------------------------------------------------------------- // MESH -----------------------------------------------------------------
#[derive(Clone)] #[derive(Clone)]
struct Mesh { pub struct Mesh {
triangles: Vec<Arc<Triangle>>, triangles: Vec<Arc<Triangle>>,
material: Arc<Material>, material: Arc<Material>,
bounding_box: BoundingBox, bounding_box: BoundingBox,
} }
impl Mesh { impl Mesh {
fn new(triangles: Vec<Arc<Triangle>>, material: Arc<Material>) -> Arc<dyn Primitive> { #[allow(dead_code)]
pub fn new(triangles: Vec<Arc<Triangle>>, material: Arc<Material>) -> Arc<dyn Primitive> {
// Calculate the bounding box for the entire mesh based on the bounding boxes of individual triangles // Calculate the bounding box for the entire mesh based on the bounding boxes of individual triangles
let bounding_box = Mesh::compute_bounding_box(&triangles); let bounding_box = Mesh::compute_bounding_box(&triangles);
@@ -798,6 +803,7 @@ impl Mesh {
}) })
} }
#[allow(dead_code)]
fn compute_bounding_box(triangles: &Vec<Arc<Triangle>>) -> BoundingBox { fn compute_bounding_box(triangles: &Vec<Arc<Triangle>>) -> BoundingBox {
let mut bln = Point3::new(INFINITY, INFINITY, INFINITY); let mut bln = Point3::new(INFINITY, INFINITY, INFINITY);
let mut trf = -bln; let mut trf = -bln;
@@ -813,7 +819,7 @@ impl Mesh {
BoundingBox { bln, trf } BoundingBox { bln, trf }
} }
fn from_file(filename: &str, material: Arc<Material>) -> Arc<dyn Primitive> { pub fn from_file(filename: &str, material: Arc<Material>) -> Arc<dyn Primitive> {
let mut triangles: Vec<Arc<dyn Primitive>> = Vec::new(); let mut triangles: Vec<Arc<dyn Primitive>> = Vec::new();
let mut vertices: Vec<Point3<f64>> = Vec::new(); let mut vertices: Vec<Point3<f64>> = Vec::new();

View File

@@ -1,8 +1,8 @@
use crate::{ use crate::{
primitive::{self, Intersection}, primitive::Intersection,
raytracer::phong_shade_point, raytracer::phong_shade_point,
scene::{Node, Scene}, scene::{Node, Scene},
EPSILON, INFINITY, INFINITY,
}; };
use nalgebra::{Matrix4, Point3, Vector3}; use nalgebra::{Matrix4, Point3, Vector3};
@@ -12,6 +12,7 @@ pub struct Ray {
pub b: Vector3<f64>, pub b: Vector3<f64>,
} }
#[allow(dead_code)]
impl Ray { impl Ray {
pub fn new(a: Point3<f64>, b: Vector3<f64>) -> Ray { pub fn new(a: Point3<f64>, b: Vector3<f64>) -> Ray {
Ray { Ray {
@@ -45,7 +46,7 @@ impl Ray {
for node in nodes { for node in nodes {
let primitive = node.primitive.clone(); let primitive = node.primitive.clone();
//Transform ray to view coords //Transform ray from view coords
let ray = self.transform(&node.inv_viewmodel); let ray = self.transform(&node.inv_viewmodel);
if primitive.intersect_bounding_box(&ray).is_some() { if primitive.intersect_bounding_box(&ray).is_some() {
@@ -54,6 +55,7 @@ impl Ray {
closest_distance = intersect.distance; closest_distance = intersect.distance;
//Convert back to world coords //Convert back to world coords
let intersect = intersect.transform(&node.model, &node.inv_model); let intersect = intersect.transform(&node.model, &node.inv_model);
closest_intersect = Some(intersect); closest_intersect = Some(intersect);
} }
} }

View File

@@ -24,8 +24,14 @@ pub fn phong_shade_point(scene: &Scene, intersect: &Intersection) -> Vector3<u8>
position: light_position, position: light_position,
colour: light_colour, colour: light_colour,
falloff: light_falloff, falloff: light_falloff,
ambient: light_ambient,
} = light; } = light;
if *light_ambient {
colour += light_colour;
continue;
}
// Point to light // Point to light
let to_light = light_position - point; let to_light = light_position - point;
let light_distance = to_light.norm(); let light_distance = to_light.norm();

View File

@@ -4,6 +4,7 @@ use crate::camera::Camera;
use crate::ray::Ray; use crate::ray::Ray;
use crate::{gui::Gui, scene::Scene}; use crate::{gui::Gui, scene::Scene};
use crate::{gui::GuiEvent, log_error}; use crate::{gui::GuiEvent, log_error};
use std::path::Path;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use rand::thread_rng; use rand::thread_rng;
@@ -19,11 +20,12 @@ use winit::event::{Event, KeyboardInput, MouseButton, VirtualKeyCode, WindowEven
use winit::event_loop::{ControlFlow, EventLoop}; use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::{Window, WindowBuilder}; use winit::window::{Window, WindowBuilder};
const START_WIDTH: i32 = 1400; const START_WIDTH: i32 = 1200;
const START_HEIGHT: i32 = 1000; const START_HEIGHT: i32 = 1200;
const COLOUR_CLEAR: [u8; 4] = [0x22, 0x00, 0x11, 0xff]; const COLOUR_CLEAR: [u8; 4] = [0x22, 0x00, 0x11, 0xff];
pub const INIT_FILE: &str = "scene.rhai"; pub const INIT_FILE: &str = "scene.rhai";
pub const SAVE_FILE: &str = "img.png";
pub struct State { pub struct State {
scene: Scene, scene: Scene,
@@ -75,6 +77,17 @@ impl State {
self.scene = scene; self.scene = scene;
self.reset_queue(); self.reset_queue();
} }
GuiEvent::SaveImage(filename) => {
let pixels = self.pixels.lock().unwrap();
let frame = pixels.frame();
image::save_buffer(
Path::new(&filename),
frame,
self.buffer_width,
self.buffer_height,
image::ColorType::Rgba8,
)?
}
} }
}; };
Ok(()) Ok(())
@@ -93,13 +106,11 @@ impl State {
let mut pixels = self.pixels.lock().unwrap(); let mut pixels = self.pixels.lock().unwrap();
pixels.resize_buffer(self.buffer_width, self.buffer_height)?; pixels.resize_buffer(self.buffer_width, self.buffer_height)?;
pixels.resize_surface(size.width, size.height)?;
Ok(()) Ok(())
} }
fn resize(&mut self, size: &PhysicalSize<u32>) -> Result<(), Box<dyn Error>> { fn resize(&mut self, size: &PhysicalSize<u32>) -> Result<(), Box<dyn Error>> {
self.buffer_width = (size.width) as u32;
self.buffer_height = (size.height) as u32;
self.reset_queue();
let mut pixels = self.pixels.lock().unwrap(); let mut pixels = self.pixels.lock().unwrap();
pixels.resize_surface(size.width, size.height)?; pixels.resize_surface(size.width, size.height)?;
Ok(()) Ok(())

22
test.rhai Normal file
View File

@@ -0,0 +1,22 @@
let scene = Scene();
let material = Material(V(0.2,0.2,0.2), V(0.2, 0.8, 0.8), 10.0);
//let ambient = Light(P(10.0,0.0,0.0), V(1.0,1.0,1.0), V(0.0, 0.0, 0.0));
//scene.addLight(ambient);
let light = Light(P(0.0,7.0,0.0), V(0.0,0.0,1.0), V(0.1, 0.01, 0.001));
scene.addLight(light);
let light = Light(P(2.0,7.0,0.0), V(0.0,1.0,0.0), V(0.1, 0.01, 0.001));
scene.addLight(light);
let light = Light(P(-2.0,7.0,0.0), V(1.0,0.0,0.0), V(0.1, 0.01, 0.001));
scene.addLight(light);
let light = Ambient(V(0.1,0.1,0.1));
scene.addLight(light);
let sphere = Stein(material);
let sphere_node = Node(sphere);
scene.addNode(sphere_node);
scene