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

Last change on this file since 472 was 470, checked in by epyon, 10 years ago
  • animation time definition fixes
File size: 8.7 KB
Line 
1// Copyright (C) 2011-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#include "nv/gfx/keyframed_mesh.hh"
8
9#include "nv/interface/context.hh"
10#include "nv/interface/device.hh"
11#include "nv/core/logging.hh"
12
13using namespace nv;
14
15nv::keyframed_mesh::keyframed_mesh( context* a_context, const data_channel_set* a_data, const mesh_nodes_data* a_tag_map )
16        : animated_mesh()
17        , m_context( a_context )
18        , m_tag_map( a_tag_map )
19        , m_last_frame( 0 )
20        , m_next_frame( 0 )
21        , m_interpolation( 0.0f )
22        , m_active( false )
23{
24        m_index_count   = a_data->get_channel_size( slot::INDEX );
25        m_vertex_count  = a_data->get_channel_size<vertex_t>();
26        uint32 pos_size = a_data->get_channel_size<vertex_pnt>();
27        if ( pos_size == 0 )
28        {
29                pos_size      = a_data->get_channel_size<vertex_pn>();
30                m_has_tangent = false;
31                m_vsize       = sizeof( vertex_pn );
32        }
33        else
34        {
35                m_has_tangent = true;
36                m_vsize       = sizeof( vertex_pnt );
37        }
38        m_frame_count  = pos_size / m_vertex_count;
39        m_pbuffer      = buffer();
40
41        if ( m_tag_map && m_tag_map->size() > 0 )
42        {
43                m_interpolation_key = (*m_tag_map)[ 0 ]->get_interpolation_key();
44        }
45}
46
47nv::size_t keyframed_mesh::get_max_frames() const
48{
49        return m_frame_count;
50}
51
52transform keyframed_mesh::get_node_transform( uint32 node_id ) const
53{
54        if ( !m_tag_map ) return transform();
55        NV_ASSERT( node_id < m_tag_map->size(), "TAGMAP FAIL" );
56        NV_ASSERT( (*m_tag_map)[node_id]->size() > 0, "TAG FAIL" );
57        raw_channel_interpolator interpolator( ( *m_tag_map )[node_id], m_interpolation_key );
58        return interpolator.get< transform >( m_last_frame, m_next_frame, m_interpolation );
59}
60
61mat4 keyframed_mesh::get_node_matrix( uint32 node_id ) const
62{
63        return get_node_transform( node_id ).extract();
64}
65
66void nv::keyframed_mesh::set_frame( uint32 frame )
67{
68        m_last_frame    = frame;
69        m_next_frame    = frame;
70        m_active        = false;
71        m_interpolation = 0.0f;
72}
73
74void nv::keyframed_mesh::update_animation( animation_entry* anim, uint32 a_ms_anim_time )
75{
76        if ( m_active )
77        {
78                float  fframe   = ( static_cast<float>( a_ms_anim_time ) * 0.001f ) * anim->get_fps();
79                uint32 frame    = uint32( fframe );
80                float  reminder = fframe - static_cast<float>( frame );
81                uint32 duration = anim->is_looping() ? anim->get_frame_count() + 1 : anim->get_frame_count();
82
83                if ( frame >= duration )
84                {
85                        if ( anim->is_looping() )
86                        {
87                                frame  = frame % duration;
88                                fframe = static_cast<float>( frame ) + reminder;
89                        }
90                        else
91                        {
92                                m_active        = false;
93                                m_last_frame    = anim->get_end_frame();
94                                m_next_frame    = m_last_frame;
95                                m_interpolation = 0.0f;
96                                return;
97                        }
98                }
99                m_last_frame    = frame + anim->get_start_frame();
100                m_next_frame    = m_last_frame + 1;
101                if ( m_next_frame > anim->get_end_frame() ) m_next_frame = anim->get_start_frame();
102                m_interpolation = reminder;
103        }
104}
105
106
107void nv::keyframed_mesh::update( program a_program )
108{
109        m_context->get_device()->set_opt_uniform( a_program, "nv_interpolate", m_interpolation );
110}
111
112nv::keyframed_mesh::~keyframed_mesh()
113{
114        m_context->release( m_va );
115}
116
117void nv::keyframed_mesh::run_animation( animation_entry* a_anim )
118{
119        if ( a_anim )
120        {
121                m_active        = true;
122                m_last_frame    = 0;
123                m_next_frame    = 0;
124                m_interpolation = 0.0f;
125        }
126        else
127        {
128                m_active = false;
129        }
130}
131
132nv::keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, const data_channel_set* a_data, const mesh_nodes_data* a_tag_map )
133        : keyframed_mesh( a_context, a_data, a_tag_map )
134        , m_loc_next_position( -1 )
135        , m_loc_next_normal( -1 )
136        , m_loc_next_tangent( -1 )
137        , m_gpu_last_frame( 0xFFFFFFFF )
138        , m_gpu_next_frame( 0xFFFFFFFF )
139{
140        m_va      = a_context->create_vertex_array( a_data, STATIC_DRAW );
141        m_pbuffer = a_context->find_buffer( m_va, slot::POSITION );
142}
143
144
145void nv::keyframed_mesh_gpu::update_animation( animation_entry* anim, uint32 a_anim_time )
146{
147        keyframed_mesh::update_animation( anim, a_anim_time );
148        if ( m_loc_next_position == -1 ) return;
149        if ( m_gpu_last_frame != m_last_frame )
150        {
151                uint32 base_offset = m_last_frame * m_vertex_count * m_vsize;
152                m_context->update_attribute_offset( m_va, slot::POSITION, base_offset );
153                m_context->update_attribute_offset( m_va, slot::NORMAL,   base_offset + sizeof( vec3 ) );
154                if ( m_has_tangent && m_loc_next_tangent != -1 )
155                {
156                        m_context->update_attribute_offset( m_va, slot::TANGENT, base_offset + 2*sizeof( vec3 ) );
157                }
158                m_gpu_last_frame = m_last_frame;
159        }
160        if ( m_loc_next_position != -1 && m_gpu_next_frame != m_next_frame )
161        {
162                uint32 base_offset = m_next_frame * m_vertex_count * m_vsize;
163                m_context->update_attribute_offset( m_va, static_cast<slot>( m_loc_next_position ), base_offset );
164                m_context->update_attribute_offset( m_va, static_cast<slot>( m_loc_next_normal ), base_offset + sizeof( vec3 ) );
165                if ( m_has_tangent && m_loc_next_tangent != -1 )
166                {
167                        m_context->update_attribute_offset( m_va, static_cast<slot>( m_loc_next_tangent ), base_offset + 2*sizeof( vec3 ) );
168                }
169                m_gpu_next_frame = m_next_frame;
170        }
171}
172
173void nv::keyframed_mesh_gpu::update( program a_program )
174{
175        if ( m_loc_next_position == -1 )
176        {
177                device* dev = m_context->get_device();
178                m_loc_next_position = dev->get_attribute_location( a_program, "nv_next_position" );
179                m_loc_next_normal   = dev->get_attribute_location( a_program, "nv_next_normal" );
180                if ( m_has_tangent )
181                        m_loc_next_tangent  = dev->get_attribute_location( a_program, "nv_next_tangent" );
182
183                m_context->add_vertex_buffer( m_va, static_cast<slot>( m_loc_next_position ), m_pbuffer, FLOAT, 3, 0, m_vsize, false );
184                m_context->add_vertex_buffer( m_va, static_cast<slot>( m_loc_next_normal ),   m_pbuffer, FLOAT, 3, sizeof( vec3 ), m_vsize, false );
185                if ( m_has_tangent )
186                        m_context->add_vertex_buffer( m_va, static_cast<slot>( m_loc_next_tangent ), m_pbuffer, FLOAT, 4, 2*sizeof( vec3 ), m_vsize, false );
187        }
188        keyframed_mesh::update( a_program );
189}
190
191nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, const data_channel_set* a_data, const mesh_nodes_data* a_tag_map )
192        : keyframed_mesh( a_context, a_data, a_tag_map )
193{
194        const raw_data_channel* vchannel = m_has_tangent ? a_data->get_channel< vertex_pnt >() : a_data->get_channel< vertex_pn >();
195        m_va      = m_context->create_vertex_array();
196        m_pbuffer = m_context->get_device()->create_buffer( VERTEX_BUFFER, STATIC_DRAW, m_vertex_count * m_vsize, vchannel->raw_data() );
197        m_context->add_vertex_buffers( m_va, m_pbuffer, vchannel->descriptor() );
198
199        buffer  vb = m_context->get_device()->create_buffer( VERTEX_BUFFER, STATIC_DRAW, m_vertex_count * sizeof( vec2 ), a_data->get_channel_data<vertex_t>() );
200        m_context->add_vertex_buffers( m_va, vb, a_data->get_channel<vertex_t>()->descriptor() );
201
202        const raw_data_channel* index_channel = a_data->get_channel( slot::INDEX );
203        buffer  ib = m_context->get_device()->create_buffer( INDEX_BUFFER, STATIC_DRAW, index_channel->raw_size(), index_channel->raw_data() );
204        m_context->set_index_buffer( m_va, ib, index_channel->descriptor()[0].etype, true );
205
206        m_data = new uint8[ m_vertex_count * m_vsize ];
207
208        m_model_data = vchannel->raw_data();
209}
210
211void nv::keyframed_mesh_cpu::update_animation( animation_entry* anim, uint32 a_anim_time )
212{
213        keyframed_mesh::update_animation( anim, a_anim_time );
214        // TODO: this could be done generic for any data
215        if ( m_has_tangent )
216        {
217                const vertex_pnt* data = reinterpret_cast< const vertex_pnt* >( m_model_data );
218                const vertex_pnt* prev = data + m_vertex_count * m_last_frame;
219                const vertex_pnt* next = data + m_vertex_count * m_next_frame;
220                      vertex_pnt* vtx  = reinterpret_cast<vertex_pnt*>( m_data );
221                for ( size_t i = 0; i < m_vertex_count; ++i )
222                {
223                        vtx[i].position = math::mix( prev[i].position, next[i].position, m_interpolation );
224                        vtx[i].normal   = math::mix( prev[i].normal,   next[i].normal,   m_interpolation );
225                        vtx[i].tangent  = math::mix( prev[i].tangent,  next[i].tangent,   m_interpolation );
226                }
227        }
228        else
229        {
230                const vertex_pn* data = reinterpret_cast< const vertex_pn* >( m_model_data );
231                const vertex_pn* prev = data + m_vertex_count * m_last_frame;
232                const vertex_pn* next = data + m_vertex_count * m_next_frame;
233                      vertex_pn* vtx  = reinterpret_cast<vertex_pn*>( m_data );
234
235                for ( size_t i = 0; i < m_vertex_count; ++i )
236                {
237                        vtx[i].position = math::mix( prev[i].position, next[i].position, m_interpolation );
238                        vtx[i].normal   = math::mix( prev[i].normal,   next[i].normal,   m_interpolation );
239                }
240        }
241
242        m_context->update( m_pbuffer, m_data, 0, m_vertex_count * m_vsize );
243}
244
245nv::keyframed_mesh_cpu::~keyframed_mesh_cpu()
246{
247        delete[] m_data;
248}
Note: See TracBrowser for help on using the repository browser.