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

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