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

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