Changeset 239 for trunk/tests/md5_test/md5_test.cc
- Timestamp:
- 05/17/14 02:35:19 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/md5_test/md5_test.cc
r231 r239 18 18 #include <nv/time.hh> 19 19 #include <nv/string.hh> 20 #include <nv/interface/mesh.hh>21 20 #include <nv/gfx/skeletal_mesh.hh> 22 21 #include <glm/gtx/rotate_vector.hpp> 23 22 #include <glm/gtc/matrix_access.hpp> 24 23 #include <glm/gtx/matrix_interpolation.hpp> 25 26 class mesh_part27 {28 public:29 mesh_part( const std::string& path, nv::program* program, nv::window* window )30 : m_mesh( nullptr ), m_program( program ), m_loader( nullptr ), m_animation( nullptr ), m_window( window )31 {32 33 NV_PROFILE("mesh_construct");34 {35 NV_PROFILE("loader->load");36 nv::c_file_system fs;37 nv::stream* mesh_file = fs.open( path.c_str() );38 m_loader = new nv::md5_loader();39 m_loader->load( *mesh_file );40 delete mesh_file;41 }42 43 {44 NV_PROFILE("create_mesh");45 m_mesh = new nv::skeletal_mesh( window->get_context(), m_loader );46 }47 48 }49 50 void load_animation( const std::string& path )51 {52 delete m_animation;53 m_animation = nullptr;54 NV_PROFILE("load_animation");55 nv::c_file_system fs;56 nv::stream* anim_file = fs.open( path.c_str() );57 58 if ( anim_file != nullptr )59 {60 m_animation = new nv::md5_animation();61 if ( !m_animation->load_animation(*anim_file) )62 {63 delete m_animation;64 m_animation = nullptr;65 }66 m_mesh->setup_animation( m_animation );67 delete anim_file;68 }69 }70 71 void update( nv::uint32 ms )72 {73 m_mesh->update( ms );74 }75 76 void draw( nv::context* context, nv::render_state& rstate, const glm::mat4& m, const glm::mat4& v, const glm::mat4& p )77 {78 NV_PROFILE( "mesh-draw" );79 glm::mat4 mv = v * m;80 m_program->set_opt_uniform( "nv_m_model", m );81 m_program->set_opt_uniform( "nv_m_modelview", mv );82 m_program->set_opt_uniform( "nv_m_view_inv", glm::inverse( v ) );83 m_program->set_opt_uniform( "nv_m_normal", glm::transpose(glm::inverse(glm::mat3(mv))) );84 m_program->set_uniform( "matrix_mvp", p * mv );85 context->draw( rstate, m_program, m_mesh );86 }87 88 ~mesh_part()89 {90 delete m_mesh;91 }92 93 private:94 nv::skeletal_mesh* m_mesh;95 nv::program* m_program;96 nv::md5_loader* m_loader;97 nv::md5_animation* m_animation;98 nv::window* m_window;99 };100 24 101 25 class application … … 107 31 ~application(); 108 32 protected: 33 void load_animation( const std::string& path ); 34 protected: 35 36 109 37 nv::device* m_device; 110 38 nv::window* m_window; … … 114 42 nv::clear_state m_clear_state; 115 43 nv::render_state m_render_state; 116 117 mesh_part* m_mesh; 118 nv::program* m_program; 44 nv::scene_state m_scene_state; 45 46 nv::skeletal_mesh* m_mesh; 47 nv::program* m_program; 48 nv::md5_mesh_data* m_mesh_data; 49 nv::md5_animation* m_animation; 50 119 51 }; 120 52 … … 124 56 m_device = new nv::gl_device(); 125 57 m_window = m_device->create_window( 800, 600, false ); 58 m_animation = nullptr; 126 59 127 60 nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::REPEAT ); … … 152 85 NV_PROFILE( "app_initialize" ); 153 86 m_program = m_device->create_program( nv::slurp( "md5.vert" ), nv::slurp( "md5.frag" ) ); 154 m_mesh = new mesh_part( "data/qshambler.md5mesh", m_program, m_window ); 155 m_mesh->load_animation( "data/idle02.md5anim" ); 87 88 nv::md5_loader* loader = nullptr; 89 { 90 NV_PROFILE("loader->load"); 91 nv::c_file_system fs; 92 nv::stream* mesh_file = fs.open( "data/qshambler.md5mesh" ); 93 loader = new nv::md5_loader(); 94 loader->load( *mesh_file ); 95 delete mesh_file; 96 } 97 98 { 99 NV_PROFILE("create_mesh"); 100 m_mesh_data = (nv::md5_mesh_data*)loader->release_mesh_data(); 101 m_mesh = new nv::skeletal_mesh( m_window->get_context(), m_mesh_data ); 102 delete loader; 103 } 104 105 load_animation( "data/idle02.md5anim" ); 156 106 return true; 157 107 } … … 182 132 } 183 133 184 glm::mat4 view;185 glm::mat4 projection;186 134 { 187 135 NV_PROFILE( "update_sh" ); … … 190 138 glm::vec3 eye = glm::rotate( source, (ticks / 20.f), glm::vec3( 0.0,1.0,0.0 ) ); 191 139 192 view = glm::lookAt(eye + move, glm::vec3(0.0f, 0.0f, 0.0f) + move, glm::vec3(0.0, 1.0, 0.0));193 projection = glm::perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f);140 m_scene_state.get_camera().set_lookat(eye + move, glm::vec3(0.0f, 0.0f, 0.0f) + move, glm::vec3(0.0, 1.0, 0.0)); 141 m_scene_state.get_camera().set_perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f); 194 142 195 143 m_diffuse ->bind( 0 ); 196 144 m_specular->bind( 1 ); 197 145 m_normal ->bind( 2 ); 198 m_program->set_opt_uniform( "nv_m_projection", projection ); 199 m_program->set_uniform( "light_position", glm::vec3(180.0, 180.0, 0) ); 200 m_program->set_uniform( "light_diffuse", glm::vec4(0.7,0.7,0.7,1.0) ); 201 m_program->set_uniform( "light_specular", glm::vec4(1.0,1.0,1.0,1.0) ); 202 m_program->set_uniform( "diffuse", 0 ); 203 m_program->set_uniform( "specular", 1 ); 204 m_program->set_uniform( "normalmap", 2 ); 146 m_program->set_opt_uniform( "light_position", glm::vec3(180.0, 180.0, 0) ); 147 m_program->set_opt_uniform( "light_diffuse", glm::vec4(0.7,0.7,0.7,1.0) ); 148 m_program->set_opt_uniform( "light_specular", glm::vec4(1.0,1.0,1.0,1.0) ); 205 149 } 206 150 207 151 { 208 152 NV_PROFILE( "draw" ); 209 glm::mat4 model = 210 glm::mat4(1.f,0.f,0.f,0.f, 211 0.f,0.f,1.f,0.f, 212 0.f,1.f,0.f,0.f, 213 0.f,0.f,0.f,1.f); 214 215 m_mesh->draw( m_window->get_context(), m_render_state, model, view, projection ); 153 m_scene_state.set_model(nv::mat4( 154 1.f,0.f,0.f,0.f, 155 0.f,0.f,1.f,0.f, 156 0.f,1.f,0.f,0.f, 157 0.f,0.f,0.f,1.f 158 ) ); 159 160 m_window->get_context()->draw( m_render_state, m_scene_state, m_program, m_mesh ); 216 161 } 217 162 … … 251 196 } 252 197 198 void application::load_animation( const std::string& path ) 199 { 200 delete m_animation; 201 m_animation = nullptr; 202 NV_PROFILE("load_animation"); 203 nv::c_file_system fs; 204 nv::stream* anim_file = fs.open( path.c_str() ); 205 206 if ( anim_file != nullptr ) 207 { 208 m_animation = new nv::md5_animation(); 209 if ( !m_animation->load_animation(*anim_file) ) 210 { 211 delete m_animation; 212 m_animation = nullptr; 213 } 214 m_mesh->setup_animation( m_animation ); 215 delete anim_file; 216 } 217 } 218 219 253 220 application::~application() 254 221 {
Note: See TracChangeset
for help on using the changeset viewer.