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

Last change on this file since 71 was 71, checked in by epyon, 12 years ago
  • mesh interface class with vertex_attribute, index, primitive and order support
File size: 9.4 KB
RevLine 
[46]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>
[71]17#include <nv/interface/mesh.hh>
[46]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();
[71]68        bool init_program( const std::string& name, nv::program*& p, nv::vertex_array*& va, nv::mesh* m );
[46]69        bool run();
70        ~application();
71protected:
72        nv::device* m_device;
73        nv::window* m_window;
74        nv::texture2d* m_texture;
75        nv::clear_state m_clear_state;
76        nv::render_state m_render_state;
77
78        nv::program* m_char_program;
79        nv::program* m_box_program;
80        nv::vertex_array* m_char_va;
81        nv::vertex_array* m_box_va;
82        unsigned int m_count;
83};
84
85application::application()
86{
87        m_device = new nv::gl_device();
88        m_window = m_device->create_window( 800, 600 );
89       
90        nv::load_sdl_image_library();
91        SDL_Surface* texture = IMG_Load( "spritesheet.png" );
92        nv::image sprites( glm::ivec2( texture->w, texture->h ), 4, (nv::uint8*)texture->pixels );
[49]93        nv::sampler sampler( nv::sampler::NEAREST, nv::sampler::REPEAT );
94        m_texture = m_device->create_texture2d( sprites.get_size(), nv::RGBA, nv::UBYTE, sampler, (void*)sprites.get_data() );
[46]95
96        m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
97        m_render_state.depth_test.enabled = true;
98        m_render_state.blending.enabled   = true;
99        m_render_state.blending.src_rgb_factor   = nv::blending::SRC_ALPHA;
100        m_render_state.blending.dst_rgb_factor   = nv::blending::ONE_MINUS_SRC_ALPHA;
101        m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
102        m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
103}
104
105bool application::initialize()
106{
107        { // CHARACTER
[71]108                nv::mesh cmesh;
109                nv::vertex_attribute<nv::i8vec3>::list& vtx = cmesh.add_attribute<nv::i8vec3>("coords")->get();
110                nv::vertex_attribute<nv::i8vec3>::list& mat = cmesh.add_attribute<nv::i8vec3>("material")->get();
[46]111                int m = 16;     int x = 0; int y = 0; int h = 0;
[71]112                vtx.emplace_back( x,   h, y ); 
113                vtx.emplace_back( x,   h, y+1 );
114                vtx.emplace_back( x+1, h, y+1 );
115                vtx.emplace_back( x+1, h, y+1 );
116                vtx.emplace_back( x+1, h, y ); 
117                vtx.emplace_back( x,   h, y ); 
118                mat.insert( mat.end(), 6, nv::i8vec3( m, 1, 0 ) );
[46]119
[71]120                if (!init_program( "char", m_char_program, m_char_va, &cmesh ) ) return false;
[46]121        }
122
123        { // WORLD
[71]124                nv::mesh wmesh;
125                nv::vertex_attribute<nv::i8vec3>::list& vtx = wmesh.add_attribute<nv::i8vec3>("coords")->get();
126                nv::vertex_attribute<nv::i8vec3>::list& mat = wmesh.add_attribute<nv::i8vec3>("material")->get();
127
[46]128                for (int i = 0; i < size_x * size_y; ++i )
129                {
130                        int x = i % size_x;
131                        int y = i / size_x;
132
[71]133                        vtx.emplace_back( x,   height[i], y   );
134                        vtx.emplace_back( x,   height[i], y+1 );
135                        vtx.emplace_back( x+1, height[i], y+1 );
136                        vtx.emplace_back( x+1, height[i], y+1 );
137                        vtx.emplace_back( x+1, height[i], y );   
138                        vtx.emplace_back( x,   height[i], y );   
139                        mat.insert( mat.end(), 6, nv::i8vec3( map[i], 2, 0 ) );
[46]140
141                        if (i > 0 && height[i-1] != height[i])
142                        {
143                                short dir   = height[i-1] > height[i] ? -1 : 1;
144                                nv::uint8 m = 1;
145                                for ( int h = height[i-1]; h != height[i]; h = h + dir )
146                                {
[71]147                                        m_count += 6;
148                                        vtx.emplace_back( x, h,     y );   
149                                        vtx.emplace_back( x, h,     y+1 );
150                                        vtx.emplace_back( x, h+dir, y+1 );
151                                        vtx.emplace_back( x, h+dir, y+1 );
152                                        vtx.emplace_back( x, h+dir, y );   
153                                        vtx.emplace_back( x, h,     y );   
154                                        mat.insert( mat.end(), 6, nv::i8vec3( m, -dir, 0 ) );
[46]155                                }
156                        }
157
158                        if (i >= size_x && height[i-size_x] != height[i])
159                        {
160                                short dir   = height[i-size_x] > height[i] ? -1 : 1;
161                                nv::uint8 m = 1;
162                                for ( int h = height[i-size_x]; h != height[i]; h = h + dir )
163                                {
[71]164                                        vtx.emplace_back( x,   h,     y );
165                                        vtx.emplace_back( x,   h+dir, y );
166                                        vtx.emplace_back( x+1, h+dir, y );
[46]167
[71]168                                        vtx.emplace_back( x+1, h+dir, y );
169                                        vtx.emplace_back( x+1, h,     y );
170                                        vtx.emplace_back( x,   h,     y );
171                                        mat.insert( mat.end(), 6, nv::i8vec3( m, -3*dir, 0 ) );
[46]172                                }
173                        }
174
175                }
176
[71]177                m_count = vtx.size();
178                if (!init_program( "box",  m_box_program, m_box_va, &wmesh ) ) return false;
[46]179        }
180
181        return true;
182}
183
[71]184bool application::init_program( const std::string& name, nv::program*& p, nv::vertex_array*& va, nv::mesh* m )
[46]185{
186        p  = m_device->create_program( nv::slurp( name+".vert" ), nv::slurp( name+".frag" ) );
187        va = m_device->create_vertex_array();
[71]188        const nv::mesh::map& attrs = m->get_attributes();
[46]189
[71]190        for ( auto attr : attrs )
191        {
192                nv::vertex_buffer* vb = m_device->create_vertex_buffer( nv::STATIC_DRAW, attr.second->get_size(), attr.second->get_data() );
193                va->add_vertex_buffer( p->get_attribute( attr.first )->get_location(), vb, attr.second->get_base_type(), attr.second->get_components() );
194        }
[46]195        return true;
196}
197
198bool application::run()
199{
200        int keypress = 0;
201
202        glm::vec3 move( 0, 0, 0 );
203
204        while(!keypress)
205        {
206                m_window->get_context()->clear( m_clear_state );
207
208                glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(-8.5, 0.0, -8.0));
209                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));
210                glm::mat4 projection = glm::perspective(25.0f, 1.0f*800.0f/600.0f, 0.1f, 100.0f);
211                glm::mat4 mv         = view * model;
212
213                m_texture->bind( 0 );
214                m_box_program->set_uniform( "matrix_mvp", projection * mv );
215                m_box_program->set_uniform( "light", glm::vec3(8.5, 4.5, 6.5) + move );
216                m_box_program->set_uniform( "tex", 0 );
217                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_box_program, m_box_va, m_count );
218
219                m_char_program->set_uniform( "matrix_mvp", projection * mv );
220                m_char_program->set_uniform( "pos", move + glm::vec3( 8, 4.1, 6 ) );
221                m_char_program->set_uniform( "tex", 0 );
222                m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_char_program, m_char_va, 6 );
223
224                SDL_GL_SwapBuffers();
225                SDL_Event event;
226                while(SDL_PollEvent(&event))
227                {     
228                        switch (event.type)
229                        {
230                        case SDL_QUIT:
231                                keypress = 1;
232                                break;
233                        case SDL_KEYDOWN:
234                                switch (event.key.keysym.sym)
235                                {
236                                case SDLK_ESCAPE: keypress = 1; break;
237                                case SDLK_LEFT: move.x = move.x - 1.0f; break;
238                                case SDLK_RIGHT: move.x = move.x + 1.0f; break;
239                                case SDLK_UP: move.z = move.z - 1.0f; break;
240                                case SDLK_DOWN: move.z = move.z + 1.0f; break;
241                                }
242                                break;
243                        }
244                }
245        }
246        return true;
247}
248
249application::~application()
250{
251        delete m_char_program;
252        delete m_box_program;
253        delete m_char_va;
254        delete m_box_va;
255        delete m_texture;
256        delete m_window;
257        delete m_device;
258}
259
260
261int main(int, char* [])
262{
263        nv::logger log(nv::LOG_TRACE);
264        log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
265        log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
266       
267        NV_LOG( nv::LOG_NOTICE, "Logging started" );
[47]268        application app;
269        if ( app.initialize() )
[46]270        {
[47]271                app.run();
[46]272        }
273        NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
274
275        return 0;
276}
277
Note: See TracBrowser for help on using the repository browser.