[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 |
|
---|
| 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 );
|
---|
[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 | {
|
---|
[406] | 26 | nv_key_transform* channel = reinterpret_cast<nv_key_transform*>(keys->get_channel(0)->data);
|
---|
| 27 | nv_key_transform* pchannel = reinterpret_cast<nv_key_transform*>(pkeys->get_channel(0)->data);
|
---|
[293] | 28 | for ( unsigned n = 0; n < count; ++n )
|
---|
| 29 | {
|
---|
[398] | 30 | channel[n].tform = pchannel[ nv::min( n, pcount-1 ) ].tform * channel[n].tform;
|
---|
[293] | 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;
|
---|
[406] | 39 | m_data->m_duration = static_cast<float>( max_frames );
|
---|
[293] | 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
|
---|
[410] | 59 | && old_keys->get_channel(0)->desc.slot_count() == 1
|
---|
| 60 | && old_keys->get_channel(0)->desc[0].etype == TRANSFORM ) continue;
|
---|
[293] | 61 |
|
---|
| 62 | size_t max_keys = 0;
|
---|
| 63 | for ( size_t c = 0; c < chan_count; ++c )
|
---|
| 64 | {
|
---|
[398] | 65 | max_keys = nv::max( max_keys, old_keys->get_channel(c)->count );
|
---|
[293] | 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 );
|
---|
[406] | 71 | nv_key_transform* channel = reinterpret_cast<nv_key_transform*>(raw_channel->data);
|
---|
[410] | 72 | data_descriptor final_key = old_keys->get_final_key();
|
---|
[293] | 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 | {
|
---|
[398] | 81 | size_t idx = nv::min( old_keys->get_channel(c)->count - 1, n );
|
---|
[293] | 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);
|
---|
[410] | 109 | size_t key_size = channel->desc.element_size();
|
---|
[293] | 110 | for ( size_t n = 0; n < channel->count; ++n )
|
---|
| 111 | {
|
---|
[406] | 112 | transform_key_raw( channel->desc, channel->data + n * key_size, scale, r33, ri33 );
|
---|
[293] | 113 | }
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | void 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 | {
|
---|
| 127 | const mesh_raw_channel* channel = m_data->get_channel(c);
|
---|
[410] | 128 | const data_descriptor& desc = channel->desc;
|
---|
[293] | 129 | uint8* raw_data = channel->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 )
|
---|
| 144 | for ( uint32 i = 0; i < channel->count; i++)
|
---|
| 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 )
|
---|
| 151 | for ( uint32 i = 0; i < channel->count; i++)
|
---|
| 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 )
|
---|
| 157 | for ( uint32 i = 0; i < channel->count; i++)
|
---|
| 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 |
|
---|
| 165 | struct vertex_g
|
---|
| 166 | {
|
---|
| 167 | nv::vec4 tangent;
|
---|
| 168 | };
|
---|
| 169 |
|
---|
[295] | 170 | void 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;
|
---|
[406] | 175 | mesh_raw_channel* channel = m_data->m_channels[ unsigned( ch_n ) ];
|
---|
[410] | 176 | for ( const auto& cslot : channel->desc )
|
---|
| 177 | if ( cslot.vslot == slot::NORMAL )
|
---|
[295] | 178 | {
|
---|
[410] | 179 | n_offset = cslot.offset;
|
---|
[295] | 180 | }
|
---|
| 181 |
|
---|
| 182 | for ( uint32 i = 0; i < channel->count; ++i )
|
---|
| 183 | {
|
---|
[410] | 184 | vec3& normal = *reinterpret_cast<vec3*>( channel->data + channel->desc.element_size() * i + n_offset );
|
---|
[295] | 185 | normal = -normal;
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 |
|
---|
[294] | 190 | void 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);
|
---|
[410] | 206 |
|
---|
| 207 | for ( const auto& cslot : channel->desc )
|
---|
| 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 |
|
---|
| 244 | if ( p_channel->count != n_channel->count || p_channel->count % t_channel->count != 0 || ( i_type != UINT && i_type != USHORT && i_type != NONE ) )
|
---|
| 245 | {
|
---|
| 246 | return;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | mesh_raw_channel* g_channel = mesh_raw_channel::create<vertex_g>( p_channel->count );
|
---|
[406] | 250 | vec4* tangents = reinterpret_cast<vec4*>( g_channel->data );
|
---|
[294] | 251 | vec3* tangents2 = new vec3[ p_channel->count ];
|
---|
| 252 | uint32 tri_count = i_channel ? i_channel->count / 3 : t_channel->count / 3;
|
---|
| 253 | uint32 vtx_count = p_channel->count;
|
---|
| 254 | uint32 sets = p_channel->count / t_channel->count;
|
---|
| 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 | {
|
---|
[406] | 263 | const uint32* idata = reinterpret_cast<const uint32*>( i_channel->data );
|
---|
[294] | 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 | {
|
---|
[406] | 270 | const uint16* idata = reinterpret_cast<const uint16*>( i_channel->data );
|
---|
[294] | 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 |
|
---|
[410] | 282 | const vec2& w1 = *reinterpret_cast<vec2*>(t_channel->data + t_channel->desc.element_size()*ti0 + t_offset );
|
---|
| 283 | const vec2& w2 = *reinterpret_cast<vec2*>(t_channel->data + t_channel->desc.element_size()*ti1 + t_offset );
|
---|
| 284 | const vec2& w3 = *reinterpret_cast<vec2*>(t_channel->data + t_channel->desc.element_size()*ti2 + t_offset );
|
---|
[294] | 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->count * set + ti0;
|
---|
| 293 | uint32 nti1 = t_channel->count * set + ti1;
|
---|
| 294 | uint32 nti2 = t_channel->count * set + ti2;
|
---|
[410] | 295 | vec3 v1 = *reinterpret_cast<vec3*>(p_channel->data + p_channel->desc.element_size()*nti0 + p_offset );
|
---|
| 296 | vec3 v2 = *reinterpret_cast<vec3*>(p_channel->data + p_channel->desc.element_size()*nti1 + p_offset );
|
---|
| 297 | vec3 v3 = *reinterpret_cast<vec3*>(p_channel->data + p_channel->desc.element_size()*nti2 + p_offset );
|
---|
[294] | 298 | vec3 xyz1 = v3 - v1;
|
---|
| 299 | vec3 xyz2 = v2 - v1;
|
---|
| 300 |
|
---|
[398] | 301 | //vec3 normal = glm::cross( xyz1, xyz2 );
|
---|
[294] | 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 | {
|
---|
[410] | 321 | const vec3 n = *reinterpret_cast<vec3*>( n_channel->data + n_channel->desc.element_size()*i + n_offset );
|
---|
[294] | 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 | m_data->m_channels[ n_channel_index ] = merge_channels( n_channel, g_channel );
|
---|
| 332 | delete n_channel;
|
---|
| 333 | delete g_channel;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | nv::mesh_raw_channel* nv::mesh_data_creator::merge_channels( mesh_raw_channel* a, mesh_raw_channel* b )
|
---|
| 337 | {
|
---|
| 338 | NV_ASSERT( a->count == b->count, "merge_channel - bad channels!" );
|
---|
[410] | 339 | data_descriptor adesc = a->desc;
|
---|
| 340 | data_descriptor bdesc = b->desc;
|
---|
| 341 | uint32 count = a->count;
|
---|
[294] | 342 |
|
---|
[410] | 343 | data_descriptor desc = a->desc;
|
---|
| 344 | for ( auto bslot : bdesc )
|
---|
[294] | 345 | {
|
---|
[410] | 346 | desc.push_slot( bslot.etype, bslot.vslot );
|
---|
[294] | 347 | }
|
---|
[410] | 348 | uint8* data = new uint8[ count * desc.element_size() ];
|
---|
[294] | 349 | for ( uint32 i = 0; i < count; ++i )
|
---|
| 350 | {
|
---|
[410] | 351 | raw_copy_n( a->data + i * adesc.element_size(), adesc.element_size(), data + i*desc.element_size() );
|
---|
| 352 | raw_copy_n( b->data + i * bdesc.element_size(), bdesc.element_size(), data + i*desc.element_size() + adesc.element_size() );
|
---|
[294] | 353 | }
|
---|
| 354 | mesh_raw_channel* result = new mesh_raw_channel;
|
---|
| 355 | result->count = count;
|
---|
| 356 | result->desc = desc;
|
---|
| 357 | result->data = data;
|
---|
| 358 | return result;
|
---|
| 359 | }
|
---|
[295] | 360 |
|
---|
| 361 | nv::mesh_raw_channel* nv::mesh_data_creator::append_channels( mesh_raw_channel* a, mesh_raw_channel* b, uint32 frame_count )
|
---|
| 362 | {
|
---|
| 363 | if ( a->desc != b->desc ) return nullptr;
|
---|
| 364 | if ( a->count % frame_count != 0 ) return nullptr;
|
---|
| 365 | if ( b->count % frame_count != 0 ) return nullptr;
|
---|
[410] | 366 | size_t vtx_size = a->desc.element_size();
|
---|
[295] | 367 |
|
---|
| 368 | uint8* data = new uint8[ ( a->count + b->count ) * vtx_size ];
|
---|
| 369 |
|
---|
| 370 |
|
---|
| 371 | if ( frame_count == 1 )
|
---|
| 372 | {
|
---|
| 373 | size_t a_size = vtx_size * a->count;
|
---|
[383] | 374 | raw_copy_n( a->data, a_size, data );
|
---|
| 375 | raw_copy_n( b->data, vtx_size * b->count, data + a_size );
|
---|
[295] | 376 | }
|
---|
| 377 | else
|
---|
| 378 | {
|
---|
| 379 | size_t frame_size_a = ( a->count / frame_count ) * vtx_size;
|
---|
| 380 | size_t frame_size_b = ( b->count / frame_count ) * vtx_size;
|
---|
| 381 | size_t pos_a = 0;
|
---|
| 382 | size_t pos_b = 0;
|
---|
| 383 | size_t pos = 0;
|
---|
| 384 | for ( size_t i = 0; i < frame_count; ++i )
|
---|
| 385 | {
|
---|
[383] | 386 | raw_copy_n( a->data + pos_a, frame_size_a, data + pos );
|
---|
| 387 | raw_copy_n( b->data + pos_b, frame_size_b, data + pos + frame_size_a ); pos_a += frame_size_a;
|
---|
[295] | 388 | pos_b += frame_size_b;
|
---|
| 389 | pos += frame_size_a + frame_size_b;
|
---|
| 390 | }
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | mesh_raw_channel* result = new mesh_raw_channel;
|
---|
| 394 | result->count = a->count + b->count;
|
---|
| 395 | result->desc = a->desc;
|
---|
| 396 | result->data = data;
|
---|
| 397 | return result;
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 |
|
---|
| 401 |
|
---|
| 402 | bool nv::mesh_data_creator::is_same_format( mesh_data* other )
|
---|
| 403 | {
|
---|
| 404 | if ( m_data->get_channel_count() != other->get_channel_count() ) return false;
|
---|
| 405 | for ( uint32 c = 0; c < m_data->get_channel_count(); ++c )
|
---|
| 406 | {
|
---|
| 407 | if ( m_data->get_channel(c)->desc != other->get_channel(c)->desc )
|
---|
| 408 | return false;
|
---|
| 409 | }
|
---|
| 410 | return true;
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | void nv::mesh_data_creator::merge( mesh_data* other )
|
---|
| 414 | {
|
---|
| 415 | if ( !is_same_format( other ) ) return;
|
---|
| 416 | int ch_pi = m_data->get_channel_index( slot::POSITION );
|
---|
| 417 | int ch_ti = m_data->get_channel_index( slot::TEXCOORD );
|
---|
| 418 | int och_pi = other->get_channel_index( slot::POSITION );
|
---|
| 419 | int och_ti = other->get_channel_index( slot::TEXCOORD );
|
---|
| 420 | if ( ch_pi == -1 || ch_ti == -1 ) return;
|
---|
[406] | 421 | size_t size = m_data->m_channels[ unsigned(ch_ti) ]->count;
|
---|
| 422 | size_t osize = other->m_channels[ unsigned(och_ti) ]->count;
|
---|
| 423 | size_t count = m_data->m_channels[ unsigned(ch_pi) ]->count;
|
---|
| 424 | size_t ocount = other->m_channels[ unsigned(och_pi) ]->count;
|
---|
[295] | 425 | if ( count % size != 0 || ocount % osize != 0 ) return;
|
---|
| 426 | if ( count / size != ocount / osize ) return;
|
---|
| 427 |
|
---|
| 428 | for ( uint32 c = 0; c < m_data->get_channel_count(); ++c )
|
---|
| 429 | {
|
---|
| 430 | mesh_raw_channel* old = m_data->m_channels[c];
|
---|
[302] | 431 | size_t frame_count = ( old->get_buffer_type() == INDEX_BUFFER ? 1 : old->count / size );
|
---|
[295] | 432 | m_data->m_channels[c] = append_channels( old, other->m_channels[c], frame_count );
|
---|
| 433 | NV_ASSERT( m_data->m_channels[c], "Merge problem!" );
|
---|
[302] | 434 | if ( old->get_buffer_type() == INDEX_BUFFER )
|
---|
[295] | 435 | {
|
---|
[410] | 436 | switch ( old->desc[0].etype )
|
---|
[295] | 437 | {
|
---|
| 438 | case USHORT :
|
---|
| 439 | {
|
---|
| 440 | NV_ASSERT( size + osize < uint16(-1), "Index out of range!" );
|
---|
[406] | 441 | uint16* indexes = reinterpret_cast<uint16*>( m_data->m_channels[c]->data );
|
---|
| 442 | for ( uint16 i = uint16( old->count ); i < m_data->m_channels[c]->count; ++i )
|
---|
| 443 | indexes[i] += uint16( size );
|
---|
[295] | 444 |
|
---|
| 445 | }
|
---|
| 446 | break;
|
---|
| 447 | case UINT :
|
---|
| 448 | {
|
---|
[406] | 449 | uint32* indexes = reinterpret_cast<uint32*>( m_data->m_channels[c]->data );
|
---|
[295] | 450 | for ( uint32 i = old->count; i < m_data->m_channels[c]->count; ++i )
|
---|
| 451 | indexes[i] += size;
|
---|
| 452 | }
|
---|
| 453 | break;
|
---|
| 454 | default : NV_ASSERT( false, "Unsupported index type!" ); break;
|
---|
| 455 | }
|
---|
| 456 | m_data->m_index_channel = m_data->m_channels[c];
|
---|
| 457 | }
|
---|
| 458 | delete old;
|
---|
| 459 | }
|
---|
| 460 | }
|
---|