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