Changeset 295 for trunk/src/gfx/mesh_creator.cc
- Timestamp:
- 07/31/14 02:32:59 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/gfx/mesh_creator.cc
r294 r295 168 168 }; 169 169 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; 175 mesh_raw_channel* channel = m_data->m_channels[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 170 190 void nv::mesh_data_creator::generate_tangents() 171 191 { … … 339 359 return result; 340 360 } 361 362 nv::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 403 bool 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 414 void 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[ ch_ti ]->count; 423 size_t osize = other->m_channels[ och_ti ]->count; 424 size_t count = m_data->m_channels[ ch_pi ]->count; 425 size_t ocount = other->m_channels[ 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->is_index() ? 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->is_index() ) 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 TracChangeset
for help on using the changeset viewer.