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

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