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