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
24 //#define VG_MINIMAL_TEST
25 #ifndef VG_MINIMAL_TEST
40 int main( int argc
, char *argv
[] )
42 vg_prealloc_quota( 128*1024*1024 );
43 vg_enter( argc
, argv
, "Voyager Game Engine" );
46 VG_STATIC
void highscores_save_at_exit(void*_
)
48 highscores_serialize_all();
51 VG_STATIC
void vg_preload(void)
53 vg_convar_push( (struct vg_convar
){
56 .data_type
= k_convar_dtype_i32
,
57 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
61 vg_info(" Copyright . . . -----, ,----- ,---. .---. \n" );
62 vg_info(" 2021-2022 |\\ /| | / | | | | /| \n" );
63 vg_info(" | \\ / | +-- / +----- +---' | / | \n" );
64 vg_info(" | \\ / | | / | | \\ | / | \n" );
65 vg_info(" | \\/ | | / | | \\ | / | \n" );
66 vg_info(" ' ' '--' [] '----- '----- ' ' '---' "
69 highscores_init( 2000, 50 );
70 if( !highscores_read() )
71 highscores_create_db();
73 vg_loader_highwater( NULL
, highscores_save_at_exit
, NULL
);
78 vg_loader_highwater( NULL
, steam_end
, NULL
);
79 vg_loader_highwater( network_init
, network_end
, NULL
);
82 VG_STATIC
void vg_load(void)
84 vg_loader_highwater( render_init
, NULL
, NULL
);
85 vg_loader_highwater( menu_init
, NULL
, NULL
);
86 vg_loader_highwater( world_init
, NULL
, NULL
);
87 vg_loader_highwater( player_init
, NULL
, NULL
);
90 vg_loader_highwater( audio_init
, audio_free
, NULL
);
92 /* 'systems' are completely loaded now */
93 world_load( "models/mp_dev.mdl" );
94 vg_console_load_autos();
97 VG_STATIC
void vg_start(void)
99 reset_player( 1, (const char *[]){ "start" } );
102 VG_STATIC
void draw_origin_axis(void)
104 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, 0xffff0000 );
105 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 0.0f
, 1.0f
, 0.0f
}, 0xff00ff00 );
106 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 0.0f
, 0.0f
, 1.0f
}, 0xff0000ff );
109 VG_STATIC
void vg_update( int loaded
)
119 world_update( player
.phys
.rb
.co
);
123 VG_STATIC
void vg_update_fixed( int loaded
)
127 player_update_fixed();
131 VG_STATIC
void vg_update_post( int loaded
)
135 player_update_post();
140 VG_STATIC
void vg_framebuffer_resize( int w
, int h
)
146 VG_STATIC
void render_main_game(void)
149 m4x3_expand( camera_mtx_inverse
, world_4x4
);
151 static float fov
= 97.0f
;
153 float fov_target
= 108.0f
;
154 if( player
.phys
.on_board
)
158 fov_target
= menu_fov_target
;
160 fov
= vg_lerpf( fov
, fov_target
, vg
.frame_delta
* 2.0f
);
162 gpipeline
.fov
= freecam
? 60.0f
: fov
; /* 120 */
163 m4x4_projection( vg
.pv
, gpipeline
.fov
,
164 (float)vg
.window_x
/ (float)vg
.window_y
,
167 m4x4_mul( vg
.pv
, world_4x4
, vg
.pv
);
168 glEnable( GL_DEPTH_TEST
);
174 int draw_solid
= player
.is_dead
| freecam
;
176 render_world( vg
.pv
, camera_mtx
);
178 draw_player( camera_mtx
);
180 render_water_texture( camera_mtx
);
182 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
183 render_water_surface( vg
.pv
, camera_mtx
);
184 render_world_gates( vg
.pv
, player
.phys
.rb
.co
, camera_mtx
);
188 glClear( GL_DEPTH_BUFFER_BIT
);
189 menu_render( vg
.pv
);
192 /* Copy the RGB of what we have into the background buffer */
193 glBindFramebuffer( GL_READ_FRAMEBUFFER
, 0 );
194 glBindFramebuffer( GL_DRAW_FRAMEBUFFER
, gpipeline
.fb_background
);
195 glBlitFramebuffer( 0,0, vg
.window_x
, vg
.window_y
,
196 0,0, vg
.window_x
, vg
.window_y
,
200 /* Clear out the colour buffer, but keep depth */
201 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
202 glClearColor( 0.0f
, 0.0f
, 0.0f
, 0.0f
);
204 if( !player
.is_dead
)
205 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
207 glClear( GL_COLOR_BUFFER_BIT
);
211 m4x4_projection( vg
.pv
, gpipeline
.fov
,
212 (float)vg
.window_x
/ (float)vg
.window_y
,
214 m4x4_mul( vg
.pv
, world_4x4
, vg
.pv
);
215 draw_player( camera_mtx
);
218 /* Draw back in the background
220 * TODO: need to disable alpha write in the terrain shader so this works
224 glDisable(GL_DEPTH_TEST
);
225 glBlendFunc(GL_ONE_MINUS_DST_ALPHA
, GL_DST_ALPHA
);
226 glBlendEquation(GL_FUNC_ADD
);
229 shader_blit_uTexMain( 0 );
230 glActiveTexture(GL_TEXTURE0
);
231 glBindTexture( GL_TEXTURE_2D
, gpipeline
.rgb_background
);
236 VG_STATIC
void vg_render(void)
238 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
239 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
240 glDisable( GL_DEPTH_TEST
);
242 glClearColor( 0.11f
, 0.35f
, 0.37f
, 1.0f
);
243 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
);
250 glDisable( GL_DEPTH_TEST
);
251 vg_lines_drawall( (float *)vg
.pv
);
252 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
255 VG_STATIC
void vg_ui(void)
262 vg
.window_x
/ 2 - 150,
263 vg
.window_y
/ 2 - 50,
268 ui_fill_rect( &ui_global_ctx
, menu
, 0xa0000000 );
270 ui_text( &ui_global_ctx
, (ui_rect
){ menu
[0]+menu
[2]/2,menu
[1]+6, 0, 0 },
271 steam_username_at_startup
,
272 1, k_text_align_center
);
276 ui_rect_pad( menu
, 8 );
277 ui_fill_rect( &ui_global_ctx
, menu
, 0x90ffffff );
278 ui_rect_pad( menu
, 2 );
279 ui_fill_rect( &ui_global_ctx
, menu
, 0xa0000000 );
282 ui_text( &ui_global_ctx
, (ui_rect
){ menu
[0]+menu
[2]/2,menu
[1], 0, 0 },
283 "Exit", 2, k_text_align_center
);
285 if( vg_get_button_down( "jump" ) )
287 glfwSetWindowShouldClose( vg
.window
, 1 );
293 ui_global_ctx
.cursor
[0] = 10;
294 ui_global_ctx
.cursor
[1] = 10;
295 ui_global_ctx
.cursor
[2] = 200;
296 ui_global_ctx
.cursor
[3] = 20;
298 struct ub_world_lighting
*wl
= &gpipeline
.ub_world_lighting
;
299 struct ui_slider_vector
300 s5
= { .min
=0.0f
, .max
=2.0f
, .len
=3, .data
=wl
->g_ambient_colour
};
303 s8
= { .min
=0.0f
, .max
=2.0f
, .data
= &gpipeline
.shadow_spread
},
304 s9
= { .min
=0.0f
, .max
=25.0f
, .data
= &gpipeline
.shadow_length
};
306 for( int i
=0; i
<3; i
++ )
307 run_light_widget( &gpipeline
.widgets
[i
] );
309 gui_text( ui_global_ctx
.cursor
, "Ambient", 1, 0 );
310 ui_global_ctx
.cursor
[1] += 16;
311 ui_slider_vector( &ui_global_ctx
, &s5
);
313 gui_text( ui_global_ctx
.cursor
, "Shadows", 1, 0 );
314 ui_global_ctx
.cursor
[1] += 16;
315 ui_slider( &ui_global_ctx
, &s8
);
316 ui_slider( &ui_global_ctx
, &s9
);
318 gui_text( ui_global_ctx
.cursor
, "Misc", 1, 0 );
319 ui_global_ctx
.cursor
[1] += 16;
320 struct ui_checkbox c1
= {.data
= &wl
->g_light_preview
};
321 ui_checkbox( &ui_global_ctx
, &c1
);
323 render_update_lighting_ub();
327 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
330 render_world_routes_ui();
332 //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
334 audio_debug_soundscapes();
338 VG_STATIC
void run_light_widget( struct light_widget
*lw
)
340 struct ui_checkbox c1
= { .data
=&lw
->enabled
};
342 ui_checkbox( &ui_global_ctx
, &c1
);
346 struct ui_slider_vector
347 colour
= { .min
=0.0f
, .max
=2.0f
, .len
=3, .data
=lw
->colour
},
348 dir
= { .min
=-VG_PIf
, .max
=VG_PIf
, .len
=2, .data
=lw
->dir
};
350 ui_slider_vector( &ui_global_ctx
, &colour
);
351 ui_global_ctx
.cursor
[1] += 4;
352 ui_slider_vector( &ui_global_ctx
, &dir
);
357 VG_STATIC
void run_debug_info(void)
361 snprintf( buf
, 40, "%.2fm/s", v3_length( player
.phys
.rb
.v
) );
362 ui_text( (ui_px
[2]){ 0, 0 }, buf
, 1, k_text_align_left
);
364 snprintf( buf
, 40, "%.2f %.2f %.2f m/s",
365 player
.phys
.a
[0], player
.phys
.a
[1], player
.phys
.a
[2] );
366 ui_text( (ui_px
[2]){ 0, 20 }, buf
, 1, k_text_align_left
);
368 snprintf( buf
, 40, "pos %.2f %.2f %.2f",
369 player
.phys
.rb
.co
[0], player
.phys
.rb
.co
[1], player
.phys
.rb
.co
[2] );
370 ui_text( (ui_px
[2]){ 0, 40 }, buf
, 1, k_text_align_left
);
372 if( vg
.gamepad_ready
)
374 for( int i
=0; i
<6; i
++ )
376 snprintf( buf
, 40, "%.2f", vg
.gamepad
.axes
[i
] );
377 ui_text( (ui_px
[2]){ 0, (i
+3)*20 }, buf
, 1, k_text_align_left
);
382 ui_text( (ui_px
[2]){ 0, 60 },
383 "Gamepad not ready", 1, k_text_align_left
);
389 #define VG_TIMESTEP_FIXED (1.0/60.0)
391 #define VG_FRAMEBUFFER_RESIZE 1
394 int main( int argc
, char *argv
[] )
396 vg_prealloc_quota( 512*1024*1024 );
397 vg_enter( argc
, argv
, "Voyager Game Engine" );
400 VG_STATIC
void vg_preload(void)
402 vg_info(" Copyright . . . -----, ,----- ,---. .---. \n" );
403 vg_info(" 2021-2022 |\\ /| | / | | | | /| \n" );
404 vg_info(" | \\ / | +-- / +----- +---' | / | \n" );
405 vg_info(" | \\ / | | / | | \\ | / | \n" );
406 vg_info(" | \\/ | | / | | \\ | / | \n" );
407 vg_info(" ' ' '--' [] '----- '----- ' ' '---' "
411 VG_STATIC
void vg_load(void)
414 vg_console_load_autos();
417 VG_STATIC
void vg_start(void)
421 VG_STATIC
void vg_update( int loaded
)
425 VG_STATIC
void vg_update_fixed( int loaded
)
429 VG_STATIC
void vg_update_post( int loaded
)
433 VG_STATIC
void vg_framebuffer_resize( int w
, int h
)
437 VG_STATIC
void vg_render(void)
439 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
440 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
441 glDisable( GL_DEPTH_TEST
);
443 glClearColor( 0.11f
, 0.35f
, 0.37f
, 1.0f
);
444 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
);
448 glDisable( GL_DEPTH_TEST
);
449 vg_lines_drawall( (float *)vg
.pv
);
450 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
453 VG_STATIC
void vg_ui(void)