source: trunk/src/formats/nmd_loader.cc @ 470

Last change on this file since 470 was 470, checked in by epyon, 10 years ago
  • animation time definition fixes
File size: 7.7 KB
RevLine 
[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
11using namespace nv;
12
13bool 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]36bool 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]45data_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]52mesh_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]65void 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]74void 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]80nv::nmd_loader::~nmd_loader()
81{
82        reset();
83}
84
[423]85bool 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]95bool 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 );
109                m_node_data->push_back( set );
[283]110        }
111        return true;
112}
113
[420]114bool nv::nmd_loader::load_channel( stream& source, data_channel_set* channel_set )
115{
116        data_channel_set_creator kaccess( channel_set );
117        nmd_channel_header cheader;
118        source.read( &cheader, sizeof( cheader ), 1 );
119        raw_data_channel_access channel( kaccess.add_channel( cheader.format, cheader.count ) );
120        source.read( channel.raw_data(), channel.element_size(), channel.size() );
121        return true;
122}
123
124bool nv::nmd_loader::load_channel_set( stream& source, data_channel_set* channel_set, const nmd_element_header& e )
125{
126        data_channel_set_creator kaccess( channel_set );
127        for ( uint32 c = 0; c < e.children; ++c )
128        {
129                load_channel( source, channel_set );
130        }
[425]131        data_channel_set_creator access( channel_set );
132        access.set_name( e.name );
133        access.set_parent_id( e.parent_id );
134        access.set_transform( e.transform );
[420]135        return true;
136}
137
[291]138mesh_nodes_data* nv::nmd_loader::release_mesh_nodes_data( size_t )
[283]139{
[287]140        if ( m_node_data )
[283]141        {
[287]142                mesh_nodes_data* result = m_node_data;
143                m_node_data = nullptr;
144                return result;
[283]145        }
[287]146        return nullptr;
[283]147}
148
[292]149// ----------------------------------------------------------------
150// nmd format dump
151// HACK : TEMPORARY - will go to it's own file, probably nmd_io
[420]152
[423]153void nv::nmd_dump_header( stream& stream_out, uint32 elements, uint64 name )
[292]154{
[423]155        nmd_header header;
156        header.id = four_cc<'n', 'm', 'f', '1'>::value;
157        header.elements = elements; // +1 string array
158        header.name = name;
159        header.version = 1;
160        header.attributes = 0;
161        stream_out.write( &header, sizeof( header ), 1 );
162}
163
[427]164void nv::nmd_dump_element( stream& stream_out, const data_channel_set& data, nmd_type type )
[423]165{
166        uint32 size = 0;
[427]167        for ( auto& chan : data )
[423]168        {
169                size += sizeof( nmd_channel_header );
170                size += chan.raw_size();
171        }
172
173        nmd_element_header eheader;
[427]174        eheader.type       = type;
175        eheader.children   = static_cast<uint16>( data.size() );
[423]176        eheader.size       = size;
[427]177        eheader.name       = data.get_name();
178        eheader.transform  = data.get_transform();
179        eheader.parent_id  = data.get_parent_id();
[423]180        eheader.attributes = 0;
181        stream_out.write( &eheader, sizeof( eheader ), 1 );
[427]182        for ( auto& channel : data )
183        {
184                nmd_channel_header cheader;
185                cheader.format = channel.descriptor();
186                cheader.count = channel.size();
187                stream_out.write( &cheader, sizeof( cheader ), 1 );
188                stream_out.write( channel.raw_data(), channel.element_size(), channel.size() );
189        }
[423]190}
191
192void nv::nmd_dump_nodes( stream& stream_out, const mesh_nodes_data& nodes )
193{
[292]194        uint32 total = sizeof( nmd_animation_header );
[427]195        for ( auto node : nodes )
[292]196        {
[420]197                total += sizeof( nmd_element_header );
[427]198                for ( uint32 c = 0; c < node->size(); ++c )
199                {
200                        total += sizeof( nmd_channel_header );
201                        total += node->get_channel( c )->raw_size();
202                }
[292]203        }
204
205        nmd_element_header header;
[423]206        header.type = nmd_type::ANIMATION;
[428]207        header.children = static_cast<uint16>( nodes.size() );
[423]208        header.size = total;
209        header.name = nodes.get_name();
210        header.transform = mat4();
211        header.parent_id = -1;
[420]212        header.attributes = 0;
213
[292]214        stream_out.write( &header, sizeof( header ), 1 );
215
216        nmd_animation_header aheader;
[470]217        aheader.frame_rate  = nodes.get_fps();
218        aheader.frame_count = nodes.get_frame_count();
[423]219        aheader.flat = nodes.is_flat();
[292]220        stream_out.write( &aheader, sizeof( aheader ), 1 );
221
[427]222        for ( auto node : nodes )
[292]223        {
[427]224                nmd_dump_element( stream_out, *node, nv::nmd_type::NODE );
[292]225        }
226}
227
[423]228void nv::nmd_dump_strings( stream& stream_out, const string_table& strings )
[292]229{
[423]230        nmd_element_header sheader;
231        sheader.type       = nv::nmd_type::STRINGS;
232        sheader.children   = 0;
233        sheader.size       = strings.dump_size();
[431]234        sheader.name       = shash64();
[423]235        sheader.parent_id  = -1;
236    sheader.attributes = 0;
237        stream_out.write( &sheader, sizeof( sheader ), 1 );
238        strings.dump( stream_out );
239}
240
241void nv::nmd_dump( stream& stream_out, const mesh_data_pack* model, const string_table* strings, uint64 name )
242{
243        uint32 elements = ( strings ? 1 : 0 ) // +1 string array
244                + model->get_count() // meshes
245                + ( model->get_node_count() > 0 ? 1 : 0 ); // nodes
246        nmd_dump_header( stream_out, elements, name );
247
248        for ( uint32 i = 0; i < model->get_count(); ++i )
[292]249        {
[427]250                nmd_dump_element( stream_out, *model->get_mesh( i ), nv::nmd_type::MESH );
[292]251        }
252
[423]253        if ( model->get_node_count() > 0 )
[292]254        {
[423]255                nmd_dump_nodes( stream_out, *model->get_nodes() );
[292]256        }
257
[423]258        if ( strings )
[292]259        {
[423]260                nmd_dump_strings( stream_out, *strings );
[292]261        }
262}
Note: See TracBrowser for help on using the repository browser.