1 | // Copyright (C) 2014-2017 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/ndfm_loader.hh"
|
---|
8 | #include "nv/interface/data_channel_access.hh"
|
---|
9 | #include "nv/stl/string.hh"
|
---|
10 |
|
---|
11 | using namespace nv;
|
---|
12 |
|
---|
13 | #if 0
|
---|
14 |
|
---|
15 | bool nv::ndfm_loader::load( stream& source )
|
---|
16 | {
|
---|
17 | // TODO: proper error handling
|
---|
18 | reset();
|
---|
19 | ndf_header header;
|
---|
20 | source.read( &header, sizeof( header ), 1 );
|
---|
21 |
|
---|
22 | ndf_element root_header;
|
---|
23 | source.read( &root_header, sizeof( root_header ), 1 );
|
---|
24 | skip_attributes( source, root_header.attributes );
|
---|
25 | for ( uint32 i = 0; i < root_header.children; ++i )
|
---|
26 | {
|
---|
27 | ndf_element element_header;
|
---|
28 | source.read( &element_header, sizeof( element_header ), 1 );
|
---|
29 | skip_attributes( source, element_header.attributes );
|
---|
30 | if ( element_header.type == "mesh"_sh64 )
|
---|
31 | load_mesh( source, element_header );
|
---|
32 | else if ( element_header.type == "animation"_sh64 )
|
---|
33 | load_animation( source, element_header );
|
---|
34 | else if ( element_header.type == "strings"_sh64 )
|
---|
35 | load_strings( source );
|
---|
36 | else NV_ASSERT( false, "UNKNOWN NMD ELEMENT!" );
|
---|
37 | }
|
---|
38 | return true;
|
---|
39 | }
|
---|
40 |
|
---|
41 | bool nv::ndfm_loader::load_mesh( stream& source, const ndf_element& e )
|
---|
42 | {
|
---|
43 | data_channel_set* mesh = data_channel_set_creator::create_set( e.children );
|
---|
44 | load_channel_set( source, mesh, e );
|
---|
45 | m_meshes.push_back( mesh );
|
---|
46 | return true;
|
---|
47 | }
|
---|
48 |
|
---|
49 | data_channel_set* nv::ndfm_loader::release_mesh_data( uint32 index )
|
---|
50 | {
|
---|
51 | data_channel_set* result = m_meshes[index];
|
---|
52 | m_meshes[index] = nullptr;
|
---|
53 | return result;
|
---|
54 | }
|
---|
55 |
|
---|
56 | void nv::ndfm_loader::reset()
|
---|
57 | {
|
---|
58 | for ( auto mesh : m_meshes ) if ( mesh ) delete mesh;
|
---|
59 | if ( m_node_data ) delete m_node_data;
|
---|
60 | m_meshes.clear();
|
---|
61 |
|
---|
62 | m_node_data = nullptr;
|
---|
63 | }
|
---|
64 |
|
---|
65 | void nv::ndfm_loader::skip_attributes( stream& source, uint32 count )
|
---|
66 | {
|
---|
67 | if ( count == 0 ) return;
|
---|
68 | source.seek( count * sizeof( ndf_attribute ), origin::CUR );
|
---|
69 | }
|
---|
70 |
|
---|
71 | nv::ndfm_loader::~ndfm_loader()
|
---|
72 | {
|
---|
73 | reset();
|
---|
74 | }
|
---|
75 |
|
---|
76 | bool nv::ndfm_loader::load_strings( stream& source )
|
---|
77 | {
|
---|
78 | if ( !m_strings ) return true;
|
---|
79 | // TODO: load strings optionally
|
---|
80 | string_table* strings = new string_table( source );
|
---|
81 | m_strings->insert( strings );
|
---|
82 | delete strings;
|
---|
83 | return true;
|
---|
84 | }
|
---|
85 |
|
---|
86 | bool nv::ndfm_loader::load_animation( stream& source, const ndf_element& e )
|
---|
87 | {
|
---|
88 | NV_ASSERT( m_node_data == nullptr, "MULTIPLE NODE ENTRIES!" );
|
---|
89 | ndfm_animation_data animation_header;
|
---|
90 | source.read( &animation_header, sizeof( animation_header ), 1 );
|
---|
91 | m_node_data = new mesh_nodes_data( e.name, animation_header.frame_rate, animation_header.frame_count, animation_header.flat );
|
---|
92 | for ( uint32 i = 0; i < e.children; ++i )
|
---|
93 | {
|
---|
94 | ndf_element element_header;
|
---|
95 | source.read( &element_header, sizeof( element_header ), 1 );
|
---|
96 | skip_attributes( source, element_header.attributes );
|
---|
97 | NV_ASSERT( element_header.type == "node"_sh64, "NODE expected!" );
|
---|
98 | data_channel_set* set = data_channel_set_creator::create_set( element_header.children );
|
---|
99 | load_channel_set( source, set, element_header );
|
---|
100 | m_node_data->append( set );
|
---|
101 | }
|
---|
102 | m_node_data->initialize();
|
---|
103 | return true;
|
---|
104 | }
|
---|
105 |
|
---|
106 | bool nv::ndfm_loader::load_channel( stream& source, data_channel_set* channel_set )
|
---|
107 | {
|
---|
108 | data_channel_set_creator kaccess( channel_set );
|
---|
109 | ndfm_channel_header cheader;
|
---|
110 | source.read( &cheader, sizeof( cheader ), 1 );
|
---|
111 | raw_data_channel_access channel( kaccess.add_channel( cheader.format, cheader.count ) );
|
---|
112 | source.read( channel.raw_data(), channel.element_size(), channel.size() );
|
---|
113 | return true;
|
---|
114 | }
|
---|
115 |
|
---|
116 | bool nv::ndfm_loader::load_channel_set( stream& source, data_channel_set* channel_set, const ndf_element& e )
|
---|
117 | {
|
---|
118 | ndfm_node_data nheader;
|
---|
119 | source.read( &nheader, sizeof( nheader ), 1 );
|
---|
120 |
|
---|
121 | data_channel_set_creator kaccess( channel_set );
|
---|
122 | for ( uint32 c = 0; c < e.children; ++c )
|
---|
123 | {
|
---|
124 | load_channel( source, channel_set );
|
---|
125 | }
|
---|
126 | data_channel_set_creator access( channel_set );
|
---|
127 | access.set_name( e.name );
|
---|
128 | access.set_parent_id( nheader.parent_id );
|
---|
129 | access.set_transform( nheader.transform );
|
---|
130 | return true;
|
---|
131 | }
|
---|
132 |
|
---|
133 | mesh_nodes_data* nv::ndfm_loader::release_mesh_nodes_data( uint32 )
|
---|
134 | {
|
---|
135 | mesh_nodes_data* result = m_node_data;
|
---|
136 | m_node_data = nullptr;
|
---|
137 | return result;
|
---|
138 | }
|
---|
139 |
|
---|
140 | data_node_list* nv::ndfm_loader::release_data_node_list( uint32 /*= 0 */ )
|
---|
141 | {
|
---|
142 | if ( !m_node_data ) return nullptr;
|
---|
143 | data_node_list* result = new data_node_list( m_node_data->get_name() );
|
---|
144 | for ( auto node : *m_node_data )
|
---|
145 | result->append( node->get_info() );
|
---|
146 | delete m_node_data;
|
---|
147 | m_node_data = nullptr;
|
---|
148 | return result;
|
---|
149 | }
|
---|
150 |
|
---|
151 | bool nv::ndfm_loader::is_animated( uint32 /*= 0 */ )
|
---|
152 | {
|
---|
153 | if ( !m_node_data ) return false;
|
---|
154 | return m_node_data->is_animated();
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 |
|
---|
159 | // ----------------------------------------------------------------
|
---|
160 | // nmd format dump
|
---|
161 | // HACK : TEMPORARY - will go to it's own file, probably nmd_io
|
---|
162 |
|
---|
163 |
|
---|
164 | void nv::ndfm_dump_header( stream& stream_out, uint16 elements, shash64 name )
|
---|
165 | {
|
---|
166 | ndf_header header;
|
---|
167 | header.id = four_cc<'n', 'd', 'f', '1'>::value;
|
---|
168 | header.version = 1;
|
---|
169 | stream_out.write( &header, sizeof( header ), 1 );
|
---|
170 |
|
---|
171 | ndf_element eheader;
|
---|
172 | eheader.name = name;
|
---|
173 | eheader.type = "model"_sh64;
|
---|
174 | eheader.flags = 0;
|
---|
175 | eheader.size = 0;
|
---|
176 | eheader.attributes = 0;
|
---|
177 | eheader.children = elements;
|
---|
178 | stream_out.write( &eheader, sizeof( eheader ), 1 );
|
---|
179 | }
|
---|
180 |
|
---|
181 | void nv::ndfm_dump_element( stream& stream_out, const data_channel_set& data, shash64 type )
|
---|
182 | {
|
---|
183 | uint32 size = sizeof( ndfm_node_data );
|
---|
184 | for ( auto& chan : data )
|
---|
185 | {
|
---|
186 | size += sizeof( ndfm_channel_header );
|
---|
187 | size += chan.raw_size();
|
---|
188 | }
|
---|
189 |
|
---|
190 | ndf_element eheader;
|
---|
191 | eheader.name = data.get_name();
|
---|
192 | eheader.type = type;
|
---|
193 | eheader.flags = 0;
|
---|
194 | eheader.size = size;
|
---|
195 | eheader.attributes = 0;
|
---|
196 | eheader.children = static_cast<uint16>( data.size() );
|
---|
197 | stream_out.write( &eheader, sizeof( eheader ), 1 );
|
---|
198 |
|
---|
199 | ndfm_node_data nheader;
|
---|
200 | int confirm_that_not_needed;
|
---|
201 | // nheader.transform = data.get_transform();
|
---|
202 | nheader.parent_id = data.get_parent_id();
|
---|
203 | stream_out.write( &nheader, sizeof( nheader ), 1 );
|
---|
204 | for ( auto& channel : data )
|
---|
205 | {
|
---|
206 | ndfm_channel_header cheader;
|
---|
207 | cheader.format = channel.descriptor();
|
---|
208 | cheader.count = channel.size();
|
---|
209 | stream_out.write( &cheader, sizeof( cheader ), 1 );
|
---|
210 | stream_out.write( channel.raw_data(), channel.element_size(), channel.size() );
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | void nv::ndfm_dump_animation( stream& stream_out, const mesh_nodes_data& nodes )
|
---|
215 | {
|
---|
216 | uint32 total = sizeof( ndfm_animation_data );
|
---|
217 | for ( auto node : nodes )
|
---|
218 | {
|
---|
219 | total += sizeof( ndf_element ) + sizeof( ndfm_node_data );
|
---|
220 | for ( uint32 c = 0; c < node->size(); ++c )
|
---|
221 | {
|
---|
222 | total += sizeof( ndfm_channel_header );
|
---|
223 | total += node->get_channel( c )->raw_size();
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | ndf_element eheader;
|
---|
228 | eheader.name = nodes.get_name();
|
---|
229 | eheader.type = "animation"_sh64;
|
---|
230 | eheader.flags = 0;
|
---|
231 | eheader.size = total;
|
---|
232 | eheader.attributes = 0;
|
---|
233 | eheader.children = static_cast<uint16>( nodes.size() );
|
---|
234 | stream_out.write( &eheader, sizeof( eheader ), 1 );
|
---|
235 |
|
---|
236 | ndfm_animation_data aheader;
|
---|
237 | aheader.frame_rate = nodes.get_fps();
|
---|
238 | aheader.frame_count = nodes.get_frame_count();
|
---|
239 | stream_out.write( &aheader, sizeof( aheader ), 1 );
|
---|
240 |
|
---|
241 | for ( auto node : nodes )
|
---|
242 | {
|
---|
243 | ndfm_dump_element( stream_out, *node, "node"_sh64 );
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | void nv::ndfm_dump_bones( stream& stream_out, const data_node_list& bones )
|
---|
248 | {
|
---|
249 | uint32 total = sizeof( ndfm_animation_data );
|
---|
250 | for ( auto bone : bones )
|
---|
251 | {
|
---|
252 | total += sizeof( ndf_element ) + sizeof( ndfm_node_data );
|
---|
253 | }
|
---|
254 |
|
---|
255 | ndf_element eheader;
|
---|
256 | eheader.name = bones.get_name();
|
---|
257 | eheader.type = "animation"_sh64;
|
---|
258 | eheader.flags = 0;
|
---|
259 | eheader.size = total;
|
---|
260 | eheader.attributes = 0;
|
---|
261 | eheader.children = static_cast<uint16>( bones.size() );
|
---|
262 | stream_out.write( &eheader, sizeof( eheader ), 1 );
|
---|
263 |
|
---|
264 | for ( auto bone : bones )
|
---|
265 | {
|
---|
266 | uint32 size = sizeof( ndfm_node_data );
|
---|
267 | ndf_element eheader;
|
---|
268 | eheader.name = bone.name;
|
---|
269 | eheader.type = "bone"_sh64;
|
---|
270 | eheader.flags = 0;
|
---|
271 | eheader.size = size;
|
---|
272 | eheader.attributes = 0;
|
---|
273 | eheader.children = 0;
|
---|
274 | stream_out.write( &eheader, sizeof( eheader ), 1 );
|
---|
275 |
|
---|
276 | ndfm_node_data nheader;
|
---|
277 | nheader.transform = bone.transform;
|
---|
278 | nheader.parent_id = bone.parent_id;
|
---|
279 | stream_out.write( &nheader, sizeof( nheader ), 1 );
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | void nv::ndfm_dump_strings( stream& stream_out, const string_table& strings )
|
---|
284 | {
|
---|
285 | ndf_element sheader;
|
---|
286 | sheader.name = shash64();
|
---|
287 | sheader.type = "strings"_sh64;
|
---|
288 | sheader.flags = 0;
|
---|
289 | sheader.size = strings.dump_size();
|
---|
290 | sheader.children = 0;
|
---|
291 | sheader.attributes = 0;
|
---|
292 | stream_out.write( &sheader, sizeof( sheader ), 1 );
|
---|
293 | strings.dump( stream_out );
|
---|
294 | }
|
---|
295 |
|
---|
296 | // void nv::ndfm_dump( stream& stream_out, const mesh_data_pack* model, const string_table* strings, shash64 name )
|
---|
297 | // {
|
---|
298 | // uint16 elements = ( strings ? 1 : 0 ) // +1 string array
|
---|
299 | // + uint16( model->get_count() ) // meshes
|
---|
300 | // + ( model->get_node_count() > 0 ? 1 : 0 ); // nodes
|
---|
301 | // ndfm_dump_header( stream_out, elements, name );
|
---|
302 | //
|
---|
303 | // for ( uint32 i = 0; i < model->get_count(); ++i )
|
---|
304 | // {
|
---|
305 | // ndfm_dump_element( stream_out, *model->get_mesh( i ), "mesh"_sh64 );
|
---|
306 | // }
|
---|
307 | //
|
---|
308 | // if ( model->get_node_count() > 0 )
|
---|
309 | // {
|
---|
310 | // ndfm_dump_nodes( stream_out, *model->get_nodes() );
|
---|
311 | // }
|
---|
312 | //
|
---|
313 | // if ( strings )
|
---|
314 | // {
|
---|
315 | // ndfm_dump_strings( stream_out, *strings );
|
---|
316 | // }
|
---|
317 | // }
|
---|
318 |
|
---|
319 | #endif
|
---|