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

Last change on this file since 424 was 424, checked in by epyon, 10 years ago
  • refactoring WIP! (compiles though - warnings on purpose)
File size: 15.2 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 ( size_t i = 0; i < m_data->get_count(); ++i )
19        {
20                sint16 parent_id = m_data->m_nodes[i].data->get_parent_id();
21                data_channel_set* keys   = m_data->m_nodes[i].data;
22                data_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 );
25                max_frames = nv::max<uint32>( count, max_frames );
26                if ( pkeys && pkeys->size() > 0 && keys && keys->size() > 0 )
27                {
28                        data_channel_access< nv_key_transform > channel_creator( keys, 0 );
29                        nv_key_transform* channel = channel_creator.data();
30                        const nv_key_transform* pchannel = pkeys->get_channel(0)->data_cast< nv_key_transform >();
31                        for ( unsigned n = 0; n < count; ++n )
32                        {
33                                channel[n].tform = pchannel[ nv::min( n, pcount-1 ) ].tform * channel[n].tform;
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;
42                m_data->m_duration   = static_cast<float>( max_frames );
43        }
44
45        m_data->m_flat = true;
46}
47
48
49void nv::mesh_nodes_creator::merge_keys()
50{
51        for ( size_t i = 0; i < m_data->get_count(); ++i )
52        {
53                data_channel_set* old_keys = m_data->m_nodes[i].data;
54                if ( old_keys && old_keys->size() > 0 )
55                {
56                        size_t chan_count = old_keys->size();
57                        if ( chan_count == 1
58                                && old_keys->get_channel(0)->descriptor().size() == 1
59                                && old_keys->get_channel(0)->descriptor()[0].etype == TRANSFORM ) continue;
60
61                        size_t max_keys = 0;
62                        for ( size_t c = 0; c < chan_count; ++c )
63                        {
64                                max_keys = nv::max( max_keys, old_keys->get_channel(c)->size() );
65                        }
66
67                        data_channel_set* new_keys = data_channel_set_creator::create_set( 1 );
68                        data_channel_set_creator nk_access( new_keys );
69                        nk_access.set_name( old_keys->get_name() );
70                        nk_access.set_parent_id( old_keys->get_parent_id() );
71                        nk_access.set_transform( old_keys->get_transform() );
72                        data_channel_access< nv_key_transform > kt_channel( nk_access.add_channel<nv_key_transform>( max_keys ) );
73
74                        raw_channel_interpolator interpolator( old_keys );
75                        data_descriptor final_key = interpolator.get_interpolation_key();
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                                {
84                                        size_t idx = nv::min( old_keys->get_channel_size(c) - 1, n );
85                                        pkey += raw_channel_interpolator::get_raw( *old_keys->get_channel(c), idx, pkey );
86                                }
87                                kt_channel.data()[n].tform = extract_key_raw< nv::transform >( final_key, key );
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.data->m_transform = pre_transform * node.data->m_transform * post_transform;
106                if ( node.data )
107                {
108                        data_channel_set* kdata  = node.data;
109                        for ( size_t c = 0; c < kdata->size(); ++c )
110                        {
111                                raw_data_channel_access channel( kdata, c );
112                                size_t key_size = channel.element_size();
113                                for ( size_t n = 0; n < channel.size(); ++n )
114                                {
115                                        transform_key_raw( kdata->get_channel( c )->descriptor(), channel.raw_data() + n * key_size, scale, r33, ri33 );
116                                }
117                        }
118                }
119        }
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 = glm::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( glm::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        int ch_n  = m_data->get_channel_index( slot::NORMAL );
176        size_t n_offset = 0;
177        if ( ch_n == -1 ) return;
178        raw_data_channel_access channel( m_data, unsigned( ch_n ) );
179        for ( const auto& cslot : channel.descriptor() )
180                if ( cslot.vslot == slot::NORMAL )
181                {
182                        n_offset  = cslot.offset;
183                }
184
185        for ( uint32 i = 0; i < channel.size(); ++i )
186        {
187                vec3& normal = *reinterpret_cast<vec3*>( channel.raw_data() + channel.element_size() * i + n_offset );
188                normal = -normal;
189        }
190}
191
192
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
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;
205
206        for ( uint32 c = 0; c < m_data->size(); ++c )
207        {
208                const raw_data_channel* channel = m_data->get_channel(c);
209
210                for ( const auto& cslot : channel->descriptor() )
211                switch ( cslot.vslot )
212                {
213                        case slot::POSITION :
214                                if ( cslot.etype == FLOAT_VECTOR_3 )
215                                {
216                                        p_offset  = int( cslot.offset );
217                                        p_channel = channel;
218                                }
219                                break;
220                        case slot::NORMAL   :
221                                if ( cslot.etype == FLOAT_VECTOR_3 )
222                                {
223                                        n_offset  = int( cslot.offset );
224                                        n_channel = data_channel_set_creator( m_data )[ c ];
225                                        n_channel_index = c;
226                                }
227                                break;
228                        case slot::TEXCOORD :
229                                if ( cslot.etype == FLOAT_VECTOR_2 )
230                                {
231                                        t_offset  = int( cslot.offset );
232                                        t_channel = channel;
233                                }
234                                break;
235                        case slot::INDEX    :
236                                {
237                                        i_type    = cslot.etype;
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
247        if ( p_channel->size() != n_channel->size()
248                || p_channel->size() % t_channel->size() != 0
249                || ( i_type != UINT && i_type != USHORT && i_type != NONE ) )
250        {
251                return;
252        }
253
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 );
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();
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                {
268                        const uint32* idata = reinterpret_cast<const uint32*>( i_channel->raw_data() );
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                {
275                        const uint16* idata = reinterpret_cast<const uint16*>( i_channel->raw_data() );
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
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 );
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                {
297                        uint32 nti0 = t_channel->size() * set + ti0;
298                        uint32 nti1 = t_channel->size() * set + ti1;
299                        uint32 nti2 = t_channel->size() * set + ti2;
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 );
303                        vec3 xyz1 = v3 - v1;
304                        vec3 xyz2 = v2 - v1;
305
306                        //vec3 normal = glm::cross( xyz1, xyz2 );
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        {
326                const vec3 n = *reinterpret_cast<const vec3*>( n_channel->raw_data() + n_channel->element_size()*i + n_offset );
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
336        data_channel_set_creator( m_data ).set_channel( n_channel_index, merge_channels( *n_channel, g_channel ) );
337}
338
339nv::raw_data_channel nv::mesh_data_creator::merge_channels( const raw_data_channel& a, const raw_data_channel& b )
340{
341        NV_ASSERT( a.size() == b.size(), "merge_channel - bad channels!" );
342        data_descriptor desc  = a.descriptor();
343        desc.append( b.descriptor() );
344
345        raw_data_channel result = data_channel_creator::create( desc, a.size() );
346        for ( uint32 i = 0; i < a.size(); ++i )
347        {
348                raw_copy_n( a.raw_data() + i * a.element_size(), a.element_size(), raw_data_channel_access( &result ).raw_data() + i*desc.element_size() );
349                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() );
350        }
351
352        return result;
353}
354
355nv::raw_data_channel nv::mesh_data_creator::append_channels( const raw_data_channel& a, const raw_data_channel& b, uint32 frame_count )
356{
357        NV_ASSERT( a.descriptor() == b.descriptor(), "Merge - append not compatible format!" );
358        NV_ASSERT( a.size() % frame_count == 0, "Merge - append first mesh empty!" );
359        NV_ASSERT( b.size() % frame_count == 0, "Merge - append second mesh empty!" );
360        size_t vtx_size = a.element_size();
361
362        raw_data_channel result = data_channel_creator::create( a.descriptor(), a.size() + b.size() );
363        uint8* rdata = raw_data_channel_access( &result ).raw_data();
364
365        if ( frame_count == 1 )
366        {
367                size_t a_size = vtx_size * a.size();
368                raw_copy_n( a.raw_data(), a_size, rdata );
369                raw_copy_n( b.raw_data(), vtx_size * b.size(), rdata + a_size );
370        }
371        else
372        {
373                size_t frame_size_a = ( a.size() / frame_count ) * vtx_size;
374                size_t frame_size_b = ( b.size() / frame_count ) * vtx_size;
375                size_t pos_a = 0;
376                size_t pos_b = 0;
377                size_t pos   = 0;
378                for ( size_t i = 0; i < frame_count; ++i )
379                {
380                        raw_copy_n( a.raw_data() + pos_a, frame_size_a, rdata + pos );
381                        raw_copy_n( b.raw_data() + pos_b, frame_size_b, rdata + pos + frame_size_a );                           pos_a += frame_size_a;
382                        pos_b += frame_size_b;
383                        pos   += frame_size_a + frame_size_b;
384                }
385        }
386
387        return result;
388}
389
390
391
392bool nv::mesh_data_creator::is_same_format( mesh_data* other )
393{
394        if ( m_data->size() != other->size() ) return false;
395        for ( uint32 c = 0; c < m_data->size(); ++c )
396        {
397                if ( m_data->get_channel(c)->descriptor() != other->get_channel(c)->descriptor() )
398                        return false;
399        }
400        return true;
401}
402
403void nv::mesh_data_creator::merge( mesh_data* other )
404{
405        if ( !is_same_format( other ) ) return;
406        int ch_pi  = m_data->get_channel_index( slot::POSITION );
407        int ch_ti  = m_data->get_channel_index( slot::TEXCOORD );
408        int och_pi = other->get_channel_index( slot::POSITION );
409        int och_ti = other->get_channel_index( slot::TEXCOORD );
410        if ( ch_pi == -1 || ch_ti == -1 ) return;
411        size_t size   = m_data->get_channel_size( unsigned(ch_ti) );
412        size_t osize  =  other->get_channel_size( unsigned(och_ti) );
413        size_t count  = m_data->get_channel_size( unsigned(ch_pi) );
414        size_t ocount =  other->get_channel_size( unsigned(och_pi) );
415        if ( count % size != 0 || ocount % osize != 0 ) return;
416        if ( count / size != ocount / osize ) return;
417       
418        data_channel_set_creator data( m_data );
419
420        for ( uint32 c = 0; c < m_data->size(); ++c )
421        {
422                const raw_data_channel* old = m_data->get_channel( c );
423                uint32 old_size = old->size();
424                data_descriptor old_desc = old->descriptor();
425                bool old_is_index = old_size > 0 && old_desc[0].vslot == slot::INDEX;
426                size_t frame_count = ( old_is_index ? 1 : old_size / size );
427                data.set_channel( c, append_channels( *old, *other->get_channel(c), frame_count ) );
428                if ( old_is_index )
429                {
430                        switch ( old_desc[0].etype )
431                        {
432                        case USHORT :
433                                {
434                                        NV_ASSERT( size + osize < uint16(-1), "Index out of range!" );
435                                        raw_data_channel_access ic( data[c] );
436                                        uint16* indexes = reinterpret_cast<uint16*>( ic.raw_data() );
437                                        for ( uint16 i = uint16( old_size ); i < ic.size(); ++i )
438                                                indexes[i] += uint16( size );
439
440                                }
441                                break;
442                        case UINT   :
443                                {
444                                        raw_data_channel_access ic( data[c] );
445                                        uint32* indexes = reinterpret_cast<uint32*>( ic.raw_data() );
446                                        for ( uint32 i = old_size; i < ic.size(); ++i )
447                                                indexes[i] += size;
448                                }
449                                break;
450                        default : NV_ASSERT( false, "Unsupported index type!" ); break;
451                        }
452                }
453        }
454}
455
456void nv::mesh_creator::delete_mesh( uint32 index )
457{
458        if ( index < m_pack->get_count() )
459        {
460                data_channel_set_creator( &m_pack->m_meshes[m_pack->m_count - 1] ).move_to( m_pack->m_meshes[index] );
461                m_pack->m_count--;
462        }
463}
Note: See TracBrowser for help on using the repository browser.