Changeset 223


Ignore:
Timestamp:
01/02/14 16:50:01 (11 years ago)
Author:
epyon
Message:
  • keyframed_mesh can be animated either cpu or gpu side
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/nv/gfx/keyframed_mesh.hh

    r204 r223  
    1818        public:
    1919                keyframed_mesh( context* a_context, keyframed_mesh_data* a_data, program* a_program );
     20                size_t get_max_frames() const;
    2021                mat4 get_tag( const std::string& tag ) const;
    21                 size_t get_max_frames() const;
    22                 void set_frame( uint32 frame );
    23                 void setup_animation( uint32 start, uint32 count, uint32 fps, bool loop );
    24                 void update( uint32 ms );
    25                 void draw( render_state& rstate );
     22                virtual void setup_animation( uint32 start, uint32 count, uint32 fps, bool loop );
     23                virtual void set_frame( uint32 frame );
     24                virtual void update( uint32 ms );
     25                virtual void draw( render_state& rstate );
    2626                program* get_program() { return m_program; }
    27                 ~keyframed_mesh();
    28         private:
     27                virtual ~keyframed_mesh();
     28        protected:
    2929                context*             m_context;
    3030                keyframed_mesh_data* m_data;
     
    3232                vertex_array*        m_va;
    3333
     34                uint32 m_start_frame;
     35                uint32 m_stop_frame;
     36                uint32 m_last_frame;
     37                uint32 m_next_frame;
     38                uint32 m_time;
     39                uint32 m_fps;
     40                f32    m_interpolation;
     41                bool   m_looping;
     42                bool   m_active;
     43        };
     44
     45
     46        class keyframed_mesh_gpu : public keyframed_mesh
     47        {
     48        public:
     49                keyframed_mesh_gpu( context* a_context, keyframed_mesh_data* a_data, program* a_program );
     50                void draw( render_state& rstate );
     51        private:
    3452                int m_loc_next_position;
    3553                int m_loc_next_normal;
    3654
    37                 uint32 m_last_frame;
    38                 uint32 m_next_frame;
    3955                uint32 m_gpu_last_frame;
    4056                uint32 m_gpu_next_frame;
    41                 f32    m_interpolation;
    42                 uint32 m_start_frame;
    43                 uint32 m_stop_frame;
    44                 uint32 m_time;
    45                 uint32 m_fps;
    46                 bool   m_looping;
    47                 bool   m_active;
     57        };
     58
     59        class keyframed_mesh_cpu : public keyframed_mesh
     60        {
     61        public:
     62                keyframed_mesh_cpu( context* a_context, keyframed_mesh_data* a_data, program* a_program );
     63                void update( uint32 ms );
     64        private:
     65                std::vector<vec3> m_position;
     66                std::vector<vec3> m_normal;
     67                vertex_buffer* m_vb_position;
     68                vertex_buffer* m_vb_normal;
    4869        };
    4970
  • trunk/src/gfx/keyframed_mesh.cc

    r204 r223  
    2020        , m_program( a_program )
    2121        , m_va( nullptr )
    22         , m_loc_next_position( 0 )
    23         , m_loc_next_normal( 0 )
     22        , m_start_frame( false )
     23        , m_stop_frame( false )
    2424        , m_last_frame( 0 )
    2525        , m_next_frame( 0 )
    26         , m_gpu_last_frame( 0xFFFFFFFF )
    27         , m_gpu_next_frame( 0xFFFFFFFF )
     26        , m_time( 0 )
     27        , m_fps( 0 )
    2828        , m_interpolation( 0.0f )
    29         , m_start_frame( false )
    30         , m_stop_frame( false )
    3129        , m_looping( false )
    3230        , m_active( false )
    3331{
    3432        m_va = m_context->get_device()->create_vertex_array();
    35 
    36         nv::vertex_buffer* vb;
    37         m_loc_next_position = m_program->get_attribute( "nv_next_position" )->get_location();
    38         m_loc_next_normal   = m_program->get_attribute( "nv_next_normal" )->get_location();
    39 
    40         vb = 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_positions().data() );
    41         m_va->add_vertex_buffer( m_loc_next_position, vb, nv::FLOAT, 3, 0, 0, false );
    42         m_va->add_vertex_buffer( nv::POSITION, vb, nv::FLOAT, 3 );
    43 
    44         vb = 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_normals().data() );
    45         m_va->add_vertex_buffer( m_loc_next_normal, vb, nv::FLOAT, 3, 0, 0, false );
    46         m_va->add_vertex_buffer( nv::NORMAL, vb, nv::FLOAT, 3 );
    47 
    48         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() );
    49         m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
    50         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() );
    51         m_va->set_index_buffer( ib, nv::USHORT, true );
     33}
     34
     35size_t keyframed_mesh::get_max_frames() const
     36{
     37        return m_data->get_frame_count();
    5238}
    5339
     
    5642        const std::vector< nv::mat4 >& transforms = m_data->get_tag_map().at( tag );
    5743        return glm::interpolate( transforms[ m_last_frame ], transforms[ m_next_frame ], m_interpolation );
    58 }
    59 
    60 size_t keyframed_mesh::get_max_frames() const
    61 {
    62         return m_data->get_frame_count();
    63 }
    64 
    65 void keyframed_mesh::set_frame( uint32 frame )
    66 {
    67         m_last_frame    = frame;
    68         m_next_frame    = frame;
    69         m_interpolation = 0.0f;
    70         m_active        = false;
    7144}
    7245
     
    7851        m_fps           = fps;
    7952        m_active        = count > 1;
     53        m_time          = 0;
    8054        m_last_frame    = start;
    8155        m_next_frame    = (count > 1 ? start + 1 : start );
    8256        m_interpolation = 0.0f;
    83         m_time          = 0;
     57}
     58
     59void nv::keyframed_mesh::set_frame( uint32 frame )
     60{
     61        m_last_frame    = frame;
     62        m_next_frame    = frame;
     63        m_active        = false;
     64        m_interpolation = 0.0f;
    8465}
    8566
     
    122103void nv::keyframed_mesh::draw( render_state& rstate )
    123104{
     105        m_program->set_opt_uniform( "nv_interpolate", m_interpolation );
     106        m_context->draw( nv::TRIANGLES, rstate, m_program, m_va, m_data->get_index_count() );
     107}
     108
     109nv::keyframed_mesh::~keyframed_mesh()
     110{
     111        delete m_va;
     112}
     113
     114keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, keyframed_mesh_data* a_data, program* a_program )
     115        : keyframed_mesh( a_context, a_data, a_program )
     116        , m_loc_next_position( 0 )
     117        , m_loc_next_normal( 0 )
     118        , m_gpu_last_frame( 0xFFFFFFFF )
     119        , m_gpu_next_frame( 0xFFFFFFFF )
     120{
     121        nv::vertex_buffer* vb;
     122        m_loc_next_position = m_program->get_attribute( "nv_next_position" )->get_location();
     123        m_loc_next_normal   = m_program->get_attribute( "nv_next_normal" )->get_location();
     124
     125        vb = 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_positions().data() );
     126        m_va->add_vertex_buffer( m_loc_next_position, vb, nv::FLOAT, 3, 0, 0, false );
     127        m_va->add_vertex_buffer( nv::POSITION, vb, nv::FLOAT, 3 );
     128
     129        vb = 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_normals().data() );
     130        m_va->add_vertex_buffer( m_loc_next_normal, vb, nv::FLOAT, 3, 0, 0, false );
     131        m_va->add_vertex_buffer( nv::NORMAL, vb, nv::FLOAT, 3 );
     132
     133        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() );
     134        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 );
     137}
     138
     139void nv::keyframed_mesh_gpu::draw( render_state& rstate )
     140{
    124141        size_t vtx_count = m_data->get_vertex_count();
    125142        if ( m_gpu_last_frame != m_last_frame )
     
    135152                m_gpu_next_frame = m_next_frame;
    136153        }
    137         m_program->set_uniform( "nv_interpolate", m_interpolation );
    138         m_context->draw( nv::TRIANGLES, rstate, m_program, m_va, m_data->get_index_count() );
    139 }
    140 
    141 nv::keyframed_mesh::~keyframed_mesh()
    142 {
    143         delete m_va;
    144 }
     154        keyframed_mesh::draw( rstate );
     155}
     156
     157
     158nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, keyframed_mesh_data* a_data, program* a_program )
     159        : keyframed_mesh( a_context, a_data, a_program )
     160{
     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) );
     162        m_va->add_vertex_buffer( nv::slot::POSITION, m_vb_position, nv::FLOAT, 3 );
     163
     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) );
     165        m_va->add_vertex_buffer( nv::slot::NORMAL, m_vb_normal, nv::FLOAT, 3 );
     166
     167        nv::vertex_buffer* vb;
     168        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() );
     169        m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
     170
     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 );
     173
     174        m_position.resize( m_data->get_vertex_count() );
     175        m_normal.resize( m_data->get_vertex_count() );
     176}
     177
     178void nv::keyframed_mesh_cpu::update( uint32 ms )
     179{
     180        keyframed_mesh::update( ms );
     181
     182        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 );
     187
     188        for ( size_t i = 0; i < vtx_count; ++i )
     189        {
     190                m_position[i] = glm::mix( prev_position[i], next_position[i], m_interpolation );
     191                m_normal[i]   = glm::mix( prev_normal[i],   next_normal[i],   m_interpolation );
     192        }
     193
     194        m_vb_position->bind();
     195        m_vb_position->update( m_position.data(), 0, vtx_count * sizeof( nv::vec3 ) );
     196        m_vb_position->unbind();
     197
     198        m_vb_normal->bind();
     199        m_vb_normal->update( m_normal.data(), 0, vtx_count * sizeof( nv::vec3 ) );
     200        m_vb_normal->unbind();
     201}
  • trunk/tests/md2_test/md2_test.cc

    r214 r223  
    4747                {
    4848                        NV_PROFILE("create_mesh");
    49                         m_mesh      = new nv::keyframed_mesh( window->get_context(), m_mesh_data, program );
     49                        m_mesh      = new nv::keyframed_mesh_gpu( window->get_context(), m_mesh_data, program );
    5050                }
    5151
     
    8585private:
    8686        nv::keyframed_mesh_data* m_mesh_data;
    87         nv::keyframed_mesh*      m_mesh;
     87        nv::keyframed_mesh_gpu*  m_mesh;
    8888};
    8989
Note: See TracChangeset for help on using the changeset viewer.