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 |
|
---|
9 | struct nv_key_transform { nv::transform tform; };
|
---|
10 |
|
---|
11 | void 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
|
---|
46 | struct assimp_key_p { float time; nv::vec3 position; };
|
---|
47 | struct assimp_key_r { float time; nv::quat rotation; };
|
---|
48 |
|
---|
49 |
|
---|
50 | void 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 |
|
---|
93 | void 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 |
|
---|
119 | void 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 | int 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 = desc.slots[i].offset; break;
|
---|
138 | case slot::NORMAL : if ( desc.slots[i].etype == FLOAT_VECTOR_3 ) n_offset = desc.slots[i].offset; break;
|
---|
139 | case slot::TANGENT : if ( desc.slots[i].etype == FLOAT_VECTOR_4 ) t_offset = 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 | }
|
---|