SiebenCorgie / ori-engine

A small engine written in rust + vulkan.
Other
7 stars 1 forks source link

New Storing system of nodes #23

Closed SiebenCorgie closed 7 years ago

SiebenCorgie commented 7 years ago

Currently the nodes are stored per type. Better would be something like a nested struct. Idea:

Type: light, renderable, text, hud (enum) Type_Data: ie. for renderable: mesh, terrain, voxel (enum as well) and then store only a Arc in the node which can be sorted so a get_all_meshes would boil down to

match self.type {
    renderable(r) => {
        match r {
            mesh(m) => return Some(m),
            _ => return None,
        },
    _ => return None,
    }
SiebenCorgie commented 7 years ago

well moved to this approach: Node:

pub struct GenericNode {

    children: Vec<GenericNode>,
    ///There is a difference between a `Node`'s name and its `content` name
    pub name: String,
    ///And ID which needs to be unique TODO: Implement
    pub id: u32,
    ///Transform of this node in local space
    pub transform: Decomposed<Vector3<f32>, Quaternion<f32>>,
    ///The bounds of this note, takes the own `content` bound as well as the max and min values of
    ///all its children into consideration
    bound: Aabb3<f32>,
    ///The content is a contaier from the `ContentTypes` type which can hold any implemented
    ///Enum value
    content: ContentType,
}

And the ContentType which is an enum made from enums:

///All possible types of content a Node can hold.
///Changed in order to apply a new type
#[derive(Clone)]
pub enum ContentType {
    Renderable(RenderableContent),
    Light(LightsContent),
    Other(OtherContent),
}
///All renderable types
#[derive(Clone)]
pub enum RenderableContent {
    Mesh(Arc<Mutex<mesh::Mesh>>),
}
///All lights
#[derive(Clone)]
pub enum LightsContent {
    PointLight(light::LightPoint),
    DirectionalLight(light::LightDirectional),
    SpotLight(light::LightSpot),
}
///All Other components
#[derive(Clone)]
pub enum OtherContent {
    Empty(empty::Empty),
}

As you can see it should be quiet easy now to get a specific item time from a node, for instance getting a mesh reference based on its name became this:

    ///Returns a mesh from childs with this name
    pub fn get_mesh(&mut self, name: &str)-> Option<Arc<Mutex<core::resources::mesh::Mesh>>>{
        let mut result_value: Option<Arc<Mutex<core::resources::mesh::Mesh>>> = None;

        match self.content{
            ContentType::Renderable(RenderableContent::Mesh(ref m)) => {
                let mesh_lock = m.lock().expect("failed to lock mesh");
                if (*mesh_lock).name == String::from(name){
                    result_value = Some(m.clone());
                }
            },
            _ => {}, //no mesh
        }

        match result_value{
            Some(_)=> {},
            None=> {
                for i in self.children.iter_mut(){
                    match result_value{
                        None=> result_value = i.get_mesh(name.clone()),
                        Some(value)=> return Some(value),
                    }
                }
            }
        }
        result_value
    }