source: trunk/src/gfx/mesh_creator.cc @ 467

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