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

Last change on this file since 93 was 93, checked in by epyon, 12 years ago
  • io_event - event and key list expanded
  • window - is_event_pending, poll_event, swap_buffers added to interface
  • gl_window - implementations of the above, SDL_Event translation
  • render_test - no longer directly using any foreign libs
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>
10#include <glm/glm.hpp>
11#include <glm/gtc/matrix_transform.hpp>
12#include <glm/gtc/type_ptr.hpp>
13#include <nv/string.hh>
14#include <nv/types.hh>
[71]15#include <nv/interface/mesh.hh>
[46]16
17const nv::uint16 size_x  = 16;
18const nv::uint16 size_y  = 16;
19const nv::uint16 size_xy = size_x * size_y;
20
21nv::uint8 height[size_xy] =
22{
23        4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
24        4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
25        4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
26        4,  4,  4,  6,  6,  6,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,
27        4,  4,  4,  6,  6,  6,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,
28        4,  4,  4,  6,  6,  6,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,
29        4,  4,  4,  5,  5,  5,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
30        4,  4,  4,  5,  5,  5,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
31        4,  4,  4,  5,  5,  5,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
32        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
33        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
34        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
35        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  4,  4,  4,  4,  4,  4,
36        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  4,  4,  4,  4,  4,  4,
37        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  4,  4,  4,  4,  4,  4,
38        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  4,  4,  4,  4,  4,  4,
39};
40
41nv::uint8 map[size_xy] =
42{
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,  2,  2,  2,  2,  2,  2,  2,  2,  2,
48        2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
49        2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
50        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  6,  6,  6,  2,  2,  2,
51        3,  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,  6,  6,  6,  2,  2,  2,
53        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  6,  6,  6,  2,  2,  2,
54        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  6,  6,  6,  2,  2,  2,
55        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  2,  2,  2,  2,  2,  2,
56        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  2,  2,  2,  2,  2,  2,
57        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  2,  2,  2,  2,  2,  2,
58        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  2,  2,  2,  2,  2,  2,
59};
60
61class application
62{
63public:
64        application();
65        bool initialize();
66        bool run();
67        ~application();
68protected:
69        nv::device* m_device;
70        nv::window* m_window;
71        nv::texture2d* m_texture;
72        nv::clear_state m_clear_state;
73        nv::render_state m_render_state;
74
75        nv::program* m_char_program;
76        nv::program* m_box_program;
77        nv::vertex_array* m_char_va;
78        nv::vertex_array* m_box_va;
79        unsigned int m_count;
80};
81
82application::application()
83{
84        m_device = new nv::gl_device();
85        m_window = m_device->create_window( 800, 600 );
86       
[90]87        nv::image_data* sprites = m_device->create_image_data( "spritesheet.png" );
[49]88        nv::sampler sampler( nv::sampler::NEAREST, nv::sampler::REPEAT );
[90]89        m_texture = m_device->create_texture2d( sprites->get_size(), nv::RGBA, nv::UBYTE, sampler, (void*)sprites->get_data() );
90        delete sprites;
[46]91
92        m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
93        m_render_state.depth_test.enabled = true;
94        m_render_state.blending.enabled   = true;
95        m_render_state.blending.src_rgb_factor   = nv::blending::SRC_ALPHA;
96        m_render_state.blending.dst_rgb_factor   = nv::blending::ONE_MINUS_SRC_ALPHA;
97        m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
98        m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
99}
100
101bool application::initialize()
102{
103        { // CHARACTER
[71]104                nv::mesh cmesh;
105                nv::vertex_attribute<nv::i8vec3>::list& vtx = cmesh.add_attribute<nv::i8vec3>("coords")->get();
106                nv::vertex_attribute<nv::i8vec3>::list& mat = cmesh.add_attribute<nv::i8vec3>("material")->get();
[46]107                int m = 16;     int x = 0; int y = 0; int h = 0;
[71]108                vtx.emplace_back( x,   h, y ); 
109                vtx.emplace_back( x,   h, y+1 );
110                vtx.emplace_back( x+1, h, y+1 );
111                vtx.emplace_back( x+1, h, y+1 );
112                vtx.emplace_back( x+1, h, y ); 
113                vtx.emplace_back( x,   h, y ); 
114                mat.insert( mat.end(), 6, nv::i8vec3( m, 1, 0 ) );
[73]115                m_char_program = m_device->create_program( nv::slurp( "char.vert" ), nv::slurp( "char.frag" ) );
116                m_char_va      = m_device->create_vertex_array( &cmesh, &(m_char_program->get_attributes()), nv::STATIC_DRAW );
[46]117        }
118
119        { // WORLD
[71]120                nv::mesh wmesh;
121                nv::vertex_attribute<nv::i8vec3>::list& vtx = wmesh.add_attribute<nv::i8vec3>("coords")->get();
122                nv::vertex_attribute<nv::i8vec3>::list& mat = wmesh.add_attribute<nv::i8vec3>("material")->get();
123
[46]124                for (int i = 0; i < size_x * size_y; ++i )
125                {
126                        int x = i % size_x;
127                        int y = i / size_x;
128
[71]129                        vtx.emplace_back( x,   height[i], y   );
130                        vtx.emplace_back( x,   height[i], y+1 );
131                        vtx.emplace_back( x+1, height[i], y+1 );
132                        vtx.emplace_back( x+1, height[i], y+1 );
133                        vtx.emplace_back( x+1, height[i], y );   
134                        vtx.emplace_back( x,   height[i], y );   
135                        mat.insert( mat.end(), 6, nv::i8vec3( map[i], 2, 0 ) );
[46]136
137                        if (i > 0 && height[i-1] != height[i])
138                        {
139                                short dir   = height[i-1] > height[i] ? -1 : 1;
140                                nv::uint8 m = 1;
141                                for ( int h = height[i-1]; h != height[i]; h = h + dir )
142                                {
[71]143                                        m_count += 6;
144                                        vtx.emplace_back( x, h,     y );   
145                                        vtx.emplace_back( x, h,     y+1 );
146                                        vtx.emplace_back( x, h+dir, y+1 );
147                                        vtx.emplace_back( x, h+dir, y+1 );
148                                        vtx.emplace_back( x, h+dir, y );   
149                                        vtx.emplace_back( x, h,     y );   
150                                        mat.insert( mat.end(), 6, nv::i8vec3( m, -dir, 0 ) );
[46]151                                }
152                        }
153
154                        if (i >= size_x && height[i-size_x] != height[i])
155                        {
156                                short dir   = height[i-size_x] > height[i] ? -1 : 1;
157                                nv::uint8 m = 1;
158                                for ( int h = height[i-size_x]; h != height[i]; h = h + dir )
159                                {
[71]160                                        vtx.emplace_back( x,   h,     y );
161                                        vtx.emplace_back( x,   h+dir, y );
162                                        vtx.emplace_back( x+1, h+dir, y );
[46]163
[71]164                                        vtx.emplace_back( x+1, h+dir, y );
165                                        vtx.emplace_back( x+1, h,     y );
166                                        vtx.emplace_back( x,   h,     y );
167                                        mat.insert( mat.end(), 6, nv::i8vec3( m, -3*dir, 0 ) );
[46]168                                }
169                        }
170
171                }
172
[73]173                m_count       = vtx.size();
174                m_box_program = m_device->create_program( nv::slurp( "box.vert" ), nv::slurp( "box.frag" ) );
175                m_box_va      = m_device->create_vertex_array( &wmesh, &(m_box_program->get_attributes()), nv::STATIC_DRAW );
[46]176        }
177
178        return true;
179}
180
181bool application::run()
182{
183        int keypress = 0;
184
185        glm::vec3 move( 0, 0, 0 );
186
187        while(!keypress)
188        {
189                m_window->get_context()->clear( m_clear_state );
190
191                glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(-8.5, 0.0, -8.0));
192                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));
193                glm::mat4 projection = glm::perspective(25.0f, 1.0f*800.0f/600.0f, 0.1f, 100.0f);
194                glm::mat4 mv         = view * model;
195
196                m_texture->bind( 0 );
197                m_box_program->set_uniform( "matrix_mvp", projection * mv );
198                m_box_program->set_uniform( "light", glm::vec3(8.5, 4.5, 6.5) + move );
199                m_box_program->set_uniform( "tex", 0 );
200                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_box_program, m_box_va, m_count );
201
202                m_char_program->set_uniform( "matrix_mvp", projection * mv );
203                m_char_program->set_uniform( "pos", move + glm::vec3( 8, 4.1, 6 ) );
204                m_char_program->set_uniform( "tex", 0 );
205                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_char_program, m_char_va, 6 );
[93]206                m_window->swap_buffers();
[46]207
[93]208                nv::io_event event;
209                while(m_window->poll_event(event))
[46]210                {     
211                        switch (event.type)
212                        {
[93]213                        case nv::EV_QUIT:
[46]214                                keypress = 1;
215                                break;
[93]216                        case nv::EV_KEY:
217                                if (event.key.pressed)
[46]218                                {
[93]219                                        switch (event.key.code)
220                                        {
221                                        case nv::KEY_ESCAPE : keypress = 1; break;
222                                        case nv::KEY_LEFT   : move.x = move.x - 1.0f; break;
223                                        case nv::KEY_RIGHT  : move.x = move.x + 1.0f; break;
224                                        case nv::KEY_UP     : move.z = move.z - 1.0f; break;
225                                        case nv::KEY_DOWN   : move.z = move.z + 1.0f; break;
226                                        }
[46]227                                }
228                                break;
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.