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 |
|
---|
13 | using namespace nv;
|
---|
14 |
|
---|
15 | bool 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["flags"].as< flags<32> >();
|
---|
21 | gm->attach = table["attach"].get_shash64();
|
---|
22 | gm->root.set_position( table["root_position"].as<vec3>() );
|
---|
23 | gm->root.set_orientation( vec4_to_quat( table["root_orientation"].as<vec4>( vec4(0.0f,0.0f,0.0f,1.0f) ) ) );
|
---|
24 |
|
---|
25 | resource< mesh_data > def_data;
|
---|
26 | if ( table["path"].is_string() )
|
---|
27 | def_data = m_rm->get< mesh_data >( table["path"].get_string128() );
|
---|
28 |
|
---|
29 | if ( table["ragdoll"].is_string() )
|
---|
30 | gm->ragdoll_id = table["ragdoll"].get_string32();
|
---|
31 |
|
---|
32 | if ( table["animator"] )
|
---|
33 | {
|
---|
34 | gm->animator = m_rm->get< animator_data >( table["animator"].get_string() );
|
---|
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[ "phx_mesh" ] )
|
---|
43 | {
|
---|
44 | nv::string128 cpath( table["phx_mesh"].get_string128() );
|
---|
45 | gm->phx_mesh = m_mesh_datas->get_path( cpath, def_data );
|
---|
46 | }
|
---|
47 |
|
---|
48 | if ( table["model"].is_table() )
|
---|
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 |
|
---|
60 | void 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["material"].is_string() )
|
---|
69 | {
|
---|
70 | nv::string128 mat_id( table["material"].get_string128() );
|
---|
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["tag"].is_string() )
|
---|
77 | node->tag = table["tag"].get_string32();
|
---|
78 |
|
---|
79 | if ( table[ "path" ] )
|
---|
80 | {
|
---|
81 | nv::string128 cpath( table["path"].get_string128() );
|
---|
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[ "phx_hextents"] )
|
---|
88 | node->phx_hextents = table["phx_hextents"].as<vec3>();
|
---|
89 | if ( table[ "phx_offset"] )
|
---|
90 | node->phx_offset = table["phx_offset"].as<vec3>();
|
---|
91 | if ( table[ "phx_mass"] )
|
---|
92 | node->phx_mass = table["phx_mass"].as<float>( 0.0f );
|
---|
93 | if ( table["phx_mass"] )
|
---|
94 | node->phx_shape = nv::phx_shape( table["phx_shape"].as<int>( 0 ) );
|
---|
95 |
|
---|
96 | if ( table["local_position"] )
|
---|
97 | node->local.set_position( table["local_position"].as<vec3>() );
|
---|
98 | if ( table["local_orientation"] )
|
---|
99 | node->local.set_orientation( vec4_to_quat( table["local_orientation"].as<vec4>( vec4( 0.0f, 0.0f, 0.0f, 1.0f ) ) ) );
|
---|
100 |
|
---|
101 | if ( table["position"] )
|
---|
102 | {
|
---|
103 | node->position.min = table["position"].as<vec3>();
|
---|
104 | node->position.max = node->position.min;
|
---|
105 | }
|
---|
106 | if ( table["rotation"] )
|
---|
107 | {
|
---|
108 | node->rotation.min = table["rotation"].as<vec3>();
|
---|
109 | node->rotation.max = node->rotation.min;
|
---|
110 | }
|
---|
111 | if ( table["position_min"] ) node->position.min = table["position_min"].as<vec3>();
|
---|
112 | if ( table["position_max"] ) node->position.max = table["position_max"].as<vec3>();
|
---|
113 | if ( table["position_dist"] ) node->position.dist = random_dist( table["position_dist"].get_uint32() );
|
---|
114 | if ( table["rotation_min"] ) node->rotation.min = table["rotation_min"].as<vec3>();
|
---|
115 | if ( table["rotation_max"] ) node->rotation.max = table["rotation_max"].as<vec3>();
|
---|
116 | if ( table["rotation_dist"] ) node->rotation.dist = random_dist( table["rotation_dist"].get_uint32() );
|
---|
117 |
|
---|
118 | if ( table["attach"] )
|
---|
119 | {
|
---|
120 | if ( table["attach"].is_number() )
|
---|
121 | {
|
---|
122 | attach_id = sint16( table["attach"].get_sint32( -1 ) );
|
---|
123 | // parent_id = 0;
|
---|
124 | }
|
---|
125 | else if ( auto m = def_data.lock() )
|
---|
126 | {
|
---|
127 | auto it = m->node_names.find( table["attach"].get_shash64() );
|
---|
128 | if ( it != m->node_names.end() )
|
---|
129 | attach_id = sint16( it->second + 1 );
|
---|
130 | int error; int hack;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | node->nflags = table["flags"].as< flags<16,uint16> >();
|
---|
135 | node->choice = model_node_choice( table["choice"].get_uint32( 0 ) );
|
---|
136 | node->chance = table["chance"].get_f32( 1.0f );
|
---|
137 | node->weight = static_cast< uint16 >( table["weight"].get_uint32( 1 ) );
|
---|
138 | node->mesh = cmesh;
|
---|
139 | node->attach_id = attach_id;
|
---|
140 | node->material = cmaterial;
|
---|
141 |
|
---|
142 | for ( uint32 i = 1; i <= table.size(); ++i )
|
---|
143 | {
|
---|
144 | lua::table_guard child_table( table, i );
|
---|
145 | model_node* child = new model_node;
|
---|
146 | node->children.push_back( child );
|
---|
147 | read_model_node( child_table, child, def_data );
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|