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