stuff
[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 render_world( vg_pv, player.camera );
282 render_water_texture( player.camera );
283
284 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
285 render_water_surface( vg_pv, player.camera );
286
287 vg_tex2d_bind( &tex_water, 1 ); /*TODO: ?*/
288 render_world_gates( vg_pv, player.camera );
289
290 /* Copy the RGB of what we have into the background buffer */
291 glBindFramebuffer( GL_READ_FRAMEBUFFER, 0 );
292 glBindFramebuffer( GL_DRAW_FRAMEBUFFER, gpipeline.fb_background );
293 glBlitFramebuffer( 0,0, vg_window_x, vg_window_y,
294 0,0, vg_window_x, vg_window_y,
295 GL_COLOR_BUFFER_BIT,
296 GL_LINEAR );
297
298 /* Clear out the colour buffer, but keep depth */
299 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
300 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
301
302 if( !player.is_dead )
303 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
304 else
305 glClear( GL_COLOR_BUFFER_BIT );
306
307 if( !player.is_dead )
308 {
309 m4x4_projection( vg_pv, gpipeline.fov,
310 (float)vg_window_x / (float)vg_window_y,
311 0.01f, 600.0f );
312 m4x4_mul( vg_pv, world_4x4, vg_pv );
313 }
314 draw_player();
315
316 /* Draw back in the background
317 *
318 * TODO: need to disable alpha write in the terrain shader so this works
319 * again.
320 */
321 glEnable(GL_BLEND);
322 glDisable(GL_DEPTH_TEST);
323 glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
324 glBlendEquation(GL_FUNC_ADD);
325
326 shader_blit_use();
327 shader_blit_uTexMain( 0 );
328 glActiveTexture(GL_TEXTURE0);
329 glBindTexture( GL_TEXTURE_2D, gpipeline.rgb_background );
330
331 render_fsquad();
332 glDisable(GL_BLEND);
333
334 /* Other shite */
335 glDisable( GL_DEPTH_TEST );
336 vg_lines_drawall( (float *)vg_pv );
337 glViewport( 0,0, vg_window_x, vg_window_y );
338 }
339
340 void vg_render(void)
341 {
342 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
343 glViewport( 0,0, vg_window_x, vg_window_y );
344
345 glDisable( GL_DEPTH_TEST );
346 glClearColor( 0.11f, 0.35f, 0.37f, 1.0f );
347 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
348
349 #ifndef SR_NETWORK_TEST
350 draw_origin_axis();
351
352 if( sv_scene == 0 )
353 {
354 render_main_game();
355 }
356 else if( sv_scene == 1 )
357 {
358 physics_test_render();
359 }
360 else if( sv_scene == 2 )
361 {
362 anim_test_render( &tex_characters );
363 }
364 #endif
365 }
366
367 static void run_light_widget( struct light_widget *lw )
368 {
369 struct ui_checkbox c1 = { .data=&lw->enabled };
370
371 ui_checkbox( &ui_global_ctx, &c1 );
372
373 if( lw->enabled )
374 {
375 struct ui_slider_vector
376 colour = { .min=0.0f, .max=2.0f, .len=3, .data=lw->colour },
377 dir = { .min=-VG_PIf, .max=VG_PIf, .len=2, .data=lw->dir };
378
379 ui_slider_vector( &ui_global_ctx, &colour );
380 ui_global_ctx.cursor[1] += 4;
381 ui_slider_vector( &ui_global_ctx, &dir );
382 }
383 }
384
385 static void run_debug_info(void)
386 {
387 char buf[40];
388
389 snprintf( buf, 40, "%.2fm/s", v3_length( player.rb.v ) );
390 gui_text( (ui_px [2]){ 0, 0 }, buf, 1, k_text_align_left );
391
392 snprintf( buf, 40, "%.2f %.2f %.2f m/s",
393 player.a[0], player.a[1], player.a[2] );
394 gui_text( (ui_px [2]){ 0, 20 }, buf, 1, k_text_align_left );
395
396 snprintf( buf, 40, "pos %.2f %.2f %.2f",
397 player.rb.co[0], player.rb.co[1], player.rb.co[2] );
398 gui_text( (ui_px [2]){ 0, 40 }, buf, 1, k_text_align_left );
399
400 if( vg_gamepad_ready )
401 {
402 for( int i=0; i<6; i++ )
403 {
404 snprintf( buf, 40, "%.2f", vg_gamepad.axes[i] );
405 gui_text( (ui_px [2]){ 0, (i+3)*20 }, buf, 1, k_text_align_left );
406 }
407 }
408 else
409 {
410 gui_text( (ui_px [2]){ 0, 60 },
411 "Gamepad not ready", 1, k_text_align_left );
412 }
413 }
414
415 void vg_ui(void)
416 {
417 if( lightedit )
418 {
419 ui_global_ctx.cursor[0] = 10;
420 ui_global_ctx.cursor[1] = 10;
421 ui_global_ctx.cursor[2] = 200;
422 ui_global_ctx.cursor[3] = 20;
423
424 struct ub_world_lighting *wl = &gpipeline.ub_world_lighting;
425 struct ui_slider_vector
426 s5 = { .min=0.0f, .max=2.0f, .len=3, .data=wl->g_ambient_colour };
427
428 struct ui_slider
429 s8 = { .min=0.0f, .max=2.0f, .data = &gpipeline.shadow_spread },
430 s9 = { .min=0.0f, .max=25.0f, .data = &gpipeline.shadow_length };
431
432 for( int i=0; i<3; i++ )
433 run_light_widget( &gpipeline.widgets[i] );
434
435 gui_text( ui_global_ctx.cursor, "Ambient", 1, 0 );
436 ui_global_ctx.cursor[1] += 16;
437 ui_slider_vector( &ui_global_ctx, &s5 );
438
439 gui_text( ui_global_ctx.cursor, "Shadows", 1, 0 );
440 ui_global_ctx.cursor[1] += 16;
441 ui_slider( &ui_global_ctx, &s8 );
442 ui_slider( &ui_global_ctx, &s9 );
443
444 gui_text( ui_global_ctx.cursor, "Misc", 1, 0 );
445 ui_global_ctx.cursor[1] += 16;
446 struct ui_checkbox c1 = {.data = &wl->g_light_preview};
447 ui_checkbox( &ui_global_ctx, &c1 );
448
449 render_update_lighting_ub();
450 }
451
452 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
453 render_world_routes_ui();
454 //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
455
456 #if 0
457 static double last_b_press = 0.0;
458
459 double localtime = vg_time - last_b_press;
460
461 world_routes_ui_updatetime( 0, localtime );
462 world_routes_ui_draw( 0, (v4f){ 1.0f,0.0f,1.0f,1.0f}, 9.0f );
463
464 if( glfwGetKey(vg_window,GLFW_KEY_B) )
465 world_routes_ui_notch( 0, localtime );
466
467 if( vg_time-last_b_press > 1.0 )
468 if( glfwGetKey(vg_window,GLFW_KEY_N) )
469 {
470 last_b_press = vg_time;
471 world_routes_ui_newseg( 0 );
472 }
473
474 static double last_m_press;
475 if( vg_time-last_m_press > 1.0 )
476 if( glfwGetKey( vg_window, GLFW_KEY_M) )
477 {
478 last_m_press = vg_time;
479
480 vg_info( "start: %u\n",world.routes.routes[0].ui.segment_count );
481 for( int i=0; i<world.routes.routes[0].ui.segment_count; i++ )
482 world_routes_ui_popfirst(0);
483
484 vg_info( "new: %u\n",world.routes.routes[0].ui.segment_count );
485 }
486 #endif
487 }