Ignore:
Timestamp:
07/28/14 03:05:19 (11 years ago)
Author:
epyon
Message:
  • mesh_creator -- very robust tangent generation mechanism
  • keyframed_mesh - support for meshes with tangent data
  • minor cleanups
File:
1 edited

Legend:

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

    r288 r294  
    2828        m_index_count  = m_mesh_data->get_index_channel()->count;
    2929        m_vertex_count = m_mesh_data->get_channel<vertex_t>()->count;
    30         m_frame_count  = m_mesh_data->get_channel<vertex_pn>()->count / m_vertex_count;
     30        m_vchannel     = m_mesh_data->get_channel<vertex_pnt>();
     31        m_vsize        = sizeof( vertex_pnt );
     32        m_has_tangent  = true;
     33        if ( m_vchannel == nullptr )
     34        {
     35                m_vchannel     = m_mesh_data->get_channel<vertex_pn>();
     36                m_has_tangent  = false;
     37                m_vsize        = sizeof( vertex_pn );
     38        }
     39        m_frame_count  = m_vchannel->count / m_vertex_count;
    3140}
    3241
     
    116125nv::keyframed_mesh_gpu::keyframed_mesh_gpu( device* a_device, const mesh_data* a_data, const mesh_nodes_data* a_tag_map, program* a_program )
    117126        : keyframed_mesh( a_device, a_data, a_tag_map )
    118         , m_loc_next_position( 0 )
    119         , m_loc_next_normal( 0 )
     127        , m_loc_next_position( -1 )
     128        , m_loc_next_normal( -1 )
     129        , m_loc_next_tangent( -1 )
    120130        , m_gpu_last_frame( 0xFFFFFFFF )
    121131        , m_gpu_next_frame( 0xFFFFFFFF )
     
    123133        m_loc_next_position = a_program->get_attribute( "nv_next_position" )->get_location();
    124134        m_loc_next_normal   = a_program->get_attribute( "nv_next_normal" )->get_location();
     135        m_loc_next_tangent  = a_program->try_get_attribute_location( "nv_next_tangent" );
    125136        m_va = a_device->create_vertex_array( a_data, STATIC_DRAW );
    126137        vertex_buffer* vb = m_va->find_buffer( slot::POSITION );
    127         m_va->add_vertex_buffer( m_loc_next_position, vb, FLOAT, 3, 0,              sizeof( vertex_pn ), false );
    128         m_va->add_vertex_buffer( m_loc_next_normal,   vb, FLOAT, 3, sizeof( vec3 ), sizeof( vertex_pn ), false );
     138        m_va->add_vertex_buffer( m_loc_next_position, vb, FLOAT, 3, 0, m_vsize, false );
     139        m_va->add_vertex_buffer( m_loc_next_normal,   vb, FLOAT, 3, sizeof( vec3 ), m_vsize, false );
     140        if ( m_has_tangent )
     141                m_va->add_vertex_buffer( m_loc_next_tangent, vb, FLOAT, 4, 2*sizeof( vec3 ), m_vsize, false );
    129142}
    130143
     
    136149        if ( m_gpu_last_frame != m_last_frame )
    137150        {
    138                 m_va->update_vertex_buffer( slot::POSITION, m_last_frame * m_vertex_count * sizeof( vertex_pn ) );
    139                 m_va->update_vertex_buffer( slot::NORMAL,   m_last_frame * m_vertex_count * sizeof( vertex_pn ) + sizeof( vec3 ) );
     151                m_va->update_vertex_buffer( slot::POSITION, m_last_frame * m_vertex_count * m_vsize );
     152                m_va->update_vertex_buffer( slot::NORMAL,   m_last_frame * m_vertex_count * m_vsize + sizeof( vec3 ) );
     153                if ( m_has_tangent && m_loc_next_tangent != -1 )
     154                {
     155                        m_va->update_vertex_buffer( slot::TANGENT,   m_last_frame * m_vertex_count * m_vsize + 2*sizeof( vec3 ) );
     156                }
    140157                m_gpu_last_frame = m_last_frame;
    141158        }
    142159        if ( m_gpu_next_frame != m_next_frame )
    143160        {
    144                 m_va->update_vertex_buffer( m_loc_next_position, m_next_frame * m_vertex_count * sizeof( vertex_pn ) );
    145                 m_va->update_vertex_buffer( m_loc_next_normal,   m_next_frame * m_vertex_count * sizeof( vertex_pn ) + sizeof( vec3 ) );
     161                m_va->update_vertex_buffer( m_loc_next_position, m_next_frame * m_vertex_count * m_vsize );
     162                m_va->update_vertex_buffer( m_loc_next_normal,   m_next_frame * m_vertex_count * m_vsize + sizeof( vec3 ) );
     163                m_va->update_vertex_buffer( m_loc_next_tangent,   m_next_frame * m_vertex_count * m_vsize + 2*sizeof( vec3 ) );
    146164                m_gpu_next_frame = m_next_frame;
    147165        }
     
    151169        : keyframed_mesh( a_device, a_data, a_tag_map )
    152170{
    153         m_vb = a_device->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * sizeof( vertex_pn ), (void*)m_mesh_data->get_channel<vertex_pn>()->data );
    154         m_va->add_vertex_buffers( m_vb, m_mesh_data->get_channel<vertex_pn>() );
     171        m_vb = a_device->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * m_vsize, (void*)m_vchannel->data );
     172        m_va->add_vertex_buffers( m_vb, m_vchannel );
    155173
    156174        nv::vertex_buffer* vb = a_device->create_vertex_buffer( nv::STATIC_DRAW, m_vertex_count * sizeof( nv::vec2 ), (void*)m_mesh_data->get_channel<vertex_t>()->data );
     
    160178        m_va->set_index_buffer( ib, m_mesh_data->get_index_channel()->desc.slots[0].etype, true );
    161179
    162         m_vertex.resize( m_vertex_count );
     180        m_data = new uint8[ m_vertex_count * m_vsize ];
    163181}
    164182
     
    167185        animated_mesh::update( ms );
    168186
    169         const vertex_pn* data = m_mesh_data->get_channel_data<vertex_pn>();
    170         const vertex_pn* prev = data + m_vertex_count * m_last_frame;
    171         const vertex_pn* next = data + m_vertex_count * m_next_frame;
    172 
    173         for ( size_t i = 0; i < m_vertex_count; ++i )
    174         {
    175                 m_vertex[i].position = glm::mix( prev[i].position, next[i].position, m_interpolation );
    176                 m_vertex[i].normal   = glm::mix( prev[i].normal,   next[i].normal,   m_interpolation );
     187        // TODO: this could be done generic for any data
     188        if ( m_has_tangent )
     189        {
     190                const vertex_pnt* data = m_mesh_data->get_channel_data<vertex_pnt>();
     191                const vertex_pnt* prev = data + m_vertex_count * m_last_frame;
     192                const vertex_pnt* next = data + m_vertex_count * m_next_frame;
     193                      vertex_pnt* vtx  = (vertex_pnt*)m_data;
     194                for ( size_t i = 0; i < m_vertex_count; ++i )
     195                {
     196                        vtx[i].position = glm::mix( prev[i].position, next[i].position, m_interpolation );
     197                        vtx[i].normal   = glm::mix( prev[i].normal,   next[i].normal,   m_interpolation );
     198                        vtx[i].tangent  = glm::mix( prev[i].tangent,  next[i].tangent,   m_interpolation );
     199                }
     200        }
     201        else
     202        {
     203                const vertex_pn* data = m_mesh_data->get_channel_data<vertex_pn>();
     204                const vertex_pn* prev = data + m_vertex_count * m_last_frame;
     205                const vertex_pn* next = data + m_vertex_count * m_next_frame;
     206                      vertex_pn* vtx  = (vertex_pn*)m_data;
     207
     208                for ( size_t i = 0; i < m_vertex_count; ++i )
     209                {
     210                        vtx[i].position = glm::mix( prev[i].position, next[i].position, m_interpolation );
     211                        vtx[i].normal   = glm::mix( prev[i].normal,   next[i].normal,   m_interpolation );
     212                }
    177213        }
    178214
    179215        m_vb->bind();
    180         m_vb->update( m_vertex.data(), 0, m_vertex_count * sizeof( vertex_pn ) );
     216        m_vb->update( m_data, 0, m_vertex_count * m_vsize );
    181217        m_vb->unbind();
    182218}
     219
     220nv::keyframed_mesh_cpu::~keyframed_mesh_cpu()
     221{
     222        delete[] m_data;
     223}
Note: See TracChangeset for help on using the changeset viewer.