Changeset 149 for trunk/src/formats


Ignore:
Timestamp:
07/07/13 17:34:37 (12 years ago)
Author:
epyon
Message:
  • time - choice of uint32/uint64 depending on requested precision in both functions and template util classes
  • md3_loader - severe performance improvements
  • md3_loader - tag transform array loading
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/formats/md3_loader.cc

    r148 r149  
    100100        sint16 y;
    101101        sint16 z;
    102         sint16 normal;
     102        uint16 normal;
    103103};
    104104
     
    118118        md3_tag_t*     tags;
    119119        md3_surface_t* surfaces;
     120        // extra information (not in md3 file)
     121        sint32         vertices_per_frame;
    120122};
    121123
     
    208210
    209211        source.seek( md3->header.ofs_surfaces, origin::SET );
     212        md3->vertices_per_frame = 0;
    210213
    211214        for ( sint32 i = 0; i < md3->header.num_surfaces; ++i )
     
    213216                if ( !read_surface( md3->surfaces + i, source ) ) return false;
    214217                if ( md3->header.num_frames != md3->surfaces[i].header.num_frames ) return false;
     218                md3->vertices_per_frame += md3->surfaces[i].header.num_verts;
    215219        }
    216220        return true;
     
    228232}
    229233
    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 
     234static vec3 s_normal_cache[256*256];
     235static bool s_normal_ready = false;
    250236
    251237md3_loader::md3_loader()
    252238        : m_md3( nullptr ), m_size( 0 )
    253239{
    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        }
    255265}
    256266
     
    318328}
    319329
     330void 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
     353sint32 nv::md3_loader::get_max_frames() const
     354{
     355        return ((md3_t*)m_md3)->header.num_frames;
     356}
     357
    320358mat4 md3_loader::get_tag( sint32 frame, const std::string& name ) const
    321359{
     
    352390        sint32 frame_count   = ( frame == -1 ? md3->header.num_frames : 1 );
    353391
     392        p.reserve( md3->vertices_per_frame * frame_count );
     393
    354394        while ( frame_count > 0 )
    355395        {
     
    359399                        sint32         vcount  = surface.header.num_verts;
    360400                        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 )
    363403                        {
    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 ) );
    365406                        }
    366407                }
     
    378419        sint32 frame_count   = ( frame == -1 ? md3->header.num_frames : 1 );
    379420
     421        n.reserve( md3->vertices_per_frame * frame_count );
     422
    380423        while ( frame_count > 0 )
    381424        {
     
    385428                        sint32         vcount  = surface.header.num_verts;
    386429                        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 )
    389432                        {
    390                                 n.push_back( md3_normal( surface.vertices[j + offset] ) );
     433                                n.push_back( s_normal_cache[ surface.vertices[j].normal ] );
    391434                        }
    392435                }
Note: See TracChangeset for help on using the changeset viewer.