source: trunk/legacy/keyframed_mesh.cc @ 488

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