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

Last change on this file since 428 was 428, checked in by epyon, 10 years ago
  • renaming mesh_nodes_data members to more compliant
  • moved string_table to stl
File size: 7.8 KB
Line 
1// Copyright (C) 2014-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#include "nv/formats/nmd_loader.hh"
8#include "nv/io/std_stream.hh"
9#include "nv/stl/string.hh"
10#include "nv/interface/data_channel_access.hh"
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 );
20        skip_attributes( source, root_header.attributes );
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 );
25                skip_attributes( source, element_header.attributes );
26                switch ( element_header.type )
27                {
28                case nmd_type::MESH           : load_mesh( source, element_header ); break;
29                case nmd_type::ANIMATION      : load_animation( source, element_header ); break;
30                case nmd_type::STRINGS        : load_strings( source ); break;
31                default: NV_ASSERT( false, "UNKNOWN NMD ELEMENT!" ); break;
32                }
33        }
34        return true;
35}
36
37bool nv::nmd_loader::load_mesh( stream& source, const nmd_element_header& e )
38{
39        data_channel_set* mesh = data_channel_set_creator::create_set( e.children );
40        load_channel_set( source, mesh, e );
41//      m_mesh_names.push_back( e.name );
42        m_meshes.push_back( mesh );
43        return true;
44}
45
46data_channel_set* nv::nmd_loader::release_mesh_data( size_t index )
47{
48        data_channel_set* result = m_meshes[ index ];
49        m_meshes[ index ] = nullptr;
50        return result;
51}
52
53mesh_data_pack* nv::nmd_loader::release_mesh_data_pack()
54{
55        uint32 size = m_meshes.size();
56        data_channel_set* meshes = data_channel_set_creator::create_set_array( size, 0 );
57        for ( uint32 i = 0; i < size; ++i )
58        {
59                meshes[i] = move( *m_meshes[i] );
60                delete m_meshes[i];
61        }
62        m_meshes.clear();
63        return new mesh_data_pack( size, meshes, release_mesh_nodes_data() );
64}
65
66void nv::nmd_loader::reset()
67{
68        for ( auto mesh : m_meshes ) if ( mesh ) delete mesh;
69        if ( m_node_data ) delete m_node_data;
70        m_meshes.clear();
71
72        m_node_data  = nullptr;
73}
74
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
81nv::nmd_loader::~nmd_loader()
82{
83        reset();
84}
85
86bool nv::nmd_loader::load_strings( stream& source )
87{
88        if ( !m_strings ) return true;
89        // TODO: load strings optionally
90        string_table* strings = new string_table( source );
91        m_strings->insert( strings );
92        delete strings;
93        return true;
94}
95
96bool nv::nmd_loader::load_animation( stream& source, const nmd_element_header& e )
97{
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 );
101        m_node_data = new mesh_nodes_data( e.name, animation_header.frame_rate, animation_header.duration, animation_header.flat );
102        for ( uint32 i = 0; i < e.children; ++i )
103        {
104                nmd_element_header element_header;
105                source.read( &element_header, sizeof( element_header ), 1 );
106                skip_attributes( source, element_header.attributes );
107                NV_ASSERT( element_header.type == nmd_type::NODE, "NODE expected!" );
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 );
111        }
112        return true;
113}
114
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        }
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 );
136        return true;
137}
138
139mesh_nodes_data* nv::nmd_loader::release_mesh_nodes_data( size_t )
140{
141        if ( m_node_data )
142        {
143                mesh_nodes_data* result = m_node_data;
144                m_node_data = nullptr;
145                return result;
146        }
147        return nullptr;
148}
149
150// ----------------------------------------------------------------
151// nmd format dump
152// HACK : TEMPORARY - will go to it's own file, probably nmd_io
153
154void nv::nmd_dump_header( stream& stream_out, uint32 elements, uint64 name )
155{
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
165void nv::nmd_dump_element( stream& stream_out, const data_channel_set& data, nmd_type type )
166{
167        uint32 size = 0;
168        for ( auto& chan : data )
169        {
170                size += sizeof( nmd_channel_header );
171                size += chan.raw_size();
172        }
173
174        nmd_element_header eheader;
175        eheader.type       = type;
176        eheader.children   = static_cast<uint16>( data.size() );
177        eheader.size       = size;
178        eheader.name       = data.get_name();
179        eheader.transform  = data.get_transform();
180        eheader.parent_id  = data.get_parent_id();
181        eheader.attributes = 0;
182        stream_out.write( &eheader, sizeof( eheader ), 1 );
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        }
191}
192
193void nv::nmd_dump_nodes( stream& stream_out, const mesh_nodes_data& nodes )
194{
195        uint32 total = sizeof( nmd_animation_header );
196        for ( auto node : nodes )
197        {
198                total += sizeof( nmd_element_header );
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                }
204        }
205
206        nmd_element_header header;
207        header.type = nmd_type::ANIMATION;
208        header.children = static_cast<uint16>( nodes.size() );
209        header.size = total;
210        header.name = nodes.get_name();
211        header.transform = mat4();
212        header.parent_id = -1;
213        header.attributes = 0;
214
215        stream_out.write( &header, sizeof( header ), 1 );
216
217        nmd_animation_header aheader;
218        aheader.frame_rate = nodes.get_frame_rate();
219        aheader.duration = nodes.get_duration();
220        aheader.flat = nodes.is_flat();
221        stream_out.write( &aheader, sizeof( aheader ), 1 );
222
223        for ( auto node : nodes )
224        {
225                nmd_dump_element( stream_out, *node, nv::nmd_type::NODE );
226        }
227}
228
229void nv::nmd_dump_strings( stream& stream_out, const string_table& strings )
230{
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 )
250        {
251                nmd_dump_element( stream_out, *model->get_mesh( i ), nv::nmd_type::MESH );
252        }
253
254        if ( model->get_node_count() > 0 )
255        {
256                nmd_dump_nodes( stream_out, *model->get_nodes() );
257        }
258
259        if ( strings )
260        {
261                nmd_dump_strings( stream_out, *strings );
262        }
263}
Note: See TracBrowser for help on using the repository browser.