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/stl/string.hh"
|
---|
9 | #include "nv/interface/data_channel_access.hh"
|
---|
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 );
|
---|
19 | skip_attributes( source, root_header.attributes );
|
---|
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 );
|
---|
24 | skip_attributes( source, element_header.attributes );
|
---|
25 | switch ( element_header.type )
|
---|
26 | {
|
---|
27 | case nmd_type::MESH : load_mesh( source, element_header ); break;
|
---|
28 | case nmd_type::ANIMATION : load_animation( source, element_header ); break;
|
---|
29 | case nmd_type::STRINGS : load_strings( source ); break;
|
---|
30 | default: NV_ASSERT( false, "UNKNOWN NMD ELEMENT!" ); break;
|
---|
31 | }
|
---|
32 | }
|
---|
33 | return true;
|
---|
34 | }
|
---|
35 |
|
---|
36 | bool nv::nmd_loader::load_mesh( stream& source, const nmd_element_header& e )
|
---|
37 | {
|
---|
38 | data_channel_set* mesh = data_channel_set_creator::create_set( e.children );
|
---|
39 | load_channel_set( source, mesh, e );
|
---|
40 | // m_mesh_names.push_back( e.name );
|
---|
41 | m_meshes.push_back( mesh );
|
---|
42 | return true;
|
---|
43 | }
|
---|
44 |
|
---|
45 | data_channel_set* nv::nmd_loader::release_mesh_data( size_t index )
|
---|
46 | {
|
---|
47 | data_channel_set* result = m_meshes[ index ];
|
---|
48 | m_meshes[ index ] = nullptr;
|
---|
49 | return result;
|
---|
50 | }
|
---|
51 |
|
---|
52 | mesh_data_pack* nv::nmd_loader::release_mesh_data_pack()
|
---|
53 | {
|
---|
54 | uint32 size = m_meshes.size();
|
---|
55 | data_channel_set* meshes = data_channel_set_creator::create_set_array( size, 0 );
|
---|
56 | for ( uint32 i = 0; i < size; ++i )
|
---|
57 | {
|
---|
58 | meshes[i] = move( *m_meshes[i] );
|
---|
59 | delete m_meshes[i];
|
---|
60 | }
|
---|
61 | m_meshes.clear();
|
---|
62 | return new mesh_data_pack( size, meshes, release_mesh_nodes_data() );
|
---|
63 | }
|
---|
64 |
|
---|
65 | void nv::nmd_loader::reset()
|
---|
66 | {
|
---|
67 | for ( auto mesh : m_meshes ) if ( mesh ) delete mesh;
|
---|
68 | if ( m_node_data ) delete m_node_data;
|
---|
69 | m_meshes.clear();
|
---|
70 |
|
---|
71 | m_node_data = nullptr;
|
---|
72 | }
|
---|
73 |
|
---|
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 |
|
---|
80 | nv::nmd_loader::~nmd_loader()
|
---|
81 | {
|
---|
82 | reset();
|
---|
83 | }
|
---|
84 |
|
---|
85 | bool nv::nmd_loader::load_strings( stream& source )
|
---|
86 | {
|
---|
87 | if ( !m_strings ) return true;
|
---|
88 | // TODO: load strings optionally
|
---|
89 | string_table* strings = new string_table( source );
|
---|
90 | m_strings->insert( strings );
|
---|
91 | delete strings;
|
---|
92 | return true;
|
---|
93 | }
|
---|
94 |
|
---|
95 | bool nv::nmd_loader::load_animation( stream& source, const nmd_element_header& e )
|
---|
96 | {
|
---|
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 );
|
---|
100 | m_node_data = new mesh_nodes_data( e.name, animation_header.frame_rate, animation_header.duration, animation_header.flat );
|
---|
101 | for ( uint32 i = 0; i < e.children; ++i )
|
---|
102 | {
|
---|
103 | nmd_element_header element_header;
|
---|
104 | source.read( &element_header, sizeof( element_header ), 1 );
|
---|
105 | skip_attributes( source, element_header.attributes );
|
---|
106 | NV_ASSERT( element_header.type == nmd_type::NODE, "NODE expected!" );
|
---|
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 );
|
---|
110 | }
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 |
|
---|
114 | bool 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 |
|
---|
124 | bool 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 | }
|
---|
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 );
|
---|
135 | return true;
|
---|
136 | }
|
---|
137 |
|
---|
138 | mesh_nodes_data* nv::nmd_loader::release_mesh_nodes_data( size_t )
|
---|
139 | {
|
---|
140 | if ( m_node_data )
|
---|
141 | {
|
---|
142 | mesh_nodes_data* result = m_node_data;
|
---|
143 | m_node_data = nullptr;
|
---|
144 | return result;
|
---|
145 | }
|
---|
146 | return nullptr;
|
---|
147 | }
|
---|
148 |
|
---|
149 | // ----------------------------------------------------------------
|
---|
150 | // nmd format dump
|
---|
151 | // HACK : TEMPORARY - will go to it's own file, probably nmd_io
|
---|
152 |
|
---|
153 | void nv::nmd_dump_header( stream& stream_out, uint32 elements, uint64 name )
|
---|
154 | {
|
---|
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 |
|
---|
164 | void nv::nmd_dump_element( stream& stream_out, const data_channel_set& data, nmd_type type )
|
---|
165 | {
|
---|
166 | uint32 size = 0;
|
---|
167 | for ( auto& chan : data )
|
---|
168 | {
|
---|
169 | size += sizeof( nmd_channel_header );
|
---|
170 | size += chan.raw_size();
|
---|
171 | }
|
---|
172 |
|
---|
173 | nmd_element_header eheader;
|
---|
174 | eheader.type = type;
|
---|
175 | eheader.children = static_cast<uint16>( data.size() );
|
---|
176 | eheader.size = size;
|
---|
177 | eheader.name = data.get_name();
|
---|
178 | eheader.transform = data.get_transform();
|
---|
179 | eheader.parent_id = data.get_parent_id();
|
---|
180 | eheader.attributes = 0;
|
---|
181 | stream_out.write( &eheader, sizeof( eheader ), 1 );
|
---|
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 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | void nv::nmd_dump_nodes( stream& stream_out, const mesh_nodes_data& nodes )
|
---|
193 | {
|
---|
194 | uint32 total = sizeof( nmd_animation_header );
|
---|
195 | for ( auto node : nodes )
|
---|
196 | {
|
---|
197 | total += sizeof( nmd_element_header );
|
---|
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 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | nmd_element_header header;
|
---|
206 | header.type = nmd_type::ANIMATION;
|
---|
207 | header.children = static_cast<uint16>( nodes.size() );
|
---|
208 | header.size = total;
|
---|
209 | header.name = nodes.get_name();
|
---|
210 | header.transform = mat4();
|
---|
211 | header.parent_id = -1;
|
---|
212 | header.attributes = 0;
|
---|
213 |
|
---|
214 | stream_out.write( &header, sizeof( header ), 1 );
|
---|
215 |
|
---|
216 | nmd_animation_header aheader;
|
---|
217 | aheader.frame_rate = nodes.get_frame_rate();
|
---|
218 | aheader.duration = nodes.get_duration();
|
---|
219 | aheader.flat = nodes.is_flat();
|
---|
220 | stream_out.write( &aheader, sizeof( aheader ), 1 );
|
---|
221 |
|
---|
222 | for ( auto node : nodes )
|
---|
223 | {
|
---|
224 | nmd_dump_element( stream_out, *node, nv::nmd_type::NODE );
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | void nv::nmd_dump_strings( stream& stream_out, const string_table& strings )
|
---|
229 | {
|
---|
230 | nmd_element_header sheader;
|
---|
231 | sheader.type = nv::nmd_type::STRINGS;
|
---|
232 | sheader.children = 0;
|
---|
233 | sheader.size = strings.dump_size();
|
---|
234 | sheader.name = shash64();
|
---|
235 | sheader.parent_id = -1;
|
---|
236 | sheader.attributes = 0;
|
---|
237 | stream_out.write( &sheader, sizeof( sheader ), 1 );
|
---|
238 | strings.dump( stream_out );
|
---|
239 | }
|
---|
240 |
|
---|
241 | void 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 )
|
---|
249 | {
|
---|
250 | nmd_dump_element( stream_out, *model->get_mesh( i ), nv::nmd_type::MESH );
|
---|
251 | }
|
---|
252 |
|
---|
253 | if ( model->get_node_count() > 0 )
|
---|
254 | {
|
---|
255 | nmd_dump_nodes( stream_out, *model->get_nodes() );
|
---|
256 | }
|
---|
257 |
|
---|
258 | if ( strings )
|
---|
259 | {
|
---|
260 | nmd_dump_strings( stream_out, *strings );
|
---|
261 | }
|
---|
262 | }
|
---|