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

Last change on this file since 453 was 430, checked in by epyon, 10 years ago
  • mesh_data -> data_channel_set rename
  • keyframed_mesh doesn't store full data_channel_set anymore
File size: 14.9 KB
Line 
1// Copyright (C) 2012-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#include "nv/gfx/mesh_creator.hh"
8
9#include "nv/interface/data_channel_access.hh"
10
11struct nv_key_transform { nv::transform tform; };
12
13void nv::mesh_nodes_creator::pre_transform_keys()
14{
15        if ( m_data->m_flat ) return;
16        merge_keys();
17        uint32 max_frames = 0;
18        for ( auto keys : m_data->m_data )
19        {
20                sint16 parent_id = keys->get_parent_id();
21                data_channel_set* pkeys  = ( parent_id != -1 ? m_data->m_data[parent_id] : nullptr );
22                size_t count     = ( keys ? keys->get_channel_size(0) : 0 );
23                size_t pcount    = ( pkeys ? pkeys->get_channel_size(0) : 0 );
24                max_frames = nv::max<uint32>( count, max_frames );
25                if ( pkeys && pkeys->size() > 0 && keys && keys->size() > 0 )
26                {
27                        data_channel_access< nv_key_transform > channel_creator( keys, 0 );
28                        nv_key_transform* channel = channel_creator.data();
29                        const nv_key_transform* pchannel = pkeys->get_channel(0)->data_cast< nv_key_transform >();
30                        for ( unsigned n = 0; n < count; ++n )
31                        {
32                                channel[n].tform = pchannel[ nv::min( n, pcount-1 ) ].tform * channel[n].tform;
33                        }
34                }
35        }
36
37        // DAE pre_transform hack
38        if ( m_data->m_frame_rate == 1 )
39        {
40                m_data->m_frame_rate = 32;
41                m_data->m_duration   = static_cast<float>( max_frames );
42        }
43
44        m_data->m_flat = true;
45}
46
47
48void nv::mesh_nodes_creator::merge_keys()
49{
50        for ( size_t i = 0; i < m_data->size(); ++i )
51        {
52                data_channel_set* old_keys = m_data->m_data[i];
53                if ( old_keys && old_keys->size() > 0 )
54                {
55                        size_t chan_count = old_keys->size();
56                        if ( chan_count == 1
57                                && old_keys->get_channel(0)->descriptor().size() == 1
58                                && old_keys->get_channel(0)->descriptor()[0].etype == TRANSFORM ) continue;
59
60                        size_t max_keys = 0;
61                        for ( size_t c = 0; c < chan_count; ++c )
62                        {
63                                max_keys = nv::max( max_keys, old_keys->get_channel(c)->size() );
64                        }
65
66                        data_channel_set* new_keys = data_channel_set_creator::create_set( 1 );
67                        data_channel_set_creator nk_access( new_keys );
68                        nk_access.set_name( old_keys->get_name() );
69                        nk_access.set_parent_id( old_keys->get_parent_id() );
70                        nk_access.set_transform( old_keys->get_transform() );
71                        data_channel_access< nv_key_transform > kt_channel( nk_access.add_channel<nv_key_transform>( max_keys ) );
72
73                        raw_channel_interpolator interpolator( old_keys );
74                        data_descriptor final_key = interpolator.get_interpolation_key();
75
76                        for ( unsigned n = 0; n < max_keys; ++n )
77                        {
78                                float key[ 16 ];
79                                float* pkey = key;
80
81                                for ( uint16 c = 0; c < chan_count; ++c )
82                                {
83                                        size_t idx = nv::min( old_keys->get_channel_size(c) - 1, n );
84                                        pkey += raw_channel_interpolator::get_raw( *old_keys->get_channel(c), idx, pkey );
85                                }
86                                kt_channel.data()[n].tform = extract_key_raw< nv::transform >( final_key, key );
87                        }
88
89                        delete old_keys;
90                        m_data->m_data[i] = new_keys;
91                }
92        }
93}
94
95void nv::mesh_nodes_creator::transform( float scale, const mat3& r33 )
96{
97        mat3 ri33 = glm::inverse( r33 );
98        mat4 pre_transform ( scale * r33 );
99        mat4 post_transform( 1.f/scale * ri33 );
100
101        for ( auto node : m_data->m_data )
102        {
103                node->m_transform = pre_transform * node->m_transform * post_transform;
104
105                for ( size_t c = 0; c < node->size(); ++c )
106                {
107                        raw_data_channel_access channel( node, c );
108                        size_t key_size = channel.element_size();
109                        for ( size_t n = 0; n < channel.size(); ++n )
110                        {
111                                transform_key_raw( node->get_channel( c )->descriptor(), channel.raw_data() + n * key_size, scale, r33, ri33 );
112                        }
113                }
114        }
115}
116
117void nv::mesh_data_creator::transform( float scale, const mat3& r33 )
118{
119        vec3 vertex_offset     = vec3();
120        mat3 vertex_transform  = scale * r33;
121        mat3 normal_transform  = r33;
122
123        for ( uint32 c = 0; c < m_data->size(); ++c )
124        {
125                raw_data_channel_access channel( m_data, c );
126                const data_descriptor&  desc    = channel.descriptor();
127                uint8* raw_data = channel.raw_data();
128                uint32 vtx_size = desc.element_size();
129                int p_offset = -1;
130                int n_offset = -1;
131                int t_offset = -1;
132                for ( const auto& cslot : desc  )
133                        switch ( cslot.vslot )
134                        {
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;
138                                default             : break;
139                        }
140
141                if ( p_offset != -1 )
142                        for ( uint32 i = 0; i < channel.size(); i++)
143                        {
144                                vec3& p = *reinterpret_cast<vec3*>( raw_data + vtx_size*i + p_offset );
145                                p = vertex_transform * p + vertex_offset;
146                        }
147
148                if ( n_offset != -1 )
149                        for ( uint32 i = 0; i < channel.size(); i++)
150                        {
151                                vec3& n = *reinterpret_cast<vec3*>( raw_data + vtx_size*i + n_offset );
152                                n = glm::normalize( normal_transform * n );
153                        }
154                if ( t_offset != -1 )
155                        for ( uint32 i = 0; i < channel.size(); i++)
156                        {
157                                vec4& t = *reinterpret_cast<vec4*>(raw_data + vtx_size*i + t_offset );
158                                t = vec4( glm::normalize( normal_transform * vec3(t) ), t[3] );
159                        }
160        }
161}
162
163struct vertex_g
164{
165        nv::vec4 tangent;
166};
167
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;
173        raw_data_channel_access channel( m_data, unsigned( ch_n ) );
174        for ( const auto& cslot : channel.descriptor() )
175                if ( cslot.vslot == slot::NORMAL )
176                {
177                        n_offset  = cslot.offset;
178                }
179
180        for ( uint32 i = 0; i < channel.size(); ++i )
181        {
182                vec3& normal = *reinterpret_cast<vec3*>( channel.raw_data() + channel.element_size() * i + n_offset );
183                normal = -normal;
184        }
185}
186
187
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
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;
200
201        for ( uint32 c = 0; c < m_data->size(); ++c )
202        {
203                const raw_data_channel* channel = m_data->get_channel(c);
204
205                for ( const auto& cslot : channel->descriptor() )
206                switch ( cslot.vslot )
207                {
208                        case slot::POSITION :
209                                if ( cslot.etype == FLOAT_VECTOR_3 )
210                                {
211                                        p_offset  = int( cslot.offset );
212                                        p_channel = channel;
213                                }
214                                break;
215                        case slot::NORMAL   :
216                                if ( cslot.etype == FLOAT_VECTOR_3 )
217                                {
218                                        n_offset  = int( cslot.offset );
219                                        n_channel = data_channel_set_creator( m_data )[ c ];
220                                        n_channel_index = c;
221                                }
222                                break;
223                        case slot::TEXCOORD :
224                                if ( cslot.etype == FLOAT_VECTOR_2 )
225                                {
226                                        t_offset  = int( cslot.offset );
227                                        t_channel = channel;
228                                }
229                                break;
230                        case slot::INDEX    :
231                                {
232                                        i_type    = cslot.etype;
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
242        if ( p_channel->size() != n_channel->size()
243                || p_channel->size() % t_channel->size() != 0
244                || ( i_type != UINT && i_type != USHORT && i_type != NONE ) )
245        {
246                return;
247        }
248
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 );
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();
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                {
263                        const uint32* idata = reinterpret_cast<const uint32*>( i_channel->raw_data() );
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                {
270                        const uint16* idata = reinterpret_cast<const uint16*>( i_channel->raw_data() );
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
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 );
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                {
292                        uint32 nti0 = t_channel->size() * set + ti0;
293                        uint32 nti1 = t_channel->size() * set + ti1;
294                        uint32 nti2 = t_channel->size() * set + ti2;
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 );
298                        vec3 xyz1 = v3 - v1;
299                        vec3 xyz2 = v2 - v1;
300
301                        //vec3 normal = glm::cross( xyz1, xyz2 );
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        {
321                const vec3 n = *reinterpret_cast<const vec3*>( n_channel->raw_data() + n_channel->element_size()*i + n_offset );
322                const vec3 t = vec3(tangents[i]);
323                if ( ! ( t.x == 0.0f && t.y == 0.0f && t.z == 0.0f ) )
324                {
325                        tangents[i]    = vec4( glm::normalize(t - n * glm::dot( n, t )), 0.0f );
326                        tangents[i][3] = (glm::dot(glm::cross(n, t), tangents2[i]) < 0.0f) ? -1.0f : 1.0f;
327                }
328        }
329        delete[] tangents2;
330
331        data_channel_set_creator( m_data ).set_channel( n_channel_index, merge_channels( *n_channel, g_channel ) );
332}
333
334nv::raw_data_channel nv::mesh_data_creator::merge_channels( const raw_data_channel& a, const raw_data_channel& b )
335{
336        NV_ASSERT( a.size() == b.size(), "merge_channel - bad channels!" );
337        data_descriptor desc  = a.descriptor();
338        desc.append( b.descriptor() );
339
340        raw_data_channel result = data_channel_creator::create( desc, a.size() );
341        for ( uint32 i = 0; i < a.size(); ++i )
342        {
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() );
345        }
346
347        return result;
348}
349
350nv::raw_data_channel nv::mesh_data_creator::append_channels( const raw_data_channel& a, const raw_data_channel& b, uint32 frame_count )
351{
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();
356
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();
359
360        if ( frame_count == 1 )
361        {
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 );
365        }
366        else
367        {
368                size_t frame_size_a = ( a.size() / frame_count ) * vtx_size;
369                size_t frame_size_b = ( b.size() / frame_count ) * vtx_size;
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                {
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;
377                        pos_b += frame_size_b;
378                        pos   += frame_size_a + frame_size_b;
379                }
380        }
381
382        return result;
383}
384
385
386
387bool nv::mesh_data_creator::is_same_format( data_channel_set* other )
388{
389        if ( m_data->size() != other->size() ) return false;
390        for ( uint32 c = 0; c < m_data->size(); ++c )
391        {
392                if ( m_data->get_channel(c)->descriptor() != other->get_channel(c)->descriptor() )
393                        return false;
394        }
395        return true;
396}
397
398void nv::mesh_data_creator::merge( data_channel_set* other )
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;
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) );
410        if ( count % size != 0 || ocount % osize != 0 ) return;
411        if ( count / size != ocount / osize ) return;
412       
413        data_channel_set_creator data( m_data );
414
415        for ( uint32 c = 0; c < m_data->size(); ++c )
416        {
417                const raw_data_channel* old = m_data->get_channel( c );
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 ) );
423                if ( old_is_index )
424                {
425                        switch ( old_desc[0].etype )
426                        {
427                        case USHORT :
428                                {
429                                        NV_ASSERT( size + osize < uint16(-1), "Index out of range!" );
430                                        raw_data_channel_access ic( data[c] );
431                                        uint16* indexes = reinterpret_cast<uint16*>( ic.raw_data() );
432                                        for ( uint16 i = uint16( old_size ); i < ic.size(); ++i )
433                                                indexes[i] += uint16( size );
434
435                                }
436                                break;
437                        case UINT   :
438                                {
439                                        raw_data_channel_access ic( data[c] );
440                                        uint32* indexes = reinterpret_cast<uint32*>( ic.raw_data() );
441                                        for ( uint32 i = old_size; i < ic.size(); ++i )
442                                                indexes[i] += size;
443                                }
444                                break;
445                        default : NV_ASSERT( false, "Unsupported index type!" ); break;
446                        }
447                }
448        }
449}
450
451void nv::mesh_creator::delete_mesh( uint32 index )
452{
453        if ( index < m_pack->get_count() )
454        {
455
456                m_pack->m_meshes[index] = move( m_pack->m_meshes[m_pack->m_count - 1] );
457                m_pack->m_count--;
458        }
459}
Note: See TracBrowser for help on using the repository browser.