[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 |
|
---|
| 9 | #include "nv/lua/lua_math.hh"
|
---|
[512] | 10 | #include "nv/lua/lua_flags.hh"
|
---|
[507] | 11 |
|
---|
| 12 | using namespace nv;
|
---|
| 13 |
|
---|
| 14 | bool nv::model_manager::load_resource( lua::table_guard& table, shash64 id )
|
---|
| 15 | {
|
---|
[509] | 16 | auto vec4_to_quat = [] ( const vec4& v ) { return quat( v.w, v.x, v.y, v.z ); };
|
---|
| 17 |
|
---|
[507] | 18 | model* gm = new model;
|
---|
[512] | 19 | gm->flags = table.get< flags<32> >( "flags" );
|
---|
[507] | 20 | gm->attach = table.get_string_hash_64( "attach" );
|
---|
[509] | 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 |
|
---|
[507] | 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 |
|
---|
| 50 | void nv::model_manager::read_model_node( lua::table_guard& table, model_node* node, resource< mesh_data > def_data )
|
---|
| 51 | {
|
---|
[509] | 52 | auto vec4_to_quat = [] ( const vec4& v ) { return quat( v.w, v.x, v.y, v.z ); };
|
---|
| 53 |
|
---|
[507] | 54 | resource< data_channel_set > cmesh;
|
---|
| 55 | resource< material > cmaterial;
|
---|
| 56 | sint16 attach_id = -1;
|
---|
| 57 |
|
---|
| 58 | if ( table.is_string( "material" ) )
|
---|
[523] | 59 | {
|
---|
| 60 | nv::string128 mat_id( table.get_string128( "material" ) );
|
---|
| 61 | cmaterial = m_rm->get< material >( mat_id );
|
---|
| 62 | if ( !cmaterial )
|
---|
| 63 | NV_LOG_ERROR( "Can't load material : ", mat_id );
|
---|
| 64 | }
|
---|
[507] | 65 |
|
---|
[515] | 66 | if ( table.is_string( "tag" ) )
|
---|
| 67 | node->tag = table.get_string32( "tag" );
|
---|
| 68 |
|
---|
[507] | 69 | if ( table.has_field( "path" ) )
|
---|
| 70 | {
|
---|
| 71 | nv::string128 cpath( table.get_string128( "path" ) );
|
---|
| 72 | nv::data_node_info info;
|
---|
| 73 | cmesh = m_mesh_datas->get_path( cpath, def_data, &info );
|
---|
| 74 | attach_id = info.parent_id;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[509] | 77 | if ( table.has_field( "local_position" ) )
|
---|
| 78 | node->local.set_position( table.get<vec3>( "local_position", vec3() ) );
|
---|
| 79 | if ( table.has_field( "local_orientation" ) )
|
---|
| 80 | node->local.set_orientation( vec4_to_quat( table.get<vec4>( "local_orientation", vec4( 0.0f, 0.0f, 0.0f, 1.0f ) ) ) );
|
---|
| 81 |
|
---|
| 82 | if ( table.has_field( "position" ) )
|
---|
| 83 | {
|
---|
| 84 | node->position.min = table.get<vec3>( "position", vec3() );
|
---|
| 85 | node->position.max = node->position.min;
|
---|
| 86 | }
|
---|
| 87 | if ( table.has_field( "rotation" ) )
|
---|
| 88 | {
|
---|
| 89 | node->rotation.min = table.get<vec3>( "rotation", vec3() );
|
---|
| 90 | node->rotation.max = node->rotation.min;
|
---|
| 91 | }
|
---|
| 92 | if ( table.has_field( "position_min" ) ) node->position.min = table.get<vec3>( "position_min", vec3() );
|
---|
| 93 | if ( table.has_field( "position_max" ) ) node->position.max = table.get<vec3>( "position_max", vec3() );
|
---|
| 94 | if ( table.has_field( "position_dist" ) ) node->position.dist = random_dist( table.get_unsigned( "position_dist", 0 ) );
|
---|
| 95 | if ( table.has_field( "rotation_min" ) ) node->rotation.min = table.get<vec3>( "rotation_min", vec3() );
|
---|
| 96 | if ( table.has_field( "rotation_max" ) ) node->rotation.max = table.get<vec3>( "rotation_max", vec3() );
|
---|
| 97 | if ( table.has_field( "rotation_dist" ) ) node->rotation.dist = random_dist( table.get_unsigned( "rotation_dist", 0 ) );
|
---|
| 98 |
|
---|
[507] | 99 | if ( table.has_field( "attach" ) )
|
---|
| 100 | {
|
---|
| 101 | if ( table.is_number( "attach" ) )
|
---|
| 102 | {
|
---|
[508] | 103 | attach_id = sint16( table.get_integer( "attach", -1 ) );
|
---|
[507] | 104 | // parent_id = 0;
|
---|
| 105 | }
|
---|
| 106 | else if ( auto m = def_data.lock() )
|
---|
| 107 | {
|
---|
| 108 | auto it = m->node_names.find( table.get_string_hash_64( "attach" ) );
|
---|
| 109 | if ( it != m->node_names.end() )
|
---|
[508] | 110 | attach_id = sint16( it->second + 1 );
|
---|
[507] | 111 | int error; int hack;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
[516] | 114 |
|
---|
| 115 | node->nflags = table.get< flags<16,uint16> >( "flags" );
|
---|
[510] | 116 | node->choice = model_node_choice( table.get_unsigned( "choice", false ) );
|
---|
[509] | 117 | node->chance = table.get_float( "chance", 1.0f );
|
---|
| 118 | node->weight = table.get_unsigned( "weight", 1 );
|
---|
| 119 | node->mesh = cmesh;
|
---|
[507] | 120 | node->attach_id = attach_id;
|
---|
[509] | 121 | node->material = cmaterial;
|
---|
[507] | 122 |
|
---|
| 123 | for ( uint32 i = 1; i <= table.get_size(); ++i )
|
---|
| 124 | {
|
---|
| 125 | lua::table_guard child_table( table, i );
|
---|
| 126 | model_node* child = new model_node;
|
---|
| 127 | node->children.push_back( child );
|
---|
| 128 | read_model_node( child_table, child, def_data );
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|