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 | void nv::nmd_loader::reset()
|
---|
53 | {
|
---|
54 | for ( auto mesh : m_meshes ) if ( mesh ) delete mesh;
|
---|
55 | if ( m_node_data ) delete m_node_data;
|
---|
56 | m_meshes.clear();
|
---|
57 |
|
---|
58 | m_node_data = nullptr;
|
---|
59 | }
|
---|
60 |
|
---|
61 | void 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 |
|
---|
67 | nv::nmd_loader::~nmd_loader()
|
---|
68 | {
|
---|
69 | reset();
|
---|
70 | }
|
---|
71 |
|
---|
72 | bool nv::nmd_loader::load_strings( stream& source )
|
---|
73 | {
|
---|
74 | if ( !m_strings ) return true;
|
---|
75 | // TODO: load strings optionally
|
---|
76 | string_table* strings = new string_table( source );
|
---|
77 | m_strings->insert( strings );
|
---|
78 | delete strings;
|
---|
79 | return true;
|
---|
80 | }
|
---|
81 |
|
---|
82 | bool nv::nmd_loader::load_animation( stream& source, const nmd_element_header& e )
|
---|
83 | {
|
---|
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 );
|
---|
87 | m_node_data = new mesh_nodes_data( e.name, animation_header.frame_rate, animation_header.frame_count, animation_header.flat );
|
---|
88 | for ( uint32 i = 0; i < e.children; ++i )
|
---|
89 | {
|
---|
90 | nmd_element_header element_header;
|
---|
91 | source.read( &element_header, sizeof( element_header ), 1 );
|
---|
92 | skip_attributes( source, element_header.attributes );
|
---|
93 | NV_ASSERT( element_header.type == nmd_type::NODE, "NODE expected!" );
|
---|
94 | data_channel_set* set = data_channel_set_creator::create_set( element_header.children );
|
---|
95 | load_channel_set( source, set, element_header );
|
---|
96 | m_node_data->append( set );
|
---|
97 | }
|
---|
98 | m_node_data->initialize();
|
---|
99 | return true;
|
---|
100 | }
|
---|
101 |
|
---|
102 | bool 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 |
|
---|
112 | bool 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 | }
|
---|
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 );
|
---|
123 | return true;
|
---|
124 | }
|
---|
125 |
|
---|
126 | mesh_nodes_data* nv::nmd_loader::release_mesh_nodes_data( size_t )
|
---|
127 | {
|
---|
128 | if ( m_node_data )
|
---|
129 | {
|
---|
130 | mesh_nodes_data* result = m_node_data;
|
---|
131 | m_node_data = nullptr;
|
---|
132 | return result;
|
---|
133 | }
|
---|
134 | return nullptr;
|
---|
135 | }
|
---|
136 |
|
---|
137 | // ----------------------------------------------------------------
|
---|
138 | // nmd format dump
|
---|
139 | // HACK : TEMPORARY - will go to it's own file, probably nmd_io
|
---|
140 |
|
---|
141 | void nv::nmd_dump_header( stream& stream_out, uint32 elements, uint64 name )
|
---|
142 | {
|
---|
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 |
|
---|
152 | void nv::nmd_dump_element( stream& stream_out, const data_channel_set& data, nmd_type type )
|
---|
153 | {
|
---|
154 | uint32 size = 0;
|
---|
155 | for ( auto& chan : data )
|
---|
156 | {
|
---|
157 | size += sizeof( nmd_channel_header );
|
---|
158 | size += chan.raw_size();
|
---|
159 | }
|
---|
160 |
|
---|
161 | nmd_element_header eheader;
|
---|
162 | eheader.type = type;
|
---|
163 | eheader.children = static_cast<uint16>( data.size() );
|
---|
164 | eheader.size = size;
|
---|
165 | eheader.name = data.get_name();
|
---|
166 | eheader.transform = data.get_transform();
|
---|
167 | eheader.parent_id = data.get_parent_id();
|
---|
168 | eheader.attributes = 0;
|
---|
169 | stream_out.write( &eheader, sizeof( eheader ), 1 );
|
---|
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 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | void nv::nmd_dump_nodes( stream& stream_out, const mesh_nodes_data& nodes )
|
---|
181 | {
|
---|
182 | uint32 total = sizeof( nmd_animation_header );
|
---|
183 | for ( auto node : nodes )
|
---|
184 | {
|
---|
185 | total += sizeof( nmd_element_header );
|
---|
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 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | nmd_element_header header;
|
---|
194 | header.type = nmd_type::ANIMATION;
|
---|
195 | header.children = static_cast<uint16>( nodes.size() );
|
---|
196 | header.size = total;
|
---|
197 | header.name = nodes.get_name();
|
---|
198 | header.transform = mat4();
|
---|
199 | header.parent_id = -1;
|
---|
200 | header.attributes = 0;
|
---|
201 |
|
---|
202 | stream_out.write( &header, sizeof( header ), 1 );
|
---|
203 |
|
---|
204 | nmd_animation_header aheader;
|
---|
205 | aheader.frame_rate = nodes.get_fps();
|
---|
206 | aheader.frame_count = nodes.get_frame_count();
|
---|
207 | aheader.flat = nodes.is_flat();
|
---|
208 | stream_out.write( &aheader, sizeof( aheader ), 1 );
|
---|
209 |
|
---|
210 | for ( auto node : nodes )
|
---|
211 | {
|
---|
212 | nmd_dump_element( stream_out, *node, nv::nmd_type::NODE );
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | void nv::nmd_dump_strings( stream& stream_out, const string_table& strings )
|
---|
217 | {
|
---|
218 | nmd_element_header sheader;
|
---|
219 | sheader.type = nv::nmd_type::STRINGS;
|
---|
220 | sheader.children = 0;
|
---|
221 | sheader.size = strings.dump_size();
|
---|
222 | sheader.name = shash64();
|
---|
223 | sheader.parent_id = -1;
|
---|
224 | sheader.attributes = 0;
|
---|
225 | stream_out.write( &sheader, sizeof( sheader ), 1 );
|
---|
226 | strings.dump( stream_out );
|
---|
227 | }
|
---|
228 |
|
---|
229 | void 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 */ )
|
---|
230 | {
|
---|
231 | uint32 elements = ( strings ? 1 : 0 ) // +1 string array
|
---|
232 | + meshes.size() // meshes
|
---|
233 | + ( nodes && nodes->size() > 0 ? 1 : 0 ); // nodes
|
---|
234 | nmd_dump_header( stream_out, elements, name );
|
---|
235 |
|
---|
236 | for ( uint32 i = 0; i < meshes.size(); ++i )
|
---|
237 | {
|
---|
238 | NV_ASSERT( meshes[i], "mesh is null!" );
|
---|
239 | nmd_dump_element( stream_out, *meshes[i], nv::nmd_type::MESH );
|
---|
240 | }
|
---|
241 |
|
---|
242 | if ( nodes && nodes->size() > 0 )
|
---|
243 | {
|
---|
244 | nmd_dump_nodes( stream_out, *nodes );
|
---|
245 | }
|
---|
246 |
|
---|
247 | if ( strings )
|
---|
248 | {
|
---|
249 | nmd_dump_strings( stream_out, *strings );
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | void 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 | }
|
---|