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