1 | // Copyright (C) 2012-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/gfx/mesh_creator.hh"
|
---|
8 |
|
---|
9 | #include "nv/interface/data_channel_access.hh"
|
---|
10 |
|
---|
11 | #include "nv/core/logging.hh"
|
---|
12 |
|
---|
13 | struct nv_key_transform { nv::transform tform; };
|
---|
14 |
|
---|
15 | void nv::mesh_nodes_creator::pre_transform_keys()
|
---|
16 | {
|
---|
17 | if ( m_data->m_flat ) return;
|
---|
18 | merge_keys();
|
---|
19 | uint32 max_frames = 0;
|
---|
20 |
|
---|
21 | nv::vector< sint16 > ids;
|
---|
22 | {
|
---|
23 | // TODO: simplify this shit!
|
---|
24 | // The complexity here is that we cannot pre-transform in any order
|
---|
25 | // as the bones depend on previous bones, but ARE NOT IN ORDER
|
---|
26 | //
|
---|
27 | // Either rewrite this a lot nicer, or sort the bones on creation
|
---|
28 | // by tree-order.
|
---|
29 |
|
---|
30 | ids.reserve( m_data->m_data.size() );
|
---|
31 | {
|
---|
32 | nv::vector< sint16 > ids_next;
|
---|
33 | ids_next.reserve( m_data->m_data.size() );
|
---|
34 | ids_next.push_back( -1 );
|
---|
35 | while ( !ids_next.empty() )
|
---|
36 | {
|
---|
37 | sint16 pid = ids_next.back();
|
---|
38 | ids_next.pop_back();
|
---|
39 | for ( sint16 i = 0; i < sint16(m_data->m_data.size()); ++i )
|
---|
40 | if ( m_data->m_data[i]->get_parent_id() == pid )
|
---|
41 | {
|
---|
42 | sint16* it = nv::find( ids.begin(), ids.end(), i );
|
---|
43 | if ( it == ids.end() )
|
---|
44 | {
|
---|
45 | ids.push_back( i );
|
---|
46 | ids_next.push_back( i );
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | if ( ids.size() != m_data->m_data.size() )
|
---|
53 | {
|
---|
54 | NV_LOG_WARNING( "Bad skeleton!" );
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | NV_LOG_DEBUG( "ID/PID" );
|
---|
59 | for ( auto id : ids )
|
---|
60 | {
|
---|
61 | data_channel_set* keys = m_data->m_data[id];
|
---|
62 | sint16 parent_id = keys->get_parent_id();
|
---|
63 | NV_LOG_DEBUG( "Id : ", id, " PID", parent_id );
|
---|
64 | data_channel_set* pkeys = ( parent_id != -1 ? m_data->m_data[parent_id] : nullptr );
|
---|
65 | size_t count = ( keys ? keys->get_channel_size(0) : 0 );
|
---|
66 | size_t pcount = ( pkeys ? pkeys->get_channel_size(0) : 0 );
|
---|
67 | max_frames = nv::max<uint32>( count, max_frames );
|
---|
68 | if ( pkeys && pkeys->size() > 0 && keys && keys->size() > 0 )
|
---|
69 | {
|
---|
70 | data_channel_access< nv_key_transform > channel_creator( keys, 0 );
|
---|
71 |
|
---|
72 | nv_key_transform* channel = channel_creator.data();
|
---|
73 | const nv_key_transform* pchannel = pkeys->get_channel(0)->data_cast< nv_key_transform >();
|
---|
74 | for ( unsigned n = 0; n < count; ++n )
|
---|
75 | {
|
---|
76 | channel[n].tform = pchannel[ nv::min( n, pcount-1 ) ].tform * channel[n].tform;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | // DAE pre_transform hack
|
---|
82 | if ( m_data->m_frame_rate == 1 )
|
---|
83 | {
|
---|
84 | m_data->m_frame_rate = 32;
|
---|
85 | m_data->m_frame_count = max_frames;
|
---|
86 | }
|
---|
87 |
|
---|
88 | m_data->m_flat = true;
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | void nv::mesh_nodes_creator::merge_keys()
|
---|
93 | {
|
---|
94 | for ( size_t i = 0; i < m_data->size(); ++i )
|
---|
95 | {
|
---|
96 | data_channel_set* old_keys = m_data->m_data[i];
|
---|
97 | if ( old_keys && old_keys->size() > 0 )
|
---|
98 | {
|
---|
99 | size_t chan_count = old_keys->size();
|
---|
100 | if ( chan_count == 1
|
---|
101 | && old_keys->get_channel(0)->descriptor().size() == 1
|
---|
102 | && old_keys->get_channel(0)->descriptor()[0].etype == TRANSFORM ) continue;
|
---|
103 |
|
---|
104 | size_t max_keys = 0;
|
---|
105 | for ( size_t c = 0; c < chan_count; ++c )
|
---|
106 | {
|
---|
107 | max_keys = nv::max( max_keys, old_keys->get_channel(c)->size() );
|
---|
108 | }
|
---|
109 |
|
---|
110 | data_channel_set* new_keys = data_channel_set_creator::create_set( 1 );
|
---|
111 | data_channel_set_creator nk_access( new_keys );
|
---|
112 | nk_access.set_name( old_keys->get_name() );
|
---|
113 | nk_access.set_parent_id( old_keys->get_parent_id() );
|
---|
114 | nk_access.set_transform( old_keys->get_transform() );
|
---|
115 | data_channel_access< nv_key_transform > kt_channel( nk_access.add_channel<nv_key_transform>( max_keys ) );
|
---|
116 |
|
---|
117 | raw_channel_interpolator interpolator( old_keys );
|
---|
118 | data_descriptor final_key = interpolator.get_interpolation_key();
|
---|
119 |
|
---|
120 | for ( unsigned n = 0; n < max_keys; ++n )
|
---|
121 | {
|
---|
122 | float key[ 16 ];
|
---|
123 | float* pkey = key;
|
---|
124 |
|
---|
125 | for ( uint16 c = 0; c < chan_count; ++c )
|
---|
126 | {
|
---|
127 | size_t idx = nv::min( old_keys->get_channel_size(c) - 1, n );
|
---|
128 | pkey += raw_channel_interpolator::get_raw( *old_keys->get_channel(c), idx, pkey );
|
---|
129 | }
|
---|
130 | kt_channel.data()[n].tform = extract_key_raw< nv::transform >( final_key, key );
|
---|
131 | }
|
---|
132 |
|
---|
133 | delete old_keys;
|
---|
134 | m_data->m_data[i] = new_keys;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | void nv::mesh_nodes_creator::transform( float scale, const mat3& r33 )
|
---|
140 | {
|
---|
141 | mat3 ri33 = math::inverse( r33 );
|
---|
142 | mat4 pre_transform ( scale * r33 );
|
---|
143 | mat4 post_transform( 1.f/scale * ri33 );
|
---|
144 |
|
---|
145 | for ( auto node : m_data->m_data )
|
---|
146 | {
|
---|
147 | node->m_transform = pre_transform * node->m_transform * post_transform;
|
---|
148 |
|
---|
149 | for ( size_t c = 0; c < node->size(); ++c )
|
---|
150 | {
|
---|
151 | raw_data_channel_access channel( node, c );
|
---|
152 | size_t key_size = channel.element_size();
|
---|
153 | for ( size_t n = 0; n < channel.size(); ++n )
|
---|
154 | {
|
---|
155 | transform_key_raw( node->get_channel( c )->descriptor(), channel.raw_data() + n * key_size, scale, r33, ri33 );
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | nv::mesh_data_creator::mesh_data_creator( data_channel_set* data ) : m_data( data )
|
---|
162 | {
|
---|
163 | initialize();
|
---|
164 | }
|
---|
165 |
|
---|
166 | void nv::mesh_data_creator::transform( float scale, const mat3& r33 )
|
---|
167 | {
|
---|
168 | vec3 vertex_offset = vec3();
|
---|
169 | mat3 vertex_transform = scale * r33;
|
---|
170 | mat3 normal_transform = r33;
|
---|
171 |
|
---|
172 | for ( uint32 c = 0; c < m_data->size(); ++c )
|
---|
173 | {
|
---|
174 | raw_data_channel_access channel( m_data, c );
|
---|
175 | const data_descriptor& desc = channel.descriptor();
|
---|
176 | uint8* raw_data = channel.raw_data();
|
---|
177 | uint32 vtx_size = desc.element_size();
|
---|
178 | int p_offset = -1;
|
---|
179 | int n_offset = -1;
|
---|
180 | int t_offset = -1;
|
---|
181 | for ( const auto& cslot : desc )
|
---|
182 | switch ( cslot.vslot )
|
---|
183 | {
|
---|
184 | case slot::POSITION : if ( cslot.etype == FLOAT_VECTOR_3 ) p_offset = int( cslot.offset ); break;
|
---|
185 | case slot::NORMAL : if ( cslot.etype == FLOAT_VECTOR_3 ) n_offset = int( cslot.offset ); break;
|
---|
186 | case slot::TANGENT : if ( cslot.etype == FLOAT_VECTOR_4 ) t_offset = int( cslot.offset ); break;
|
---|
187 | default : break;
|
---|
188 | }
|
---|
189 |
|
---|
190 | if ( p_offset != -1 )
|
---|
191 | for ( uint32 i = 0; i < channel.size(); i++)
|
---|
192 | {
|
---|
193 | vec3& p = *reinterpret_cast<vec3*>( raw_data + vtx_size*i + p_offset );
|
---|
194 | p = vertex_transform * p + vertex_offset;
|
---|
195 | }
|
---|
196 |
|
---|
197 | if ( n_offset != -1 )
|
---|
198 | for ( uint32 i = 0; i < channel.size(); i++)
|
---|
199 | {
|
---|
200 | vec3& n = *reinterpret_cast<vec3*>( raw_data + vtx_size*i + n_offset );
|
---|
201 | n = math::normalize( normal_transform * n );
|
---|
202 | }
|
---|
203 | if ( t_offset != -1 )
|
---|
204 | for ( uint32 i = 0; i < channel.size(); i++)
|
---|
205 | {
|
---|
206 | vec4& t = *reinterpret_cast<vec4*>(raw_data + vtx_size*i + t_offset );
|
---|
207 | t = vec4( math::normalize( normal_transform * vec3(t) ), t[3] );
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | struct vertex_g
|
---|
213 | {
|
---|
214 | nv::vec4 tangent;
|
---|
215 | };
|
---|
216 |
|
---|
217 | void nv::mesh_data_creator::flip_normals()
|
---|
218 | {
|
---|
219 | if ( m_nrm_channel == nullptr ) return;
|
---|
220 | NV_ASSERT( m_nrm_type == FLOAT_VECTOR_3, "Unknown normal vector type!" );
|
---|
221 | raw_data_channel_access channel( m_nrm_channel );
|
---|
222 | for ( uint32 i = 0; i < channel.size(); ++i )
|
---|
223 | {
|
---|
224 | vec3& normal = *reinterpret_cast<vec3*>( channel.raw_data() + channel.element_size() * i + m_nrm_offset );
|
---|
225 | normal = -normal;
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | void nv::mesh_data_creator::scale_texture( vec2 min, vec2 max )
|
---|
231 | {
|
---|
232 | if ( m_tex_channel == nullptr ) return;
|
---|
233 | NV_ASSERT( m_tex_type == FLOAT_VECTOR_2, "Unknown texcoord vector type!" );
|
---|
234 | raw_data_channel_access channel( m_tex_channel );
|
---|
235 | vec2 scale = max - min;
|
---|
236 | for ( uint32 i = 0; i < channel.size(); ++i )
|
---|
237 | {
|
---|
238 | vec2& tc = *reinterpret_cast<vec2*>( channel.raw_data() + channel.element_size() * i + m_tex_offset );
|
---|
239 | tc = min + tc * scale;
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | void nv::mesh_data_creator::generate_tangents()
|
---|
244 | {
|
---|
245 | if ( m_tan_channel != nullptr ) return;
|
---|
246 | if ( !m_pos_channel || !m_nrm_channel || !m_tex_channel ) return;
|
---|
247 |
|
---|
248 | if ( m_pos_channel->size() != m_nrm_channel->size()
|
---|
249 | || m_pos_channel->size() % m_tex_channel->size() != 0
|
---|
250 | || ( m_idx_type != UINT && m_idx_type != USHORT && m_idx_type != NONE ) )
|
---|
251 | {
|
---|
252 | return;
|
---|
253 | }
|
---|
254 |
|
---|
255 | NV_ASSERT( m_pos_type == FLOAT_VECTOR_3, "Unsupported position vector type!" );
|
---|
256 | NV_ASSERT( m_nrm_type == FLOAT_VECTOR_3, "Unsupported normal vector type!" );
|
---|
257 | NV_ASSERT( m_tex_type == FLOAT_VECTOR_2, "Unknown texcoord vector type!" );
|
---|
258 |
|
---|
259 | raw_data_channel g_channel = data_channel_creator::create< vertex_g >( m_pos_channel->size() );
|
---|
260 | vec4* tangents = &( data_channel_access< vertex_g >( &g_channel ).data()[0].tangent );
|
---|
261 | fill_n( tangents, m_pos_channel->size(), vec4() );
|
---|
262 | vec3* tangents2 = new vec3[ m_pos_channel->size() ];
|
---|
263 | uint32 tri_count = m_idx_channel ? m_idx_channel->size() / 3 : m_tex_channel->size() / 3;
|
---|
264 | uint32 vtx_count = m_pos_channel->size();
|
---|
265 | uint32 sets = m_pos_channel->size() / m_tex_channel->size();
|
---|
266 |
|
---|
267 | for ( unsigned int i = 0; i < tri_count; ++i )
|
---|
268 | {
|
---|
269 | uint32 ti0 = 0;
|
---|
270 | uint32 ti1 = 0;
|
---|
271 | uint32 ti2 = 0;
|
---|
272 | if ( m_idx_type == UINT )
|
---|
273 | {
|
---|
274 | const uint32* idata = reinterpret_cast<const uint32*>( m_idx_channel->raw_data() );
|
---|
275 | ti0 = idata[ i * 3 ];
|
---|
276 | ti1 = idata[ i * 3 + 1 ];
|
---|
277 | ti2 = idata[ i * 3 + 2 ];
|
---|
278 | }
|
---|
279 | else if ( m_idx_type == USHORT )
|
---|
280 | {
|
---|
281 | const uint16* idata = reinterpret_cast<const uint16*>( m_idx_channel->raw_data() );
|
---|
282 | ti0 = idata[ i * 3 ];
|
---|
283 | ti1 = idata[ i * 3 + 1 ];
|
---|
284 | ti2 = idata[ i * 3 + 2 ];
|
---|
285 | }
|
---|
286 | else // if ( m_idx_type == NONE )
|
---|
287 | {
|
---|
288 | ti0 = i * 3;
|
---|
289 | ti1 = i * 3 + 1;
|
---|
290 | ti2 = i * 3 + 2;
|
---|
291 | }
|
---|
292 |
|
---|
293 | vec2 w1 = *reinterpret_cast<const vec2*>( m_tex_channel->raw_data() + m_tex_channel->element_size()*ti0 + m_tex_offset );
|
---|
294 | vec2 w2 = *reinterpret_cast<const vec2*>( m_tex_channel->raw_data() + m_tex_channel->element_size()*ti1 + m_tex_offset );
|
---|
295 | vec2 w3 = *reinterpret_cast<const vec2*>( m_tex_channel->raw_data() + m_tex_channel->element_size()*ti2 + m_tex_offset );
|
---|
296 | vec2 st1 = w3 - w1;
|
---|
297 | vec2 st2 = w2 - w1;
|
---|
298 | float stst = (st1.x * st2.y - st2.x * st1.y);
|
---|
299 | float coef = ( stst != 0.0f ? 1.0f / stst : 0.0f );
|
---|
300 |
|
---|
301 | for ( uint32 set = 0; set < sets; ++set )
|
---|
302 | {
|
---|
303 | uint32 nti0 = m_tex_channel->size() * set + ti0;
|
---|
304 | uint32 nti1 = m_tex_channel->size() * set + ti1;
|
---|
305 | uint32 nti2 = m_tex_channel->size() * set + ti2;
|
---|
306 | const vec3& v1 = *reinterpret_cast<const vec3*>( m_pos_channel->raw_data() + m_pos_channel->element_size()*nti0 + m_pos_offset );
|
---|
307 | const vec3& v2 = *reinterpret_cast<const vec3*>( m_pos_channel->raw_data() + m_pos_channel->element_size()*nti1 + m_pos_offset );
|
---|
308 | const vec3& v3 = *reinterpret_cast<const vec3*>( m_pos_channel->raw_data() + m_pos_channel->element_size()*nti2 + m_pos_offset );
|
---|
309 | vec3 xyz1 = v3 - v1;
|
---|
310 | vec3 xyz2 = v2 - v1;
|
---|
311 |
|
---|
312 | //vec3 normal = math::cross( xyz1, xyz2 );
|
---|
313 | //
|
---|
314 | //vtcs[ ti0 ].normal += normal;
|
---|
315 | //vtcs[ ti1 ].normal += normal;
|
---|
316 | //vtcs[ ti2 ].normal += normal;
|
---|
317 | vec3 tangent = (( xyz1 * st2.y ) - ( xyz2 * st1.y )) * coef;
|
---|
318 | vec3 tangent2 = (( xyz2 * st1.x ) - ( xyz1 * st2.x )) * coef;
|
---|
319 |
|
---|
320 | tangents[nti0] = vec4( vec3( tangents[nti0] ) + tangent, 0 );
|
---|
321 | tangents[nti1] = vec4( vec3( tangents[nti1] ) + tangent, 0 );
|
---|
322 | tangents[nti2] = vec4( vec3( tangents[nti2] ) + tangent, 0 );
|
---|
323 |
|
---|
324 | tangents2[nti0] += tangent2;
|
---|
325 | tangents2[nti1] += tangent2;
|
---|
326 | tangents2[nti2] += tangent2;
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 | for ( unsigned int i = 0; i < vtx_count; ++i )
|
---|
331 | {
|
---|
332 | const vec3 n = *reinterpret_cast<const vec3*>( m_nrm_channel->raw_data() + m_nrm_channel->element_size()*i + m_nrm_offset );
|
---|
333 | const vec3 t = vec3(tangents[i]);
|
---|
334 | if ( ! ( t.x == 0.0f && t.y == 0.0f && t.z == 0.0f ) )
|
---|
335 | {
|
---|
336 | tangents[i] = vec4( math::normalize(t - n * math::dot( n, t )), 0.0f );
|
---|
337 | tangents[i][3] = ( math::dot( math::cross(n, t), tangents2[i]) < 0.0f) ? -1.0f : 1.0f;
|
---|
338 | }
|
---|
339 | }
|
---|
340 | delete[] tangents2;
|
---|
341 |
|
---|
342 | uint32 n_channel_index = m_data->get_channel_index( slot::NORMAL );
|
---|
343 | data_channel_set_creator( m_data ).set_channel( n_channel_index, merge_channels( *m_nrm_channel, g_channel ) );
|
---|
344 | initialize();
|
---|
345 | }
|
---|
346 |
|
---|
347 | void nv::mesh_data_creator::rotate_quadrant( uint8 rotation )
|
---|
348 | {
|
---|
349 | if ( rotation % 4 == 0 ) return;
|
---|
350 | NV_ASSERT( m_pos_type == FLOAT_VECTOR_3, "Unsupported position vector type!" );
|
---|
351 | NV_ASSERT( m_nrm_type == FLOAT_VECTOR_3, "Unsupported normal vector type!" );
|
---|
352 | NV_ASSERT( m_tan_type == FLOAT_VECTOR_4, "Unsupported tangent vector type!" );
|
---|
353 |
|
---|
354 | float r11 = 0.f;
|
---|
355 | float r12 = 0.f;
|
---|
356 | float r21 = 0.f;
|
---|
357 | float r22 = 0.f;
|
---|
358 |
|
---|
359 | switch ( rotation % 4 )
|
---|
360 | {
|
---|
361 | case 1: r12 = -1.f; r21 = 1.f; break;
|
---|
362 | case 2: r11 = -1.f; r22 = -1.f; break;
|
---|
363 | case 3: r12 = 1.f; r21 = -1.f; break;
|
---|
364 | default:
|
---|
365 | break;
|
---|
366 | }
|
---|
367 |
|
---|
368 | unsigned vtx_count = m_pos_channel->size();
|
---|
369 | uint8* pos_data = raw_data_channel_access( m_pos_channel ).raw_data();
|
---|
370 | uint8* nrm_data = raw_data_channel_access( m_nrm_channel ).raw_data();
|
---|
371 | uint8* tan_data = raw_data_channel_access( m_tan_channel ).raw_data();
|
---|
372 | for ( unsigned int i = 0; i < vtx_count; ++i )
|
---|
373 | {
|
---|
374 | vec3& pos = *reinterpret_cast<vec3*>( pos_data + m_pos_channel->element_size() * i + m_pos_offset );
|
---|
375 | vec3& nrm = *reinterpret_cast<vec3*>( nrm_data + m_nrm_channel->element_size() * i + m_nrm_offset );
|
---|
376 | vec4& tan = *reinterpret_cast<vec4*>( tan_data + m_tan_channel->element_size() * i + m_tan_offset );
|
---|
377 |
|
---|
378 | pos = vec3(
|
---|
379 | pos.x * r11 + pos.z * r12,
|
---|
380 | pos.y,
|
---|
381 | pos.x * r21 + pos.z * r22
|
---|
382 | );
|
---|
383 | nrm = vec3(
|
---|
384 | nrm.x * r11 + nrm.z * r12,
|
---|
385 | nrm.y,
|
---|
386 | nrm.x * r21 + nrm.z * r22
|
---|
387 | );
|
---|
388 | tan = vec4(
|
---|
389 | tan.x * r11 + tan.z * r12,
|
---|
390 | tan.y,
|
---|
391 | tan.x * r21 + tan.z * r22,
|
---|
392 | tan.w // make sure this is proper
|
---|
393 | );
|
---|
394 | }
|
---|
395 |
|
---|
396 |
|
---|
397 | }
|
---|
398 |
|
---|
399 | void nv::mesh_data_creator::mirror( bool x, bool z )
|
---|
400 | {
|
---|
401 | if ( !x && !z ) return;
|
---|
402 | NV_ASSERT( m_pos_type == FLOAT_VECTOR_3, "Unsupported position vector type!" );
|
---|
403 | NV_ASSERT( m_nrm_type == FLOAT_VECTOR_3, "Unsupported normal vector type!" );
|
---|
404 | NV_ASSERT( m_tan_type == FLOAT_VECTOR_4, "Unsupported tangent vector type!" );
|
---|
405 |
|
---|
406 | float kx = x ? -1.0f : 1.0f;
|
---|
407 | float kz = z ? -1.0f : 1.0f;
|
---|
408 |
|
---|
409 | unsigned vtx_count = m_pos_channel->size();
|
---|
410 | uint8* pos_data = raw_data_channel_access( m_pos_channel ).raw_data();
|
---|
411 | uint8* nrm_data = raw_data_channel_access( m_nrm_channel ).raw_data();
|
---|
412 | uint8* tan_data = raw_data_channel_access( m_tan_channel ).raw_data();
|
---|
413 | for ( unsigned int i = 0; i < vtx_count; ++i )
|
---|
414 | {
|
---|
415 | vec3& pos = *reinterpret_cast<vec3*>( pos_data + m_pos_channel->element_size() * i + m_pos_offset );
|
---|
416 | vec3& nrm = *reinterpret_cast<vec3*>( nrm_data + m_nrm_channel->element_size() * i + m_nrm_offset );
|
---|
417 | vec4& tan = *reinterpret_cast<vec4*>( tan_data + m_tan_channel->element_size() * i + m_tan_offset );
|
---|
418 |
|
---|
419 | pos = vec3(
|
---|
420 | pos.x * kx,
|
---|
421 | pos.y,
|
---|
422 | pos.z * kz
|
---|
423 | );
|
---|
424 | nrm = vec3(
|
---|
425 | nrm.x * kx,
|
---|
426 | nrm.y,
|
---|
427 | nrm.z * kz
|
---|
428 | );
|
---|
429 | tan = vec4(
|
---|
430 | tan.x * kx,
|
---|
431 | tan.y,
|
---|
432 | tan.z * kz,
|
---|
433 | tan.w * kx * kz// make sure this is proper
|
---|
434 | );
|
---|
435 | }
|
---|
436 |
|
---|
437 | if ( !( x && z ) )
|
---|
438 | swap_culling();
|
---|
439 | }
|
---|
440 |
|
---|
441 | template < typename T >
|
---|
442 | static inline void swap_culling_impl( nv::raw_data_channel* index_channel )
|
---|
443 | {
|
---|
444 | nv::raw_data_channel_access ichannel( index_channel );
|
---|
445 | T* indices = reinterpret_cast<T*>( ichannel.raw_data() );
|
---|
446 | nv::uint32 count = index_channel->size() / 3;
|
---|
447 | for ( nv::uint32 i = 0; i < count; ++i )
|
---|
448 | {
|
---|
449 | nv::swap( indices[i * 3], indices[i * 3 + 1] );
|
---|
450 | }
|
---|
451 | }
|
---|
452 |
|
---|
453 | void nv::mesh_data_creator::swap_culling()
|
---|
454 | {
|
---|
455 | NV_ASSERT( m_idx_channel, "Swap culling unsupported on non-indexed meshes!" );
|
---|
456 | NV_ASSERT( m_idx_channel->descriptor().size() == 1, "Malformed index channel!" );
|
---|
457 | NV_ASSERT( m_idx_channel->size() % 3 == 0, "Malformed index channel - not per GL_TRIANGLE LAYOUT?" );
|
---|
458 |
|
---|
459 | if ( m_idx_channel->size() == 0 ) return;
|
---|
460 | switch ( m_idx_type )
|
---|
461 | {
|
---|
462 | case USHORT: swap_culling_impl< uint16 >( m_idx_channel ); break;
|
---|
463 | case UINT: swap_culling_impl< uint16 >( m_idx_channel ); break;
|
---|
464 | default: NV_ASSERT( false, "Swap culling supports only unsigned and unsigned short indices!" ); break;
|
---|
465 | }
|
---|
466 | }
|
---|
467 |
|
---|
468 | void nv::mesh_data_creator::translate( vec3 offset )
|
---|
469 | {
|
---|
470 | if ( m_pos_channel == nullptr ) return;
|
---|
471 | NV_ASSERT( m_pos_type == FLOAT_VECTOR_3, "Unsupported poosition vector type!" );
|
---|
472 | raw_data_channel_access channel( m_pos_channel );
|
---|
473 | for ( uint32 i = 0; i < channel.size(); ++i )
|
---|
474 | {
|
---|
475 | vec3& p = *reinterpret_cast<vec3*>( channel.raw_data() + channel.element_size() * i + m_pos_offset );
|
---|
476 | p = p + offset;
|
---|
477 | }
|
---|
478 |
|
---|
479 | }
|
---|
480 |
|
---|
481 | void nv::mesh_data_creator::initialize()
|
---|
482 | {
|
---|
483 | NV_ASSERT( m_data, "bad parameter!" );
|
---|
484 | m_pos_channel = nullptr;
|
---|
485 | m_nrm_channel = nullptr;
|
---|
486 | m_tan_channel = nullptr;
|
---|
487 | m_tex_channel = nullptr;
|
---|
488 | m_idx_channel = nullptr;
|
---|
489 |
|
---|
490 | m_pos_offset = -1;
|
---|
491 | m_nrm_offset = -1;
|
---|
492 | m_tan_offset = -1;
|
---|
493 | m_tex_offset = -1;
|
---|
494 | m_idx_offset = -1;
|
---|
495 |
|
---|
496 | m_pos_type = NONE;
|
---|
497 | m_nrm_type = NONE;
|
---|
498 | m_tan_type = NONE;
|
---|
499 | m_tex_type = NONE;
|
---|
500 | m_idx_type = NONE;
|
---|
501 |
|
---|
502 | for ( uint32 c = 0; c < m_data->size(); ++c )
|
---|
503 | {
|
---|
504 | raw_data_channel* channel = data_channel_set_creator( m_data )[c];
|
---|
505 |
|
---|
506 | for ( const auto& cslot : channel->descriptor() )
|
---|
507 | switch ( cslot.vslot )
|
---|
508 | {
|
---|
509 | case slot::POSITION:
|
---|
510 | m_pos_type = cslot.etype;
|
---|
511 | m_pos_offset = int( cslot.offset );
|
---|
512 | m_pos_channel = channel;
|
---|
513 | break;
|
---|
514 | case slot::NORMAL:
|
---|
515 | m_nrm_type = cslot.etype;
|
---|
516 | m_nrm_offset = int( cslot.offset );
|
---|
517 | m_nrm_channel = channel;
|
---|
518 | break;
|
---|
519 | case slot::TANGENT:
|
---|
520 | m_tan_type = cslot.etype;
|
---|
521 | m_tan_offset = int( cslot.offset );
|
---|
522 | m_tan_channel = channel;
|
---|
523 | break;
|
---|
524 | case slot::TEXCOORD:
|
---|
525 | m_tex_type = cslot.etype;
|
---|
526 | m_tex_offset = int( cslot.offset );
|
---|
527 | m_tex_channel = channel;
|
---|
528 | break;
|
---|
529 | case slot::INDEX:
|
---|
530 | m_idx_type = cslot.etype;
|
---|
531 | m_idx_offset = int( cslot.offset );
|
---|
532 | m_idx_channel = channel;
|
---|
533 | break;
|
---|
534 | default: break;
|
---|
535 | }
|
---|
536 | }
|
---|
537 | }
|
---|
538 |
|
---|
539 | nv::raw_data_channel nv::mesh_data_creator::merge_channels( const raw_data_channel& a, const raw_data_channel& b )
|
---|
540 | {
|
---|
541 | NV_ASSERT( a.size() == b.size(), "merge_channel - bad channels!" );
|
---|
542 | data_descriptor desc = a.descriptor();
|
---|
543 | desc.append( b.descriptor() );
|
---|
544 |
|
---|
545 | raw_data_channel result = data_channel_creator::create( desc, a.size() );
|
---|
546 | for ( uint32 i = 0; i < a.size(); ++i )
|
---|
547 | {
|
---|
548 | raw_copy_n( a.raw_data() + i * a.element_size(), a.element_size(), raw_data_channel_access( &result ).raw_data() + i*desc.element_size() );
|
---|
549 | raw_copy_n( b.raw_data() + i * b.element_size(), b.element_size(), raw_data_channel_access( &result ).raw_data() + i*desc.element_size() + a.element_size() );
|
---|
550 | }
|
---|
551 | initialize();
|
---|
552 | return result;
|
---|
553 | }
|
---|
554 |
|
---|
555 | nv::raw_data_channel nv::mesh_data_creator::append_channels( const raw_data_channel& a, const raw_data_channel& b, uint32 frame_count )
|
---|
556 | {
|
---|
557 | NV_ASSERT( a.descriptor() == b.descriptor(), "Merge - append not compatible format!" );
|
---|
558 | NV_ASSERT( a.size() % frame_count == 0, "Merge - append first mesh empty!" );
|
---|
559 | NV_ASSERT( b.size() % frame_count == 0, "Merge - append second mesh empty!" );
|
---|
560 | size_t vtx_size = a.element_size();
|
---|
561 |
|
---|
562 | raw_data_channel result = data_channel_creator::create( a.descriptor(), a.size() + b.size() );
|
---|
563 | uint8* rdata = raw_data_channel_access( &result ).raw_data();
|
---|
564 |
|
---|
565 | if ( frame_count == 1 )
|
---|
566 | {
|
---|
567 | size_t a_size = vtx_size * a.size();
|
---|
568 | raw_copy_n( a.raw_data(), a_size, rdata );
|
---|
569 | raw_copy_n( b.raw_data(), vtx_size * b.size(), rdata + a_size );
|
---|
570 | }
|
---|
571 | else
|
---|
572 | {
|
---|
573 | size_t frame_size_a = ( a.size() / frame_count ) * vtx_size;
|
---|
574 | size_t frame_size_b = ( b.size() / frame_count ) * vtx_size;
|
---|
575 | size_t pos_a = 0;
|
---|
576 | size_t pos_b = 0;
|
---|
577 | size_t pos = 0;
|
---|
578 | for ( size_t i = 0; i < frame_count; ++i )
|
---|
579 | {
|
---|
580 | raw_copy_n( a.raw_data() + pos_a, frame_size_a, rdata + pos );
|
---|
581 | raw_copy_n( b.raw_data() + pos_b, frame_size_b, rdata + pos + frame_size_a ); pos_a += frame_size_a;
|
---|
582 | pos_b += frame_size_b;
|
---|
583 | pos += frame_size_a + frame_size_b;
|
---|
584 | }
|
---|
585 | }
|
---|
586 |
|
---|
587 | initialize();
|
---|
588 | return result;
|
---|
589 | }
|
---|
590 |
|
---|
591 |
|
---|
592 |
|
---|
593 | bool nv::mesh_data_creator::is_same_format( const data_channel_set* other )
|
---|
594 | {
|
---|
595 | if ( m_data->size() != other->size() ) return false;
|
---|
596 | for ( uint32 c = 0; c < m_data->size(); ++c )
|
---|
597 | {
|
---|
598 | if ( m_data->get_channel(c)->descriptor() != other->get_channel(c)->descriptor() )
|
---|
599 | return false;
|
---|
600 | }
|
---|
601 | return true;
|
---|
602 | }
|
---|
603 |
|
---|
604 | void nv::mesh_data_creator::merge( const data_channel_set* other )
|
---|
605 | {
|
---|
606 | if ( !is_same_format( other ) ) return;
|
---|
607 | int ch_pi = m_data->get_channel_index( slot::POSITION );
|
---|
608 | int ch_ti = m_data->get_channel_index( slot::TEXCOORD );
|
---|
609 | int och_pi = other->get_channel_index( slot::POSITION );
|
---|
610 | int och_ti = other->get_channel_index( slot::TEXCOORD );
|
---|
611 | if ( ch_pi == -1 || ch_ti == -1 ) return;
|
---|
612 | size_t size = m_data->get_channel_size( unsigned(ch_ti) );
|
---|
613 | size_t osize = other->get_channel_size( unsigned(och_ti) );
|
---|
614 | size_t count = m_data->get_channel_size( unsigned(ch_pi) );
|
---|
615 | size_t ocount = other->get_channel_size( unsigned(och_pi) );
|
---|
616 | if ( count % size != 0 || ocount % osize != 0 ) return;
|
---|
617 | if ( count / size != ocount / osize ) return;
|
---|
618 |
|
---|
619 | data_channel_set_creator data( m_data );
|
---|
620 |
|
---|
621 | for ( uint32 c = 0; c < m_data->size(); ++c )
|
---|
622 | {
|
---|
623 | const raw_data_channel* old = m_data->get_channel( c );
|
---|
624 | uint32 old_size = old->size();
|
---|
625 | data_descriptor old_desc = old->descriptor();
|
---|
626 | bool old_is_index = old_size > 0 && old_desc[0].vslot == slot::INDEX;
|
---|
627 | size_t frame_count = ( old_is_index ? 1 : old_size / size );
|
---|
628 | data.set_channel( c, append_channels( *old, *other->get_channel(c), frame_count ) );
|
---|
629 | if ( old_is_index )
|
---|
630 | {
|
---|
631 | switch ( old_desc[0].etype )
|
---|
632 | {
|
---|
633 | case USHORT :
|
---|
634 | {
|
---|
635 | NV_ASSERT( size + osize < uint16(-1), "Index out of range!" );
|
---|
636 | raw_data_channel_access ic( data[c] );
|
---|
637 | uint16* indexes = reinterpret_cast<uint16*>( ic.raw_data() );
|
---|
638 | for ( uint16 i = uint16( old_size ); i < ic.size(); ++i )
|
---|
639 | indexes[i] += uint16( size );
|
---|
640 |
|
---|
641 | }
|
---|
642 | break;
|
---|
643 | case UINT :
|
---|
644 | {
|
---|
645 | raw_data_channel_access ic( data[c] );
|
---|
646 | uint32* indexes = reinterpret_cast<uint32*>( ic.raw_data() );
|
---|
647 | for ( uint32 i = old_size; i < ic.size(); ++i )
|
---|
648 | indexes[i] += size;
|
---|
649 | }
|
---|
650 | break;
|
---|
651 | default : NV_ASSERT( false, "Unsupported index type!" ); break;
|
---|
652 | }
|
---|
653 | }
|
---|
654 | }
|
---|
655 | initialize();
|
---|
656 | }
|
---|
657 |
|
---|
658 | void nv::mesh_creator::delete_mesh( uint32 index )
|
---|
659 | {
|
---|
660 | if ( index < m_pack->get_count() )
|
---|
661 | {
|
---|
662 |
|
---|
663 | m_pack->m_meshes[index] = move( m_pack->m_meshes[m_pack->m_count - 1] );
|
---|
664 | m_pack->m_count--;
|
---|
665 | }
|
---|
666 | }
|
---|