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

Last change on this file since 480 was 480, checked in by epyon, 10 years ago
  • cleanup of legacy code
  • resource updates
File size: 7.9 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
52void nv::nmd_loader::reset()
53{
54        for ( auto mesh : m_meshes ) if ( mesh ) delete mesh;
[287]55        if ( m_node_data ) delete m_node_data;
[283]56        m_meshes.clear();
[287]57
58        m_node_data  = nullptr;
[283]59}
60
[420]61void nv::nmd_loader::skip_attributes( stream& source, uint32 count )
62{
63        if ( count == 0 ) return;
64        source.seek( count * sizeof( nmd_attribute ), origin::CUR );
65}
66
[283]67nv::nmd_loader::~nmd_loader()
68{
69        reset();
70}
71
[423]72bool nv::nmd_loader::load_strings( stream& source )
[283]73{
[425]74        if ( !m_strings ) return true;
[423]75        // TODO: load strings optionally
[425]76        string_table* strings = new string_table( source );
77        m_strings->insert( strings );
78        delete strings;
[283]79        return true;
80}
81
[284]82bool nv::nmd_loader::load_animation( stream& source, const nmd_element_header& e )
[283]83{
[287]84        NV_ASSERT( m_node_data == nullptr, "MULTIPLE NODE ENTRIES!" );
85        nmd_animation_header animation_header;
86        source.read( &animation_header, sizeof( animation_header ), 1 );
[470]87        m_node_data = new mesh_nodes_data( e.name, animation_header.frame_rate, animation_header.frame_count, animation_header.flat );
[284]88        for ( uint32 i = 0; i < e.children; ++i )
[283]89        {
90                nmd_element_header element_header;
91                source.read( &element_header, sizeof( element_header ), 1 );
[420]92                skip_attributes( source, element_header.attributes );
[287]93                NV_ASSERT( element_header.type == nmd_type::NODE, "NODE expected!" );
[427]94                data_channel_set* set = data_channel_set_creator::create_set( element_header.children );
95                load_channel_set( source, set, element_header );
[475]96                m_node_data->append( set );
[283]97        }
[475]98        m_node_data->initialize();
[283]99        return true;
100}
101
[420]102bool nv::nmd_loader::load_channel( stream& source, data_channel_set* channel_set )
103{
104        data_channel_set_creator kaccess( channel_set );
105        nmd_channel_header cheader;
106        source.read( &cheader, sizeof( cheader ), 1 );
107        raw_data_channel_access channel( kaccess.add_channel( cheader.format, cheader.count ) );
108        source.read( channel.raw_data(), channel.element_size(), channel.size() );
109        return true;
110}
111
112bool nv::nmd_loader::load_channel_set( stream& source, data_channel_set* channel_set, const nmd_element_header& e )
113{
114        data_channel_set_creator kaccess( channel_set );
115        for ( uint32 c = 0; c < e.children; ++c )
116        {
117                load_channel( source, channel_set );
118        }
[425]119        data_channel_set_creator access( channel_set );
120        access.set_name( e.name );
121        access.set_parent_id( e.parent_id );
122        access.set_transform( e.transform );
[420]123        return true;
124}
125
[291]126mesh_nodes_data* nv::nmd_loader::release_mesh_nodes_data( size_t )
[283]127{
[287]128        if ( m_node_data )
[283]129        {
[287]130                mesh_nodes_data* result = m_node_data;
131                m_node_data = nullptr;
132                return result;
[283]133        }
[287]134        return nullptr;
[283]135}
136
[292]137// ----------------------------------------------------------------
138// nmd format dump
139// HACK : TEMPORARY - will go to it's own file, probably nmd_io
[420]140
[423]141void nv::nmd_dump_header( stream& stream_out, uint32 elements, uint64 name )
[292]142{
[423]143        nmd_header header;
144        header.id = four_cc<'n', 'm', 'f', '1'>::value;
145        header.elements = elements; // +1 string array
146        header.name = name;
147        header.version = 1;
148        header.attributes = 0;
149        stream_out.write( &header, sizeof( header ), 1 );
150}
151
[427]152void nv::nmd_dump_element( stream& stream_out, const data_channel_set& data, nmd_type type )
[423]153{
154        uint32 size = 0;
[427]155        for ( auto& chan : data )
[423]156        {
157                size += sizeof( nmd_channel_header );
158                size += chan.raw_size();
159        }
160
161        nmd_element_header eheader;
[427]162        eheader.type       = type;
163        eheader.children   = static_cast<uint16>( data.size() );
[423]164        eheader.size       = size;
[427]165        eheader.name       = data.get_name();
166        eheader.transform  = data.get_transform();
167        eheader.parent_id  = data.get_parent_id();
[423]168        eheader.attributes = 0;
169        stream_out.write( &eheader, sizeof( eheader ), 1 );
[427]170        for ( auto& channel : data )
171        {
172                nmd_channel_header cheader;
173                cheader.format = channel.descriptor();
174                cheader.count = channel.size();
175                stream_out.write( &cheader, sizeof( cheader ), 1 );
176                stream_out.write( channel.raw_data(), channel.element_size(), channel.size() );
177        }
[423]178}
179
180void nv::nmd_dump_nodes( stream& stream_out, const mesh_nodes_data& nodes )
181{
[292]182        uint32 total = sizeof( nmd_animation_header );
[427]183        for ( auto node : nodes )
[292]184        {
[420]185                total += sizeof( nmd_element_header );
[427]186                for ( uint32 c = 0; c < node->size(); ++c )
187                {
188                        total += sizeof( nmd_channel_header );
189                        total += node->get_channel( c )->raw_size();
190                }
[292]191        }
192
193        nmd_element_header header;
[423]194        header.type = nmd_type::ANIMATION;
[428]195        header.children = static_cast<uint16>( nodes.size() );
[423]196        header.size = total;
197        header.name = nodes.get_name();
198        header.transform = mat4();
199        header.parent_id = -1;
[420]200        header.attributes = 0;
201
[292]202        stream_out.write( &header, sizeof( header ), 1 );
203
204        nmd_animation_header aheader;
[470]205        aheader.frame_rate  = nodes.get_fps();
206        aheader.frame_count = nodes.get_frame_count();
[423]207        aheader.flat = nodes.is_flat();
[292]208        stream_out.write( &aheader, sizeof( aheader ), 1 );
209
[427]210        for ( auto node : nodes )
[292]211        {
[427]212                nmd_dump_element( stream_out, *node, nv::nmd_type::NODE );
[292]213        }
214}
215
[423]216void nv::nmd_dump_strings( stream& stream_out, const string_table& strings )
[292]217{
[423]218        nmd_element_header sheader;
219        sheader.type       = nv::nmd_type::STRINGS;
220        sheader.children   = 0;
221        sheader.size       = strings.dump_size();
[431]222        sheader.name       = shash64();
[423]223        sheader.parent_id  = -1;
224    sheader.attributes = 0;
225        stream_out.write( &sheader, sizeof( sheader ), 1 );
226        strings.dump( stream_out );
227}
228
[480]229void nv::nmd_dump( stream& stream_out, array_view< data_channel_set* > meshes, const mesh_nodes_data* nodes, const string_table* strings /*= nullptr*/, uint64 name /*= 0 */ )
[423]230{
231        uint32 elements = ( strings ? 1 : 0 ) // +1 string array
[480]232                + meshes.size() // meshes
233                + ( nodes && nodes->size() > 0 ? 1 : 0 ); // nodes
[423]234        nmd_dump_header( stream_out, elements, name );
235
[480]236        for ( uint32 i = 0; i < meshes.size(); ++i )
[292]237        {
[480]238                NV_ASSERT( meshes[i], "mesh is null!" );
239                nmd_dump_element( stream_out, *meshes[i], nv::nmd_type::MESH );
[292]240        }
241
[480]242        if ( nodes && nodes->size() > 0 )
[292]243        {
[480]244                nmd_dump_nodes( stream_out, *nodes );
[292]245        }
246
[423]247        if ( strings )
[292]248        {
[423]249                nmd_dump_strings( stream_out, *strings );
[292]250        }
251}
[480]252
253void nv::nmd_dump( stream& stream_out, const mesh_nodes_data& animation, const string_table* strings, uint64 name )
254{
255        uint32 elements = ( strings ? 1 : 0 ) // +1 string array
256                + ( animation.size() > 0 ? 1 : 0 ); // nodes
257        nmd_dump_header( stream_out, elements, name );
258
259        if ( animation.size() > 0 )
260        {
261                nmd_dump_nodes( stream_out, animation );
262        }
263
264        if ( strings )
265        {
266                nmd_dump_strings( stream_out, *strings );
267        }
268}
Note: See TracBrowser for help on using the repository browser.