source: trunk/src/engine/model_manager.cc @ 508

Last change on this file since 508 was 508, checked in by epyon, 9 years ago
  • nv::engine upgrades
  • default_resource_manager implementation
File size: 2.7 KB
Line 
1// Copyright (C) 2016-2016 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#include "nv/engine/model_manager.hh"
8
9#include "nv/lua/lua_math.hh"
10
11using namespace nv;
12
13bool nv::model_manager::load_resource( lua::table_guard& table, shash64 id )
14{
15        model* gm = new model;
16        gm->attach = table.get_string_hash_64( "attach" );
17        gm->root = transform( table.get<vec3>( "position", vec3() ) );
18
19        resource< mesh_data > def_data;
20        if ( table.is_string( "path" ) )
21                def_data = m_rm->get< mesh_data >( table.get_string128( "path" ) );
22
23        if ( table.has_field( "animator" ) )
24        {
25                gm->animator = m_rm->get< animator_data >( table.get_string( "animator" ) );
26                pose_data_set* poses = gm->animator.lock()->poses;
27                if ( !def_data || !def_data.lock()->node_data )
28                        gm->bind_data = m_animator_binds->add( id, new animator_bind_data( poses->get_tree(), poses->get_tree() ) );
29                else
30                        gm->bind_data = m_animator_binds->add( id, new animator_bind_data( poses->get_tree(), *def_data.lock()->node_data ) );
31        }
32
33        if ( table.is_table( "model" ) )
34        {
35                lua::table_guard model_table( table, "model" );
36                read_model_node( model_table, gm, def_data );
37        }
38
39        add( id, gm );
40        return true;
41}
42
43
44
45void nv::model_manager::read_model_node( lua::table_guard& table, model_node* node, resource< mesh_data > def_data )
46{
47        resource< data_channel_set > cmesh;
48        resource< material >         cmaterial;
49        sint16 attach_id = -1;
50
51        if ( table.is_string( "material" ) )
52                cmaterial = m_rm->get< material >( table.get_string128( "material" ) );
53
54        if ( table.has_field( "path" ) )
55        {
56                nv::string128 cpath( table.get_string128( "path" ) );
57                nv::data_node_info info;
58                cmesh = m_mesh_datas->get_path( cpath, def_data, &info );
59                attach_id = info.parent_id;
60        }
61
62        if ( table.has_field( "attach" ) )
63        {
64                if ( table.is_number( "attach" ) )
65                {
66                        attach_id = sint16( table.get_integer( "attach", -1 ) );
67                        //                              parent_id = 0;
68                }
69                else if ( auto m = def_data.lock() )
70                {
71                        auto it = m->node_names.find( table.get_string_hash_64( "attach" ) );
72                        if ( it != m->node_names.end() )
73                                attach_id = sint16( it->second + 1 );
74                        int error; int hack;
75                }
76        }
77
78        node->force = table.get_boolean( "force", false );
79        node->chance = table.get_float( "chance", 1.0f );
80        node->random_rotate_y = table.get_boolean( "random_rotate", false );
81
82        node->mesh = cmesh;
83        node->attach_id = attach_id;
84        node->material = cmaterial;
85
86        for ( uint32 i = 1; i <= table.get_size(); ++i )
87        {
88                lua::table_guard child_table( table, i );
89                model_node* child = new model_node;
90                node->children.push_back( child );
91                read_model_node( child_table, child, def_data );
92        }
93}
94
Note: See TracBrowser for help on using the repository browser.