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"
20 VG_STATIC
void world_routes_local_set_record( world_instance
*world
,
21 u32 route
, double lap_time
)
24 vg_success( " NEW LAP TIME: %f\n", lap_time
);
26 if( pr
->track_id
!= 0xffffffff )
28 double time_centiseconds
= lap_time
* 100.0;
29 if( time_centiseconds
> (float)0xfffe )
32 highscore_record temp
;
33 temp
.trackid
= pr
->track_id
;
34 temp
.datetime
= time(NULL
);
37 temp
.time
= time_centiseconds
;
39 highscores_push_record( &temp
);
41 struct track_info
*pti
= &track_infos
[ pr
->track_id
];
44 if( pti
->achievement_id
)
46 steam_set_achievement( pti
->achievement_id
);
47 steam_store_achievements();
52 vg_warn( "There is no associated track for this record...\n" );
55 vg_warn( "set_record unimplemented\n" );
59 VG_STATIC
void world_routes_clear( world_instance
*world
)
61 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
62 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
63 route
->active_checkpoint
= 0xffffffff;
66 world_global
.current_run_version
+= 4;
67 world_global
.last_use
= 0.0;
71 * When going through a gate this is called for bookkeeping purposes
73 VG_STATIC
void world_routes_activate_entry_gate( world_instance
*world
,
76 ent_gate
*dest
= mdl_arritm( &world
->ent_gate
, rg
->target
);
78 world_global
.last_use
= world_global
.time
;
79 rg
->timing_version
= world_global
.current_run_version
;
80 rg
->timing_time
= world_global
.time
;
82 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
83 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
85 u32 active_prev
= route
->active_checkpoint
;
86 route
->active_checkpoint
= 0xffffffff;
88 for( u32 j
=0; j
<4; j
++ ){
89 if( dest
->routes
[j
] == i
){
90 for( u32 k
=0; k
<route
->checkpoints_count
; k
++ ){
91 ent_checkpoint
*cp
= mdl_arritm( &world
->ent_checkpoint
,
92 route
->checkpoints_start
+k
);
94 ent_gate
*gk
= mdl_arritm( &world
->ent_gate
, cp
->gate_index
);
96 route
->active_checkpoint
= k
;
105 world_global
.current_run_version
++;
107 dest
->timing_version
= world_global
.current_run_version
;
108 dest
->timing_time
= world_global
.time
;
109 world_global
.current_run_version
++;
112 /* draw lines along the paths */
113 VG_STATIC
void world_routes_debug( world_instance
*world
)
115 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route_node
); i
++ ){
116 ent_route_node
*rn
= mdl_arritm(&world
->ent_route_node
,i
);
117 vg_line_pt3( rn
->co
, 0.25f
, VG__WHITE
);
120 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
121 ent_route
*route
= mdl_arritm(&world
->ent_route
, i
);
123 u32 colours
[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
127 if( route
->active_checkpoint
!= 0xffffffff ){
128 cc
= colours
[i
%vg_list_size(colours
)];
131 for( int i
=0; i
<route
->checkpoints_count
; i
++ ){
132 int i0
= route
->checkpoints_start
+i
,
133 i1
= route
->checkpoints_start
+((i
+1)%route
->checkpoints_count
);
135 ent_checkpoint
*c0
= mdl_arritm(&world
->ent_checkpoint
, i0
),
136 *c1
= mdl_arritm(&world
->ent_checkpoint
, i1
);
138 ent_gate
*start_gate
= mdl_arritm( &world
->ent_gate
, c0
->gate_index
);
139 ent_gate
*end_gate
= mdl_arritm( &world
->ent_gate
, c1
->gate_index
);
142 v3_copy( start_gate
->co
[1], p0
);
144 for( int j
=0; j
<c0
->path_count
; j
++ ){
145 ent_path_index
*index
= mdl_arritm( &world
->ent_path_index
,
148 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
,
151 v3_copy( rn
->co
, p1
);
152 vg_line( p0
, p1
, cc
);
156 v3_copy( end_gate
->co
[0], p1
);
157 vg_line( p0
, p1
, cc
);
162 VG_STATIC
void world_routes_place_curve( world_instance
*world
,
163 v4f h
[4], v3f n0
, v3f n2
)
169 float total_length
= 0.0f
,
173 v3_copy( h
[0], last
);
174 for( int it
=0; it
<128; it
++ ){
175 t
= (float)(it
+1) * (1.0f
/128.0f
);
176 eval_bezier3( h
[0], h
[1], h
[2], t
, p
);
177 total_length
+= v3_dist( p
, last
);
181 float patch_size
= 4.0f
,
182 patch_count
= ceilf( total_length
/ patch_size
);
185 v3_copy( h
[0], last
);
187 for( int it
=0; it
<128; it
++ ){
188 float const k_sample_dist
= 0.0025f
,
191 eval_bezier3( h
[0], h
[1], h
[2], t
, p
);
192 eval_bezier3( h
[0], h
[1], h
[2], t
+k_sample_dist
, pd
);
194 travel_length
+= v3_dist( p
, last
);
196 float mod
= k_sample_dist
/ v3_dist( p
, pd
);
200 v3_muls( n0
, -(1.0f
-t
), up
);
201 v3_muladds( up
, n2
, -t
, up
);
205 v3_cross( up
, v0
, right
);
206 v3_normalize( right
);
208 float cur_x
= (1.0f
-t
)*h
[0][3] + t
*h
[2][3];
210 v3f sc
, sa
, sb
, down
;
211 v3_muladds( p
, right
, cur_x
* k_line_width
, sc
);
212 v3_muladds( sc
, up
, 1.5f
, sc
);
213 v3_muladds( sc
, right
, k_line_width
*0.95f
, sa
);
214 v3_muladds( sc
, right
, 0.0f
, sb
);
215 v3_muls( up
, -1.0f
, down
);
220 if( ray_world( world
, sa
, down
, &ha
) &&
221 ray_world( world
, sb
, down
, &hb
))
225 v3_muladds( ha
.pos
, up
, 0.06f
, va
.co
);
226 v3_muladds( hb
.pos
, up
, 0.06f
, vb
.co
);
228 scene_vert_pack_norm( &va
, up
);
229 scene_vert_pack_norm( &vb
, up
);
231 float t1
= (travel_length
/ total_length
) * patch_count
;
237 scene_push_vert( world
->scene_lines
, &va
);
238 scene_push_vert( world
->scene_lines
, &vb
);
241 /* Connect them with triangles */
242 scene_push_tri( world
->scene_lines
, (u32
[3]){
243 last_valid
+0-2, last_valid
+1-2, last_valid
+2-2} );
244 scene_push_tri( world
->scene_lines
, (u32
[3]){
245 last_valid
+1-2, last_valid
+3-2, last_valid
+2-2} );
248 last_valid
= world
->scene_lines
->vertex_count
;
264 VG_STATIC
void world_routes_create_mesh( world_instance
*world
, u32 route_id
)
266 ent_route
*route
= mdl_arritm( &world
->ent_route
, route_id
);
269 for( int i
=0; i
<route
->checkpoints_count
; i
++ ){
270 int i0
= route
->checkpoints_start
+i
,
271 i1
= route
->checkpoints_start
+((i
+1)%route
->checkpoints_count
);
273 ent_checkpoint
*c0
= mdl_arritm(&world
->ent_checkpoint
, i0
),
274 *c1
= mdl_arritm(&world
->ent_checkpoint
, i1
);
276 ent_gate
*start_gate
= mdl_arritm( &world
->ent_gate
, c0
->gate_index
);
277 start_gate
= mdl_arritm( &world
->ent_gate
, start_gate
->target
);
279 ent_gate
*end_gate
= mdl_arritm( &world
->ent_gate
, c1
->gate_index
);
283 v3_add( (v3f
){0.0f
,0.1f
,0.0f
}, start_gate
->co
[0], p
[0] );
284 p
[0][3] = start_gate
->ref_count
;
285 p
[0][3] -= (float)start_gate
->ref_total
* 0.5f
;
286 start_gate
->ref_count
++;
288 if( !c0
->path_count
)
291 /* this is so that we get nice flow through the gates */
292 v3f temp_alignments
[2];
293 ent_gate
*both
[] = { start_gate
, end_gate
};
295 for( int j
=0; j
<2; j
++ ){
296 int pi
= c0
->path_start
+ ((j
==1)? c0
->path_count
-1: 0);
298 ent_path_index
*index
= mdl_arritm( &world
->ent_path_index
, pi
);
299 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
,
302 v3_sub( rn
->co
, both
[j
]->co
[0], v0
);
303 float d
= v3_dot( v0
, both
[j
]->to_world
[2] );
305 v3_muladds( both
[j
]->co
[0], both
[j
]->to_world
[2], d
,
306 temp_alignments
[j
] );
307 v3_add( (v3f
){0.0f
,0.1f
,0.0f
}, temp_alignments
[j
], temp_alignments
[j
]);
311 for( int j
=0; j
<c0
->path_count
; j
++ ){
312 ent_path_index
*index
= mdl_arritm( &world
->ent_path_index
,
314 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
,
316 if( j
==0 || j
==c0
->path_count
-1 )
318 v3_copy( temp_alignments
[0], p
[1] );
320 v3_copy( temp_alignments
[1], p
[1] );
322 v3_copy( rn
->co
, p
[1] );
324 p
[1][3] = rn
->ref_count
;
325 p
[1][3] -= (float)rn
->ref_total
* 0.5f
;
328 if( j
+1 < c0
->path_count
){
329 index
= mdl_arritm( &world
->ent_path_index
,
330 c0
->path_start
+j
+1 );
331 rn
= mdl_arritm( &world
->ent_route_node
, index
->index
);
333 if( j
+1 == c0
->path_count
-1 )
334 v3_lerp( p
[1], temp_alignments
[1], 0.5f
, p
[2] );
336 v3_lerp( p
[1], rn
->co
, 0.5f
, p
[2] );
338 p
[2][3] = rn
->ref_count
;
339 p
[2][3] -= (float)rn
->ref_total
* 0.5f
;
342 v3_copy( end_gate
->co
[0], p
[2] );
343 v3_add( (v3f
){0.0f
,0.1f
,0.0f
}, p
[2], p
[2] );
344 p
[2][3] = end_gate
->ref_count
;
345 p
[2][3] -= (float)end_gate
->ref_total
* 0.5f
;
346 end_gate
->ref_count
++;
349 /* p0,p1,p2 bezier patch is complete
350 * --------------------------------------*/
351 v3f surf0
, surf2
, n0
, n2
;
353 if( bh_closest_point( world
->geo_bh
, p
[0], surf0
, 5.0f
) == -1 )
354 v3_add( (v3f
){0.0f
,-0.1f
,0.0f
}, p
[0], surf0
);
356 if( bh_closest_point( world
->geo_bh
, p
[2], surf2
, 5.0f
) == -1 )
357 v3_add( (v3f
){0.0f
,-0.1f
,0.0f
}, p
[2], surf2
);
359 v3_sub( surf0
, p
[0], n0
);
360 v3_sub( surf2
, p
[2], n2
);
364 world_routes_place_curve( world
, p
, n0
, n2
);
367 v4_copy( p
[2], p
[0] );
371 scene_copy_slice( world
->scene_lines
, &route
->sm
);
375 * Create the strips of colour that run through the world along course paths
377 VG_STATIC
void world_routes_generate( world_instance
*world
)
379 vg_info( "Generating route meshes\n" );
380 world
->scene_lines
= scene_init( world_global
.generic_heap
, 200000, 300000 );
382 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
383 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, i
);
388 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route_node
); i
++ ){
389 ent_route_node
*rn
= mdl_arritm( &world
->ent_route
, i
);
394 for( u32 k
=0; k
<mdl_arrcount(&world
->ent_route
); k
++ ){
395 ent_route
*route
= mdl_arritm( &world
->ent_route
, k
);
397 for( int i
=0; i
<route
->checkpoints_count
; i
++ ){
398 int i0
= route
->checkpoints_start
+i
,
399 i1
= route
->checkpoints_start
+((i
+1)%route
->checkpoints_count
);
401 ent_checkpoint
*c0
= mdl_arritm(&world
->ent_checkpoint
, i0
),
402 *c1
= mdl_arritm(&world
->ent_checkpoint
, i1
);
404 ent_gate
*start_gate
= mdl_arritm( &world
->ent_gate
, c0
->gate_index
);
405 start_gate
= mdl_arritm( &world
->ent_gate
, start_gate
->target
);
407 ent_gate
*end_gate
= mdl_arritm( &world
->ent_gate
, c1
->gate_index
);
408 start_gate
->ref_total
++;
410 if( !c0
->path_count
)
413 for( int j
=0; j
<c0
->path_count
; j
++ ){
414 ent_path_index
*index
= mdl_arritm( &world
->ent_path_index
,
416 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
,
420 if( j
+1 < c0
->path_count
){
423 end_gate
->ref_total
++;
429 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ )
430 world_routes_create_mesh( world
, i
);
432 vg_acquire_thread_sync();
434 scene_upload( world
->scene_lines
, &world
->mesh_route_lines
);
436 vg_release_thread_sync();
437 vg_linear_del( world_global
.generic_heap
, world
->scene_lines
);
440 /* load all routes from model header */
441 VG_STATIC
void world_routes_ent_init( world_instance
*world
)
443 vg_info( "Initializing routes\n" );
445 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
446 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, i
);
447 for( u32 j
=0; j
<4; j
++ ){
448 gate
->routes
[j
] = 0xffff;
452 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
453 ent_route
*route
= mdl_arritm(&world
->ent_route
,i
);
455 for( u32 j
=0; j
<route
->checkpoints_count
; j
++ ){
456 u32 id
= route
->checkpoints_start
+ j
;
457 ent_checkpoint
*cp
= mdl_arritm(&world
->ent_checkpoint
,id
);
459 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, cp
->gate_index
);
461 for( u32 k
=0; k
<4; k
++ ){
462 if( gate
->routes
[k
] == 0xffff ){
468 if( gate
->type
== k_gate_type_teleport
){
469 gate
= mdl_arritm(&world
->ent_gate
, gate
->target
);
471 for( u32 k
=0; k
<4; k
++ ){
472 if( gate
->routes
[k
] == 0xffff ){
481 world_routes_clear( world
);
485 * -----------------------------------------------------------------------------
487 * -----------------------------------------------------------------------------
490 VG_STATIC
void world_routes_init(void)
492 world_global
.current_run_version
= 200;
493 world_global
.time
= RESET_MAX_TIME
*2.0;
494 world_global
.last_use
= 0.0;
496 shader_scene_route_register();
497 shader_routeui_register();
500 VG_STATIC
void world_routes_update( world_instance
*world
)
502 world_global
.time
+= vg
.time_delta
;
504 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
505 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
507 int target
= route
->active_checkpoint
== 0xffffffff? 0: 1;
508 route
->factive
= vg_lerpf( route
->factive
, target
, 0.6f
*vg
.time_delta
);
512 VG_STATIC
void bind_terrain_noise(void);
513 VG_STATIC
void world_bind_light_array( world_instance
*world
,
514 GLuint shader
, GLuint location
,
516 VG_STATIC
void world_bind_light_index( world_instance
*world
,
517 GLuint shader
, GLuint location
,
520 VG_STATIC
void render_world_routes( world_instance
*world
, camera
*cam
)
522 m4x3f identity_matrix
;
523 m4x3_identity( identity_matrix
);
525 shader_scene_route_use();
526 shader_scene_route_uTexGarbage(0);
527 world_link_lighting_ub( world
, _shader_scene_route
.id
);
528 world_bind_position_texture( world
, _shader_scene_route
.id
,
529 _uniform_scene_route_g_world_depth
, 2 );
530 world_bind_light_array( world
, _shader_scene_route
.id
,
531 _uniform_scene_route_uLightsArray
, 3 );
532 world_bind_light_index( world
, _shader_scene_route
.id
,
533 _uniform_scene_route_uLightsIndex
, 4 );
534 bind_terrain_noise();
536 shader_scene_route_uPv( cam
->mtx
.pv
);
537 shader_scene_route_uPvmPrev( cam
->mtx_prev
.pv
);
538 shader_scene_route_uMdl( identity_matrix
);
539 shader_scene_route_uCamera( cam
->transform
[3] );
540 shader_scene_route_uBoard0( TEMP_BOARD_0
);
541 shader_scene_route_uBoard1( TEMP_BOARD_1
);
543 mesh_bind( &world
->mesh_route_lines
);
545 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
546 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
549 v3_lerp( (v3f
){0.7f
,0.7f
,0.7f
}, route
->colour
, route
->factive
, colour
);
550 colour
[3] = route
->factive
*0.2f
;
552 shader_scene_route_uColour( colour
);
553 mdl_draw_submesh( &route
->sm
);
556 shader_model_gate_use();
557 shader_model_gate_uPv( cam
->mtx
.pv
);
558 shader_model_gate_uCam( cam
->pos
);
559 shader_model_gate_uTime( vg
.time
*0.25f
);
560 shader_model_gate_uInvRes( (v2f
){
561 1.0f
/ (float)vg
.window_x
,
562 1.0f
/ (float)vg
.window_y
});
564 mesh_bind( &world_global
.mesh_gate
);
566 /* skip writing into the motion vectors for this */
567 glDrawBuffers( 1, (GLenum
[]){ GL_COLOR_ATTACHMENT0
} );
569 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
570 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
572 if( route
->active_checkpoint
!= 0xffffffff ){
574 float brightness
= 0.3f
+ world
->ub_lighting
.g_day_phase
;
575 v3_muls( route
->colour
, brightness
, colour
);
576 colour
[3] = 1.0f
-route
->factive
;
578 shader_model_gate_uColour( colour
);
580 u32 next
= route
->checkpoints_start
+
581 (route
->active_checkpoint
+1) % route
->checkpoints_count
;
583 ent_checkpoint
*cp
= mdl_arritm( &world
->ent_checkpoint
, next
);
584 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, cp
->gate_index
);
585 shader_model_gate_uMdl( gate
->to_world
);
587 for( u32 j
=0; j
<4; j
++ ){
588 if( gate
->routes
[j
] == i
){
589 mdl_draw_submesh( &world_global
.sm_gate_marker
[j
] );
595 glDrawBuffers( 2, (GLenum
[]){ GL_COLOR_ATTACHMENT0
, GL_COLOR_ATTACHMENT1
} );
598 #endif /* ROUTES_H */