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