whole
[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 = "dt",
176 .data = &ktimestep,
177 .data_type = k_convar_dtype_f32,
178 .opt_f32 = { .clamp = 0 },
179 .persistent = 0
180 });
181
182 vg_convar_push( (struct vg_convar){
183 .name = "debugcam",
184 .data = &sv_debugcam,
185 .data_type = k_convar_dtype_i32,
186 .opt_i32 = { .min=0, .max=1, .clamp=0 },
187 .persistent = 1
188 });
189
190 vg_convar_push( (struct vg_convar){
191 .name = "debugview",
192 .data = &debugview,
193 .data_type = k_convar_dtype_i32,
194 .opt_i32 = { .min=0, .max=1, .clamp=0 },
195 .persistent = 1
196 });
197
198 vg_function_push( (struct vg_cmd){
199 .name = "reset",
200 .function = reset_player
201 });
202
203 vg_tex2d_init( texture_list, vg_list_size( texture_list ) );
204
205 init_other();
206
207 /*
208 * If we're in physics test mode we dont need to load anything else, this
209 * parameter is dev only. TODO: dev only cvars that don't ship with the game
210 * when building in release mode.
211 */
212
213 if( sv_scene == 0 )
214 {
215 character_load( &player.mdl, "ch_new" );
216 character_init_ragdoll( &player.mdl );
217
218 world_load();
219
220 reset_player( 1, (const char *[]){ "start" } );
221 rb_init( &player.rb );
222
223 network_init();
224 }
225 else if( sv_scene == 1 )
226 {
227 physics_test_start();
228 }
229 else if( sv_scene == 2 )
230 {
231 anim_test_start();
232 }
233 }
234
235 void vg_free(void)
236 {
237 network_end();
238 vg_tex2d_free( texture_list, vg_list_size(texture_list) );
239 /* TODO: THE REST OF THE GOD DAMN FREEING STUFF */
240 steam_end();
241 }
242
243 void vg_update(void)
244 {
245 steam_update();
246
247 if( sv_scene == 0 )
248 {
249 network_update();
250 player_update();
251 world_update();
252 //traffic_visualize( world.traffic, world.traffic_count );
253 //
254 /* TEMP */
255 if( glfwGetKey( vg_window, GLFW_KEY_J ))
256 {
257 v3_copy( player.camera_pos, world.mr_ball.co );
258 }
259 }
260 else if( sv_scene == 1 )
261 {
262 physics_test_update();
263 }
264 else if( sv_scene == 2 )
265 {
266 anim_test_update();
267 }
268 }
269
270 static void vg_framebuffer_resize( int w, int h )
271 {
272 render_fb_resize();
273 gate_fb_resize();
274 water_fb_resize();
275 }
276
277 static void draw_origin_axis(void)
278 {
279 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 1.0f, 0.0f, 0.0f }, 0xffff0000 );
280 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 1.0f, 0.0f }, 0xff00ff00 );
281 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 0.0f, 1.0f }, 0xff0000ff );
282 }
283
284 static void render_main_game(void)
285 {
286 float speed = freecam? 0.0f: v3_length( player.rb.v );
287 v3f shake = { vg_randf()-0.5f, vg_randf()-0.5f, vg_randf()-0.5f };
288 v3_muls( shake, speed*0.01f, shake );
289
290 m4x4f world_4x4;
291 m4x3_expand( player.camera_inverse, world_4x4 );
292
293 gpipeline.fov = freecam? 60.0f: 125.0f; /* 120 */
294 m4x4_projection( vg_pv, gpipeline.fov,
295 (float)vg_window_x / (float)vg_window_y,
296 0.02f, 2100.0f );
297
298 m4x4_mul( vg_pv, world_4x4, vg_pv );
299
300 glEnable( GL_DEPTH_TEST );
301
302 /*
303 * Draw world
304 */
305
306 int draw_solid = player.is_dead | freecam;
307
308 render_world( vg_pv, player.camera );
309 if( draw_solid )
310 draw_player();
311 render_water_texture( player.camera );
312
313 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
314 render_water_surface( vg_pv, player.camera );
315
316 vg_tex2d_bind( &tex_water, 1 ); /*TODO: ?*/
317 render_world_gates( vg_pv, player.camera );
318
319 /* Copy the RGB of what we have into the background buffer */
320 glBindFramebuffer( GL_READ_FRAMEBUFFER, 0 );
321 glBindFramebuffer( GL_DRAW_FRAMEBUFFER, gpipeline.fb_background );
322 glBlitFramebuffer( 0,0, vg_window_x, vg_window_y,
323 0,0, vg_window_x, vg_window_y,
324 GL_COLOR_BUFFER_BIT,
325 GL_LINEAR );
326
327 /* Clear out the colour buffer, but keep depth */
328 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
329 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
330
331 if( !player.is_dead )
332 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
333 else
334 glClear( GL_COLOR_BUFFER_BIT );
335
336 if( !draw_solid )
337 {
338 m4x4_projection( vg_pv, gpipeline.fov,
339 (float)vg_window_x / (float)vg_window_y,
340 0.01f, 600.0f );
341 m4x4_mul( vg_pv, world_4x4, vg_pv );
342 draw_player();
343 }
344
345 /* Draw back in the background
346 *
347 * TODO: need to disable alpha write in the terrain shader so this works
348 * again.
349 */
350 glEnable(GL_BLEND);
351 glDisable(GL_DEPTH_TEST);
352 glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
353 glBlendEquation(GL_FUNC_ADD);
354
355 shader_blit_use();
356 shader_blit_uTexMain( 0 );
357 glActiveTexture(GL_TEXTURE0);
358 glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_background );
359
360 render_fsquad();
361 glDisable(GL_BLEND);
362
363 /* Other shite */
364 glDisable( GL_DEPTH_TEST );
365 vg_lines_drawall( (float *)vg_pv );
366 glViewport( 0,0, vg_window_x, vg_window_y );
367 }
368
369 void vg_render(void)
370 {
371 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
372 glViewport( 0,0, vg_window_x, vg_window_y );
373
374 glDisable( GL_DEPTH_TEST );
375 glClearColor( 0.11f, 0.35f, 0.37f, 1.0f );
376 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
377
378 #ifndef SR_NETWORK_TEST
379 draw_origin_axis();
380
381 if( sv_scene == 0 )
382 {
383 render_main_game();
384 }
385 else if( sv_scene == 1 )
386 {
387 physics_test_render();
388 }
389 else if( sv_scene == 2 )
390 {
391 anim_test_render( &tex_characters );
392 }
393 #endif
394 }
395
396 static void run_light_widget( struct light_widget *lw )
397 {
398 struct ui_checkbox c1 = { .data=&lw->enabled };
399
400 ui_checkbox( &ui_global_ctx, &c1 );
401
402 if( lw->enabled )
403 {
404 struct ui_slider_vector
405 colour = { .min=0.0f, .max=2.0f, .len=3, .data=lw->colour },
406 dir = { .min=-VG_PIf, .max=VG_PIf, .len=2, .data=lw->dir };
407
408 ui_slider_vector( &ui_global_ctx, &colour );
409 ui_global_ctx.cursor[1] += 4;
410 ui_slider_vector( &ui_global_ctx, &dir );
411 }
412 }
413
414 static void run_debug_info(void)
415 {
416 char buf[40];
417
418 snprintf( buf, 40, "%.2fm/s", v3_length( player.rb.v ) );
419 gui_text( (ui_px [2]){ 0, 0 }, buf, 1, k_text_align_left );
420
421 snprintf( buf, 40, "%.2f %.2f %.2f m/s",
422 player.a[0], player.a[1], player.a[2] );
423 gui_text( (ui_px [2]){ 0, 20 }, buf, 1, k_text_align_left );
424
425 snprintf( buf, 40, "pos %.2f %.2f %.2f",
426 player.rb.co[0], player.rb.co[1], player.rb.co[2] );
427 gui_text( (ui_px [2]){ 0, 40 }, buf, 1, k_text_align_left );
428
429 if( vg_gamepad_ready )
430 {
431 for( int i=0; i<6; i++ )
432 {
433 snprintf( buf, 40, "%.2f", vg_gamepad.axes[i] );
434 gui_text( (ui_px [2]){ 0, (i+3)*20 }, buf, 1, k_text_align_left );
435 }
436 }
437 else
438 {
439 gui_text( (ui_px [2]){ 0, 60 },
440 "Gamepad not ready", 1, k_text_align_left );
441 }
442 }
443
444 void vg_ui(void)
445 {
446 if( lightedit )
447 {
448 ui_global_ctx.cursor[0] = 10;
449 ui_global_ctx.cursor[1] = 10;
450 ui_global_ctx.cursor[2] = 200;
451 ui_global_ctx.cursor[3] = 20;
452
453 struct ub_world_lighting *wl = &gpipeline.ub_world_lighting;
454 struct ui_slider_vector
455 s5 = { .min=0.0f, .max=2.0f, .len=3, .data=wl->g_ambient_colour };
456
457 struct ui_slider
458 s8 = { .min=0.0f, .max=2.0f, .data = &gpipeline.shadow_spread },
459 s9 = { .min=0.0f, .max=25.0f, .data = &gpipeline.shadow_length };
460
461 for( int i=0; i<3; i++ )
462 run_light_widget( &gpipeline.widgets[i] );
463
464 gui_text( ui_global_ctx.cursor, "Ambient", 1, 0 );
465 ui_global_ctx.cursor[1] += 16;
466 ui_slider_vector( &ui_global_ctx, &s5 );
467
468 gui_text( ui_global_ctx.cursor, "Shadows", 1, 0 );
469 ui_global_ctx.cursor[1] += 16;
470 ui_slider( &ui_global_ctx, &s8 );
471 ui_slider( &ui_global_ctx, &s9 );
472
473 gui_text( ui_global_ctx.cursor, "Misc", 1, 0 );
474 ui_global_ctx.cursor[1] += 16;
475 struct ui_checkbox c1 = {.data = &wl->g_light_preview};
476 ui_checkbox( &ui_global_ctx, &c1 );
477
478 render_update_lighting_ub();
479 }
480
481 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
482 if( cl_ui )
483 {
484 render_world_routes_ui();
485 }
486 //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
487
488 #if 0
489 static double last_b_press = 0.0;
490
491 double localtime = vg_time - last_b_press;
492
493 world_routes_ui_updatetime( 0, localtime );
494 world_routes_ui_draw( 0, (v4f){ 1.0f,0.0f,1.0f,1.0f}, 9.0f );
495
496 if( glfwGetKey(vg_window,GLFW_KEY_B) )
497 world_routes_ui_notch( 0, localtime );
498
499 if( vg_time-last_b_press > 1.0 )
500 if( glfwGetKey(vg_window,GLFW_KEY_N) )
501 {
502 last_b_press = vg_time;
503 world_routes_ui_newseg( 0 );
504 }
505
506 static double last_m_press;
507 if( vg_time-last_m_press > 1.0 )
508 if( glfwGetKey( vg_window, GLFW_KEY_M) )
509 {
510 last_m_press = vg_time;
511
512 vg_info( "start: %u\n",world.routes.routes[0].ui.segment_count );
513 for( int i=0; i<world.routes.routes[0].ui.segment_count; i++ )
514 world_routes_ui_popfirst(0);
515
516 vg_info( "new: %u\n",world.routes.routes[0].ui.segment_count );
517 }
518 #endif
519 }