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

Last change on this file since 540 was 534, checked in by epyon, 8 years ago

CONTINUED:

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