1 | #include <nv/common.hh>
|
---|
2 | #include <iomanip>
|
---|
3 | #include <nv/gfx/keyframed_mesh.hh>
|
---|
4 | #include <nv/interface/vertex_buffer.hh>
|
---|
5 | #include <nv/gl/gl_device.hh>
|
---|
6 | #include <nv/gfx/image.hh>
|
---|
7 | #include <nv/interface/context.hh>
|
---|
8 | #include <nv/interface/window.hh>
|
---|
9 | #include <nv/interface/program.hh>
|
---|
10 | #include <nv/interface/texture2d.hh>
|
---|
11 | #include <nv/interface/mesh_loader.hh>
|
---|
12 | #include <nv/io/c_file_system.hh>
|
---|
13 | #include <nv/formats/md5_loader.hh>
|
---|
14 | #include <nv/profiler.hh>
|
---|
15 | #include <nv/logging.hh>
|
---|
16 | #include <nv/logger.hh>
|
---|
17 | #include <nv/math.hh>
|
---|
18 | #include <nv/time.hh>
|
---|
19 | #include <nv/string.hh>
|
---|
20 | #include <nv/interface/mesh.hh>
|
---|
21 | #include <nv/gfx/skeletal_mesh.hh>
|
---|
22 | #include <glm/gtx/rotate_vector.hpp>
|
---|
23 | #include <glm/gtc/matrix_access.hpp>
|
---|
24 | #include <glm/gtx/matrix_interpolation.hpp>
|
---|
25 |
|
---|
26 | class mesh_part
|
---|
27 | {
|
---|
28 | public:
|
---|
29 | mesh_part( const std::string& path, nv::program* program, nv::window* window )
|
---|
30 | : m_mesh( nullptr ), m_program( program ), m_loader( nullptr ), m_animation( nullptr ), m_window( window )
|
---|
31 | {
|
---|
32 |
|
---|
33 | NV_PROFILE("mesh_construct");
|
---|
34 | {
|
---|
35 | NV_PROFILE("loader->load");
|
---|
36 | nv::c_file_system fs;
|
---|
37 | nv::stream* mesh_file = fs.open( path.c_str() );
|
---|
38 | m_loader = new nv::md5_loader();
|
---|
39 | m_loader->load( *mesh_file );
|
---|
40 | delete mesh_file;
|
---|
41 | }
|
---|
42 |
|
---|
43 | {
|
---|
44 | NV_PROFILE("create_mesh");
|
---|
45 | m_mesh = new nv::skeletal_mesh( window->get_context(), m_loader );
|
---|
46 | }
|
---|
47 |
|
---|
48 | }
|
---|
49 |
|
---|
50 | void load_animation( const std::string& path )
|
---|
51 | {
|
---|
52 | delete m_animation;
|
---|
53 | m_animation = nullptr;
|
---|
54 | NV_PROFILE("load_animation");
|
---|
55 | nv::c_file_system fs;
|
---|
56 | nv::stream* anim_file = fs.open( path.c_str() );
|
---|
57 |
|
---|
58 | if ( anim_file != nullptr )
|
---|
59 | {
|
---|
60 | m_animation = new nv::md5_animation();
|
---|
61 | if ( !m_animation->load_animation(*anim_file) )
|
---|
62 | {
|
---|
63 | delete m_animation;
|
---|
64 | m_animation = nullptr;
|
---|
65 | }
|
---|
66 | m_mesh->setup_animation( m_animation );
|
---|
67 | delete anim_file;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | void update( nv::uint32 ms )
|
---|
72 | {
|
---|
73 | m_mesh->update( ms );
|
---|
74 | }
|
---|
75 |
|
---|
76 | void draw( nv::context* context, nv::render_state& rstate, const glm::mat4& m, const glm::mat4& v, const glm::mat4& p )
|
---|
77 | {
|
---|
78 | NV_PROFILE( "mesh-draw" );
|
---|
79 | glm::mat4 mv = v * m;
|
---|
80 | m_program->set_opt_uniform( "nv_m_model", m );
|
---|
81 | m_program->set_opt_uniform( "nv_m_modelview", mv );
|
---|
82 | m_program->set_opt_uniform( "nv_m_view_inv", glm::inverse( v ) );
|
---|
83 | m_program->set_opt_uniform( "nv_m_normal", glm::transpose(glm::inverse(glm::mat3(mv))) );
|
---|
84 | m_program->set_uniform( "matrix_mvp", p * mv );
|
---|
85 | context->draw( rstate, m_program, m_mesh );
|
---|
86 | }
|
---|
87 |
|
---|
88 | ~mesh_part()
|
---|
89 | {
|
---|
90 | delete m_mesh;
|
---|
91 | }
|
---|
92 |
|
---|
93 | private:
|
---|
94 | nv::skeletal_mesh* m_mesh;
|
---|
95 | nv::program* m_program;
|
---|
96 | nv::md5_loader* m_loader;
|
---|
97 | nv::md5_animation* m_animation;
|
---|
98 | nv::window* m_window;
|
---|
99 | };
|
---|
100 |
|
---|
101 | class application
|
---|
102 | {
|
---|
103 | public:
|
---|
104 | application();
|
---|
105 | bool initialize();
|
---|
106 | bool run();
|
---|
107 | ~application();
|
---|
108 | protected:
|
---|
109 | nv::device* m_device;
|
---|
110 | nv::window* m_window;
|
---|
111 | nv::texture2d* m_diffuse;
|
---|
112 | nv::texture2d* m_specular;
|
---|
113 | nv::texture2d* m_normal;
|
---|
114 | nv::clear_state m_clear_state;
|
---|
115 | nv::render_state m_render_state;
|
---|
116 |
|
---|
117 | mesh_part* m_mesh;
|
---|
118 | nv::program* m_program;
|
---|
119 | };
|
---|
120 |
|
---|
121 | application::application()
|
---|
122 | {
|
---|
123 | NV_PROFILE( "app_construct" );
|
---|
124 | m_device = new nv::gl_device();
|
---|
125 | m_window = m_device->create_window( 800, 600, false );
|
---|
126 |
|
---|
127 | nv::sampler sampler( nv::sampler::LINEAR, nv::sampler::REPEAT );
|
---|
128 | nv::image_data* data = m_device->create_image_data( "data/qshambler_d.png" );
|
---|
129 | m_diffuse = m_device->create_texture2d( data->get_size(), nv::RGBA, nv::UBYTE, sampler, (void*)data->get_data() );
|
---|
130 | delete data;
|
---|
131 | data = m_device->create_image_data( "data/qshambler_s.png" );
|
---|
132 | m_specular = m_device->create_texture2d( data->get_size(), nv::RGBA, nv::UBYTE, sampler, (void*)data->get_data() );
|
---|
133 | delete data;
|
---|
134 |
|
---|
135 | data = m_device->create_image_data( "data/qshambler_local.png" );
|
---|
136 | m_normal = m_device->create_texture2d( data->get_size(), nv::RGBA, nv::UBYTE, sampler, (void*)data->get_data() );
|
---|
137 | delete data;
|
---|
138 |
|
---|
139 | m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
|
---|
140 | m_render_state.depth_test.enabled = true;
|
---|
141 | m_render_state.culling.enabled = true;
|
---|
142 | m_render_state.culling.order = nv::culling::CCW;
|
---|
143 | m_render_state.blending.enabled = false;
|
---|
144 | m_render_state.blending.src_rgb_factor = nv::blending::SRC_ALPHA;
|
---|
145 | m_render_state.blending.dst_rgb_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
|
---|
146 | m_render_state.blending.src_alpha_factor = nv::blending::SRC_ALPHA;
|
---|
147 | m_render_state.blending.dst_alpha_factor = nv::blending::ONE_MINUS_SRC_ALPHA;
|
---|
148 | }
|
---|
149 |
|
---|
150 | bool application::initialize()
|
---|
151 | {
|
---|
152 | NV_PROFILE( "app_initialize" );
|
---|
153 | m_program = m_device->create_program( nv::slurp( "md5.vert" ), nv::slurp( "md5.frag" ) );
|
---|
154 | m_mesh = new mesh_part( "data/qshambler.md5mesh", m_program, m_window );
|
---|
155 | m_mesh->load_animation( "data/idle02.md5anim" );
|
---|
156 | return true;
|
---|
157 | }
|
---|
158 |
|
---|
159 | bool application::run()
|
---|
160 | {
|
---|
161 | nv::profiler::pointer()->log_report();
|
---|
162 | NV_PROFILE( "app_run" );
|
---|
163 | int keypress = 0;
|
---|
164 |
|
---|
165 | glm::vec3 move( 0, 50.f, 0 );
|
---|
166 |
|
---|
167 | nv::uint32 ticks = m_device->get_ticks();
|
---|
168 | nv::uint32 last_ticks;
|
---|
169 | nv::fps_counter_class< nv::system_us_timer > fps_counter;
|
---|
170 |
|
---|
171 | //m_mesh->setup_animation( 0, m_mesh->get_max_frames(), 2, true );
|
---|
172 |
|
---|
173 | while(!keypress)
|
---|
174 | {
|
---|
175 | last_ticks = ticks;
|
---|
176 | ticks = m_device->get_ticks();
|
---|
177 | nv::uint32 elapsed = ticks - last_ticks;
|
---|
178 | m_mesh->update( elapsed );
|
---|
179 | {
|
---|
180 | NV_PROFILE( "clear" );
|
---|
181 | m_window->get_context()->clear( m_clear_state );
|
---|
182 | }
|
---|
183 |
|
---|
184 | glm::mat4 view;
|
---|
185 | glm::mat4 projection;
|
---|
186 | {
|
---|
187 | NV_PROFILE( "update_sh" );
|
---|
188 |
|
---|
189 | glm::vec3 source( 150.0f, 0.0f, 0.0f );
|
---|
190 | glm::vec3 eye = glm::rotate( source, (ticks / 20.f), glm::vec3( 0.0,1.0,0.0 ) );
|
---|
191 |
|
---|
192 | view = glm::lookAt(eye + move, glm::vec3(0.0f, 0.0f, 0.0f) + move, glm::vec3(0.0, 1.0, 0.0));
|
---|
193 | projection = glm::perspective(60.0f, 1.0f*800.0f/600.0f, 0.1f, 1000.0f);
|
---|
194 |
|
---|
195 | m_diffuse ->bind( 0 );
|
---|
196 | m_specular->bind( 1 );
|
---|
197 | m_normal ->bind( 2 );
|
---|
198 | m_program->set_opt_uniform( "nv_m_projection", projection );
|
---|
199 | m_program->set_uniform( "light_position", glm::vec3(180.0, 180.0, 0) );
|
---|
200 | m_program->set_uniform( "light_diffuse", glm::vec4(0.7,0.7,0.7,1.0) );
|
---|
201 | m_program->set_uniform( "light_specular", glm::vec4(1.0,1.0,1.0,1.0) );
|
---|
202 | m_program->set_uniform( "diffuse", 0 );
|
---|
203 | m_program->set_uniform( "specular", 1 );
|
---|
204 | m_program->set_uniform( "normalmap", 2 );
|
---|
205 | }
|
---|
206 |
|
---|
207 | {
|
---|
208 | NV_PROFILE( "draw" );
|
---|
209 | glm::mat4 model =
|
---|
210 | glm::mat4(1.f,0.f,0.f,0.f,
|
---|
211 | 0.f,0.f,1.f,0.f,
|
---|
212 | 0.f,1.f,0.f,0.f,
|
---|
213 | 0.f,0.f,0.f,1.f);
|
---|
214 |
|
---|
215 | m_mesh->draw( m_window->get_context(), m_render_state, model, view, projection );
|
---|
216 | }
|
---|
217 |
|
---|
218 | {
|
---|
219 | NV_PROFILE( "swap" );
|
---|
220 | m_window->swap_buffers();
|
---|
221 | }
|
---|
222 |
|
---|
223 | {
|
---|
224 | NV_PROFILE( "pollevent" );
|
---|
225 | nv::io_event event;
|
---|
226 | while(m_window->poll_event(event))
|
---|
227 | {
|
---|
228 | switch (event.type)
|
---|
229 | {
|
---|
230 | case nv::EV_QUIT:
|
---|
231 | keypress = 1;
|
---|
232 | break;
|
---|
233 | case nv::EV_KEY:
|
---|
234 | if (event.key.pressed)
|
---|
235 | {
|
---|
236 | switch (event.key.code)
|
---|
237 | {
|
---|
238 | case nv::KEY_ESCAPE : keypress = 1; break;
|
---|
239 | case nv::KEY_F1 : nv::profiler::pointer()->log_report(); break;
|
---|
240 | default: break;
|
---|
241 | }
|
---|
242 | }
|
---|
243 | break;
|
---|
244 | default: break;
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|
248 | fps_counter.tick();
|
---|
249 | }
|
---|
250 | return true;
|
---|
251 | }
|
---|
252 |
|
---|
253 | application::~application()
|
---|
254 | {
|
---|
255 | delete m_program;
|
---|
256 | delete m_mesh;
|
---|
257 | delete m_diffuse;
|
---|
258 | delete m_window;
|
---|
259 | delete m_device;
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | int main(int, char* [])
|
---|
264 | {
|
---|
265 | nv::logger log(nv::LOG_TRACE);
|
---|
266 | log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
|
---|
267 | log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
|
---|
268 |
|
---|
269 | NV_LOG( nv::LOG_NOTICE, "Logging started" );
|
---|
270 | application app;
|
---|
271 | if ( app.initialize() )
|
---|
272 | {
|
---|
273 | app.run();
|
---|
274 | }
|
---|
275 | NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
|
---|
276 |
|
---|
277 | return 0;
|
---|
278 | }
|
---|
279 |
|
---|