[395] | 1 | // Copyright (C) 2014-2015 ChaosForge Ltd
|
---|
[283] | 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
[395] | 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
[283] | 6 |
|
---|
| 7 | #include "nv/formats/nmd_loader.hh"
|
---|
| 8 | #include "nv/io/std_stream.hh"
|
---|
[368] | 9 | #include "nv/stl/string.hh"
|
---|
[416] | 10 | #include "nv/interface/data_channel_access.hh"
|
---|
[283] | 11 |
|
---|
| 12 | using namespace nv;
|
---|
| 13 |
|
---|
| 14 | bool nv::nmd_loader::load( stream& source )
|
---|
| 15 | {
|
---|
| 16 | // TODO: proper error handling
|
---|
| 17 | reset();
|
---|
| 18 | nmd_header root_header;
|
---|
| 19 | source.read( &root_header, sizeof( root_header ), 1 );
|
---|
[420] | 20 | skip_attributes( source, root_header.attributes );
|
---|
[283] | 21 | for ( uint32 i = 0; i < root_header.elements; ++i )
|
---|
| 22 | {
|
---|
| 23 | nmd_element_header element_header;
|
---|
| 24 | source.read( &element_header, sizeof( element_header ), 1 );
|
---|
[420] | 25 | skip_attributes( source, element_header.attributes );
|
---|
[283] | 26 | switch ( element_header.type )
|
---|
| 27 | {
|
---|
[284] | 28 | case nmd_type::MESH : load_mesh( source, element_header ); break;
|
---|
| 29 | case nmd_type::ANIMATION : load_animation( source, element_header ); break;
|
---|
[423] | 30 | case nmd_type::STRINGS : load_strings( source ); break;
|
---|
[283] | 31 | default: NV_ASSERT( false, "UNKNOWN NMD ELEMENT!" ); break;
|
---|
| 32 | }
|
---|
| 33 | }
|
---|
| 34 | return true;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[284] | 37 | bool nv::nmd_loader::load_mesh( stream& source, const nmd_element_header& e )
|
---|
[283] | 38 | {
|
---|
[424] | 39 | data_channel_set* mesh = data_channel_set_creator::create_set( e.children );
|
---|
[420] | 40 | load_channel_set( source, mesh, e );
|
---|
| 41 | // m_mesh_names.push_back( e.name );
|
---|
[417] | 42 | m_meshes.push_back( mesh );
|
---|
[283] | 43 | return true;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[416] | 46 | data_channel_set* nv::nmd_loader::release_mesh_data( size_t index )
|
---|
[283] | 47 | {
|
---|
[416] | 48 | data_channel_set* result = m_meshes[ index ];
|
---|
[420] | 49 | // if ( m_strings ) data_channel_set_creator( result ).set_name( m_strings->get( m_mesh_names[ index ] ) );
|
---|
[283] | 50 | m_meshes[ index ] = nullptr;
|
---|
| 51 | return result;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[287] | 54 | mesh_data_pack* nv::nmd_loader::release_mesh_data_pack()
|
---|
| 55 | {
|
---|
| 56 | uint32 size = m_meshes.size();
|
---|
[424] | 57 | data_channel_set* meshes = data_channel_set_creator::create_set_array( size, 0 );
|
---|
[287] | 58 | for ( uint32 i = 0; i < size; ++i )
|
---|
| 59 | {
|
---|
[421] | 60 | meshes[i] = move( *m_meshes[i] );
|
---|
[287] | 61 | delete m_meshes[i];
|
---|
| 62 | }
|
---|
| 63 | m_meshes.clear();
|
---|
| 64 | return new mesh_data_pack( size, meshes, release_mesh_nodes_data() );
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[283] | 67 | void nv::nmd_loader::reset()
|
---|
| 68 | {
|
---|
| 69 | for ( auto mesh : m_meshes ) if ( mesh ) delete mesh;
|
---|
[423] | 70 | if ( m_strings ) delete m_strings;
|
---|
[287] | 71 | if ( m_node_data ) delete m_node_data;
|
---|
[283] | 72 | m_meshes.clear();
|
---|
[420] | 73 | // m_mesh_names.clear();
|
---|
| 74 | // m_node_names.clear();
|
---|
[287] | 75 |
|
---|
| 76 | m_node_data = nullptr;
|
---|
| 77 | m_node_array = nullptr;
|
---|
[423] | 78 | m_strings = nullptr;
|
---|
[283] | 79 | }
|
---|
| 80 |
|
---|
[420] | 81 | void nv::nmd_loader::skip_attributes( stream& source, uint32 count )
|
---|
| 82 | {
|
---|
| 83 | if ( count == 0 ) return;
|
---|
| 84 | source.seek( count * sizeof( nmd_attribute ), origin::CUR );
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[283] | 87 | nv::nmd_loader::~nmd_loader()
|
---|
| 88 | {
|
---|
| 89 | reset();
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[423] | 92 | bool nv::nmd_loader::load_strings( stream& source )
|
---|
[283] | 93 | {
|
---|
[423] | 94 | NV_ASSERT( m_strings == nullptr, "MULTIPLE STRING ENTRIES!" );
|
---|
| 95 | // TODO: load strings optionally
|
---|
| 96 | m_strings = new string_table( source );
|
---|
[283] | 97 | return true;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[284] | 100 | bool nv::nmd_loader::load_animation( stream& source, const nmd_element_header& e )
|
---|
[283] | 101 | {
|
---|
[287] | 102 | NV_ASSERT( m_node_data == nullptr, "MULTIPLE NODE ENTRIES!" );
|
---|
| 103 | nmd_animation_header animation_header;
|
---|
| 104 | source.read( &animation_header, sizeof( animation_header ), 1 );
|
---|
| 105 | m_node_array = new mesh_node_data[ e.children ];
|
---|
[284] | 106 | for ( uint32 i = 0; i < e.children; ++i )
|
---|
[283] | 107 | {
|
---|
| 108 | nmd_element_header element_header;
|
---|
| 109 | source.read( &element_header, sizeof( element_header ), 1 );
|
---|
[420] | 110 | skip_attributes( source, element_header.attributes );
|
---|
[287] | 111 | NV_ASSERT( element_header.type == nmd_type::NODE, "NODE expected!" );
|
---|
[420] | 112 | // m_node_names.push_back( element_header.name );
|
---|
| 113 | load_node( source, &m_node_array[i], element_header );
|
---|
[283] | 114 | }
|
---|
[423] | 115 | m_node_data = new mesh_nodes_data( e.name, e.children, m_node_array, animation_header.frame_rate, animation_header.duration, animation_header.flat );
|
---|
[283] | 116 | return true;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[420] | 119 | bool nv::nmd_loader::load_node( stream& source, mesh_node_data* data, const nmd_element_header& e )
|
---|
| 120 | {
|
---|
[424] | 121 | data->data = data_channel_set_creator::create_set( e.children );
|
---|
[420] | 122 | if ( e.children > 0 )
|
---|
| 123 | {
|
---|
| 124 | load_channel_set( source, data->data, e );
|
---|
| 125 | }
|
---|
[424] | 126 | data_channel_set_creator access( data->data );
|
---|
| 127 | access.set_name( e.name );
|
---|
| 128 | access.set_parent_id( e.parent_id );
|
---|
| 129 | access.set_transform( e.transform );
|
---|
[420] | 130 | return true;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | bool nv::nmd_loader::load_channel( stream& source, data_channel_set* channel_set )
|
---|
| 134 | {
|
---|
| 135 | data_channel_set_creator kaccess( channel_set );
|
---|
| 136 | nmd_channel_header cheader;
|
---|
| 137 | source.read( &cheader, sizeof( cheader ), 1 );
|
---|
| 138 | raw_data_channel_access channel( kaccess.add_channel( cheader.format, cheader.count ) );
|
---|
| 139 | source.read( channel.raw_data(), channel.element_size(), channel.size() );
|
---|
| 140 | return true;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | bool nv::nmd_loader::load_channel_set( stream& source, data_channel_set* channel_set, const nmd_element_header& e )
|
---|
| 144 | {
|
---|
| 145 | data_channel_set_creator kaccess( channel_set );
|
---|
| 146 | for ( uint32 c = 0; c < e.children; ++c )
|
---|
| 147 | {
|
---|
| 148 | load_channel( source, channel_set );
|
---|
| 149 | }
|
---|
| 150 | return true;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[291] | 153 | mesh_nodes_data* nv::nmd_loader::release_mesh_nodes_data( size_t )
|
---|
[283] | 154 | {
|
---|
[287] | 155 | if ( m_node_data )
|
---|
[283] | 156 | {
|
---|
[420] | 157 | // if ( m_strings )
|
---|
| 158 | // {
|
---|
| 159 | // for ( uint32 i = 0; i < m_node_data->get_count(); ++i )
|
---|
| 160 | // {
|
---|
| 161 | // m_node_array[i].name = m_strings->get( m_node_names[i] );
|
---|
| 162 | // m_node_array[i].name_hash = hash_string< uint64 >( m_strings->get( m_node_names[i] ) );
|
---|
| 163 | // }
|
---|
| 164 | // }
|
---|
[287] | 165 | mesh_nodes_data* result = m_node_data;
|
---|
| 166 | m_node_data = nullptr;
|
---|
| 167 | m_node_array = nullptr;
|
---|
| 168 | return result;
|
---|
[283] | 169 | }
|
---|
[287] | 170 | return nullptr;
|
---|
[283] | 171 | }
|
---|
| 172 |
|
---|
[292] | 173 | // ----------------------------------------------------------------
|
---|
| 174 | // nmd format dump
|
---|
| 175 | // HACK : TEMPORARY - will go to it's own file, probably nmd_io
|
---|
[420] | 176 | static void nmd_dump_channel( const raw_data_channel* channel , stream& stream_out )
|
---|
| 177 | {
|
---|
| 178 | nmd_channel_header sheader;
|
---|
| 179 | sheader.format = channel->descriptor();
|
---|
| 180 | sheader.count = channel->size();
|
---|
| 181 | stream_out.write( &sheader, sizeof( sheader ), 1 );
|
---|
| 182 | stream_out.write( channel->raw_data(), channel->element_size(), channel->size() );
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | static void nmd_dump_channel_set( const data_channel_set* channel_set, stream& stream_out )
|
---|
| 186 | {
|
---|
| 187 | for ( auto& channel : *channel_set )
|
---|
| 188 | {
|
---|
| 189 | nmd_dump_channel( &channel, stream_out );
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | static void nmd_dump_node( const mesh_node_data* node, stream& stream_out )
|
---|
| 194 | {
|
---|
| 195 | uint32 chan_size = 0;
|
---|
| 196 | uint32 chan_count = ( node->data ? node->data->size() : 0 );
|
---|
| 197 | for ( uint32 c = 0; c < chan_count; ++c )
|
---|
[292] | 198 | {
|
---|
[420] | 199 | chan_size += sizeof( nmd_channel_header );
|
---|
| 200 | chan_size += node->data->get_channel( c )->raw_size();
|
---|
| 201 | }
|
---|
[292] | 202 |
|
---|
[420] | 203 | nmd_element_header eheader;
|
---|
| 204 | eheader.type = nmd_type::NODE;
|
---|
| 205 | // eheader.name = strings->insert( node->name );
|
---|
| 206 | eheader.children = static_cast<uint16>( chan_count );
|
---|
| 207 | eheader.size = chan_size;
|
---|
[424] | 208 | eheader.name = node->data->get_name();
|
---|
| 209 | eheader.parent_id = node->data->get_parent_id();
|
---|
| 210 | eheader.transform = node->data->get_transform();
|
---|
[420] | 211 | eheader.attributes = 0;
|
---|
| 212 | stream_out.write( &eheader, sizeof( eheader ), 1 );
|
---|
[424] | 213 | if ( chan_count > 0 ) nmd_dump_channel_set( node->data, stream_out );
|
---|
[292] | 214 | }
|
---|
| 215 |
|
---|
[423] | 216 | void nv::nmd_dump_header( stream& stream_out, uint32 elements, uint64 name )
|
---|
[292] | 217 | {
|
---|
[423] | 218 | nmd_header header;
|
---|
| 219 | header.id = four_cc<'n', 'm', 'f', '1'>::value;
|
---|
| 220 | header.elements = elements; // +1 string array
|
---|
| 221 | header.name = name;
|
---|
| 222 | header.version = 1;
|
---|
| 223 | header.attributes = 0;
|
---|
| 224 | stream_out.write( &header, sizeof( header ), 1 );
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | void nv::nmd_dump_mesh( stream& stream_out, const data_channel_set& mesh )
|
---|
| 228 | {
|
---|
| 229 | uint32 size = 0;
|
---|
| 230 | for ( auto& chan : mesh )
|
---|
| 231 | {
|
---|
| 232 | size += sizeof( nmd_channel_header );
|
---|
| 233 | size += chan.raw_size();
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | nmd_element_header eheader;
|
---|
| 237 | eheader.type = nmd_type::MESH;
|
---|
| 238 | eheader.children = static_cast<uint16>( mesh.size() );
|
---|
| 239 | eheader.size = size;
|
---|
[424] | 240 | eheader.name = mesh.get_name();
|
---|
| 241 | eheader.transform = mesh.get_transform();
|
---|
| 242 | eheader.parent_id = mesh.get_parent_id();
|
---|
[423] | 243 | eheader.attributes = 0;
|
---|
| 244 | stream_out.write( &eheader, sizeof( eheader ), 1 );
|
---|
| 245 | nmd_dump_channel_set( &mesh, stream_out );
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | void nv::nmd_dump_nodes( stream& stream_out, const mesh_nodes_data& nodes )
|
---|
| 249 | {
|
---|
[292] | 250 | uint32 total = sizeof( nmd_animation_header );
|
---|
[423] | 251 | for ( uint32 i = 0; i < nodes.get_count(); ++i )
|
---|
[292] | 252 | {
|
---|
[423] | 253 | const mesh_node_data* node = nodes.get_node( i );
|
---|
[420] | 254 | total += sizeof( nmd_element_header );
|
---|
[292] | 255 | if ( node->data )
|
---|
[416] | 256 | for ( uint32 c = 0; c < node->data->size(); ++c )
|
---|
[292] | 257 | {
|
---|
[420] | 258 | total += sizeof( nmd_channel_header );
|
---|
[423] | 259 | total += node->data->get_channel( c )->raw_size();
|
---|
[292] | 260 | }
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | nmd_element_header header;
|
---|
[423] | 264 | header.type = nmd_type::ANIMATION;
|
---|
| 265 | header.children = static_cast<uint16>( nodes.get_count() );
|
---|
| 266 | header.size = total;
|
---|
| 267 | header.name = nodes.get_name();
|
---|
| 268 | header.transform = mat4();
|
---|
| 269 | header.parent_id = -1;
|
---|
[420] | 270 | header.attributes = 0;
|
---|
| 271 |
|
---|
[292] | 272 | stream_out.write( &header, sizeof( header ), 1 );
|
---|
| 273 |
|
---|
| 274 | nmd_animation_header aheader;
|
---|
[423] | 275 | aheader.frame_rate = nodes.get_frame_rate();
|
---|
| 276 | aheader.duration = nodes.get_duration();
|
---|
| 277 | aheader.flat = nodes.is_flat();
|
---|
[292] | 278 | stream_out.write( &aheader, sizeof( aheader ), 1 );
|
---|
| 279 |
|
---|
[423] | 280 | for ( uint32 i = 0; i < nodes.get_count(); ++i )
|
---|
[292] | 281 | {
|
---|
[423] | 282 | nmd_dump_node( nodes.get_node( i ), stream_out );
|
---|
[292] | 283 | }
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[423] | 286 | void nv::nmd_dump_strings( stream& stream_out, const string_table& strings )
|
---|
[292] | 287 | {
|
---|
[423] | 288 | nmd_element_header sheader;
|
---|
| 289 | sheader.type = nv::nmd_type::STRINGS;
|
---|
| 290 | sheader.children = 0;
|
---|
| 291 | sheader.size = strings.dump_size();
|
---|
| 292 | sheader.name = 0;
|
---|
| 293 | sheader.parent_id = -1;
|
---|
| 294 | sheader.attributes = 0;
|
---|
| 295 | stream_out.write( &sheader, sizeof( sheader ), 1 );
|
---|
| 296 | strings.dump( stream_out );
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | void nv::nmd_dump( stream& stream_out, const mesh_data_pack* model, const string_table* strings, uint64 name )
|
---|
| 300 | {
|
---|
| 301 | uint32 elements = ( strings ? 1 : 0 ) // +1 string array
|
---|
| 302 | + model->get_count() // meshes
|
---|
| 303 | + ( model->get_node_count() > 0 ? 1 : 0 ); // nodes
|
---|
| 304 | nmd_dump_header( stream_out, elements, name );
|
---|
| 305 |
|
---|
| 306 | for ( uint32 i = 0; i < model->get_count(); ++i )
|
---|
[292] | 307 | {
|
---|
[423] | 308 | nmd_dump_mesh( stream_out, *model->get_mesh( i ) );
|
---|
[292] | 309 | }
|
---|
| 310 |
|
---|
[423] | 311 | if ( model->get_node_count() > 0 )
|
---|
[292] | 312 | {
|
---|
[423] | 313 | nmd_dump_nodes( stream_out, *model->get_nodes() );
|
---|
[292] | 314 | }
|
---|
| 315 |
|
---|
[423] | 316 | if ( strings )
|
---|
[292] | 317 | {
|
---|
[423] | 318 | nmd_dump_strings( stream_out, *strings );
|
---|
[292] | 319 | }
|
---|
| 320 | }
|
---|