source: trunk/tests/md3_test/md3_test.cc @ 239

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