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

Last change on this file since 509 was 509, checked in by epyon, 9 years ago
  • random distributions
  • resource - rename/remove support
  • debug gizmo support
  • minor resource_manager upgrades
  • several minor changes
File size: 4.3 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        auto vec4_to_quat = [] ( const vec4& v ) { return quat( v.w, v.x, v.y, v.z ); };
16
17        model* gm = new model;
18        gm->attach = table.get_string_hash_64( "attach" );
19        gm->root.set_position( table.get<vec3>( "root_position", vec3() ) );
20        gm->root.set_orientation( vec4_to_quat( table.get<vec4>( "root_orientation", vec4(0.0f,0.0f,0.0f,1.0f) ) ) );
21       
22        resource< mesh_data > def_data;
23        if ( table.is_string( "path" ) )
24                def_data = m_rm->get< mesh_data >( table.get_string128( "path" ) );
25
26        if ( table.has_field( "animator" ) )
27        {
28                gm->animator = m_rm->get< animator_data >( table.get_string( "animator" ) );
29                pose_data_set* poses = gm->animator.lock()->poses;
30                if ( !def_data || !def_data.lock()->node_data )
31                        gm->bind_data = m_animator_binds->add( id, new animator_bind_data( poses->get_tree(), poses->get_tree() ) );
32                else
33                        gm->bind_data = m_animator_binds->add( id, new animator_bind_data( poses->get_tree(), *def_data.lock()->node_data ) );
34        }
35
36        if ( table.is_table( "model" ) )
37        {
38                lua::table_guard model_table( table, "model" );
39                read_model_node( model_table, gm, def_data );
40        }
41
42        add( id, gm );
43        return true;
44}
45
46
47
48void nv::model_manager::read_model_node( lua::table_guard& table, model_node* node, resource< mesh_data > def_data )
49{
50        auto vec4_to_quat = [] ( const vec4& v ) { return quat( v.w, v.x, v.y, v.z ); };
51
52        resource< data_channel_set > cmesh;
53        resource< material >         cmaterial;
54        sint16 attach_id = -1;
55
56        if ( table.is_string( "material" ) )
57                cmaterial = m_rm->get< material >( table.get_string128( "material" ) );
58
59        if ( table.has_field( "path" ) )
60        {
61                nv::string128 cpath( table.get_string128( "path" ) );
62                nv::data_node_info info;
63                cmesh = m_mesh_datas->get_path( cpath, def_data, &info );
64                attach_id = info.parent_id;
65        }
66
67        if ( table.has_field( "local_position" ) )
68                node->local.set_position( table.get<vec3>( "local_position", vec3() ) );
69        if ( table.has_field( "local_orientation" ) )
70                node->local.set_orientation( vec4_to_quat( table.get<vec4>( "local_orientation", vec4( 0.0f, 0.0f, 0.0f, 1.0f ) ) ) );
71
72        if ( table.has_field( "position" ) )
73        {
74                node->position.min = table.get<vec3>( "position", vec3() );
75                node->position.max = node->position.min;
76        }
77        if ( table.has_field( "rotation" ) )
78        {
79                node->rotation.min = table.get<vec3>( "rotation", vec3() );
80                node->rotation.max = node->rotation.min;
81        }
82        if ( table.has_field( "position_min" ) )  node->position.min  = table.get<vec3>( "position_min", vec3() );
83        if ( table.has_field( "position_max" ) )  node->position.max  = table.get<vec3>( "position_max", vec3() );
84        if ( table.has_field( "position_dist" ) ) node->position.dist = random_dist( table.get_unsigned( "position_dist", 0 ) );
85        if ( table.has_field( "rotation_min" ) )  node->rotation.min  = table.get<vec3>( "rotation_min", vec3() );
86        if ( table.has_field( "rotation_max" ) )  node->rotation.max  = table.get<vec3>( "rotation_max", vec3() );
87        if ( table.has_field( "rotation_dist" ) ) node->rotation.dist = random_dist( table.get_unsigned( "rotation_dist", 0 ) );
88
89        if ( table.has_field( "attach" ) )
90        {
91                if ( table.is_number( "attach" ) )
92                {
93                        attach_id = sint16( table.get_integer( "attach", -1 ) );
94                        //                              parent_id = 0;
95                }
96                else if ( auto m = def_data.lock() )
97                {
98                        auto it = m->node_names.find( table.get_string_hash_64( "attach" ) );
99                        if ( it != m->node_names.end() )
100                                attach_id = sint16( it->second + 1 );
101                        int error; int hack;
102                }
103        }
104
105        node->force     = table.get_boolean( "force", false );
106        node->choice    = table.get_boolean( "choice", false );
107        node->chance    = table.get_float( "chance", 1.0f );
108        node->weight    = table.get_unsigned( "weight", 1 );
109        node->mesh      = cmesh;
110        node->attach_id = attach_id;
111        node->material  = cmaterial;
112
113        for ( uint32 i = 1; i <= table.get_size(); ++i )
114        {
115                lua::table_guard child_table( table, i );
116                model_node* child = new model_node;
117                node->children.push_back( child );
118                read_model_node( child_table, child, def_data );
119        }
120}
121
Note: See TracBrowser for help on using the repository browser.