2 * Copyright 2021-2022 (C) Mount0 Software, Harry Godden - All Rights Reserved
4 * module.h structure definitions
5 * module_submodule.h implementation
12 vg_tex2d tex_norwey
= { .path
= "textures/norway_foliage.qoi" };
13 vg_tex2d tex_grid
= { .path
= "textures/grid.qoi" };
14 vg_tex2d tex_sky
= { .path
= "textures/sky.qoi" };
15 vg_tex2d tex_gradients
= { .path
= "textures/gradients.qoi",
16 .flags
= VG_TEXTURE_CLAMP
};
17 vg_tex2d tex_cement
= { .path
= "textures/cement512.qoi" };
18 vg_tex2d tex_water
= { .path
= "textures/water.qoi" };
21 static int debugview
= 0;
22 static int sv_debugcam
= 0;
23 static int lightedit
= 0;
24 static int sv_scene
= 0;
30 /* uncomment this to run the game without any graphics being drawn */
31 //#define SR_NETWORK_TEST
41 //#include "terrain.h"
42 //#include "character.h"
44 #include "rigidbody.h"
49 #include "shaders/blit.h"
50 #include "shaders/standard.h"
51 #include "shaders/unlit.h"
53 #include "physics_test.h"
54 #include "anim_test.h"
58 void vg_register(void)
60 shader_blit_register();
61 shader_standard_register();
62 shader_vblend_register();
63 shader_unlit_register();
70 static void init_other(void)
79 vg_tex2d
*texture_list
[] =
90 int main( int argc
, char *argv
[] )
92 highscores_init( 2000, 50 );
93 vg_init( argc
, argv
, "Voyager Game Engine" );
96 static int playermodel( int argc
, char const *argv
[] )
98 if( argc
< 1 ) return 0;
100 glmesh old_mesh
= player
.mdl
.mesh
;
102 if( player_load_model( argv
[0] ) )
103 mesh_free( &old_mesh
);
112 vg_convar_push( (struct vg_convar
){
115 .data_type
= k_convar_dtype_i32
,
116 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
120 vg_convar_push( (struct vg_convar
){
122 .data
= &walk_grid_iterations
,
123 .data_type
= k_convar_dtype_i32
,
124 .opt_i32
= { .min
=0, .max
=1, .clamp
=0 },
128 vg_convar_push( (struct vg_convar
){
131 .data_type
= k_convar_dtype_f32
,
132 .opt_f32
= { .clamp
= 0 },
136 vg_convar_push( (struct vg_convar
){
139 .data_type
= k_convar_dtype_i32
,
140 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
144 vg_convar_push( (struct vg_convar
){
147 .data_type
= k_convar_dtype_i32
,
148 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
152 vg_convar_push( (struct vg_convar
){
153 .name
= "walk_speed",
154 .data
= &k_walkspeed
,
155 .data_type
= k_convar_dtype_f32
,
156 .opt_f32
= { .clamp
= 0 },
160 vg_convar_push( (struct vg_convar
){
163 .data_type
= k_convar_dtype_f32
,
164 .opt_f32
= { .clamp
= 0 },
168 vg_convar_push( (struct vg_convar
){
169 .name
= "walk_accel",
170 .data
= &k_walk_accel
,
171 .data_type
= k_convar_dtype_f32
,
172 .opt_f32
= { .clamp
= 0 },
176 vg_convar_push( (struct vg_convar
){
178 .data
= &k_ragdoll_floatyiness
,
179 .data_type
= k_convar_dtype_f32
,
180 .opt_f32
= { .clamp
= 0 },
184 vg_convar_push( (struct vg_convar
){
186 .data
= &k_ragdoll_floatydrag
,
187 .data_type
= k_convar_dtype_f32
,
188 .opt_f32
= { .clamp
= 0 },
192 vg_convar_push( (struct vg_convar
){
195 .data_type
= k_convar_dtype_f32
,
196 .opt_f32
= { .clamp
= 0 },
200 vg_convar_push( (struct vg_convar
){
202 .data
= &sv_debugcam
,
203 .data_type
= k_convar_dtype_i32
,
204 .opt_i32
= { .min
=0, .max
=1, .clamp
=0 },
208 vg_convar_push( (struct vg_convar
){
211 .data_type
= k_convar_dtype_i32
,
212 .opt_i32
= { .min
=0, .max
=1, .clamp
=0 },
216 vg_function_push( (struct vg_cmd
){
218 .function
= reset_player
221 vg_tex2d_init( texture_list
, vg_list_size( texture_list
) );
226 * If we're in physics test mode we dont need to load anything else, this
227 * parameter is dev only. TODO: dev only cvars that don't ship with the game
228 * when building in release mode.
233 player_load_model( "ch_new" );
237 reset_player( 1, (const char *[]){ "start" } );
238 rb_init( &player
.phys
.rb
);
242 else if( sv_scene
== 1 )
244 physics_test_start();
246 else if( sv_scene
== 2 )
255 vg_tex2d_free( texture_list
, vg_list_size(texture_list
) );
256 /* TODO: THE REST OF THE GOD DAMN FREEING STUFF */
259 highscores_serialize_all();
271 world_update( player
.phys
.rb
.co
);
272 //traffic_visualize( world.traffic, world.traffic_count );
275 if( glfwGetKey( vg_window
, GLFW_KEY_J
))
277 v3_copy( player
.camera_pos
, world
.mr_ball
.co
);
280 else if( sv_scene
== 1 )
282 physics_test_update();
284 else if( sv_scene
== 2 )
290 static void vg_framebuffer_resize( int w
, int h
)
297 static void draw_origin_axis(void)
299 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, 0xffff0000 );
300 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 0.0f
, 1.0f
, 0.0f
}, 0xff00ff00 );
301 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 0.0f
, 0.0f
, 1.0f
}, 0xff0000ff );
304 static void render_main_game(void)
306 float speed
= freecam
? 0.0f
: v3_length( player
.phys
.rb
.v
);
307 v3f shake
= { vg_randf()-0.5f
, vg_randf()-0.5f
, vg_randf()-0.5f
};
308 v3_muls( shake
, speed
*0.01f
, shake
);
311 m4x3_expand( player
.camera_inverse
, world_4x4
);
313 gpipeline
.fov
= freecam
? 60.0f
: 125.0f
; /* 120 */
314 m4x4_projection( vg_pv
, gpipeline
.fov
,
315 (float)vg_window_x
/ (float)vg_window_y
,
318 m4x4_mul( vg_pv
, world_4x4
, vg_pv
);
320 glEnable( GL_DEPTH_TEST
);
326 int draw_solid
= player
.is_dead
| freecam
;
328 render_world( vg_pv
, player
.camera
);
331 render_water_texture( player
.camera
);
333 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
334 render_water_surface( vg_pv
, player
.camera
);
336 vg_tex2d_bind( &tex_water
, 1 ); /*TODO: ?*/
337 render_world_gates( vg_pv
, player
.phys
.rb
.co
, player
.camera
);
339 /* Copy the RGB of what we have into the background buffer */
340 glBindFramebuffer( GL_READ_FRAMEBUFFER
, 0 );
341 glBindFramebuffer( GL_DRAW_FRAMEBUFFER
, gpipeline
.fb_background
);
342 glBlitFramebuffer( 0,0, vg_window_x
, vg_window_y
,
343 0,0, vg_window_x
, vg_window_y
,
347 /* Clear out the colour buffer, but keep depth */
348 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
349 glClearColor( 0.0f
, 0.0f
, 0.0f
, 0.0f
);
351 if( !player
.is_dead
)
352 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
354 glClear( GL_COLOR_BUFFER_BIT
);
358 m4x4_projection( vg_pv
, gpipeline
.fov
,
359 (float)vg_window_x
/ (float)vg_window_y
,
361 m4x4_mul( vg_pv
, world_4x4
, vg_pv
);
365 /* Draw back in the background
367 * TODO: need to disable alpha write in the terrain shader so this works
371 glDisable(GL_DEPTH_TEST
);
372 glBlendFunc(GL_ONE_MINUS_DST_ALPHA
, GL_DST_ALPHA
);
373 glBlendEquation(GL_FUNC_ADD
);
376 shader_blit_uTexMain( 0 );
377 glActiveTexture(GL_TEXTURE0
);
378 glBindTexture( GL_TEXTURE_2D
, gpipeline
.rgb_background
);
384 glDisable( GL_DEPTH_TEST
);
385 vg_lines_drawall( (float *)vg_pv
);
386 glViewport( 0,0, vg_window_x
, vg_window_y
);
391 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
392 glViewport( 0,0, vg_window_x
, vg_window_y
);
394 glDisable( GL_DEPTH_TEST
);
395 glClearColor( 0.11f
, 0.35f
, 0.37f
, 1.0f
);
396 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
);
398 #ifndef SR_NETWORK_TEST
405 else if( sv_scene
== 1 )
407 physics_test_render();
409 else if( sv_scene
== 2 )
411 anim_test_render( &tex_characters
);
416 static void run_light_widget( struct light_widget
*lw
)
418 struct ui_checkbox c1
= { .data
=&lw
->enabled
};
420 ui_checkbox( &ui_global_ctx
, &c1
);
424 struct ui_slider_vector
425 colour
= { .min
=0.0f
, .max
=2.0f
, .len
=3, .data
=lw
->colour
},
426 dir
= { .min
=-VG_PIf
, .max
=VG_PIf
, .len
=2, .data
=lw
->dir
};
428 ui_slider_vector( &ui_global_ctx
, &colour
);
429 ui_global_ctx
.cursor
[1] += 4;
430 ui_slider_vector( &ui_global_ctx
, &dir
);
434 static void run_debug_info(void)
438 snprintf( buf
, 40, "%.2fm/s", v3_length( player
.phys
.rb
.v
) );
439 gui_text( (ui_px
[2]){ 0, 0 }, buf
, 1, k_text_align_left
);
441 snprintf( buf
, 40, "%.2f %.2f %.2f m/s",
442 player
.phys
.a
[0], player
.phys
.a
[1], player
.phys
.a
[2] );
443 gui_text( (ui_px
[2]){ 0, 20 }, buf
, 1, k_text_align_left
);
445 snprintf( buf
, 40, "pos %.2f %.2f %.2f",
446 player
.phys
.rb
.co
[0], player
.phys
.rb
.co
[1], player
.phys
.rb
.co
[2] );
447 gui_text( (ui_px
[2]){ 0, 40 }, buf
, 1, k_text_align_left
);
449 if( vg_gamepad_ready
)
451 for( int i
=0; i
<6; i
++ )
453 snprintf( buf
, 40, "%.2f", vg_gamepad
.axes
[i
] );
454 gui_text( (ui_px
[2]){ 0, (i
+3)*20 }, buf
, 1, k_text_align_left
);
459 gui_text( (ui_px
[2]){ 0, 60 },
460 "Gamepad not ready", 1, k_text_align_left
);
468 ui_global_ctx
.cursor
[0] = 10;
469 ui_global_ctx
.cursor
[1] = 10;
470 ui_global_ctx
.cursor
[2] = 200;
471 ui_global_ctx
.cursor
[3] = 20;
473 struct ub_world_lighting
*wl
= &gpipeline
.ub_world_lighting
;
474 struct ui_slider_vector
475 s5
= { .min
=0.0f
, .max
=2.0f
, .len
=3, .data
=wl
->g_ambient_colour
};
478 s8
= { .min
=0.0f
, .max
=2.0f
, .data
= &gpipeline
.shadow_spread
},
479 s9
= { .min
=0.0f
, .max
=25.0f
, .data
= &gpipeline
.shadow_length
};
481 for( int i
=0; i
<3; i
++ )
482 run_light_widget( &gpipeline
.widgets
[i
] );
484 gui_text( ui_global_ctx
.cursor
, "Ambient", 1, 0 );
485 ui_global_ctx
.cursor
[1] += 16;
486 ui_slider_vector( &ui_global_ctx
, &s5
);
488 gui_text( ui_global_ctx
.cursor
, "Shadows", 1, 0 );
489 ui_global_ctx
.cursor
[1] += 16;
490 ui_slider( &ui_global_ctx
, &s8
);
491 ui_slider( &ui_global_ctx
, &s9
);
493 gui_text( ui_global_ctx
.cursor
, "Misc", 1, 0 );
494 ui_global_ctx
.cursor
[1] += 16;
495 struct ui_checkbox c1
= {.data
= &wl
->g_light_preview
};
496 ui_checkbox( &ui_global_ctx
, &c1
);
498 render_update_lighting_ub();
501 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
504 render_world_routes_ui();
506 //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
508 audio_debug_soundscapes();
512 static double last_b_press
= 0.0;
514 double localtime
= vg_time
- last_b_press
;
516 world_routes_ui_updatetime( 0, localtime
);
517 world_routes_ui_draw( 0, (v4f
){ 1.0f
,0.0f
,1.0f
,1.0f
}, 9.0f
);
519 if( glfwGetKey(vg_window
,GLFW_KEY_B
) )
520 world_routes_ui_notch( 0, localtime
);
522 if( vg_time
-last_b_press
> 1.0 )
523 if( glfwGetKey(vg_window
,GLFW_KEY_N
) )
525 last_b_press
= vg_time
;
526 world_routes_ui_newseg( 0 );
529 static double last_m_press
;
530 if( vg_time
-last_m_press
> 1.0 )
531 if( glfwGetKey( vg_window
, GLFW_KEY_M
) )
533 last_m_press
= vg_time
;
535 vg_info( "start: %u\n",world
.routes
.routes
[0].ui
.segment_count
);
536 for( int i
=0; i
<world
.routes
.routes
[0].ui
.segment_count
; i
++ )
537 world_routes_ui_popfirst(0);
539 vg_info( "new: %u\n",world
.routes
.routes
[0].ui
.segment_count
);