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

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