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

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