source: trunk/tests/render_test/rl.cc @ 238

Last change on this file since 238 was 238, checked in by epyon, 11 years ago
  • mesh data interface and usage
  • new wavefront importer (old pending removal)
  • updates to test projects
File size: 8.7 KB
RevLine 
[46]1#include <nv/interface/vertex_buffer.hh>
2#include <nv/gl/gl_device.hh>
[89]3#include <nv/gfx/image.hh>
[46]4#include <nv/interface/context.hh>
5#include <nv/interface/window.hh>
6#include <nv/interface/program.hh>
7#include <nv/interface/texture2d.hh>
8#include <nv/logging.hh>
9#include <nv/logger.hh>
[121]10#include <nv/math.hh>
[46]11#include <nv/string.hh>
[71]12#include <nv/interface/mesh.hh>
[46]13
14const nv::uint16 size_x  = 16;
15const nv::uint16 size_y  = 16;
16const nv::uint16 size_xy = size_x * size_y;
17
[121]18static nv::uint8 height[size_xy] =
[46]19{
20        4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
21        4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
22        4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
23        4,  4,  4,  6,  6,  6,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,
24        4,  4,  4,  6,  6,  6,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,
25        4,  4,  4,  6,  6,  6,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,
26        4,  4,  4,  5,  5,  5,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
27        4,  4,  4,  5,  5,  5,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
28        4,  4,  4,  5,  5,  5,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
29        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
30        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
31        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
32        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  4,  4,  4,  4,  4,  4,
33        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  4,  4,  4,  4,  4,  4,
34        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  4,  4,  4,  4,  4,  4,
35        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  4,  4,  4,  4,  4,  4,
36};
37
[121]38static nv::uint8 map[size_xy] =
[46]39{
40        2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
41        2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
42        2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
43        2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
44        2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
45        2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
46        2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
47        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  6,  6,  6,  2,  2,  2,
48        3,  2,  2,  2,  2,  2,  2,  6,  6,  6,  6,  6,  6,  2,  2,  2,
49        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  6,  6,  6,  2,  2,  2,
50        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  6,  6,  6,  2,  2,  2,
51        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  6,  6,  6,  2,  2,  2,
52        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  2,  2,  2,  2,  2,  2,
53        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  2,  2,  2,  2,  2,  2,
54        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  2,  2,  2,  2,  2,  2,
55        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  2,  2,  2,  2,  2,  2,
56};
57
58class application
59{
60public:
61        application();
62        bool initialize();
63        bool run();
64        ~application();
65protected:
66        nv::device* m_device;
67        nv::window* m_window;
68        nv::texture2d* m_texture;
[238]69
70        nv::clear_state  m_clear_state;
[46]71        nv::render_state m_render_state;
[238]72        nv::scene_state  m_scene_state;
[46]73
74        nv::program* m_char_program;
75        nv::program* m_box_program;
76        nv::vertex_array* m_char_va;
77        nv::vertex_array* m_box_va;
78        unsigned int m_count;
79};
80
[238]81struct vtx
82{
83        nv::i8vec3 position;
84        nv::i8vec3 color;
85
86        vtx( const nv::i8vec3& a_position, const nv::i8vec3& a_color )
87                : position( a_position ), color( a_color ) {}
88};
89
[46]90application::application()
91{
92        m_device = new nv::gl_device();
[231]93        m_window = m_device->create_window( 800, 600, false );
[46]94       
[90]95        nv::image_data* sprites = m_device->create_image_data( "spritesheet.png" );
[49]96        nv::sampler sampler( nv::sampler::NEAREST, nv::sampler::REPEAT );
[90]97        m_texture = m_device->create_texture2d( sprites->get_size(), nv::RGBA, nv::UBYTE, sampler, (void*)sprites->get_data() );
98        delete sprites;
[46]99
100        m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
101        m_render_state.depth_test.enabled = true;
102        m_render_state.blending.enabled   = true;
103        m_render_state.blending.src_rgb_factor   = nv::blending::SRC_ALPHA;
104        m_render_state.blending.dst_rgb_factor   = nv::blending::ONE_MINUS_SRC_ALPHA;
105        m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
106        m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
107}
108
109bool application::initialize()
110{
111        { // CHARACTER
[238]112                std::vector< vtx > v;
[46]113                int m = 16;     int x = 0; int y = 0; int h = 0;
[238]114                nv::i8vec3 mt( m, 1, 0 );
115                v.emplace_back( nv::i8vec3( x,   h, y ), mt ); 
116                v.emplace_back( nv::i8vec3( x,   h, y+1 ), mt );
117                v.emplace_back( nv::i8vec3( x+1, h, y+1 ), mt );
118                v.emplace_back( nv::i8vec3( x+1, h, y+1 ), mt );
119                v.emplace_back( nv::i8vec3( x+1, h, y ), mt ); 
120                v.emplace_back( nv::i8vec3( x,   h, y ), mt ); 
[73]121                m_char_program = m_device->create_program( nv::slurp( "char.vert" ), nv::slurp( "char.frag" ) );
[238]122                m_char_va      = m_device->create_vertex_array( v, nv::STATIC_DRAW );
[46]123        }
124
125        { // WORLD
[238]126                std::vector< vtx > v;
127                nv::i8vec3 mt;
[46]128                for (int i = 0; i < size_x * size_y; ++i )
129                {
130                        int x = i % size_x;
131                        int y = i / size_x;
132
[238]133                        mt = nv::i8vec3( map[i], 2, 0 );
134                        v.emplace_back( nv::i8vec3( x,   height[i], y   ), mt );
135                        v.emplace_back( nv::i8vec3( x,   height[i], y+1 ), mt );
136                        v.emplace_back( nv::i8vec3( x+1, height[i], y+1 ), mt );
137                        v.emplace_back( nv::i8vec3( x+1, height[i], y+1 ), mt );
138                        v.emplace_back( nv::i8vec3( x+1, height[i], y ), mt );
139                        v.emplace_back( nv::i8vec3( x,   height[i], y ), mt );
[46]140
141                        if (i > 0 && height[i-1] != height[i])
142                        {
143                                short dir   = height[i-1] > height[i] ? -1 : 1;
144                                nv::uint8 m = 1;
145                                for ( int h = height[i-1]; h != height[i]; h = h + dir )
146                                {
[71]147                                        m_count += 6;
[238]148                                        mt = nv::i8vec3( m, -dir, 0 );
149                                        v.emplace_back( nv::i8vec3( x, h,     y ), mt );
150                                        v.emplace_back( nv::i8vec3( x, h,     y+1 ), mt );
151                                        v.emplace_back( nv::i8vec3( x, h+dir, y+1 ), mt );
152                                        v.emplace_back( nv::i8vec3( x, h+dir, y+1 ), mt );
153                                        v.emplace_back( nv::i8vec3( x, h+dir, y ), mt );
154                                        v.emplace_back( nv::i8vec3( x, h,     y ), mt );
[46]155                                }
156                        }
157
158                        if (i >= size_x && height[i-size_x] != height[i])
159                        {
160                                short dir   = height[i-size_x] > height[i] ? -1 : 1;
161                                nv::uint8 m = 1;
162                                for ( int h = height[i-size_x]; h != height[i]; h = h + dir )
163                                {
[238]164                                        mt = nv::i8vec3( m, -3*dir, 0 );
165                                        v.emplace_back( nv::i8vec3( x,   h,     y ), mt );
166                                        v.emplace_back( nv::i8vec3( x,   h+dir, y ), mt );
167                                        v.emplace_back( nv::i8vec3( x+1, h+dir, y ), mt );
[46]168
[238]169                                        v.emplace_back( nv::i8vec3( x+1, h+dir, y ), mt );
170                                        v.emplace_back( nv::i8vec3( x+1, h,     y ), mt );
171                                        v.emplace_back( nv::i8vec3( x,   h,     y ), mt );
[46]172                                }
173                        }
174
175                }
176
[238]177                m_count       = v.size();
[73]178                m_box_program = m_device->create_program( nv::slurp( "box.vert" ), nv::slurp( "box.frag" ) );
[238]179                m_box_va      = m_device->create_vertex_array( v, nv::STATIC_DRAW );
[46]180        }
181
182        return true;
183}
184
185bool application::run()
186{
187        int keypress = 0;
188
189        glm::vec3 move( 0, 0, 0 );
[238]190        m_scene_state.get_camera().set_perspective(25.0f, 1.0f*800.0f/600.0f, 0.1f, 100.0f);
[46]191
192        while(!keypress)
193        {
[238]194                m_scene_state.set_model( glm::translate(glm::mat4(1.0f), glm::vec3(-8.5, 0.0, -8.0)) );
195                m_scene_state.get_camera().set_lookat(glm::vec3(0.0, 20.0, 5.0) + move, glm::vec3(0.0, 4.0, 0.0) + move, glm::vec3(0.0, 1.0, 0.0));
196
[46]197                m_window->get_context()->clear( m_clear_state );
198                m_texture->bind( 0 );
199                m_box_program->set_uniform( "light", glm::vec3(8.5, 4.5, 6.5) + move );
[238]200                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_scene_state, m_box_program, m_box_va, m_count );
[46]201
202                m_char_program->set_uniform( "pos", move + glm::vec3( 8, 4.1, 6 ) );
[238]203                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_scene_state, m_char_program, m_char_va, 6 );
[93]204                m_window->swap_buffers();
[46]205
[93]206                nv::io_event event;
207                while(m_window->poll_event(event))
[46]208                {     
209                        switch (event.type)
210                        {
[93]211                        case nv::EV_QUIT:
[46]212                                keypress = 1;
213                                break;
[93]214                        case nv::EV_KEY:
215                                if (event.key.pressed)
[46]216                                {
[93]217                                        switch (event.key.code)
218                                        {
219                                        case nv::KEY_ESCAPE : keypress = 1; break;
220                                        case nv::KEY_LEFT   : move.x = move.x - 1.0f; break;
221                                        case nv::KEY_RIGHT  : move.x = move.x + 1.0f; break;
222                                        case nv::KEY_UP     : move.z = move.z - 1.0f; break;
223                                        case nv::KEY_DOWN   : move.z = move.z + 1.0f; break;
[121]224                                        default: break;
[93]225                                        }
[46]226                                }
227                                break;
[121]228                        default: break;
[46]229                        }
230                }
231        }
232        return true;
233}
234
235application::~application()
236{
237        delete m_char_program;
238        delete m_box_program;
239        delete m_char_va;
240        delete m_box_va;
241        delete m_texture;
242        delete m_window;
243        delete m_device;
244}
245
246
247int main(int, char* [])
248{
249        nv::logger log(nv::LOG_TRACE);
250        log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
251        log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
252       
253        NV_LOG( nv::LOG_NOTICE, "Logging started" );
[47]254        application app;
255        if ( app.initialize() )
[46]256        {
[47]257                app.run();
[46]258        }
259        NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
260
261        return 0;
262}
263
Note: See TracBrowser for help on using the repository browser.