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

Last change on this file since 456 was 456, checked in by epyon, 10 years ago
  • mesh_creator minor cleanup - need more
  • data channel cloning
  • removed debug msg
File size: 18.0 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        vec3* tangents2             = new vec3[ m_pos_channel->size() ];
218        uint32 tri_count = m_idx_channel ? m_idx_channel->size() / 3 : m_tex_channel->size() / 3;
219        uint32 vtx_count = m_pos_channel->size();
220        uint32 sets      = m_pos_channel->size() / m_tex_channel->size();
221
222        for ( unsigned int i = 0; i < tri_count; ++i )
223        {
224                uint32 ti0 = 0;
225                uint32 ti1 = 0;
226                uint32 ti2 = 0;
227                if ( m_idx_type == UINT )
228                {
229                        const uint32* idata = reinterpret_cast<const uint32*>( m_idx_channel->raw_data() );
230                        ti0 = idata[ i * 3 ];
231                        ti1 = idata[ i * 3 + 1 ];
232                        ti2 = idata[ i * 3 + 2 ];
233                }
234                else if ( m_idx_type == USHORT )
235                {
236                        const uint16* idata = reinterpret_cast<const uint16*>( m_idx_channel->raw_data() );
237                        ti0 = idata[ i * 3 ];
238                        ti1 = idata[ i * 3 + 1 ];
239                        ti2 = idata[ i * 3 + 2 ];
240                }
241                else // if ( m_idx_type == NONE )
242                {
243                        ti0 = i * 3;
244                        ti1 = i * 3 + 1;
245                        ti2 = i * 3 + 2;
246                }
247
248                const vec2& w1 = *reinterpret_cast<const vec2*>( m_tex_channel->raw_data() + m_tex_channel->element_size()*ti0 + m_tex_offset );
249                const vec2& w2 = *reinterpret_cast<const vec2*>( m_tex_channel->raw_data() + m_tex_channel->element_size()*ti1 + m_tex_offset );
250                const vec2& w3 = *reinterpret_cast<const vec2*>( m_tex_channel->raw_data() + m_tex_channel->element_size()*ti2 + m_tex_offset );
251                vec2 st1 = w3 - w1;
252                vec2 st2 = w2 - w1;
253                float stst = (st1.x * st2.y - st2.x * st1.y);
254                float coef = ( stst != 0.0f ? 1.0f / stst : 0.0f );
255
256                for ( uint32 set = 0; set < sets; ++set )
257                {
258                        uint32 nti0 = m_tex_channel->size() * set + ti0;
259                        uint32 nti1 = m_tex_channel->size() * set + ti1;
260                        uint32 nti2 = m_tex_channel->size() * set + ti2;
261                        const vec3& v1 = *reinterpret_cast<const vec3*>( m_pos_channel->raw_data() + m_pos_channel->element_size()*nti0 + m_pos_offset );
262                        const vec3& v2 = *reinterpret_cast<const vec3*>( m_pos_channel->raw_data() + m_pos_channel->element_size()*nti1 + m_pos_offset );
263                        const vec3& v3 = *reinterpret_cast<const vec3*>( m_pos_channel->raw_data() + m_pos_channel->element_size()*nti2 + m_pos_offset );
264                        vec3 xyz1 = v3 - v1;
265                        vec3 xyz2 = v2 - v1;
266
267                        //vec3 normal = math::cross( xyz1, xyz2 );
268                        //
269                        //vtcs[ ti0 ].normal += normal;
270                        //vtcs[ ti1 ].normal += normal;
271                        //vtcs[ ti2 ].normal += normal;
272                        vec3 tangent  = (( xyz1 * st2.y ) - ( xyz2 * st1.y )) * coef;
273                        vec3 tangent2 = (( xyz2 * st1.x ) - ( xyz1 * st2.x )) * coef;
274
275                        tangents[nti0] = vec4( vec3( tangents[nti0] ) + tangent, 0 );
276                        tangents[nti1] = vec4( vec3( tangents[nti1] ) + tangent, 0 );
277                        tangents[nti2] = vec4( vec3( tangents[nti2] ) + tangent, 0 );
278
279                        tangents2[nti0] += tangent2;
280                        tangents2[nti1] += tangent2;
281                        tangents2[nti2] += tangent2;
282                }
283        }
284
285        for ( unsigned int i = 0; i < vtx_count; ++i )
286        {
287                const vec3 n = *reinterpret_cast<const vec3*>( m_nrm_channel->raw_data() + m_nrm_channel->element_size()*i + m_nrm_offset );
288                const vec3 t = vec3(tangents[i]);
289                if ( ! ( t.x == 0.0f && t.y == 0.0f && t.z == 0.0f ) )
290                {
291                        tangents[i]    = vec4( math::normalize(t - n * math::dot( n, t )), 0.0f );
292                        tangents[i][3] = ( math::dot( math::cross(n, t), tangents2[i]) < 0.0f) ? -1.0f : 1.0f;
293                }
294        }
295        delete[] tangents2;
296
297        uint32 n_channel_index = m_data->get_channel_index( slot::NORMAL );
298        data_channel_set_creator( m_data ).set_channel( n_channel_index, merge_channels( *m_nrm_channel, g_channel ) );
299        initialize();
300}
301
302void nv::mesh_data_creator::rotate_quadrant( uint8 rotation )
303{
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                        1.0f // make sure this is proper
348                        );
349        }
350
351
352}
353
354void nv::mesh_data_creator::translate( vec3 offset )
355{
356        if ( m_pos_channel == nullptr ) return;
357        NV_ASSERT( m_pos_type == FLOAT_VECTOR_3, "Unsupported poosition vector type!" );
358        raw_data_channel_access channel( m_pos_channel );
359        for ( uint32 i = 0; i < channel.size(); ++i )
360        {
361                vec3& p = *reinterpret_cast<vec3*>( channel.raw_data() + channel.element_size() * i + m_pos_offset );
362                p = p + offset;
363        }
364
365}
366
367void nv::mesh_data_creator::initialize()
368{
369        NV_ASSERT( m_data, "bad parameter!" );
370        m_pos_channel = nullptr;
371        m_nrm_channel = nullptr;
372        m_tan_channel = nullptr;
373        m_tex_channel = nullptr;
374        m_idx_channel = nullptr;
375
376        m_pos_offset = -1;
377        m_nrm_offset = -1;
378        m_tan_offset = -1;
379        m_tex_offset = -1;
380        m_idx_offset = -1;
381
382        m_pos_type = NONE;
383        m_nrm_type = NONE;
384        m_tan_type = NONE;
385        m_tex_type = NONE;
386        m_idx_type = NONE;
387
388        for ( uint32 c = 0; c < m_data->size(); ++c )
389        {
390                raw_data_channel* channel = data_channel_set_creator( m_data )[c];
391
392                for ( const auto& cslot : channel->descriptor() )
393                        switch ( cslot.vslot )
394                        {
395                        case slot::POSITION:
396                                m_pos_type = cslot.etype;
397                                m_pos_offset = int( cslot.offset );
398                                m_pos_channel = channel;
399                                break;
400                        case slot::NORMAL:
401                                m_nrm_type = cslot.etype;
402                                m_nrm_offset = int( cslot.offset );
403                                m_nrm_channel = channel;
404                                break;
405                        case slot::TANGENT:
406                                m_tan_type = cslot.etype;
407                                m_tan_offset = int( cslot.offset );
408                                m_tan_channel = channel;
409                                break;
410                        case slot::TEXCOORD:
411                                m_tex_type = cslot.etype;
412                                m_tex_offset = int( cslot.offset );
413                                m_tex_channel = channel;
414                                break;
415                        case slot::INDEX:
416                                m_idx_type = cslot.etype;
417                                m_idx_offset = int( cslot.offset );
418                                m_idx_channel = channel;
419                                break;
420                        default: break;
421                        }
422        }
423}
424
425nv::raw_data_channel nv::mesh_data_creator::merge_channels( const raw_data_channel& a, const raw_data_channel& b )
426{
427        NV_ASSERT( a.size() == b.size(), "merge_channel - bad channels!" );
428        data_descriptor desc  = a.descriptor();
429        desc.append( b.descriptor() );
430
431        raw_data_channel result = data_channel_creator::create( desc, a.size() );
432        for ( uint32 i = 0; i < a.size(); ++i )
433        {
434                raw_copy_n( a.raw_data() + i * a.element_size(), a.element_size(), raw_data_channel_access( &result ).raw_data() + i*desc.element_size() );
435                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() );
436        }
437        initialize();
438        return result;
439}
440
441nv::raw_data_channel nv::mesh_data_creator::append_channels( const raw_data_channel& a, const raw_data_channel& b, uint32 frame_count )
442{
443        NV_ASSERT( a.descriptor() == b.descriptor(), "Merge - append not compatible format!" );
444        NV_ASSERT( a.size() % frame_count == 0, "Merge - append first mesh empty!" );
445        NV_ASSERT( b.size() % frame_count == 0, "Merge - append second mesh empty!" );
446        size_t vtx_size = a.element_size();
447
448        raw_data_channel result = data_channel_creator::create( a.descriptor(), a.size() + b.size() );
449        uint8* rdata = raw_data_channel_access( &result ).raw_data();
450
451        if ( frame_count == 1 )
452        {
453                size_t a_size = vtx_size * a.size();
454                raw_copy_n( a.raw_data(), a_size, rdata );
455                raw_copy_n( b.raw_data(), vtx_size * b.size(), rdata + a_size );
456        }
457        else
458        {
459                size_t frame_size_a = ( a.size() / frame_count ) * vtx_size;
460                size_t frame_size_b = ( b.size() / frame_count ) * vtx_size;
461                size_t pos_a = 0;
462                size_t pos_b = 0;
463                size_t pos   = 0;
464                for ( size_t i = 0; i < frame_count; ++i )
465                {
466                        raw_copy_n( a.raw_data() + pos_a, frame_size_a, rdata + pos );
467                        raw_copy_n( b.raw_data() + pos_b, frame_size_b, rdata + pos + frame_size_a );                           pos_a += frame_size_a;
468                        pos_b += frame_size_b;
469                        pos   += frame_size_a + frame_size_b;
470                }
471        }
472
473        initialize();
474        return result;
475}
476
477
478
479bool nv::mesh_data_creator::is_same_format( data_channel_set* other )
480{
481        if ( m_data->size() != other->size() ) return false;
482        for ( uint32 c = 0; c < m_data->size(); ++c )
483        {
484                if ( m_data->get_channel(c)->descriptor() != other->get_channel(c)->descriptor() )
485                        return false;
486        }
487        return true;
488}
489
490void nv::mesh_data_creator::merge( data_channel_set* other )
491{
492        if ( !is_same_format( other ) ) return;
493        int ch_pi  = m_data->get_channel_index( slot::POSITION );
494        int ch_ti  = m_data->get_channel_index( slot::TEXCOORD );
495        int och_pi = other->get_channel_index( slot::POSITION );
496        int och_ti = other->get_channel_index( slot::TEXCOORD );
497        if ( ch_pi == -1 || ch_ti == -1 ) return;
498        size_t size   = m_data->get_channel_size( unsigned(ch_ti) );
499        size_t osize  =  other->get_channel_size( unsigned(och_ti) );
500        size_t count  = m_data->get_channel_size( unsigned(ch_pi) );
501        size_t ocount =  other->get_channel_size( unsigned(och_pi) );
502        if ( count % size != 0 || ocount % osize != 0 ) return;
503        if ( count / size != ocount / osize ) return;
504       
505        data_channel_set_creator data( m_data );
506
507        for ( uint32 c = 0; c < m_data->size(); ++c )
508        {
509                const raw_data_channel* old = m_data->get_channel( c );
510                uint32 old_size = old->size();
511                data_descriptor old_desc = old->descriptor();
512                bool old_is_index = old_size > 0 && old_desc[0].vslot == slot::INDEX;
513                size_t frame_count = ( old_is_index ? 1 : old_size / size );
514                data.set_channel( c, append_channels( *old, *other->get_channel(c), frame_count ) );
515                if ( old_is_index )
516                {
517                        switch ( old_desc[0].etype )
518                        {
519                        case USHORT :
520                                {
521                                        NV_ASSERT( size + osize < uint16(-1), "Index out of range!" );
522                                        raw_data_channel_access ic( data[c] );
523                                        uint16* indexes = reinterpret_cast<uint16*>( ic.raw_data() );
524                                        for ( uint16 i = uint16( old_size ); i < ic.size(); ++i )
525                                                indexes[i] += uint16( size );
526
527                                }
528                                break;
529                        case UINT   :
530                                {
531                                        raw_data_channel_access ic( data[c] );
532                                        uint32* indexes = reinterpret_cast<uint32*>( ic.raw_data() );
533                                        for ( uint32 i = old_size; i < ic.size(); ++i )
534                                                indexes[i] += size;
535                                }
536                                break;
537                        default : NV_ASSERT( false, "Unsupported index type!" ); break;
538                        }
539                }
540        }
541        initialize();
542}
543
544void nv::mesh_creator::delete_mesh( uint32 index )
545{
546        if ( index < m_pack->get_count() )
547        {
548
549                m_pack->m_meshes[index] = move( m_pack->m_meshes[m_pack->m_count - 1] );
550                m_pack->m_count--;
551        }
552}
Note: See TracBrowser for help on using the repository browser.