source: trunk/src/gfx/keyframed_mesh.cc @ 287

Last change on this file since 287 was 287, checked in by epyon, 11 years ago
  • mesh_data_pack's in every format
  • md5_mesh_data removed, uses standard mesh_data
  • BONE_ARRAY in NMD is now a simple set of animation nodes
  • bone and animation node concepts merged
  • several minor changes
File size: 6.5 KB
Line 
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/keyframed_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
13#include "nv/logging.hh"
14
15using namespace nv;
16
17nv::keyframed_mesh::keyframed_mesh( device* a_device, const mesh_data* a_data, const mesh_nodes_data* a_tag_map )
18        : animated_mesh()
19        , m_mesh_data( a_data )
20        , m_tag_map( a_tag_map )
21        , m_start_frame( false )
22        , m_stop_frame( false )
23        , m_last_frame( 0 )
24        , m_next_frame( 0 )
25        , m_fps( 0 )
26        , m_interpolation( 0.0f )
27        , m_looping( false )
28        , m_active( false )
29{
30        m_va = a_device->create_vertex_array();
31
32        m_index_count  = m_mesh_data->get_index_channel()->count;
33        m_vertex_count = m_mesh_data->get_channel<vertex_t>()->count;
34        m_frame_count  = m_mesh_data->get_channel<vertex_pn>()->count / m_vertex_count;
35}
36
37size_t keyframed_mesh::get_max_frames() const
38{
39        return m_frame_count;
40}
41
42transform keyframed_mesh::get_node_transform( uint32 node_id ) const
43{
44        NV_ASSERT( m_tag_map, "TAGMAP FAIL" );
45        NV_ASSERT( node_id < m_tag_map->get_count(), "TAGMAP FAIL" );
46        const key_data* data = m_tag_map->get_node( node_id )->data;
47        NV_ASSERT( data, "TAG FAIL" );
48        transform last = data->get_raw_transform( m_last_frame );
49        transform next = data->get_raw_transform( m_next_frame );
50        return interpolate( last, next, m_interpolation  );
51}
52
53mat4 keyframed_mesh::get_node_matrix( uint32 node_id ) const
54{
55        return get_node_transform( node_id ).extract();
56}
57
58void keyframed_mesh::setup_animation( uint32 start, uint32 count, uint32 fps, bool loop )
59{
60        m_start_frame   = start;
61        m_stop_frame    = start+count-1;
62        m_looping       = loop;
63        m_fps           = fps;
64        m_active        = count > 1;
65        m_last_frame    = start;
66        m_next_frame    = (count > 1 ? start + 1 : start );
67        m_interpolation = 0.0f;
68}
69
70void nv::keyframed_mesh::set_frame( uint32 frame )
71{
72        m_last_frame    = frame;
73        m_next_frame    = frame;
74        m_active        = false;
75        m_interpolation = 0.0f;
76}
77
78void nv::keyframed_mesh::update_animation( animation_entry*, uint32 a_anim_time )
79{
80        if ( m_active )
81        {
82                uint32 f_diff = (m_stop_frame - m_start_frame);
83                float  f_time = 1000 / (float)m_fps;
84                float f_max   = ( m_looping ? ( f_diff + 1 ) : f_diff ) * f_time;
85                uint32 time   = a_anim_time;
86                if ( time >= f_max )
87                {
88                        if ( m_looping )
89                        {
90                                time = time % static_cast< uint32 >( f_max );
91                        }
92                        else
93                        {
94                                m_active     = false;
95                                m_last_frame = m_stop_frame;
96                                m_next_frame = m_stop_frame;
97                        }
98                }
99                float f_pos   = time / f_time;
100
101                m_last_frame    = (uint32)glm::floor( f_pos ) + m_start_frame;
102                m_next_frame    = m_last_frame + 1;
103                if ( m_next_frame > m_stop_frame ) m_next_frame = m_start_frame;
104                m_interpolation = f_pos - glm::floor( f_pos );
105        }
106}
107
108
109void nv::keyframed_mesh::update( program* a_program ) const
110{
111        a_program->set_opt_uniform( "nv_interpolate", m_interpolation );
112}
113
114nv::keyframed_mesh::~keyframed_mesh()
115{
116        delete m_va;
117}
118
119void nv::keyframed_mesh::run_animation( animation_entry* a_anim )
120{
121        if ( a_anim )
122        {
123                keyframed_animation_entry * anim = down_cast<keyframed_animation_entry>(a_anim);
124                m_active = true;
125                setup_animation( anim->m_start, anim->m_frames, anim->m_fps, anim->m_looping );
126        }
127        else
128        {
129                m_active = false;
130        }
131}
132
133nv::keyframed_mesh_gpu::keyframed_mesh_gpu( device* a_device, const mesh_data* a_data, const mesh_nodes_data* a_tag_map, program* a_program )
134        : keyframed_mesh( a_device, a_data, a_tag_map )
135        , m_loc_next_position( 0 )
136        , m_loc_next_normal( 0 )
137        , m_gpu_last_frame( 0xFFFFFFFF )
138        , m_gpu_next_frame( 0xFFFFFFFF )
139{
140        m_loc_next_position = a_program->get_attribute( "nv_next_position" )->get_location();
141        m_loc_next_normal   = a_program->get_attribute( "nv_next_normal" )->get_location();
142        m_va = a_device->create_vertex_array( a_data, STATIC_DRAW );
143        vertex_buffer* vb = m_va->find_buffer( slot::POSITION );
144        m_va->add_vertex_buffer( m_loc_next_position, vb, FLOAT, 3, 0,              sizeof( vertex_pn ), false );
145        m_va->add_vertex_buffer( m_loc_next_normal,   vb, FLOAT, 3, sizeof( vec3 ), sizeof( vertex_pn ), false );
146}
147
148
149void nv::keyframed_mesh_gpu::update( uint32 ms )
150{
151        animated_mesh::update( ms );
152
153        if ( m_gpu_last_frame != m_last_frame )
154        {
155                m_va->update_vertex_buffer( slot::POSITION, m_last_frame * m_vertex_count * sizeof( vertex_pn ) );
156                m_va->update_vertex_buffer( slot::NORMAL,   m_last_frame * m_vertex_count * sizeof( vertex_pn ) + sizeof( vec3 ) );
157                m_gpu_last_frame = m_last_frame;
158        }
159        if ( m_gpu_next_frame != m_next_frame )
160        {
161                m_va->update_vertex_buffer( m_loc_next_position, m_next_frame * m_vertex_count * sizeof( vertex_pn ) );
162                m_va->update_vertex_buffer( m_loc_next_normal,   m_next_frame * m_vertex_count * sizeof( vertex_pn ) + sizeof( vec3 ) );
163                m_gpu_next_frame = m_next_frame;
164        }
165}
166
167nv::keyframed_mesh_cpu::keyframed_mesh_cpu( device* a_device, const mesh_data* a_data, const mesh_nodes_data* a_tag_map )
168        : keyframed_mesh( a_device, a_data, a_tag_map )
169{
170        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 );
171        m_va->add_vertex_buffers( m_vb, m_mesh_data->get_channel<vertex_pn>() );
172
173        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 );
174        m_va->add_vertex_buffers( vb, m_mesh_data->get_channel<vertex_t>() );
175
176        nv::index_buffer* ib = a_device->create_index_buffer( nv::STATIC_DRAW, m_mesh_data->get_index_channel()->size(), (void*)m_mesh_data->get_index_channel()->data );
177        m_va->set_index_buffer( ib, m_mesh_data->get_index_channel()->desc.slots[0].etype, true );
178
179        m_vertex.resize( m_vertex_count );
180}
181
182void nv::keyframed_mesh_cpu::update( uint32 ms )
183{
184        animated_mesh::update( ms );
185
186        const vertex_pn* data = m_mesh_data->get_channel_data<vertex_pn>();
187        const vertex_pn* prev = data + m_vertex_count * m_last_frame;
188        const vertex_pn* next = data + m_vertex_count * m_next_frame;
189
190        for ( size_t i = 0; i < m_vertex_count; ++i )
191        {
192                m_vertex[i].position = glm::mix( prev[i].position, next[i].position, m_interpolation );
193                m_vertex[i].normal   = glm::mix( prev[i].normal,   next[i].normal,   m_interpolation );
194        }
195
196        m_vb->bind();
197        m_vb->update( m_vertex.data(), 0, m_vertex_count * sizeof( vertex_pn ) );
198        m_vb->unbind();
199}
Note: See TracBrowser for help on using the repository browser.