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

Last change on this file since 224 was 224, checked in by epyon, 11 years ago
  • universal mesh format
  • removed keyframed_mesh_data
File size: 7.1 KB
Line 
1// Copyright (C) 2011 Kornel Kisielewicz
2// This file is part of NV Libraries.
3// For conditions of distribution and use, see copyright notice in nv.hh
4
5#include "nv/gfx/keyframed_mesh.hh"
6
7#include <glm/gtc/matrix_access.hpp>
8#include <glm/gtx/matrix_interpolation.hpp>
9#include "nv/interface/context.hh"
10#include "nv/interface/device.hh"
11
12
13#include "nv/logging.hh"
14
15using namespace nv;
16
17keyframed_mesh::keyframed_mesh( context* a_context, mesh_data* a_data, program* a_program )
18        : m_context( a_context )
19        , m_data( a_data )
20        , m_program( a_program )
21        , m_va( nullptr )
22        , m_start_frame( false )
23        , m_stop_frame( false )
24        , m_last_frame( 0 )
25        , m_next_frame( 0 )
26        , m_time( 0 )
27        , m_fps( 0 )
28        , m_interpolation( 0.0f )
29        , m_looping( false )
30        , m_active( false )
31{
32        m_va = m_context->get_device()->create_vertex_array();
33}
34
35size_t keyframed_mesh::get_max_frames() const
36{
37        return m_data->get_frame_count();
38}
39
40mat4 keyframed_mesh::get_tag( const std::string& tag ) const
41{
42        const std::vector< nv::mat4 >& transforms = m_data->get_tag_map().at( tag );
43        return glm::interpolate( transforms[ m_last_frame ], transforms[ m_next_frame ], m_interpolation );
44}
45
46void keyframed_mesh::setup_animation( uint32 start, uint32 count, uint32 fps, bool loop )
47{
48        m_start_frame   = start;
49        m_stop_frame    = start+count-1;
50        m_looping       = loop;
51        m_fps           = fps;
52        m_active        = count > 1;
53        m_time          = 0;
54        m_last_frame    = start;
55        m_next_frame    = (count > 1 ? start + 1 : start );
56        m_interpolation = 0.0f;
57}
58
59void nv::keyframed_mesh::set_frame( uint32 frame )
60{
61        m_last_frame    = frame;
62        m_next_frame    = frame;
63        m_active        = false;
64        m_interpolation = 0.0f;
65}
66
67void keyframed_mesh::update( uint32 ms )
68{
69        if ( m_active )
70        {
71                m_time += ms;
72                uint32 f_diff = (m_stop_frame - m_start_frame);
73                float f_time  = 1000 / (float)m_fps;
74                float f_max   = ( m_looping ? ( f_diff + 1 ) : f_diff ) * f_time;
75                float f_pos   = m_time / f_time;
76
77                m_last_frame    = (uint32)glm::floor( f_pos ) + m_start_frame;
78                m_next_frame    = m_last_frame + 1;
79                if ( m_next_frame > m_stop_frame )
80                {
81                        m_next_frame = m_start_frame;
82                }
83
84                if ( m_time >= f_max )
85                {
86                        if ( m_looping )
87                        {
88                                uint32 left = m_time - static_cast< uint32 >( f_max );
89                                m_time = 0;
90                                update( left );
91                        }
92                        else
93                        {
94                                m_active     = false;
95                                m_last_frame = m_stop_frame;
96                                m_next_frame = m_stop_frame;
97                        }
98                }
99                m_interpolation = f_pos - glm::floor( f_pos );
100        }
101}
102
103void nv::keyframed_mesh::draw( render_state& rstate )
104{
105        m_program->set_opt_uniform( "nv_interpolate", m_interpolation );
106        m_context->draw( nv::TRIANGLES, rstate, m_program, m_va, m_data->get_index_count() );
107}
108
109nv::keyframed_mesh::~keyframed_mesh()
110{
111        delete m_va;
112}
113
114keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, mesh_data* a_data, program* a_program )
115        : keyframed_mesh( a_context, a_data, a_program )
116        , m_loc_next_position( 0 )
117        , m_loc_next_normal( 0 )
118        , m_gpu_last_frame( 0xFFFFFFFF )
119        , m_gpu_next_frame( 0xFFFFFFFF )
120{
121        nv::vertex_buffer* vb;
122        m_loc_next_position = m_program->get_attribute( "nv_next_position" )->get_location();
123        m_loc_next_normal   = m_program->get_attribute( "nv_next_normal" )->get_location();
124
125        vb = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ) * m_data->get_frame_count(), (void*)m_data->get_positions().data() );
126        m_va->add_vertex_buffer( m_loc_next_position, vb, nv::FLOAT, 3, 0, 0, false );
127        m_va->add_vertex_buffer( nv::POSITION, vb, nv::FLOAT, 3 );
128
129        vb = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ) * m_data->get_frame_count(), (void*)m_data->get_normals().data() );
130        m_va->add_vertex_buffer( m_loc_next_normal, vb, nv::FLOAT, 3, 0, 0, false );
131        m_va->add_vertex_buffer( nv::NORMAL, vb, nv::FLOAT, 3 );
132
133        vb = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec2 ), (void*)m_data->get_texcoords().data() );
134        m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
135        nv::index_buffer* ib = m_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint32 ), (void*)m_data->get_indices().data() );
136        m_va->set_index_buffer( ib, nv::UINT, true );
137}
138
139void nv::keyframed_mesh_gpu::draw( render_state& rstate )
140{
141        size_t vtx_count = m_data->get_vertex_count();
142        if ( m_gpu_last_frame != m_last_frame )
143        {
144                m_va->update_vertex_buffer( slot::POSITION, m_last_frame * vtx_count * sizeof( nv::vec3 ) );
145                m_va->update_vertex_buffer( slot::NORMAL,   m_last_frame * vtx_count * sizeof( nv::vec3 ) );
146                m_gpu_last_frame = m_last_frame;
147        }
148        if ( m_gpu_next_frame != m_next_frame )
149        {
150                m_va->update_vertex_buffer( m_loc_next_position, m_next_frame * vtx_count * sizeof( nv::vec3 ) );
151                m_va->update_vertex_buffer( m_loc_next_normal,   m_next_frame * vtx_count * sizeof( nv::vec3 ) );
152                m_gpu_next_frame = m_next_frame;
153        }
154        keyframed_mesh::draw( rstate );
155}
156
157
158nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, mesh_data* a_data, program* a_program )
159        : keyframed_mesh( a_context, a_data, a_program )
160{
161        m_vb_position = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ), (void*)m_data->get_position_frame(0) );
162        m_va->add_vertex_buffer( nv::slot::POSITION, m_vb_position, nv::FLOAT, 3 );
163
164        m_vb_normal   = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ), (void*)m_data->get_normal_frame(0) );
165        m_va->add_vertex_buffer( nv::slot::NORMAL, m_vb_normal, nv::FLOAT, 3 );
166
167        nv::vertex_buffer* vb;
168        vb = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec2 ), (void*)m_data->get_texcoords().data() );
169        m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
170
171        nv::index_buffer* ib = m_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint32 ), (void*)m_data->get_indices().data() );
172        m_va->set_index_buffer( ib, nv::UINT, true );
173
174        m_position.resize( m_data->get_vertex_count() );
175        m_normal.resize( m_data->get_vertex_count() );
176}
177
178void nv::keyframed_mesh_cpu::update( uint32 ms )
179{
180        keyframed_mesh::update( ms );
181
182        size_t vtx_count = m_data->get_vertex_count();
183        const vec3* prev_position = m_data->get_position_frame( m_last_frame );
184        const vec3* next_position = m_data->get_position_frame( m_next_frame );
185        const vec3* prev_normal   = m_data->get_normal_frame( m_last_frame );
186        const vec3* next_normal   = m_data->get_normal_frame( m_next_frame );
187
188        for ( size_t i = 0; i < vtx_count; ++i )
189        {
190                m_position[i] = glm::mix( prev_position[i], next_position[i], m_interpolation );
191                m_normal[i]   = glm::mix( prev_normal[i],   next_normal[i],   m_interpolation );
192        }
193
194        m_vb_position->bind();
195        m_vb_position->update( m_position.data(), 0, vtx_count * sizeof( nv::vec3 ) );
196        m_vb_position->unbind();
197
198        m_vb_normal->bind();
199        m_vb_normal->update( m_normal.data(), 0, vtx_count * sizeof( nv::vec3 ) );
200        m_vb_normal->unbind();
201}
Note: See TracBrowser for help on using the repository browser.