2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 #include "world_gate.h"
13 #include "shaders/vblend.h"
16 #include "shaders/scene_route.h"
17 #include "shaders/routeui.h"
21 void world_routes_local_set_record( world_instance
*world
, ent_route
*route
,
24 vg_success( " NEW LAP TIME: %f\n", lap_time
);
26 if( route
->official_track_id
!= 0xffffffff ){
27 double time_centiseconds
= lap_time
* 100.0;
28 if( time_centiseconds
> (float)0xfffe ) /* skill issue */
31 highscore_record temp
;
32 temp
.trackid
= route
->official_track_id
;
33 temp
.datetime
= time(NULL
);
36 temp
.time
= time_centiseconds
;
39 highscores_push_record( &temp
);
42 struct track_info
*ti
= &track_infos
[ route
->official_track_id
];
45 if( ti
->achievement_id
){
47 steam_set_achievement( ti
->achievement_id
);
48 steam_store_achievements();
53 vg_warn( "There is no associated track for this record...\n" );
58 VG_STATIC
void world_routes_clear( world_instance
*world
)
60 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
61 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
62 route
->active_checkpoint
= 0xffffffff;
65 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_gate
); i
++ ){
66 ent_gate
*rg
= mdl_arritm( &world
->ent_gate
, i
);
67 rg
->timing_version
= 0;
68 rg
->timing_time
= 0.0;
71 world_global
.current_run_version
+= 4;
72 world_global
.last_use
= 0.0;
75 VG_STATIC
void world_routes_time_lap( world_instance
*world
, ent_route
*route
)
77 vg_info( "------- time lap %s -------\n",
78 mdl_pstr(&world
->meta
,route
->pstr_name
) );
80 double total_time
= 0.0;
84 for( u32 i
=0; i
<route
->checkpoints_count
; i
++ ){
85 u32 cpid
= route
->checkpoints_start
+(i
+route
->active_checkpoint
);
86 cpid
= cpid
% route
->checkpoints_count
;
88 ent_checkpoint
*cp
= mdl_arritm( &world
->ent_checkpoint
, cpid
);
89 ent_gate
*rg
= mdl_arritm( &world
->ent_gate
, cp
->gate_index
);
90 rg
= mdl_arritm( &world
->ent_gate
, rg
->target
);
93 total_time
= rg
->timing_time
;
95 if( last_version
+1 != rg
->timing_version
)
99 last_version
= rg
->timing_version
;
100 vg_info( "%u %f\n", rg
->timing_version
, rg
->timing_time
);
103 vg_info( "%u %f\n", world_global
.current_run_version
, world_global
.time
);
105 if( validated
&& (world_global
.current_run_version
== last_version
+1)){
106 total_time
= world_global
.time
- total_time
;
107 world_routes_local_set_record( world
, route
, total_time
);
109 vg_info( "----------------------------\n" );
113 * When going through a gate this is called for bookkeeping purposes
115 VG_STATIC
void world_routes_activate_entry_gate( world_instance
*world
,
118 ent_gate
*dest
= mdl_arritm( &world
->ent_gate
, rg
->target
);
120 world_global
.last_use
= world_global
.time
;
122 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
123 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
125 u32 active_prev
= route
->active_checkpoint
;
126 route
->active_checkpoint
= 0xffffffff;
128 for( u32 j
=0; j
<4; j
++ ){
129 if( dest
->routes
[j
] == i
){
130 for( u32 k
=0; k
<route
->checkpoints_count
; k
++ ){
131 ent_checkpoint
*cp
= mdl_arritm( &world
->ent_checkpoint
,
132 route
->checkpoints_start
+k
);
134 ent_gate
*gk
= mdl_arritm( &world
->ent_gate
, cp
->gate_index
);
135 gk
= mdl_arritm( &world
->ent_gate
, gk
->target
);
137 route
->active_checkpoint
= k
;
138 world_routes_time_lap( world
, route
);
147 dest
->timing_version
= world_global
.current_run_version
;
148 dest
->timing_time
= world_global
.time
;
150 world_global
.current_run_version
++;
153 /* draw lines along the paths */
154 VG_STATIC
void world_routes_debug( world_instance
*world
)
156 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route_node
); i
++ ){
157 ent_route_node
*rn
= mdl_arritm(&world
->ent_route_node
,i
);
158 vg_line_pt3( rn
->co
, 0.25f
, VG__WHITE
);
161 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
162 ent_route
*route
= mdl_arritm(&world
->ent_route
, i
);
164 u32 colours
[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
168 if( route
->active_checkpoint
!= 0xffffffff ){
169 cc
= colours
[i
%vg_list_size(colours
)];
172 for( int i
=0; i
<route
->checkpoints_count
; i
++ ){
173 int i0
= route
->checkpoints_start
+i
,
174 i1
= route
->checkpoints_start
+((i
+1)%route
->checkpoints_count
);
176 ent_checkpoint
*c0
= mdl_arritm(&world
->ent_checkpoint
, i0
),
177 *c1
= mdl_arritm(&world
->ent_checkpoint
, i1
);
179 ent_gate
*start_gate
= mdl_arritm( &world
->ent_gate
, c0
->gate_index
);
180 ent_gate
*end_gate
= mdl_arritm( &world
->ent_gate
, c1
->gate_index
);
183 v3_copy( start_gate
->co
[1], p0
);
185 for( int j
=0; j
<c0
->path_count
; j
++ ){
186 ent_path_index
*index
= mdl_arritm( &world
->ent_path_index
,
189 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
,
192 v3_copy( rn
->co
, p1
);
193 vg_line( p0
, p1
, cc
);
197 v3_copy( end_gate
->co
[0], p1
);
198 vg_line( p0
, p1
, cc
);
203 VG_STATIC
void world_routes_place_curve( world_instance
*world
,
204 v4f h
[3], v3f n0
, v3f n2
)
210 float total_length
= 0.0f
,
214 v3_copy( h
[0], last
);
215 for( int it
=0; it
<128; it
++ ){
216 t
= (float)(it
+1) * (1.0f
/128.0f
);
217 eval_bezier3( h
[0], h
[1], h
[2], t
, p
);
218 total_length
+= v3_dist( p
, last
);
222 float patch_size
= 4.0f
,
223 patch_count
= ceilf( total_length
/ patch_size
);
226 v3_copy( h
[0], last
);
228 for( int it
=0; it
<128; it
++ ){
229 float const k_sample_dist
= 0.0025f
,
232 eval_bezier3( h
[0], h
[1], h
[2], t
, p
);
233 eval_bezier3( h
[0], h
[1], h
[2], t
+k_sample_dist
, pd
);
235 travel_length
+= v3_dist( p
, last
);
237 float mod
= k_sample_dist
/ v3_dist( p
, pd
);
241 v3_muls( n0
, -(1.0f
-t
), up
);
242 v3_muladds( up
, n2
, -t
, up
);
246 v3_cross( up
, v0
, right
);
247 v3_normalize( right
);
249 float cur_x
= (1.0f
-t
)*h
[0][3] + t
*h
[2][3];
251 v3f sc
, sa
, sb
, down
;
252 v3_muladds( p
, right
, cur_x
* k_line_width
, sc
);
253 v3_muladds( sc
, up
, 1.5f
, sc
);
254 v3_muladds( sc
, right
, k_line_width
*0.95f
, sa
);
255 v3_muladds( sc
, right
, 0.0f
, sb
);
256 v3_muls( up
, -1.0f
, down
);
262 int resa
= ray_world( world
, sa
, down
, &ha
),
263 resb
= ray_world( world
, sb
, down
, &hb
);
266 struct world_surface
*surfa
= ray_hit_surface( world
, &ha
),
267 *surfb
= ray_hit_surface( world
, &hb
);
269 if( (surfa
->info
.flags
& k_material_flag_skate_surface
) &&
270 (surfb
->info
.flags
& k_material_flag_skate_surface
) )
274 float gap
= vg_fractf(cur_x
*0.5f
)*0.02f
;
276 v3_muladds( ha
.pos
, up
, 0.06f
+gap
, va
.co
);
277 v3_muladds( hb
.pos
, up
, 0.06f
+gap
, vb
.co
);
279 scene_vert_pack_norm( &va
, up
);
280 scene_vert_pack_norm( &vb
, up
);
282 float t1
= (travel_length
/ total_length
) * patch_count
;
288 scene_push_vert( world
->scene_lines
, &va
);
289 scene_push_vert( world
->scene_lines
, &vb
);
292 /* Connect them with triangles */
293 scene_push_tri( world
->scene_lines
, (u32
[3]){
294 last_valid
+0-2, last_valid
+1-2, last_valid
+2-2} );
295 scene_push_tri( world
->scene_lines
, (u32
[3]){
296 last_valid
+1-2, last_valid
+3-2, last_valid
+2-2} );
299 last_valid
= world
->scene_lines
->vertex_count
;
318 VG_STATIC
void world_routes_create_mesh( world_instance
*world
, u32 route_id
)
320 ent_route
*route
= mdl_arritm( &world
->ent_route
, route_id
);
323 for( int i
=0; i
<route
->checkpoints_count
; i
++ ){
324 int i0
= route
->checkpoints_start
+i
,
325 i1
= route
->checkpoints_start
+((i
+1)%route
->checkpoints_count
);
327 ent_checkpoint
*c0
= mdl_arritm(&world
->ent_checkpoint
, i0
),
328 *c1
= mdl_arritm(&world
->ent_checkpoint
, i1
);
330 ent_gate
*start_gate
= mdl_arritm( &world
->ent_gate
, c0
->gate_index
);
331 start_gate
= mdl_arritm( &world
->ent_gate
, start_gate
->target
);
333 ent_gate
*end_gate
= mdl_arritm( &world
->ent_gate
, c1
->gate_index
),
334 *collector
= mdl_arritm( &world
->ent_gate
, end_gate
->target
);
338 v3_add( (v3f
){0.0f
,0.1f
,0.0f
}, start_gate
->co
[0], p
[0] );
339 p
[0][3] = start_gate
->ref_count
;
340 p
[0][3] -= (float)start_gate
->ref_total
* 0.5f
;
341 start_gate
->ref_count
++;
343 if( !c0
->path_count
)
346 /* this is so that we get nice flow through the gates */
347 v3f temp_alignments
[2];
348 ent_gate
*both
[] = { start_gate
, end_gate
};
350 for( int j
=0; j
<2; j
++ ){
351 int pi
= c0
->path_start
+ ((j
==1)? c0
->path_count
-1: 0);
353 ent_path_index
*index
= mdl_arritm( &world
->ent_path_index
, pi
);
354 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
,
357 v3_sub( rn
->co
, both
[j
]->co
[0], v0
);
358 float d
= v3_dot( v0
, both
[j
]->to_world
[2] );
360 v3_muladds( both
[j
]->co
[0], both
[j
]->to_world
[2], d
,
361 temp_alignments
[j
] );
362 v3_add( (v3f
){0.0f
,0.1f
,0.0f
}, temp_alignments
[j
], temp_alignments
[j
]);
366 for( int j
=0; j
<c0
->path_count
; j
++ ){
367 ent_path_index
*index
= mdl_arritm( &world
->ent_path_index
,
369 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
,
371 if( j
==0 || j
==c0
->path_count
-1 )
373 v3_copy( temp_alignments
[0], p
[1] );
375 v3_copy( temp_alignments
[1], p
[1] );
377 v3_copy( rn
->co
, p
[1] );
379 p
[1][3] = rn
->ref_count
;
380 p
[1][3] -= (float)rn
->ref_total
* 0.5f
;
383 if( j
+1 < c0
->path_count
){
384 index
= mdl_arritm( &world
->ent_path_index
,
385 c0
->path_start
+j
+1 );
386 rn
= mdl_arritm( &world
->ent_route_node
, index
->index
);
388 if( j
+1 == c0
->path_count
-1 )
389 v3_lerp( p
[1], temp_alignments
[1], 0.5f
, p
[2] );
391 v3_lerp( p
[1], rn
->co
, 0.5f
, p
[2] );
393 p
[2][3] = rn
->ref_count
;
394 p
[2][3] -= (float)rn
->ref_total
* 0.5f
;
397 v3_copy( end_gate
->co
[0], p
[2] );
398 v3_add( (v3f
){0.0f
,0.1f
,0.0f
}, p
[2], p
[2] );
399 p
[2][3] = collector
->ref_count
;
401 if( i
== route
->checkpoints_count
-1)
404 p
[2][3] -= (float)collector
->ref_total
* 0.5f
;
405 //collector->ref_count ++;
408 /* p0,p1,p2 bezier patch is complete
409 * --------------------------------------*/
410 v3f surf0
, surf2
, n0
, n2
;
412 if( bh_closest_point( world
->geo_bh
, p
[0], surf0
, 5.0f
) == -1 )
413 v3_add( (v3f
){0.0f
,-0.1f
,0.0f
}, p
[0], surf0
);
415 if( bh_closest_point( world
->geo_bh
, p
[2], surf2
, 5.0f
) == -1 )
416 v3_add( (v3f
){0.0f
,-0.1f
,0.0f
}, p
[2], surf2
);
418 v3_sub( surf0
, p
[0], n0
);
419 v3_sub( surf2
, p
[2], n2
);
423 world_routes_place_curve( world
, p
, n0
, n2
);
426 v4_copy( p
[2], p
[0] );
430 scene_copy_slice( world
->scene_lines
, &route
->sm
);
434 * Create the strips of colour that run through the world along course paths
436 VG_STATIC
void world_routes_generate( world_instance
*world
)
438 vg_info( "Generating route meshes\n" );
439 world
->scene_lines
= scene_init( world_global
.generic_heap
, 200000, 300000 );
441 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
442 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, i
);
447 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route_node
); i
++ ){
448 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
, i
);
453 for( u32 k
=0; k
<mdl_arrcount(&world
->ent_route
); k
++ ){
454 ent_route
*route
= mdl_arritm( &world
->ent_route
, k
);
456 for( int i
=0; i
<route
->checkpoints_count
; i
++ ){
457 int i0
= route
->checkpoints_start
+i
,
458 i1
= route
->checkpoints_start
+((i
+1)%route
->checkpoints_count
);
460 ent_checkpoint
*c0
= mdl_arritm(&world
->ent_checkpoint
, i0
),
461 *c1
= mdl_arritm(&world
->ent_checkpoint
, i1
);
463 ent_gate
*start_gate
= mdl_arritm( &world
->ent_gate
, c0
->gate_index
);
464 start_gate
= mdl_arritm( &world
->ent_gate
, start_gate
->target
);
465 start_gate
->ref_total
++;
467 if( !c0
->path_count
)
470 for( int j
=0; j
<c0
->path_count
; j
++ ){
471 ent_path_index
*index
= mdl_arritm( &world
->ent_path_index
,
473 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
,
480 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ )
481 world_routes_create_mesh( world
, i
);
483 vg_acquire_thread_sync();
485 scene_upload( world
->scene_lines
, &world
->mesh_route_lines
);
487 vg_release_thread_sync();
488 vg_linear_del( world_global
.generic_heap
, world
->scene_lines
);
490 world_routes_clear( world
);
493 /* load all routes from model header */
494 VG_STATIC
void world_routes_ent_init( world_instance
*world
)
496 vg_info( "Initializing routes\n" );
498 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
499 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, i
);
500 for( u32 j
=0; j
<4; j
++ ){
501 gate
->routes
[j
] = 0xffff;
505 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
506 ent_route
*route
= mdl_arritm(&world
->ent_route
,i
);
507 mdl_transform_m4x3( &route
->transform
, route
->board_transform
);
509 route
->official_track_id
= 0xffffffff;
510 for( u32 j
=0; j
<vg_list_size(track_infos
); j
++ ){
511 if( !strcmp(track_infos
[j
].name
,
512 mdl_pstr(&world
->meta
,route
->pstr_name
))){
513 route
->official_track_id
= j
;
517 for( u32 j
=0; j
<route
->checkpoints_count
; j
++ ){
518 u32 id
= route
->checkpoints_start
+ j
;
519 ent_checkpoint
*cp
= mdl_arritm(&world
->ent_checkpoint
,id
);
521 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, cp
->gate_index
);
523 for( u32 k
=0; k
<4; k
++ ){
524 if( gate
->routes
[k
] == 0xffff ){
530 if( gate
->type
== k_gate_type_teleport
){
531 gate
= mdl_arritm(&world
->ent_gate
, gate
->target
);
533 for( u32 k
=0; k
<4; k
++ ){
534 if( gate
->routes
[k
] == i
){
535 vg_error( "already assigned route to gate\n" );
538 if( gate
->routes
[k
] == 0xffff ){
547 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
548 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, i
);
550 vg_info( "ROUTES :: %hu %hu %hu %hu\n", gate
->routes
[0],
556 world_routes_clear( world
);
560 * -----------------------------------------------------------------------------
562 * -----------------------------------------------------------------------------
565 VG_STATIC
void world_routes_init(void)
567 world_global
.current_run_version
= 200;
568 world_global
.time
= RESET_MAX_TIME
*2.0;
569 world_global
.last_use
= 0.0;
571 shader_scene_route_register();
572 shader_routeui_register();
575 VG_STATIC
void world_routes_update( world_instance
*world
)
577 world_global
.time
+= vg
.time_delta
;
579 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
580 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
582 int target
= route
->active_checkpoint
== 0xffffffff? 0: 1;
583 route
->factive
= vg_lerpf( route
->factive
, target
, 0.6f
*vg
.time_delta
);
587 VG_STATIC
void bind_terrain_noise(void);
588 VG_STATIC
void world_bind_light_array( world_instance
*world
,
589 GLuint shader
, GLuint location
,
591 VG_STATIC
void world_bind_light_index( world_instance
*world
,
592 GLuint shader
, GLuint location
,
595 VG_STATIC
void render_world_routes( world_instance
*world
, camera
*cam
,
598 m4x3f identity_matrix
;
599 m4x3_identity( identity_matrix
);
601 shader_scene_route_use();
602 shader_scene_route_uTexGarbage(0);
603 world_link_lighting_ub( world
, _shader_scene_route
.id
);
604 world_bind_position_texture( world
, _shader_scene_route
.id
,
605 _uniform_scene_route_g_world_depth
, 2 );
606 world_bind_light_array( world
, _shader_scene_route
.id
,
607 _uniform_scene_route_uLightsArray
, 3 );
608 world_bind_light_index( world
, _shader_scene_route
.id
,
609 _uniform_scene_route_uLightsIndex
, 4 );
610 bind_terrain_noise();
612 shader_scene_route_uPv( cam
->mtx
.pv
);
613 shader_scene_route_uPvmPrev( cam
->mtx_prev
.pv
);
614 shader_scene_route_uMdl( identity_matrix
);
615 shader_scene_route_uCamera( cam
->transform
[3] );
616 shader_scene_route_uBoard0( TEMP_BOARD_0
);
617 shader_scene_route_uBoard1( TEMP_BOARD_1
);
619 mesh_bind( &world
->mesh_route_lines
);
621 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
622 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
625 v3_lerp( (v3f
){0.7f
,0.7f
,0.7f
}, route
->colour
, route
->factive
, colour
);
626 colour
[3] = route
->factive
*0.2f
;
628 shader_scene_route_uColour( colour
);
629 mdl_draw_submesh( &route
->sm
);
632 shader_model_gate_use();
633 shader_model_gate_uPv( cam
->mtx
.pv
);
634 shader_model_gate_uCam( cam
->pos
);
635 shader_model_gate_uTime( vg
.time
*0.25f
);
636 shader_model_gate_uInvRes( (v2f
){
637 1.0f
/ (float)vg
.window_x
,
638 1.0f
/ (float)vg
.window_y
});
640 mesh_bind( &world_global
.mesh_gate
);
642 /* skip writing into the motion vectors for this */
643 glDrawBuffers( 1, (GLenum
[]){ GL_COLOR_ATTACHMENT0
} );
645 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
646 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
648 if( route
->active_checkpoint
!= 0xffffffff ){
650 float brightness
= 0.3f
+ world
->ub_lighting
.g_day_phase
;
651 v3_muls( route
->colour
, brightness
, colour
);
652 colour
[3] = 1.0f
-route
->factive
;
654 shader_model_gate_uColour( colour
);
656 u32 next
= route
->active_checkpoint
+1+layer_depth
;
657 next
= next
% route
->checkpoints_count
;
658 next
+= route
->checkpoints_start
;
660 ent_checkpoint
*cp
= mdl_arritm( &world
->ent_checkpoint
, next
);
661 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, cp
->gate_index
);
662 shader_model_gate_uMdl( gate
->to_world
);
664 for( u32 j
=0; j
<4; j
++ ){
665 if( gate
->routes
[j
] == i
){
666 mdl_draw_submesh( &world_global
.sm_gate_marker
[j
] );
672 glDrawBuffers( 2, (GLenum
[]){ GL_COLOR_ATTACHMENT0
, GL_COLOR_ATTACHMENT1
} );
675 #endif /* ROUTES_H */