2 * =============================================================================
4 * Copyright . . . -----, ,----- ,---. .---.
5 * 2021-2022 |\ /| | / | | | | /|
6 * | \ / | +-- / +----- +---' | / |
7 * | \ / | | / | | \ | / |
8 * | \/ | | / | | \ | / |
9 * ' ' '--' [] '----- '----- ' ' '---' SOFTWARE
11 * =============================================================================
13 * register: shader register & init scheduling
14 * init: initialization
22 #define VG_STATIC static
25 //#define VG_MINIMAL_TEST
26 #ifndef VG_MINIMAL_TEST
41 int main( int argc
, char *argv
[] )
43 vg_mem
.use_libc_malloc
= 0;
44 vg_set_mem_quota( 128*1024*1024 );
45 vg_enter( argc
, argv
, "Voyager Game Engine" );
48 VG_STATIC
void highscores_save_at_exit(void*_
)
50 highscores_serialize_all();
53 VG_STATIC
void vg_preload(void)
56 vg_convar_push( (struct vg_convar
){
59 .data_type
= k_convar_dtype_i32
,
60 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
64 vg_convar_push( (struct vg_convar
){
66 .data
= &g_fov_option
,
67 .data_type
= k_convar_dtype_f32
,
68 .opt_f32
= { .clamp
= 0 },
72 vg_info(" Copyright . . . -----, ,----- ,---. .---. \n" );
73 vg_info(" 2021-2022 |\\ /| | / | | | | /| \n" );
74 vg_info(" | \\ / | +-- / +----- +---' | / | \n" );
75 vg_info(" | \\ / | | / | | \\ | / | \n" );
76 vg_info(" | \\/ | | / | | \\ | / | \n" );
77 vg_info(" ' ' '--' [] '----- '----- ' ' '---' "
80 highscores_init( 2000, 50 );
81 if( !highscores_read() )
82 highscores_create_db();
84 vg_loader_highwater( NULL
, highscores_save_at_exit
, NULL
);
89 vg_loader_highwater( NULL
, steam_end
, NULL
);
90 vg_loader_highwater( network_init
, network_end
, NULL
);
93 VG_STATIC
void vg_load(void)
95 vg_loader_highwater( render_init
, NULL
, NULL
);
96 vg_loader_highwater( menu_init
, NULL
, NULL
);
97 vg_loader_highwater( world_init
, NULL
, NULL
);
98 vg_loader_highwater( player_init
, NULL
, NULL
);
101 vg_loader_highwater( audio_init
, audio_free
, NULL
);
104 /* 'systems' are completely loaded now */
105 strcpy( world
.world_name
, "maps/mp_mtzero.mdl" );
107 vg_console_load_autos();
110 VG_STATIC
void vg_start(void)
112 reset_player( 1, (const char *[]){ "start" } );
115 VG_STATIC
void draw_origin_axis(void)
117 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, 0xffff0000 );
118 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 0.0f
, 1.0f
, 0.0f
}, 0xff00ff00 );
119 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 0.0f
, 0.0f
, 1.0f
}, 0xff0000ff );
122 VG_STATIC
void vg_update( int loaded
)
132 world_update( player
.phys
.rb
.co
);
136 VG_STATIC
void vg_update_fixed( int loaded
)
140 player_update_fixed();
144 VG_STATIC
void vg_update_post( int loaded
)
148 player_update_post();
153 VG_STATIC
void vg_framebuffer_resize( int w
, int h
)
159 VG_STATIC
void render_main_game(void)
162 m4x3_expand( camera_mtx_inverse
, world_4x4
);
164 static float fov
= 60.0f
;
166 float fov_target
= vg_lerpf( 90.0f
, 110.0f
, g_fov_option
);
168 if( player
.phys
.on_board
)
169 fov_target
= vg_lerpf( 97.0f
, 135.0f
, g_fov_option
);
172 fov_target
= menu_fov_target
;
174 fov
= vg_lerpf( fov
, fov_target
, vg
.frame_delta
* 2.0f
);
176 gpipeline
.fov
= freecam
? 60.0f
: fov
; /* 120 */
177 m4x4_projection( vg
.pv
, gpipeline
.fov
,
178 (float)vg
.window_x
/ (float)vg
.window_y
,
181 m4x4_mul( vg
.pv
, world_4x4
, vg
.pv
);
182 glEnable( GL_DEPTH_TEST
);
188 int draw_solid
= player
.is_dead
| freecam
;
189 render_world( vg
.pv
, camera_mtx
);
192 draw_player( camera_mtx
);
194 render_water_texture( camera_mtx
);
196 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
197 render_water_surface( vg
.pv
, camera_mtx
);
198 render_world_gates( vg
.pv
, player
.phys
.rb
.co
, camera_mtx
);
202 glClear( GL_DEPTH_BUFFER_BIT
);
203 menu_render( vg
.pv
);
206 /* Copy the RGB of what we have into the background buffer */
207 glBindFramebuffer( GL_READ_FRAMEBUFFER
, 0 );
208 glBindFramebuffer( GL_DRAW_FRAMEBUFFER
, gpipeline
.fb_background
);
209 glBlitFramebuffer( 0,0, vg
.window_x
, vg
.window_y
,
210 0,0, vg
.window_x
, vg
.window_y
,
214 /* Clear out the colour buffer, but keep depth */
215 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
216 glClearColor( 0.0f
, 0.0f
, 0.0f
, 0.0f
);
218 if( !player
.is_dead
)
219 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
221 glClear( GL_COLOR_BUFFER_BIT
);
225 m4x4_projection( vg
.pv
, gpipeline
.fov
,
226 (float)vg
.window_x
/ (float)vg
.window_y
,
228 m4x4_mul( vg
.pv
, world_4x4
, vg
.pv
);
229 draw_player( camera_mtx
);
232 /* Draw back in the background
234 * TODO: need to disable alpha write in the terrain shader so this works
238 glDisable(GL_DEPTH_TEST
);
239 glBlendFunc(GL_ONE_MINUS_DST_ALPHA
, GL_DST_ALPHA
);
240 glBlendEquation(GL_FUNC_ADD
);
243 shader_blit_uTexMain( 0 );
244 glActiveTexture(GL_TEXTURE0
);
245 glBindTexture( GL_TEXTURE_2D
, gpipeline
.rgb_background
);
250 VG_STATIC
void vg_render(void)
252 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
253 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
254 glDisable( GL_DEPTH_TEST
);
256 glClearColor( 0.11f
, 0.35f
, 0.37f
, 1.0f
);
257 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
);
264 glDisable( GL_DEPTH_TEST
);
265 vg_lines_drawall( (float *)vg
.pv
);
266 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
269 VG_STATIC
void vg_ui(void)
276 ui_global_ctx
.cursor
[0] = 10;
277 ui_global_ctx
.cursor
[1] = 10;
278 ui_global_ctx
.cursor
[2] = 200;
279 ui_global_ctx
.cursor
[3] = 20;
281 struct ub_world_lighting
*wl
= &gpipeline
.ub_world_lighting
;
282 struct ui_slider_vector
283 s5
= { .min
=0.0f
, .max
=2.0f
, .len
=3, .data
=wl
->g_ambient_colour
};
286 s8
= { .min
=0.0f
, .max
=2.0f
, .data
= &gpipeline
.shadow_spread
},
287 s9
= { .min
=0.0f
, .max
=25.0f
, .data
= &gpipeline
.shadow_length
};
289 for( int i
=0; i
<3; i
++ )
290 run_light_widget( &gpipeline
.widgets
[i
] );
292 gui_text( ui_global_ctx
.cursor
, "Ambient", 1, 0 );
293 ui_global_ctx
.cursor
[1] += 16;
294 ui_slider_vector( &ui_global_ctx
, &s5
);
296 gui_text( ui_global_ctx
.cursor
, "Shadows", 1, 0 );
297 ui_global_ctx
.cursor
[1] += 16;
298 ui_slider( &ui_global_ctx
, &s8
);
299 ui_slider( &ui_global_ctx
, &s9
);
301 gui_text( ui_global_ctx
.cursor
, "Misc", 1, 0 );
302 ui_global_ctx
.cursor
[1] += 16;
303 struct ui_checkbox c1
= {.data
= &wl
->g_light_preview
};
304 ui_checkbox( &ui_global_ctx
, &c1
);
306 render_update_lighting_ub();
310 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
313 render_world_routes_ui();
315 //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
317 audio_debug_soundscapes();
321 VG_STATIC
void run_light_widget( struct light_widget
*lw
)
323 struct ui_checkbox c1
= { .data
=&lw
->enabled
};
325 ui_checkbox( &ui_global_ctx
, &c1
);
329 struct ui_slider_vector
330 colour
= { .min
=0.0f
, .max
=2.0f
, .len
=3, .data
=lw
->colour
},
331 dir
= { .min
=-VG_PIf
, .max
=VG_PIf
, .len
=2, .data
=lw
->dir
};
333 ui_slider_vector( &ui_global_ctx
, &colour
);
334 ui_global_ctx
.cursor
[1] += 4;
335 ui_slider_vector( &ui_global_ctx
, &dir
);
340 VG_STATIC
void run_debug_info(void)
344 snprintf( buf
, 40, "%.2fm/s", v3_length( player
.phys
.rb
.v
) );
345 ui_text( (ui_px
[2]){ 0, 0 }, buf
, 1, k_text_align_left
);
347 snprintf( buf
, 40, "%.2f %.2f %.2f m/s",
348 player
.phys
.a
[0], player
.phys
.a
[1], player
.phys
.a
[2] );
349 ui_text( (ui_px
[2]){ 0, 20 }, buf
, 1, k_text_align_left
);
351 snprintf( buf
, 40, "pos %.2f %.2f %.2f",
352 player
.phys
.rb
.co
[0], player
.phys
.rb
.co
[1], player
.phys
.rb
.co
[2] );
353 ui_text( (ui_px
[2]){ 0, 40 }, buf
, 1, k_text_align_left
);
355 if( vg
.gamepad_ready
)
357 for( int i
=0; i
<6; i
++ )
359 snprintf( buf
, 40, "%.2f", vg
.gamepad
.axes
[i
] );
360 ui_text( (ui_px
[2]){ 0, (i
+3)*20 }, buf
, 1, k_text_align_left
);
365 ui_text( (ui_px
[2]){ 0, 60 },
366 "Gamepad not ready", 1, k_text_align_left
);
372 #define VG_TIMESTEP_FIXED (1.0/60.0)
374 #define VG_FRAMEBUFFER_RESIZE 1
377 int main( int argc
, char *argv
[] )
379 vg_prealloc_quota( 512*1024*1024 );
380 vg_enter( argc
, argv
, "Voyager Game Engine" );
383 VG_STATIC
void vg_preload(void)
385 vg_info(" Copyright . . . -----, ,----- ,---. .---. \n" );
386 vg_info(" 2021-2022 |\\ /| | / | | | | /| \n" );
387 vg_info(" | \\ / | +-- / +----- +---' | / | \n" );
388 vg_info(" | \\ / | | / | | \\ | / | \n" );
389 vg_info(" | \\/ | | / | | \\ | / | \n" );
390 vg_info(" ' ' '--' [] '----- '----- ' ' '---' "
394 VG_STATIC
void vg_load(void)
397 vg_console_load_autos();
400 VG_STATIC
void vg_start(void)
404 VG_STATIC
void vg_update( int loaded
)
408 VG_STATIC
void vg_update_fixed( int loaded
)
412 VG_STATIC
void vg_update_post( int loaded
)
416 VG_STATIC
void vg_framebuffer_resize( int w
, int h
)
420 VG_STATIC
void vg_render(void)
422 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
423 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
424 glDisable( GL_DEPTH_TEST
);
426 glClearColor( 0.11f
, 0.35f
, 0.37f
, 1.0f
);
427 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
);
431 glDisable( GL_DEPTH_TEST
);
432 vg_lines_drawall( (float *)vg
.pv
);
433 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
436 VG_STATIC
void vg_ui(void)