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

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