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

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