Changeset 239 for trunk/src/formats
- Timestamp:
- 05/17/14 02:35:19 (11 years ago)
- Location:
- trunk/src/formats
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/formats/md2_loader.cc
r236 r239 203 203 static bool s_md2_normal_ready = false; 204 204 205 md2_loader::md2_loader() : m_md2( nullptr ) , m_size( 0 )205 md2_loader::md2_loader() : m_md2( nullptr ) 206 206 { 207 207 if ( !s_md2_normal_ready ) … … 230 230 bool md2_loader::load( stream& source ) 231 231 { 232 m_size = 0;233 232 m_md2 = (void*)(new md2_t); 234 233 if ( !read_md2( (md2_t*)m_md2, source ) ) … … 237 236 } 238 237 reindex(); 239 m_size = m_new_indexes.size();240 238 return true; 241 239 } 242 240 243 mesh_data_old* nv::md2_loader::release_mesh_data()244 {245 mesh_data_creator m;246 247 load_positions( m.get_positions() );248 load_normals( m.get_normals() );249 load_texcoords( m.get_texcoords() );250 load_indicies( m.get_indices() );251 252 m_size = m.get_indices().size();253 return m.release();254 }255 256 mesh_data_old* nv::md2_loader::get_frame( sint32 frame )257 {258 mesh_data_creator m;259 260 load_positions( m.get_positions(), frame );261 load_normals( m.get_normals(), frame );262 load_texcoords( m.get_texcoords() );263 load_indicies( m.get_indices() );264 265 m_size = m.get_indices().size();266 return m.release();267 }268 269 270 /*271 mesh* nv::md2_loader::release_mesh()272 {273 return get_frame( 0 );274 }275 276 mesh* nv::md2_loader::get_frame( sint32 frame )277 {278 md2_t* md2 = (md2_t*)m_md2;279 if ( md2 == nullptr || frame >= md2->header.num_frames ) return nullptr;280 mesh* m = new mesh();281 282 vertex_attribute< vec3 >* position = m->add_attribute<vec3>("nv_position");283 vertex_attribute< vec3 >* normal = m->add_attribute<vec3>("nv_normal");284 vertex_attribute< vec2 >* texcoord = m->add_attribute<vec2>("nv_texcoord");285 vertex_attribute< uint32 >* indices = m->add_indices<uint32>();286 287 load_positions( position->get(), frame );288 load_normals( normal->get(), frame );289 290 load_texcoords( texcoord->get() );291 load_indicies( indices->get() );292 293 m_size = indices->get().size();294 return m;295 }296 */297 298 241 size_t md2_loader::get_max_frames() const 299 242 { 300 243 return static_cast< size_t >( ((md2_t*)m_md2)->header.num_frames ); 301 }302 303 void md2_loader::load_positions( std::vector<vec3>& p, sint32 frame /*=-1*/ )304 {305 md2_t* md2 = (md2_t*)m_md2;306 size_t num_frames = static_cast< size_t >( md2->header.num_frames );307 size_t num_verts = m_new_vindexes.size();308 p.clear();309 size_t current_frame = ( frame == -1 ? 0 : static_cast< size_t >( frame ) );310 size_t frame_count = ( frame == -1 ? num_frames : 1 );311 312 p.reserve( num_verts * frame_count );313 314 while ( frame_count > 0 )315 {316 const md2_frame_t& cframe = md2->frames[current_frame];317 NV_LOG( LOG_INFO, "FrameID = " << cframe.name );318 319 vec3 scale = md2_vec3( cframe.scale );320 vec3 translate = md2_vec3( cframe.translate );321 322 for (size_t i = 0; i < num_verts; ++i )323 {324 const md2_vertex_t& v = cframe.vertices[ m_new_vindexes[ i ] ];325 p.push_back( vec3( v.v[0], v.v[2], v.v[1] ) * scale + translate );326 }327 ++current_frame;328 --frame_count;329 }330 }331 332 void md2_loader::load_normals( std::vector<vec3>& n, sint32 frame /*=-1*/ )333 {334 md2_t* md2 = (md2_t*)m_md2;335 size_t num_frames = static_cast< size_t >( md2->header.num_frames );336 size_t num_verts = m_new_vindexes.size();337 n.clear();338 size_t current_frame = ( frame == -1 ? 0 : static_cast< size_t>( frame ) );339 size_t frame_count = ( frame == -1 ? num_frames : 1 );340 341 n.reserve( num_verts * frame_count );342 343 while ( frame_count > 0 )344 {345 const md2_frame_t& cframe = md2->frames[current_frame];346 347 for (size_t i = 0; i < num_verts; ++i )348 {349 const md2_vertex_t& v = cframe.vertices[ m_new_vindexes[ i ] ];350 n.push_back( s_md2_normal_cache[ v.n ] );351 }352 ++current_frame;353 --frame_count;354 }355 }356 357 void md2_loader::load_texcoords( std::vector<vec2>& t )358 {359 md2_t* md2 = (md2_t*)m_md2;360 size_t num_verts = m_new_vindexes.size();361 362 t.clear();363 t.reserve( num_verts );364 365 vec2 scale( 1.0f / (float) md2->header.skinwidth, 1.0f / (float) md2->header.skinheight );366 367 for (size_t i = 0; i < num_verts; ++i )368 {369 const md2_texcoord_t& st = md2->texcoords[ m_new_tindexes[ i ] ];370 t.push_back( scale * vec2( st.s, st.t ) );371 }372 373 }374 375 void md2_loader::load_indicies( std::vector<uint32>& idx )376 {377 idx.assign( m_new_indexes.begin(), m_new_indexes.end() );378 244 } 379 245 … … 433 299 NV_LOG( LOG_INFO, "Reuse count = " << stats_reuse ); 434 300 } 301 302 303 struct vtx_md2_pn 304 { 305 nv::vec3 position; 306 nv::vec3 normal; 307 }; 308 309 struct vtx_md2_t 310 { 311 nv::vec2 texcoord; 312 }; 313 314 315 mesh_data* nv::md2_loader::release_mesh_data() 316 { 317 return release_mesh_data( -1 ); 318 } 319 320 mesh_data* nv::md2_loader::release_mesh_data( sint32 frame ) 321 { 322 md2_t* md2 = (md2_t*)m_md2; 323 size_t num_frames = static_cast< size_t >( md2->header.num_frames ); 324 size_t num_verts = m_new_vindexes.size(); 325 size_t current_frame = ( frame == -1 ? 0 : static_cast< size_t >( frame ) ); 326 size_t frame_count = ( frame == -1 ? num_frames : 1 ); 327 328 mesh_raw_channel* mc_pn = mesh_raw_channel::create< vtx_md2_pn >( num_verts * frame_count ); 329 vtx_md2_pn* vtx_pn = (vtx_md2_pn*)mc_pn->data; 330 331 uint32 index = 0; 332 while ( frame_count > 0 ) 333 { 334 const md2_frame_t& cframe = md2->frames[current_frame]; 335 NV_LOG( LOG_INFO, "FrameID = " << cframe.name ); 336 337 vec3 scale = md2_vec3( cframe.scale ); 338 vec3 translate = md2_vec3( cframe.translate ); 339 340 for (size_t i = 0; i < num_verts; ++i ) 341 { 342 const md2_vertex_t& v = cframe.vertices[ m_new_vindexes[ i ] ]; 343 vtx_pn[index].position = vec3( v.v[0], v.v[2], v.v[1] ) * scale + translate; 344 vtx_pn[index].normal = s_md2_normal_cache[ v.n ]; 345 index++; 346 } 347 ++current_frame; 348 --frame_count; 349 } 350 351 mesh_raw_channel* mc_t = mesh_raw_channel::create< vtx_md2_t >( num_verts ); 352 vtx_md2_t* vtx_t = (vtx_md2_t*)mc_t->data; 353 354 vec2 scale( 1.0f / (float) md2->header.skinwidth, 1.0f / (float) md2->header.skinheight ); 355 for (size_t i = 0; i < num_verts; ++i ) 356 { 357 const md2_texcoord_t& st = md2->texcoords[ m_new_tindexes[ i ] ]; 358 vtx_t[i].texcoord = scale * vec2( st.s, st.t ); 359 } 360 361 mesh_raw_index_channel* ic = mesh_raw_index_channel::create< uint16 >( m_new_indexes.size() ); 362 if ( m_new_indexes.size() > 0 ) 363 { 364 uint16* icp = (uint16*)ic->data; 365 std::copy_n( m_new_indexes.data(), m_new_indexes.size(), icp ); 366 } 367 368 mesh_data* result = new mesh_data(); 369 result->add_channel( mc_pn ); 370 result->add_channel( mc_t ); 371 result->set_index_channel( ic ); 372 return result; 373 } -
trunk/src/formats/md3_loader.cc
r236 r239 234 234 235 235 md3_loader::md3_loader() 236 : m_md3( nullptr ) , m_size( 0 )236 : m_md3( nullptr ) 237 237 { 238 238 if ( !s_normal_ready ) … … 276 276 { 277 277 m_tags.clear(); 278 m_size = 0;279 278 280 279 m_md3 = (void*)(new md3_t); … … 285 284 return true; 286 285 } 287 288 /*289 mesh* nv::md3_loader::release_mesh()290 {291 return get_frame( 0 );292 }293 294 mesh* nv::md3_loader::get_frame( sint32 frame )295 {296 mesh* m = new mesh();297 md3_t* md3 = (md3_t*)m_md3;298 299 NV_LOG( LOG_INFO, "Tags:" );300 for ( sint32 i = 0; i < md3->header.num_tags; ++i )301 {302 const md3_tag_t& rtag = md3->tags[i + md3->header.num_tags * frame];303 304 md3_tag& tag = m_tags[ (char*)(rtag.name) ];305 tag.name = (char*)(rtag.name);306 vec4 axisx = vec4( md3_vec3( rtag.axis[0] ), 0.0 );307 vec4 axisz = vec4( md3_vec3( rtag.axis[1] ), 0.0 );308 vec4 axisy = vec4( md3_vec3( rtag.axis[2] ), 0.0 );309 vec4 origin = vec4( md3_vec3( rtag.origin ), 1.0 );310 tag.transform = glm::mat4( axisx, axisy, axisz, origin );311 NV_LOG( LOG_INFO, "Tag " << tag.name << " found" );312 }313 314 vertex_attribute< vec3 >* position = m->add_attribute<vec3>("nv_position");315 vertex_attribute< vec3 >* normal = m->add_attribute<vec3>("nv_normal");316 vertex_attribute< vec2 >* texcoord = m->add_attribute<vec2>("nv_texcoord");317 vertex_attribute< uint32 >* indices = m->add_indices<uint32>();318 319 load_positions( position->get(), frame );320 load_normals( normal->get(), frame );321 322 load_texcoords( texcoord->get() );323 load_indicies( indices->get() );324 325 m_size = indices->get().size();326 return m;327 }328 */329 286 330 287 void nv::md3_loader::load_tags( std::vector<transform>& t, const std::string& tag ) … … 351 308 } 352 309 353 mesh_data_old* nv::md3_loader::release_mesh_data() 354 { 355 mesh_data_creator m; 356 357 load_positions( m.get_positions() ); 358 load_normals( m.get_normals() ); 359 load_texcoords( m.get_texcoords() ); 360 load_indicies( m.get_indices() ); 361 362 std::vector< std::string > names; 363 load_tag_names( names ); 364 365 for ( auto& name : names ) 366 { 367 load_tags( m.get_tag_map()[ name ], name ); 368 } 369 370 return m.release(); 371 } 372 373 mesh_data_old* nv::md3_loader::get_frame( sint32 frame ) 374 { 375 mesh_data_creator m; 376 377 load_positions( m.get_positions(), frame ); 378 load_normals( m.get_normals(), frame ); 379 load_texcoords( m.get_texcoords() ); 380 load_indicies( m.get_indices() ); 381 382 std::vector< std::string > names; 383 load_tag_names( names ); 384 385 for ( auto& name : names ) 386 { 387 load_tags( m.get_tag_map()[ name ], name ); 388 } 389 return m.release(); 390 } 391 392 size_t md3_loader::get_max_frames() const 393 { 394 return static_cast< size_t >( ((md3_t*)m_md3)->header.num_frames ); 395 } 396 397 void md3_loader::load_tag_names( std::vector< std::string >& tags ) 398 { 399 tags.clear(); 400 md3_t* md3 = (md3_t*)m_md3; 401 for ( sint32 i = 0; i < md3->header.num_tags; ++i ) 402 { 403 const md3_tag_t& rtag = md3->tags[i + md3->header.num_tags]; 404 tags.push_back( (char*)(rtag.name) ); 405 } 406 } 407 408 transform md3_loader::get_tag( sint32 frame, const std::string& name ) const 409 { 410 md3_t* md3 = (md3_t*)m_md3; 411 for ( sint32 i = 0; i < md3->header.num_tags; ++i ) 412 { 413 const md3_tag_t& rtag = md3->tags[i + md3->header.num_tags * frame]; 414 std::string rname((char*)(rtag.name)); 415 if (rname == name) 416 { 417 vec3 axisx ( md3_vec3( rtag.axis[0] ) ); 418 vec3 axisz ( md3_vec3( rtag.axis[1] ) ); 419 vec3 axisy ( md3_vec3( rtag.axis[2] ) ); 420 vec3 origin( md3_vec3( rtag.origin ) ); 421 return transform( origin, quat( mat3( axisx, axisy, axisz ) ) ); 422 } 423 } 424 return transform(); 425 } 426 427 const md3_tag* md3_loader::get_tag( const std::string& name ) const 428 { 429 auto it = m_tags.find( name ); 430 if ( it == m_tags.end() ) return nullptr; 431 return &(it->second); 432 } 433 434 void md3_loader::load_positions( std::vector<vec3>& p, sint32 frame /*=-1*/ ) 310 struct vtx_md3_pn 311 { 312 nv::vec3 position; 313 nv::vec3 normal; 314 }; 315 316 struct vtx_md3_t 317 { 318 nv::vec2 texcoord; 319 }; 320 321 mesh_data* nv::md3_loader::release_mesh_data() 322 { 323 return release_mesh_data( -1 ); 324 } 325 326 mesh_data* nv::md3_loader::release_mesh_data( sint32 frame ) 435 327 { 436 328 md3_t* md3 = (md3_t*)m_md3; 437 329 sint32 num_surfaces = md3->header.num_surfaces; 438 p.clear();330 sint32 num_verts = md3->vertices_per_frame; 439 331 sint32 current_frame = ( frame == -1 ? 0 : frame ); 440 332 sint32 frame_count = ( frame == -1 ? md3->header.num_frames : 1 ); 441 333 442 p.reserve( static_cast< size_t >( md3->vertices_per_frame * frame_count ) ); 443 334 mesh_raw_channel* mc_pn = mesh_raw_channel::create< vtx_md3_pn >( num_verts * frame_count ); 335 vtx_md3_pn* vtx_pn = (vtx_md3_pn*)mc_pn->data; 336 337 uint32 index = 0; 444 338 while ( frame_count > 0 ) 445 339 { … … 453 347 { 454 348 md3_vertex_t& v = surface.vertices[j]; 455 p.push_back( vec3( v.x * MD3_XYZ_SCALE, v.z * MD3_XYZ_SCALE, v.y * MD3_XYZ_SCALE ) ); 349 vtx_pn[index].position = vec3( v.x * MD3_XYZ_SCALE, v.z * MD3_XYZ_SCALE, v.y * MD3_XYZ_SCALE ); 350 vtx_pn[index].normal = s_normal_cache[ v.normal ]; 351 index++; 456 352 } 353 457 354 } 458 355 ++current_frame; 459 356 --frame_count; 460 357 } 461 } 462 463 void md3_loader::load_normals( std::vector<vec3>& n, sint32 frame /*=-1*/ ) 464 { 465 md3_t* md3 = (md3_t*)m_md3; 466 sint32 num_surfaces = md3->header.num_surfaces; 467 n.clear(); 468 sint32 current_frame = ( frame == -1 ? 0 : frame ); 469 sint32 frame_count = ( frame == -1 ? md3->header.num_frames : 1 ); 470 471 n.reserve( static_cast< size_t >( md3->vertices_per_frame * frame_count ) ); 472 473 while ( frame_count > 0 ) 474 { 475 for ( sint32 i = 0; i < num_surfaces; ++i ) 476 { 477 md3_surface_t& surface = md3->surfaces[i]; 478 sint32 vcount = surface.header.num_verts; 479 sint32 offset = vcount * current_frame; 480 sint32 limit = vcount + offset; 481 for (sint32 j = offset; j < limit; ++j ) 482 { 483 n.push_back( s_normal_cache[ surface.vertices[j].normal ] ); 484 } 485 } 486 ++current_frame; 487 --frame_count; 488 } 489 } 490 491 void md3_loader::load_texcoords( std::vector<vec2>& t ) 492 { 493 md3_t* md3 = (md3_t*)m_md3; 494 sint32 num_surfaces = md3->header.num_surfaces; 495 t.clear(); 358 359 index = 0; 360 mesh_raw_channel* mc_t = mesh_raw_channel::create< vtx_md3_t >( num_verts ); 361 vtx_md3_t* vtx_t = (vtx_md3_t*)mc_t->data; 496 362 for ( sint32 i = 0; i < num_surfaces; ++i ) 497 363 { 498 364 const md3_surface_t& surface = md3->surfaces[i]; 499 365 const uint32 vcount = static_cast< uint32 >( surface.header.num_verts ); 500 t.reserve( t.size() + vcount );501 366 for (uint32 j = 0; j < vcount; ++j ) 502 367 { 503 t.push_back( md3_texcoord( surface.st[j] ));368 vtx_t[index++].texcoord = md3_texcoord( surface.st[j] ); 504 369 } 505 370 } 506 } 507 508 void md3_loader::load_indicies( std::vector<uint32>& idx ) 509 { 510 md3_t* md3 = (md3_t*)m_md3; 511 sint32 num_surfaces = md3->header.num_surfaces; 512 idx.clear(); 371 372 sint32 index_count = 0; 373 for ( sint32 i = 0; i < num_surfaces; ++i ) 374 { 375 index_count += md3->surfaces[i].header.num_triangles * 3; 376 } 377 378 index = 0; 513 379 sint32 index_base = 0; 380 mesh_raw_index_channel* ic = mesh_raw_index_channel::create< uint16 >( index_count ); 381 uint16* icp = (uint16*)ic->data; 514 382 for ( sint32 i = 0; i < num_surfaces; ++i ) 515 383 { 516 384 const md3_surface_t& surface = md3->surfaces[i]; 517 385 const size_t tcount = static_cast< size_t >( surface.header.num_triangles ); 518 519 idx.reserve( idx.size() + tcount * 3 );520 386 for (size_t j = 0; j < tcount; ++j ) 521 387 { 522 388 const md3_triangle_t& t = surface.triangles[j]; 523 i dx.push_back( static_cast< uint32 >( index_base + t.indexes[0] ));524 i dx.push_back( static_cast< uint32 >( index_base + t.indexes[1] ));525 i dx.push_back( static_cast< uint32 >( index_base + t.indexes[2] ));389 icp[index++] = static_cast< uint16 >( index_base + t.indexes[0] ); 390 icp[index++] = static_cast< uint16 >( index_base + t.indexes[1] ); 391 icp[index++] = static_cast< uint16 >( index_base + t.indexes[2] ); 526 392 } 527 528 393 index_base += surface.header.num_verts; 529 394 } 530 395 531 } 532 396 mesh_data* result = new mesh_data(); 397 result->add_channel( mc_pn ); 398 result->add_channel( mc_t ); 399 result->set_index_channel( ic ); 400 return result; 401 } 402 403 tag_map* nv::md3_loader::create_tag_map() 404 { 405 md3_t* md3 = (md3_t*)m_md3; 406 tag_map* result = new tag_map(); 407 for ( sint32 i = 0; i < md3->header.num_tags; ++i ) 408 { 409 const md3_tag_t& rtag = md3->tags[i + md3->header.num_tags]; 410 std::string name( (char*)(rtag.name) ); 411 load_tags( result->get_map()[ name ], name ); 412 } 413 return result; 414 } 415 416 size_t md3_loader::get_max_frames() const 417 { 418 return static_cast< size_t >( ((md3_t*)m_md3)->header.num_frames ); 419 } -
trunk/src/formats/md5_loader.cc
r236 r239 90 90 else if ( command == "mesh" ) 91 91 { 92 md5_mesh* mesh = new md5_mesh; 92 md5_mesh_data* mesh = new md5_mesh_data(); 93 93 94 int num_verts, num_tris, num_weights; 94 95 … … 99 100 if ( command == "shader" ) 100 101 { 101 sstream >> mesh-> shader;102 remove_quotes( mesh-> shader );102 sstream >> mesh->m_shader; 103 remove_quotes( mesh->m_shader ); 103 104 // texturePath.replace_extension( ".tga" ); 104 105 next_line( sstream ); … … 107 108 { 108 109 sstream >> num_verts; 110 111 { 112 mesh_raw_channel* ch_pnt = mesh_raw_channel::create<md5_vtx_pnt>( num_verts ); 113 mesh_raw_channel* ch_t = mesh_raw_channel::create<md5_vtx_t>( num_verts ); 114 mesh->m_pntdata = (md5_vtx_pnt*)ch_pnt->data; 115 mesh->m_tdata = (md5_vtx_t*)ch_t->data; 116 mesh->add_channel( ch_pnt ); 117 mesh->add_channel( ch_t ); 118 } 119 mesh->m_vtx_data.resize( num_verts ); 120 109 121 next_line( sstream ); 110 122 std::string line; 111 123 for ( int i = 0; i < num_verts; ++i ) 112 124 { 113 md5_vertex vert; 125 md5_vtx_data& vdata = mesh->m_vtx_data[i]; 126 size_t weight_count; 127 size_t start_weight; 128 vec2 texcoord; 114 129 115 130 std::getline( sstream, line ); 116 sscanf( line.c_str(), "%*s %*u ( %f %f ) %u %u", &(vert.texcoord.x), &(vert.texcoord.y), &(vert.start_weight), &(vert.weight_count) ); 117 118 // std::string ignore; 119 // discard( sstream, "vert" ); 120 // sstream >> ignore; 121 // discard( sstream, "(" ); 122 // sstream >> vert.texcoord.x >> vert.texcoord.y; 123 // discard( sstream, ")" ); 124 // sstream >> vert.start_weight >> vert.weight_count; 125 // next_line( sstream ); 126 127 mesh->verts.push_back(vert); 128 mesh->texcoord_buffer.push_back( vert.texcoord ); 131 sscanf( line.c_str(), "%*s %*u ( %f %f ) %u %u", &(texcoord.x), &(texcoord.y), &(start_weight), &(weight_count) ); 132 vdata.start_weight = start_weight; 133 vdata.weight_count = weight_count; 134 mesh->m_tdata[i].texcoord = texcoord; 129 135 } 130 136 } … … 132 138 { 133 139 sstream >> num_tris; 140 141 mesh_raw_index_channel* ch_i = mesh_raw_index_channel::create<uint32>( num_tris * 3 ); 142 uint32* vtx_i = (uint32*)ch_i->data; 143 mesh->m_idata = vtx_i; 144 uint32 idx = 0; 145 mesh->set_index_channel( ch_i ); 146 134 147 next_line( sstream ); 135 148 std::string line; 136 149 for ( int i = 0; i < num_tris; ++i ) 137 150 { 138 md5_triangle tri; 151 size_t ti0; 152 size_t ti1; 153 size_t ti2; 139 154 140 155 std::getline( sstream, line ); 141 sscanf( line.c_str(), "%*s %*u %u %u %u )", &(tri.indices[0]), &(tri.indices[1]), &(tri.indices[2])); 142 143 // std::string ignore; 144 // discard( sstream, "tri" ); 145 // sstream >> ignore >> tri.indices[0] >> tri.indices[1] >> tri.indices[2]; 146 // next_line( sstream ); 147 148 mesh->tris.push_back( tri ); 149 mesh->index_buffer.push_back( (uint32)tri.indices[0] ); 150 mesh->index_buffer.push_back( (uint32)tri.indices[1] ); 151 mesh->index_buffer.push_back( (uint32)tri.indices[2] ); 156 sscanf( line.c_str(), "%*s %*u %u %u %u )", &(ti0), &(ti1), &(ti2)); 157 158 vtx_i[idx++] = (uint32)ti0; 159 vtx_i[idx++] = (uint32)ti1; 160 vtx_i[idx++] = (uint32)ti2; 152 161 } 153 162 } … … 155 164 { 156 165 sstream >> num_weights; 157 mesh-> weights.reserve( num_weights );166 mesh->m_weights.reserve( num_weights ); 158 167 next_line( sstream ); 159 168 std::string line; … … 164 173 std::getline( sstream, line ); 165 174 sscanf( line.c_str(), "%*s %*u %u %f ( %f %f %f )", &(weight.joint_id), &(weight.bias), &(weight.pos.x), &(weight.pos.y), &(weight.pos.z)); 166 167 // std::string ignore; 168 // discard( sstream, "weight" ); 169 // sstream >> ignore >> weight.joint_id >> weight.bias; 170 // discard( sstream, "(" ); 171 // sstream >> weight.pos.x >> weight.pos.y >> weight.pos.z; 172 // discard( sstream, ")" ); 173 // next_line( sstream ); 174 mesh->weights.push_back(weight); 175 mesh->m_weights.push_back(weight); 175 176 } 176 177 } … … 184 185 185 186 prepare_mesh( mesh ); 186 prepare_normals( mesh );187 187 188 188 m_meshes.push_back(mesh); … … 196 196 } 197 197 198 bool md5_loader::prepare_mesh( md5_mesh* mesh ) 199 { 200 mesh->position_buffer.clear(); 201 mesh->texcoord_buffer.clear(); 202 203 for ( uint32 i = 0; i < mesh->verts.size(); ++i ) 204 { 205 md5_vertex& vert = mesh->verts[i]; 206 207 vert.position = glm::vec3(0); 208 vert.normal = glm::vec3(0); 209 vert.tangent = glm::vec3(0); 210 211 for ( size_t j = 0; j < vert.weight_count; ++j ) 212 { 213 md5_weight& weight = mesh->weights[vert.start_weight + j]; 198 bool md5_loader::prepare_mesh( md5_mesh_data* mdata ) 199 { 200 uint32 vtx_count = mdata->m_vtx_data.size(); 201 md5_vtx_pnt* vtcs = mdata->m_pntdata; 202 203 for ( uint32 i = 0; i < vtx_count; ++i ) 204 { 205 md5_vtx_data& vdata = mdata->m_vtx_data[i]; 206 md5_vtx_pnt& vtc = vtcs[i]; 207 208 vtc.position = glm::vec3(0); 209 vtc.normal = glm::vec3(0); 210 vtc.tangent = glm::vec3(0); 211 212 for ( size_t j = 0; j < vdata.weight_count; ++j ) 213 { 214 md5_weight& weight = mdata->m_weights[vdata.start_weight + j]; 214 215 md5_joint& joint = m_joints[weight.joint_id]; 215 216 216 217 glm::vec3 rot_pos = joint.orient * weight.pos; 217 218 218 vert.position += ( joint.pos + rot_pos ) * weight.bias; 219 } 220 221 mesh->position_buffer.push_back(vert.position); 222 mesh->texcoord_buffer.push_back(vert.texcoord); 223 } 224 225 return true; 226 } 227 228 bool md5_loader::prepare_normals( md5_mesh* mesh ) 229 { 230 mesh->normal_buffer.clear(); 231 232 for ( unsigned int i = 0; i < mesh->tris.size(); ++i ) 233 { 234 const md5_triangle& tri = mesh->tris[i]; 235 glm::vec3 v1 = mesh->verts[ tri.indices[0] ].position; 236 glm::vec3 v2 = mesh->verts[ tri.indices[1] ].position; 237 glm::vec3 v3 = mesh->verts[ tri.indices[2] ].position; 219 vtc.position += ( joint.pos + rot_pos ) * weight.bias; 220 } 221 } 222 223 // Prepare normals 224 uint32 tri_count = mdata->get_count() / 3; 225 for ( unsigned int i = 0; i < tri_count; ++i ) 226 { 227 uint32 ti0 = mdata->m_idata[ i * 3 ]; 228 uint32 ti1 = mdata->m_idata[ i * 3 + 1 ]; 229 uint32 ti2 = mdata->m_idata[ i * 3 + 2 ]; 230 231 glm::vec3 v1 = vtcs[ ti0 ].position; 232 glm::vec3 v2 = vtcs[ ti1 ].position; 233 glm::vec3 v3 = vtcs[ ti2 ].position; 238 234 glm::vec3 xyz1 = v3 - v1; 239 235 glm::vec3 xyz2 = v2 - v1; … … 241 237 glm::vec3 normal = glm::cross( xyz1, xyz2 ); 242 238 243 mesh->verts[ tri.indices[0]].normal += normal;244 mesh->verts[ tri.indices[1]].normal += normal;245 mesh->verts[ tri.indices[2]].normal += normal;246 247 const vec2& w1 = m esh->verts[ tri.indices[0]].texcoord;248 const vec2& w2 = m esh->verts[ tri.indices[1]].texcoord;249 const vec2& w3 = m esh->verts[ tri.indices[2]].texcoord;239 vtcs[ ti0 ].normal += normal; 240 vtcs[ ti1 ].normal += normal; 241 vtcs[ ti2 ].normal += normal; 242 243 const vec2& w1 = mdata->m_tdata[ ti0 ].texcoord; 244 const vec2& w2 = mdata->m_tdata[ ti1 ].texcoord; 245 const vec2& w3 = mdata->m_tdata[ ti2 ].texcoord; 250 246 251 247 vec2 st1 = w3 - w1; … … 256 252 vec3 tangent = (( xyz1 * st2.y ) - ( xyz2 * st1.y )) * coef; 257 253 258 mesh->verts[ tri.indices[0]].tangent += tangent;259 mesh->verts[ tri.indices[1]].tangent += tangent;260 mesh->verts[ tri.indices[2]].tangent += tangent;261 } 262 263 for ( size_t i = 0; i < mesh->verts.size(); ++i )264 { 265 md5_v ertex& vert = mesh->verts[i];266 267 glm::vec3 normal = glm::normalize( v ert.normal );268 glm::vec3 tangent = glm::normalize( v ert.tangent );269 mesh->normal_buffer.push_back( normal );270 mesh->tangent_buffer.push_back( tangent );271 272 vert.normal = glm::vec3(0);273 vert.tangent = glm::vec3(0);274 275 for ( size_t j = 0; j < vert.weight_count; ++j )276 {277 const md5_weight& weight = mesh->weights[vert.start_weight + j];278 const md5_joint& joint = m_joints[weight.joint_id];279 vert.normal += ( normal * joint.orient ) * weight.bias;280 vert.tangent += ( tangent * joint.orient ) * weight.bias;281 }254 vtcs[ ti0 ].tangent += tangent; 255 vtcs[ ti1 ].tangent += tangent; 256 vtcs[ ti2 ].tangent += tangent; 257 } 258 259 for ( size_t i = 0; i < vtx_count; ++i ) 260 { 261 md5_vtx_data& vdata = mdata->m_vtx_data[i]; 262 263 glm::vec3 normal = glm::normalize( vtcs[i].normal ); 264 glm::vec3 tangent = glm::normalize( vtcs[i].tangent ); 265 vtcs[i].normal = normal; 266 vtcs[i].tangent = tangent; 267 268 vdata.normal = glm::vec3(0); 269 vdata.tangent = glm::vec3(0); 270 271 for ( size_t j = 0; j < vdata.weight_count; ++j ) 272 { 273 const md5_weight& weight = mdata->m_weights[vdata.start_weight + j]; 274 const md5_joint& joint = m_joints[weight.joint_id]; 275 vdata.normal += ( normal * joint.orient ) * weight.bias; 276 vdata.tangent += ( tangent * joint.orient ) * weight.bias; 277 } 282 278 } 283 279 … … 285 281 } 286 282 287 mesh_data_old* nv::md5_loader::release_submesh_data( uint32 mesh_id )288 {289 mesh_data_creator m;290 m.get_positions().assign( m_meshes[mesh_id]->position_buffer.begin(), m_meshes[mesh_id]->position_buffer.begin() );291 m.get_normals() .assign( m_meshes[mesh_id]->normal_buffer.begin(), m_meshes[mesh_id]->normal_buffer.begin() );292 m.get_tangents() .assign( m_meshes[mesh_id]->tangent_buffer.begin(), m_meshes[mesh_id]->tangent_buffer.begin() );293 m.get_texcoords().assign( m_meshes[mesh_id]->texcoord_buffer.begin(), m_meshes[mesh_id]->texcoord_buffer.begin() );294 m.get_indices() .assign( m_meshes[mesh_id]->index_buffer.begin(), m_meshes[mesh_id]->index_buffer.begin() );295 296 return m.release();297 }298 299 /*300 mesh* md5_loader::release_mesh()301 {302 mesh* m = new mesh();303 auto position = m->add_attribute< vec3 >( "nv_position" );304 auto normal = m->add_attribute< vec3 >( "nv_normal" );305 auto texcoord = m->add_attribute< vec2 >( "nv_texcoord" );306 auto tangent = m->add_attribute< vec3 >( "nv_tangent" );307 auto indices = m->add_indices< uint32 >();308 309 position->get().assign( m_meshes[0].position_buffer.begin(), m_meshes[0].position_buffer.end() );310 normal ->get().assign( m_meshes[0].normal_buffer.begin(), m_meshes[0].normal_buffer.end() );311 texcoord->get().assign( m_meshes[0].texcoord_buffer.begin(), m_meshes[0].texcoord_buffer.end() );312 tangent ->get().assign( m_meshes[0].tangent_buffer.begin(), m_meshes[0].tangent_buffer.end() );313 indices ->get().assign( m_meshes[0].index_buffer.begin(), m_meshes[0].index_buffer.end() );314 315 m_size = m_meshes[0].index_buffer.size();316 return m;317 }318 */319 283 320 284 md5_animation::md5_animation() … … 570 534 } 571 535 572 bool md5_loader::prepare_animated_mesh( md5_mesh* mesh, const md5_animation::md5_frame_skeleton& skel ) 573 { 574 for ( unsigned int i = 0; i < mesh->verts.size(); ++i ) 575 { 576 const md5_vertex& vert = mesh->verts[i]; 577 glm::vec3& pos = mesh->position_buffer[i]; 578 glm::vec3& normal = mesh->normal_buffer[i]; 579 glm::vec3& tangent = mesh->tangent_buffer[i]; 580 581 pos = glm::vec3(0); 582 normal = glm::vec3(0); 583 tangent = glm::vec3(0); 536 mesh_data* nv::md5_loader::release_mesh_data( uint32 mesh ) 537 { 538 mesh_data* result = m_meshes[ mesh ]; 539 m_meshes[ mesh ] = nullptr; 540 return result; 541 } 542 543 void nv::md5_mesh_data::apply( const md5_animation& animation ) 544 { 545 const md5_animation::md5_frame_skeleton& skeleton = animation.get_skeleton(); 546 547 for ( unsigned int i = 0; i < m_vtx_data.size(); ++i ) 548 { 549 const md5_vtx_data& vert = m_vtx_data[i]; 550 md5_vtx_pnt& result = m_pntdata[i]; 551 552 result.position = glm::vec3(0); 553 result.normal = glm::vec3(0); 554 result.tangent = glm::vec3(0); 584 555 585 556 for ( size_t j = 0; j < vert.weight_count; ++j ) 586 557 { 587 const md5_weight& weight = m esh->weights[vert.start_weight + j];588 const md5_animation::md5_skeleton_joint& joint = skel .joints[weight.joint_id];558 const md5_weight& weight = m_weights[vert.start_weight + j]; 559 const md5_animation::md5_skeleton_joint& joint = skeleton.joints[weight.joint_id]; 589 560 590 561 glm::vec3 rot_pos = joint.orient * weight.pos; 591 pos += ( joint.pos + rot_pos ) * weight.bias; 592 593 normal += ( joint.orient * vert.normal ) * weight.bias; 594 tangent += ( joint.orient * vert.tangent ) * weight.bias; 595 } 596 } 597 return true; 598 } 599 600 void md5_loader::apply( const md5_animation& animation ) 601 { 602 const md5_animation::md5_frame_skeleton& skeleton = animation.get_skeleton(); 603 604 for ( unsigned int i = 0; i < m_meshes.size(); ++i ) 605 { 606 prepare_animated_mesh( m_meshes[i], skeleton ); 607 } 608 } 609 610 size_t nv::md5_loader::get_size() 611 { 612 return m_size; 613 } 562 result.position += ( joint.pos + rot_pos ) * weight.bias; 563 564 result.normal += ( joint.orient * vert.normal ) * weight.bias; 565 result.tangent += ( joint.orient * vert.tangent ) * weight.bias; 566 } 567 } 568 } 569 570 nv::md5_loader::~md5_loader() 571 { 572 for ( auto m : m_meshes ) { if (m) delete m; } 573 } -
trunk/src/formats/obj_loader.cc
r238 r239 56 56 bool read_stream( std::istream& stream ); 57 57 virtual size_t add_face( uint32* vi, uint32* ti, uint32* ni, size_t count ) = 0; 58 virtual size_t raw_size() { return 0; }59 virtual const uint8* raw_pointer() { return nullptr; }58 virtual size_t raw_size() const = 0; 59 virtual const uint8* raw_pointer() const = 0; 60 60 virtual void calculate_tangents() {} 61 61 … … 151 151 152 152 153 struct mesh_obj_reader : public obj_reader154 {155 mesh_obj_reader( mesh_data_creator* m ) : m_mesh( m ) {}156 virtual std::size_t add_face( uint32* vi, uint32* ti, uint32* ni, size_t count );157 virtual void calculate_tangents();158 159 mesh_data_creator* m_mesh;160 };161 162 size_t mesh_obj_reader::add_face( uint32* vi, uint32* ti, uint32* ni, size_t count )163 {164 if ( count < 3 )165 {166 // TODO : report error?167 return 0;168 }169 170 // TODO : support if normals not present;171 172 std::vector< vec3 >& vp = m_mesh->get_positions();173 std::vector< vec3 >& vn = m_mesh->get_normals();174 std::vector< vec2 >& vt = m_mesh->get_texcoords();175 176 std::size_t result = 0;177 178 // Simple triangulation - obj's shouldn't have more than quads anyway179 for ( size_t i = 2; i < count; ++i )180 {181 result++;182 vp.push_back( v[ vi[ 0 ] ] ); vt.push_back( t[ ti[ 0 ] ] ); vn.push_back( n[ ni[ 0 ] ] );183 vp.push_back( v[ vi[ i-1 ] ] ); vt.push_back( t[ ti[ i-1 ] ] ); vn.push_back( n[ ni[ i-1 ] ] );184 vp.push_back( v[ vi[ i ] ] ); vt.push_back( t[ ti[ i ] ] ); vn.push_back( n[ ni[ i ] ] );185 }186 187 return result;188 }189 190 // based on http://www.terathon.com/code/tangent.html191 void mesh_obj_reader::calculate_tangents()192 {193 const std::vector< vec3 >& vp = m_mesh->get_positions();194 const std::vector< vec2 >& vt = m_mesh->get_texcoords();195 const std::vector< vec3 >& vn = m_mesh->get_normals();196 std::vector< vec3 >& tg = m_mesh->get_tangents();197 198 size_t count = vp.size();199 size_t tcount = count / 3;200 201 std::vector< vec3 > tan1( count );202 std::vector< vec3 > tan2( count );203 tg.resize( count );204 205 for (size_t a = 0; a < tcount; ++a )206 {207 size_t i1 = a * 3;208 size_t i2 = a * 3 + 1;209 size_t i3 = a * 3 + 2;210 211 // TODO: simplify212 213 const vec3& v1 = vp[i1];214 const vec3& v2 = vp[i2];215 const vec3& v3 = vp[i3];216 217 const vec2& w1 = vt[i1];218 const vec2& w2 = vt[i2];219 const vec2& w3 = vt[i3];220 221 vec3 xyz1 = v2 - v1;222 vec3 xyz2 = v3 - v1;223 //vec2 st1 = w2 - w1;224 //vec2 st2 = w3 - w1;225 226 float s1 = w2.x - w1.x;227 float t1 = w2.y - w1.y;228 float s2 = w3.x - w1.x;229 float t2 = w3.y - w1.y;230 231 float stst = s1 * t2 - s2 * t1;232 float r = 0.0f;233 if (stst > 0.0f || stst < 0.0f) r = 1.0f / stst;234 235 vec3 sdir = ( t2 * xyz1 - t1 * xyz2 ) * r;236 vec3 tdir = ( s1 * xyz2 - s2 * xyz1 ) * r;237 238 // the += below obviously doesn't make sense in this case, but I'll239 // leave it here for when I move to indices240 tan1[i1] += sdir;241 tan1[i2] += sdir;242 tan1[i3] += sdir;243 244 // tan2 not needed anymore??245 tan2[i1] += tdir;246 tan2[i2] += tdir;247 tan2[i3] += tdir;248 }249 250 for (std::size_t a = 0; a < count; ++a )251 {252 const vec3& n = vn[a];253 const vec3& t = tan1[a];254 if ( ! (t.x == 0.0f && t.y == 0.0f && t.z == 0.0f) )255 tg[a] = vec3( glm::normalize(t - n * glm::dot( n, t )) );256 //tg[a][3] = (glm::dot(glm::cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f;257 }258 259 }260 261 nv::obj_loader::obj_loader( bool tangents )262 : m_mesh( nullptr ), m_tangents( tangents )263 {264 265 }266 267 nv::obj_loader::~obj_loader()268 {269 delete m_mesh;270 }271 272 bool nv::obj_loader::load( stream& source )273 {274 if ( m_mesh != nullptr ) delete m_mesh;275 mesh_data_creator creator;276 mesh_obj_reader reader( &creator );277 std_stream sstream( &source );278 reader.read_stream( sstream );279 m_size = reader.size;280 if ( m_tangents )281 {282 reader.calculate_tangents();283 }284 m_mesh = creator.release();285 return true;286 }287 153 288 154 struct mesh_data_reader_vt : public obj_reader … … 305 171 } 306 172 std::vector< obj_vertex_vt > m_data; 307 virtual size_t raw_size() { return m_data.size() * sizeof( obj_vertex_vt ); }308 virtual const uint8* raw_pointer() { return (const uint8*)m_data.data(); }173 virtual size_t raw_size() const { return m_data.size() * sizeof( obj_vertex_vt ); } 174 virtual const uint8* raw_pointer() const { return (const uint8*)m_data.data(); } 309 175 }; 310 176 … … 328 194 } 329 195 std::vector< obj_vertex_vtn > m_data; 330 virtual size_t raw_size() { return m_data.size() * sizeof( obj_vertex_vtn ); }331 virtual const uint8* raw_pointer() { return (const uint8*)m_data.data(); }196 virtual size_t raw_size() const { return m_data.size() * sizeof( obj_vertex_vtn ); } 197 virtual const uint8* raw_pointer() const { return (const uint8*)m_data.data(); } 332 198 }; 333 199 … … 351 217 } 352 218 std::vector< obj_vertex_vtnt > m_data; 353 virtual size_t raw_size() { return m_data.size() * sizeof( obj_vertex_vtnt ); }354 virtual const uint8* raw_pointer() { return (const uint8*)m_data.data(); }219 virtual size_t raw_size() const { return m_data.size() * sizeof( obj_vertex_vtnt ); } 220 virtual const uint8* raw_pointer() const { return (const uint8*)m_data.data(); } 355 221 356 222 // based on http://www.terathon.com/code/tangent.html … … 423 289 }; 424 290 425 nv:: wavefront_loader::wavefront_loader( bool normals /*= true*/, bool tangents /*= false */ )291 nv::obj_loader::obj_loader( bool normals /*= true*/, bool tangents /*= false */ ) 426 292 : m_normals( normals ), m_tangents( tangents ), m_mesh( nullptr ) 427 293 { … … 437 303 } 438 304 439 bool nv:: wavefront_loader::load( stream& source )305 bool nv::obj_loader::load( stream& source ) 440 306 { 441 307 if ( m_mesh ) delete m_mesh; … … 462 328 mesh_raw_channel* channel = new mesh_raw_channel(); 463 329 nv::uint8* data = nullptr; 330 464 331 if ( reader->raw_size() > 0 ) 465 332 { … … 479 346 } 480 347 481 mesh_data* nv:: wavefront_loader::release_mesh_data()348 mesh_data* nv::obj_loader::release_mesh_data() 482 349 { 483 350 mesh_data* result = m_mesh; … … 486 353 } 487 354 488 nv:: wavefront_loader::~wavefront_loader()355 nv::obj_loader::~obj_loader() 489 356 { 490 357 if ( m_mesh ) delete m_mesh;
Note: See TracChangeset
for help on using the changeset viewer.