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