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

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