audio occlusion
[carveJwlIkooP6JGAAIwe30JlM.git] / main.c
1 /*
2 * Copyright (C) Mount0 Software, Harry Godden - All Rights Reserved
3 */
4
5 #include "common.h"
6
7 /* Resources */
8 vg_tex2d tex_norwey = { .path = "textures/norway_foliage.qoi" };
9 vg_tex2d tex_grid = { .path = "textures/grid.qoi" };
10 vg_tex2d tex_sky = { .path = "textures/sky.qoi" };
11 vg_tex2d tex_gradients = { .path = "textures/gradients.qoi",
12 .flags = VG_TEXTURE_CLAMP };
13 vg_tex2d tex_cement = { .path = "textures/cement512.qoi" };
14 vg_tex2d tex_water = { .path = "textures/water.qoi" };
15
16 /* Convars */
17 static int debugview = 0;
18 static int sv_debugcam = 0;
19 static int lightedit = 0;
20 static int sv_scene = 0;
21 static int cl_ui = 1;
22
23 /* Components */
24 //#define SR_NETWORKED
25
26 /* uncomment this to run the game without any graphics being drawn */
27 //#define SR_NETWORK_TEST
28
29 #include "steam.h"
30 #include "network.h"
31
32 #include "model.h"
33 //#include "road.h"
34 #include "scene.h"
35 //#include "ik.h"
36 #include "audio.h"
37 //#include "terrain.h"
38 //#include "character.h"
39 #include "ragdoll.h"
40 #include "rigidbody.h"
41 #include "render.h"
42 #include "world.h"
43 #include "player.h"
44
45 #include "shaders/blit.h"
46 #include "shaders/standard.h"
47 #include "shaders/unlit.h"
48
49 #include "physics_test.h"
50 #include "anim_test.h"
51
52 #include "gate.h"
53 #include "water.h"
54
55 void vg_register(void)
56 {
57 shader_blit_register();
58 shader_standard_register();
59 shader_vblend_register();
60 shader_unlit_register();
61
62 world_register();
63 character_register();
64 water_register();
65 gate_register();
66 }
67
68 static void init_other(void)
69 {
70 player_init();
71 render_init();
72 gate_init();
73 world_init();
74 character_init();
75 audio_init();
76 }
77
78 vg_tex2d *texture_list[] =
79 {
80 &tex_norwey,
81 &tex_gradients,
82 &tex_grid,
83 &tex_sky,
84 &tex_cement,
85 &tex_water,
86 &tex_water_surf
87 };
88
89 int main( int argc, char *argv[] )
90 {
91 vg_init( argc, argv, "Voyager Game Engine" );
92 }
93
94 static int playermodel( int argc, char const *argv[] )
95 {
96 if( argc < 1 ) return 0;
97
98 glmesh old_mesh = player.mdl.mesh;
99
100 if( character_load( &player.mdl, argv[0] ) )
101 mesh_free( &old_mesh );
102
103 return 1;
104 }
105
106 void vg_start(void)
107 {
108 steam_init();
109
110 vg_convar_push( (struct vg_convar){
111 .name = "fc",
112 .data = &freecam,
113 .data_type = k_convar_dtype_i32,
114 .opt_i32 = { .min=0, .max=1, .clamp=1 },
115 .persistent = 1
116 });
117
118 vg_convar_push( (struct vg_convar){
119 .name = "grid",
120 .data = &walk_grid_iterations,
121 .data_type = k_convar_dtype_i32,
122 .opt_i32 = { .min=0, .max=1, .clamp=0 },
123 .persistent = 1
124 });
125
126 vg_convar_push( (struct vg_convar){
127 .name = "fcs",
128 .data = &fc_speed,
129 .data_type = k_convar_dtype_f32,
130 .opt_f32 = { .clamp = 0 },
131 .persistent = 1
132 });
133
134 vg_convar_push( (struct vg_convar){
135 .name = "ledit",
136 .data = &lightedit,
137 .data_type = k_convar_dtype_i32,
138 .opt_i32 = { .min=0, .max=1, .clamp=1 },
139 .persistent = 1
140 });
141
142 vg_convar_push( (struct vg_convar){
143 .name = "cl_ui",
144 .data = &cl_ui,
145 .data_type = k_convar_dtype_i32,
146 .opt_i32 = { .min=0, .max=1, .clamp=1 },
147 .persistent = 1
148 });
149
150 vg_convar_push( (struct vg_convar){
151 .name = "walk_speed",
152 .data = &k_walkspeed,
153 .data_type = k_convar_dtype_f32,
154 .opt_f32 = { .clamp = 0 },
155 .persistent = 1
156 });
157
158 vg_convar_push( (struct vg_convar){
159 .name = "run_speed",
160 .data = &k_runspeed,
161 .data_type = k_convar_dtype_f32,
162 .opt_f32 = { .clamp = 0 },
163 .persistent = 1
164 });
165
166 vg_convar_push( (struct vg_convar){
167 .name = "walk_accel",
168 .data = &k_walk_accel,
169 .data_type = k_convar_dtype_f32,
170 .opt_f32 = { .clamp = 0 },
171 .persistent = 1
172 });
173
174 vg_convar_push( (struct vg_convar){
175 .name = "rd_floaty",
176 .data = &k_ragdoll_floatyiness,
177 .data_type = k_convar_dtype_f32,
178 .opt_f32 = { .clamp = 0 },
179 .persistent = 1
180 });
181
182 vg_convar_push( (struct vg_convar){
183 .name = "rd_floatd",
184 .data = &k_ragdoll_floatydrag,
185 .data_type = k_convar_dtype_f32,
186 .opt_f32 = { .clamp = 0 },
187 .persistent = 1
188 });
189
190 vg_convar_push( (struct vg_convar){
191 .name = "dt",
192 .data = &ktimestep,
193 .data_type = k_convar_dtype_f32,
194 .opt_f32 = { .clamp = 0 },
195 .persistent = 0
196 });
197
198 vg_convar_push( (struct vg_convar){
199 .name = "debugcam",
200 .data = &sv_debugcam,
201 .data_type = k_convar_dtype_i32,
202 .opt_i32 = { .min=0, .max=1, .clamp=0 },
203 .persistent = 1
204 });
205
206 vg_convar_push( (struct vg_convar){
207 .name = "debugview",
208 .data = &debugview,
209 .data_type = k_convar_dtype_i32,
210 .opt_i32 = { .min=0, .max=1, .clamp=0 },
211 .persistent = 1
212 });
213
214 vg_function_push( (struct vg_cmd){
215 .name = "reset",
216 .function = reset_player
217 });
218
219 vg_tex2d_init( texture_list, vg_list_size( texture_list ) );
220
221 init_other();
222
223 /*
224 * If we're in physics test mode we dont need to load anything else, this
225 * parameter is dev only. TODO: dev only cvars that don't ship with the game
226 * when building in release mode.
227 */
228
229 if( sv_scene == 0 )
230 {
231 character_load( &player.mdl, "ch_new" );
232 character_init_ragdoll( &player.mdl );
233
234 world_load();
235
236 reset_player( 1, (const char *[]){ "start" } );
237 rb_init( &player.rb );
238
239 network_init();
240 }
241 else if( sv_scene == 1 )
242 {
243 physics_test_start();
244 }
245 else if( sv_scene == 2 )
246 {
247 anim_test_start();
248 }
249 }
250
251 void vg_free(void)
252 {
253 network_end();
254 vg_tex2d_free( texture_list, vg_list_size(texture_list) );
255 /* TODO: THE REST OF THE GOD DAMN FREEING STUFF */
256 steam_end();
257 }
258
259 void vg_update(void)
260 {
261 steam_update();
262
263 if( sv_scene == 0 )
264 {
265 network_update();
266 player_update();
267 world_update();
268 //traffic_visualize( world.traffic, world.traffic_count );
269 //
270 /* TEMP */
271 if( glfwGetKey( vg_window, GLFW_KEY_J ))
272 {
273 v3_copy( player.camera_pos, world.mr_ball.co );
274 }
275 }
276 else if( sv_scene == 1 )
277 {
278 physics_test_update();
279 }
280 else if( sv_scene == 2 )
281 {
282 anim_test_update();
283 }
284 }
285
286 static void vg_framebuffer_resize( int w, int h )
287 {
288 render_fb_resize();
289 gate_fb_resize();
290 water_fb_resize();
291 }
292
293 static void draw_origin_axis(void)
294 {
295 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 1.0f, 0.0f, 0.0f }, 0xffff0000 );
296 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 1.0f, 0.0f }, 0xff00ff00 );
297 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 0.0f, 1.0f }, 0xff0000ff );
298 }
299
300 static void render_main_game(void)
301 {
302 float speed = freecam? 0.0f: v3_length( player.rb.v );
303 v3f shake = { vg_randf()-0.5f, vg_randf()-0.5f, vg_randf()-0.5f };
304 v3_muls( shake, speed*0.01f, shake );
305
306 m4x4f world_4x4;
307 m4x3_expand( player.camera_inverse, world_4x4 );
308
309 gpipeline.fov = freecam? 60.0f: 125.0f; /* 120 */
310 m4x4_projection( vg_pv, gpipeline.fov,
311 (float)vg_window_x / (float)vg_window_y,
312 0.02f, 2100.0f );
313
314 m4x4_mul( vg_pv, world_4x4, vg_pv );
315
316 glEnable( GL_DEPTH_TEST );
317
318 /*
319 * Draw world
320 */
321
322 int draw_solid = player.is_dead | freecam;
323
324 render_world( vg_pv, player.camera );
325 if( draw_solid )
326 draw_player();
327 render_water_texture( player.camera );
328
329 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
330 render_water_surface( vg_pv, player.camera );
331
332 vg_tex2d_bind( &tex_water, 1 ); /*TODO: ?*/
333 render_world_gates( vg_pv, player.camera );
334
335 /* Copy the RGB of what we have into the background buffer */
336 glBindFramebuffer( GL_READ_FRAMEBUFFER, 0 );
337 glBindFramebuffer( GL_DRAW_FRAMEBUFFER, gpipeline.fb_background );
338 glBlitFramebuffer( 0,0, vg_window_x, vg_window_y,
339 0,0, vg_window_x, vg_window_y,
340 GL_COLOR_BUFFER_BIT,
341 GL_LINEAR );
342
343 /* Clear out the colour buffer, but keep depth */
344 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
345 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
346
347 if( !player.is_dead )
348 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
349 else
350 glClear( GL_COLOR_BUFFER_BIT );
351
352 if( !draw_solid )
353 {
354 m4x4_projection( vg_pv, gpipeline.fov,
355 (float)vg_window_x / (float)vg_window_y,
356 0.01f, 600.0f );
357 m4x4_mul( vg_pv, world_4x4, vg_pv );
358 draw_player();
359 }
360
361 /* Draw back in the background
362 *
363 * TODO: need to disable alpha write in the terrain shader so this works
364 * again.
365 */
366 glEnable(GL_BLEND);
367 glDisable(GL_DEPTH_TEST);
368 glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
369 glBlendEquation(GL_FUNC_ADD);
370
371 shader_blit_use();
372 shader_blit_uTexMain( 0 );
373 glActiveTexture(GL_TEXTURE0);
374 glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_background );
375
376 render_fsquad();
377 glDisable(GL_BLEND);
378
379 /* Other shite */
380 glDisable( GL_DEPTH_TEST );
381 vg_lines_drawall( (float *)vg_pv );
382 glViewport( 0,0, vg_window_x, vg_window_y );
383 }
384
385 void vg_render(void)
386 {
387 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
388 glViewport( 0,0, vg_window_x, vg_window_y );
389
390 glDisable( GL_DEPTH_TEST );
391 glClearColor( 0.11f, 0.35f, 0.37f, 1.0f );
392 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
393
394 #ifndef SR_NETWORK_TEST
395 draw_origin_axis();
396
397 if( sv_scene == 0 )
398 {
399 render_main_game();
400 }
401 else if( sv_scene == 1 )
402 {
403 physics_test_render();
404 }
405 else if( sv_scene == 2 )
406 {
407 anim_test_render( &tex_characters );
408 }
409 #endif
410 }
411
412 static void run_light_widget( struct light_widget *lw )
413 {
414 struct ui_checkbox c1 = { .data=&lw->enabled };
415
416 ui_checkbox( &ui_global_ctx, &c1 );
417
418 if( lw->enabled )
419 {
420 struct ui_slider_vector
421 colour = { .min=0.0f, .max=2.0f, .len=3, .data=lw->colour },
422 dir = { .min=-VG_PIf, .max=VG_PIf, .len=2, .data=lw->dir };
423
424 ui_slider_vector( &ui_global_ctx, &colour );
425 ui_global_ctx.cursor[1] += 4;
426 ui_slider_vector( &ui_global_ctx, &dir );
427 }
428 }
429
430 static void run_debug_info(void)
431 {
432 char buf[40];
433
434 snprintf( buf, 40, "%.2fm/s", v3_length( player.rb.v ) );
435 gui_text( (ui_px [2]){ 0, 0 }, buf, 1, k_text_align_left );
436
437 snprintf( buf, 40, "%.2f %.2f %.2f m/s",
438 player.a[0], player.a[1], player.a[2] );
439 gui_text( (ui_px [2]){ 0, 20 }, buf, 1, k_text_align_left );
440
441 snprintf( buf, 40, "pos %.2f %.2f %.2f",
442 player.rb.co[0], player.rb.co[1], player.rb.co[2] );
443 gui_text( (ui_px [2]){ 0, 40 }, buf, 1, k_text_align_left );
444
445 if( vg_gamepad_ready )
446 {
447 for( int i=0; i<6; i++ )
448 {
449 snprintf( buf, 40, "%.2f", vg_gamepad.axes[i] );
450 gui_text( (ui_px [2]){ 0, (i+3)*20 }, buf, 1, k_text_align_left );
451 }
452 }
453 else
454 {
455 gui_text( (ui_px [2]){ 0, 60 },
456 "Gamepad not ready", 1, k_text_align_left );
457 }
458 }
459
460 void vg_ui(void)
461 {
462 if( lightedit )
463 {
464 ui_global_ctx.cursor[0] = 10;
465 ui_global_ctx.cursor[1] = 10;
466 ui_global_ctx.cursor[2] = 200;
467 ui_global_ctx.cursor[3] = 20;
468
469 struct ub_world_lighting *wl = &gpipeline.ub_world_lighting;
470 struct ui_slider_vector
471 s5 = { .min=0.0f, .max=2.0f, .len=3, .data=wl->g_ambient_colour };
472
473 struct ui_slider
474 s8 = { .min=0.0f, .max=2.0f, .data = &gpipeline.shadow_spread },
475 s9 = { .min=0.0f, .max=25.0f, .data = &gpipeline.shadow_length };
476
477 for( int i=0; i<3; i++ )
478 run_light_widget( &gpipeline.widgets[i] );
479
480 gui_text( ui_global_ctx.cursor, "Ambient", 1, 0 );
481 ui_global_ctx.cursor[1] += 16;
482 ui_slider_vector( &ui_global_ctx, &s5 );
483
484 gui_text( ui_global_ctx.cursor, "Shadows", 1, 0 );
485 ui_global_ctx.cursor[1] += 16;
486 ui_slider( &ui_global_ctx, &s8 );
487 ui_slider( &ui_global_ctx, &s9 );
488
489 gui_text( ui_global_ctx.cursor, "Misc", 1, 0 );
490 ui_global_ctx.cursor[1] += 16;
491 struct ui_checkbox c1 = {.data = &wl->g_light_preview};
492 ui_checkbox( &ui_global_ctx, &c1 );
493
494 render_update_lighting_ub();
495 }
496
497 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
498 if( cl_ui )
499 {
500 render_world_routes_ui();
501 }
502 //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
503
504 audio_debug_soundscapes();
505
506 #if 0
507 static double last_b_press = 0.0;
508
509 double localtime = vg_time - last_b_press;
510
511 world_routes_ui_updatetime( 0, localtime );
512 world_routes_ui_draw( 0, (v4f){ 1.0f,0.0f,1.0f,1.0f}, 9.0f );
513
514 if( glfwGetKey(vg_window,GLFW_KEY_B) )
515 world_routes_ui_notch( 0, localtime );
516
517 if( vg_time-last_b_press > 1.0 )
518 if( glfwGetKey(vg_window,GLFW_KEY_N) )
519 {
520 last_b_press = vg_time;
521 world_routes_ui_newseg( 0 );
522 }
523
524 static double last_m_press;
525 if( vg_time-last_m_press > 1.0 )
526 if( glfwGetKey( vg_window, GLFW_KEY_M) )
527 {
528 last_m_press = vg_time;
529
530 vg_info( "start: %u\n",world.routes.routes[0].ui.segment_count );
531 for( int i=0; i<world.routes.routes[0].ui.segment_count; i++ )
532 world_routes_ui_popfirst(0);
533
534 vg_info( "new: %u\n",world.routes.routes[0].ui.segment_count );
535 }
536 #endif
537 }