summaryrefslogtreecommitdiff
path: root/bindgen/rust/src/lib.rs
blob: c8e56dfb2f4d0e038d7abbe6e7b0b7a9133182ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! Celeritas bindings wrapper library

#![warn(missing_docs)]
#![cfg_attr(docsrs, feature(doc_cfg))]

pub use celeritas_sys as ffi;

/// Commonly used types
pub mod prelude;

pub mod ral;

use std::{
    fs::{self, File},
    io::Write,
    path::Path,
};

use celeritas_sys::{DirectionalLight, PointLight, Transform, 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);

///
#[derive(Debug, Serialize, Deserialize)]
pub struct ModelNode {
    model_path: ModelPath,
    transform: Transform,
}

/// Scene that can be saved and loaded from disk
#[derive(Debug, Serialize, Deserialize)]
pub struct SerializableScene {
    /// main light
    pub sun: DirectionalLight,
    pub point_lights: [Option<PointLight>; 4],
    pub camera_orientation: (Vec3, Vec3),
    pub models: Vec<ModelNode>,
}

// 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")
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Light {
    Point(ffi::PointLight),
    Directional(ffi::DirectionalLight),
    // Spot(ffi::Spotlight)
}