source: trunk/src/gfx/skeletal_mesh.cc @ 251

Last change on this file since 251 was 241, checked in by epyon, 11 years ago
  • significant simplification of the md5 code
  • proper instancing for both md5 animations and meshes
  • transform_vectors
File size: 1.8 KB
RevLine 
[227]1// Copyright (C) 2011 Kornel Kisielewicz
2// This file is part of NV Libraries.
3// For conditions of distribution and use, see copyright notice in nv.hh
4
5#include "nv/gfx/skeletal_mesh.hh"
6
7#include <glm/gtc/matrix_access.hpp>
8#include <glm/gtx/matrix_interpolation.hpp>
9#include "nv/interface/context.hh"
10#include "nv/interface/device.hh"
11
12
[239]13nv::skeletal_mesh::skeletal_mesh( context* a_context, md5_mesh_data* a_mesh_data )
[231]14        : animated_mesh()
[241]15        , m_mesh_data( nullptr )
[227]16        , m_animation( nullptr )
[241]17        , m_animation_time( 0 )
[227]18{
[241]19        m_mesh_data = a_mesh_data->spawn();
20        m_va        = a_context->get_device()->create_vertex_array( a_mesh_data, nv::STREAM_DRAW );
[239]21}
[227]22
23
24void nv::skeletal_mesh::setup_animation( md5_animation* a_anim )
25{
[241]26        m_animation      = a_anim;
27        m_animation_time = 0;
28        if ( m_animation )
29        {
30                m_transform.resize( m_animation->get_num_joints() );
31                update( 0 );
32        }
[227]33}
34
35void nv::skeletal_mesh::update( uint32 ms )
36{
[241]37        if ( m_animation )
[227]38        {
[241]39                m_animation_time += ms;
40                float frame_duration = 1000.f / (float)m_animation->get_frame_rate();
41                uint32 anim_duration = uint32( frame_duration * (float)m_animation->get_frame_count() );
42                while ( m_animation_time >= anim_duration ) m_animation_time -= anim_duration;
43                m_animation->update_skeleton( m_transform, (float)m_animation_time * 0.001f );
44                m_mesh_data->apply( m_transform );
[239]45                vertex_buffer* vb = m_va->find_buffer( nv::POSITION );
46                vb->bind();
[241]47                vb->update( m_mesh_data->data(), 0, m_mesh_data->size() );
[239]48                vb->unbind();
[227]49        }
50}
51
[230]52nv::skeletal_mesh::~skeletal_mesh()
[227]53{
[230]54        delete m_va;
[241]55        delete m_mesh_data;
[227]56}
57
[230]58void nv::skeletal_mesh::run_animation( animation_entry* a_anim )
[227]59{
[230]60        skeletal_animation_entry * anim = down_cast<skeletal_animation_entry>(a_anim);
61        setup_animation( anim->m_animation );
[227]62}
Note: See TracBrowser for help on using the repository browser.