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

Last change on this file since 427 was 427, checked in by epyon, 10 years ago
  • cleanup of mesh_node_data
File size: 7.8 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"
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
12using namespace nv;
13
14bool 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]37bool 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]46data_channel_set* nv::nmd_loader::release_mesh_data( size_t index )
[283]47{
[416]48        data_channel_set* result = m_meshes[ index ];
[283]49        m_meshes[ index ] = nullptr;
50        return result;
51}
52
[287]53mesh_data_pack* nv::nmd_loader::release_mesh_data_pack()
54{
55        uint32 size = m_meshes.size();
[424]56        data_channel_set* meshes = data_channel_set_creator::create_set_array( size, 0 );
[287]57        for ( uint32 i = 0; i < size; ++i )
58        {
[421]59                meshes[i] = move( *m_meshes[i] );
[287]60                delete m_meshes[i];
61        }
62        m_meshes.clear();
63        return new mesh_data_pack( size, meshes, release_mesh_nodes_data() );
64}
65
[283]66void nv::nmd_loader::reset()
67{
68        for ( auto mesh : m_meshes ) if ( mesh ) delete mesh;
[287]69        if ( m_node_data ) delete m_node_data;
[283]70        m_meshes.clear();
[287]71
72        m_node_data  = nullptr;
[283]73}
74
[420]75void nv::nmd_loader::skip_attributes( stream& source, uint32 count )
76{
77        if ( count == 0 ) return;
78        source.seek( count * sizeof( nmd_attribute ), origin::CUR );
79}
80
[283]81nv::nmd_loader::~nmd_loader()
82{
83        reset();
84}
85
[423]86bool nv::nmd_loader::load_strings( stream& source )
[283]87{
[425]88        if ( !m_strings ) return true;
[423]89        // TODO: load strings optionally
[425]90        string_table* strings = new string_table( source );
91        m_strings->insert( strings );
92        delete strings;
[283]93        return true;
94}
95
[284]96bool nv::nmd_loader::load_animation( stream& source, const nmd_element_header& e )
[283]97{
[287]98        NV_ASSERT( m_node_data == nullptr, "MULTIPLE NODE ENTRIES!" );
99        nmd_animation_header animation_header;
100        source.read( &animation_header, sizeof( animation_header ), 1 );
[427]101        m_node_data = new mesh_nodes_data( e.name, animation_header.frame_rate, animation_header.duration, animation_header.flat );
[284]102        for ( uint32 i = 0; i < e.children; ++i )
[283]103        {
104                nmd_element_header element_header;
105                source.read( &element_header, sizeof( element_header ), 1 );
[420]106                skip_attributes( source, element_header.attributes );
[287]107                NV_ASSERT( element_header.type == nmd_type::NODE, "NODE expected!" );
[427]108                data_channel_set* set = data_channel_set_creator::create_set( element_header.children );
109                load_channel_set( source, set, element_header );
110                m_node_data->push_back( set );
[283]111        }
112        return true;
113}
114
[420]115bool 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
125bool 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]139mesh_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]154void 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]165void 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
193void 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;
208        header.children = static_cast<uint16>( nodes.get_count() );
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;
[423]218        aheader.frame_rate = nodes.get_frame_rate();
219        aheader.duration = nodes.get_duration();
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]229void 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();
235        sheader.name       = 0;
236        sheader.parent_id  = -1;
237    sheader.attributes = 0;
238        stream_out.write( &sheader, sizeof( sheader ), 1 );
239        strings.dump( stream_out );
240}
241
242void 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}
Note: See TracBrowser for help on using the repository browser.