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

Last change on this file since 89 was 89, checked in by epyon, 12 years ago
  • gfx folders added - moved image, texture_atlas and texture_font to them
File size: 9.0 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/gfx/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#include <nv/interface/mesh.hh>
18
19const nv::uint16 size_x  = 16;
20const nv::uint16 size_y  = 16;
21const nv::uint16 size_xy = size_x * size_y;
22
23nv::uint8 height[size_xy] =
24{
25        4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
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,  6,  6,  6,  6,  6,  6,  6,  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,  5,  5,  5,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
32        4,  4,  4,  5,  5,  5,  4,  2,  2,  2,  2,  2,  2,  4,  4,  4,
33        4,  4,  4,  5,  5,  5,  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,  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,  4,  4,  4,  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};
42
43nv::uint8 map[size_xy] =
44{
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,  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,  6,  6,  6,  6,  6,  6,  2,  2,  2,
53        3,  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,  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,  2,  2,  2,  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};
62
63class application
64{
65public:
66        application();
67        bool initialize();
68        bool run();
69        ~application();
70protected:
71        nv::device* m_device;
72        nv::window* m_window;
73        nv::texture2d* m_texture;
74        nv::clear_state m_clear_state;
75        nv::render_state m_render_state;
76
77        nv::program* m_char_program;
78        nv::program* m_box_program;
79        nv::vertex_array* m_char_va;
80        nv::vertex_array* m_box_va;
81        unsigned int m_count;
82};
83
84application::application()
85{
86        m_device = new nv::gl_device();
87        m_window = m_device->create_window( 800, 600 );
88       
89        nv::load_sdl_image_library();
90        SDL_Surface* texture = IMG_Load( "spritesheet.png" );
91        nv::image sprites( glm::ivec2( texture->w, texture->h ), 4, (nv::uint8*)texture->pixels );
92        nv::sampler sampler( nv::sampler::NEAREST, nv::sampler::REPEAT );
93        m_texture = m_device->create_texture2d( sprites.get_size(), nv::RGBA, nv::UBYTE, sampler, (void*)sprites.get_data() );
94
95        m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
96        m_render_state.depth_test.enabled = true;
97        m_render_state.blending.enabled   = true;
98        m_render_state.blending.src_rgb_factor   = nv::blending::SRC_ALPHA;
99        m_render_state.blending.dst_rgb_factor   = nv::blending::ONE_MINUS_SRC_ALPHA;
100        m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
101        m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
102}
103
104bool application::initialize()
105{
106        { // CHARACTER
107                nv::mesh cmesh;
108                nv::vertex_attribute<nv::i8vec3>::list& vtx = cmesh.add_attribute<nv::i8vec3>("coords")->get();
109                nv::vertex_attribute<nv::i8vec3>::list& mat = cmesh.add_attribute<nv::i8vec3>("material")->get();
110                int m = 16;     int x = 0; int y = 0; int h = 0;
111                vtx.emplace_back( x,   h, y ); 
112                vtx.emplace_back( x,   h, y+1 );
113                vtx.emplace_back( x+1, h, y+1 );
114                vtx.emplace_back( x+1, h, y+1 );
115                vtx.emplace_back( x+1, h, y ); 
116                vtx.emplace_back( x,   h, y ); 
117                mat.insert( mat.end(), 6, nv::i8vec3( m, 1, 0 ) );
118                m_char_program = m_device->create_program( nv::slurp( "char.vert" ), nv::slurp( "char.frag" ) );
119                m_char_va      = m_device->create_vertex_array( &cmesh, &(m_char_program->get_attributes()), nv::STATIC_DRAW );
120        }
121
122        { // WORLD
123                nv::mesh wmesh;
124                nv::vertex_attribute<nv::i8vec3>::list& vtx = wmesh.add_attribute<nv::i8vec3>("coords")->get();
125                nv::vertex_attribute<nv::i8vec3>::list& mat = wmesh.add_attribute<nv::i8vec3>("material")->get();
126
127                for (int i = 0; i < size_x * size_y; ++i )
128                {
129                        int x = i % size_x;
130                        int y = i / size_x;
131
132                        vtx.emplace_back( x,   height[i], y   );
133                        vtx.emplace_back( x,   height[i], y+1 );
134                        vtx.emplace_back( x+1, height[i], y+1 );
135                        vtx.emplace_back( x+1, height[i], y+1 );
136                        vtx.emplace_back( x+1, height[i], y );   
137                        vtx.emplace_back( x,   height[i], y );   
138                        mat.insert( mat.end(), 6, nv::i8vec3( map[i], 2, 0 ) );
139
140                        if (i > 0 && height[i-1] != height[i])
141                        {
142                                short dir   = height[i-1] > height[i] ? -1 : 1;
143                                nv::uint8 m = 1;
144                                for ( int h = height[i-1]; h != height[i]; h = h + dir )
145                                {
146                                        m_count += 6;
147                                        vtx.emplace_back( x, h,     y );   
148                                        vtx.emplace_back( x, h,     y+1 );
149                                        vtx.emplace_back( x, h+dir, y+1 );
150                                        vtx.emplace_back( x, h+dir, y+1 );
151                                        vtx.emplace_back( x, h+dir, y );   
152                                        vtx.emplace_back( x, h,     y );   
153                                        mat.insert( mat.end(), 6, nv::i8vec3( m, -dir, 0 ) );
154                                }
155                        }
156
157                        if (i >= size_x && height[i-size_x] != height[i])
158                        {
159                                short dir   = height[i-size_x] > height[i] ? -1 : 1;
160                                nv::uint8 m = 1;
161                                for ( int h = height[i-size_x]; h != height[i]; h = h + dir )
162                                {
163                                        vtx.emplace_back( x,   h,     y );
164                                        vtx.emplace_back( x,   h+dir, y );
165                                        vtx.emplace_back( x+1, h+dir, y );
166
167                                        vtx.emplace_back( x+1, h+dir, y );
168                                        vtx.emplace_back( x+1, h,     y );
169                                        vtx.emplace_back( x,   h,     y );
170                                        mat.insert( mat.end(), 6, nv::i8vec3( m, -3*dir, 0 ) );
171                                }
172                        }
173
174                }
175
176                m_count       = vtx.size();
177                m_box_program = m_device->create_program( nv::slurp( "box.vert" ), nv::slurp( "box.frag" ) );
178                m_box_va      = m_device->create_vertex_array( &wmesh, &(m_box_program->get_attributes()), nv::STATIC_DRAW );
179        }
180
181        return true;
182}
183
184bool application::run()
185{
186        int keypress = 0;
187
188        glm::vec3 move( 0, 0, 0 );
189
190        while(!keypress)
191        {
192                m_window->get_context()->clear( m_clear_state );
193
194                glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(-8.5, 0.0, -8.0));
195                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));
196                glm::mat4 projection = glm::perspective(25.0f, 1.0f*800.0f/600.0f, 0.1f, 100.0f);
197                glm::mat4 mv         = view * model;
198
199                m_texture->bind( 0 );
200                m_box_program->set_uniform( "matrix_mvp", projection * mv );
201                m_box_program->set_uniform( "light", glm::vec3(8.5, 4.5, 6.5) + move );
202                m_box_program->set_uniform( "tex", 0 );
203                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_box_program, m_box_va, m_count );
204
205                m_char_program->set_uniform( "matrix_mvp", projection * mv );
206                m_char_program->set_uniform( "pos", move + glm::vec3( 8, 4.1, 6 ) );
207                m_char_program->set_uniform( "tex", 0 );
208                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_char_program, m_char_va, 6 );
209
210                SDL_GL_SwapBuffers();
211                SDL_Event event;
212                while(SDL_PollEvent(&event))
213                {     
214                        switch (event.type)
215                        {
216                        case SDL_QUIT:
217                                keypress = 1;
218                                break;
219                        case SDL_KEYDOWN:
220                                switch (event.key.keysym.sym)
221                                {
222                                case SDLK_ESCAPE: keypress = 1; break;
223                                case SDLK_LEFT: move.x = move.x - 1.0f; break;
224                                case SDLK_RIGHT: move.x = move.x + 1.0f; break;
225                                case SDLK_UP: move.z = move.z - 1.0f; break;
226                                case SDLK_DOWN: move.z = move.z + 1.0f; break;
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" );
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.