source: trunk/tests/render_test/nv_render_test.cc @ 544

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