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

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