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

Last change on this file since 454 was 454, checked in by epyon, 10 years ago
  • math library work
  • glm only referenced in math/common.hh
  • still a lot work to do, but its WURF
File size: 14.9 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
[293]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;
[428]18        for ( auto keys : m_data->m_data )
[293]19        {
[427]20                sint16 parent_id = keys->get_parent_id();
21                data_channel_set* pkeys  = ( parent_id != -1 ? m_data->m_data[parent_id] : nullptr );
[417]22                size_t count     = ( keys ? keys->get_channel_size(0) : 0 );
23                size_t pcount    = ( pkeys ? pkeys->get_channel_size(0) : 0 );
[398]24                max_frames = nv::max<uint32>( count, max_frames );
[416]25                if ( pkeys && pkeys->size() > 0 && keys && keys->size() > 0 )
[293]26                {
[417]27                        data_channel_access< nv_key_transform > channel_creator( keys, 0 );
[413]28                        nv_key_transform* channel = channel_creator.data();
29                        const nv_key_transform* pchannel = pkeys->get_channel(0)->data_cast< nv_key_transform >();
[293]30                        for ( unsigned n = 0; n < count; ++n )
31                        {
[398]32                                channel[n].tform = pchannel[ nv::min( n, pcount-1 ) ].tform * channel[n].tform;
[293]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;
[406]41                m_data->m_duration   = static_cast<float>( max_frames );
[293]42        }
43
44        m_data->m_flat = true;
45}
46
47
48void nv::mesh_nodes_creator::merge_keys()
49{
[428]50        for ( size_t i = 0; i < m_data->size(); ++i )
[293]51        {
[427]52                data_channel_set* old_keys = m_data->m_data[i];
[416]53                if ( old_keys && old_keys->size() > 0 )
[293]54                {
[416]55                        size_t chan_count = old_keys->size();
[293]56                        if ( chan_count == 1
[415]57                                && old_keys->get_channel(0)->descriptor().size() == 1
[412]58                                && old_keys->get_channel(0)->descriptor()[0].etype == TRANSFORM ) continue;
[293]59
60                        size_t max_keys = 0;
61                        for ( size_t c = 0; c < chan_count; ++c )
62                        {
[415]63                                max_keys = nv::max( max_keys, old_keys->get_channel(c)->size() );
[293]64                        }
65
[424]66                        data_channel_set* new_keys = data_channel_set_creator::create_set( 1 );
[419]67                        data_channel_set_creator nk_access( new_keys );
[424]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() );
[417]71                        data_channel_access< nv_key_transform > kt_channel( nk_access.add_channel<nv_key_transform>( max_keys ) );
72
[419]73                        raw_channel_interpolator interpolator( old_keys );
74                        data_descriptor final_key = interpolator.get_interpolation_key();
[293]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                                {
[418]83                                        size_t idx = nv::min( old_keys->get_channel_size(c) - 1, n );
[419]84                                        pkey += raw_channel_interpolator::get_raw( *old_keys->get_channel(c), idx, pkey );
[293]85                                }
[419]86                                kt_channel.data()[n].tform = extract_key_raw< nv::transform >( final_key, key );
[293]87                        }
88
89                        delete old_keys;
[427]90                        m_data->m_data[i] = new_keys;
[293]91                }
92        }
93}
94
95void nv::mesh_nodes_creator::transform( float scale, const mat3& r33 )
96{
[454]97        mat3 ri33 = math::inverse( r33 );
[293]98        mat4 pre_transform ( scale * r33 );
99        mat4 post_transform( 1.f/scale * ri33 );
100
[428]101        for ( auto node : m_data->m_data )
[293]102        {
[427]103                node->m_transform = pre_transform * node->m_transform * post_transform;
104
105                for ( size_t c = 0; c < node->size(); ++c )
[293]106                {
[427]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 )
[293]110                        {
[427]111                                transform_key_raw( node->get_channel( c )->descriptor(), channel.raw_data() + n * key_size, scale, r33, ri33 );
[293]112                        }
113                }
114        }
115}
116
117void nv::mesh_data_creator::transform( float scale, const mat3& r33 )
118{
[398]119        vec3 vertex_offset     = vec3();
[293]120        mat3 vertex_transform  = scale * r33;
121        mat3 normal_transform  = r33;
122
[416]123        for ( uint32 c = 0; c < m_data->size(); ++c )
[293]124        {
[417]125                raw_data_channel_access channel( m_data, c );
[413]126                const data_descriptor&  desc    = channel.descriptor();
127                uint8* raw_data = channel.raw_data();
[410]128                uint32 vtx_size = desc.element_size();
[293]129                int p_offset = -1;
130                int n_offset = -1;
131                int t_offset = -1;
[410]132                for ( const auto& cslot : desc  )
133                        switch ( cslot.vslot )
[293]134                        {
[410]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;
[293]138                                default             : break;
139                        }
140
141                if ( p_offset != -1 )
[413]142                        for ( uint32 i = 0; i < channel.size(); i++)
[293]143                        {
[406]144                                vec3& p = *reinterpret_cast<vec3*>( raw_data + vtx_size*i + p_offset );
[293]145                                p = vertex_transform * p + vertex_offset;
146                        }
147
148                if ( n_offset != -1 )
[413]149                        for ( uint32 i = 0; i < channel.size(); i++)
[293]150                        {
[406]151                                vec3& n = *reinterpret_cast<vec3*>( raw_data + vtx_size*i + n_offset );
[454]152                                n = math::normalize( normal_transform * n );
[293]153                        }
154                if ( t_offset != -1 )
[413]155                        for ( uint32 i = 0; i < channel.size(); i++)
[293]156                        {
[406]157                                vec4& t = *reinterpret_cast<vec4*>(raw_data + vtx_size*i + t_offset );
[454]158                                t = vec4( math::normalize( normal_transform * vec3(t) ), t[3] );
[293]159                        }
160        }
161}
[294]162
163struct vertex_g
164{
165        nv::vec4 tangent;
166};
167
[295]168void nv::mesh_data_creator::flip_normals()
169{
170        int ch_n  = m_data->get_channel_index( slot::NORMAL );
171        size_t n_offset = 0;
172        if ( ch_n == -1 ) return;
[417]173        raw_data_channel_access channel( m_data, unsigned( ch_n ) );
[413]174        for ( const auto& cslot : channel.descriptor() )
[410]175                if ( cslot.vslot == slot::NORMAL )
[295]176                {
[410]177                        n_offset  = cslot.offset;
[295]178                }
179
[413]180        for ( uint32 i = 0; i < channel.size(); ++i )
[295]181        {
[413]182                vec3& normal = *reinterpret_cast<vec3*>( channel.raw_data() + channel.element_size() * i + n_offset );
[295]183                normal = -normal;
184        }
185}
186
187
[294]188void nv::mesh_data_creator::generate_tangents()
189{
190        int p_offset = -1;
191        int n_offset = -1;
192        int t_offset = -1;
193        datatype i_type = NONE;
194        uint32 n_channel_index = 0;
195
[411]196        const raw_data_channel* p_channel = nullptr;
197              raw_data_channel* n_channel = nullptr;
198        const raw_data_channel* t_channel = nullptr;
199        const raw_data_channel* i_channel = nullptr;
[294]200
[416]201        for ( uint32 c = 0; c < m_data->size(); ++c )
[294]202        {
[411]203                const raw_data_channel* channel = m_data->get_channel(c);
[410]204
[412]205                for ( const auto& cslot : channel->descriptor() )
[410]206                switch ( cslot.vslot )
[294]207                {
208                        case slot::POSITION :
[410]209                                if ( cslot.etype == FLOAT_VECTOR_3 )
[294]210                                {
[410]211                                        p_offset  = int( cslot.offset );
[294]212                                        p_channel = channel;
213                                }
214                                break;
[410]215                        case slot::NORMAL   :
216                                if ( cslot.etype == FLOAT_VECTOR_3 )
[294]217                                {
[410]218                                        n_offset  = int( cslot.offset );
[416]219                                        n_channel = data_channel_set_creator( m_data )[ c ];
[294]220                                        n_channel_index = c;
221                                }
222                                break;
[410]223                        case slot::TEXCOORD :
224                                if ( cslot.etype == FLOAT_VECTOR_2 )
[294]225                                {
[410]226                                        t_offset  = int( cslot.offset );
[294]227                                        t_channel = channel;
228                                }
229                                break;
230                        case slot::INDEX    :
231                                {
[410]232                                        i_type    = cslot.etype;
[294]233                                        i_channel = channel;
234                                }
235                                break;
236                        case slot::TANGENT  : return;
237                        default             : break;
238                }
239        }
240        if ( !p_channel || !n_channel || !t_channel ) return;
241
[415]242        if ( p_channel->size() != n_channel->size()
243                || p_channel->size() % t_channel->size() != 0
[412]244                || ( i_type != UINT && i_type != USHORT && i_type != NONE ) )
[294]245        {
246                return;
247        }
248
[418]249        raw_data_channel g_channel  = data_channel_creator::create< vertex_g >( p_channel->size() );
250        vec4* tangents              = &( data_channel_access< vertex_g >( &g_channel ).data()[0].tangent );
[415]251        vec3* tangents2             = new vec3[ p_channel->size() ];
252        uint32 tri_count = i_channel ? i_channel->size() / 3 : t_channel->size() / 3;
253        uint32 vtx_count = p_channel->size();
254        uint32 sets      = p_channel->size() / t_channel->size();
[294]255
256        for ( unsigned int i = 0; i < tri_count; ++i )
257        {
258                uint32 ti0 = 0;
259                uint32 ti1 = 0;
260                uint32 ti2 = 0;
261                if ( i_type == UINT )
262                {
[413]263                        const uint32* idata = reinterpret_cast<const uint32*>( i_channel->raw_data() );
[294]264                        ti0 = idata[ i * 3 ];
265                        ti1 = idata[ i * 3 + 1 ];
266                        ti2 = idata[ i * 3 + 2 ];
267                }
268                else if ( i_type == USHORT )
269                {
[413]270                        const uint16* idata = reinterpret_cast<const uint16*>( i_channel->raw_data() );
[294]271                        ti0 = idata[ i * 3 ];
272                        ti1 = idata[ i * 3 + 1 ];
273                        ti2 = idata[ i * 3 + 2 ];
274                }
275                else // if ( i_type == NONE )
276                {
277                        ti0 = i * 3;
278                        ti1 = i * 3 + 1;
279                        ti2 = i * 3 + 2;
280                }
281
[413]282                const vec2& w1 = *reinterpret_cast<const vec2*>(t_channel->raw_data() + t_channel->element_size()*ti0 + t_offset );
283                const vec2& w2 = *reinterpret_cast<const vec2*>(t_channel->raw_data() + t_channel->element_size()*ti1 + t_offset );
284                const vec2& w3 = *reinterpret_cast<const vec2*>(t_channel->raw_data() + t_channel->element_size()*ti2 + t_offset );
[294]285                vec2 st1 = w3 - w1;
286                vec2 st2 = w2 - w1;
287                float stst = (st1.x * st2.y - st2.x * st1.y);
288                float coef = ( stst != 0.0f ? 1.0f / stst : 0.0f );
289
290                for ( uint32 set = 0; set < sets; ++set )
291                {
[415]292                        uint32 nti0 = t_channel->size() * set + ti0;
293                        uint32 nti1 = t_channel->size() * set + ti1;
294                        uint32 nti2 = t_channel->size() * set + ti2;
[413]295                        const vec3& v1 = *reinterpret_cast<const vec3*>(p_channel->raw_data() + p_channel->element_size()*nti0 + p_offset );
296                        const vec3& v2 = *reinterpret_cast<const vec3*>(p_channel->raw_data() + p_channel->element_size()*nti1 + p_offset );
297                        const vec3& v3 = *reinterpret_cast<const vec3*>(p_channel->raw_data() + p_channel->element_size()*nti2 + p_offset );
[294]298                        vec3 xyz1 = v3 - v1;
299                        vec3 xyz2 = v2 - v1;
300
[454]301                        //vec3 normal = math::cross( xyz1, xyz2 );
[294]302                        //
303                        //vtcs[ ti0 ].normal += normal;
304                        //vtcs[ ti1 ].normal += normal;
305                        //vtcs[ ti2 ].normal += normal;
306                        vec3 tangent  = (( xyz1 * st2.y ) - ( xyz2 * st1.y )) * coef;
307                        vec3 tangent2 = (( xyz2 * st1.x ) - ( xyz1 * st2.x )) * coef;
308
309                        tangents[nti0] = vec4( vec3( tangents[nti0] ) + tangent, 0 );
310                        tangents[nti1] = vec4( vec3( tangents[nti1] ) + tangent, 0 );
311                        tangents[nti2] = vec4( vec3( tangents[nti2] ) + tangent, 0 );
312
313                        tangents2[nti0] += tangent2;
314                        tangents2[nti1] += tangent2;
315                        tangents2[nti2] += tangent2;
316                }
317        }
318
319        for ( unsigned int i = 0; i < vtx_count; ++i )
320        {
[413]321                const vec3 n = *reinterpret_cast<const vec3*>( n_channel->raw_data() + n_channel->element_size()*i + n_offset );
[294]322                const vec3 t = vec3(tangents[i]);
323                if ( ! ( t.x == 0.0f && t.y == 0.0f && t.z == 0.0f ) )
324                {
[454]325                        tangents[i]    = vec4( math::normalize(t - n * math::dot( n, t )), 0.0f );
326                        tangents[i][3] = ( math::dot( math::cross(n, t), tangents2[i]) < 0.0f) ? -1.0f : 1.0f;
[294]327                }
328        }
[419]329        delete[] tangents2;
[294]330
[418]331        data_channel_set_creator( m_data ).set_channel( n_channel_index, merge_channels( *n_channel, g_channel ) );
[294]332}
333
[418]334nv::raw_data_channel nv::mesh_data_creator::merge_channels( const raw_data_channel& a, const raw_data_channel& b )
[294]335{
[418]336        NV_ASSERT( a.size() == b.size(), "merge_channel - bad channels!" );
337        data_descriptor desc  = a.descriptor();
338        desc.append( b.descriptor() );
[294]339
[418]340        raw_data_channel result = data_channel_creator::create( desc, a.size() );
341        for ( uint32 i = 0; i < a.size(); ++i )
[294]342        {
[418]343                raw_copy_n( a.raw_data() + i * a.element_size(), a.element_size(), raw_data_channel_access( &result ).raw_data() + i*desc.element_size() );
344                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]345        }
[412]346
[417]347        return result;
[294]348}
[295]349
[418]350nv::raw_data_channel nv::mesh_data_creator::append_channels( const raw_data_channel& a, const raw_data_channel& b, uint32 frame_count )
[295]351{
[418]352        NV_ASSERT( a.descriptor() == b.descriptor(), "Merge - append not compatible format!" );
353        NV_ASSERT( a.size() % frame_count == 0, "Merge - append first mesh empty!" );
354        NV_ASSERT( b.size() % frame_count == 0, "Merge - append second mesh empty!" );
355        size_t vtx_size = a.element_size();
[295]356
[418]357        raw_data_channel result = data_channel_creator::create( a.descriptor(), a.size() + b.size() );
358        uint8* rdata = raw_data_channel_access( &result ).raw_data();
[295]359
360        if ( frame_count == 1 )
361        {
[418]362                size_t a_size = vtx_size * a.size();
363                raw_copy_n( a.raw_data(), a_size, rdata );
364                raw_copy_n( b.raw_data(), vtx_size * b.size(), rdata + a_size );
[295]365        }
366        else
367        {
[418]368                size_t frame_size_a = ( a.size() / frame_count ) * vtx_size;
369                size_t frame_size_b = ( b.size() / frame_count ) * vtx_size;
[295]370                size_t pos_a = 0;
371                size_t pos_b = 0;
372                size_t pos   = 0;
373                for ( size_t i = 0; i < frame_count; ++i )
374                {
[418]375                        raw_copy_n( a.raw_data() + pos_a, frame_size_a, rdata + pos );
376                        raw_copy_n( b.raw_data() + pos_b, frame_size_b, rdata + pos + frame_size_a );                           pos_a += frame_size_a;
[295]377                        pos_b += frame_size_b;
378                        pos   += frame_size_a + frame_size_b;
379                }
380        }
381
[417]382        return result;
[295]383}
384
385
386
[430]387bool nv::mesh_data_creator::is_same_format( data_channel_set* other )
[295]388{
[416]389        if ( m_data->size() != other->size() ) return false;
390        for ( uint32 c = 0; c < m_data->size(); ++c )
[295]391        {
[411]392                if ( m_data->get_channel(c)->descriptor() != other->get_channel(c)->descriptor() )
[295]393                        return false;
394        }
395        return true;
396}
397
[430]398void nv::mesh_data_creator::merge( data_channel_set* other )
[295]399{
400        if ( !is_same_format( other ) ) return;
401        int ch_pi  = m_data->get_channel_index( slot::POSITION );
402        int ch_ti  = m_data->get_channel_index( slot::TEXCOORD );
403        int och_pi = other->get_channel_index( slot::POSITION );
404        int och_ti = other->get_channel_index( slot::TEXCOORD );
405        if ( ch_pi == -1 || ch_ti == -1 ) return;
[416]406        size_t size   = m_data->get_channel_size( unsigned(ch_ti) );
407        size_t osize  =  other->get_channel_size( unsigned(och_ti) );
408        size_t count  = m_data->get_channel_size( unsigned(ch_pi) );
409        size_t ocount =  other->get_channel_size( unsigned(och_pi) );
[295]410        if ( count % size != 0 || ocount % osize != 0 ) return;
411        if ( count / size != ocount / osize ) return;
412       
[416]413        data_channel_set_creator data( m_data );
414
415        for ( uint32 c = 0; c < m_data->size(); ++c )
[295]416        {
[416]417                const raw_data_channel* old = m_data->get_channel( c );
[418]418                uint32 old_size = old->size();
419                data_descriptor old_desc = old->descriptor();
420                bool old_is_index = old_size > 0 && old_desc[0].vslot == slot::INDEX;
421                size_t frame_count = ( old_is_index ? 1 : old_size / size );
422                data.set_channel( c, append_channels( *old, *other->get_channel(c), frame_count ) );
[412]423                if ( old_is_index )
[295]424                {
[418]425                        switch ( old_desc[0].etype )
[295]426                        {
427                        case USHORT :
428                                {
429                                        NV_ASSERT( size + osize < uint16(-1), "Index out of range!" );
[417]430                                        raw_data_channel_access ic( data[c] );
[413]431                                        uint16* indexes = reinterpret_cast<uint16*>( ic.raw_data() );
[418]432                                        for ( uint16 i = uint16( old_size ); i < ic.size(); ++i )
[406]433                                                indexes[i] += uint16( size );
[295]434
435                                }
436                                break;
437                        case UINT   :
438                                {
[417]439                                        raw_data_channel_access ic( data[c] );
[413]440                                        uint32* indexes = reinterpret_cast<uint32*>( ic.raw_data() );
[418]441                                        for ( uint32 i = old_size; i < ic.size(); ++i )
[295]442                                                indexes[i] += size;
443                                }
444                                break;
445                        default : NV_ASSERT( false, "Unsupported index type!" ); break;
446                        }
447                }
448        }
449}
[416]450
451void nv::mesh_creator::delete_mesh( uint32 index )
452{
453        if ( index < m_pack->get_count() )
454        {
[427]455
456                m_pack->m_meshes[index] = move( m_pack->m_meshes[m_pack->m_count - 1] );
[416]457                m_pack->m_count--;
458        }
459}
Note: See TracBrowser for help on using the repository browser.