source: trunk/tests/planet_test/nv_planet_test.cc @ 544

Last change on this file since 544 was 327, checked in by epyon, 11 years ago
  • minor fix to array2d
  • window_manager fix for planet_test
File size: 7.1 KB
Line 
1#include <nv/gfx/keyframed_mesh.hh>
2#include <nv/gl/gl_device.hh>
3#include <nv/sdl/sdl_window_manager.hh>
4#include <nv/gfx/image.hh>
5#include <nv/gfx/debug_draw.hh>
6#include <nv/interface/context.hh>
7#include <nv/interface/window.hh>
8#include <nv/core/profiler.hh>
9#include <nv/core/logging.hh>
10#include <nv/core/logger.hh>
11#include <nv/core/math.hh>
12#include <nv/core/time.hh>
13#include <nv/core/string.hh>
14#include <glm/gtx/rotate_vector.hpp>
15
16class application
17{
18public:
19        application();
20        bool initialize();
21        bool run();
22        ~application();
23protected:
24        nv::mesh_data* generate_box();
25        nv::mesh_data* generate_subdiv_sphere( int levels );
26        nv::window_manager* m_wm;
27        nv::device*      m_device;
28        nv::window*      m_window;
29        nv::context*     m_context;
30        nv::debug_data*  m_debug_data;
31        nv::texture      m_diffuse;
32        nv::program      m_program;
33        nv::mesh_data*   m_data;
34        nv::vertex_array m_va;
35
36        nv::clear_state   m_clear_state;
37        nv::render_state  m_render_state;
38        nv::scene_state   m_scene_state;
39};
40
41struct vertex
42{
43        nv::vec3 position;
44};
45
46nv::mesh_data* generate_subdiv_sphere( int levels )
47{
48
49
50        nv::mesh_raw_channel* vchannel = nv::mesh_raw_channel::create<vertex>( 8 );
51        nv::mesh_raw_channel* ichannel = nv::mesh_raw_channel::create_index( nv::USHORT, 6 * 6 );
52        vertex*     vtx = (vertex*)vchannel->data;
53        nv::uint16* idx = (nv::uint16*)ichannel->data;
54
55        nv::mesh_data* result = new nv::mesh_data;
56        result->add_channel( vchannel );
57        result->add_channel( ichannel );
58        return result;
59
60}
61
62nv::mesh_data* application::generate_box()
63{
64        nv::mesh_raw_channel* vchannel = nv::mesh_raw_channel::create<vertex>( 8 );
65        nv::mesh_raw_channel* ichannel = nv::mesh_raw_channel::create_index( nv::USHORT, 6 * 6 );
66        vertex*     vtx = (vertex*)vchannel->data;
67        nv::uint16* idx = (nv::uint16*)ichannel->data;
68       
69        vtx[0].position = nv::vec3( 1.0f, 1.0f, 1.0f );
70        vtx[1].position = nv::vec3( 1.0f, 1.0f,-1.0f );
71        vtx[2].position = nv::vec3(-1.0f, 1.0f,-1.0f );
72        vtx[3].position = nv::vec3(-1.0f, 1.0f, 1.0f );
73        vtx[4].position = nv::vec3( 1.0f,-1.0f, 1.0f );
74        vtx[5].position = nv::vec3( 1.0f,-1.0f,-1.0f );
75        vtx[6].position = nv::vec3(-1.0f,-1.0f,-1.0f );
76        vtx[7].position = nv::vec3(-1.0f,-1.0f, 1.0f );
77
78        auto square = [&]( nv::uint16 i, nv::uint16 a, nv::uint16 b, nv::uint16 c, nv::uint16 d )
79        {
80                idx[i+0] = a;
81                idx[i+1] = b;
82                idx[i+2] = c;
83               
84                idx[i+3] = c;
85                idx[i+4] = d;
86                idx[i+5] = a;
87        };
88
89        square( 0, 0, 1, 2, 3 );
90        square( 6, 1, 0, 4, 5 );
91        square(12, 2, 1, 5, 6 );
92        square(18, 3, 2, 6, 7 );
93        square(24, 0, 3, 7, 4 );
94        square(30, 7, 6, 5, 4 );
95
96        nv::mesh_data* result = new nv::mesh_data;
97        result->add_channel( vchannel );
98        result->add_channel( ichannel );
99        return result;
100}
101
102
103application::application()
104{
105        NV_PROFILE( "app_construct" );
106        m_wm      = new nv::sdl::window_manager;
107        m_device  = new nv::gl_device();
108        m_window  = m_wm->create_window( m_device, 1024, 800, false );
109        m_context = m_window->get_context();
110
111//      nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::REPEAT );
112//      nv::image_data* data = m_device->create_image_data( "data/manc.png" );
113//      m_diffuse  = m_device->create_texture( data, sampler );
114//      delete data;
115
116        m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
117        m_clear_state.color   = nv::vec4(0.2f,0.2f,0.2f,1.0f);
118        m_render_state.depth_test.enabled = true;
119        m_render_state.culling.enabled    = true;
120        m_render_state.blending.enabled   = false;
121        m_render_state.blending.src_rgb_factor   = nv::blending::SRC_ALPHA;
122        m_render_state.blending.dst_rgb_factor   = nv::blending::ONE_MINUS_SRC_ALPHA;
123        m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
124        m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
125
126        m_debug_data = new nv::debug_data( m_context );
127        m_debug_data->push_aabox( nv::vec3( 1.0f, 1.0f, 1.0f ), nv::vec3(-1.0f, -1.0f,-1.0f ), nv::vec3( 1.0f, 0.0f, 0.0f ) );
128        m_debug_data->update();
129}
130
131bool application::initialize()
132{
133        NV_PROFILE( "app_initialize" );
134        m_program = m_device->create_program( nv::slurp( "planet.vert" ), nv::slurp( "planet.frag" ) );
135        m_data = generate_box();
136        m_va   = m_context->create_vertex_array( m_data, nv::STATIC_DRAW );
137        return true;
138}
139
140bool application::run()
141{
142        nv::profiler::pointer()->log_report();
143        NV_PROFILE( "app_run" );
144        int keypress = 0;
145
146        nv::uint32 ticks   = m_wm->get_ticks();
147        nv::uint32 last_ticks;
148        nv::fps_counter_class< nv::system_us_timer > fps_counter;
149
150        nv::uint16 count = 0;
151        while(!keypress)
152        {
153                last_ticks = ticks;
154                ticks      = m_wm->get_ticks();
155                nv::uint32 elapsed = ticks - last_ticks;
156                m_context->clear( m_clear_state );
157                glm::vec3 eye = glm::rotate( glm::vec3( 3.0f, 0.0f, 0.0f ), (ticks / 20.f), glm::vec3( 0.0,1.0,0.0 ) );
158//              eye = glm::vec3( 3.0f, 0.0f, 0.0f );
159                m_scene_state.set_model( nv::mat4(1.0f) );
160                m_scene_state.get_camera().set_lookat(eye, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0, 1.0, 0.0));
161                float ratio = (float)m_window->get_width() / (float)m_window->get_height();
162                m_scene_state.get_camera().set_perspective(60.0f, ratio, 0.1f, 1000.0f);
163
164//              m_context->bind( m_diffuse, nv::TEX_DIFFUSE );
165                m_device->set_opt_uniform( m_program, "center", glm::vec3(0.0,0.0,0.0) );
166                m_device->set_opt_uniform( m_program, "radius", 1.0f );
167                m_device->set_opt_uniform( m_program, "light_position", glm::vec3(120.0, 120.0, 0) );
168                m_device->set_opt_uniform( m_program, "light_diffuse",  glm::vec4(0.5,0.5,0.5,1.0) );
169                m_device->set_opt_uniform( m_program, "light_specular", glm::vec4(1.0,1.0,1.0,1.0) );
170                m_render_state.culling.order    = nv::culling::CW;
171                m_context->draw( nv::TRIANGLES, m_render_state, m_scene_state, m_program, m_va, 6*6 );
172                m_render_state.culling.order    = nv::culling::CCW;
173
174                m_context->draw( nv::LINES, m_render_state, m_scene_state, m_debug_data->get_program(), m_debug_data->get_vertex_array(), m_debug_data->get_count() );
175
176                m_window->swap_buffers();
177
178                nv::io_event event;
179                while(m_window->poll_event(event))
180                {     
181                        switch (event.type)
182                        {
183                        case nv::EV_QUIT:
184                                keypress = 1;
185                                break;
186                        case nv::EV_KEY:
187                                if (event.key.pressed)
188                                {
189                                        switch (event.key.code)
190                                        {
191                                        case nv::KEY_ESCAPE : keypress = 1; break;
192                                        case nv::KEY_F1 : nv::profiler::pointer()->log_report(); break;
193                                        default: break;
194                                        }
195                                }
196                                break;
197                        default: break;
198                        }
199                }
200
201                fps_counter.tick();
202                if ( count % 120 == 0 )
203                {
204                        m_window->set_title( "Nova Engine - " + nv::to_string( (nv::uint32)fps_counter.fps() ) );
205                }
206                count++;
207        }
208        return true;
209}
210
211application::~application()
212{
213        m_context->release( m_va );
214        m_device->release( m_program );
215        m_device->release( m_diffuse );
216
217        delete m_window;
218        delete m_device;
219}
220
221
222int main(int, char* [])
223{
224        nv::logger log(nv::LOG_TRACE);
225        log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
226        log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
227       
228        NV_LOG( nv::LOG_NOTICE, "Logging started" );
229        application app;
230        if ( app.initialize() )
231        {
232                app.run();
233        }
234        NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
235
236        return 0;
237}
238
Note: See TracBrowser for help on using the repository browser.