1b106250da54ba3e0359a97bcc2ec158ef0fdd63
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"
59 void vg_register(void)
61 shader_blit_register();
62 shader_standard_register();
63 shader_vblend_register();
64 shader_unlit_register();
72 static void init_other(void)
82 vg_tex2d
*texture_list
[] =
93 int main( int argc
, char *argv
[] )
95 highscores_init( 2000, 50 );
96 vg_init( argc
, argv
, "Voyager Game Engine" );
99 static int playermodel( int argc
, char const *argv
[] )
101 if( argc
< 1 ) return 0;
103 glmesh old_mesh
= player
.mdl
.mesh
;
105 if( character_load( &player
.mdl
, argv
[0] ) )
106 mesh_free( &old_mesh
);
115 vg_convar_push( (struct vg_convar
){
118 .data_type
= k_convar_dtype_i32
,
119 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
123 vg_convar_push( (struct vg_convar
){
125 .data
= &walk_grid_iterations
,
126 .data_type
= k_convar_dtype_i32
,
127 .opt_i32
= { .min
=0, .max
=1, .clamp
=0 },
131 vg_convar_push( (struct vg_convar
){
134 .data_type
= k_convar_dtype_f32
,
135 .opt_f32
= { .clamp
= 0 },
139 vg_convar_push( (struct vg_convar
){
142 .data_type
= k_convar_dtype_i32
,
143 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
147 vg_convar_push( (struct vg_convar
){
150 .data_type
= k_convar_dtype_i32
,
151 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
155 vg_convar_push( (struct vg_convar
){
156 .name
= "walk_speed",
157 .data
= &k_walkspeed
,
158 .data_type
= k_convar_dtype_f32
,
159 .opt_f32
= { .clamp
= 0 },
163 vg_convar_push( (struct vg_convar
){
166 .data_type
= k_convar_dtype_f32
,
167 .opt_f32
= { .clamp
= 0 },
171 vg_convar_push( (struct vg_convar
){
172 .name
= "walk_accel",
173 .data
= &k_walk_accel
,
174 .data_type
= k_convar_dtype_f32
,
175 .opt_f32
= { .clamp
= 0 },
179 vg_convar_push( (struct vg_convar
){
181 .data
= &k_ragdoll_floatyiness
,
182 .data_type
= k_convar_dtype_f32
,
183 .opt_f32
= { .clamp
= 0 },
187 vg_convar_push( (struct vg_convar
){
189 .data
= &k_ragdoll_floatydrag
,
190 .data_type
= k_convar_dtype_f32
,
191 .opt_f32
= { .clamp
= 0 },
195 vg_convar_push( (struct vg_convar
){
198 .data_type
= k_convar_dtype_f32
,
199 .opt_f32
= { .clamp
= 0 },
203 vg_convar_push( (struct vg_convar
){
205 .data
= &sv_debugcam
,
206 .data_type
= k_convar_dtype_i32
,
207 .opt_i32
= { .min
=0, .max
=1, .clamp
=0 },
211 vg_convar_push( (struct vg_convar
){
214 .data_type
= k_convar_dtype_i32
,
215 .opt_i32
= { .min
=0, .max
=1, .clamp
=0 },
219 vg_function_push( (struct vg_cmd
){
221 .function
= reset_player
224 vg_tex2d_init( texture_list
, vg_list_size( texture_list
) );
229 * If we're in physics test mode we dont need to load anything else, this
230 * parameter is dev only. TODO: dev only cvars that don't ship with the game
231 * when building in release mode.
236 character_load( &player
.mdl
, "ch_new" );
237 character_init_ragdoll( &player
.mdl
);
241 reset_player( 1, (const char *[]){ "start" } );
242 rb_init( &player
.rb
);
246 else if( sv_scene
== 1 )
248 physics_test_start();
250 else if( sv_scene
== 2 )
259 vg_tex2d_free( texture_list
, vg_list_size(texture_list
) );
260 /* TODO: THE REST OF THE GOD DAMN FREEING STUFF */
263 highscores_serialize_all();
276 //traffic_visualize( world.traffic, world.traffic_count );
279 if( glfwGetKey( vg_window
, GLFW_KEY_J
))
281 v3_copy( player
.camera_pos
, world
.mr_ball
.co
);
284 else if( sv_scene
== 1 )
286 physics_test_update();
288 else if( sv_scene
== 2 )
294 static void vg_framebuffer_resize( int w
, int h
)
301 static void draw_origin_axis(void)
303 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, 0xffff0000 );
304 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 0.0f
, 1.0f
, 0.0f
}, 0xff00ff00 );
305 vg_line( (v3f
){ 0.0f
, 0.0f
, 0.0f
}, (v3f
){ 0.0f
, 0.0f
, 1.0f
}, 0xff0000ff );
308 static void render_main_game(void)
310 float speed
= freecam
? 0.0f
: v3_length( player
.rb
.v
);
311 v3f shake
= { vg_randf()-0.5f
, vg_randf()-0.5f
, vg_randf()-0.5f
};
312 v3_muls( shake
, speed
*0.01f
, shake
);
315 m4x3_expand( player
.camera_inverse
, world_4x4
);
317 gpipeline
.fov
= freecam
? 60.0f
: 125.0f
; /* 120 */
318 m4x4_projection( vg_pv
, gpipeline
.fov
,
319 (float)vg_window_x
/ (float)vg_window_y
,
322 m4x4_mul( vg_pv
, world_4x4
, vg_pv
);
324 glEnable( GL_DEPTH_TEST
);
330 int draw_solid
= player
.is_dead
| freecam
;
332 render_world( vg_pv
, player
.camera
);
335 render_water_texture( player
.camera
);
337 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
338 render_water_surface( vg_pv
, player
.camera
);
340 vg_tex2d_bind( &tex_water
, 1 ); /*TODO: ?*/
341 render_world_gates( vg_pv
, player
.rb
.co
, player
.camera
);
343 /* Copy the RGB of what we have into the background buffer */
344 glBindFramebuffer( GL_READ_FRAMEBUFFER
, 0 );
345 glBindFramebuffer( GL_DRAW_FRAMEBUFFER
, gpipeline
.fb_background
);
346 glBlitFramebuffer( 0,0, vg_window_x
, vg_window_y
,
347 0,0, vg_window_x
, vg_window_y
,
351 /* Clear out the colour buffer, but keep depth */
352 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
353 glClearColor( 0.0f
, 0.0f
, 0.0f
, 0.0f
);
355 if( !player
.is_dead
)
356 glClear( GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
);
358 glClear( GL_COLOR_BUFFER_BIT
);
362 m4x4_projection( vg_pv
, gpipeline
.fov
,
363 (float)vg_window_x
/ (float)vg_window_y
,
365 m4x4_mul( vg_pv
, world_4x4
, vg_pv
);
369 /* Draw back in the background
371 * TODO: need to disable alpha write in the terrain shader so this works
375 glDisable(GL_DEPTH_TEST
);
376 glBlendFunc(GL_ONE_MINUS_DST_ALPHA
, GL_DST_ALPHA
);
377 glBlendEquation(GL_FUNC_ADD
);
380 shader_blit_uTexMain( 0 );
381 glActiveTexture(GL_TEXTURE0
);
382 glBindTexture( GL_TEXTURE_2D
, gpipeline
.rgb_background
);
388 glDisable( GL_DEPTH_TEST
);
389 vg_lines_drawall( (float *)vg_pv
);
390 glViewport( 0,0, vg_window_x
, vg_window_y
);
395 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
396 glViewport( 0,0, vg_window_x
, vg_window_y
);
398 glDisable( GL_DEPTH_TEST
);
399 glClearColor( 0.11f
, 0.35f
, 0.37f
, 1.0f
);
400 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
);
402 #ifndef SR_NETWORK_TEST
409 else if( sv_scene
== 1 )
411 physics_test_render();
413 else if( sv_scene
== 2 )
415 anim_test_render( &tex_characters
);
420 static void run_light_widget( struct light_widget
*lw
)
422 struct ui_checkbox c1
= { .data
=&lw
->enabled
};
424 ui_checkbox( &ui_global_ctx
, &c1
);
428 struct ui_slider_vector
429 colour
= { .min
=0.0f
, .max
=2.0f
, .len
=3, .data
=lw
->colour
},
430 dir
= { .min
=-VG_PIf
, .max
=VG_PIf
, .len
=2, .data
=lw
->dir
};
432 ui_slider_vector( &ui_global_ctx
, &colour
);
433 ui_global_ctx
.cursor
[1] += 4;
434 ui_slider_vector( &ui_global_ctx
, &dir
);
438 static void run_debug_info(void)
442 snprintf( buf
, 40, "%.2fm/s", v3_length( player
.rb
.v
) );
443 gui_text( (ui_px
[2]){ 0, 0 }, buf
, 1, k_text_align_left
);
445 snprintf( buf
, 40, "%.2f %.2f %.2f m/s",
446 player
.a
[0], player
.a
[1], player
.a
[2] );
447 gui_text( (ui_px
[2]){ 0, 20 }, buf
, 1, k_text_align_left
);
449 snprintf( buf
, 40, "pos %.2f %.2f %.2f",
450 player
.rb
.co
[0], player
.rb
.co
[1], player
.rb
.co
[2] );
451 gui_text( (ui_px
[2]){ 0, 40 }, buf
, 1, k_text_align_left
);
453 if( vg_gamepad_ready
)
455 for( int i
=0; i
<6; i
++ )
457 snprintf( buf
, 40, "%.2f", vg_gamepad
.axes
[i
] );
458 gui_text( (ui_px
[2]){ 0, (i
+3)*20 }, buf
, 1, k_text_align_left
);
463 gui_text( (ui_px
[2]){ 0, 60 },
464 "Gamepad not ready", 1, k_text_align_left
);
472 ui_global_ctx
.cursor
[0] = 10;
473 ui_global_ctx
.cursor
[1] = 10;
474 ui_global_ctx
.cursor
[2] = 200;
475 ui_global_ctx
.cursor
[3] = 20;
477 struct ub_world_lighting
*wl
= &gpipeline
.ub_world_lighting
;
478 struct ui_slider_vector
479 s5
= { .min
=0.0f
, .max
=2.0f
, .len
=3, .data
=wl
->g_ambient_colour
};
482 s8
= { .min
=0.0f
, .max
=2.0f
, .data
= &gpipeline
.shadow_spread
},
483 s9
= { .min
=0.0f
, .max
=25.0f
, .data
= &gpipeline
.shadow_length
};
485 for( int i
=0; i
<3; i
++ )
486 run_light_widget( &gpipeline
.widgets
[i
] );
488 gui_text( ui_global_ctx
.cursor
, "Ambient", 1, 0 );
489 ui_global_ctx
.cursor
[1] += 16;
490 ui_slider_vector( &ui_global_ctx
, &s5
);
492 gui_text( ui_global_ctx
.cursor
, "Shadows", 1, 0 );
493 ui_global_ctx
.cursor
[1] += 16;
494 ui_slider( &ui_global_ctx
, &s8
);
495 ui_slider( &ui_global_ctx
, &s9
);
497 gui_text( ui_global_ctx
.cursor
, "Misc", 1, 0 );
498 ui_global_ctx
.cursor
[1] += 16;
499 struct ui_checkbox c1
= {.data
= &wl
->g_light_preview
};
500 ui_checkbox( &ui_global_ctx
, &c1
);
502 render_update_lighting_ub();
505 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
508 render_world_routes_ui();
510 //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
512 audio_debug_soundscapes();
516 static double last_b_press
= 0.0;
518 double localtime
= vg_time
- last_b_press
;
520 world_routes_ui_updatetime( 0, localtime
);
521 world_routes_ui_draw( 0, (v4f
){ 1.0f
,0.0f
,1.0f
,1.0f
}, 9.0f
);
523 if( glfwGetKey(vg_window
,GLFW_KEY_B
) )
524 world_routes_ui_notch( 0, localtime
);
526 if( vg_time
-last_b_press
> 1.0 )
527 if( glfwGetKey(vg_window
,GLFW_KEY_N
) )
529 last_b_press
= vg_time
;
530 world_routes_ui_newseg( 0 );
533 static double last_m_press
;
534 if( vg_time
-last_m_press
> 1.0 )
535 if( glfwGetKey( vg_window
, GLFW_KEY_M
) )
537 last_m_press
= vg_time
;
539 vg_info( "start: %u\n",world
.routes
.routes
[0].ui
.segment_count
);
540 for( int i
=0; i
<world
.routes
.routes
[0].ui
.segment_count
; i
++ )
541 world_routes_ui_popfirst(0);
543 vg_info( "new: %u\n",world
.routes
.routes
[0].ui
.segment_count
);