[283] | 1 | // Copyright (C) 2014 ChaosForge / Kornel Kisielewicz
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of NV Libraries.
|
---|
| 5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 6 |
|
---|
| 7 | #include "nv/formats/nmd_loader.hh"
|
---|
| 8 | #include "nv/io/std_stream.hh"
|
---|
[287] | 9 | #include "nv/string.hh"
|
---|
[283] | 10 |
|
---|
| 11 | using namespace nv;
|
---|
| 12 |
|
---|
| 13 | bool nv::nmd_loader::load( stream& source )
|
---|
| 14 | {
|
---|
| 15 | // TODO: proper error handling
|
---|
| 16 | reset();
|
---|
| 17 | nmd_header root_header;
|
---|
| 18 | source.read( &root_header, sizeof( root_header ), 1 );
|
---|
| 19 | for ( uint32 i = 0; i < root_header.elements; ++i )
|
---|
| 20 | {
|
---|
| 21 | nmd_element_header element_header;
|
---|
| 22 | source.read( &element_header, sizeof( element_header ), 1 );
|
---|
| 23 | switch ( element_header.type )
|
---|
| 24 | {
|
---|
[284] | 25 | case nmd_type::MESH : load_mesh( source, element_header ); break;
|
---|
| 26 | case nmd_type::ANIMATION : load_animation( source, element_header ); break;
|
---|
[283] | 27 | case nmd_type::STRING_TABLE : load_strings( source ); break;
|
---|
| 28 | default: NV_ASSERT( false, "UNKNOWN NMD ELEMENT!" ); break;
|
---|
| 29 | }
|
---|
| 30 | }
|
---|
| 31 | return true;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[284] | 34 | bool nv::nmd_loader::load_mesh( stream& source, const nmd_element_header& e )
|
---|
[283] | 35 | {
|
---|
| 36 | mesh_data* mesh = new mesh_data();
|
---|
[284] | 37 | for ( uint32 s = 0; s < e.children; ++s )
|
---|
[283] | 38 | {
|
---|
| 39 | nmd_element_header element_header;
|
---|
| 40 | source.read( &element_header, sizeof( element_header ), 1 );
|
---|
| 41 | NV_ASSERT( element_header.type == nmd_type::STREAM, "STREAM expected!" );
|
---|
| 42 |
|
---|
| 43 | nmd_stream_header stream_header;
|
---|
| 44 | source.read( &stream_header, sizeof( stream_header ), 1 );
|
---|
| 45 | mesh_raw_channel* channel = mesh_raw_channel::create( stream_header.format, stream_header.count );
|
---|
| 46 | source.read( channel->data, stream_header.format.size, stream_header.count );
|
---|
| 47 | mesh->add_channel( channel );
|
---|
| 48 | }
|
---|
[287] | 49 | m_mesh_names.push_back( e.name );
|
---|
[283] | 50 | m_meshes.push_back( mesh );
|
---|
| 51 | return true;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | mesh_data* nv::nmd_loader::release_mesh_data( size_t index )
|
---|
| 55 | {
|
---|
| 56 | mesh_data* result = m_meshes[ index ];
|
---|
[287] | 57 | if ( m_strings ) result->set_name( m_strings->get( m_mesh_names[ index ] ) );
|
---|
[283] | 58 | m_meshes[ index ] = nullptr;
|
---|
| 59 | return result;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[287] | 62 | mesh_data_pack* nv::nmd_loader::release_mesh_data_pack()
|
---|
| 63 | {
|
---|
| 64 | uint32 size = m_meshes.size();
|
---|
| 65 | mesh_data* meshes = new mesh_data[ size ];
|
---|
| 66 | for ( uint32 i = 0; i < size; ++i )
|
---|
| 67 | {
|
---|
| 68 | m_meshes[i]->move_to( meshes[i] );
|
---|
| 69 | delete m_meshes[i];
|
---|
| 70 | }
|
---|
| 71 | m_meshes.clear();
|
---|
| 72 | return new mesh_data_pack( size, meshes, release_mesh_nodes_data() );
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[283] | 75 | void nv::nmd_loader::reset()
|
---|
| 76 | {
|
---|
| 77 | for ( auto mesh : m_meshes ) if ( mesh ) delete mesh;
|
---|
| 78 | if ( m_strings ) delete m_strings;
|
---|
[287] | 79 | if ( m_node_data ) delete m_node_data;
|
---|
[283] | 80 | m_meshes.clear();
|
---|
[287] | 81 | m_mesh_names.clear();
|
---|
| 82 | m_node_names.clear();
|
---|
| 83 |
|
---|
| 84 | m_node_data = nullptr;
|
---|
| 85 | m_node_array = nullptr;
|
---|
| 86 | m_strings = nullptr;
|
---|
[283] | 87 | }
|
---|
| 88 |
|
---|
| 89 | nv::nmd_loader::~nmd_loader()
|
---|
| 90 | {
|
---|
| 91 | reset();
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | bool nv::nmd_loader::load_strings( stream& source )
|
---|
| 95 | {
|
---|
| 96 | NV_ASSERT( m_strings == nullptr, "MULTIPLE STRING ENTRIES!" );
|
---|
| 97 | m_strings = new string_table( &source );
|
---|
| 98 | return true;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[284] | 101 | bool nv::nmd_loader::load_animation( stream& source, const nmd_element_header& e )
|
---|
[283] | 102 | {
|
---|
[287] | 103 | NV_ASSERT( m_node_data == nullptr, "MULTIPLE NODE ENTRIES!" );
|
---|
| 104 | nmd_animation_header animation_header;
|
---|
| 105 | source.read( &animation_header, sizeof( animation_header ), 1 );
|
---|
| 106 | m_node_array = new mesh_node_data[ e.children ];
|
---|
[284] | 107 | for ( uint32 i = 0; i < e.children; ++i )
|
---|
[283] | 108 | {
|
---|
| 109 | nmd_element_header element_header;
|
---|
| 110 | source.read( &element_header, sizeof( element_header ), 1 );
|
---|
[287] | 111 | NV_ASSERT( element_header.type == nmd_type::NODE, "NODE expected!" );
|
---|
| 112 | m_node_names.push_back( element_header.name );
|
---|
[283] | 113 | uint16 ch_count = element_header.children;
|
---|
| 114 |
|
---|
[287] | 115 | nmd_node_header node_header;
|
---|
[283] | 116 | source.read( &node_header, sizeof( node_header ), 1 );
|
---|
[287] | 117 | m_node_array[i].parent_id = node_header.parent_id;
|
---|
| 118 | m_node_array[i].transform = node_header.transform;
|
---|
| 119 | m_node_array[i].data = nullptr;
|
---|
[283] | 120 | if ( ch_count > 0 )
|
---|
| 121 | {
|
---|
[285] | 122 | key_data* kdata = new key_data;
|
---|
[287] | 123 | m_node_array[i].data = kdata;
|
---|
[283] | 124 | for ( uint32 c = 0; c < ch_count; ++c )
|
---|
| 125 | {
|
---|
| 126 | source.read( &element_header, sizeof( element_header ), 1 );
|
---|
[287] | 127 | NV_ASSERT( element_header.type == nmd_type::KEY_CHANNEL, "CHANNEL expected!" );
|
---|
| 128 | nv::nmd_key_channel_header cheader;
|
---|
[283] | 129 | source.read( &cheader, sizeof( cheader ), 1 );
|
---|
| 130 | key_raw_channel* channel = key_raw_channel::create( cheader.format, cheader.count );
|
---|
| 131 | source.read( channel->data, channel->desc.size, channel->count );
|
---|
[285] | 132 | kdata->add_channel( channel );
|
---|
[283] | 133 | }
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
[287] | 136 | m_node_data = new mesh_nodes_data( "animation", e.children, m_node_array, animation_header.frame_rate, animation_header.duration, animation_header.flat );
|
---|
[283] | 137 | return true;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[287] | 140 | mesh_nodes_data* nv::nmd_loader::release_mesh_nodes_data()
|
---|
[283] | 141 | {
|
---|
[287] | 142 | if ( m_node_data )
|
---|
[283] | 143 | {
|
---|
[287] | 144 | if ( m_strings )
|
---|
[283] | 145 | {
|
---|
[287] | 146 | for ( uint32 i = 0; i < m_node_data->get_count(); ++i )
|
---|
[283] | 147 | {
|
---|
[287] | 148 | m_node_array[i].name = m_strings->get( m_node_names[i] );
|
---|
[283] | 149 | }
|
---|
| 150 | }
|
---|
[287] | 151 | mesh_nodes_data* result = m_node_data;
|
---|
| 152 | m_node_data = nullptr;
|
---|
| 153 | m_node_array = nullptr;
|
---|
| 154 | return result;
|
---|
[283] | 155 | }
|
---|
[287] | 156 | return nullptr;
|
---|
[283] | 157 | }
|
---|
| 158 |
|
---|
[287] | 159 | // TEMPORARY
|
---|
[283] | 160 |
|
---|
[287] | 161 | nv::nmd_temp_model_data::nmd_temp_model_data( nmd_loader* loader )
|
---|
[283] | 162 | {
|
---|
| 163 | for ( unsigned m = 0; m < loader->get_mesh_count(); ++m )
|
---|
| 164 | {
|
---|
| 165 | m_mesh_data.push_back(loader->release_mesh_data(m));
|
---|
| 166 | }
|
---|
[287] | 167 | m_node_data = loader->release_mesh_nodes_data();
|
---|
[283] | 168 | }
|
---|
| 169 |
|
---|
[287] | 170 | nv::nmd_temp_model_data::~nmd_temp_model_data()
|
---|
[283] | 171 | {
|
---|
| 172 | for ( unsigned m = 0; m < m_mesh_data.size(); ++m )
|
---|
| 173 | {
|
---|
| 174 | delete m_mesh_data[m];
|
---|
| 175 | }
|
---|
[287] | 176 | delete m_node_data;
|
---|
[283] | 177 | }
|
---|