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, keyframed_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_loc_next_position( 0 )
|
---|
23 | , m_loc_last_position( 0 )
|
---|
24 | , m_loc_next_normal( 0 )
|
---|
25 | , m_loc_last_normal( 0 )
|
---|
26 | , m_last_frame( 0 )
|
---|
27 | , m_next_frame( 0 )
|
---|
28 | , m_gpu_last_frame( 0xFFFFFFFF )
|
---|
29 | , m_gpu_next_frame( 0xFFFFFFFF )
|
---|
30 | , m_interpolation( 0.0f )
|
---|
31 | , m_start_frame( false )
|
---|
32 | , m_stop_frame( false )
|
---|
33 | , m_looping( false )
|
---|
34 | , m_active( false )
|
---|
35 | {
|
---|
36 | m_va = m_context->get_device()->create_vertex_array();
|
---|
37 |
|
---|
38 | nv::vertex_buffer* vb;
|
---|
39 | m_loc_next_position = m_program->get_attribute( "next_position" )->get_location();
|
---|
40 | m_loc_last_position = m_program->get_attribute( "last_position" )->get_location();
|
---|
41 | m_loc_next_normal = m_program->get_attribute( "next_normal" )->get_location();
|
---|
42 | m_loc_last_normal = m_program->get_attribute( "last_normal" )->get_location();
|
---|
43 |
|
---|
44 | 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() );
|
---|
45 | m_va->add_vertex_buffer( m_loc_next_position, vb, nv::FLOAT, 3, 0, 0, false );
|
---|
46 | m_va->add_vertex_buffer( m_loc_last_position, vb, nv::FLOAT, 3 );
|
---|
47 |
|
---|
48 | 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() );
|
---|
49 | m_va->add_vertex_buffer( m_loc_next_normal, vb, nv::FLOAT, 3, 0, 0, false );
|
---|
50 | m_va->add_vertex_buffer( m_loc_last_normal, vb, nv::FLOAT, 3 );
|
---|
51 |
|
---|
52 | 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() );
|
---|
53 | m_va->add_vertex_buffer( m_program->get_attribute( "texcoord" )->get_location(), vb, nv::FLOAT, 2 );
|
---|
54 | nv::index_buffer* ib = m_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint16 ), (void*)m_data->get_indices().data() );
|
---|
55 | m_va->set_index_buffer( ib, nv::USHORT, true );
|
---|
56 | }
|
---|
57 |
|
---|
58 | mat4 keyframed_mesh::get_tag( const std::string& tag ) const
|
---|
59 | {
|
---|
60 | const std::vector< nv::mat4 >& transforms = m_data->get_tag_map().at( tag );
|
---|
61 | return glm::interpolate( transforms[ m_last_frame ], transforms[ m_next_frame ], m_interpolation );
|
---|
62 | }
|
---|
63 |
|
---|
64 | uint32 keyframed_mesh::get_max_frames() const
|
---|
65 | {
|
---|
66 | return m_data->get_frame_count();
|
---|
67 | }
|
---|
68 |
|
---|
69 | void keyframed_mesh::set_frame( uint32 frame )
|
---|
70 | {
|
---|
71 | m_last_frame = frame;
|
---|
72 | m_next_frame = frame;
|
---|
73 | m_interpolation = 0.0f;
|
---|
74 | m_active = false;
|
---|
75 | }
|
---|
76 |
|
---|
77 | void keyframed_mesh::setup_animation( uint32 start, uint32 stop, uint32 fps, bool loop )
|
---|
78 | {
|
---|
79 | m_start_frame = start;
|
---|
80 | m_stop_frame = stop;
|
---|
81 | m_looping = loop;
|
---|
82 | m_fps = fps;
|
---|
83 | m_active = ( stop > start );
|
---|
84 | m_last_frame = start;
|
---|
85 | m_next_frame = (stop > start ? start + 1 : start );
|
---|
86 | m_interpolation = 0.0f;
|
---|
87 | m_time = 0;
|
---|
88 | }
|
---|
89 |
|
---|
90 | void keyframed_mesh::update( uint32 ms )
|
---|
91 | {
|
---|
92 | if ( m_active )
|
---|
93 | {
|
---|
94 | m_time += ms;
|
---|
95 | uint32 f_diff = (m_stop_frame - m_start_frame);
|
---|
96 | float f_time = 1000 / (float)m_fps;
|
---|
97 | float f_max = ( m_looping ? ( f_diff + 1 ) : f_diff ) * f_time;
|
---|
98 | float f_pos = m_time / f_time;
|
---|
99 |
|
---|
100 | m_last_frame = (uint32)glm::floor( f_pos ) + m_start_frame;
|
---|
101 | m_next_frame = m_last_frame + 1;
|
---|
102 | if ( m_next_frame > m_stop_frame )
|
---|
103 | {
|
---|
104 | m_next_frame = m_start_frame;
|
---|
105 | }
|
---|
106 |
|
---|
107 | if ( m_time >= f_max )
|
---|
108 | {
|
---|
109 | if ( m_looping )
|
---|
110 | {
|
---|
111 | uint32 left = m_time - f_max;
|
---|
112 | m_time = 0;
|
---|
113 | update( left );
|
---|
114 | }
|
---|
115 | else
|
---|
116 | {
|
---|
117 | m_active = false;
|
---|
118 | m_last_frame = m_stop_frame;
|
---|
119 | m_next_frame = m_stop_frame;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | m_interpolation = f_pos - glm::floor( f_pos );
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | void nv::keyframed_mesh::draw( render_state& rstate )
|
---|
127 | {
|
---|
128 | nv::uint32 vtx_count = m_data->get_vertex_count();
|
---|
129 | if ( m_gpu_last_frame != m_last_frame )
|
---|
130 | {
|
---|
131 | m_va->update_vertex_buffer( m_loc_last_position, m_last_frame * vtx_count * sizeof( nv::vec3 ) );
|
---|
132 | m_va->update_vertex_buffer( m_loc_last_normal, m_last_frame * vtx_count * sizeof( nv::vec3 ) );
|
---|
133 | m_gpu_last_frame = m_last_frame;
|
---|
134 | }
|
---|
135 | if ( m_gpu_next_frame != m_next_frame )
|
---|
136 | {
|
---|
137 | m_va->update_vertex_buffer( m_loc_next_position, m_next_frame * vtx_count * sizeof( nv::vec3 ) );
|
---|
138 | m_va->update_vertex_buffer( m_loc_next_normal, m_next_frame * vtx_count * sizeof( nv::vec3 ) );
|
---|
139 | m_gpu_next_frame = m_next_frame;
|
---|
140 | }
|
---|
141 | m_program->set_uniform( "interpolate", m_interpolation );
|
---|
142 | m_context->draw( nv::TRIANGLES, rstate, m_program, m_va, m_data->get_index_count() );
|
---|
143 | }
|
---|
144 |
|
---|
145 | nv::keyframed_mesh::~keyframed_mesh()
|
---|
146 | {
|
---|
147 | delete m_va;
|
---|
148 | }
|
---|