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 |
|
---|
15 | using namespace nv;
|
---|
16 |
|
---|
17 | keyframed_mesh::keyframed_mesh( context* a_context, mesh_data* a_data )
|
---|
18 | : animated_mesh()
|
---|
19 | , m_data( a_data )
|
---|
20 | , m_start_frame( false )
|
---|
21 | , m_stop_frame( false )
|
---|
22 | , m_last_frame( 0 )
|
---|
23 | , m_next_frame( 0 )
|
---|
24 | , m_time( 0 )
|
---|
25 | , m_fps( 0 )
|
---|
26 | , m_interpolation( 0.0f )
|
---|
27 | , m_looping( false )
|
---|
28 | , m_active( false )
|
---|
29 | {
|
---|
30 | m_va = a_context->get_device()->create_vertex_array();
|
---|
31 | }
|
---|
32 |
|
---|
33 | size_t keyframed_mesh::get_max_frames() const
|
---|
34 | {
|
---|
35 | return m_data->get_frame_count();
|
---|
36 | }
|
---|
37 |
|
---|
38 | transform keyframed_mesh::get_tag( const std::string& tag ) const
|
---|
39 | {
|
---|
40 | const std::vector< transform >& transforms = m_data->get_tag_map().at( tag );
|
---|
41 | return interpolate( transforms[ m_last_frame ], transforms[ m_next_frame ], m_interpolation );
|
---|
42 | }
|
---|
43 |
|
---|
44 | void keyframed_mesh::setup_animation( uint32 start, uint32 count, uint32 fps, bool loop )
|
---|
45 | {
|
---|
46 | m_start_frame = start;
|
---|
47 | m_stop_frame = start+count-1;
|
---|
48 | m_looping = loop;
|
---|
49 | m_fps = fps;
|
---|
50 | m_active = count > 1;
|
---|
51 | m_time = 0;
|
---|
52 | m_last_frame = start;
|
---|
53 | m_next_frame = (count > 1 ? start + 1 : start );
|
---|
54 | m_interpolation = 0.0f;
|
---|
55 | }
|
---|
56 |
|
---|
57 | void nv::keyframed_mesh::set_frame( uint32 frame )
|
---|
58 | {
|
---|
59 | m_last_frame = frame;
|
---|
60 | m_next_frame = frame;
|
---|
61 | m_active = false;
|
---|
62 | m_interpolation = 0.0f;
|
---|
63 | }
|
---|
64 |
|
---|
65 | void keyframed_mesh::update( uint32 ms )
|
---|
66 | {
|
---|
67 | if ( m_active )
|
---|
68 | {
|
---|
69 | m_time += ms;
|
---|
70 | uint32 f_diff = (m_stop_frame - m_start_frame);
|
---|
71 | float f_time = 1000 / (float)m_fps;
|
---|
72 | float f_max = ( m_looping ? ( f_diff + 1 ) : f_diff ) * f_time;
|
---|
73 | float f_pos = m_time / f_time;
|
---|
74 |
|
---|
75 | m_last_frame = (uint32)glm::floor( f_pos ) + m_start_frame;
|
---|
76 | m_next_frame = m_last_frame + 1;
|
---|
77 | if ( m_next_frame > m_stop_frame )
|
---|
78 | {
|
---|
79 | m_next_frame = m_start_frame;
|
---|
80 | }
|
---|
81 |
|
---|
82 | if ( m_time >= f_max )
|
---|
83 | {
|
---|
84 | if ( m_looping )
|
---|
85 | {
|
---|
86 | uint32 left = m_time - static_cast< uint32 >( f_max );
|
---|
87 | m_time = 0;
|
---|
88 | update( left );
|
---|
89 | }
|
---|
90 | else
|
---|
91 | {
|
---|
92 | m_active = false;
|
---|
93 | m_last_frame = m_stop_frame;
|
---|
94 | m_next_frame = m_stop_frame;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | m_interpolation = f_pos - glm::floor( f_pos );
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | void nv::keyframed_mesh::update( program* a_program )
|
---|
102 | {
|
---|
103 | a_program->set_opt_uniform( "nv_interpolate", m_interpolation );
|
---|
104 | }
|
---|
105 |
|
---|
106 | nv::keyframed_mesh::~keyframed_mesh()
|
---|
107 | {
|
---|
108 | delete m_va;
|
---|
109 | }
|
---|
110 |
|
---|
111 | void nv::keyframed_mesh::run_animation( animation_entry* a_anim )
|
---|
112 | {
|
---|
113 | keyframed_animation_entry * anim = down_cast<keyframed_animation_entry>(a_anim);
|
---|
114 | setup_animation( anim->m_start, anim->m_frames, anim->m_fps, anim->m_looping );
|
---|
115 | }
|
---|
116 |
|
---|
117 | keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, mesh_data* a_data, program* a_program )
|
---|
118 | : keyframed_mesh( a_context, a_data )
|
---|
119 | , m_loc_next_position( 0 )
|
---|
120 | , m_loc_next_normal( 0 )
|
---|
121 | , m_gpu_last_frame( 0xFFFFFFFF )
|
---|
122 | , m_gpu_next_frame( 0xFFFFFFFF )
|
---|
123 | {
|
---|
124 | nv::vertex_buffer* vb;
|
---|
125 | m_loc_next_position = a_program->get_attribute( "nv_next_position" )->get_location();
|
---|
126 | m_loc_next_normal = a_program->get_attribute( "nv_next_normal" )->get_location();
|
---|
127 |
|
---|
128 | vb = a_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() );
|
---|
129 | m_va->add_vertex_buffer( m_loc_next_position, vb, nv::FLOAT, 3, 0, 0, false );
|
---|
130 | m_va->add_vertex_buffer( nv::POSITION, vb, nv::FLOAT, 3 );
|
---|
131 |
|
---|
132 | vb = a_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() );
|
---|
133 | m_va->add_vertex_buffer( m_loc_next_normal, vb, nv::FLOAT, 3, 0, 0, false );
|
---|
134 | m_va->add_vertex_buffer( nv::NORMAL, vb, nv::FLOAT, 3 );
|
---|
135 |
|
---|
136 | vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec2 ), (void*)m_data->get_texcoords().data() );
|
---|
137 | m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
|
---|
138 | nv::index_buffer* ib = a_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint32 ), (void*)m_data->get_indices().data() );
|
---|
139 | m_va->set_index_buffer( ib, nv::UINT, true );
|
---|
140 | }
|
---|
141 |
|
---|
142 | void nv::keyframed_mesh_gpu::update( uint32 ms )
|
---|
143 | {
|
---|
144 | keyframed_mesh::update( ms );
|
---|
145 |
|
---|
146 | size_t vtx_count = m_data->get_vertex_count();
|
---|
147 | if ( m_gpu_last_frame != m_last_frame )
|
---|
148 | {
|
---|
149 | m_va->update_vertex_buffer( slot::POSITION, m_last_frame * vtx_count * sizeof( nv::vec3 ) );
|
---|
150 | m_va->update_vertex_buffer( slot::NORMAL, m_last_frame * vtx_count * sizeof( nv::vec3 ) );
|
---|
151 | m_gpu_last_frame = m_last_frame;
|
---|
152 | }
|
---|
153 | if ( m_gpu_next_frame != m_next_frame )
|
---|
154 | {
|
---|
155 | m_va->update_vertex_buffer( m_loc_next_position, m_next_frame * vtx_count * sizeof( nv::vec3 ) );
|
---|
156 | m_va->update_vertex_buffer( m_loc_next_normal, m_next_frame * vtx_count * sizeof( nv::vec3 ) );
|
---|
157 | m_gpu_next_frame = m_next_frame;
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, mesh_data* a_data )
|
---|
163 | : keyframed_mesh( a_context, a_data )
|
---|
164 | {
|
---|
165 | m_vb_position = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ), (void*)m_data->get_position_frame(0) );
|
---|
166 | m_va->add_vertex_buffer( nv::slot::POSITION, m_vb_position, nv::FLOAT, 3 );
|
---|
167 |
|
---|
168 | m_vb_normal = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ), (void*)m_data->get_normal_frame(0) );
|
---|
169 | m_va->add_vertex_buffer( nv::slot::NORMAL, m_vb_normal, nv::FLOAT, 3 );
|
---|
170 |
|
---|
171 | nv::vertex_buffer* vb;
|
---|
172 | vb = a_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec2 ), (void*)m_data->get_texcoords().data() );
|
---|
173 | m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 );
|
---|
174 |
|
---|
175 | nv::index_buffer* ib = a_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint32 ), (void*)m_data->get_indices().data() );
|
---|
176 | m_va->set_index_buffer( ib, nv::UINT, true );
|
---|
177 |
|
---|
178 | m_position.resize( m_data->get_vertex_count() );
|
---|
179 | m_normal.resize( m_data->get_vertex_count() );
|
---|
180 | }
|
---|
181 |
|
---|
182 | void nv::keyframed_mesh_cpu::update( uint32 ms )
|
---|
183 | {
|
---|
184 | keyframed_mesh::update( ms );
|
---|
185 |
|
---|
186 | size_t vtx_count = m_data->get_vertex_count();
|
---|
187 | const vec3* prev_position = m_data->get_position_frame( m_last_frame );
|
---|
188 | const vec3* next_position = m_data->get_position_frame( m_next_frame );
|
---|
189 | const vec3* prev_normal = m_data->get_normal_frame( m_last_frame );
|
---|
190 | const vec3* next_normal = m_data->get_normal_frame( m_next_frame );
|
---|
191 |
|
---|
192 | for ( size_t i = 0; i < vtx_count; ++i )
|
---|
193 | {
|
---|
194 | m_position[i] = glm::mix( prev_position[i], next_position[i], m_interpolation );
|
---|
195 | m_normal[i] = glm::mix( prev_normal[i], next_normal[i], m_interpolation );
|
---|
196 | }
|
---|
197 |
|
---|
198 | m_vb_position->bind();
|
---|
199 | m_vb_position->update( m_position.data(), 0, vtx_count * sizeof( nv::vec3 ) );
|
---|
200 | m_vb_position->unbind();
|
---|
201 |
|
---|
202 | m_vb_normal->bind();
|
---|
203 | m_vb_normal->update( m_normal.data(), 0, vtx_count * sizeof( nv::vec3 ) );
|
---|
204 | m_vb_normal->unbind();
|
---|
205 | }
|
---|