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 | nv::keyframed_mesh::keyframed_mesh( context* a_context, const mesh_data* a_data, const mesh_nodes_data* a_tag_map )
|
---|
18 | : animated_mesh()
|
---|
19 | , m_context( a_context )
|
---|
20 | , m_mesh_data( a_data )
|
---|
21 | , m_tag_map( a_tag_map )
|
---|
22 | , m_last_frame( 0 )
|
---|
23 | , m_next_frame( 0 )
|
---|
24 | , m_interpolation( 0.0f )
|
---|
25 | , m_active( false )
|
---|
26 | {
|
---|
27 | m_index_count = m_mesh_data->get_index_channel()->count;
|
---|
28 | m_vertex_count = m_mesh_data->get_channel<vertex_t>()->count;
|
---|
29 | m_vchannel = m_mesh_data->get_channel<vertex_pnt>();
|
---|
30 | m_vsize = sizeof( vertex_pnt );
|
---|
31 | m_has_tangent = true;
|
---|
32 | if ( m_vchannel == nullptr )
|
---|
33 | {
|
---|
34 | m_vchannel = m_mesh_data->get_channel<vertex_pn>();
|
---|
35 | m_has_tangent = false;
|
---|
36 | m_vsize = sizeof( vertex_pn );
|
---|
37 | }
|
---|
38 | m_frame_count = m_vchannel->count / m_vertex_count;
|
---|
39 | m_pbuffer = buffer();
|
---|
40 | }
|
---|
41 |
|
---|
42 | size_t keyframed_mesh::get_max_frames() const
|
---|
43 | {
|
---|
44 | return m_frame_count;
|
---|
45 | }
|
---|
46 |
|
---|
47 | transform keyframed_mesh::get_node_transform( uint32 node_id ) const
|
---|
48 | {
|
---|
49 | if ( !m_tag_map ) return transform();
|
---|
50 | NV_ASSERT( node_id < m_tag_map->get_count(), "TAGMAP FAIL" );
|
---|
51 | const key_data* data = m_tag_map->get_node( node_id )->data;
|
---|
52 | NV_ASSERT( data, "TAG FAIL" );
|
---|
53 | transform last = data->get_raw_transform( m_last_frame );
|
---|
54 | transform next = data->get_raw_transform( m_next_frame );
|
---|
55 | return interpolate( last, next, m_interpolation );
|
---|
56 | }
|
---|
57 |
|
---|
58 | mat4 keyframed_mesh::get_node_matrix( uint32 node_id ) const
|
---|
59 | {
|
---|
60 | return get_node_transform( node_id ).extract();
|
---|
61 | }
|
---|
62 |
|
---|
63 | void nv::keyframed_mesh::set_frame( uint32 frame )
|
---|
64 | {
|
---|
65 | m_last_frame = frame;
|
---|
66 | m_next_frame = frame;
|
---|
67 | m_active = false;
|
---|
68 | m_interpolation = 0.0f;
|
---|
69 | }
|
---|
70 |
|
---|
71 | void nv::keyframed_mesh::update_animation( animation_entry* anim, uint32 a_anim_time )
|
---|
72 | {
|
---|
73 | if ( m_active )
|
---|
74 | {
|
---|
75 | float tick_time = ( (float)a_anim_time * 0.001f ) * anim->get_frame_rate();
|
---|
76 | float duration = anim->is_looping() ? anim->get_duration() + 1.0f : anim->get_duration();
|
---|
77 | if ( tick_time >= duration )
|
---|
78 | {
|
---|
79 | if ( anim->is_looping() )
|
---|
80 | {
|
---|
81 | tick_time = fmodf( tick_time, duration );
|
---|
82 | }
|
---|
83 | else
|
---|
84 | {
|
---|
85 | m_active = false;
|
---|
86 | m_last_frame = (uint32)anim->get_end();
|
---|
87 | m_next_frame = m_last_frame;
|
---|
88 | m_interpolation = 0.0f;
|
---|
89 | return;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | m_last_frame = (uint32)( glm::floor( tick_time ) + anim->get_start() );
|
---|
93 | m_next_frame = m_last_frame + 1;
|
---|
94 | if ( m_next_frame > (uint32)anim->get_end() ) m_next_frame = (uint32)anim->get_start();
|
---|
95 | m_interpolation = tick_time - glm::floor( tick_time );
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | void nv::keyframed_mesh::update( program a_program )
|
---|
101 | {
|
---|
102 | m_context->get_device()->set_opt_uniform( a_program, "nv_interpolate", m_interpolation );
|
---|
103 | }
|
---|
104 |
|
---|
105 | nv::keyframed_mesh::~keyframed_mesh()
|
---|
106 | {
|
---|
107 | m_context->release( m_va );
|
---|
108 | }
|
---|
109 |
|
---|
110 | void nv::keyframed_mesh::run_animation( animation_entry* a_anim )
|
---|
111 | {
|
---|
112 | if ( a_anim )
|
---|
113 | {
|
---|
114 | m_active = true;
|
---|
115 | m_last_frame = 0;
|
---|
116 | m_next_frame = 0;
|
---|
117 | m_interpolation = 0.0f;
|
---|
118 | }
|
---|
119 | else
|
---|
120 | {
|
---|
121 | m_active = false;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | nv::keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, const mesh_data* a_data, const mesh_nodes_data* a_tag_map )
|
---|
126 | : keyframed_mesh( a_context, a_data, a_tag_map )
|
---|
127 | , m_loc_next_position( -1 )
|
---|
128 | , m_loc_next_normal( -1 )
|
---|
129 | , m_loc_next_tangent( -1 )
|
---|
130 | , m_gpu_last_frame( 0xFFFFFFFF )
|
---|
131 | , m_gpu_next_frame( 0xFFFFFFFF )
|
---|
132 | {
|
---|
133 | m_va = a_context->create_vertex_array( a_data, STATIC_DRAW );
|
---|
134 | m_pbuffer = a_context->find_buffer( m_va, slot::POSITION );
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | void nv::keyframed_mesh_gpu::update( uint32 ms )
|
---|
139 | {
|
---|
140 | if ( m_loc_next_position == -1 ) return;
|
---|
141 | animated_mesh::update( ms );
|
---|
142 | if ( m_gpu_last_frame != m_last_frame )
|
---|
143 | {
|
---|
144 | uint32 base_offset = m_last_frame * m_vertex_count * m_vsize;
|
---|
145 | m_context->update_attribute_offset( m_va, slot::POSITION, base_offset );
|
---|
146 | m_context->update_attribute_offset( m_va, slot::NORMAL, base_offset + sizeof( vec3 ) );
|
---|
147 | if ( m_has_tangent && m_loc_next_tangent != -1 )
|
---|
148 | {
|
---|
149 | m_context->update_attribute_offset( m_va, slot::TANGENT, base_offset + 2*sizeof( vec3 ) );
|
---|
150 | }
|
---|
151 | m_gpu_last_frame = m_last_frame;
|
---|
152 | }
|
---|
153 | if ( m_loc_next_position != -1 && m_gpu_next_frame != m_next_frame )
|
---|
154 | {
|
---|
155 | uint32 base_offset = m_next_frame * m_vertex_count * m_vsize;
|
---|
156 | m_context->update_attribute_offset( m_va, (slot)m_loc_next_position, base_offset );
|
---|
157 | m_context->update_attribute_offset( m_va, (slot)m_loc_next_normal, base_offset + sizeof( vec3 ) );
|
---|
158 | if ( m_has_tangent && m_loc_next_tangent != -1 )
|
---|
159 | {
|
---|
160 | m_context->update_attribute_offset( m_va, (slot)m_loc_next_tangent, base_offset + 2*sizeof( vec3 ) );
|
---|
161 | }
|
---|
162 | m_gpu_next_frame = m_next_frame;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | void nv::keyframed_mesh_gpu::update( program a_program )
|
---|
167 | {
|
---|
168 | if ( m_loc_next_position == -1 )
|
---|
169 | {
|
---|
170 | device* dev = m_context->get_device();
|
---|
171 | m_loc_next_position = dev->get_attribute_location( a_program, "nv_next_position" );
|
---|
172 | m_loc_next_normal = dev->get_attribute_location( a_program, "nv_next_normal" );
|
---|
173 | if ( m_has_tangent )
|
---|
174 | m_loc_next_tangent = dev->get_attribute_location( a_program, "nv_next_tangent" );
|
---|
175 |
|
---|
176 | m_context->add_vertex_buffer( m_va, (slot)m_loc_next_position, m_pbuffer, FLOAT, 3, 0, m_vsize, false );
|
---|
177 | m_context->add_vertex_buffer( m_va, (slot)m_loc_next_normal, m_pbuffer, FLOAT, 3, sizeof( vec3 ), m_vsize, false );
|
---|
178 | if ( m_has_tangent )
|
---|
179 | m_context->add_vertex_buffer( m_va, (slot)m_loc_next_tangent, m_pbuffer, FLOAT, 4, 2*sizeof( vec3 ), m_vsize, false );
|
---|
180 | }
|
---|
181 | keyframed_mesh::update( a_program );
|
---|
182 | }
|
---|
183 |
|
---|
184 | nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, const mesh_data* a_data, const mesh_nodes_data* a_tag_map )
|
---|
185 | : keyframed_mesh( a_context, a_data, a_tag_map )
|
---|
186 | {
|
---|
187 | m_va = m_context->create_vertex_array();
|
---|
188 | m_pbuffer = m_context->get_device()->create_buffer( VERTEX_BUFFER, STATIC_DRAW, m_vertex_count * m_vsize, (void*)m_vchannel->data );
|
---|
189 | m_context->add_vertex_buffers( m_va, m_pbuffer, m_vchannel );
|
---|
190 |
|
---|
191 | buffer vb = m_context->get_device()->create_buffer( VERTEX_BUFFER, STATIC_DRAW, m_vertex_count * sizeof( vec2 ), (void*)m_mesh_data->get_channel<vertex_t>()->data );
|
---|
192 | m_context->add_vertex_buffers( m_va, vb, m_mesh_data->get_channel<vertex_t>() );
|
---|
193 |
|
---|
194 | buffer ib = m_context->get_device()->create_buffer( INDEX_BUFFER, STATIC_DRAW, m_mesh_data->get_index_channel()->size(), (void*)m_mesh_data->get_index_channel()->data );
|
---|
195 |
|
---|
196 | m_context->set_index_buffer( m_va, ib, m_mesh_data->get_index_channel()->desc.slots[0].etype, true );
|
---|
197 |
|
---|
198 | m_data = new uint8[ m_vertex_count * m_vsize ];
|
---|
199 | }
|
---|
200 |
|
---|
201 | void nv::keyframed_mesh_cpu::update( uint32 ms )
|
---|
202 | {
|
---|
203 | animated_mesh::update( ms );
|
---|
204 |
|
---|
205 | // TODO: this could be done generic for any data
|
---|
206 | if ( m_has_tangent )
|
---|
207 | {
|
---|
208 | const vertex_pnt* data = m_mesh_data->get_channel_data<vertex_pnt>();
|
---|
209 | const vertex_pnt* prev = data + m_vertex_count * m_last_frame;
|
---|
210 | const vertex_pnt* next = data + m_vertex_count * m_next_frame;
|
---|
211 | vertex_pnt* vtx = (vertex_pnt*)m_data;
|
---|
212 | for ( size_t i = 0; i < m_vertex_count; ++i )
|
---|
213 | {
|
---|
214 | vtx[i].position = glm::mix( prev[i].position, next[i].position, m_interpolation );
|
---|
215 | vtx[i].normal = glm::mix( prev[i].normal, next[i].normal, m_interpolation );
|
---|
216 | vtx[i].tangent = glm::mix( prev[i].tangent, next[i].tangent, m_interpolation );
|
---|
217 | }
|
---|
218 | }
|
---|
219 | else
|
---|
220 | {
|
---|
221 | const vertex_pn* data = m_mesh_data->get_channel_data<vertex_pn>();
|
---|
222 | const vertex_pn* prev = data + m_vertex_count * m_last_frame;
|
---|
223 | const vertex_pn* next = data + m_vertex_count * m_next_frame;
|
---|
224 | vertex_pn* vtx = (vertex_pn*)m_data;
|
---|
225 |
|
---|
226 | for ( size_t i = 0; i < m_vertex_count; ++i )
|
---|
227 | {
|
---|
228 | vtx[i].position = glm::mix( prev[i].position, next[i].position, m_interpolation );
|
---|
229 | vtx[i].normal = glm::mix( prev[i].normal, next[i].normal, m_interpolation );
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | m_context->update( m_pbuffer, m_data, 0, m_vertex_count * m_vsize );
|
---|
234 | }
|
---|
235 |
|
---|
236 | nv::keyframed_mesh_cpu::~keyframed_mesh_cpu()
|
---|
237 | {
|
---|
238 | delete[] m_data;
|
---|
239 | }
|
---|