[102] | 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 <glm/glm.hpp>
|
---|
| 11 | #include <glm/gtc/matrix_transform.hpp>
|
---|
| 12 | #include <glm/gtc/type_ptr.hpp>
|
---|
| 13 | #include <nv/string.hh>
|
---|
| 14 | #include <nv/types.hh>
|
---|
| 15 | #include <nv/interface/mesh.hh>
|
---|
| 16 | #include <cstdlib> // rand
|
---|
| 17 | #include <ctime> // time
|
---|
| 18 |
|
---|
| 19 | #include <nv/gfx/cached_buffer.hh>
|
---|
| 20 |
|
---|
| 21 | struct vertex
|
---|
| 22 | {
|
---|
| 23 | nv::ivec2 coord;
|
---|
| 24 | nv::vec4 color;
|
---|
| 25 | vertex() {}
|
---|
| 26 | vertex( const nv::ivec2& coord, const nv::vec4& color )
|
---|
| 27 | : coord( coord ), color( color ) {}
|
---|
| 28 | };
|
---|
| 29 |
|
---|
| 30 | struct quad
|
---|
| 31 | {
|
---|
| 32 | vertex vtx[6];
|
---|
| 33 | quad( const nv::ivec2& coorda, const nv::ivec2& coordb, const nv::vec4& color )
|
---|
| 34 | {
|
---|
| 35 | vtx[0].color = color;
|
---|
| 36 | vtx[1].color = color;
|
---|
| 37 | vtx[2].color = color;
|
---|
| 38 | vtx[3].color = color;
|
---|
| 39 | vtx[4].color = color;
|
---|
| 40 | vtx[5].color = color;
|
---|
| 41 | vtx[0].coord = coorda;
|
---|
| 42 | vtx[1].coord = nv::ivec2( coorda.x, coordb.y );
|
---|
| 43 | vtx[2].coord = coordb;
|
---|
| 44 | vtx[3].coord = coordb;
|
---|
| 45 | vtx[4].coord = nv::ivec2( coordb.x, coorda.y );
|
---|
| 46 | vtx[5].coord = coorda;
|
---|
| 47 | }
|
---|
| 48 | quad( const nv::ivec2& coorda, const nv::ivec2& coordb, const nv::ivec2& coordc, const nv::ivec2& coordd, const nv::vec4& color )
|
---|
| 49 | {
|
---|
| 50 | vtx[0].color = color;
|
---|
| 51 | vtx[1].color = color;
|
---|
| 52 | vtx[2].color = color;
|
---|
| 53 | vtx[3].color = color;
|
---|
| 54 | vtx[4].color = color;
|
---|
| 55 | vtx[5].color = color;
|
---|
| 56 | vtx[0].coord = coorda;
|
---|
| 57 | vtx[1].coord = coordb;
|
---|
| 58 | vtx[2].coord = coordc;
|
---|
| 59 | vtx[3].coord = coordc;
|
---|
| 60 | vtx[4].coord = coordd;
|
---|
| 61 | vtx[5].coord = coorda;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | };
|
---|
| 65 |
|
---|
| 66 | struct app_window
|
---|
| 67 | {
|
---|
| 68 | app_window( nv::cached_buffer<quad>* cache, const glm::ivec2& a, const glm::ivec2& b, const glm::vec4& color )
|
---|
| 69 | : m_slice( cache )
|
---|
| 70 | {
|
---|
| 71 | glm::vec4 dcolor( color.x * 0.5, color.y * 0.5, color.z * 0.5, 1.0 );
|
---|
| 72 | std::vector<quad>& v = m_slice.lock();
|
---|
| 73 | nv::ivec2 a2( a.x, b.y );
|
---|
| 74 | nv::ivec2 b2( b.x, a.y );
|
---|
| 75 | nv::ivec2 t1( 10, 10 );
|
---|
| 76 | nv::ivec2 t2( -10, 10 );
|
---|
| 77 | v.emplace_back( a - t1, a, b2, b2 - t2, dcolor );
|
---|
| 78 | v.emplace_back( a - t1, a2 + t2, a2, a, dcolor );
|
---|
| 79 | v.emplace_back( a2 + t2, b + t1, b, a2, dcolor );
|
---|
| 80 | v.emplace_back( b + t1, b2 - t2, b2, b, dcolor );
|
---|
| 81 | v.emplace_back( a, b, color );
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | void change_color( const glm::vec4& color )
|
---|
| 85 | {
|
---|
| 86 | glm::vec4 dcolor( color.x * 0.5, color.y * 0.5, color.z * 0.5, 1.0 );
|
---|
| 87 | std::vector<quad>& v = m_slice.lock();
|
---|
| 88 | vertex* vtx = (vertex*)v.data();
|
---|
| 89 | for (size_t i = 0; i < (v.size() - 1) * 6; ++i )
|
---|
| 90 | vtx[i].color = color;
|
---|
| 91 | for (size_t i = (v.size() - 1) * 6; i < (v.size()) * 6; ++i )
|
---|
| 92 | vtx[i].color = dcolor;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | void draw()
|
---|
| 96 | {
|
---|
| 97 | m_slice.commit();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | nv::buffer_slice<quad> m_slice;
|
---|
| 101 | };
|
---|
| 102 |
|
---|
| 103 | class application
|
---|
| 104 | {
|
---|
| 105 | public:
|
---|
| 106 | application();
|
---|
| 107 | bool initialize();
|
---|
| 108 | bool run();
|
---|
| 109 | void kill_window();
|
---|
| 110 | void spawn_window();
|
---|
| 111 | void recolor_window();
|
---|
| 112 | ~application();
|
---|
| 113 | protected:
|
---|
| 114 | nv::device* m_device;
|
---|
| 115 | nv::window* m_window;
|
---|
| 116 | nv::clear_state m_clear_state;
|
---|
| 117 | nv::render_state m_render_state;
|
---|
| 118 |
|
---|
| 119 | nv::cached_buffer<quad>* m_quad_cache;
|
---|
| 120 | std::vector<app_window> m_windows;
|
---|
| 121 |
|
---|
| 122 | nv::program* m_program;
|
---|
| 123 | nv::vertex_array* m_va;
|
---|
| 124 | unsigned int m_count;
|
---|
| 125 | int m_coord_loc;
|
---|
| 126 | int m_color_loc;
|
---|
| 127 | };
|
---|
| 128 |
|
---|
| 129 | application::application()
|
---|
| 130 | {
|
---|
| 131 | m_device = new nv::gl_device();
|
---|
| 132 | m_window = m_device->create_window( 800, 600 );
|
---|
| 133 |
|
---|
| 134 | m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
|
---|
| 135 | m_render_state.depth_test.enabled = false;
|
---|
| 136 | m_render_state.culling.enabled = false;
|
---|
| 137 | m_render_state.blending.enabled = false;
|
---|
| 138 | m_render_state.blending.src_rgb_factor = nv::blending::SRC_ALPHA;
|
---|
| 139 | m_render_state.blending.dst_rgb_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
|
---|
| 140 | m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
|
---|
| 141 | m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | bool application::initialize()
|
---|
| 145 | {
|
---|
| 146 | {
|
---|
| 147 | m_program = m_device->create_program( nv::slurp( "cachebuf.vert" ), nv::slurp( "cachebuf.frag" ) );
|
---|
| 148 | m_va = m_device->create_vertex_array();
|
---|
| 149 |
|
---|
| 150 | m_quad_cache = new nv::cached_buffer<quad>( m_device, nv::DYNAMIC_DRAW, 20, true );
|
---|
| 151 | m_coord_loc = m_program->get_attribute("coord")->get_location();
|
---|
| 152 | m_color_loc = m_program->get_attribute("color")->get_location();
|
---|
| 153 |
|
---|
| 154 | m_va->add_vertex_buffer( m_coord_loc, (nv::vertex_buffer*)m_quad_cache->get_buffer(), nv::INT, 2, 0, sizeof( vertex ), false );
|
---|
| 155 | m_va->add_vertex_buffer( m_color_loc, (nv::vertex_buffer*)m_quad_cache->get_buffer(), nv::FLOAT, 4, offset_of( &vertex::color ), sizeof( vertex ), false );
|
---|
| 156 | }
|
---|
| 157 | return true;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | bool application::run()
|
---|
| 161 | {
|
---|
| 162 | int keypress = 0;
|
---|
| 163 | m_program->bind();
|
---|
| 164 | glm::mat4 projection = glm::ortho( 0.0f, 800.0f, 600.0f, 0.0f, -1.0f, 1.0f );
|
---|
| 165 | m_program->set_uniform( "projection", glm::mat4(projection) );
|
---|
| 166 |
|
---|
| 167 | while(!keypress)
|
---|
| 168 | {
|
---|
| 169 | for ( auto& w : m_windows )
|
---|
| 170 | {
|
---|
| 171 | w.draw();
|
---|
| 172 | }
|
---|
| 173 | m_quad_cache->commit();
|
---|
| 174 | m_va->update_vertex_buffer( m_coord_loc, (nv::vertex_buffer*)m_quad_cache->get_buffer(), false );
|
---|
| 175 | m_va->update_vertex_buffer( m_color_loc, (nv::vertex_buffer*)m_quad_cache->get_buffer(), false );
|
---|
| 176 |
|
---|
| 177 | m_window->get_context()->clear( m_clear_state );
|
---|
| 178 | m_program->bind();
|
---|
| 179 | // m_program->set_uniform( "tex", 0 );
|
---|
| 180 | m_window->get_context()->draw( nv::TRIANGLES, m_render_state, m_program, m_va, m_windows.size() * 5 * 6 );
|
---|
| 181 | m_window->swap_buffers();
|
---|
| 182 |
|
---|
| 183 | nv::io_event event;
|
---|
| 184 | while(m_window->poll_event(event))
|
---|
| 185 | {
|
---|
| 186 | switch (event.type)
|
---|
| 187 | {
|
---|
| 188 | case nv::EV_QUIT:
|
---|
| 189 | keypress = 1;
|
---|
| 190 | break;
|
---|
| 191 | case nv::EV_KEY:
|
---|
| 192 | if (event.key.pressed)
|
---|
| 193 | {
|
---|
| 194 | switch (event.key.code)
|
---|
| 195 | {
|
---|
| 196 | case nv::KEY_N : spawn_window(); break;
|
---|
| 197 | case nv::KEY_C : recolor_window(); break;
|
---|
| 198 | case nv::KEY_K : kill_window(); break;
|
---|
| 199 | case nv::KEY_ESCAPE : keypress = 1; break;
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 | break;
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 | return true;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | void application::spawn_window()
|
---|
| 210 | {
|
---|
| 211 | glm::ivec2 a( std::rand() % 600, std::rand() % 400 );
|
---|
| 212 | glm::ivec2 b( std::rand() % 200, std::rand() % 200 );
|
---|
| 213 | NV_LOG( nv::LOG_INFO, "Spawn (" << a.x << "," << a.y << "x" << b.x << "," << b.y << ")" );
|
---|
| 214 | m_windows.emplace_back( m_quad_cache, a, a + b, glm::vec4( 0, 0, 1, 1 ) );
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | void application::kill_window()
|
---|
| 218 | {
|
---|
| 219 | if ( m_windows.size() == 0 ) return;
|
---|
| 220 | size_t index = rand() % m_windows.size();
|
---|
| 221 | m_windows.erase( m_windows.begin() + index );
|
---|
| 222 | }
|
---|
| 223 | void application::recolor_window()
|
---|
| 224 | {
|
---|
| 225 | if ( m_windows.size() == 0 ) return;
|
---|
| 226 | size_t index = rand() % m_windows.size();
|
---|
| 227 | m_windows[ index ].change_color( nv::vec4( (float)rand() / float(RAND_MAX), (float)rand() / float(RAND_MAX), (float)rand() / float(RAND_MAX), 1.0 ) );
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | application::~application()
|
---|
| 231 | {
|
---|
| 232 | m_windows.clear();
|
---|
| 233 | delete m_quad_cache;
|
---|
| 234 |
|
---|
| 235 | delete m_program;
|
---|
| 236 | delete m_va;
|
---|
| 237 | delete m_window;
|
---|
| 238 | delete m_device;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 |
|
---|
| 242 | int main(int, char* [])
|
---|
| 243 | {
|
---|
| 244 | std::srand((unsigned int) std::time(0) );
|
---|
| 245 | nv::logger log(nv::LOG_TRACE);
|
---|
| 246 | log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
|
---|
| 247 | log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
|
---|
| 248 |
|
---|
| 249 | NV_LOG( nv::LOG_NOTICE, "Logging started" );
|
---|
| 250 | application app;
|
---|
| 251 | if ( app.initialize() )
|
---|
| 252 | {
|
---|
| 253 | app.run();
|
---|
| 254 | }
|
---|
| 255 | NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
|
---|
| 256 |
|
---|
| 257 | return 0;
|
---|
| 258 | }
|
---|
| 259 |
|
---|