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

Last change on this file since 137 was 121, checked in by epyon, 12 years ago
  • Nova builds with -Weverything/-Wall/-pedantic/etc on: on MSVC 2012 on GCC 4.6.3 on clang 3.2
  • ... without a single fucking warning.
File size: 8.9 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#include <nv/types.hh>
13#include <nv/interface/mesh.hh>
14
15const nv::uint16 size_x  = 16;
16const nv::uint16 size_y  = 16;
17const nv::uint16 size_xy = size_x * size_y;
18
19static nv::uint8 height[size_xy] =
20{
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,  4,  4,  4,  4,  4,  4,  4,  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,  6,  6,  6,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,
27        4,  4,  4,  5,  5,  5,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
28        4,  4,  4,  5,  5,  5,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
29        4,  4,  4,  5,  5,  5,  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,  2,  2,  2,  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        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  4,  4,  4,  4,  4,  4,
37};
38
39static nv::uint8 map[size_xy] =
40{
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,  2,  2,  2,  2,  2,  2,  2,  2,  2,
48        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  6,  6,  6,  2,  2,  2,
49        3,  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,  6,  6,  6,  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        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  2,  2,  2,  2,  2,  2,
57};
58
59class application
60{
61public:
62        application();
63        bool initialize();
64        bool run();
65        ~application();
66protected:
67        nv::device* m_device;
68        nv::window* m_window;
69        nv::texture2d* m_texture;
70        nv::clear_state m_clear_state;
71        nv::render_state m_render_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
80application::application()
81{
82        m_device = new nv::gl_device();
83        m_window = m_device->create_window( 800, 600 );
84       
85        nv::image_data* sprites = m_device->create_image_data( "spritesheet.png" );
86        nv::sampler sampler( nv::sampler::NEAREST, nv::sampler::REPEAT );
87        m_texture = m_device->create_texture2d( sprites->get_size(), nv::RGBA, nv::UBYTE, sampler, (void*)sprites->get_data() );
88        delete sprites;
89
90        m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
91        m_render_state.depth_test.enabled = true;
92        m_render_state.blending.enabled   = true;
93        m_render_state.blending.src_rgb_factor   = nv::blending::SRC_ALPHA;
94        m_render_state.blending.dst_rgb_factor   = nv::blending::ONE_MINUS_SRC_ALPHA;
95        m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
96        m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
97}
98
99bool application::initialize()
100{
101        { // CHARACTER
102                nv::mesh cmesh;
103                nv::vertex_attribute<nv::i8vec3>::list& vtx = cmesh.add_attribute<nv::i8vec3>("coords")->get();
104                nv::vertex_attribute<nv::i8vec3>::list& mat = cmesh.add_attribute<nv::i8vec3>("material")->get();
105                int m = 16;     int x = 0; int y = 0; int h = 0;
106                vtx.emplace_back( x,   h, y ); 
107                vtx.emplace_back( x,   h, y+1 );
108                vtx.emplace_back( x+1, h, y+1 );
109                vtx.emplace_back( x+1, h, y+1 );
110                vtx.emplace_back( x+1, h, y ); 
111                vtx.emplace_back( x,   h, y ); 
112                mat.insert( mat.end(), 6, nv::i8vec3( m, 1, 0 ) );
113                m_char_program = m_device->create_program( nv::slurp( "char.vert" ), nv::slurp( "char.frag" ) );
114                m_char_va      = m_device->create_vertex_array( &cmesh, &(m_char_program->get_attributes()), nv::STATIC_DRAW );
115        }
116
117        { // WORLD
118                nv::mesh wmesh;
119                nv::vertex_attribute<nv::i8vec3>::list& vtx = wmesh.add_attribute<nv::i8vec3>("coords")->get();
120                nv::vertex_attribute<nv::i8vec3>::list& mat = wmesh.add_attribute<nv::i8vec3>("material")->get();
121
122                for (int i = 0; i < size_x * size_y; ++i )
123                {
124                        int x = i % size_x;
125                        int y = i / size_x;
126
127                        vtx.emplace_back( x,   height[i], y   );
128                        vtx.emplace_back( x,   height[i], y+1 );
129                        vtx.emplace_back( x+1, height[i], y+1 );
130                        vtx.emplace_back( x+1, height[i], y+1 );
131                        vtx.emplace_back( x+1, height[i], y );   
132                        vtx.emplace_back( x,   height[i], y );   
133                        mat.insert( mat.end(), 6, nv::i8vec3( map[i], 2, 0 ) );
134
135                        if (i > 0 && height[i-1] != height[i])
136                        {
137                                short dir   = height[i-1] > height[i] ? -1 : 1;
138                                nv::uint8 m = 1;
139                                for ( int h = height[i-1]; h != height[i]; h = h + dir )
140                                {
141                                        m_count += 6;
142                                        vtx.emplace_back( x, h,     y );   
143                                        vtx.emplace_back( x, h,     y+1 );
144                                        vtx.emplace_back( x, h+dir, y+1 );
145                                        vtx.emplace_back( x, h+dir, y+1 );
146                                        vtx.emplace_back( x, h+dir, y );   
147                                        vtx.emplace_back( x, h,     y );   
148                                        mat.insert( mat.end(), 6, nv::i8vec3( m, -dir, 0 ) );
149                                }
150                        }
151
152                        if (i >= size_x && height[i-size_x] != height[i])
153                        {
154                                short dir   = height[i-size_x] > height[i] ? -1 : 1;
155                                nv::uint8 m = 1;
156                                for ( int h = height[i-size_x]; h != height[i]; h = h + dir )
157                                {
158                                        vtx.emplace_back( x,   h,     y );
159                                        vtx.emplace_back( x,   h+dir, y );
160                                        vtx.emplace_back( x+1, h+dir, y );
161
162                                        vtx.emplace_back( x+1, h+dir, y );
163                                        vtx.emplace_back( x+1, h,     y );
164                                        vtx.emplace_back( x,   h,     y );
165                                        mat.insert( mat.end(), 6, nv::i8vec3( m, -3*dir, 0 ) );
166                                }
167                        }
168
169                }
170
171                m_count       = vtx.size();
172                m_box_program = m_device->create_program( nv::slurp( "box.vert" ), nv::slurp( "box.frag" ) );
173                m_box_va      = m_device->create_vertex_array( &wmesh, &(m_box_program->get_attributes()), nv::STATIC_DRAW );
174        }
175
176        return true;
177}
178
179bool application::run()
180{
181        int keypress = 0;
182
183        glm::vec3 move( 0, 0, 0 );
184
185        while(!keypress)
186        {
187                m_window->get_context()->clear( m_clear_state );
188
189                glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(-8.5, 0.0, -8.0));
190                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));
191                glm::mat4 projection = glm::perspective(25.0f, 1.0f*800.0f/600.0f, 0.1f, 100.0f);
192                glm::mat4 mv         = view * model;
193
194                m_texture->bind( 0 );
195                m_box_program->set_uniform( "matrix_mvp", projection * mv );
196                m_box_program->set_uniform( "light", glm::vec3(8.5, 4.5, 6.5) + move );
197                m_box_program->set_uniform( "tex", 0 );
198                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_box_program, m_box_va, m_count );
199
200                m_char_program->set_uniform( "matrix_mvp", projection * mv );
201                m_char_program->set_uniform( "pos", move + glm::vec3( 8, 4.1, 6 ) );
202                m_char_program->set_uniform( "tex", 0 );
203                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_char_program, m_char_va, 6 );
204                m_window->swap_buffers();
205
206                nv::io_event event;
207                while(m_window->poll_event(event))
208                {     
209                        switch (event.type)
210                        {
211                        case nv::EV_QUIT:
212                                keypress = 1;
213                                break;
214                        case nv::EV_KEY:
215                                if (event.key.pressed)
216                                {
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;
224                                        default: break;
225                                        }
226                                }
227                                break;
228                        default: 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" );
254        application app;
255        if ( app.initialize() )
256        {
257                app.run();
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.