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