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

Last change on this file since 515 was 515, checked in by epyon, 9 years ago
  • model tag support
  • local transform particle engines
  • fix for 3d textures
  • minor cleanups/fixes
File size: 4.5 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.is_string( "tag" ) )
62                node->tag = table.get_string32( "tag" );
63
64        if ( table.has_field( "path" ) )
65        {
66                nv::string128 cpath( table.get_string128( "path" ) );
67                nv::data_node_info info;
68                cmesh = m_mesh_datas->get_path( cpath, def_data, &info );
69                attach_id = info.parent_id;
70        }
71
72        if ( table.has_field( "local_position" ) )
73                node->local.set_position( table.get<vec3>( "local_position", vec3() ) );
74        if ( table.has_field( "local_orientation" ) )
75                node->local.set_orientation( vec4_to_quat( table.get<vec4>( "local_orientation", vec4( 0.0f, 0.0f, 0.0f, 1.0f ) ) ) );
76
77        if ( table.has_field( "position" ) )
78        {
79                node->position.min = table.get<vec3>( "position", vec3() );
80                node->position.max = node->position.min;
81        }
82        if ( table.has_field( "rotation" ) )
83        {
84                node->rotation.min = table.get<vec3>( "rotation", vec3() );
85                node->rotation.max = node->rotation.min;
86        }
87        if ( table.has_field( "position_min" ) )  node->position.min  = table.get<vec3>( "position_min", vec3() );
88        if ( table.has_field( "position_max" ) )  node->position.max  = table.get<vec3>( "position_max", vec3() );
89        if ( table.has_field( "position_dist" ) ) node->position.dist = random_dist( table.get_unsigned( "position_dist", 0 ) );
90        if ( table.has_field( "rotation_min" ) )  node->rotation.min  = table.get<vec3>( "rotation_min", vec3() );
91        if ( table.has_field( "rotation_max" ) )  node->rotation.max  = table.get<vec3>( "rotation_max", vec3() );
92        if ( table.has_field( "rotation_dist" ) ) node->rotation.dist = random_dist( table.get_unsigned( "rotation_dist", 0 ) );
93
94        if ( table.has_field( "attach" ) )
95        {
96                if ( table.is_number( "attach" ) )
97                {
98                        attach_id = sint16( table.get_integer( "attach", -1 ) );
99                        //                              parent_id = 0;
100                }
101                else if ( auto m = def_data.lock() )
102                {
103                        auto it = m->node_names.find( table.get_string_hash_64( "attach" ) );
104                        if ( it != m->node_names.end() )
105                                attach_id = sint16( it->second + 1 );
106                        int error; int hack;
107                }
108        }
109
110        node->force     = table.get_boolean( "force", false );
111        node->choice    = model_node_choice( table.get_unsigned( "choice", false ) );
112        node->chance    = table.get_float( "chance", 1.0f );
113        node->weight    = table.get_unsigned( "weight", 1 );
114        node->mesh      = cmesh;
115        node->attach_id = attach_id;
116        node->material  = cmaterial;
117
118        for ( uint32 i = 1; i <= table.get_size(); ++i )
119        {
120                lua::table_guard child_table( table, i );
121                model_node* child = new model_node;
122                node->children.push_back( child );
123                read_model_node( child_table, child, def_data );
124        }
125}
126
Note: See TracBrowser for help on using the repository browser.