[137] | 1 | #include <nv/gl/gl_device.hh>
|
---|
| 2 | #include <nv/gfx/image.hh>
|
---|
| 3 | #include <nv/interface/context.hh>
|
---|
| 4 | #include <nv/interface/window.hh>
|
---|
| 5 | #include <nv/interface/mesh_loader.hh>
|
---|
| 6 | #include <nv/io/c_file_system.hh>
|
---|
| 7 | #include <nv/formats/obj_loader.hh>
|
---|
[321] | 8 | #include <nv/core/logging.hh>
|
---|
| 9 | #include <nv/core/logger.hh>
|
---|
| 10 | #include <nv/core/math.hh>
|
---|
| 11 | #include <nv/core/string.hh>
|
---|
[137] | 12 |
|
---|
| 13 | class application
|
---|
| 14 | {
|
---|
| 15 | public:
|
---|
| 16 | application();
|
---|
| 17 | bool initialize();
|
---|
| 18 | bool run();
|
---|
| 19 | ~application();
|
---|
| 20 | protected:
|
---|
[139] | 21 | nv::device* m_device;
|
---|
[321] | 22 | nv::context* m_context;
|
---|
[139] | 23 | nv::window* m_window;
|
---|
[321] | 24 | nv::texture m_diffuse;
|
---|
| 25 | nv::texture m_specular;
|
---|
| 26 | nv::scene_state m_scene_state;
|
---|
[139] | 27 | nv::clear_state m_clear_state;
|
---|
| 28 | nv::render_state m_render_state;
|
---|
[137] | 29 |
|
---|
[321] | 30 | nv::vertex_array m_va;
|
---|
| 31 | nv::program m_program;
|
---|
[238] | 32 | nv::mesh_data* m_mesh;
|
---|
[137] | 33 | };
|
---|
| 34 |
|
---|
| 35 | application::application()
|
---|
| 36 | {
|
---|
| 37 | m_device = new nv::gl_device();
|
---|
[237] | 38 | m_window = m_device->create_window( 800, 600, false );
|
---|
[321] | 39 | m_context = m_window->get_context();
|
---|
| 40 |
|
---|
[139] | 41 | nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::REPEAT );
|
---|
[137] | 42 |
|
---|
[321] | 43 | nv::image_data* image = m_device->create_image_data( "diffuse.png" );
|
---|
| 44 | m_diffuse = m_device->create_texture( image, sampler );
|
---|
| 45 | delete image;
|
---|
[139] | 46 |
|
---|
[321] | 47 | image = m_device->create_image_data( "specular.png" );
|
---|
| 48 | m_specular = m_device->create_texture( image, sampler );
|
---|
| 49 | delete image;
|
---|
| 50 |
|
---|
[137] | 51 | m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
|
---|
| 52 | m_render_state.depth_test.enabled = true;
|
---|
| 53 | m_render_state.culling.enabled = true;
|
---|
| 54 | m_render_state.blending.enabled = false;
|
---|
| 55 | m_render_state.blending.src_rgb_factor = nv::blending::SRC_ALPHA;
|
---|
| 56 | m_render_state.blending.dst_rgb_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
|
---|
| 57 | m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
|
---|
| 58 | m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | bool application::initialize()
|
---|
| 62 | {
|
---|
| 63 | nv::c_file_system fs;
|
---|
| 64 | nv::stream* mesh_file = fs.open( "mesh.obj" );
|
---|
[238] | 65 |
|
---|
[239] | 66 | nv::obj_loader* loader = new nv::obj_loader();
|
---|
[137] | 67 | loader->load( *mesh_file );
|
---|
[224] | 68 | m_mesh = loader->release_mesh_data();
|
---|
[137] | 69 | delete mesh_file;
|
---|
| 70 | delete loader;
|
---|
| 71 |
|
---|
| 72 | m_program = m_device->create_program( nv::slurp( "obj.vert" ), nv::slurp( "obj.frag" ) );
|
---|
[321] | 73 | m_va = m_context->create_vertex_array( m_mesh, nv::STATIC_DRAW );
|
---|
[224] | 74 |
|
---|
[137] | 75 | return true;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | bool application::run()
|
---|
| 79 | {
|
---|
| 80 | int keypress = 0;
|
---|
| 81 |
|
---|
| 82 | glm::vec3 move( 0, 0, 0 );
|
---|
| 83 |
|
---|
| 84 | while(!keypress)
|
---|
| 85 | {
|
---|
| 86 | m_window->get_context()->clear( m_clear_state );
|
---|
[321] | 87 | m_scene_state.set_model( glm::mat4() );
|
---|
| 88 | m_scene_state.get_camera().set_perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f);
|
---|
| 89 | m_scene_state.get_camera().set_lookat(glm::vec3(6.0f, 8.0f, 6.0f) + move, glm::vec3(0.0f, 4.0f, 0.0f) + move, glm::vec3(0.0, 1.0, 0.0));
|
---|
[137] | 90 |
|
---|
[321] | 91 | m_context->bind( m_diffuse, nv::TEX_DIFFUSE );
|
---|
| 92 | m_context->bind( m_specular, nv::TEX_SPECULAR );
|
---|
| 93 | m_device->set_uniform( m_program, "light_position", glm::vec3(12.0, 12.0, 0) );
|
---|
| 94 | m_device->set_uniform( m_program, "light_diffuse", glm::vec4(0.7,0.7,0.7,1.0) );
|
---|
| 95 | m_device->set_uniform( m_program, "light_specular", glm::vec4(1.0,1.0,1.0,1.0) );
|
---|
| 96 | m_device->set_uniform( m_program, "custom_color_1", glm::vec3(1.0,0.0,0.0) );
|
---|
| 97 | m_device->set_uniform( m_program, "custom_color_2", glm::vec3(0.0,0.0,1.0) );
|
---|
| 98 | m_context->draw( nv::TRIANGLES, m_render_state, m_scene_state, m_program, m_va, m_mesh->get_count() );
|
---|
[137] | 99 | m_window->swap_buffers();
|
---|
| 100 |
|
---|
| 101 | nv::io_event event;
|
---|
| 102 | while(m_window->poll_event(event))
|
---|
| 103 | {
|
---|
| 104 | switch (event.type)
|
---|
| 105 | {
|
---|
| 106 | case nv::EV_QUIT:
|
---|
| 107 | keypress = 1;
|
---|
| 108 | break;
|
---|
| 109 | case nv::EV_KEY:
|
---|
| 110 | if (event.key.pressed)
|
---|
| 111 | {
|
---|
| 112 | switch (event.key.code)
|
---|
| 113 | {
|
---|
| 114 | case nv::KEY_ESCAPE : keypress = 1; break;
|
---|
| 115 | case nv::KEY_LEFT : move.x = move.x - 1.0f; break;
|
---|
| 116 | case nv::KEY_RIGHT : move.x = move.x + 1.0f; break;
|
---|
| 117 | case nv::KEY_UP : move.z = move.z - 1.0f; break;
|
---|
| 118 | case nv::KEY_DOWN : move.z = move.z + 1.0f; break;
|
---|
| 119 | default: break;
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | break;
|
---|
| 123 | default: break;
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | return true;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | application::~application()
|
---|
| 131 | {
|
---|
[238] | 132 | delete m_mesh;
|
---|
[321] | 133 | m_device->release( m_program );
|
---|
| 134 | m_device->release( m_diffuse );
|
---|
| 135 | m_device->release( m_specular );
|
---|
[137] | 136 | delete m_window;
|
---|
| 137 | delete m_device;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 |
|
---|
| 141 | int main(int, char* [])
|
---|
| 142 | {
|
---|
| 143 | nv::logger log(nv::LOG_TRACE);
|
---|
| 144 | log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
|
---|
| 145 | log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
|
---|
| 146 |
|
---|
| 147 | NV_LOG( nv::LOG_NOTICE, "Logging started" );
|
---|
| 148 | application app;
|
---|
| 149 | if ( app.initialize() )
|
---|
| 150 | {
|
---|
| 151 | app.run();
|
---|
| 152 | }
|
---|
| 153 | NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
|
---|
| 154 |
|
---|
| 155 | return 0;
|
---|
| 156 | }
|
---|
| 157 |
|
---|