Changeset 239 for trunk/src/gfx


Ignore:
Timestamp:
05/17/14 02:35:19 (11 years ago)
Author:
epyon
Message:
  • massive update of mesh handling
  • universal mesh handling routines
  • removed a lot of legacy code
  • significantly streamlined MD5 loading
  • all tests updated to new features
Location:
trunk/src/gfx
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gfx/keyframed_mesh.cc

    r236 r239  
    1515using namespace nv;
    1616
    17 keyframed_mesh::keyframed_mesh( context* a_context, mesh_data_old* a_data )
     17nv::keyframed_mesh::keyframed_mesh( context* a_context, mesh_data* a_data, tag_map* a_tag_map )
    1818        : animated_mesh()
    19         , m_data( a_data )
     19        , m_mesh_data( a_data )
     20        , m_tag_map( a_tag_map )
    2021        , m_start_frame( false )
    2122        , m_stop_frame( false )
     
    2930{
    3031        m_va = a_context->get_device()->create_vertex_array();
     32
     33        m_index_count  = m_mesh_data->get_index_channel()->count;
     34        m_vertex_count = m_mesh_data->get_channel_data()[1]->count;
     35        m_frame_count  = m_mesh_data->get_channel_data()[0]->count / m_vertex_count;
    3136}
    3237
    3338size_t keyframed_mesh::get_max_frames() const
    3439{
    35         return m_data->get_frame_count();
     40        return m_frame_count;
    3641}
    3742
    3843transform keyframed_mesh::get_tag( const std::string& tag ) const
    3944{
    40         const std::vector< transform >& transforms = m_data->get_tag_map().at( tag );
    41         return interpolate( transforms[ m_last_frame ], transforms[ m_next_frame ], m_interpolation );
     45        NV_ASSERT( m_tag_map, "TAGMAP FAIL" );
     46        const std::vector< transform >* transforms = m_tag_map->get_tag( tag );
     47        NV_ASSERT( transforms, "TAG FAIL" );
     48        return interpolate( (*transforms)[ m_last_frame ], (*transforms)[ m_next_frame ], m_interpolation );
    4249}
    4350
     
    115122}
    116123
    117 keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, mesh_data_old* a_data, program* a_program )
    118         : keyframed_mesh( a_context, a_data )
     124nv::keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, mesh_data* a_data, tag_map* a_tag_map, program* a_program )
     125        : keyframed_mesh( a_context, a_data, a_tag_map )
    119126        , m_loc_next_position( 0 )
    120127        , m_loc_next_normal( 0 )
     
    122129        , m_gpu_next_frame( 0xFFFFFFFF )
    123130{
    124         nv::vertex_buffer* vb;
    125131        m_loc_next_position = a_program->get_attribute( "nv_next_position" )->get_location();
    126132        m_loc_next_normal   = a_program->get_attribute( "nv_next_normal" )->get_location();
     133        m_va = a_context->get_device()->create_vertex_array( a_data, nv::STATIC_DRAW );
     134        vertex_buffer* vb = m_va->find_buffer( nv::POSITION );
     135        m_va->add_vertex_buffer( m_loc_next_position, vb, nv::FLOAT, 3, 0,              sizeof( vertex_pn ), false );
     136        m_va->add_vertex_buffer( m_loc_next_normal,   vb, nv::FLOAT, 3, sizeof( vec3 ), sizeof( vertex_pn ), false );
     137}
    127138
    128         vb = a_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_positions().data() );
    129         m_va->add_vertex_buffer( m_loc_next_position, vb, nv::FLOAT, 3, 0, 0, false );
    130         m_va->add_vertex_buffer( nv::POSITION, vb, nv::FLOAT, 3 );
    131 
    132         vb = a_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_normals().data() );
    133         m_va->add_vertex_buffer( m_loc_next_normal, vb, nv::FLOAT, 3, 0, 0, false );
    134         m_va->add_vertex_buffer( nv::NORMAL, vb, nv::FLOAT, 3 );
    135 
    136         vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec2 ), (void*)m_data->get_texcoords().data() );
    137         m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
    138         nv::index_buffer* ib = a_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint32 ), (void*)m_data->get_indices().data() );
    139         m_va->set_index_buffer( ib, nv::UINT, true );
    140 }
    141139
    142140void nv::keyframed_mesh_gpu::update( uint32 ms )
     
    144142        keyframed_mesh::update( ms );
    145143
    146         size_t vtx_count = m_data->get_vertex_count();
    147144        if ( m_gpu_last_frame != m_last_frame )
    148145        {
    149                 m_va->update_vertex_buffer( slot::POSITION, m_last_frame * vtx_count * sizeof( nv::vec3 ) );
    150                 m_va->update_vertex_buffer( slot::NORMAL,   m_last_frame * vtx_count * sizeof( nv::vec3 ) );
     146                m_va->update_vertex_buffer( slot::POSITION, m_last_frame * m_vertex_count * sizeof( vertex_pn ) );
     147                m_va->update_vertex_buffer( slot::NORMAL,   m_last_frame * m_vertex_count * sizeof( vertex_pn ) + sizeof( vec3 ) );
    151148                m_gpu_last_frame = m_last_frame;
    152149        }
    153150        if ( m_gpu_next_frame != m_next_frame )
    154151        {
    155                 m_va->update_vertex_buffer( m_loc_next_position, m_next_frame * vtx_count * sizeof( nv::vec3 ) );
    156                 m_va->update_vertex_buffer( m_loc_next_normal,   m_next_frame * vtx_count * sizeof( nv::vec3 ) );
     152                m_va->update_vertex_buffer( m_loc_next_position, m_next_frame * m_vertex_count * sizeof( vertex_pn ) );
     153                m_va->update_vertex_buffer( m_loc_next_normal,   m_next_frame * m_vertex_count * sizeof( vertex_pn ) + sizeof( vec3 ) );
    157154                m_gpu_next_frame = m_next_frame;
    158155        }
    159156}
    160157
     158nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, mesh_data* a_data, tag_map* a_tag_map )
     159        : keyframed_mesh( a_context, a_data, a_tag_map )
     160{
     161        m_vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * sizeof( vertex_pn ), (void*)m_mesh_data->get_channel_data()[0]->data );
     162        m_va->add_vertex_buffers( m_vb, m_mesh_data->get_channel_data()[0] );
    161163
    162 nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, mesh_data_old* a_data )
    163         : keyframed_mesh( a_context, a_data )
    164 {
    165         m_vb_position = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ), (void*)m_data->get_position_frame(0) );
    166         m_va->add_vertex_buffer( nv::slot::POSITION, m_vb_position, nv::FLOAT, 3 );
     164        nv::vertex_buffer* vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * sizeof( nv::vec2 ), (void*)m_mesh_data->get_channel_data()[1]->data );
     165        m_va->add_vertex_buffers( vb, m_mesh_data->get_channel_data()[1] );
    167166
    168         m_vb_normal   = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ), (void*)m_data->get_normal_frame(0) );
    169         m_va->add_vertex_buffer( nv::slot::NORMAL, m_vb_normal, nv::FLOAT, 3 );
     167        nv::index_buffer* ib = a_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_mesh_data->get_index_channel()->size, (void*)m_mesh_data->get_index_channel()->data );
     168        m_va->set_index_buffer( ib, m_mesh_data->get_index_channel()->etype, true );
    170169
    171         nv::vertex_buffer* vb;
    172         vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec2 ), (void*)m_data->get_texcoords().data() );
    173         m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
    174 
    175         nv::index_buffer* ib = a_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint32 ), (void*)m_data->get_indices().data() );
    176         m_va->set_index_buffer( ib, nv::UINT, true );
    177 
    178         m_position.resize( m_data->get_vertex_count() );
    179         m_normal.resize( m_data->get_vertex_count() );
     170        m_vertex.resize( m_vertex_count );
    180171}
    181172
     
    184175        keyframed_mesh::update( ms );
    185176
    186         size_t vtx_count = m_data->get_vertex_count();
    187         const vec3* prev_position = m_data->get_position_frame( m_last_frame );
    188         const vec3* next_position = m_data->get_position_frame( m_next_frame );
    189         const vec3* prev_normal   = m_data->get_normal_frame( m_last_frame );
    190         const vec3* next_normal   = m_data->get_normal_frame( m_next_frame );
     177        const vertex_pn* data = (const vertex_pn*)(m_mesh_data->get_channel_data()[0]->data);
     178        const vertex_pn* prev = data + m_vertex_count * m_last_frame;
     179        const vertex_pn* next = data + m_vertex_count * m_next_frame;
    191180
    192         for ( size_t i = 0; i < vtx_count; ++i )
     181        for ( size_t i = 0; i < m_vertex_count; ++i )
    193182        {
    194                 m_position[i] = glm::mix( prev_position[i], next_position[i], m_interpolation );
    195                 m_normal[i]   = glm::mix( prev_normal[i],   next_normal[i],   m_interpolation );
     183                m_vertex[i].position = glm::mix( prev[i].position, next[i].position, m_interpolation );
     184                m_vertex[i].normal   = glm::mix( prev[i].normal,   next[i].normal,   m_interpolation );
    196185        }
    197186
    198         m_vb_position->bind();
    199         m_vb_position->update( m_position.data(), 0, vtx_count * sizeof( nv::vec3 ) );
    200         m_vb_position->unbind();
    201 
    202         m_vb_normal->bind();
    203         m_vb_normal->update( m_normal.data(), 0, vtx_count * sizeof( nv::vec3 ) );
    204         m_vb_normal->unbind();
     187        m_vb->bind();
     188        m_vb->update( m_vertex.data(), 0, m_vertex_count * sizeof( vertex_pn ) );
     189        m_vb->unbind();
    205190}
  • trunk/src/gfx/skeletal_mesh.cc

    r237 r239  
    1111
    1212
    13 nv::skeletal_mesh::skeletal_mesh( context* a_context, md5_loader* a_loader )
     13nv::skeletal_mesh::skeletal_mesh( context* a_context, md5_mesh_data* a_mesh_data )
    1414        : animated_mesh()
    15         , m_data( a_loader )
     15        , m_mesh_data( a_mesh_data )
    1616        , m_animation( nullptr )
    1717{
    18         nv::uint32 vcount = a_loader->get_vertex_count(0);
    19         m_va = a_context->get_device()->create_vertex_array();
     18        m_va = a_context->get_device()->create_vertex_array( a_mesh_data, nv::STREAM_DRAW );
     19}
    2020
    21         m_vb_position = a_context->get_device()->create_vertex_buffer( nv::STREAM_DRAW, vcount * sizeof( nv::vec3 ), (const void*)a_loader->get_positions(0).data() );
    22         m_va->add_vertex_buffer( nv::slot::POSITION, m_vb_position, nv::FLOAT, 3, 0, 0, false );
    23         m_vb_normal = a_context->get_device()->create_vertex_buffer( nv::STREAM_DRAW, vcount * sizeof( nv::vec3 ), (const void*)a_loader->get_normals(0).data()  );
    24         m_va->add_vertex_buffer( nv::slot::NORMAL,   m_vb_normal, nv::FLOAT, 3, 0, 0, false );
    25         m_vb_tangent = a_context->get_device()->create_vertex_buffer( nv::STREAM_DRAW, vcount * sizeof( nv::vec3 ), (const void*)a_loader->get_tangents(0).data() );
    26         m_va->add_vertex_buffer( nv::slot::TANGENT,  m_vb_tangent, nv::FLOAT, 3, 0, 0, false );
    27 
    28         nv::vertex_buffer* vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, vcount * sizeof( nv::vec2 ), (const void*)a_loader->get_texcoords(0).data() );
    29         m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
    30         nv::index_buffer* ib = a_context->get_device()->create_index_buffer( nv::STATIC_DRAW, a_loader->get_index_count(0) * sizeof( nv::uint32 ), (const void*)a_loader->get_indices(0).data() );
    31         m_va->set_index_buffer( ib, nv::UINT, true );
    32 }
    3321
    3422void nv::skeletal_mesh::setup_animation( md5_animation* a_anim )
     
    4533        {
    4634                m_animation->update( ms * 0.001f );
    47                 m_data->apply( *m_animation );
     35                m_mesh_data->apply( *m_animation );
     36                vertex_buffer* vb = m_va->find_buffer( nv::POSITION );
     37                const mesh_raw_channel* pch = m_mesh_data->get_channel_data()[0];
     38                vb->bind();
     39                vb->update( (const void*)pch->data, 0, pch->size );
     40                vb->unbind();
    4841        }
    49 
    50         nv::uint32 usize = m_data->get_vertex_count(0) * sizeof( nv::vec3 );
    51         m_vb_position->bind();
    52         m_vb_position->update( (const void*)m_data->get_positions(0).data(), 0, usize );
    53         m_vb_normal  ->bind();
    54         m_vb_normal  ->update( (const void*)m_data->get_normals(0).data(),   0, usize );
    55         m_vb_tangent ->bind();
    56         m_vb_tangent ->update( (const void*)m_data->get_tangents(0).data(),  0, usize );
    57 
    58         // Technically this is not needed, because the va is just a fake class,
    59         // but if it's real it will be needed?
    60 //      m_va->update_vertex_buffer( nv::slot::POSITION, m_vb_position, false );
    61 //      m_va->update_vertex_buffer( nv::slot::NORMAL,   m_vb_normal,   false );
    62 //      m_va->update_vertex_buffer( nv::slot::TANGENT,  m_vb_tangent,  false );
    63         // TODO: answer is - probably not
    6442}
    6543
     
    6745{
    6846        delete m_va;
     47        delete m_mesh_data;
    6948}
    7049
Note: See TracChangeset for help on using the changeset viewer.