source: trunk/tests/md2_test/md2_test.cc @ 189

Last change on this file since 189 was 189, checked in by epyon, 12 years ago
  • formats - full md2 implementation, including re-indexing and frame animation
  • tests/md2_test - with Mancubi For Added Fun (tm)
File size: 6.3 KB
RevLine 
[189]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/md2_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 <nv/types.hh>
21#include <nv/interface/mesh.hh>
22#include <glm/gtx/rotate_vector.hpp>
23#include <glm/gtc/matrix_access.hpp>
24#include <glm/gtx/matrix_interpolation.hpp>
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::md2_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::md2_loader();
38                        loader->load( *mesh_file );
39                        delete mesh_file;
40                }
41
42                {
43                        NV_PROFILE("create_mesh_data");
44                        m_mesh_data = new nv::keyframed_mesh_data( loader );
45                }
46                delete loader;
47
48                {
49                        NV_PROFILE("create_mesh");
50                        m_mesh      = new nv::keyframed_mesh( window->get_context(), m_mesh_data, program );
51                }
52
53        }
54
55        void setup_animation( nv::uint32 start, nv::uint32 stop, nv::uint32 fps, bool loop )
56        {
57                m_mesh->setup_animation( start, stop, fps, loop );
58        }
59
60        void update( nv::uint32 ms )
61        {
62                m_mesh->update( ms );
63        }
64
65        size_t get_max_frames()
66        {
67                return m_mesh->get_max_frames();
68        }
69
70        void draw( nv::render_state& rstate, const glm::mat4& m, const glm::mat4& v, const glm::mat4& p )
71        {
72                NV_PROFILE( "mesh-draw" );
73                glm::mat4 mv = v * m;
74                m_mesh->get_program()->set_opt_uniform( "nv_m_modelview", mv );
75                m_mesh->get_program()->set_opt_uniform( "nv_m_normal", glm::transpose(glm::inverse(glm::mat3(mv))) );
76                m_mesh->get_program()->set_uniform( "matrix_mvp", p * mv );
77                m_mesh->draw( rstate );
78        }
79
80        ~mesh_part()
81        {
82                delete m_mesh_data;
83                delete m_mesh;
84        }
85
86private:
87        nv::keyframed_mesh_data* m_mesh_data;
88        nv::keyframed_mesh*      m_mesh;
89};
90
91class application
92{
93public:
94        application();
95        bool initialize();
96        bool run();
97        ~application();
98protected:
99        nv::device*       m_device;
100        nv::window*       m_window;
101        nv::texture2d*    m_diffuse;
102        nv::clear_state   m_clear_state;
103        nv::render_state  m_render_state;
104
105        mesh_part*   m_mesh;
106        nv::program* m_program;
107};
108
109application::application()
110{
111        NV_PROFILE( "app_construct" );
112        m_device = new nv::gl_device();
113        m_window = m_device->create_window( 800, 600 );
114
115        nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::REPEAT );
116        nv::image_data* data = m_device->create_image_data( "data/manc.png" );
117        m_diffuse  = m_device->create_texture2d( data->get_size(), nv::RGB, nv::UBYTE, sampler, (void*)data->get_data() );
118        delete data;
119
120        m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
121        m_render_state.depth_test.enabled = true;
122        m_render_state.culling.enabled    = true;
123        m_render_state.blending.enabled   = false;
124        m_render_state.blending.src_rgb_factor   = nv::blending::SRC_ALPHA;
125        m_render_state.blending.dst_rgb_factor   = nv::blending::ONE_MINUS_SRC_ALPHA;
126        m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
127        m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
128}
129
130bool application::initialize()
131{
132        NV_PROFILE( "app_initialize" );
133        m_program = m_device->create_program( nv::slurp( "obj.vert" ), nv::slurp( "obj.frag" ) );
134        m_mesh    = new mesh_part( "data/manc.md2", m_program, m_window );
135        return true;
136}
137
138bool application::run()
139{
140        nv::profiler::pointer()->log_report();
141        NV_PROFILE( "app_run" );
142        int keypress = 0;
143
144        glm::vec3 move( 0, 25.f, 0 );
145
146        nv::uint32 ticks   = m_device->get_ticks();
147        nv::uint32 last_ticks;
148        nv::fps_counter_class< nv::system_us_timer > fps_counter;
149
150        m_mesh->setup_animation( 0, m_mesh->get_max_frames(), 2, true );
151
152        while(!keypress)
153        {
154                last_ticks = ticks;
155                ticks      = m_device->get_ticks();
156                nv::uint32 elapsed = ticks - last_ticks;
157                m_mesh->update( elapsed );
158                {
159                        NV_PROFILE( "clear" );
160                        m_window->get_context()->clear( m_clear_state );
161                }
162
163                glm::mat4 view;
164                glm::mat4 projection;
165                {
166                        NV_PROFILE( "update_sh" );
167
168                        glm::vec3 source( 100.0f, 0.0f, 0.0f );
169                        glm::vec3 eye = glm::rotate( source, (ticks / 20.f), glm::vec3( 0.0,1.0,0.0 ) );
170
171                        view       = glm::lookAt(eye + move, glm::vec3(0.0f, 0.0f, 0.0f) + move, glm::vec3(0.0, 1.0, 0.0));
172                        projection = glm::perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f);
173
174                        m_diffuse->bind( 0 );
175                        m_program->set_opt_uniform( "nv_m_projection", projection );
176                        m_program->set_uniform( "light_position", glm::vec3(120.0, 120.0, 0) );
177                        m_program->set_uniform( "light_diffuse",  glm::vec4(1.0,1.0,1.0,1.0) );
178                        m_program->set_uniform( "light_specular", glm::vec4(1.0,1.0,1.0,1.0) );
179                        m_program->set_uniform( "diffuse", 0 );
180                }
181
182                {
183                        NV_PROFILE( "draw" );
184                        glm::mat4 model      = glm::mat4(1.0f);
185                        m_mesh->draw( m_render_state, model, view, projection );
186                }
187
188                {
189                        NV_PROFILE( "swap" );
190                        m_window->swap_buffers();
191                }
192
193                {
194                        NV_PROFILE( "pollevent" );
195                        nv::io_event event;
196                        while(m_window->poll_event(event))
197                        {     
198                                switch (event.type)
199                                {
200                                case nv::EV_QUIT:
201                                        keypress = 1;
202                                        break;
203                                case nv::EV_KEY:
204                                        if (event.key.pressed)
205                                        {
206                                                switch (event.key.code)
207                                                {
208                                                case nv::KEY_ESCAPE : keypress = 1; break;
209                                                case nv::KEY_F1 : nv::profiler::pointer()->log_report(); break;
210                                                default: break;
211                                                }
212                                        }
213                                        break;
214                                default: break;
215                                }
216                        }
217                }
218                fps_counter.tick();
219        }
220        return true;
221}
222
223application::~application()
224{
225        delete m_program;
226        delete m_mesh;
227        delete m_diffuse;
228        delete m_window;
229        delete m_device;
230}
231
232
233int main(int, char* [])
234{
235        nv::logger log(nv::LOG_TRACE);
236        log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
237        log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
238       
239        NV_LOG( nv::LOG_NOTICE, "Logging started" );
240        application app;
241        if ( app.initialize() )
242        {
243                app.run();
244        }
245        NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
246
247        return 0;
248}
249
Note: See TracBrowser for help on using the repository browser.