Changeset 223
- Timestamp:
- 01/02/14 16:50:01 (11 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/nv/gfx/keyframed_mesh.hh
r204 r223 18 18 public: 19 19 keyframed_mesh( context* a_context, keyframed_mesh_data* a_data, program* a_program ); 20 size_t get_max_frames() const; 20 21 mat4 get_tag( const std::string& tag ) const; 21 size_t get_max_frames() const; 22 void set_frame( uint32 frame ); 23 void setup_animation( uint32 start, uint32 count, uint32 fps, bool loop ); 24 void update( uint32 ms ); 25 void draw( render_state& rstate ); 22 virtual void setup_animation( uint32 start, uint32 count, uint32 fps, bool loop ); 23 virtual void set_frame( uint32 frame ); 24 virtual void update( uint32 ms ); 25 virtual void draw( render_state& rstate ); 26 26 program* get_program() { return m_program; } 27 ~keyframed_mesh();28 pr ivate:27 virtual ~keyframed_mesh(); 28 protected: 29 29 context* m_context; 30 30 keyframed_mesh_data* m_data; … … 32 32 vertex_array* m_va; 33 33 34 uint32 m_start_frame; 35 uint32 m_stop_frame; 36 uint32 m_last_frame; 37 uint32 m_next_frame; 38 uint32 m_time; 39 uint32 m_fps; 40 f32 m_interpolation; 41 bool m_looping; 42 bool m_active; 43 }; 44 45 46 class keyframed_mesh_gpu : public keyframed_mesh 47 { 48 public: 49 keyframed_mesh_gpu( context* a_context, keyframed_mesh_data* a_data, program* a_program ); 50 void draw( render_state& rstate ); 51 private: 34 52 int m_loc_next_position; 35 53 int m_loc_next_normal; 36 54 37 uint32 m_last_frame;38 uint32 m_next_frame;39 55 uint32 m_gpu_last_frame; 40 56 uint32 m_gpu_next_frame; 41 f32 m_interpolation; 42 uint32 m_start_frame; 43 uint32 m_stop_frame; 44 uint32 m_time; 45 uint32 m_fps; 46 bool m_looping; 47 bool m_active; 57 }; 58 59 class keyframed_mesh_cpu : public keyframed_mesh 60 { 61 public: 62 keyframed_mesh_cpu( context* a_context, keyframed_mesh_data* a_data, program* a_program ); 63 void update( uint32 ms ); 64 private: 65 std::vector<vec3> m_position; 66 std::vector<vec3> m_normal; 67 vertex_buffer* m_vb_position; 68 vertex_buffer* m_vb_normal; 48 69 }; 49 70 -
trunk/src/gfx/keyframed_mesh.cc
r204 r223 20 20 , m_program( a_program ) 21 21 , m_va( nullptr ) 22 , m_ loc_next_position( 0)23 , m_ loc_next_normal( 0)22 , m_start_frame( false ) 23 , m_stop_frame( false ) 24 24 , m_last_frame( 0 ) 25 25 , m_next_frame( 0 ) 26 , m_ gpu_last_frame( 0xFFFFFFFF)27 , m_ gpu_next_frame( 0xFFFFFFFF)26 , m_time( 0 ) 27 , m_fps( 0 ) 28 28 , m_interpolation( 0.0f ) 29 , m_start_frame( false )30 , m_stop_frame( false )31 29 , m_looping( false ) 32 30 , m_active( false ) 33 31 { 34 32 m_va = m_context->get_device()->create_vertex_array(); 35 36 nv::vertex_buffer* vb; 37 m_loc_next_position = m_program->get_attribute( "nv_next_position" )->get_location(); 38 m_loc_next_normal = m_program->get_attribute( "nv_next_normal" )->get_location(); 39 40 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() ); 41 m_va->add_vertex_buffer( m_loc_next_position, vb, nv::FLOAT, 3, 0, 0, false ); 42 m_va->add_vertex_buffer( nv::POSITION, vb, nv::FLOAT, 3 ); 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_normals().data() ); 45 m_va->add_vertex_buffer( m_loc_next_normal, vb, nv::FLOAT, 3, 0, 0, false ); 46 m_va->add_vertex_buffer( nv::NORMAL, vb, nv::FLOAT, 3 ); 47 48 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() ); 49 m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 ); 50 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() ); 51 m_va->set_index_buffer( ib, nv::USHORT, true ); 33 } 34 35 size_t keyframed_mesh::get_max_frames() const 36 { 37 return m_data->get_frame_count(); 52 38 } 53 39 … … 56 42 const std::vector< nv::mat4 >& transforms = m_data->get_tag_map().at( tag ); 57 43 return glm::interpolate( transforms[ m_last_frame ], transforms[ m_next_frame ], m_interpolation ); 58 }59 60 size_t keyframed_mesh::get_max_frames() const61 {62 return m_data->get_frame_count();63 }64 65 void keyframed_mesh::set_frame( uint32 frame )66 {67 m_last_frame = frame;68 m_next_frame = frame;69 m_interpolation = 0.0f;70 m_active = false;71 44 } 72 45 … … 78 51 m_fps = fps; 79 52 m_active = count > 1; 53 m_time = 0; 80 54 m_last_frame = start; 81 55 m_next_frame = (count > 1 ? start + 1 : start ); 82 56 m_interpolation = 0.0f; 83 m_time = 0; 57 } 58 59 void 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; 84 65 } 85 66 … … 122 103 void nv::keyframed_mesh::draw( render_state& rstate ) 123 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 109 nv::keyframed_mesh::~keyframed_mesh() 110 { 111 delete m_va; 112 } 113 114 keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, keyframed_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::uint16 ), (void*)m_data->get_indices().data() ); 136 m_va->set_index_buffer( ib, nv::USHORT, true ); 137 } 138 139 void nv::keyframed_mesh_gpu::draw( render_state& rstate ) 140 { 124 141 size_t vtx_count = m_data->get_vertex_count(); 125 142 if ( m_gpu_last_frame != m_last_frame ) … … 135 152 m_gpu_next_frame = m_next_frame; 136 153 } 137 m_program->set_uniform( "nv_interpolate", m_interpolation ); 138 m_context->draw( nv::TRIANGLES, rstate, m_program, m_va, m_data->get_index_count() ); 139 } 140 141 nv::keyframed_mesh::~keyframed_mesh() 142 { 143 delete m_va; 144 } 154 keyframed_mesh::draw( rstate ); 155 } 156 157 158 nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, keyframed_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 ) * m_data->get_frame_count(), (void*)m_data->get_position_data(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 ) * m_data->get_frame_count(), (void*)m_data->get_normal_data(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::uint16 ), (void*)m_data->get_indices().data() ); 172 m_va->set_index_buffer( ib, nv::USHORT, true ); 173 174 m_position.resize( m_data->get_vertex_count() ); 175 m_normal.resize( m_data->get_vertex_count() ); 176 } 177 178 void 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_data( m_last_frame ); 184 const vec3* next_position = m_data->get_position_data( m_next_frame ); 185 const vec3* prev_normal = m_data->get_normal_data( m_last_frame ); 186 const vec3* next_normal = m_data->get_normal_data( 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 } -
trunk/tests/md2_test/md2_test.cc
r214 r223 47 47 { 48 48 NV_PROFILE("create_mesh"); 49 m_mesh = new nv::keyframed_mesh ( window->get_context(), m_mesh_data, program );49 m_mesh = new nv::keyframed_mesh_gpu( window->get_context(), m_mesh_data, program ); 50 50 } 51 51 … … 85 85 private: 86 86 nv::keyframed_mesh_data* m_mesh_data; 87 nv::keyframed_mesh *m_mesh;87 nv::keyframed_mesh_gpu* m_mesh; 88 88 }; 89 89
Note: See TracChangeset
for help on using the changeset viewer.