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

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