Changeset 149 for trunk/src/formats
- Timestamp:
- 07/07/13 17:34:37 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/formats/md3_loader.cc
r148 r149 100 100 sint16 y; 101 101 sint16 z; 102 sint16 normal;102 uint16 normal; 103 103 }; 104 104 … … 118 118 md3_tag_t* tags; 119 119 md3_surface_t* surfaces; 120 // extra information (not in md3 file) 121 sint32 vertices_per_frame; 120 122 }; 121 123 … … 208 210 209 211 source.seek( md3->header.ofs_surfaces, origin::SET ); 212 md3->vertices_per_frame = 0; 210 213 211 214 for ( sint32 i = 0; i < md3->header.num_surfaces; ++i ) … … 213 216 if ( !read_surface( md3->surfaces + i, source ) ) return false; 214 217 if ( md3->header.num_frames != md3->surfaces[i].header.num_frames ) return false; 218 md3->vertices_per_frame += md3->surfaces[i].header.num_verts; 215 219 } 216 220 return true; … … 228 232 } 229 233 230 static inline vec3 md3_vec3( const md3_vertex_t& v ) 231 { 232 // return vec3( v.x * MD3_XYZ_SCALE, v.y * MD3_XYZ_SCALE, v.z * MD3_XYZ_SCALE ); 233 return vec3( v.x * MD3_XYZ_SCALE, v.z * MD3_XYZ_SCALE, v.y * MD3_XYZ_SCALE ); 234 } 235 236 static inline vec3 md3_normal( const md3_vertex_t& v ) 237 { 238 float pi = glm::pi<float>(); 239 float lat = (((v.normal >> 8) & 255) * (2 * pi)) / 255.0f; 240 float lng = ((v.normal & 255) * (2 * pi)) / 255.0f; 241 return vec3( 242 glm::cos( lat ) * glm::sin( lng ), 243 // glm::sin( lat ) * glm::sin( lng ), 244 // glm::cos( lng ) 245 glm::cos( lng ), 246 glm::sin( lat ) * glm::sin( lng ) 247 ); 248 } 249 234 static vec3 s_normal_cache[256*256]; 235 static bool s_normal_ready = false; 250 236 251 237 md3_loader::md3_loader() 252 238 : m_md3( nullptr ), m_size( 0 ) 253 239 { 254 240 if ( !s_normal_ready ) 241 { 242 float pi = glm::pi<float>(); 243 float convert = (2 * pi) / 255.0f; 244 int n = 0; 245 for ( int lat = 0; lat < 256; ++lat ) 246 { 247 float flat = lat * convert; 248 float sin_lat = glm::sin( flat ); 249 float cos_lat = glm::cos( flat ); 250 for ( int lng = 0; lng < 256; ++lng, ++n ) 251 { 252 float flng = lng * convert; 253 float sin_lng = glm::sin( flng ); 254 float cos_lng = glm::cos( flng ); 255 s_normal_cache[n].x = cos_lat * sin_lng; 256 // s_normal_cache[n].y = sin_lat * sin_lng; 257 // s_normal_cache[n].z = cos_lng; 258 s_normal_cache[n].z = sin_lat * sin_lng; 259 s_normal_cache[n].y = cos_lng; 260 } 261 } 262 263 s_normal_ready = true; 264 } 255 265 } 256 266 … … 318 328 } 319 329 330 void nv::md3_loader::load_tags( std::vector<mat4>& t, const std::string& tag ) 331 { 332 md3_t* md3 = (md3_t*)m_md3; 333 t.clear(); 334 for ( sint32 f = 0; f < md3->header.num_frames; ++f ) 335 { 336 for ( sint32 i = 0; i < md3->header.num_tags; ++i ) 337 { 338 const md3_tag_t& rtag = md3->tags[i + md3->header.num_tags * f]; 339 std::string rname((char*)(rtag.name)); 340 if (rname == tag) 341 { 342 vec4 axisx = vec4( md3_vec3( rtag.axis[0] ), 0.0 ); 343 vec4 axisz = vec4( md3_vec3( rtag.axis[1] ), 0.0 ); 344 vec4 axisy = vec4( md3_vec3( rtag.axis[2] ), 0.0 ); 345 vec4 origin = vec4( md3_vec3( rtag.origin ), 1.0 ); 346 t.emplace_back( axisx, axisy, axisz, origin ); 347 } 348 } 349 350 } 351 } 352 353 sint32 nv::md3_loader::get_max_frames() const 354 { 355 return ((md3_t*)m_md3)->header.num_frames; 356 } 357 320 358 mat4 md3_loader::get_tag( sint32 frame, const std::string& name ) const 321 359 { … … 352 390 sint32 frame_count = ( frame == -1 ? md3->header.num_frames : 1 ); 353 391 392 p.reserve( md3->vertices_per_frame * frame_count ); 393 354 394 while ( frame_count > 0 ) 355 395 { … … 359 399 sint32 vcount = surface.header.num_verts; 360 400 sint32 offset = vcount * current_frame; 361 p.reserve( p.size() + vcount );362 for (sint32 j = 0; j < vcount; ++j )401 sint32 limit = vcount + offset; 402 for (sint32 j = offset; j < limit; ++j ) 363 403 { 364 p.push_back( md3_vec3( surface.vertices[j + offset] ) ); 404 md3_vertex_t& v = surface.vertices[j]; 405 p.push_back( vec3( v.x * MD3_XYZ_SCALE, v.z * MD3_XYZ_SCALE, v.y * MD3_XYZ_SCALE ) ); 365 406 } 366 407 } … … 378 419 sint32 frame_count = ( frame == -1 ? md3->header.num_frames : 1 ); 379 420 421 n.reserve( md3->vertices_per_frame * frame_count ); 422 380 423 while ( frame_count > 0 ) 381 424 { … … 385 428 sint32 vcount = surface.header.num_verts; 386 429 sint32 offset = vcount * current_frame; 387 n.reserve( n.size() + vcount );388 for (sint32 j = 0; j < vcount; ++j )430 sint32 limit = vcount + offset; 431 for (sint32 j = offset; j < limit; ++j ) 389 432 { 390 n.push_back( md3_normal( surface.vertices[j + offset] ));433 n.push_back( s_normal_cache[ surface.vertices[j].normal ] ); 391 434 } 392 435 }
Note: See TracChangeset
for help on using the changeset viewer.