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

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