Changeset 224 for trunk/src


Ignore:
Timestamp:
01/02/14 20:52:34 (11 years ago)
Author:
epyon
Message:
  • universal mesh format
  • removed keyframed_mesh_data
Location:
trunk/src
Files:
5 edited

Legend:

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

    r204 r224  
    241241}
    242242
     243mesh_data* 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
     256mesh_data* 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/*
    243271mesh* nv::md2_loader::release_mesh()
    244272{
     
    255283        vertex_attribute< vec3 >*   normal   = m->add_attribute<vec3>("nv_normal");
    256284        vertex_attribute< vec2 >*   texcoord = m->add_attribute<vec2>("nv_texcoord");
    257         vertex_attribute< uint16 >* indices  = m->add_indices<uint16>();
     285        vertex_attribute< uint32 >* indices  = m->add_indices<uint32>();
    258286
    259287        load_positions( position->get(), frame );
     
    266294        return m;
    267295}
     296*/
    268297
    269298size_t md2_loader::get_max_frames() const
     
    344373}
    345374
    346 void md2_loader::load_indicies( std::vector<uint16>& idx )
     375void md2_loader::load_indicies( std::vector<uint32>& idx )
    347376{
    348377        idx.assign( m_new_indexes.begin(), m_new_indexes.end() );
  • trunk/src/formats/md3_loader.cc

    r200 r224  
    286286}
    287287
     288/*
    288289mesh* nv::md3_loader::release_mesh()
    289290{
     
    314315        vertex_attribute< vec3 >* normal   = m->add_attribute<vec3>("nv_normal");
    315316        vertex_attribute< vec2 >* texcoord = m->add_attribute<vec2>("nv_texcoord");
    316         vertex_attribute< uint16 >* indices  = m->add_indices<uint16>();
     317        vertex_attribute< uint32 >* indices  = m->add_indices<uint32>();
    317318
    318319        load_positions( position->get(), frame );
     
    325326        return m;
    326327}
     328*/
    327329
    328330void nv::md3_loader::load_tags( std::vector<mat4>& t, const std::string& tag )
     
    349351}
    350352
     353mesh_data* 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
     373mesh_data* 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
    351392size_t md3_loader::get_max_frames() const
    352393{
     
    465506}
    466507
    467 void md3_loader::load_indicies( std::vector<uint16>& idx )
     508void md3_loader::load_indicies( std::vector<uint32>& idx )
    468509{
    469510        md3_t* md3 = (md3_t*)m_md3;
     
    480521                {
    481522                        const md3_triangle_t& t = surface.triangles[j];
    482                         idx.push_back( static_cast< uint16 >( index_base + t.indexes[0] ) );
    483                         idx.push_back( static_cast< uint16 >( index_base + t.indexes[1] ) );
    484                         idx.push_back( static_cast< uint16 >( index_base + t.indexes[2] ) );
     523                        idx.push_back( static_cast< uint32 >( index_base + t.indexes[0] ) );
     524                        idx.push_back( static_cast< uint32 >( index_base + t.indexes[1] ) );
     525                        idx.push_back( static_cast< uint32 >( index_base + t.indexes[2] ) );
    485526                }
    486527
     
    490531}
    491532
    492 keyframed_mesh_data::keyframed_mesh_data( keyframed_loader* loader )
    493 {
    494         loader->load_positions( m_positions );
    495         loader->load_normals( m_normals );
    496         loader->load_texcoords( m_texcoords );
    497         loader->load_indicies( m_indices );
    498 
    499         std::vector< std::string > names;
    500 
    501         md3_loader* md3loader = dynamic_cast< md3_loader* >( loader );
    502         if ( md3loader != nullptr )
    503         {
    504                 md3loader->load_tag_names( names );
    505                 for ( auto& name : names )
    506                 {
    507                         md3loader->load_tags( m_tags[ name ], name );
    508                 }
    509         }
    510 
    511         m_frames = loader->get_max_frames();
    512 }
    513 
  • trunk/src/formats/md5_loader.cc

    r200 r224  
    9090                {
    9191                        // TODO : efficiency dammit
    92                         md5_mesh mesh;
     92                        md5_mesh* mesh = new md5_mesh;
    9393                        int num_verts, num_tris, num_weights;
    9494
     
    9999                                if ( command == "shader" )
    100100                                {
    101                                         sstream >> mesh.shader;
    102                                         remove_quotes( mesh.shader );
     101                                        sstream >> mesh->shader;
     102                                        remove_quotes( mesh->shader );
    103103                                        // texturePath.replace_extension( ".tga" );
    104104                                        next_line( sstream );
     
    120120                                                next_line( sstream );
    121121
    122                                                 mesh.verts.push_back(vert);
    123                                                 mesh.texcoord_buffer.push_back( vert.texcoord );
     122                                                mesh->verts.push_back(vert);
     123                                                mesh->texcoord_buffer.push_back( vert.texcoord );
    124124                                        } 
    125125                                }
     
    136136                                                next_line( sstream );
    137137
    138                                                 mesh.tris.push_back( tri );
    139                                                 mesh.index_buffer.push_back( (uint32)tri.indices[0] );
    140                                                 mesh.index_buffer.push_back( (uint32)tri.indices[1] );
    141                                                 mesh.index_buffer.push_back( (uint32)tri.indices[2] );
     138                                                mesh->tris.push_back( tri );
     139                                                mesh->index_buffer.push_back( (uint32)tri.indices[0] );
     140                                                mesh->index_buffer.push_back( (uint32)tri.indices[1] );
     141                                                mesh->index_buffer.push_back( (uint32)tri.indices[2] );
    142142                                        }             
    143143                                }
     
    156156                                                discard( sstream, ")" );
    157157                                                next_line( sstream );
    158                                                 mesh.weights.push_back(weight);
     158                                                mesh->weights.push_back(weight);
    159159                                        }
    160160                                }
     
    180180}
    181181
    182 bool md5_loader::prepare_mesh( md5_mesh& mesh )
    183 {
    184         mesh.position_buffer.clear();
    185         mesh.texcoord_buffer.clear();
    186 
    187         for ( uint32 i = 0; i < mesh.verts.size(); ++i )
    188         {
    189                 md5_vertex& vert = mesh.verts[i];
     182bool md5_loader::prepare_mesh( md5_mesh* mesh )
     183{
     184        mesh->position_buffer.clear();
     185        mesh->texcoord_buffer.clear();
     186
     187        for ( uint32 i = 0; i < mesh->verts.size(); ++i )
     188        {
     189                md5_vertex& vert = mesh->verts[i];
    190190
    191191                vert.position = glm::vec3(0);
     
    195195                for ( size_t j = 0; j < vert.weight_count; ++j )
    196196                {
    197                         md5_weight& weight = mesh.weights[vert.start_weight + j];
     197                        md5_weight& weight = mesh->weights[vert.start_weight + j];
    198198                        md5_joint&  joint  = m_joints[weight.joint_id];
    199199
     
    203203                }
    204204
    205                 mesh.position_buffer.push_back(vert.position);
    206                 mesh.texcoord_buffer.push_back(vert.texcoord);
     205                mesh->position_buffer.push_back(vert.position);
     206                mesh->texcoord_buffer.push_back(vert.texcoord);
    207207        }
    208208
     
    210210}
    211211
    212 bool md5_loader::prepare_normals( md5_mesh& mesh )
    213 {
    214         mesh.normal_buffer.clear();
    215 
    216         for ( unsigned int i = 0; i < mesh.tris.size(); ++i )
    217         {
    218                 const md5_triangle& tri = mesh.tris[i];
    219                 glm::vec3 v1 = mesh.verts[ tri.indices[0] ].position;
    220                 glm::vec3 v2 = mesh.verts[ tri.indices[1] ].position;
    221                 glm::vec3 v3 = mesh.verts[ tri.indices[2] ].position;
     212bool md5_loader::prepare_normals( md5_mesh* mesh )
     213{
     214        mesh->normal_buffer.clear();
     215
     216        for ( unsigned int i = 0; i < mesh->tris.size(); ++i )
     217        {
     218                const md5_triangle& tri = mesh->tris[i];
     219                glm::vec3 v1 = mesh->verts[ tri.indices[0] ].position;
     220                glm::vec3 v2 = mesh->verts[ tri.indices[1] ].position;
     221                glm::vec3 v3 = mesh->verts[ tri.indices[2] ].position;
    222222                glm::vec3 xyz1 = v3 - v1;
    223223                glm::vec3 xyz2 = v2 - v1;
     
    225225                glm::vec3 normal = glm::cross( xyz1, xyz2 );
    226226
    227                 mesh.verts[ tri.indices[0] ].normal += normal;
    228                 mesh.verts[ tri.indices[1] ].normal += normal;
    229                 mesh.verts[ tri.indices[2] ].normal += normal;
    230 
    231                 const vec2& w1 = mesh.verts[ tri.indices[0] ].texcoord;
    232                 const vec2& w2 = mesh.verts[ tri.indices[1] ].texcoord;
    233                 const vec2& w3 = mesh.verts[ tri.indices[2] ].texcoord;
     227                mesh->verts[ tri.indices[0] ].normal += normal;
     228                mesh->verts[ tri.indices[1] ].normal += normal;
     229                mesh->verts[ tri.indices[2] ].normal += normal;
     230
     231                const vec2& w1 = mesh->verts[ tri.indices[0] ].texcoord;
     232                const vec2& w2 = mesh->verts[ tri.indices[1] ].texcoord;
     233                const vec2& w3 = mesh->verts[ tri.indices[2] ].texcoord;
    234234
    235235                vec2 st1 = w3 - w1;
     
    240240                vec3 tangent = (( xyz1 * st2.y ) - ( xyz2 * st1.y )) * coef;
    241241
    242                 mesh.verts[ tri.indices[0] ].tangent += tangent;
    243                 mesh.verts[ tri.indices[1] ].tangent += tangent;
    244                 mesh.verts[ tri.indices[2] ].tangent += tangent;
    245         }
    246 
    247         for ( size_t i = 0; i < mesh.verts.size(); ++i )
    248         {
    249                 md5_vertex& vert = mesh.verts[i];
     242                mesh->verts[ tri.indices[0] ].tangent += tangent;
     243                mesh->verts[ tri.indices[1] ].tangent += tangent;
     244                mesh->verts[ tri.indices[2] ].tangent += tangent;
     245        }
     246
     247        for ( size_t i = 0; i < mesh->verts.size(); ++i )
     248        {
     249                md5_vertex& vert = mesh->verts[i];
    250250
    251251                glm::vec3 normal  = glm::normalize( vert.normal );
    252252                glm::vec3 tangent = glm::normalize( vert.tangent );
    253                 mesh.normal_buffer.push_back( normal );
    254                 mesh.tangent_buffer.push_back( tangent );
     253                mesh->normal_buffer.push_back( normal );
     254                mesh->tangent_buffer.push_back( tangent );
    255255
    256256                vert.normal  = glm::vec3(0);
     
    259259                for ( size_t j = 0; j < vert.weight_count; ++j )
    260260                {
    261                         const md5_weight& weight = mesh.weights[vert.start_weight + j];
     261                        const md5_weight& weight = mesh->weights[vert.start_weight + j];
    262262                        const md5_joint&  joint  = m_joints[weight.joint_id];
    263263                        vert.normal  += ( normal  * joint.orient ) * weight.bias;
     
    269269}
    270270
     271mesh_data* nv::md5_loader::release_submesh_data( uint32 mesh_id )
     272{
     273        mesh_data_creator m;
     274        m.get_positions().assign( m_meshes[mesh_id]->position_buffer.begin(), m_meshes[mesh_id]->position_buffer.begin() );
     275        m.get_normals()  .assign( m_meshes[mesh_id]->normal_buffer.begin(),   m_meshes[mesh_id]->normal_buffer.begin() );
     276        m.get_tangents() .assign( m_meshes[mesh_id]->tangent_buffer.begin(),  m_meshes[mesh_id]->tangent_buffer.begin() );
     277        m.get_texcoords().assign( m_meshes[mesh_id]->texcoord_buffer.begin(), m_meshes[mesh_id]->texcoord_buffer.begin() );
     278        m.get_indices()  .assign( m_meshes[mesh_id]->index_buffer.begin(),    m_meshes[mesh_id]->index_buffer.begin() );
     279
     280        return m.release();
     281}
     282
     283/*
    271284mesh* md5_loader::release_mesh()
    272285{
     
    287300        return m;
    288301}
     302*/
    289303
    290304md5_animation::md5_animation()
     
    539553}
    540554
    541 bool md5_loader::prepare_animated_mesh( md5_mesh& mesh, const md5_animation::md5_frame_skeleton& skel )
    542 {
    543         for ( unsigned int i = 0; i < mesh.verts.size(); ++i )
    544         {
    545                 const md5_vertex& vert = mesh.verts[i];
    546                 glm::vec3& pos     = mesh.position_buffer[i];
    547                 glm::vec3& normal  = mesh.normal_buffer[i];
    548                 glm::vec3& tangent = mesh.tangent_buffer[i];
     555bool md5_loader::prepare_animated_mesh( md5_mesh* mesh, const md5_animation::md5_frame_skeleton& skel )
     556{
     557        for ( unsigned int i = 0; i < mesh->verts.size(); ++i )
     558        {
     559                const md5_vertex& vert = mesh->verts[i];
     560                glm::vec3& pos     = mesh->position_buffer[i];
     561                glm::vec3& normal  = mesh->normal_buffer[i];
     562                glm::vec3& tangent = mesh->tangent_buffer[i];
    549563
    550564                pos     = glm::vec3(0);
     
    554568                for ( size_t j = 0; j < vert.weight_count; ++j )
    555569                {
    556                         const md5_weight& weight = mesh.weights[vert.start_weight + j];
     570                        const md5_weight& weight = mesh->weights[vert.start_weight + j];
    557571                        const md5_animation::md5_skeleton_joint& joint = skel.joints[weight.joint_id];
    558572
  • trunk/src/formats/obj_loader.cc

    r204 r224  
    119119struct mesh_obj_reader : public obj_reader
    120120{
    121         mesh_obj_reader( mesh* m ) : m_position( nullptr ), m_normal( nullptr ), m_tex_coord( nullptr ), m_tangent( nullptr ), m_mesh( m ) {}
     121        mesh_obj_reader( mesh_data_creator* m ) : m_mesh( m ) {}
    122122        virtual std::size_t add_face( uint32* v, uint32* t, uint32* n, size_t count );
    123123        virtual void calculate_tangents();
    124124
    125         vertex_attribute< vec3 >* m_position;
    126         vertex_attribute< vec3 >* m_normal;
    127         vertex_attribute< vec2 >* m_tex_coord;
    128         vertex_attribute< vec4 >* m_tangent;
    129         mesh* m_mesh;
     125        mesh_data_creator* m_mesh;
    130126};
    131127
     
    138134        }
    139135
    140         if ( m_position == nullptr )
    141         {
    142                 m_position  = m_mesh->add_attribute< vec3 >( "nv_position" );
    143         }
    144         if ( m_tex_coord == nullptr )
    145         {
    146                 m_tex_coord = m_mesh->add_attribute< vec2 >( "nv_texcoord" );
    147         }
    148         if ( m_normal == nullptr && ni != nullptr )
    149         {
    150                 m_normal = m_mesh->add_attribute< vec3 >( "nv_normal" );
    151         }
    152 
    153136        // TODO : support if normals not present;
    154137
    155         std::vector< vec3 >& vp = m_position->get();
    156         std::vector< vec2 >& vt = m_tex_coord->get();
    157         std::vector< vec3 >& vn = m_normal->get();
     138        std::vector< vec3 >& vp = m_mesh->get_positions();
     139        std::vector< vec3 >& vn = m_mesh->get_normals();
     140        std::vector< vec2 >& vt = m_mesh->get_texcoords();
    158141
    159142        std::size_t result = 0;
     
    174157void mesh_obj_reader::calculate_tangents()
    175158{
    176         m_tangent = m_mesh->add_attribute< vec4 >( "nv_tangent" );
    177 
    178         std::vector< vec3 >& vp = m_position->get();
    179         std::vector< vec2 >& vt = m_tex_coord->get();
    180         std::vector< vec3 >& vn = m_normal->get();
    181         std::vector< vec4 >& tg = m_tangent->get();
     159        const std::vector< vec3 >& vp = m_mesh->get_positions();
     160        const std::vector< vec2 >& vt = m_mesh->get_texcoords();
     161        const std::vector< vec3 >& vn = m_mesh->get_normals();
     162        std::vector< vec3 >& tg = m_mesh->get_tangents();
    182163
    183164        size_t count  = vp.size();
     
    225206                tan1[i3] += sdir;
    226207
     208                // tan2 not needed anymore??
    227209                tan2[i1] += tdir;
    228210                tan2[i2] += tdir;
     
    235217                const vec3& t = tan1[a];
    236218
    237                 tg[a] = vec4( glm::normalize(t - n * glm::dot( n, t )),
    238                     (glm::dot(glm::cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f );
     219                tg[a] = vec3( glm::normalize(t - n * glm::dot( n, t )) );
     220                //tg[a][3] =    (glm::dot(glm::cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f;
    239221        }
    240222
     
    258240                delete m_mesh;
    259241        }
    260         m_mesh = new mesh();
    261         mesh_obj_reader reader( m_mesh );
     242        mesh_data_creator creator;
     243        mesh_obj_reader reader( &creator );
    262244        std_stream sstream( &source );
    263245        reader.read_stream( sstream );
     
    267249                reader.calculate_tangents();
    268250        }
     251        m_mesh = creator.release();
    269252        return true;
    270253}
  • trunk/src/gfx/keyframed_mesh.cc

    r223 r224  
    1515using namespace nv;
    1616
    17 keyframed_mesh::keyframed_mesh( context* a_context, keyframed_mesh_data* a_data, program* a_program )
     17keyframed_mesh::keyframed_mesh( context* a_context, mesh_data* a_data, program* a_program )
    1818        : m_context( a_context )
    1919        , m_data( a_data )
     
    112112}
    113113
    114 keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, keyframed_mesh_data* a_data, program* a_program )
     114keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, mesh_data* a_data, program* a_program )
    115115        : keyframed_mesh( a_context, a_data, a_program )
    116116        , m_loc_next_position( 0 )
     
    133133        vb = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec2 ), (void*)m_data->get_texcoords().data() );
    134134        m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
    135         nv::index_buffer* ib = m_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint16 ), (void*)m_data->get_indices().data() );
    136         m_va->set_index_buffer( ib, nv::USHORT, true );
     135        nv::index_buffer* ib = m_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint32 ), (void*)m_data->get_indices().data() );
     136        m_va->set_index_buffer( ib, nv::UINT, true );
    137137}
    138138
     
    156156
    157157
    158 nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, keyframed_mesh_data* a_data, program* a_program )
     158nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, mesh_data* a_data, program* a_program )
    159159        : keyframed_mesh( a_context, a_data, a_program )
    160160{
    161         m_vb_position = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ) * m_data->get_frame_count(), (void*)m_data->get_position_data(0) );
     161        m_vb_position = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ), (void*)m_data->get_position_frame(0) );
    162162        m_va->add_vertex_buffer( nv::slot::POSITION, m_vb_position, nv::FLOAT, 3 );
    163163
    164         m_vb_normal   = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ) * m_data->get_frame_count(), (void*)m_data->get_normal_data(0) );
     164        m_vb_normal   = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ), (void*)m_data->get_normal_frame(0) );
    165165        m_va->add_vertex_buffer( nv::slot::NORMAL, m_vb_normal, nv::FLOAT, 3 );
    166166
     
    169169        m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
    170170
    171         nv::index_buffer* ib = m_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint16 ), (void*)m_data->get_indices().data() );
    172         m_va->set_index_buffer( ib, nv::USHORT, true );
     171        nv::index_buffer* ib = m_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint32 ), (void*)m_data->get_indices().data() );
     172        m_va->set_index_buffer( ib, nv::UINT, true );
    173173
    174174        m_position.resize( m_data->get_vertex_count() );
     
    181181
    182182        size_t vtx_count = m_data->get_vertex_count();
    183         const vec3* prev_position = m_data->get_position_data( m_last_frame );
    184         const vec3* next_position = m_data->get_position_data( m_next_frame );
    185         const vec3* prev_normal   = m_data->get_normal_data( m_last_frame );
    186         const vec3* next_normal   = m_data->get_normal_data( m_next_frame );
     183        const vec3* prev_position = m_data->get_position_frame( m_last_frame );
     184        const vec3* next_position = m_data->get_position_frame( m_next_frame );
     185        const vec3* prev_normal   = m_data->get_normal_frame( m_last_frame );
     186        const vec3* next_normal   = m_data->get_normal_frame( m_next_frame );
    187187
    188188        for ( size_t i = 0; i < vtx_count; ++i )
Note: See TracChangeset for help on using the changeset viewer.