source: trunk/legacy/keyframed_mesh.cc

Last change on this file was 480, checked in by epyon, 10 years ago
  • cleanup of legacy code
  • resource updates
File size: 8.7 KB
RevLine 
[395]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.
[158]6
7#include "nv/gfx/keyframed_mesh.hh"
8
9#include "nv/interface/context.hh"
10#include "nv/interface/device.hh"
[319]11#include "nv/core/logging.hh"
[158]12
13using namespace nv;
14
[480]15#if 0
16
[430]17nv::keyframed_mesh::keyframed_mesh( context* a_context, const data_channel_set* a_data, const mesh_nodes_data* a_tag_map )
[231]18        : animated_mesh()
[302]19        , m_context( a_context )
[239]20        , m_tag_map( a_tag_map )
[158]21        , m_last_frame( 0 )
22        , m_next_frame( 0 )
23        , m_interpolation( 0.0f )
24        , m_active( false )
25{
[430]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>();
[417]29        if ( pos_size == 0 )
[294]30        {
[430]31                pos_size      = a_data->get_channel_size<vertex_pn>();
[417]32                m_has_tangent = false;
33                m_vsize       = sizeof( vertex_pn );
[294]34        }
[417]35        else
36        {
37                m_has_tangent = true;
38                m_vsize       = sizeof( vertex_pnt );
39        }
40        m_frame_count  = pos_size / m_vertex_count;
[302]41        m_pbuffer      = buffer();
[419]42
[428]43        if ( m_tag_map && m_tag_map->size() > 0 )
[419]44        {
[427]45                m_interpolation_key = (*m_tag_map)[ 0 ]->get_interpolation_key();
[419]46        }
[158]47}
48
[376]49nv::size_t keyframed_mesh::get_max_frames() const
[158]50{
[239]51        return m_frame_count;
[158]52}
53
[287]54transform keyframed_mesh::get_node_transform( uint32 node_id ) const
[158]55{
[304]56        if ( !m_tag_map ) return transform();
[428]57        NV_ASSERT( node_id < m_tag_map->size(), "TAGMAP FAIL" );
[427]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 );
[419]60        return interpolator.get< transform >( m_last_frame, m_next_frame, m_interpolation );
[158]61}
62
[287]63mat4 keyframed_mesh::get_node_matrix( uint32 node_id ) const
64{
65        return get_node_transform( node_id ).extract();
66}
67
[223]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
[470]76void nv::keyframed_mesh::update_animation( animation_entry* anim, uint32 a_ms_anim_time )
[158]77{
78        if ( m_active )
79        {
[470]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 )
[158]86                {
[288]87                        if ( anim->is_looping() )
[158]88                        {
[470]89                                frame  = frame % duration;
90                                fframe = static_cast<float>( frame ) + reminder;
[158]91                        }
92                        else
93                        {
[470]94                                m_active        = false;
95                                m_last_frame    = anim->get_end_frame();
96                                m_next_frame    = m_last_frame;
[288]97                                m_interpolation = 0.0f;
98                                return;
[158]99                        }
100                }
[470]101                m_last_frame    = frame + anim->get_start_frame();
[283]102                m_next_frame    = m_last_frame + 1;
[470]103                if ( m_next_frame > anim->get_end_frame() ) m_next_frame = anim->get_start_frame();
104                m_interpolation = reminder;
[158]105        }
106}
107
[283]108
[303]109void nv::keyframed_mesh::update( program a_program )
[158]110{
[303]111        m_context->get_device()->set_opt_uniform( a_program, "nv_interpolate", m_interpolation );
[223]112}
113
114nv::keyframed_mesh::~keyframed_mesh()
115{
[313]116        m_context->release( m_va );
[223]117}
118
[230]119void nv::keyframed_mesh::run_animation( animation_entry* a_anim )
120{
[252]121        if ( a_anim )
122        {
[288]123                m_active        = true;
124                m_last_frame    = 0;
125                m_next_frame    = 0;
126                m_interpolation = 0.0f;
[252]127        }
128        else
129        {
130                m_active = false;
131        }
[230]132}
133
[430]134nv::keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, const data_channel_set* a_data, const mesh_nodes_data* a_tag_map )
[302]135        : keyframed_mesh( a_context, a_data, a_tag_map )
[294]136        , m_loc_next_position( -1 )
137        , m_loc_next_normal( -1 )
138        , m_loc_next_tangent( -1 )
[223]139        , m_gpu_last_frame( 0xFFFFFFFF )
140        , m_gpu_next_frame( 0xFFFFFFFF )
141{
[313]142        m_va      = a_context->create_vertex_array( a_data, STATIC_DRAW );
143        m_pbuffer = a_context->find_buffer( m_va, slot::POSITION );
[239]144}
[223]145
146
[314]147void nv::keyframed_mesh_gpu::update_animation( animation_entry* anim, uint32 a_anim_time )
[223]148{
[314]149        keyframed_mesh::update_animation( anim, a_anim_time );
[296]150        if ( m_loc_next_position == -1 ) return;
[158]151        if ( m_gpu_last_frame != m_last_frame )
152        {
[302]153                uint32 base_offset = m_last_frame * m_vertex_count * m_vsize;
[313]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 ) );
[294]156                if ( m_has_tangent && m_loc_next_tangent != -1 )
157                {
[313]158                        m_context->update_attribute_offset( m_va, slot::TANGENT, base_offset + 2*sizeof( vec3 ) );
[294]159                }
[158]160                m_gpu_last_frame = m_last_frame;
161        }
[296]162        if ( m_loc_next_position != -1 && m_gpu_next_frame != m_next_frame )
[158]163        {
[302]164                uint32 base_offset = m_next_frame * m_vertex_count * m_vsize;
[406]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 ) );
[302]167                if ( m_has_tangent && m_loc_next_tangent != -1 )
168                {
[406]169                        m_context->update_attribute_offset( m_va, static_cast<slot>( m_loc_next_tangent ), base_offset + 2*sizeof( vec3 ) );
[302]170                }
[158]171                m_gpu_next_frame = m_next_frame;
172        }
173}
174
[303]175void nv::keyframed_mesh_gpu::update( program a_program )
[296]176{
177        if ( m_loc_next_position == -1 )
178        {
[303]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" );
[296]182                if ( m_has_tangent )
[303]183                        m_loc_next_tangent  = dev->get_attribute_location( a_program, "nv_next_tangent" );
[296]184
[406]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 );
[296]187                if ( m_has_tangent )
[406]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 );
[296]189        }
190        keyframed_mesh::update( a_program );
191}
192
[430]193nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, const data_channel_set* a_data, const mesh_nodes_data* a_tag_map )
[302]194        : keyframed_mesh( a_context, a_data, a_tag_map )
[158]195{
[417]196        const raw_data_channel* vchannel = m_has_tangent ? a_data->get_channel< vertex_pnt >() : a_data->get_channel< vertex_pn >();
[313]197        m_va      = m_context->create_vertex_array();
[417]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() );
[223]200
[430]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() );
[223]203
[430]204        const raw_data_channel* index_channel = a_data->get_channel( slot::INDEX );
[412]205        buffer  ib = m_context->get_device()->create_buffer( INDEX_BUFFER, STATIC_DRAW, index_channel->raw_size(), index_channel->raw_data() );
[416]206        m_context->set_index_buffer( m_va, ib, index_channel->descriptor()[0].etype, true );
[223]207
[294]208        m_data = new uint8[ m_vertex_count * m_vsize ];
[430]209
210        m_model_data = vchannel->raw_data();
[158]211}
[223]212
[314]213void nv::keyframed_mesh_cpu::update_animation( animation_entry* anim, uint32 a_anim_time )
[223]214{
[314]215        keyframed_mesh::update_animation( anim, a_anim_time );
[294]216        // TODO: this could be done generic for any data
217        if ( m_has_tangent )
[223]218        {
[430]219                const vertex_pnt* data = reinterpret_cast< const vertex_pnt* >( m_model_data );
[294]220                const vertex_pnt* prev = data + m_vertex_count * m_last_frame;
221                const vertex_pnt* next = data + m_vertex_count * m_next_frame;
[406]222                      vertex_pnt* vtx  = reinterpret_cast<vertex_pnt*>( m_data );
[294]223                for ( size_t i = 0; i < m_vertex_count; ++i )
224                {
[454]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 );
[294]228                }
[223]229        }
[294]230        else
231        {
[430]232                const vertex_pn* data = reinterpret_cast< const vertex_pn* >( m_model_data );
[294]233                const vertex_pn* prev = data + m_vertex_count * m_last_frame;
234                const vertex_pn* next = data + m_vertex_count * m_next_frame;
[406]235                      vertex_pn* vtx  = reinterpret_cast<vertex_pn*>( m_data );
[223]236
[294]237                for ( size_t i = 0; i < m_vertex_count; ++i )
238                {
[454]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 );
[294]241                }
242        }
243
[302]244        m_context->update( m_pbuffer, m_data, 0, m_vertex_count * m_vsize );
[223]245}
[294]246
247nv::keyframed_mesh_cpu::~keyframed_mesh_cpu()
248{
249        delete[] m_data;
250}
[480]251
252#endif
Note: See TracBrowser for help on using the repository browser.