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

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