#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include class application { public: application(); bool initialize(); bool run(); ~application(); protected: void load_animation( const std::string& path ); protected: nv::device* m_device; nv::window* m_window; nv::context* m_context; nv::texture m_diffuse; nv::texture m_specular; nv::texture m_normal; nv::clear_state m_clear_state; nv::render_state m_render_state; nv::scene_state m_scene_state; nv::skeletal_mesh* m_mesh; nv::program m_program; nv::mesh_data* m_mesh_data; nv::animation_entry* m_animation; }; application::application() { NV_PROFILE( "app_construct" ); m_device = new nv::gl_device(); m_window = m_device->create_window( 800, 600, false ); m_context = m_window->get_context(); m_animation = nullptr; nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::REPEAT ); nv::image_data* data = m_device->create_image_data( "data/qshambler_d.png" ); m_diffuse = m_device->create_texture( data, sampler ); delete data; data = m_device->create_image_data( "data/qshambler_s.png" ); m_specular = m_device->create_texture( data, sampler ); delete data; data = m_device->create_image_data( "data/qshambler_local.png" ); m_normal = m_device->create_texture( data, sampler ); delete data; m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER; m_render_state.depth_test.enabled = true; m_render_state.culling.enabled = true; m_render_state.culling.order = nv::culling::CCW; m_render_state.blending.enabled = false; m_render_state.blending.src_rgb_factor = nv::blending::SRC_ALPHA; m_render_state.blending.dst_rgb_factor = nv::blending::ONE_MINUS_SRC_ALPHA; m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA; m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA; } bool application::initialize() { NV_PROFILE( "app_initialize" ); m_program = m_device->create_program( nv::slurp( "md5.vert" ), nv::slurp( "md5.frag" ) ); nv::md5_loader* loader = nullptr; { NV_PROFILE("loader->load"); nv::c_file_system fs; nv::stream* mesh_file = fs.open( "data/qshambler.md5mesh" ); loader = new nv::md5_loader(); loader->load( *mesh_file ); delete mesh_file; } { NV_PROFILE("create_mesh"); m_mesh_data = loader->release_mesh_data(); m_mesh = new nv::skeletal_mesh_cpu( m_context, m_mesh_data, loader->release_mesh_nodes_data() ); delete loader; } load_animation( "data/walk.md5anim" ); return true; } bool application::run() { nv::profiler::pointer()->log_report(); NV_PROFILE( "app_run" ); int keypress = 0; glm::vec3 move( 0, 50.f, 0 ); nv::uint32 ticks = m_device->get_ticks(); nv::uint32 last_ticks; nv::fps_counter_class< nv::system_us_timer > fps_counter; //m_mesh->setup_animation( 0, m_mesh->get_max_frames(), 2, true ); while(!keypress) { last_ticks = ticks; ticks = m_device->get_ticks(); m_mesh->update_animation( m_animation, ticks ); m_context->clear( m_clear_state ); { NV_PROFILE( "update_sh" ); glm::vec3 source( 250.0f, 0.0f, 0.0f ); glm::vec3 eye = glm::rotate( source, (ticks / 20.f), glm::vec3( 0.0,1.0,0.0 ) ); 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)); m_scene_state.get_camera().set_perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f); m_context->bind( m_diffuse, nv::TEX_DIFFUSE ); m_context->bind( m_specular, nv::TEX_SPECULAR ); m_context->bind( m_normal, nv::TEX_NORMAL ); m_device->set_opt_uniform( m_program, "light_position", glm::vec3(180.0, 180.0, 0) ); m_device->set_opt_uniform( m_program, "light_diffuse", glm::vec4(0.7,0.7,0.7,1.0) ); m_device->set_opt_uniform( m_program, "light_specular", glm::vec4(1.0,1.0,1.0,1.0) ); } { NV_PROFILE( "draw" ); m_scene_state.set_model(nv::mat4( 1.f,0.f,0.f,0.f, 0.f,0.f,1.f,0.f, 0.f,1.f,0.f,0.f, 0.f,0.f,0.f,1.f ) ); m_mesh->update( m_program ); m_context->draw( nv::TRIANGLES, m_render_state, m_scene_state, m_program, m_mesh->get_vertex_array(), m_mesh->get_index_count() ); } { NV_PROFILE( "swap" ); m_window->swap_buffers(); } { NV_PROFILE( "pollevent" ); nv::io_event event; while(m_window->poll_event(event)) { switch (event.type) { case nv::EV_QUIT: keypress = 1; break; case nv::EV_KEY: if (event.key.pressed) { switch (event.key.code) { case nv::KEY_ESCAPE : keypress = 1; break; case nv::KEY_F1 : nv::profiler::pointer()->log_report(); break; default: break; } } break; default: break; } } } fps_counter.tick(); } return true; } void application::load_animation( const std::string& path ) { delete m_animation; m_animation = nullptr; NV_PROFILE("load_animation"); nv::c_file_system fs; nv::stream* anim_file = fs.open( path.c_str() ); if ( anim_file != nullptr ) { nv::md5_loader* loader = new nv::md5_loader(); loader->load( *anim_file ); delete anim_file; m_animation = new nv::skeletal_animation_entry_cpu( "", loader->release_mesh_nodes_data(), true ); m_mesh->run_animation( m_animation ); delete loader; } } application::~application() { m_device->release( m_program ); delete m_mesh; m_device->release( m_diffuse ); delete m_window; delete m_device; } int main(int, char* []) { nv::logger log(nv::LOG_TRACE); log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE ); log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE ); NV_LOG( nv::LOG_NOTICE, "Logging started" ); application app; if ( app.initialize() ) { app.run(); } NV_LOG( nv::LOG_NOTICE, "Logging stopped" ); return 0; }