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

Last change on this file since 454 was 454, checked in by epyon, 10 years ago
  • math library work
  • glm only referenced in math/common.hh
  • still a lot work to do, but its WURF
File size: 8.6 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_anim_time )
75{
76        if ( m_active )
77        {
78                float tick_time = ( static_cast<float>( a_anim_time ) * 0.001f ) * anim->get_frame_rate();
79                float duration  = anim->is_looping() ? anim->get_duration() + 1.0f : anim->get_duration();
80                if ( tick_time >= duration )
81                {
82                        if ( anim->is_looping() )
83                        {
84                                tick_time = fmodf( tick_time, duration );
85                        }
86                        else
87                        {
88                                m_active     = false;
89                                m_last_frame = static_cast<uint32>( anim->get_end() );
90                                m_next_frame = m_last_frame;
91                                m_interpolation = 0.0f;
92                                return;
93                        }
94                }
95                m_last_frame    = static_cast<uint32>( math::floor( tick_time ) + anim->get_start() );
96                m_next_frame    = m_last_frame + 1;
97                if ( m_next_frame > static_cast<uint32>( anim->get_end() ) ) m_next_frame = static_cast<uint32>( anim->get_start() );
98                m_interpolation = tick_time - math::floor( tick_time );
99        }
100}
101
102
103void nv::keyframed_mesh::update( program a_program )
104{
105        m_context->get_device()->set_opt_uniform( a_program, "nv_interpolate", m_interpolation );
106}
107
108nv::keyframed_mesh::~keyframed_mesh()
109{
110        m_context->release( m_va );
111}
112
113void nv::keyframed_mesh::run_animation( animation_entry* a_anim )
114{
115        if ( a_anim )
116        {
117                m_active        = true;
118                m_last_frame    = 0;
119                m_next_frame    = 0;
120                m_interpolation = 0.0f;
121        }
122        else
123        {
124                m_active = false;
125        }
126}
127
128nv::keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, const data_channel_set* a_data, const mesh_nodes_data* a_tag_map )
129        : keyframed_mesh( a_context, a_data, a_tag_map )
130        , m_loc_next_position( -1 )
131        , m_loc_next_normal( -1 )
132        , m_loc_next_tangent( -1 )
133        , m_gpu_last_frame( 0xFFFFFFFF )
134        , m_gpu_next_frame( 0xFFFFFFFF )
135{
136        m_va      = a_context->create_vertex_array( a_data, STATIC_DRAW );
137        m_pbuffer = a_context->find_buffer( m_va, slot::POSITION );
138}
139
140
141void nv::keyframed_mesh_gpu::update_animation( animation_entry* anim, uint32 a_anim_time )
142{
143        keyframed_mesh::update_animation( anim, a_anim_time );
144        if ( m_loc_next_position == -1 ) return;
145        if ( m_gpu_last_frame != m_last_frame )
146        {
147                uint32 base_offset = m_last_frame * m_vertex_count * m_vsize;
148                m_context->update_attribute_offset( m_va, slot::POSITION, base_offset );
149                m_context->update_attribute_offset( m_va, slot::NORMAL,   base_offset + sizeof( vec3 ) );
150                if ( m_has_tangent && m_loc_next_tangent != -1 )
151                {
152                        m_context->update_attribute_offset( m_va, slot::TANGENT, base_offset + 2*sizeof( vec3 ) );
153                }
154                m_gpu_last_frame = m_last_frame;
155        }
156        if ( m_loc_next_position != -1 && m_gpu_next_frame != m_next_frame )
157        {
158                uint32 base_offset = m_next_frame * m_vertex_count * m_vsize;
159                m_context->update_attribute_offset( m_va, static_cast<slot>( m_loc_next_position ), base_offset );
160                m_context->update_attribute_offset( m_va, static_cast<slot>( m_loc_next_normal ), base_offset + sizeof( vec3 ) );
161                if ( m_has_tangent && m_loc_next_tangent != -1 )
162                {
163                        m_context->update_attribute_offset( m_va, static_cast<slot>( m_loc_next_tangent ), base_offset + 2*sizeof( vec3 ) );
164                }
165                m_gpu_next_frame = m_next_frame;
166        }
167}
168
169void nv::keyframed_mesh_gpu::update( program a_program )
170{
171        if ( m_loc_next_position == -1 )
172        {
173                device* dev = m_context->get_device();
174                m_loc_next_position = dev->get_attribute_location( a_program, "nv_next_position" );
175                m_loc_next_normal   = dev->get_attribute_location( a_program, "nv_next_normal" );
176                if ( m_has_tangent )
177                        m_loc_next_tangent  = dev->get_attribute_location( a_program, "nv_next_tangent" );
178
179                m_context->add_vertex_buffer( m_va, static_cast<slot>( m_loc_next_position ), m_pbuffer, FLOAT, 3, 0, m_vsize, false );
180                m_context->add_vertex_buffer( m_va, static_cast<slot>( m_loc_next_normal ),   m_pbuffer, FLOAT, 3, sizeof( vec3 ), m_vsize, false );
181                if ( m_has_tangent )
182                        m_context->add_vertex_buffer( m_va, static_cast<slot>( m_loc_next_tangent ), m_pbuffer, FLOAT, 4, 2*sizeof( vec3 ), m_vsize, false );
183        }
184        keyframed_mesh::update( a_program );
185}
186
187nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, const data_channel_set* a_data, const mesh_nodes_data* a_tag_map )
188        : keyframed_mesh( a_context, a_data, a_tag_map )
189{
190        const raw_data_channel* vchannel = m_has_tangent ? a_data->get_channel< vertex_pnt >() : a_data->get_channel< vertex_pn >();
191        m_va      = m_context->create_vertex_array();
192        m_pbuffer = m_context->get_device()->create_buffer( VERTEX_BUFFER, STATIC_DRAW, m_vertex_count * m_vsize, vchannel->raw_data() );
193        m_context->add_vertex_buffers( m_va, m_pbuffer, vchannel->descriptor() );
194
195        buffer  vb = m_context->get_device()->create_buffer( VERTEX_BUFFER, STATIC_DRAW, m_vertex_count * sizeof( vec2 ), a_data->get_channel_data<vertex_t>() );
196        m_context->add_vertex_buffers( m_va, vb, a_data->get_channel<vertex_t>()->descriptor() );
197
198        const raw_data_channel* index_channel = a_data->get_channel( slot::INDEX );
199        buffer  ib = m_context->get_device()->create_buffer( INDEX_BUFFER, STATIC_DRAW, index_channel->raw_size(), index_channel->raw_data() );
200        m_context->set_index_buffer( m_va, ib, index_channel->descriptor()[0].etype, true );
201
202        m_data = new uint8[ m_vertex_count * m_vsize ];
203
204        m_model_data = vchannel->raw_data();
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 = reinterpret_cast< const vertex_pnt* >( m_model_data );
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 = math::mix( prev[i].position, next[i].position, m_interpolation );
220                        vtx[i].normal   = math::mix( prev[i].normal,   next[i].normal,   m_interpolation );
221                        vtx[i].tangent  = math::mix( prev[i].tangent,  next[i].tangent,   m_interpolation );
222                }
223        }
224        else
225        {
226                const vertex_pn* data = reinterpret_cast< const vertex_pn* >( m_model_data );
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 = math::mix( prev[i].position, next[i].position, m_interpolation );
234                        vtx[i].normal   = math::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.