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

Last change on this file since 231 was 231, checked in by epyon, 11 years ago
  • further simplification of mesh classes
  • updated tests
File size: 8.9 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;
69        nv::clear_state m_clear_state;
70        nv::render_state m_render_state;
71
72        nv::program* m_char_program;
73        nv::program* m_box_program;
74        nv::vertex_array* m_char_va;
75        nv::vertex_array* m_box_va;
76        unsigned int m_count;
77};
78
79application::application()
80{
81        m_device = new nv::gl_device();
[231]82        m_window = m_device->create_window( 800, 600, false );
[46]83       
[90]84        nv::image_data* sprites = m_device->create_image_data( "spritesheet.png" );
[49]85        nv::sampler sampler( nv::sampler::NEAREST, nv::sampler::REPEAT );
[90]86        m_texture = m_device->create_texture2d( sprites->get_size(), nv::RGBA, nv::UBYTE, sampler, (void*)sprites->get_data() );
87        delete sprites;
[46]88
89        m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
90        m_render_state.depth_test.enabled = true;
91        m_render_state.blending.enabled   = true;
92        m_render_state.blending.src_rgb_factor   = nv::blending::SRC_ALPHA;
93        m_render_state.blending.dst_rgb_factor   = nv::blending::ONE_MINUS_SRC_ALPHA;
94        m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
95        m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
96}
97
98bool application::initialize()
99{
100        { // CHARACTER
[71]101                nv::mesh cmesh;
102                nv::vertex_attribute<nv::i8vec3>::list& vtx = cmesh.add_attribute<nv::i8vec3>("coords")->get();
103                nv::vertex_attribute<nv::i8vec3>::list& mat = cmesh.add_attribute<nv::i8vec3>("material")->get();
[46]104                int m = 16;     int x = 0; int y = 0; int h = 0;
[71]105                vtx.emplace_back( x,   h, y ); 
106                vtx.emplace_back( x,   h, y+1 );
107                vtx.emplace_back( x+1, h, y+1 );
108                vtx.emplace_back( x+1, h, y+1 );
109                vtx.emplace_back( x+1, h, y ); 
110                vtx.emplace_back( x,   h, y ); 
111                mat.insert( mat.end(), 6, nv::i8vec3( m, 1, 0 ) );
[73]112                m_char_program = m_device->create_program( nv::slurp( "char.vert" ), nv::slurp( "char.frag" ) );
113                m_char_va      = m_device->create_vertex_array( &cmesh, &(m_char_program->get_attributes()), nv::STATIC_DRAW );
[46]114        }
115
116        { // WORLD
[71]117                nv::mesh wmesh;
118                nv::vertex_attribute<nv::i8vec3>::list& vtx = wmesh.add_attribute<nv::i8vec3>("coords")->get();
119                nv::vertex_attribute<nv::i8vec3>::list& mat = wmesh.add_attribute<nv::i8vec3>("material")->get();
120
[46]121                for (int i = 0; i < size_x * size_y; ++i )
122                {
123                        int x = i % size_x;
124                        int y = i / size_x;
125
[71]126                        vtx.emplace_back( x,   height[i], y   );
127                        vtx.emplace_back( x,   height[i], y+1 );
128                        vtx.emplace_back( x+1, height[i], y+1 );
129                        vtx.emplace_back( x+1, height[i], y+1 );
130                        vtx.emplace_back( x+1, height[i], y );   
131                        vtx.emplace_back( x,   height[i], y );   
132                        mat.insert( mat.end(), 6, nv::i8vec3( map[i], 2, 0 ) );
[46]133
134                        if (i > 0 && height[i-1] != height[i])
135                        {
136                                short dir   = height[i-1] > height[i] ? -1 : 1;
137                                nv::uint8 m = 1;
138                                for ( int h = height[i-1]; h != height[i]; h = h + dir )
139                                {
[71]140                                        m_count += 6;
141                                        vtx.emplace_back( x, h,     y );   
142                                        vtx.emplace_back( x, h,     y+1 );
143                                        vtx.emplace_back( x, h+dir, y+1 );
144                                        vtx.emplace_back( x, h+dir, y+1 );
145                                        vtx.emplace_back( x, h+dir, y );   
146                                        vtx.emplace_back( x, h,     y );   
147                                        mat.insert( mat.end(), 6, nv::i8vec3( m, -dir, 0 ) );
[46]148                                }
149                        }
150
151                        if (i >= size_x && height[i-size_x] != height[i])
152                        {
153                                short dir   = height[i-size_x] > height[i] ? -1 : 1;
154                                nv::uint8 m = 1;
155                                for ( int h = height[i-size_x]; h != height[i]; h = h + dir )
156                                {
[71]157                                        vtx.emplace_back( x,   h,     y );
158                                        vtx.emplace_back( x,   h+dir, y );
159                                        vtx.emplace_back( x+1, h+dir, y );
[46]160
[71]161                                        vtx.emplace_back( x+1, h+dir, y );
162                                        vtx.emplace_back( x+1, h,     y );
163                                        vtx.emplace_back( x,   h,     y );
164                                        mat.insert( mat.end(), 6, nv::i8vec3( m, -3*dir, 0 ) );
[46]165                                }
166                        }
167
168                }
169
[73]170                m_count       = vtx.size();
171                m_box_program = m_device->create_program( nv::slurp( "box.vert" ), nv::slurp( "box.frag" ) );
172                m_box_va      = m_device->create_vertex_array( &wmesh, &(m_box_program->get_attributes()), nv::STATIC_DRAW );
[46]173        }
174
175        return true;
176}
177
178bool application::run()
179{
180        int keypress = 0;
181
182        glm::vec3 move( 0, 0, 0 );
183
184        while(!keypress)
185        {
186                m_window->get_context()->clear( m_clear_state );
187
188                glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(-8.5, 0.0, -8.0));
189                glm::mat4 view  = glm::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));
190                glm::mat4 projection = glm::perspective(25.0f, 1.0f*800.0f/600.0f, 0.1f, 100.0f);
191                glm::mat4 mv         = view * model;
192
193                m_texture->bind( 0 );
194                m_box_program->set_uniform( "matrix_mvp", projection * mv );
195                m_box_program->set_uniform( "light", glm::vec3(8.5, 4.5, 6.5) + move );
196                m_box_program->set_uniform( "tex", 0 );
197                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_box_program, m_box_va, m_count );
198
199                m_char_program->set_uniform( "matrix_mvp", projection * mv );
200                m_char_program->set_uniform( "pos", move + glm::vec3( 8, 4.1, 6 ) );
201                m_char_program->set_uniform( "tex", 0 );
202                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_char_program, m_char_va, 6 );
[93]203                m_window->swap_buffers();
[46]204
[93]205                nv::io_event event;
206                while(m_window->poll_event(event))
[46]207                {     
208                        switch (event.type)
209                        {
[93]210                        case nv::EV_QUIT:
[46]211                                keypress = 1;
212                                break;
[93]213                        case nv::EV_KEY:
214                                if (event.key.pressed)
[46]215                                {
[93]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;
[121]223                                        default: break;
[93]224                                        }
[46]225                                }
226                                break;
[121]227                        default: break;
[46]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" );
[47]253        application app;
254        if ( app.initialize() )
[46]255        {
[47]256                app.run();
[46]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.