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
= 1;
45 vg_prealloc_quota( 128*1024*1024 );
46 void *test_allocator
= vg_create_linear_allocator( NULL
, 2048 );
48 for( int i
=0; i
<8; i
++ )
50 u64
*items
= vg_linear_alloc( test_allocator
, sizeof(u64
) );
51 items
= vg_linear_extend( test_allocator
, items
, sizeof(u64
));
55 vg_info( "%lu %lu\n", items
[0], items
[1] );
58 vg_linear_clear( test_allocator
);
62 vg_prealloc_quota( 128*1024*1024 );
63 vg_enter( argc
, argv
, "Voyager Game Engine" );
66 VG_STATIC
void highscores_save_at_exit(void*_
)
68 highscores_serialize_all();
71 VG_STATIC
void vg_preload(void)
74 vg_convar_push( (struct vg_convar
){
77 .data_type
= k_convar_dtype_i32
,
78 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
82 vg_info(" Copyright . . . -----, ,----- ,---. .---. \n" );
83 vg_info(" 2021-2022 |\\ /| | / | | | | /| \n" );
84 vg_info(" | \\ / | +-- / +----- +---' | / | \n" );
85 vg_info(" | \\ / | | / | | \\ | / | \n" );
86 vg_info(" | \\/ | | / | | \\ | / | \n" );
87 vg_info(" ' ' '--' [] '----- '----- ' ' '---' "
90 highscores_init( 2000, 50 );
91 if( !highscores_read() )
92 highscores_create_db();
94 vg_loader_highwater( NULL
, highscores_save_at_exit
, NULL
);
99 vg_loader_highwater( NULL
, steam_end
, NULL
);
100 vg_loader_highwater( network_init
, network_end
, NULL
);
103 VG_STATIC
void vg_load(void)
105 vg_loader_highwater( render_init
, NULL
, NULL
);
106 vg_loader_highwater( menu_init
, NULL
, NULL
);
107 vg_loader_highwater( world_init
, NULL
, NULL
);
108 vg_loader_highwater( player_init
, NULL
, NULL
);
111 vg_loader_highwater( audio_init
, audio_free
, NULL
);
113 /* 'systems' are completely loaded now */
114 strcpy( world
.world_name
, "models/mp_test.mdl" );
116 vg_console_load_autos();
119 VG_STATIC
void vg_start(void)
121 reset_player( 1, (const char *[]){ "start" } );
124 VG_STATIC
void draw_origin_axis(void)
126 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, 0xffff0000 );
127 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 0.0f
, 1.0f
, 0.0f
}, 0xff00ff00 );
128 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 0.0f
, 0.0f
, 1.0f
}, 0xff0000ff );
131 VG_STATIC
void vg_update( int loaded
)
141 world_update( player
.phys
.rb
.co
);
145 VG_STATIC
void vg_update_fixed( int loaded
)
149 player_update_fixed();
153 VG_STATIC
void vg_update_post( int loaded
)
157 player_update_post();
162 VG_STATIC
void vg_framebuffer_resize( int w
, int h
)
168 VG_STATIC
void render_main_game(void)
171 m4x3_expand( camera_mtx_inverse
, world_4x4
);
173 static float fov
= 97.0f
;
175 float fov_target
= 108.0f
;
176 if( player
.phys
.on_board
)
180 fov_target
= menu_fov_target
;
182 fov
= vg_lerpf( fov
, fov_target
, vg
.frame_delta
* 2.0f
);
184 gpipeline
.fov
= freecam
? 60.0f
: fov
; /* 120 */
185 m4x4_projection( vg
.pv
, gpipeline
.fov
,
186 (float)vg
.window_x
/ (float)vg
.window_y
,
189 m4x4_mul( vg
.pv
, world_4x4
, vg
.pv
);
190 glEnable( GL_DEPTH_TEST
);
196 int draw_solid
= player
.is_dead
| freecam
;
198 render_world( vg
.pv
, camera_mtx
);
200 draw_player( camera_mtx
);
202 render_water_texture( camera_mtx
);
204 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
205 render_water_surface( vg
.pv
, camera_mtx
);
206 render_world_gates( vg
.pv
, player
.phys
.rb
.co
, camera_mtx
);
210 glClear( GL_DEPTH_BUFFER_BIT
);
211 menu_render( vg
.pv
);
214 /* Copy the RGB of what we have into the background buffer */
215 glBindFramebuffer( GL_READ_FRAMEBUFFER
, 0 );
216 glBindFramebuffer( GL_DRAW_FRAMEBUFFER
, gpipeline
.fb_background
);
217 glBlitFramebuffer( 0,0, vg
.window_x
, vg
.window_y
,
218 0,0, vg
.window_x
, vg
.window_y
,
222 /* Clear out the colour buffer, but keep depth */
223 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
224 glClearColor( 0.0f
, 0.0f
, 0.0f
, 0.0f
);
226 if( !player
.is_dead
)
227 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
229 glClear( GL_COLOR_BUFFER_BIT
);
233 m4x4_projection( vg
.pv
, gpipeline
.fov
,
234 (float)vg
.window_x
/ (float)vg
.window_y
,
236 m4x4_mul( vg
.pv
, world_4x4
, vg
.pv
);
237 draw_player( camera_mtx
);
240 /* Draw back in the background
242 * TODO: need to disable alpha write in the terrain shader so this works
246 glDisable(GL_DEPTH_TEST
);
247 glBlendFunc(GL_ONE_MINUS_DST_ALPHA
, GL_DST_ALPHA
);
248 glBlendEquation(GL_FUNC_ADD
);
251 shader_blit_uTexMain( 0 );
252 glActiveTexture(GL_TEXTURE0
);
253 glBindTexture( GL_TEXTURE_2D
, gpipeline
.rgb_background
);
258 VG_STATIC
void vg_render(void)
260 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
261 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
262 glDisable( GL_DEPTH_TEST
);
264 glClearColor( 0.11f
, 0.35f
, 0.37f
, 1.0f
);
265 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
);
272 glDisable( GL_DEPTH_TEST
);
273 vg_lines_drawall( (float *)vg
.pv
);
274 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
277 VG_STATIC
void vg_ui(void)
284 vg
.window_x
/ 2 - 150,
285 vg
.window_y
/ 2 - 50,
290 ui_fill_rect( &ui_global_ctx
, menu
, 0xa0000000 );
292 ui_text( &ui_global_ctx
, (ui_rect
){ menu
[0]+menu
[2]/2,menu
[1]+6, 0, 0 },
293 steam_username_at_startup
,
294 1, k_text_align_center
);
298 ui_rect_pad( menu
, 8 );
299 ui_fill_rect( &ui_global_ctx
, menu
, 0x90ffffff );
300 ui_rect_pad( menu
, 2 );
301 ui_fill_rect( &ui_global_ctx
, menu
, 0xa0000000 );
304 ui_text( &ui_global_ctx
, (ui_rect
){ menu
[0]+menu
[2]/2,menu
[1], 0, 0 },
305 "Exit", 2, k_text_align_center
);
307 if( vg_get_button_down( "jump" ) )
309 glfwSetWindowShouldClose( vg
.window
, 1 );
315 ui_global_ctx
.cursor
[0] = 10;
316 ui_global_ctx
.cursor
[1] = 10;
317 ui_global_ctx
.cursor
[2] = 200;
318 ui_global_ctx
.cursor
[3] = 20;
320 struct ub_world_lighting
*wl
= &gpipeline
.ub_world_lighting
;
321 struct ui_slider_vector
322 s5
= { .min
=0.0f
, .max
=2.0f
, .len
=3, .data
=wl
->g_ambient_colour
};
325 s8
= { .min
=0.0f
, .max
=2.0f
, .data
= &gpipeline
.shadow_spread
},
326 s9
= { .min
=0.0f
, .max
=25.0f
, .data
= &gpipeline
.shadow_length
};
328 for( int i
=0; i
<3; i
++ )
329 run_light_widget( &gpipeline
.widgets
[i
] );
331 gui_text( ui_global_ctx
.cursor
, "Ambient", 1, 0 );
332 ui_global_ctx
.cursor
[1] += 16;
333 ui_slider_vector( &ui_global_ctx
, &s5
);
335 gui_text( ui_global_ctx
.cursor
, "Shadows", 1, 0 );
336 ui_global_ctx
.cursor
[1] += 16;
337 ui_slider( &ui_global_ctx
, &s8
);
338 ui_slider( &ui_global_ctx
, &s9
);
340 gui_text( ui_global_ctx
.cursor
, "Misc", 1, 0 );
341 ui_global_ctx
.cursor
[1] += 16;
342 struct ui_checkbox c1
= {.data
= &wl
->g_light_preview
};
343 ui_checkbox( &ui_global_ctx
, &c1
);
345 render_update_lighting_ub();
349 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
352 render_world_routes_ui();
354 //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
356 audio_debug_soundscapes();
360 VG_STATIC
void run_light_widget( struct light_widget
*lw
)
362 struct ui_checkbox c1
= { .data
=&lw
->enabled
};
364 ui_checkbox( &ui_global_ctx
, &c1
);
368 struct ui_slider_vector
369 colour
= { .min
=0.0f
, .max
=2.0f
, .len
=3, .data
=lw
->colour
},
370 dir
= { .min
=-VG_PIf
, .max
=VG_PIf
, .len
=2, .data
=lw
->dir
};
372 ui_slider_vector( &ui_global_ctx
, &colour
);
373 ui_global_ctx
.cursor
[1] += 4;
374 ui_slider_vector( &ui_global_ctx
, &dir
);
379 VG_STATIC
void run_debug_info(void)
383 snprintf( buf
, 40, "%.2fm/s", v3_length( player
.phys
.rb
.v
) );
384 ui_text( (ui_px
[2]){ 0, 0 }, buf
, 1, k_text_align_left
);
386 snprintf( buf
, 40, "%.2f %.2f %.2f m/s",
387 player
.phys
.a
[0], player
.phys
.a
[1], player
.phys
.a
[2] );
388 ui_text( (ui_px
[2]){ 0, 20 }, buf
, 1, k_text_align_left
);
390 snprintf( buf
, 40, "pos %.2f %.2f %.2f",
391 player
.phys
.rb
.co
[0], player
.phys
.rb
.co
[1], player
.phys
.rb
.co
[2] );
392 ui_text( (ui_px
[2]){ 0, 40 }, buf
, 1, k_text_align_left
);
394 if( vg
.gamepad_ready
)
396 for( int i
=0; i
<6; i
++ )
398 snprintf( buf
, 40, "%.2f", vg
.gamepad
.axes
[i
] );
399 ui_text( (ui_px
[2]){ 0, (i
+3)*20 }, buf
, 1, k_text_align_left
);
404 ui_text( (ui_px
[2]){ 0, 60 },
405 "Gamepad not ready", 1, k_text_align_left
);
411 #define VG_TIMESTEP_FIXED (1.0/60.0)
413 #define VG_FRAMEBUFFER_RESIZE 1
416 int main( int argc
, char *argv
[] )
418 vg_prealloc_quota( 512*1024*1024 );
419 vg_enter( argc
, argv
, "Voyager Game Engine" );
422 VG_STATIC
void vg_preload(void)
424 vg_info(" Copyright . . . -----, ,----- ,---. .---. \n" );
425 vg_info(" 2021-2022 |\\ /| | / | | | | /| \n" );
426 vg_info(" | \\ / | +-- / +----- +---' | / | \n" );
427 vg_info(" | \\ / | | / | | \\ | / | \n" );
428 vg_info(" | \\/ | | / | | \\ | / | \n" );
429 vg_info(" ' ' '--' [] '----- '----- ' ' '---' "
433 VG_STATIC
void vg_load(void)
436 vg_console_load_autos();
439 VG_STATIC
void vg_start(void)
443 VG_STATIC
void vg_update( int loaded
)
447 VG_STATIC
void vg_update_fixed( int loaded
)
451 VG_STATIC
void vg_update_post( int loaded
)
455 VG_STATIC
void vg_framebuffer_resize( int w
, int h
)
459 VG_STATIC
void vg_render(void)
461 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
462 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
463 glDisable( GL_DEPTH_TEST
);
465 glClearColor( 0.11f
, 0.35f
, 0.37f
, 1.0f
);
466 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
);
470 glDisable( GL_DEPTH_TEST
);
471 vg_lines_drawall( (float *)vg
.pv
);
472 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
475 VG_STATIC
void vg_ui(void)