From 2e2c57a8c04575eec164279a49947cfdba250853 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Fri, 26 Jul 2024 23:15:22 +1000 Subject: scenes and pbr cleanup to handle missing texture of param --- bindgen/rust/Cargo.lock | 48 +++++++- bindgen/rust/Cargo.toml | 4 +- bindgen/rust/celeritas-sys/Cargo.lock | 45 +++++++ bindgen/rust/celeritas-sys/Cargo.toml | 4 +- bindgen/rust/celeritas-sys/build.rs | 27 +++++ bindgen/rust/celeritas-sys/examples/scene.rs | 169 +++++++++++++++++++++++++++ bindgen/rust/celeritas-sys/src/lib.rs | 2 + bindgen/rust/examples/scene.rs | 97 +++++++++++++++ bindgen/rust/src/lib.rs | 39 +++++++ 9 files changed, 432 insertions(+), 3 deletions(-) create mode 100644 bindgen/rust/celeritas-sys/examples/scene.rs create mode 100644 bindgen/rust/examples/scene.rs (limited to 'bindgen') diff --git a/bindgen/rust/Cargo.lock b/bindgen/rust/Cargo.lock index 0a1d69e..75bf0a5 100644 --- a/bindgen/rust/Cargo.lock +++ b/bindgen/rust/Cargo.lock @@ -93,13 +93,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2aba8f4e9906c7ce3c73463f62a7f0c65183ada1a2d47e397cc8810827f9694f" [[package]] -name = "celeritas-rs" +name = "celeritas" version = "0.1.0" dependencies = [ "celeritas-sys", "egui", "egui_glfw", "gl", + "serde", + "serde_json", ] [[package]] @@ -107,6 +109,7 @@ name = "celeritas-sys" version = "0.1.0" dependencies = [ "bindgen", + "serde", ] [[package]] @@ -377,6 +380,12 @@ dependencies = [ "either", ] +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + [[package]] name = "khronos_api" version = "3.1.0" @@ -681,12 +690,49 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + [[package]] name = "scopeguard" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "serde" +version = "1.0.204" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.204" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.72", +] + +[[package]] +name = "serde_json" +version = "1.0.120" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "shlex" version = "1.3.0" diff --git a/bindgen/rust/Cargo.toml b/bindgen/rust/Cargo.toml index 765c5d0..8f9da64 100644 --- a/bindgen/rust/Cargo.toml +++ b/bindgen/rust/Cargo.toml @@ -1,10 +1,12 @@ [package] -name = "celeritas-rs" +name = "celeritas" version = "0.1.0" edition = "2021" [dependencies] celeritas-sys = { path = "./celeritas-sys" } +serde = { version = "1.0.204", features = ["derive"] } +serde_json = "1.0.120" egui = "0.27.1" egui_glfw = { git = "https://github.com/omnisci3nce/egui_glfw.git", rev = "67b432695433feb59761f3ae984b966ca3da31db" } diff --git a/bindgen/rust/celeritas-sys/Cargo.lock b/bindgen/rust/celeritas-sys/Cargo.lock index 56df9ac..89c7407 100644 --- a/bindgen/rust/celeritas-sys/Cargo.lock +++ b/bindgen/rust/celeritas-sys/Cargo.lock @@ -100,6 +100,8 @@ dependencies = [ "egui", "egui_glfw", "gl", + "serde", + "serde_json", ] [[package]] @@ -370,6 +372,12 @@ dependencies = [ "either", ] +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + [[package]] name = "khronos_api" version = "3.1.0" @@ -674,12 +682,49 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + [[package]] name = "scopeguard" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "serde" +version = "1.0.204" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.204" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.71", +] + +[[package]] +name = "serde_json" +version = "1.0.120" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "shlex" version = "1.3.0" diff --git a/bindgen/rust/celeritas-sys/Cargo.toml b/bindgen/rust/celeritas-sys/Cargo.toml index bb5d692..0b52e3c 100644 --- a/bindgen/rust/celeritas-sys/Cargo.toml +++ b/bindgen/rust/celeritas-sys/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +serde = { version = "1.0.204", features = ["derive"] } [build-dependencies] bindgen = "0.69.4" @@ -11,4 +12,5 @@ bindgen = "0.69.4" [dev-dependencies] egui = "0.27.1" egui_glfw = { git = "https://github.com/omnisci3nce/egui_glfw.git", rev = "67b432695433feb59761f3ae984b966ca3da31db" } -gl = "0.14.0" \ No newline at end of file +gl = "0.14.0" +serde_json = "1.0.120" \ No newline at end of file diff --git a/bindgen/rust/celeritas-sys/build.rs b/bindgen/rust/celeritas-sys/build.rs index 097e26b..f7bf741 100644 --- a/bindgen/rust/celeritas-sys/build.rs +++ b/bindgen/rust/celeritas-sys/build.rs @@ -1,7 +1,33 @@ use std::env; use std::path::PathBuf; +use bindgen::callbacks::ParseCallbacks; + +const serializable_types: &[&'static str] = &[ + "Vec2", + "Vec3", + "Vec4", + "Mat4", + "Quat", + "DirectionalLight", + "PointLight", +]; + +#[derive(Debug)] +struct DeriveSerialize; +impl ParseCallbacks for DeriveSerialize { + fn add_derives(&self, info: &bindgen::callbacks::DeriveInfo<'_>) -> Vec { + if (serializable_types.contains(&info.name)) { + vec!["Serialize".to_string(), "Deserialize".to_string()] + } else { + vec![] + } + } +} + fn main() { + /* */ + // Tell cargo to look for shared libraries in the specified directory // TODO: we need to look based on OS println!("cargo:rustc-link-search=../../../build/macosx/arm64/debug"); @@ -43,6 +69,7 @@ fn main() { // Tell cargo to invalidate the built crate whenever any of the // included header files changed. .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) + .parse_callbacks(Box::new(DeriveSerialize)) // Finish the builder and generate the bindings. .generate() // Unwrap the Result and panic on failure. diff --git a/bindgen/rust/celeritas-sys/examples/scene.rs b/bindgen/rust/celeritas-sys/examples/scene.rs new file mode 100644 index 0000000..f3ea135 --- /dev/null +++ b/bindgen/rust/celeritas-sys/examples/scene.rs @@ -0,0 +1,169 @@ +use std::{ + ffi::CString, + fs::{self, File}, + io::Write, + path::Path, + ptr::{self, addr_of_mut}, +}; + +use serde::{Deserialize, Serialize}; + +use celeritas_sys::*; +use egui_backend::egui::{vec2, Pos2, Rect}; +use egui_glfw as egui_backend; +use egui_glfw::glfw::{fail_on_errors, Context}; + +use egui_glfw::glfw; + +// use celeritas_sys::{ffi::*, SerializableScene}; +use celeritas_sys::*; + +/// Wrapper around a string that is the path to a gltf model **relative** to the configured +/// `ASSETS` folder +#[derive(Debug, Serialize, Deserialize)] +pub struct ModelPath(String); + +/// Scene that can be saved and loaded from disk +#[derive(Debug, Serialize, Deserialize)] +pub struct SerializableScene { + pub sun: DirectionalLight, + pub point_lights: [Option; 4], + pub camera_orientation: (Vec3, Vec3), + pub models: Vec, +} + +// Runtime Scene <-> Serialized Scene + +impl SerializableScene { + /// TODO: docs + pub fn store_to_file(&self, filepath: &Path) { + let mut file = File::create(filepath).expect("creation failed"); + let json = serde_json::to_string(&self).expect("serialize failed"); + file.write(&json.as_bytes()).expect("writing failed"); + } + /// TODO: docs + pub fn load_from_file(filepath: &Path) -> Self { + let contents = fs::read_to_string(filepath).expect("Filepath should be open and read-able"); + + serde_json::from_str(&contents).expect("Should be deserializable") + } +} + +fn main() { + unsafe { + let p: *mut GLFWwindow = ptr::null_mut(); + Core_Bringup(p); + + // let core = get_global_core(); + + let camera_pos = Vec3 { + x: 18.9, + y: 10.6, + z: 11.6, + }; + let camera_front = Vec3 { + x: -0.6, + y: -0.2, + z: -0.7, + }; + let mut camera = Camera_Create( + camera_pos, + camera_front, + Vec3 { + x: 0.0, + y: 1.0, + z: 0.0, + }, + 45.0, + ); + SetCamera(camera); + + let mut cube_geo = Geo_CreateCuboid(f32x3 { + x: 2.0, + y: 2.0, + z: 2.0, + }); + + let scene = SerializableScene { + sun: DirectionalLight { + direction: Vec3 { + x: 0.0, + y: 1.0, + z: 0.0, + }, + ambient: Vec3 { + x: 1.0, + y: 1.0, + z: 1.0, + }, + diffuse: Vec3 { + x: 1.0, + y: 1.0, + z: 1.0, + }, + specular: Vec3 { + x: 0.0, + y: 0.0, + z: 0.0, + }, + }, + point_lights: [None, None, None, None], + camera_orientation: (camera_pos, camera_front), + models: vec![], + }; + + let scene_path = Path::new("default_scene.json"); + scene.store_to_file(scene_path); + + let rehydrated_scene = SerializableScene::load_from_file(scene_path); + dbg!(&rehydrated_scene); + + // let mut crate_mesh = Mesh_Create(addr_of_mut!(cube_geo), false); + // let albedo_map = TextureLoadFromFile( + // CString::new("assets/demo/crate/Wood_Crate_001_basecolor.jpg") + // .unwrap() + // .as_ptr() as *const i8, + // ); + // let roughness_map = TextureLoadFromFile( + // CString::new("assets/demo/crate/Wood_Crate_001_roughness.jpg") + // .unwrap() + // .as_ptr() as *const i8, + // ); + // let normal_map = TextureLoadFromFile( + // CString::new("assets/demo/crate/Wood_Crate_001_normal.jpg") + // .unwrap() + // .as_ptr() as *const i8, + // ); + // let ao_map = TextureLoadFromFile( + // CString::new("assets/demo/crate/Wood_Crate_001_ambientOcclusion.jpg") + // .unwrap() + // .as_ptr() as *const i8, + // ); + // let name: [i8; 64] = [0; 64]; + // let mut crate_mat = Material { + // name: name, + // kind: 0, + // param_albedo: Vec3 { + // x: 0.0, + // y: 0.0, + // z: 0.0, + // }, + // param_metallic: 0.0, + // param_roughness: 0.0, + // param_ao: 0.0, + // pbr_albedo_map: albedo_map, + // pbr_normal_map: normal_map, + // metal_roughness_combined: true, + // pbr_metallic_map: TextureHandle { raw: 99999 }, + // pbr_roughness_map: roughness_map, + // pbr_ao_map: ao_map, + // }; + // let crate_renderent = RenderEnt { + // mesh: addr_of_mut!(crate_mesh), + // material: addr_of_mut!(crate_mat), + // affine: mat4_ident(), + // casts_shadows: true, + // }; + // let mut render_ents: [RenderEnt; 1] = [crate_renderent]; + } +} diff --git a/bindgen/rust/celeritas-sys/src/lib.rs b/bindgen/rust/celeritas-sys/src/lib.rs index 1f64680..d0a863e 100644 --- a/bindgen/rust/celeritas-sys/src/lib.rs +++ b/bindgen/rust/celeritas-sys/src/lib.rs @@ -2,6 +2,8 @@ #![allow(non_camel_case_types)] #![allow(non_snake_case)] +use serde::{Deserialize, Serialize}; + pub mod egui_utils; include!(concat!(env!("OUT_DIR"), "/bindings.rs")); diff --git a/bindgen/rust/examples/scene.rs b/bindgen/rust/examples/scene.rs new file mode 100644 index 0000000..b411ffc --- /dev/null +++ b/bindgen/rust/examples/scene.rs @@ -0,0 +1,97 @@ +use std::{ffi::CString, path::Path, ptr::addr_of_mut}; + +use celeritas::{ffi::*, SerializableScene}; + +fn main() { + unsafe { + let camera_pos = Vec3 { + x: 18.9, + y: 10.6, + z: 11.6, + }; + let camera_front = Vec3 { + x: -0.6, + y: -0.2, + z: -0.7, + }; + let mut camera = Camera_Create( + camera_pos, + camera_front, + Vec3 { + x: 0.0, + y: 1.0, + z: 0.0, + }, + 45.0, + ); + SetCamera(camera); + + let mut cube_geo = Geo_CreateCuboid(f32x3 { + x: 2.0, + y: 2.0, + z: 2.0, + }); + + let scene = SerializableScene { + sun: DirectionalLight { direction: Vec3 { x: 0.0, y: 1.0, z: 0.0 }, + ambient: Vec3 { x: 1.0, y: 1.0, z: 1.0 }, diffuse: Vec3 { x: 1.0, y: 1.0, z: 1.0 }, specular: Vec3 { x: 0.0, y: 0.0, z: 0.0 } }, + point_lights: [None, None, None, None], + camera_orientation: (camera_pos, camera_front), + models: vec![], + }; + + let scene_path = Path::new("default_scene.json"); + scene.store_to_file(scene_path); + + let rehydrated_scene = SerializableScene::load_from_file(scene_path); + dbg!(&rehydrated_scene); + + // let mut crate_mesh = Mesh_Create(addr_of_mut!(cube_geo), false); + // let albedo_map = TextureLoadFromFile( + // CString::new("assets/demo/crate/Wood_Crate_001_basecolor.jpg") + // .unwrap() + // .as_ptr() as *const i8, + // ); + // let roughness_map = TextureLoadFromFile( + // CString::new("assets/demo/crate/Wood_Crate_001_roughness.jpg") + // .unwrap() + // .as_ptr() as *const i8, + // ); + // let normal_map = TextureLoadFromFile( + // CString::new("assets/demo/crate/Wood_Crate_001_normal.jpg") + // .unwrap() + // .as_ptr() as *const i8, + // ); + // let ao_map = TextureLoadFromFile( + // CString::new("assets/demo/crate/Wood_Crate_001_ambientOcclusion.jpg") + // .unwrap() + // .as_ptr() as *const i8, + // ); + // let name: [i8; 64] = [0; 64]; + // let mut crate_mat = Material { + // name: name, + // kind: 0, + // param_albedo: Vec3 { + // x: 0.0, + // y: 0.0, + // z: 0.0, + // }, + // param_metallic: 0.0, + // param_roughness: 0.0, + // param_ao: 0.0, + // pbr_albedo_map: albedo_map, + // pbr_normal_map: normal_map, + // metal_roughness_combined: true, + // pbr_metallic_map: TextureHandle { raw: 99999 }, + // pbr_roughness_map: roughness_map, + // pbr_ao_map: ao_map, + // }; + // let crate_renderent = RenderEnt { + // mesh: addr_of_mut!(crate_mesh), + // material: addr_of_mut!(crate_mat), + // affine: mat4_ident(), + // casts_shadows: true, + // }; + // let mut render_ents: [RenderEnt; 1] = [crate_renderent]; +} +} diff --git a/bindgen/rust/src/lib.rs b/bindgen/rust/src/lib.rs index f9e77b0..f3382a2 100644 --- a/bindgen/rust/src/lib.rs +++ b/bindgen/rust/src/lib.rs @@ -3,4 +3,43 @@ #![warn(missing_docs)] #![cfg_attr(docsrs, feature(doc_cfg))] +use std::{ + fs::{self, File}, + io::Write, + path::Path, +}; + pub use celeritas_sys as ffi; +use celeritas_sys::{DirectionalLight, PointLight, Vec3}; +use serde::{Deserialize, Serialize}; + +/// Wrapper around a string that is the path to a gltf model **relative** to the configured +/// `ASSETS` folder +#[derive(Debug, Serialize, Deserialize)] +pub struct ModelPath(String); + +/// Scene that can be saved and loaded from disk +#[derive(Debug, Serialize, Deserialize)] +pub struct SerializableScene { + pub sun: DirectionalLight, + pub point_lights: [Option; 4], + pub camera_orientation: (Vec3, Vec3), + pub models: Vec, +} + +// Runtime Scene <-> Serialized Scene + +impl SerializableScene { + /// TODO: docs + pub fn store_to_file(&self, filepath: &Path) { + let mut file = File::create(filepath).expect("creation failed"); + let json = serde_json::to_string(&self).expect("serialize failed"); + file.write(&json.as_bytes()).expect("writing failed"); + } + /// TODO: docs + pub fn load_from_file(filepath: &Path) -> Self { + let contents = fs::read_to_string(filepath).expect("Filepath should be open and read-able"); + + serde_json::from_str(&contents).expect("Should be deserializable") + } +} -- cgit v1.2.3-70-g09d2