[231] | 1 | #include <nv/common.hh>
|
---|
| 2 | #include <iomanip>
|
---|
| 3 | #include <nv/gfx/keyframed_mesh.hh>
|
---|
| 4 | #include <nv/interface/vertex_buffer.hh>
|
---|
| 5 | #include <nv/gl/gl_device.hh>
|
---|
| 6 | #include <nv/gfx/image.hh>
|
---|
| 7 | #include <nv/interface/context.hh>
|
---|
| 8 | #include <nv/interface/window.hh>
|
---|
| 9 | #include <nv/interface/mesh_loader.hh>
|
---|
| 10 | #include <nv/io/c_file_system.hh>
|
---|
| 11 | #include <nv/formats/md3_loader.hh>
|
---|
| 12 | #include <nv/profiler.hh>
|
---|
| 13 | #include <nv/logging.hh>
|
---|
| 14 | #include <nv/logger.hh>
|
---|
| 15 | #include <nv/math.hh>
|
---|
| 16 | #include <nv/time.hh>
|
---|
| 17 | #include <nv/string.hh>
|
---|
| 18 | #include <glm/gtx/rotate_vector.hpp>
|
---|
| 19 | #include <glm/gtc/matrix_access.hpp>
|
---|
| 20 | #include <glm/gtx/matrix_interpolation.hpp>
|
---|
| 21 |
|
---|
[304] | 22 | bool GPU_ANIMATION = true;
|
---|
[231] | 23 |
|
---|
| 24 | class mesh_part
|
---|
| 25 | {
|
---|
| 26 | public:
|
---|
[304] | 27 | mesh_part( const std::string& path, nv::window* window )
|
---|
[231] | 28 | {
|
---|
| 29 | NV_PROFILE("mesh_construct");
|
---|
| 30 | nv::md3_loader* loader;
|
---|
| 31 | {
|
---|
| 32 | NV_PROFILE("loader->load");
|
---|
| 33 | nv::c_file_system fs;
|
---|
| 34 | nv::stream* mesh_file = fs.open( path.c_str() );
|
---|
| 35 | loader = new nv::md3_loader();
|
---|
| 36 | loader->load( *mesh_file );
|
---|
| 37 | delete mesh_file;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | {
|
---|
| 41 | NV_PROFILE("create_mesh_data");
|
---|
[304] | 42 | m_mesh_data = loader->release_mesh_data_pack();
|
---|
[231] | 43 | }
|
---|
| 44 | delete loader;
|
---|
[304] | 45 | m_entry = nullptr;
|
---|
[231] | 46 | {
|
---|
| 47 | NV_PROFILE("create_mesh");
|
---|
| 48 | if ( GPU_ANIMATION )
|
---|
[304] | 49 | m_mesh = new nv::keyframed_mesh_gpu( window->get_context(), m_mesh_data->get_mesh(0), m_mesh_data->get_nodes() );
|
---|
[231] | 50 | else
|
---|
[304] | 51 | m_mesh = new nv::keyframed_mesh_cpu( window->get_context(), m_mesh_data->get_mesh(0), m_mesh_data->get_nodes() );
|
---|
[231] | 52 | }
|
---|
| 53 |
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[304] | 56 | nv::mat4 get_transform( nv::uint32 id )
|
---|
[231] | 57 | {
|
---|
[304] | 58 | return m_mesh->get_node_transform( id ).extract();
|
---|
[231] | 59 | }
|
---|
| 60 |
|
---|
| 61 | void setup_animation( nv::uint32 start, nv::uint32 stop, nv::uint32 fps, bool loop )
|
---|
| 62 | {
|
---|
[304] | 63 | delete m_entry;
|
---|
| 64 | m_entry = new nv::animation_entry("test", loop, fps, (float)start, float(stop-1) );
|
---|
| 65 | m_mesh->run_animation( m_entry );
|
---|
[231] | 66 | }
|
---|
| 67 |
|
---|
[304] | 68 | void update( nv::uint32 ms, nv::program program )
|
---|
[231] | 69 | {
|
---|
| 70 | m_mesh->update( ms );
|
---|
[304] | 71 | m_mesh->update_animation( m_entry, ms );
|
---|
[239] | 72 | m_mesh->update( program );
|
---|
[231] | 73 | }
|
---|
| 74 |
|
---|
| 75 | nv::mesh_interface* get_mesh() const { return m_mesh; }
|
---|
| 76 |
|
---|
| 77 | size_t get_max_frames()
|
---|
| 78 | {
|
---|
| 79 | return m_mesh->get_max_frames();
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | ~mesh_part()
|
---|
| 83 | {
|
---|
[239] | 84 | delete m_mesh_data;
|
---|
[231] | 85 | delete m_mesh;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | private:
|
---|
[304] | 89 | nv::mesh_data_pack* m_mesh_data;
|
---|
| 90 | nv::keyframed_mesh* m_mesh;
|
---|
| 91 | nv::animation_entry* m_entry;
|
---|
[231] | 92 | };
|
---|
| 93 |
|
---|
| 94 | class application
|
---|
| 95 | {
|
---|
| 96 | public:
|
---|
| 97 | application();
|
---|
| 98 | bool initialize();
|
---|
| 99 | bool run();
|
---|
| 100 | ~application();
|
---|
| 101 | protected:
|
---|
| 102 | nv::device* m_device;
|
---|
[304] | 103 | nv::context* m_context;
|
---|
[231] | 104 | nv::window* m_window;
|
---|
[304] | 105 | nv::texture m_diffuse;
|
---|
| 106 | nv::texture m_diffuse_weapon;
|
---|
[239] | 107 |
|
---|
[231] | 108 | nv::clear_state m_clear_state;
|
---|
| 109 | nv::render_state m_render_state;
|
---|
[239] | 110 | nv::scene_state m_scene_state;
|
---|
[231] | 111 |
|
---|
| 112 | mesh_part* m_torso;
|
---|
| 113 | mesh_part* m_legs;
|
---|
| 114 | mesh_part* m_head;
|
---|
| 115 | mesh_part* m_weapon;
|
---|
[304] | 116 | nv::program m_program;
|
---|
[231] | 117 | };
|
---|
| 118 |
|
---|
| 119 | application::application()
|
---|
| 120 | {
|
---|
| 121 | NV_PROFILE( "app_construct" );
|
---|
[304] | 122 | m_device = new nv::gl_device();
|
---|
| 123 | m_window = m_device->create_window( 800, 600, false );
|
---|
| 124 | m_context = m_window->get_context();
|
---|
[231] | 125 |
|
---|
| 126 | nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::REPEAT );
|
---|
| 127 | nv::image_data* data = m_device->create_image_data( "data/doom.png" );
|
---|
[304] | 128 | m_diffuse = m_device->create_texture( data->get_size(), nv::image_format( nv::RGBA, nv::UBYTE ), sampler, (void*)data->get_data() );
|
---|
[231] | 129 | delete data;
|
---|
| 130 | data = m_device->create_image_data( "data/rocketl.png" );
|
---|
[304] | 131 | m_diffuse_weapon = m_device->create_texture( data->get_size(), nv::image_format( nv::RGBA, nv::UBYTE ), sampler, (void*)data->get_data() );
|
---|
[231] | 132 | delete data;
|
---|
| 133 |
|
---|
| 134 | m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
|
---|
| 135 | m_render_state.depth_test.enabled = true;
|
---|
| 136 | m_render_state.culling.enabled = true;
|
---|
| 137 | m_render_state.blending.enabled = false;
|
---|
| 138 | m_render_state.blending.src_rgb_factor = nv::blending::SRC_ALPHA;
|
---|
| 139 | m_render_state.blending.dst_rgb_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
|
---|
| 140 | m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
|
---|
| 141 | m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | bool application::initialize()
|
---|
| 145 | {
|
---|
| 146 | NV_PROFILE( "app_initialize" );
|
---|
| 147 | m_program = m_device->create_program(
|
---|
| 148 | nv::slurp( GPU_ANIMATION ? "obj_gpu.vert" : "obj_cpu.vert" ),
|
---|
| 149 | nv::slurp( "obj.frag" )
|
---|
| 150 | );
|
---|
[304] | 151 | m_torso = new mesh_part( "data/upper.md3", m_window );
|
---|
| 152 | m_legs = new mesh_part( "data/lower.md3", m_window );
|
---|
| 153 | m_head = new mesh_part( "data/head.md3", m_window );
|
---|
| 154 | m_weapon = new mesh_part( "data/rocketl.md3", m_window );
|
---|
[231] | 155 | return true;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | bool application::run()
|
---|
| 159 | {
|
---|
| 160 | nv::profiler::pointer()->log_report();
|
---|
| 161 | NV_PROFILE( "app_run" );
|
---|
| 162 | int keypress = 0;
|
---|
| 163 |
|
---|
| 164 | glm::vec3 move( 0, 0, 0 );
|
---|
| 165 |
|
---|
| 166 | nv::uint32 ticks = m_device->get_ticks();
|
---|
| 167 | nv::uint32 last_ticks;
|
---|
| 168 | nv::fps_counter_class< nv::system_us_timer > fps_counter;
|
---|
| 169 |
|
---|
| 170 | m_legs->setup_animation( 0, m_legs->get_max_frames(), 20, true );
|
---|
| 171 | m_torso->setup_animation( 0, m_torso->get_max_frames(), 20, true );
|
---|
| 172 |
|
---|
| 173 | bool common = true;
|
---|
| 174 | while(!keypress)
|
---|
| 175 | {
|
---|
| 176 | last_ticks = ticks;
|
---|
| 177 | ticks = m_device->get_ticks();
|
---|
| 178 | nv::uint32 elapsed = ticks - last_ticks;
|
---|
[304] | 179 | m_torso->update( ticks, m_program );
|
---|
| 180 | m_legs->update( ticks, m_program );
|
---|
[231] | 181 | {
|
---|
| 182 | NV_PROFILE( "clear" );
|
---|
[304] | 183 | m_context->clear( m_clear_state );
|
---|
[231] | 184 | }
|
---|
| 185 |
|
---|
| 186 | glm::mat4 view;
|
---|
| 187 | glm::mat4 projection;
|
---|
| 188 | {
|
---|
| 189 | NV_PROFILE( "update_sh" );
|
---|
| 190 |
|
---|
| 191 | glm::vec3 source( 80.0f, 0.0f, 0.0f );
|
---|
| 192 | glm::vec3 eye = glm::rotate( source, (ticks / 20.f), glm::vec3( 0.0,1.0,0.0 ) );
|
---|
| 193 |
|
---|
[239] | 194 | 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));
|
---|
| 195 | m_scene_state.get_camera().set_perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f);
|
---|
[231] | 196 |
|
---|
[304] | 197 | m_context->bind( m_diffuse, nv::TEX_DIFFUSE );
|
---|
| 198 | m_device->set_uniform( m_program, "light_position", glm::vec3(120.0, 120.0, 0) );
|
---|
| 199 | m_device->set_uniform( m_program, "light_diffuse", glm::vec4(1.0,1.0,1.0,1.0) );
|
---|
| 200 | m_device->set_uniform( m_program, "light_specular", glm::vec4(1.0,1.0,1.0,1.0) );
|
---|
[231] | 201 | }
|
---|
| 202 |
|
---|
| 203 | {
|
---|
| 204 | NV_PROFILE( "draw" );
|
---|
| 205 | glm::mat4 model = glm::mat4(1.0f);
|
---|
| 206 |
|
---|
[239] | 207 | m_scene_state.set_model( model );
|
---|
| 208 | m_window->get_context()->draw( m_render_state, m_scene_state, m_program, m_legs->get_mesh() );
|
---|
| 209 |
|
---|
[231] | 210 | //model = m_legs->get_transform( "tag_torso", last_legs_frame, legs_frame, legs_interpolate );
|
---|
[304] | 211 | model = m_legs->get_transform( 0 );
|
---|
[239] | 212 | m_scene_state.set_model( model );
|
---|
| 213 | m_window->get_context()->draw( m_render_state, m_scene_state, m_program, m_torso->get_mesh() );
|
---|
[231] | 214 |
|
---|
[304] | 215 | glm::mat4 head = model * m_torso->get_transform( 0 ); //, last_torso_frame, torso_frame, torso_interpolate );
|
---|
[239] | 216 | m_scene_state.set_model( head );
|
---|
| 217 | m_window->get_context()->draw( m_render_state, m_scene_state, m_program, m_head->get_mesh() );
|
---|
[231] | 218 |
|
---|
[304] | 219 | glm::mat4 weapon = model * m_torso->get_transform( 2 ); //, last_torso_frame, torso_frame, torso_interpolate );
|
---|
[239] | 220 | m_scene_state.set_model( weapon );
|
---|
[304] | 221 | m_context->bind( m_diffuse_weapon, nv::TEX_DIFFUSE );
|
---|
| 222 | m_context->draw( m_render_state, m_scene_state, m_program, m_weapon->get_mesh() );
|
---|
[231] | 223 |
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | {
|
---|
| 227 | NV_PROFILE( "swap" );
|
---|
| 228 | m_window->swap_buffers();
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | {
|
---|
| 232 | NV_PROFILE( "pollevent" );
|
---|
| 233 | nv::io_event event;
|
---|
| 234 | while(m_window->poll_event(event))
|
---|
| 235 | {
|
---|
| 236 | switch (event.type)
|
---|
| 237 | {
|
---|
| 238 | case nv::EV_QUIT:
|
---|
| 239 | keypress = 1;
|
---|
| 240 | break;
|
---|
| 241 | case nv::EV_KEY:
|
---|
| 242 | if (event.key.pressed)
|
---|
| 243 | {
|
---|
| 244 | switch (event.key.code)
|
---|
| 245 | {
|
---|
| 246 | case nv::KEY_ESCAPE : keypress = 1; break;
|
---|
| 247 | case nv::KEY_1 : m_legs->setup_animation( 0, 89, 20, true ); m_torso->setup_animation( 0, 89, 20, true ); common = true; break;
|
---|
| 248 | case nv::KEY_2 : m_legs->setup_animation( 90, 8, 20, true ); if (common) m_torso->setup_animation( 151, 1, 20, true ); common = false; break;
|
---|
| 249 | case nv::KEY_3 : m_legs->setup_animation( 98, 12, 20, true ); if (common) m_torso->setup_animation( 151, 1, 20, true ); common = false; break;
|
---|
| 250 | case nv::KEY_4 : m_legs->setup_animation( 110, 9, 15, true ); if (common) m_torso->setup_animation( 151, 1, 20, true ); common = false; break;
|
---|
| 251 | case nv::KEY_5 : m_legs->setup_animation( 138, 10, 15, true ); if (common) m_torso->setup_animation( 151, 1, 20, true ); common = false; break;
|
---|
| 252 | case nv::KEY_6 : m_legs->setup_animation( 171, 9, 15, true ); if (common) m_torso->setup_animation( 151, 1, 20, true ); common = false; break;
|
---|
| 253 | case nv::KEY_Q : m_torso->setup_animation( 90, 40, 20, true ); if (common) m_legs->setup_animation( 171, 10, 15, true ); common = false; break;
|
---|
| 254 | case nv::KEY_W : m_torso->setup_animation( 130, 6, 15, true ); if (common) m_legs->setup_animation( 171, 10, 15, true ); common = false; break;
|
---|
| 255 | case nv::KEY_E : m_torso->setup_animation( 136, 6, 15, true ); if (common) m_legs->setup_animation( 171, 10, 15, true ); common = false; break;
|
---|
| 256 | case nv::KEY_R : m_torso->setup_animation( 142, 5, 20, true ); if (common) m_legs->setup_animation( 171, 10, 15, true ); common = false; break;
|
---|
| 257 | case nv::KEY_T : m_torso->setup_animation( 147, 4, 20, true ); if (common) m_legs->setup_animation( 171, 10, 15, true ); common = false; break;
|
---|
| 258 | case nv::KEY_Y : m_torso->setup_animation( 151, 1, 15, true ); if (common) m_legs->setup_animation( 171, 10, 15, true ); common = false; break;
|
---|
| 259 | case nv::KEY_U : m_torso->setup_animation( 152, 1, 15, true ); if (common) m_legs->setup_animation( 171, 10, 15, true ); common = false; break;
|
---|
| 260 | case nv::KEY_F1 : nv::profiler::pointer()->log_report(); break;
|
---|
| 261 | default: break;
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 | break;
|
---|
| 265 | default: break;
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 | fps_counter.tick();
|
---|
| 270 | }
|
---|
| 271 | return true;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | application::~application()
|
---|
| 275 | {
|
---|
[304] | 276 | m_device->release( m_program );
|
---|
[231] | 277 | delete m_torso;
|
---|
| 278 | delete m_legs;
|
---|
| 279 | delete m_head;
|
---|
| 280 | delete m_weapon;
|
---|
[304] | 281 | m_device->release( m_diffuse );
|
---|
| 282 | m_device->release( m_diffuse_weapon );
|
---|
[231] | 283 | delete m_window;
|
---|
| 284 | delete m_device;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 |
|
---|
| 288 | int main(int, char* [])
|
---|
| 289 | {
|
---|
| 290 | nv::logger log(nv::LOG_TRACE);
|
---|
| 291 | log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
|
---|
| 292 | log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
|
---|
| 293 |
|
---|
| 294 | NV_LOG( nv::LOG_NOTICE, "Logging started" );
|
---|
| 295 | application app;
|
---|
| 296 | if ( app.initialize() )
|
---|
| 297 | {
|
---|
| 298 | app.run();
|
---|
| 299 | }
|
---|
| 300 | NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
|
---|
| 301 |
|
---|
| 302 | return 0;
|
---|
| 303 | }
|
---|
| 304 |
|
---|