[324] | 1 | #include <nv/gfx/keyframed_mesh.hh>
|
---|
| 2 | #include <nv/gl/gl_device.hh>
|
---|
| 3 | #include <nv/gfx/image.hh>
|
---|
| 4 | #include <nv/gfx/debug_draw.hh>
|
---|
| 5 | #include <nv/interface/context.hh>
|
---|
| 6 | #include <nv/interface/window.hh>
|
---|
| 7 | #include <nv/core/profiler.hh>
|
---|
| 8 | #include <nv/core/logging.hh>
|
---|
| 9 | #include <nv/core/logger.hh>
|
---|
| 10 | #include <nv/core/math.hh>
|
---|
| 11 | #include <nv/core/time.hh>
|
---|
| 12 | #include <nv/core/string.hh>
|
---|
| 13 | #include <glm/gtx/rotate_vector.hpp>
|
---|
| 14 |
|
---|
| 15 | class application
|
---|
| 16 | {
|
---|
| 17 | public:
|
---|
| 18 | application();
|
---|
| 19 | bool initialize();
|
---|
| 20 | bool run();
|
---|
| 21 | ~application();
|
---|
| 22 | protected:
|
---|
| 23 | nv::mesh_data* generate_box();
|
---|
| 24 | nv::mesh_data* generate_subdiv_sphere( int levels );
|
---|
| 25 |
|
---|
| 26 | nv::device* m_device;
|
---|
| 27 | nv::window* m_window;
|
---|
| 28 | nv::context* m_context;
|
---|
| 29 | nv::debug_data* m_debug_data;
|
---|
| 30 | nv::texture m_diffuse;
|
---|
| 31 | nv::program m_program;
|
---|
| 32 | nv::mesh_data* m_data;
|
---|
| 33 | nv::vertex_array m_va;
|
---|
| 34 |
|
---|
| 35 | nv::clear_state m_clear_state;
|
---|
| 36 | nv::render_state m_render_state;
|
---|
| 37 | nv::scene_state m_scene_state;
|
---|
| 38 | };
|
---|
| 39 |
|
---|
| 40 | struct vertex
|
---|
| 41 | {
|
---|
| 42 | nv::vec3 position;
|
---|
| 43 | };
|
---|
| 44 |
|
---|
| 45 | nv::mesh_data* generate_subdiv_sphere( int levels )
|
---|
| 46 | {
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | nv::mesh_raw_channel* vchannel = nv::mesh_raw_channel::create<vertex>( 8 );
|
---|
| 50 | nv::mesh_raw_channel* ichannel = nv::mesh_raw_channel::create_index( nv::USHORT, 6 * 6 );
|
---|
| 51 | vertex* vtx = (vertex*)vchannel->data;
|
---|
| 52 | nv::uint16* idx = (nv::uint16*)ichannel->data;
|
---|
| 53 |
|
---|
| 54 | nv::mesh_data* result = new nv::mesh_data;
|
---|
| 55 | result->add_channel( vchannel );
|
---|
| 56 | result->add_channel( ichannel );
|
---|
| 57 | return result;
|
---|
| 58 |
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | nv::mesh_data* application::generate_box()
|
---|
| 62 | {
|
---|
| 63 | nv::mesh_raw_channel* vchannel = nv::mesh_raw_channel::create<vertex>( 8 );
|
---|
| 64 | nv::mesh_raw_channel* ichannel = nv::mesh_raw_channel::create_index( nv::USHORT, 6 * 6 );
|
---|
| 65 | vertex* vtx = (vertex*)vchannel->data;
|
---|
| 66 | nv::uint16* idx = (nv::uint16*)ichannel->data;
|
---|
| 67 |
|
---|
| 68 | vtx[0].position = nv::vec3( 1.0f, 1.0f, 1.0f );
|
---|
| 69 | vtx[1].position = nv::vec3( 1.0f, 1.0f,-1.0f );
|
---|
| 70 | vtx[2].position = nv::vec3(-1.0f, 1.0f,-1.0f );
|
---|
| 71 | vtx[3].position = nv::vec3(-1.0f, 1.0f, 1.0f );
|
---|
| 72 | vtx[4].position = nv::vec3( 1.0f,-1.0f, 1.0f );
|
---|
| 73 | vtx[5].position = nv::vec3( 1.0f,-1.0f,-1.0f );
|
---|
| 74 | vtx[6].position = nv::vec3(-1.0f,-1.0f,-1.0f );
|
---|
| 75 | vtx[7].position = nv::vec3(-1.0f,-1.0f, 1.0f );
|
---|
| 76 |
|
---|
| 77 | auto square = [&]( nv::uint16 i, nv::uint16 a, nv::uint16 b, nv::uint16 c, nv::uint16 d )
|
---|
| 78 | {
|
---|
| 79 | idx[i+0] = a;
|
---|
| 80 | idx[i+1] = b;
|
---|
| 81 | idx[i+2] = c;
|
---|
| 82 |
|
---|
| 83 | idx[i+3] = c;
|
---|
| 84 | idx[i+4] = d;
|
---|
| 85 | idx[i+5] = a;
|
---|
| 86 | };
|
---|
| 87 |
|
---|
| 88 | square( 0, 0, 1, 2, 3 );
|
---|
| 89 | square( 6, 1, 0, 4, 5 );
|
---|
| 90 | square(12, 2, 1, 5, 6 );
|
---|
| 91 | square(18, 3, 2, 6, 7 );
|
---|
| 92 | square(24, 0, 3, 7, 4 );
|
---|
| 93 | square(30, 7, 6, 5, 4 );
|
---|
| 94 |
|
---|
| 95 | nv::mesh_data* result = new nv::mesh_data;
|
---|
| 96 | result->add_channel( vchannel );
|
---|
| 97 | result->add_channel( ichannel );
|
---|
| 98 | return result;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | application::application()
|
---|
| 103 | {
|
---|
| 104 | NV_PROFILE( "app_construct" );
|
---|
| 105 | m_device = new nv::gl_device();
|
---|
| 106 | m_window = m_device->create_window( 800, 600, false );
|
---|
| 107 | m_context = m_window->get_context();
|
---|
| 108 |
|
---|
| 109 | // nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::REPEAT );
|
---|
| 110 | // nv::image_data* data = m_device->create_image_data( "data/manc.png" );
|
---|
| 111 | // m_diffuse = m_device->create_texture( data, sampler );
|
---|
| 112 | // delete data;
|
---|
| 113 |
|
---|
| 114 | m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
|
---|
| 115 | m_clear_state.color = nv::vec4(0.2f,0.2f,0.2f,1.0f);
|
---|
| 116 | m_render_state.depth_test.enabled = true;
|
---|
| 117 | m_render_state.culling.enabled = true;
|
---|
| 118 | m_render_state.blending.enabled = false;
|
---|
| 119 | m_render_state.blending.src_rgb_factor = nv::blending::SRC_ALPHA;
|
---|
| 120 | m_render_state.blending.dst_rgb_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
|
---|
| 121 | m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
|
---|
| 122 | m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
|
---|
| 123 |
|
---|
| 124 | m_debug_data = new nv::debug_data( m_context );
|
---|
| 125 | m_debug_data->push_aabox( nv::vec3( 1.0f, 1.0f, 1.0f ), nv::vec3(-1.0f, -1.0f,-1.0f ), nv::vec3( 1.0f, 0.0f, 0.0f ) );
|
---|
| 126 | m_debug_data->update();
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | bool application::initialize()
|
---|
| 130 | {
|
---|
| 131 | NV_PROFILE( "app_initialize" );
|
---|
| 132 | m_program = m_device->create_program( nv::slurp( "planet.vert" ), nv::slurp( "planet.frag" ) );
|
---|
| 133 | m_data = generate_box();
|
---|
| 134 | m_va = m_context->create_vertex_array( m_data, nv::STATIC_DRAW );
|
---|
| 135 | return true;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | bool application::run()
|
---|
| 139 | {
|
---|
| 140 | nv::profiler::pointer()->log_report();
|
---|
| 141 | NV_PROFILE( "app_run" );
|
---|
| 142 | int keypress = 0;
|
---|
| 143 |
|
---|
| 144 | nv::uint32 ticks = m_device->get_ticks();
|
---|
| 145 | nv::uint32 last_ticks;
|
---|
| 146 | nv::fps_counter_class< nv::system_us_timer > fps_counter;
|
---|
| 147 |
|
---|
| 148 | nv::uint16 count = 0;
|
---|
| 149 | while(!keypress)
|
---|
| 150 | {
|
---|
| 151 | last_ticks = ticks;
|
---|
| 152 | ticks = m_device->get_ticks();
|
---|
| 153 | nv::uint32 elapsed = ticks - last_ticks;
|
---|
| 154 | m_context->clear( m_clear_state );
|
---|
| 155 | glm::vec3 eye = glm::rotate( glm::vec3( 2.0f, 0.0f, 0.0f ), (ticks / 20.f), glm::vec3( 0.0,1.0,0.0 ) );
|
---|
| 156 | // eye = glm::vec3( 3.0f, 0.0f, 0.0f );
|
---|
| 157 | m_scene_state.set_model( nv::mat4(1.0f) );
|
---|
| 158 | m_scene_state.get_camera().set_lookat(eye, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0, 1.0, 0.0));
|
---|
| 159 | float ratio = (float)m_window->get_width() / (float)m_window->get_height();
|
---|
| 160 | m_scene_state.get_camera().set_perspective(60.0f, ratio, 0.1f, 1000.0f);
|
---|
| 161 |
|
---|
| 162 | // m_context->bind( m_diffuse, nv::TEX_DIFFUSE );
|
---|
| 163 | m_device->set_opt_uniform( m_program, "center", glm::vec3(0.0,0.0,0.0) );
|
---|
| 164 | m_device->set_opt_uniform( m_program, "radius", 1.0f );
|
---|
| 165 | m_device->set_opt_uniform( m_program, "light_position", glm::vec3(120.0, 120.0, 0) );
|
---|
| 166 | m_device->set_opt_uniform( m_program, "light_diffuse", glm::vec4(0.5,0.5,0.5,1.0) );
|
---|
| 167 | m_device->set_opt_uniform( m_program, "light_specular", glm::vec4(1.0,1.0,1.0,1.0) );
|
---|
| 168 | m_render_state.culling.order = nv::culling::CW;
|
---|
| 169 | m_context->draw( nv::TRIANGLES, m_render_state, m_scene_state, m_program, m_va, 6*6 );
|
---|
| 170 | m_render_state.culling.order = nv::culling::CCW;
|
---|
| 171 |
|
---|
| 172 | m_context->draw( nv::LINES, m_render_state, m_scene_state, m_debug_data->get_program(), m_debug_data->get_vertex_array(), m_debug_data->get_count() );
|
---|
| 173 |
|
---|
| 174 | m_window->swap_buffers();
|
---|
| 175 |
|
---|
| 176 | nv::io_event event;
|
---|
| 177 | while(m_window->poll_event(event))
|
---|
| 178 | {
|
---|
| 179 | switch (event.type)
|
---|
| 180 | {
|
---|
| 181 | case nv::EV_QUIT:
|
---|
| 182 | keypress = 1;
|
---|
| 183 | break;
|
---|
| 184 | case nv::EV_KEY:
|
---|
| 185 | if (event.key.pressed)
|
---|
| 186 | {
|
---|
| 187 | switch (event.key.code)
|
---|
| 188 | {
|
---|
| 189 | case nv::KEY_ESCAPE : keypress = 1; break;
|
---|
| 190 | case nv::KEY_F1 : nv::profiler::pointer()->log_report(); break;
|
---|
| 191 | default: break;
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 | break;
|
---|
| 195 | default: break;
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | fps_counter.tick();
|
---|
| 200 | if ( count % 120 == 0 )
|
---|
| 201 | {
|
---|
| 202 | m_window->set_title( "Nova Engine - " + nv::to_string( (nv::uint32)fps_counter.fps() ) );
|
---|
| 203 | }
|
---|
| 204 | count++;
|
---|
| 205 | }
|
---|
| 206 | return true;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | application::~application()
|
---|
| 210 | {
|
---|
| 211 | m_context->release( m_va );
|
---|
| 212 | m_device->release( m_program );
|
---|
| 213 | m_device->release( m_diffuse );
|
---|
| 214 |
|
---|
| 215 | delete m_window;
|
---|
| 216 | delete m_device;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 |
|
---|
| 220 | int main(int, char* [])
|
---|
| 221 | {
|
---|
| 222 | nv::logger log(nv::LOG_TRACE);
|
---|
| 223 | log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
|
---|
| 224 | log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
|
---|
| 225 |
|
---|
| 226 | NV_LOG( nv::LOG_NOTICE, "Logging started" );
|
---|
| 227 | application app;
|
---|
| 228 | if ( app.initialize() )
|
---|
| 229 | {
|
---|
| 230 | app.run();
|
---|
| 231 | }
|
---|
| 232 | NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
|
---|
| 233 |
|
---|
| 234 | return 0;
|
---|
| 235 | }
|
---|
| 236 |
|
---|