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

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