source: trunk/tests/md5_test/nv_md5_test.cc @ 321

Last change on this file since 321 was 321, checked in by epyon, 11 years ago
  • updated all tests to new nova
  • cleaned up tests paths
  • general cleanup of tests and test data
File size: 6.5 KB
Line 
1#include <nv/core/common.hh>
2#include <iomanip>
3#include <nv/gfx/keyframed_mesh.hh>
4#include <nv/gl/gl_device.hh>
5#include <nv/gfx/image.hh>
6#include <nv/interface/context.hh>
7#include <nv/interface/window.hh>
8#include <nv/interface/mesh_loader.hh>
9#include <nv/io/c_file_system.hh>
10#include <nv/formats/md5_loader.hh>
11#include <nv/core/profiler.hh>
12#include <nv/core/logging.hh>
13#include <nv/core/logger.hh>
14#include <nv/core/math.hh>
15#include <nv/core/time.hh>
16#include <nv/core/string.hh>
17#include <nv/gfx/skeletal_mesh.hh>
18#include <glm/gtx/rotate_vector.hpp>
19#include <glm/gtc/matrix_access.hpp>
20#include <glm/gtx/matrix_interpolation.hpp>
21
22class application
23{
24public:
25        application();
26        bool initialize();
27        bool run();
28        ~application();
29protected:
30        void load_animation( const std::string& path );
31protected:
32
33
34        nv::device*       m_device;
35        nv::window*       m_window;
36        nv::context*      m_context;
37        nv::texture       m_diffuse;
38        nv::texture       m_specular;
39        nv::texture       m_normal;
40        nv::clear_state   m_clear_state;
41        nv::render_state  m_render_state;
42        nv::scene_state   m_scene_state;
43
44        nv::skeletal_mesh* m_mesh;
45        nv::program        m_program;
46        nv::mesh_data*       m_mesh_data;
47        nv::animation_entry* m_animation;
48
49};
50
51application::application()
52{
53        NV_PROFILE( "app_construct" );
54        m_device  = new nv::gl_device();
55        m_window  = m_device->create_window( 800, 600, false );
56        m_context = m_window->get_context();
57        m_animation = nullptr;
58
59        nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::REPEAT );
60        nv::image_data* data = m_device->create_image_data( "data/qshambler_d.png" );
61        m_diffuse  = m_device->create_texture( data, sampler );
62        delete data;
63        data = m_device->create_image_data( "data/qshambler_s.png" );
64        m_specular = m_device->create_texture( data, sampler );
65        delete data;
66
67        data = m_device->create_image_data( "data/qshambler_local.png" );
68        m_normal = m_device->create_texture( data, sampler );
69        delete data;
70
71        m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
72        m_render_state.depth_test.enabled = true;
73        m_render_state.culling.enabled    = true;
74        m_render_state.culling.order      = nv::culling::CCW;
75        m_render_state.blending.enabled   = false;
76        m_render_state.blending.src_rgb_factor   = nv::blending::SRC_ALPHA;
77        m_render_state.blending.dst_rgb_factor   = nv::blending::ONE_MINUS_SRC_ALPHA;
78        m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
79        m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
80}
81
82bool application::initialize()
83{
84        NV_PROFILE( "app_initialize" );
85        m_program = m_device->create_program( nv::slurp( "md5.vert" ), nv::slurp( "md5.frag" ) );
86
87        nv::md5_loader* loader = nullptr;
88        {
89                NV_PROFILE("loader->load");
90                nv::c_file_system fs;
91                nv::stream* mesh_file = fs.open( "data/qshambler.md5mesh" );
92                loader = new nv::md5_loader();
93                loader->load( *mesh_file );
94                delete mesh_file;
95        }
96
97        {
98                NV_PROFILE("create_mesh");
99                m_mesh_data = loader->release_mesh_data();
100                m_mesh = new nv::skeletal_mesh_cpu( m_context, m_mesh_data, loader->release_mesh_nodes_data() );
101                delete loader;
102        }
103
104        load_animation( "data/walk.md5anim" );
105        return true;
106}
107
108bool application::run()
109{
110        nv::profiler::pointer()->log_report();
111        NV_PROFILE( "app_run" );
112        int keypress = 0;
113
114        glm::vec3 move( 0, 50.f, 0 );
115
116        nv::uint32 ticks   = m_device->get_ticks();
117        nv::uint32 last_ticks;
118        nv::fps_counter_class< nv::system_us_timer > fps_counter;
119
120        //m_mesh->setup_animation( 0, m_mesh->get_max_frames(), 2, true );
121
122        while(!keypress)
123        {
124                last_ticks = ticks;
125                ticks      = m_device->get_ticks();
126                m_mesh->update_animation( m_animation, ticks );
127                m_context->clear( m_clear_state );
128
129                {
130                        NV_PROFILE( "update_sh" );
131
132                        glm::vec3 source( 250.0f, 0.0f, 0.0f );
133                        glm::vec3 eye = glm::rotate( source, (ticks / 20.f), glm::vec3( 0.0,1.0,0.0 ) );
134
135                        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));
136                        m_scene_state.get_camera().set_perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f);
137
138                        m_context->bind( m_diffuse,  nv::TEX_DIFFUSE );
139                        m_context->bind( m_specular, nv::TEX_SPECULAR );
140                        m_context->bind( m_normal,   nv::TEX_NORMAL );
141                        m_device->set_opt_uniform( m_program, "light_position", glm::vec3(180.0, 180.0, 0) );
142                        m_device->set_opt_uniform( m_program, "light_diffuse",  glm::vec4(0.7,0.7,0.7,1.0) );
143                        m_device->set_opt_uniform( m_program, "light_specular", glm::vec4(1.0,1.0,1.0,1.0) );
144                }
145
146                {
147                        NV_PROFILE( "draw" );
148                        m_scene_state.set_model(nv::mat4(
149                                1.f,0.f,0.f,0.f,
150                                0.f,0.f,1.f,0.f,
151                                0.f,1.f,0.f,0.f,
152                                0.f,0.f,0.f,1.f
153                        ) );
154
155                        m_mesh->update( m_program );
156                        m_context->draw( nv::TRIANGLES, m_render_state, m_scene_state, m_program, m_mesh->get_vertex_array(), m_mesh->get_index_count() );
157                }
158
159                {
160                        NV_PROFILE( "swap" );
161                        m_window->swap_buffers();
162                }
163
164                {
165                        NV_PROFILE( "pollevent" );
166                        nv::io_event event;
167                        while(m_window->poll_event(event))
168                        {     
169                                switch (event.type)
170                                {
171                                case nv::EV_QUIT:
172                                        keypress = 1;
173                                        break;
174                                case nv::EV_KEY:
175                                        if (event.key.pressed)
176                                        {
177                                                switch (event.key.code)
178                                                {
179                                                case nv::KEY_ESCAPE : keypress = 1; break;
180                                                case nv::KEY_F1 : nv::profiler::pointer()->log_report(); break;
181                                                default: break;
182                                                }
183                                        }
184                                        break;
185                                default: break;
186                                }
187                        }
188                }
189                fps_counter.tick();
190        }
191        return true;
192}
193
194void application::load_animation( const std::string& path )
195{
196        delete m_animation;
197        m_animation = nullptr;
198        NV_PROFILE("load_animation");
199        nv::c_file_system fs;
200        nv::stream* anim_file = fs.open( path.c_str() );
201
202        if ( anim_file != nullptr )
203        {
204                nv::md5_loader* loader = new nv::md5_loader();
205                loader->load( *anim_file );
206                delete anim_file;
207                m_animation = new nv::skeletal_animation_entry_cpu( "", loader->release_mesh_nodes_data(), true );
208                m_mesh->run_animation( m_animation );
209                delete loader;
210        }
211}
212
213
214application::~application()
215{
216        m_device->release( m_program );
217        delete m_mesh;
218        m_device->release( m_diffuse );
219        delete m_window;
220        delete m_device;
221}
222
223
224int main(int, char* [])
225{
226        nv::logger log(nv::LOG_TRACE);
227        log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
228        log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
229       
230        NV_LOG( nv::LOG_NOTICE, "Logging started" );
231        application app;
232        if ( app.initialize() )
233        {
234                app.run();
235        }
236        NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
237
238        return 0;
239}
240
Note: See TracBrowser for help on using the repository browser.