[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"
|
---|
[368] | 8 | #include "nv/stl/string.hh"
|
---|
[416] | 9 | #include "nv/interface/data_channel_access.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 );
|
---|
[420] | 19 | skip_attributes( source, root_header.attributes );
|
---|
[283] | 20 | for ( uint32 i = 0; i < root_header.elements; ++i )
|
---|
| 21 | {
|
---|
| 22 | nmd_element_header element_header;
|
---|
| 23 | source.read( &element_header, sizeof( element_header ), 1 );
|
---|
[420] | 24 | skip_attributes( source, element_header.attributes );
|
---|
[283] | 25 | switch ( element_header.type )
|
---|
| 26 | {
|
---|
[284] | 27 | case nmd_type::MESH : load_mesh( source, element_header ); break;
|
---|
| 28 | case nmd_type::ANIMATION : load_animation( source, element_header ); break;
|
---|
[423] | 29 | case nmd_type::STRINGS : load_strings( source ); break;
|
---|
[283] | 30 | default: NV_ASSERT( false, "UNKNOWN NMD ELEMENT!" ); break;
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 | return true;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[284] | 36 | bool nv::nmd_loader::load_mesh( stream& source, const nmd_element_header& e )
|
---|
[283] | 37 | {
|
---|
[424] | 38 | data_channel_set* mesh = data_channel_set_creator::create_set( e.children );
|
---|
[420] | 39 | load_channel_set( source, mesh, e );
|
---|
| 40 | // m_mesh_names.push_back( e.name );
|
---|
[417] | 41 | m_meshes.push_back( mesh );
|
---|
[283] | 42 | return true;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[416] | 45 | data_channel_set* nv::nmd_loader::release_mesh_data( size_t index )
|
---|
[283] | 46 | {
|
---|
[416] | 47 | data_channel_set* result = m_meshes[ index ];
|
---|
[283] | 48 | m_meshes[ index ] = nullptr;
|
---|
| 49 | return result;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[287] | 52 | mesh_data_pack* nv::nmd_loader::release_mesh_data_pack()
|
---|
| 53 | {
|
---|
| 54 | uint32 size = m_meshes.size();
|
---|
[424] | 55 | data_channel_set* meshes = data_channel_set_creator::create_set_array( size, 0 );
|
---|
[287] | 56 | for ( uint32 i = 0; i < size; ++i )
|
---|
| 57 | {
|
---|
[421] | 58 | meshes[i] = move( *m_meshes[i] );
|
---|
[287] | 59 | delete m_meshes[i];
|
---|
| 60 | }
|
---|
| 61 | m_meshes.clear();
|
---|
| 62 | return new mesh_data_pack( size, meshes, release_mesh_nodes_data() );
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[283] | 65 | void nv::nmd_loader::reset()
|
---|
| 66 | {
|
---|
| 67 | for ( auto mesh : m_meshes ) if ( mesh ) delete mesh;
|
---|
[287] | 68 | if ( m_node_data ) delete m_node_data;
|
---|
[283] | 69 | m_meshes.clear();
|
---|
[287] | 70 |
|
---|
| 71 | m_node_data = nullptr;
|
---|
[283] | 72 | }
|
---|
| 73 |
|
---|
[420] | 74 | void nv::nmd_loader::skip_attributes( stream& source, uint32 count )
|
---|
| 75 | {
|
---|
| 76 | if ( count == 0 ) return;
|
---|
| 77 | source.seek( count * sizeof( nmd_attribute ), origin::CUR );
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[283] | 80 | nv::nmd_loader::~nmd_loader()
|
---|
| 81 | {
|
---|
| 82 | reset();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[423] | 85 | bool nv::nmd_loader::load_strings( stream& source )
|
---|
[283] | 86 | {
|
---|
[425] | 87 | if ( !m_strings ) return true;
|
---|
[423] | 88 | // TODO: load strings optionally
|
---|
[425] | 89 | string_table* strings = new string_table( source );
|
---|
| 90 | m_strings->insert( strings );
|
---|
| 91 | delete strings;
|
---|
[283] | 92 | return true;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[284] | 95 | bool nv::nmd_loader::load_animation( stream& source, const nmd_element_header& e )
|
---|
[283] | 96 | {
|
---|
[287] | 97 | NV_ASSERT( m_node_data == nullptr, "MULTIPLE NODE ENTRIES!" );
|
---|
| 98 | nmd_animation_header animation_header;
|
---|
| 99 | source.read( &animation_header, sizeof( animation_header ), 1 );
|
---|
[470] | 100 | m_node_data = new mesh_nodes_data( e.name, animation_header.frame_rate, animation_header.frame_count, animation_header.flat );
|
---|
[284] | 101 | for ( uint32 i = 0; i < e.children; ++i )
|
---|
[283] | 102 | {
|
---|
| 103 | nmd_element_header element_header;
|
---|
| 104 | source.read( &element_header, sizeof( element_header ), 1 );
|
---|
[420] | 105 | skip_attributes( source, element_header.attributes );
|
---|
[287] | 106 | NV_ASSERT( element_header.type == nmd_type::NODE, "NODE expected!" );
|
---|
[427] | 107 | data_channel_set* set = data_channel_set_creator::create_set( element_header.children );
|
---|
| 108 | load_channel_set( source, set, element_header );
|
---|
[475] | 109 | m_node_data->append( set );
|
---|
[283] | 110 | }
|
---|
[475] | 111 | m_node_data->initialize();
|
---|
[283] | 112 | return true;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[420] | 115 | bool nv::nmd_loader::load_channel( stream& source, data_channel_set* channel_set )
|
---|
| 116 | {
|
---|
| 117 | data_channel_set_creator kaccess( channel_set );
|
---|
| 118 | nmd_channel_header cheader;
|
---|
| 119 | source.read( &cheader, sizeof( cheader ), 1 );
|
---|
| 120 | raw_data_channel_access channel( kaccess.add_channel( cheader.format, cheader.count ) );
|
---|
| 121 | source.read( channel.raw_data(), channel.element_size(), channel.size() );
|
---|
| 122 | return true;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | bool nv::nmd_loader::load_channel_set( stream& source, data_channel_set* channel_set, const nmd_element_header& e )
|
---|
| 126 | {
|
---|
| 127 | data_channel_set_creator kaccess( channel_set );
|
---|
| 128 | for ( uint32 c = 0; c < e.children; ++c )
|
---|
| 129 | {
|
---|
| 130 | load_channel( source, channel_set );
|
---|
| 131 | }
|
---|
[425] | 132 | data_channel_set_creator access( channel_set );
|
---|
| 133 | access.set_name( e.name );
|
---|
| 134 | access.set_parent_id( e.parent_id );
|
---|
| 135 | access.set_transform( e.transform );
|
---|
[420] | 136 | return true;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[291] | 139 | mesh_nodes_data* nv::nmd_loader::release_mesh_nodes_data( size_t )
|
---|
[283] | 140 | {
|
---|
[287] | 141 | if ( m_node_data )
|
---|
[283] | 142 | {
|
---|
[287] | 143 | mesh_nodes_data* result = m_node_data;
|
---|
| 144 | m_node_data = nullptr;
|
---|
| 145 | return result;
|
---|
[283] | 146 | }
|
---|
[287] | 147 | return nullptr;
|
---|
[283] | 148 | }
|
---|
| 149 |
|
---|
[292] | 150 | // ----------------------------------------------------------------
|
---|
| 151 | // nmd format dump
|
---|
| 152 | // HACK : TEMPORARY - will go to it's own file, probably nmd_io
|
---|
[420] | 153 |
|
---|
[423] | 154 | void nv::nmd_dump_header( stream& stream_out, uint32 elements, uint64 name )
|
---|
[292] | 155 | {
|
---|
[423] | 156 | nmd_header header;
|
---|
| 157 | header.id = four_cc<'n', 'm', 'f', '1'>::value;
|
---|
| 158 | header.elements = elements; // +1 string array
|
---|
| 159 | header.name = name;
|
---|
| 160 | header.version = 1;
|
---|
| 161 | header.attributes = 0;
|
---|
| 162 | stream_out.write( &header, sizeof( header ), 1 );
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[427] | 165 | void nv::nmd_dump_element( stream& stream_out, const data_channel_set& data, nmd_type type )
|
---|
[423] | 166 | {
|
---|
| 167 | uint32 size = 0;
|
---|
[427] | 168 | for ( auto& chan : data )
|
---|
[423] | 169 | {
|
---|
| 170 | size += sizeof( nmd_channel_header );
|
---|
| 171 | size += chan.raw_size();
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | nmd_element_header eheader;
|
---|
[427] | 175 | eheader.type = type;
|
---|
| 176 | eheader.children = static_cast<uint16>( data.size() );
|
---|
[423] | 177 | eheader.size = size;
|
---|
[427] | 178 | eheader.name = data.get_name();
|
---|
| 179 | eheader.transform = data.get_transform();
|
---|
| 180 | eheader.parent_id = data.get_parent_id();
|
---|
[423] | 181 | eheader.attributes = 0;
|
---|
| 182 | stream_out.write( &eheader, sizeof( eheader ), 1 );
|
---|
[427] | 183 | for ( auto& channel : data )
|
---|
| 184 | {
|
---|
| 185 | nmd_channel_header cheader;
|
---|
| 186 | cheader.format = channel.descriptor();
|
---|
| 187 | cheader.count = channel.size();
|
---|
| 188 | stream_out.write( &cheader, sizeof( cheader ), 1 );
|
---|
| 189 | stream_out.write( channel.raw_data(), channel.element_size(), channel.size() );
|
---|
| 190 | }
|
---|
[423] | 191 | }
|
---|
| 192 |
|
---|
| 193 | void nv::nmd_dump_nodes( stream& stream_out, const mesh_nodes_data& nodes )
|
---|
| 194 | {
|
---|
[292] | 195 | uint32 total = sizeof( nmd_animation_header );
|
---|
[427] | 196 | for ( auto node : nodes )
|
---|
[292] | 197 | {
|
---|
[420] | 198 | total += sizeof( nmd_element_header );
|
---|
[427] | 199 | for ( uint32 c = 0; c < node->size(); ++c )
|
---|
| 200 | {
|
---|
| 201 | total += sizeof( nmd_channel_header );
|
---|
| 202 | total += node->get_channel( c )->raw_size();
|
---|
| 203 | }
|
---|
[292] | 204 | }
|
---|
| 205 |
|
---|
| 206 | nmd_element_header header;
|
---|
[423] | 207 | header.type = nmd_type::ANIMATION;
|
---|
[428] | 208 | header.children = static_cast<uint16>( nodes.size() );
|
---|
[423] | 209 | header.size = total;
|
---|
| 210 | header.name = nodes.get_name();
|
---|
| 211 | header.transform = mat4();
|
---|
| 212 | header.parent_id = -1;
|
---|
[420] | 213 | header.attributes = 0;
|
---|
| 214 |
|
---|
[292] | 215 | stream_out.write( &header, sizeof( header ), 1 );
|
---|
| 216 |
|
---|
| 217 | nmd_animation_header aheader;
|
---|
[470] | 218 | aheader.frame_rate = nodes.get_fps();
|
---|
| 219 | aheader.frame_count = nodes.get_frame_count();
|
---|
[423] | 220 | aheader.flat = nodes.is_flat();
|
---|
[292] | 221 | stream_out.write( &aheader, sizeof( aheader ), 1 );
|
---|
| 222 |
|
---|
[427] | 223 | for ( auto node : nodes )
|
---|
[292] | 224 | {
|
---|
[427] | 225 | nmd_dump_element( stream_out, *node, nv::nmd_type::NODE );
|
---|
[292] | 226 | }
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[423] | 229 | void nv::nmd_dump_strings( stream& stream_out, const string_table& strings )
|
---|
[292] | 230 | {
|
---|
[423] | 231 | nmd_element_header sheader;
|
---|
| 232 | sheader.type = nv::nmd_type::STRINGS;
|
---|
| 233 | sheader.children = 0;
|
---|
| 234 | sheader.size = strings.dump_size();
|
---|
[431] | 235 | sheader.name = shash64();
|
---|
[423] | 236 | sheader.parent_id = -1;
|
---|
| 237 | sheader.attributes = 0;
|
---|
| 238 | stream_out.write( &sheader, sizeof( sheader ), 1 );
|
---|
| 239 | strings.dump( stream_out );
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | void nv::nmd_dump( stream& stream_out, const mesh_data_pack* model, const string_table* strings, uint64 name )
|
---|
| 243 | {
|
---|
| 244 | uint32 elements = ( strings ? 1 : 0 ) // +1 string array
|
---|
| 245 | + model->get_count() // meshes
|
---|
| 246 | + ( model->get_node_count() > 0 ? 1 : 0 ); // nodes
|
---|
| 247 | nmd_dump_header( stream_out, elements, name );
|
---|
| 248 |
|
---|
| 249 | for ( uint32 i = 0; i < model->get_count(); ++i )
|
---|
[292] | 250 | {
|
---|
[427] | 251 | nmd_dump_element( stream_out, *model->get_mesh( i ), nv::nmd_type::MESH );
|
---|
[292] | 252 | }
|
---|
| 253 |
|
---|
[423] | 254 | if ( model->get_node_count() > 0 )
|
---|
[292] | 255 | {
|
---|
[423] | 256 | nmd_dump_nodes( stream_out, *model->get_nodes() );
|
---|
[292] | 257 | }
|
---|
| 258 |
|
---|
[423] | 259 | if ( strings )
|
---|
[292] | 260 | {
|
---|
[423] | 261 | nmd_dump_strings( stream_out, *strings );
|
---|
[292] | 262 | }
|
---|
| 263 | }
|
---|