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

Last change on this file since 52 was 50, checked in by epyon, 12 years ago
  • error checking for attribute and uniform queries
File size: 10.2 KB
Line 
1#include <nv/lib/sdl12.hh>
2#include <nv/lib/sdl_image.hh>
3#include <nv/interface/vertex_buffer.hh>
4#include <nv/gl/gl_device.hh>
5#include <nv/gl/image.hh>
6#include <nv/interface/context.hh>
7#include <nv/interface/window.hh>
8#include <nv/interface/program.hh>
9#include <nv/interface/texture2d.hh>
10#include <nv/logging.hh>
11#include <nv/logger.hh>
12#include <glm/glm.hpp>
13#include <glm/gtc/matrix_transform.hpp>
14#include <glm/gtc/type_ptr.hpp>
15#include <nv/string.hh>
16#include <nv/types.hh>
17
18typedef glm::detail::tvec3<char> byte3;
19
20const nv::uint16 size_x  = 16;
21const nv::uint16 size_y  = 16;
22const nv::uint16 size_xy = size_x * size_y;
23
24nv::uint8 height[size_xy] =
25{
26        4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
27        4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
28        4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
29        4,  4,  4,  6,  6,  6,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,
30        4,  4,  4,  6,  6,  6,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,
31        4,  4,  4,  6,  6,  6,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,
32        4,  4,  4,  5,  5,  5,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
33        4,  4,  4,  5,  5,  5,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
34        4,  4,  4,  5,  5,  5,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
35        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
36        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
37        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
38        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  4,  4,  4,  4,  4,  4,
39        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  4,  4,  4,  4,  4,  4,
40        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  4,  4,  4,  4,  4,  4,
41        4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  4,  4,  4,  4,  4,  4,
42};
43
44nv::uint8 map[size_xy] =
45{
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,  2,  2,  2,  2,  2,  2,  2,  2,  2,
51        2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
52        2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
53        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  6,  6,  6,  2,  2,  2,
54        3,  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,  6,  6,  6,  2,  2,  2,
56        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  6,  6,  6,  2,  2,  2,
57        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  6,  6,  6,  2,  2,  2,
58        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  2,  2,  2,  2,  2,  2,
59        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  2,  2,  2,  2,  2,  2,
60        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  2,  2,  2,  2,  2,  2,
61        2,  2,  2,  2,  2,  2,  2,  6,  6,  6,  2,  2,  2,  2,  2,  2,
62};
63
64struct world;
65struct being;
66
67class application
68{
69public:
70        application();
71        bool initialize();
72        bool init_program( const std::string& name, nv::program*& p, nv::vertex_array*& va, int count, void* vertex, void* material );
73        bool run();
74        ~application();
75protected:
76        nv::device* m_device;
77        nv::window* m_window;
78        nv::texture2d* m_texture;
79        nv::clear_state m_clear_state;
80        nv::render_state m_render_state;
81
82        nv::program* m_char_program;
83        nv::program* m_box_program;
84        nv::vertex_array* m_char_va;
85        nv::vertex_array* m_box_va;
86        unsigned int m_count;
87};
88
89application::application()
90{
91        m_device = new nv::gl_device();
92        m_window = m_device->create_window( 800, 600 );
93       
94        nv::load_sdl_image_library();
95        SDL_Surface* texture = IMG_Load( "spritesheet.png" );
96        nv::image sprites( glm::ivec2( texture->w, texture->h ), 4, (nv::uint8*)texture->pixels );
97        nv::sampler sampler( nv::sampler::NEAREST, nv::sampler::REPEAT );
98        m_texture = m_device->create_texture2d( sprites.get_size(), nv::RGBA, nv::UBYTE, sampler, (void*)sprites.get_data() );
99
100        m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
101        m_render_state.depth_test.enabled = true;
102        m_render_state.blending.enabled   = true;
103        m_render_state.blending.src_rgb_factor   = nv::blending::SRC_ALPHA;
104        m_render_state.blending.dst_rgb_factor   = nv::blending::ONE_MINUS_SRC_ALPHA;
105        m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
106        m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
107}
108
109bool application::initialize()
110{
111        { // CHARACTER
112                byte3 vertex[6];
113                byte3 material[6];
114                int m = 16;     int x = 0; int y = 0; int h = 0;
115                vertex[0] = byte3( x,   h, y );   material[0] = byte3( m, 1, 0 );
116                vertex[1] = byte3( x,   h, y+1 ); material[1] = byte3( m, 1, 0 );
117                vertex[2] = byte3( x+1, h, y+1 ); material[2] = byte3( m, 1, 0 );
118                vertex[3] = byte3( x+1, h, y+1 ); material[3] = byte3( m, 1, 0 );
119                vertex[4] = byte3( x+1, h, y );   material[4] = byte3( m, 1, 0 );
120                vertex[5] = byte3( x,   h, y );   material[5] = byte3( m, 1, 0 );
121
122                if (!init_program( "char", m_char_program, m_char_va, 6, vertex, material ) ) return false;
123        }
124
125        { // WORLD
126                byte3 vertex[size_x * size_y * 256];
127                byte3 material[size_x * size_y * 256];
128                m_count = 0;
129                int mcount = 0;
130                for (int i = 0; i < size_x * size_y; ++i )
131                {
132                        int x = i % size_x;
133                        int y = i / size_x;
134
135                        vertex[m_count++] = byte3( x,   height[i], y   ); material[mcount++] = byte3( map[i], 2, 0 );
136                        vertex[m_count++] = byte3( x,   height[i], y+1 ); material[mcount++] = byte3( map[i], 2, 0 );
137                        vertex[m_count++] = byte3( x+1, height[i], y+1 ); material[mcount++] = byte3( map[i], 2, 0 );
138
139                        vertex[m_count++] = byte3( x+1, height[i], y+1 ); material[mcount++] = byte3( map[i], 2, 0 );
140                        vertex[m_count++] = byte3( x+1, height[i], y );   material[mcount++] = byte3( map[i], 2, 0 );
141                        vertex[m_count++] = byte3( x,   height[i], y );   material[mcount++] = byte3( map[i], 2, 0 );
142
143                        if (i > 0 && height[i-1] != height[i])
144                        {
145                                short dir   = height[i-1] > height[i] ? -1 : 1;
146                                nv::uint8 m = 1;
147                                for ( int h = height[i-1]; h != height[i]; h = h + dir )
148                                {
149                                        vertex[m_count++] = byte3( x, h,     y );   material[mcount++] = byte3( m, -dir, 0 );
150                                        vertex[m_count++] = byte3( x, h,     y+1 ); material[mcount++] = byte3( m, -dir, 0 );
151                                        vertex[m_count++] = byte3( x, h+dir, y+1 ); material[mcount++] = byte3( m, -dir, 0 );
152
153                                        vertex[m_count++] = byte3( x, h+dir, y+1 ); material[mcount++] = byte3( m, -dir, 0 );
154                                        vertex[m_count++] = byte3( x, h+dir, y );   material[mcount++] = byte3( m, -dir, 0 );
155                                        vertex[m_count++] = byte3( x, h,     y );   material[mcount++] = byte3( m, -dir, 0 );
156                                }
157                        }
158
159                        if (i >= size_x && height[i-size_x] != height[i])
160                        {
161                                short dir   = height[i-size_x] > height[i] ? -1 : 1;
162                                nv::uint8 m = 1;
163                                for ( int h = height[i-size_x]; h != height[i]; h = h + dir )
164                                {
165                                        vertex[m_count++] = byte3( x,   h,     y ); material[mcount++] = byte3( m, -3*dir, 0 );
166                                        vertex[m_count++] = byte3( x,   h+dir, y ); material[mcount++] = byte3( m, -3*dir, 0 );
167                                        vertex[m_count++] = byte3( x+1, h+dir, y ); material[mcount++] = byte3( m, -3*dir, 0 );
168
169                                        vertex[m_count++] = byte3( x+1, h+dir, y ); material[mcount++] = byte3( m, -3*dir, 0 );
170                                        vertex[m_count++] = byte3( x+1, h,     y ); material[mcount++] = byte3( m, -3*dir, 0 );
171                                        vertex[m_count++] = byte3( x,   h,     y ); material[mcount++] = byte3( m, -3*dir, 0 );
172                                }
173                        }
174
175                }
176
177                if (!init_program( "box",  m_box_program, m_box_va, m_count, vertex, material ) ) return false;
178        }
179
180        return true;
181}
182
183bool application::init_program( const std::string& name, nv::program*& p, nv::vertex_array*& va, int count, void* vertex, void* material )
184{
185        p  = m_device->create_program( nv::slurp( name+".vert" ), nv::slurp( name+".frag" ) );
186        va = m_device->create_vertex_array();
187
188        nv::vertex_buffer* vbcoords   = m_device->create_vertex_buffer( nv::STATIC_DRAW, count*sizeof(byte3), vertex );
189        va->add_vertex_buffer( p->get_attribute( "coords" )->get_location(), vbcoords, nv::BYTE, 3 );
190        nv::vertex_buffer* vbmaterial = m_device->create_vertex_buffer( nv::STATIC_DRAW, count*sizeof(byte3), material );
191        va->add_vertex_buffer( p->get_attribute( "material" )->get_location(), vbmaterial, nv::BYTE, 3 );
192
193        return true;
194}
195
196bool application::run()
197{
198        int keypress = 0;
199
200        glm::vec3 move( 0, 0, 0 );
201
202        while(!keypress)
203        {
204                m_window->get_context()->clear( m_clear_state );
205
206                glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(-8.5, 0.0, -8.0));
207                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));
208                glm::mat4 projection = glm::perspective(25.0f, 1.0f*800.0f/600.0f, 0.1f, 100.0f);
209                glm::mat4 mv         = view * model;
210
211                m_texture->bind( 0 );
212                m_box_program->set_uniform( "matrix_mvp", projection * mv );
213                m_box_program->set_uniform( "light", glm::vec3(8.5, 4.5, 6.5) + move );
214                m_box_program->set_uniform( "tex", 0 );
215                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_box_program, m_box_va, m_count );
216
217                m_char_program->set_uniform( "matrix_mvp", projection * mv );
218                m_char_program->set_uniform( "pos", move + glm::vec3( 8, 4.1, 6 ) );
219                m_char_program->set_uniform( "tex", 0 );
220                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_char_program, m_char_va, 6 );
221
222                SDL_GL_SwapBuffers();
223                SDL_Event event;
224                while(SDL_PollEvent(&event))
225                {     
226                        switch (event.type)
227                        {
228                        case SDL_QUIT:
229                                keypress = 1;
230                                break;
231                        case SDL_KEYDOWN:
232                                switch (event.key.keysym.sym)
233                                {
234                                case SDLK_ESCAPE: keypress = 1; break;
235                                case SDLK_LEFT: move.x = move.x - 1.0f; break;
236                                case SDLK_RIGHT: move.x = move.x + 1.0f; break;
237                                case SDLK_UP: move.z = move.z - 1.0f; break;
238                                case SDLK_DOWN: move.z = move.z + 1.0f; break;
239                                }
240                                break;
241                        }
242                }
243        }
244        return true;
245}
246
247application::~application()
248{
249        delete m_char_program;
250        delete m_box_program;
251        delete m_char_va;
252        delete m_box_va;
253        delete m_texture;
254        delete m_window;
255        delete m_device;
256}
257
258
259int main(int, char* [])
260{
261        nv::logger log(nv::LOG_TRACE);
262        log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
263        log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
264       
265        NV_LOG( nv::LOG_NOTICE, "Logging started" );
266        application app;
267        if ( app.initialize() )
268        {
269                app.run();
270        }
271        NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
272
273        return 0;
274}
275
Note: See TracBrowser for help on using the repository browser.