2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 #include "world_gate.h"
12 #include "pointcloud.h"
15 #include "shaders/vblend.h"
18 #include "shaders/scene_route.h"
19 #include "shaders/routeui.h"
23 void world_routes_local_set_record( world_instance
*world
, ent_route
*route
,
26 vg_success( " NEW LAP TIME: %f\n", lap_time
);
28 if( route
->official_track_id
!= 0xffffffff ){
29 double time_centiseconds
= lap_time
* 100.0;
30 if( time_centiseconds
> (float)0xfffe ) /* skill issue */
33 highscore_record temp
;
34 temp
.trackid
= route
->official_track_id
;
35 temp
.datetime
= time(NULL
);
38 temp
.time
= time_centiseconds
;
41 highscores_push_record( &temp
);
44 struct track_info
*ti
= &track_infos
[ route
->official_track_id
];
47 if( ti
->achievement_id
){
49 steam_set_achievement( ti
->achievement_id
);
50 steam_store_achievements();
55 vg_warn( "There is no associated track for this record...\n" );
60 VG_STATIC
void world_routes_clear( world_instance
*world
)
62 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
63 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
64 route
->active_checkpoint
= 0xffff;
67 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_gate
); i
++ ){
68 ent_gate
*rg
= mdl_arritm( &world
->ent_gate
, i
);
69 rg
->timing_version
= 0;
70 rg
->timing_time
= 0.0;
73 world_global
.current_run_version
+= 4;
74 world_global
.last_use
= 0.0;
77 VG_STATIC
void world_routes_time_lap( world_instance
*world
, ent_route
*route
)
79 vg_info( "------- time lap %s -------\n",
80 mdl_pstr(&world
->meta
,route
->pstr_name
) );
82 double start_time
= 0.0;
87 for( u32 i
=0; i
<route
->checkpoints_count
; i
++ ){
88 u32 cpid
= (i
+route
->active_checkpoint
) % route
->checkpoints_count
;
89 cpid
+= route
->checkpoints_start
;
91 ent_checkpoint
*cp
= mdl_arritm( &world
->ent_checkpoint
, cpid
);
92 ent_gate
*rg
= mdl_arritm( &world
->ent_gate
, cp
->gate_index
);
93 rg
= mdl_arritm( &world
->ent_gate
, rg
->target
);
96 route
->timing_base
= rg
->timing_time
;
100 start_time
= rg
->timing_time
;
102 if( last_version
+1 == rg
->timing_version
) valid_count
++;
103 else valid_count
= 0;
106 last_version
= rg
->timing_version
;
107 vg_info( "%u %f\n", rg
->timing_version
, rg
->timing_time
);
110 if( world_global
.current_run_version
== last_version
+1 ){
113 if( route
->checkpoints_count
== 1 ){
114 route
->timing_base
= world_global
.time
;
117 else valid_count
= 0;
119 vg_info( "%u %f\n", world_global
.current_run_version
, world_global
.time
);
121 if( valid_count
==route
->checkpoints_count
){
122 double lap_time
= world_global
.time
- start_time
;
123 world_routes_local_set_record( world
, route
, lap_time
);
126 route
->valid_checkpoints
= valid_count
+1;
128 vg_info( "valid: %u\n", valid_count
);
129 vg_info( "----------------------------\n" );
133 * When going through a gate this is called for bookkeeping purposes
135 VG_STATIC
void world_routes_activate_entry_gate( world_instance
*world
,
138 world_global
.last_use
= world_global
.time
;
140 /* disable all routes and leave the world */
141 if( rg
->type
== k_gate_type_nonlocel
){
142 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
143 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
144 route
->active_checkpoint
= 0xffff;
149 ent_gate
*dest
= mdl_arritm( &world
->ent_gate
, rg
->target
);
151 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
152 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
154 u32 active_prev
= route
->active_checkpoint
;
155 route
->active_checkpoint
= 0xffff;
157 for( u32 j
=0; j
<4; j
++ ){
158 if( dest
->routes
[j
] == i
){
159 for( u32 k
=0; k
<route
->checkpoints_count
; k
++ ){
160 ent_checkpoint
*cp
= mdl_arritm( &world
->ent_checkpoint
,
161 route
->checkpoints_start
+k
);
163 ent_gate
*gk
= mdl_arritm( &world
->ent_gate
, cp
->gate_index
);
164 gk
= mdl_arritm( &world
->ent_gate
, gk
->target
);
166 route
->active_checkpoint
= k
;
167 world_routes_time_lap( world
, route
);
176 dest
->timing_version
= world_global
.current_run_version
;
177 dest
->timing_time
= world_global
.time
;
179 world_global
.current_run_version
++;
182 /* draw lines along the paths */
183 VG_STATIC
void world_routes_debug( world_instance
*world
)
185 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route_node
); i
++ ){
186 ent_route_node
*rn
= mdl_arritm(&world
->ent_route_node
,i
);
187 vg_line_pt3( rn
->co
, 0.25f
, VG__WHITE
);
190 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
191 ent_route
*route
= mdl_arritm(&world
->ent_route
, i
);
193 u32 colours
[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
197 if( route
->active_checkpoint
!= 0xffff ){
198 cc
= colours
[i
%vg_list_size(colours
)];
201 for( int i
=0; i
<route
->checkpoints_count
; i
++ ){
202 int i0
= route
->checkpoints_start
+i
,
203 i1
= route
->checkpoints_start
+((i
+1)%route
->checkpoints_count
);
205 ent_checkpoint
*c0
= mdl_arritm(&world
->ent_checkpoint
, i0
),
206 *c1
= mdl_arritm(&world
->ent_checkpoint
, i1
);
208 ent_gate
*start_gate
= mdl_arritm( &world
->ent_gate
, c0
->gate_index
);
209 ent_gate
*end_gate
= mdl_arritm( &world
->ent_gate
, c1
->gate_index
);
212 v3_copy( start_gate
->co
[1], p0
);
214 for( int j
=0; j
<c0
->path_count
; j
++ ){
215 ent_path_index
*index
= mdl_arritm( &world
->ent_path_index
,
218 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
,
221 v3_copy( rn
->co
, p1
);
222 vg_line( p0
, p1
, cc
);
226 v3_copy( end_gate
->co
[0], p1
);
227 vg_line( p0
, p1
, cc
);
233 void world_routes_place_curve( world_instance
*world
,
234 v4f h
[3], v3f n0
, v3f n2
, scene_context
*scene
,
235 struct pointcloud_vert
*points
,
236 u32
*point_count
, v4f colour
)
242 float total_length
= 0.0f
,
246 v3_copy( h
[0], last
);
247 for( int it
=0; it
<128; it
++ ){
248 t
= (float)(it
+1) * (1.0f
/128.0f
);
249 eval_bezier3( h
[0], h
[1], h
[2], t
, p
);
250 total_length
+= v3_dist( p
, last
);
254 float patch_size
= 4.0f
,
255 patch_count
= ceilf( total_length
/ patch_size
);
258 v3_copy( h
[0], last
);
260 for( int it
=0; it
<128; it
++ ){
261 float const k_sample_dist
= 0.0025f
,
264 eval_bezier3( h
[0], h
[1], h
[2], t
, p
);
265 eval_bezier3( h
[0], h
[1], h
[2], t
+k_sample_dist
, pd
);
267 travel_length
+= v3_dist( p
, last
);
269 float mod
= k_sample_dist
/ v3_dist( p
, pd
);
273 v3_muls( n0
, -(1.0f
-t
), up
);
274 v3_muladds( up
, n2
, -t
, up
);
278 v3_cross( up
, v0
, right
);
279 v3_normalize( right
);
281 float cur_x
= (1.0f
-t
)*h
[0][3] + t
*h
[2][3];
283 v3f sc
, sa
, sb
, down
;
284 v3_muladds( p
, right
, cur_x
* k_line_width
, sc
);
285 v3_muladds( sc
, up
, 1.5f
, sc
);
286 v3_muladds( sc
, right
, k_line_width
*0.95f
, sa
);
287 v3_muladds( sc
, right
, 0.0f
, sb
);
288 v3_muls( up
, -1.0f
, down
);
294 int resa
= ray_world( world
, sa
, down
, &ha
),
295 resb
= ray_world( world
, sb
, down
, &hb
);
297 for( u32 j
=0; j
<10; j
++ ){
298 if( *point_count
< POINTCLOUD_POINTS
){
299 struct pointcloud_vert
*vert
= &points
[*point_count
];
300 *point_count
= (*point_count
) + 1;
302 v3f sample
, jitter
, pcpoint
;
303 vg_rand_sphere( jitter
);
304 v3_muladds( ha
.pos
, jitter
, 8.0f
, sample
);
306 if( bh_closest_point( world
->geo_bh
, sample
, pcpoint
, 10.0f
) == -1 ){
307 v3_copy( sample
, pcpoint
);
311 v3_sub( world
->scene_geo
.bbx
[1], world
->scene_geo
.bbx
[0], vol
);
312 v3_sub( pcpoint
, world
->scene_geo
.bbx
[0], pos
);
313 v3_div( pos
, vol
, pos
);
315 for( u32 i
=0; i
<3; i
++ ){
316 vert
->pos
[i
] = vg_clampf(pos
[i
], 0.0f
,1.0f
) * 65535.0f
;
317 vert
->norm
[i
] = ha
.normal
[i
] * 127.0f
;
320 float dist
= 1.0f
-(v3_length(jitter
));
322 for( u32 i
=0; i
<4; i
++ ){
323 vert
->colour
[i
] = colour
[i
] * 255.0f
* dist
*dist
;
329 struct world_surface
*surfa
= ray_hit_surface( world
, &ha
),
330 *surfb
= ray_hit_surface( world
, &hb
);
332 if( (surfa
->info
.flags
& k_material_flag_skate_target
) &&
333 (surfb
->info
.flags
& k_material_flag_skate_target
) )
337 float gap
= vg_fractf(cur_x
*0.5f
)*0.02f
;
339 v3_muladds( ha
.pos
, up
, 0.06f
+gap
, va
.co
);
340 v3_muladds( hb
.pos
, up
, 0.06f
+gap
, vb
.co
);
342 scene_vert_pack_norm( &va
, up
);
343 scene_vert_pack_norm( &vb
, up
);
345 float t1
= (travel_length
/ total_length
) * patch_count
;
351 scene_push_vert( scene
, &va
);
352 scene_push_vert( scene
, &vb
);
355 /* Connect them with triangles */
356 scene_push_tri( scene
, (u32
[3]){
357 last_valid
+0-2, last_valid
+1-2, last_valid
+2-2} );
358 scene_push_tri( scene
, (u32
[3]){
359 last_valid
+1-2, last_valid
+3-2, last_valid
+2-2} );
362 last_valid
= scene
->vertex_count
;
382 void world_routes_create_mesh( world_instance
*world
, u32 route_id
,
384 struct pointcloud_vert
*points
,
387 ent_route
*route
= mdl_arritm( &world
->ent_route
, route_id
);
389 colour
[0] = route
->colour
[0] * 255.0f
;
390 colour
[1] = route
->colour
[1] * 255.0f
;
391 colour
[2] = route
->colour
[2] * 255.0f
;
392 colour
[3] = route
->colour
[3] * 255.0f
;
396 for( int i
=0; i
<route
->checkpoints_count
; i
++ ){
397 int i0
= route
->checkpoints_start
+i
,
398 i1
= route
->checkpoints_start
+((i
+1)%route
->checkpoints_count
);
400 ent_checkpoint
*c0
= mdl_arritm(&world
->ent_checkpoint
, i0
),
401 *c1
= mdl_arritm(&world
->ent_checkpoint
, i1
);
403 ent_gate
*start_gate
= mdl_arritm( &world
->ent_gate
, c0
->gate_index
);
404 start_gate
= mdl_arritm( &world
->ent_gate
, start_gate
->target
);
406 ent_gate
*end_gate
= mdl_arritm( &world
->ent_gate
, c1
->gate_index
),
407 *collector
= mdl_arritm( &world
->ent_gate
, end_gate
->target
);
411 v3_add( (v3f
){0.0f
,0.1f
,0.0f
}, start_gate
->co
[0], p
[0] );
412 p
[0][3] = start_gate
->ref_count
;
413 p
[0][3] -= (float)start_gate
->route_count
* 0.5f
;
414 start_gate
->ref_count
++;
416 if( !c0
->path_count
)
419 /* this is so that we get nice flow through the gates */
420 v3f temp_alignments
[2];
421 ent_gate
*both
[] = { start_gate
, end_gate
};
423 for( int j
=0; j
<2; j
++ ){
424 int pi
= c0
->path_start
+ ((j
==1)? c0
->path_count
-1: 0);
426 ent_path_index
*index
= mdl_arritm( &world
->ent_path_index
, pi
);
427 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
,
430 v3_sub( rn
->co
, both
[j
]->co
[0], v0
);
431 float d
= v3_dot( v0
, both
[j
]->to_world
[2] );
433 v3_muladds( both
[j
]->co
[0], both
[j
]->to_world
[2], d
,
434 temp_alignments
[j
] );
435 v3_add( (v3f
){0.0f
,0.1f
,0.0f
}, temp_alignments
[j
], temp_alignments
[j
]);
439 for( int j
=0; j
<c0
->path_count
; j
++ ){
440 ent_path_index
*index
= mdl_arritm( &world
->ent_path_index
,
442 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
,
444 if( j
==0 || j
==c0
->path_count
-1 )
446 v3_copy( temp_alignments
[0], p
[1] );
448 v3_copy( temp_alignments
[1], p
[1] );
450 v3_copy( rn
->co
, p
[1] );
452 p
[1][3] = rn
->ref_count
;
453 p
[1][3] -= (float)rn
->ref_total
* 0.5f
;
456 if( j
+1 < c0
->path_count
){
457 index
= mdl_arritm( &world
->ent_path_index
,
458 c0
->path_start
+j
+1 );
459 rn
= mdl_arritm( &world
->ent_route_node
, index
->index
);
461 if( j
+1 == c0
->path_count
-1 )
462 v3_lerp( p
[1], temp_alignments
[1], 0.5f
, p
[2] );
464 v3_lerp( p
[1], rn
->co
, 0.5f
, p
[2] );
466 p
[2][3] = rn
->ref_count
;
467 p
[2][3] -= (float)rn
->ref_total
* 0.5f
;
470 v3_copy( end_gate
->co
[0], p
[2] );
471 v3_add( (v3f
){0.0f
,0.1f
,0.0f
}, p
[2], p
[2] );
472 p
[2][3] = collector
->ref_count
;
474 if( i
== route
->checkpoints_count
-1)
477 p
[2][3] -= (float)collector
->route_count
* 0.5f
;
478 //collector->ref_count ++;
481 /* p0,p1,p2 bezier patch is complete
482 * --------------------------------------*/
483 v3f surf0
, surf2
, n0
, n2
;
485 if( bh_closest_point( world
->geo_bh
, p
[0], surf0
, 5.0f
) == -1 )
486 v3_add( (v3f
){0.0f
,-0.1f
,0.0f
}, p
[0], surf0
);
488 if( bh_closest_point( world
->geo_bh
, p
[2], surf2
, 5.0f
) == -1 )
489 v3_add( (v3f
){0.0f
,-0.1f
,0.0f
}, p
[2], surf2
);
491 v3_sub( surf0
, p
[0], n0
);
492 v3_sub( surf2
, p
[2], n2
);
496 world_routes_place_curve( world
, p
, n0
, n2
, sc
, points
, point_count
,
500 v4_copy( p
[2], p
[0] );
504 scene_copy_slice( sc
, &route
->sm
);
508 struct world_surface
*world_tri_index_surface( world_instance
*world
,
511 * Create the strips of colour that run through the world along course paths
513 VG_STATIC
void world_routes_generate( world_instance
*world
)
515 vg_info( "Generating route meshes\n" );
518 vg_rand_seed( 2000 );
519 vg_async_item
*call
= scene_alloc_async( &world
->scene_lines
,
520 &world
->mesh_route_lines
,
522 vg_async_item
*call1
=
523 vg_async_alloc( sizeof(struct pointcloud_vert
)*POINTCLOUD_POINTS
);
524 u32 generated_points
= 0;
525 struct pointcloud_vert
*cloud_data
= call1
->payload
;
527 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
528 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, i
);
530 gate
->route_count
= 0;
533 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route_node
); i
++ ){
534 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
, i
);
539 for( u32 k
=0; k
<mdl_arrcount(&world
->ent_route
); k
++ ){
540 ent_route
*route
= mdl_arritm( &world
->ent_route
, k
);
542 for( int i
=0; i
<route
->checkpoints_count
; i
++ ){
543 int i0
= route
->checkpoints_start
+i
,
544 i1
= route
->checkpoints_start
+((i
+1)%route
->checkpoints_count
);
546 ent_checkpoint
*c0
= mdl_arritm(&world
->ent_checkpoint
, i0
),
547 *c1
= mdl_arritm(&world
->ent_checkpoint
, i1
);
549 ent_gate
*start_gate
= mdl_arritm( &world
->ent_gate
, c0
->gate_index
);
550 start_gate
= mdl_arritm( &world
->ent_gate
, start_gate
->target
);
551 start_gate
->route_count
++;
553 if( !c0
->path_count
)
556 for( int j
=0; j
<c0
->path_count
; j
++ ){
557 ent_path_index
*index
= mdl_arritm( &world
->ent_path_index
,
559 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
,
566 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
567 world_routes_create_mesh( world
, i
, &world
->scene_lines
,
568 cloud_data
, &generated_points
);
573 vg_info( "Executing awesome algorithm!\n" );
578 v3_sub( world
->scene_geo
.bbx
[1], world
->scene_geo
.bbx
[0], vol
);
579 v3_div( (v3f
){1.0f
,1.0f
,1.0f
}, vol
, vol
);
581 u8 colour
[] = { 80,80,80,255 };
582 v3f light_dir
= {0.3f
,0.8f
,0.1f
};
583 v3_normalize( light_dir
);
585 for( u32 k
=1; k
<=10; k
++ ){
586 f32 rate
= 50.0f
* (1.0f
/(float)k
);
588 for( u32 i
=0; i
<world
->scene_geo
.indice_count
/3; i
++ ){
589 u32
*tri
= &world
->scene_geo
.arrindices
[i
*3];
590 struct world_surface
*surf
= world_tri_index_surface( world
, tri
[0] );
592 if( surf
->info
.shader
== k_shader_boundary
||
593 surf
->info
.shader
== k_shader_invisible
) continue;
595 if( !(surf
->info
.flags
& k_material_flag_preview_visibile
) ) continue;
597 scene_vert
*va
= &world
->scene_geo
.arrvertices
[tri
[0]],
598 *vb
= &world
->scene_geo
.arrvertices
[tri
[1]],
599 *vc
= &world
->scene_geo
.arrvertices
[tri
[2]];
602 v3_sub( vb
->co
, va
->co
, v0
);
603 v3_sub( vc
->co
, va
->co
, v1
);
604 v3_cross( v0
, v1
, vn
);
605 if( vn
[1] < 0.0f
) continue;
607 accum
+= v3_length(vn
)*0.5f
;
611 while( accum
> rate
){
614 if( generated_points
>= POINTCLOUD_POINTS
){
615 goto too_many_points
;
618 v2f co
= { vg_randf64(), vg_randf64() };
619 if( v2_length2(co
) > 0.5f
){
625 v3_muls( v0
, co
[0], pt
);
626 v3_muladds( pt
, v1
, co
[1], pt
);
627 v3_add( va
->co
, pt
, pt
);
629 if( pt
[1] < world
->water
.height
) continue;
630 struct pointcloud_vert
*vert
= &cloud_data
[generated_points
++];
633 v3_sub( pt
, world
->scene_geo
.bbx
[0], pos
);
634 v3_mul( pos
, vol
, pos
);
636 for( u32 j
=0; j
<3; j
++ ){
637 vert
->pos
[j
] = vg_clampf(pos
[j
],0.0f
,1.0f
) * 65535.0f
;
638 vert
->norm
[j
] = vg_clampf(vn
[j
],-1.0f
,1.0f
) * 127.0f
;
641 f32 brightness
= vg_clampf( v3_dot( vn
, light_dir
), 0.0f
, 1.0f
);
643 v4f col
= {0.0f
,0.0f
,0.0f
,0.0f
};
644 if( surf
->info
.surface_prop
== k_surface_prop_wood
){
645 v4_copy( (v4f
){0.66f
,0.6f
,0.5f
}, col
);
647 else if( surf
->info
.surface_prop
== k_surface_prop_grass
){
648 v4_copy( (v4f
){0.42f
,0.5f
,0.33f
}, col
);
650 else if( surf
->info
.surface_prop
== k_surface_prop_concrete
){
651 v4_copy( (v4f
){0.57f
,0.57f
,0.55f
}, col
);
653 else if( surf
->info
.surface_prop
== k_surface_prop_metal
){
654 v4_copy( (v4f
){0.76f
,0.76f
,0.75f
}, col
);
657 v4_copy( (v4f
){0.2f
,0.2f
,0.2f
}, col
);
660 v4_copy( (v4f
){0.2f
,0.2f
,0.26f
}, col
);
661 v3_muls( col
, brightness
*0.25f
, col
);
663 for( u32 j
=0; j
<4; j
++ ){
664 vert
->colour
[j
] = col
[j
] * 255.0f
;
671 vg_info( "finished awesome algorithm! yipee (%u)!\n", generated_points
);
674 call1
->size
= generated_points
;
675 vg_async_dispatch( call
, async_scene_upload
);
676 vg_async_dispatch( call1
, async_pointcloud_sub
);
678 world_routes_clear( world
);
681 /* load all routes from model header */
682 VG_STATIC
void world_routes_ent_init( world_instance
*world
)
684 vg_info( "Initializing routes\n" );
686 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
687 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, i
);
688 for( u32 j
=0; j
<4; j
++ ){
689 gate
->routes
[j
] = 0xffff;
693 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
694 ent_route
*route
= mdl_arritm(&world
->ent_route
,i
);
695 mdl_transform_m4x3( &route
->transform
, route
->board_transform
);
697 route
->official_track_id
= 0xffffffff;
698 for( u32 j
=0; j
<vg_list_size(track_infos
); j
++ ){
699 if( !strcmp(track_infos
[j
].name
,
700 mdl_pstr(&world
->meta
,route
->pstr_name
))){
701 route
->official_track_id
= j
;
705 for( u32 j
=0; j
<route
->checkpoints_count
; j
++ ){
706 u32 id
= route
->checkpoints_start
+ j
;
707 ent_checkpoint
*cp
= mdl_arritm(&world
->ent_checkpoint
,id
);
709 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, cp
->gate_index
);
711 for( u32 k
=0; k
<4; k
++ ){
712 if( gate
->routes
[k
] == 0xffff ){
718 if( gate
->type
== k_gate_type_teleport
){
719 gate
= mdl_arritm(&world
->ent_gate
, gate
->target
);
721 for( u32 k
=0; k
<4; k
++ ){
722 if( gate
->routes
[k
] == i
){
723 vg_error( "already assigned route to gate\n" );
726 if( gate
->routes
[k
] == 0xffff ){
735 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
736 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, i
);
739 world_routes_clear( world
);
743 * -----------------------------------------------------------------------------
745 * -----------------------------------------------------------------------------
748 VG_STATIC
void world_routes_init(void)
750 world_global
.current_run_version
= 200;
751 world_global
.time
= 300.0;
752 world_global
.last_use
= 0.0;
754 shader_scene_route_register();
755 shader_routeui_register();
758 VG_STATIC
void world_routes_update( world_instance
*world
)
760 world_global
.time
+= vg
.time_delta
;
762 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
763 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
765 int target
= route
->active_checkpoint
== 0xffff? 0: 1;
766 route
->factive
= vg_lerpf( route
->factive
, target
, 0.6f
*vg
.time_delta
);
769 for( u32 i
=0; i
<world_global
.text_particle_count
; i
++ ){
770 struct text_particle
*particle
= &world_global
.text_particles
[i
];
771 rb_object_debug( &particle
->obj
, VG__RED
);
775 VG_STATIC
void world_routes_fixedupdate( world_instance
*world
)
779 for( u32 i
=0; i
<world_global
.text_particle_count
; i
++ ){
780 struct text_particle
*particle
= &world_global
.text_particles
[i
];
782 if( rb_global_has_space() ){
783 rb_ct
*buf
= rb_global_buffer();
785 int l
= rb_sphere__scene( particle
->obj
.rb
.to_world
,
786 &particle
->obj
.inf
.sphere
,
787 NULL
, &world
->rb_geo
.inf
.scene
, buf
);
789 for( int j
=0; j
<l
; j
++ ){
790 buf
[j
].rba
= &particle
->obj
.rb
;
791 buf
[j
].rbb
= &world
->rb_geo
.rb
;
794 rb_contact_count
+= l
;
798 rb_presolve_contacts( rb_contact_buffer
, rb_contact_count
);
800 for( int i
=0; i
<rb_contact_count
; i
++ ){
801 rb_contact_restitution( rb_contact_buffer
+i
, vg_randf64() );
804 for( int i
=0; i
<6; i
++ ){
805 rb_solve_contacts( rb_contact_buffer
, rb_contact_count
);
808 for( u32 i
=0; i
<world_global
.text_particle_count
; i
++ ){
809 struct text_particle
*particle
= &world_global
.text_particles
[i
];
810 rb_iter( &particle
->obj
.rb
);
813 for( u32 i
=0; i
<world_global
.text_particle_count
; i
++ ){
814 struct text_particle
*particle
= &world_global
.text_particles
[i
];
815 rb_update_transform( &particle
->obj
.rb
);
819 VG_STATIC
void bind_terrain_noise(void);
820 VG_STATIC
void world_bind_light_array( world_instance
*world
,
821 GLuint shader
, GLuint location
,
823 VG_STATIC
void world_bind_light_index( world_instance
*world
,
824 GLuint shader
, GLuint location
,
827 VG_STATIC
void world_routes_update_timer_texts( world_instance
*world
)
829 world_global
.timer_text_count
= 0;
831 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
832 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
834 if( route
->active_checkpoint
!= 0xffff ){
835 u32 next
= route
->active_checkpoint
+1;
836 next
= next
% route
->checkpoints_count
;
837 next
+= route
->checkpoints_start
;
839 ent_checkpoint
*cp
= mdl_arritm( &world
->ent_checkpoint
, next
);
840 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, cp
->gate_index
);
841 ent_gate
*dest
= mdl_arritm( &world
->ent_gate
, gate
->target
);
845 if( dest
->routes
[j
] == i
){
855 struct timer_text
*text
=
856 &world_global
.timer_texts
[ world_global
.timer_text_count
++ ];
861 if( route
->valid_checkpoints
>= route
->checkpoints_count
){
862 double lap_time
= world_global
.time
- route
->timing_base
,
863 time_centiseconds
= lap_time
* 100.0;
865 if( time_centiseconds
> (float)0xfffe ) time_centiseconds
= 0.0;
867 u16 centiseconds
= time_centiseconds
,
868 seconds
= centiseconds
/ 100,
869 minutes
= seconds
/ 60;
875 if( minutes
> 9 ) minutes
= 9;
879 highscore_intr( text
->text
, minutes
, 1, ' ' ); j
++;
880 text
->text
[j
++] = ':';
883 if( seconds
>= 10 || minutes
){
884 highscore_intr( text
->text
+j
, seconds
, 2, '0' ); j
+=2;
887 highscore_intr( text
->text
+j
, seconds
, 1, '0' ); j
++;
890 text
->text
[j
++] = '.';
891 highscore_intr( text
->text
+j
, centiseconds
, 1, '0' ); j
++;
892 text
->text
[j
] = '\0';
895 highscore_intr( text
->text
, route
->valid_checkpoints
, 1, ' ' );
897 highscore_intr( text
->text
+2, route
->checkpoints_count
+1, 1, ' ' );
898 text
->text
[3] = '\0';
901 float align_r
= font3d_string_width( &world_global
.font
, 0,
906 { -0.92f
, h0
, depth
},
907 { 0.92f
- align_r
, h0
, depth
},
908 { -0.92f
, h1
, depth
},
909 { 0.92f
- align_r
, h1
, depth
},
912 if( dest
->route_count
== 1 ){
913 positions
[0][0] = -align_r
*0.5f
;
914 positions
[0][1] = h1
;
917 m3x3_copy( gate
->to_world
, text
->transform
);
918 float ratio
= v3_length(text
->transform
[0]) /
919 v3_length(text
->transform
[1]);
921 m3x3_scale( text
->transform
, (v3f
){ size
, size
*ratio
, 0.1f
} );
922 m4x3_mulv( gate
->to_world
, positions
[j
], text
->transform
[3] );
927 VG_STATIC
void world_routes_fracture( world_instance
*world
, ent_gate
*gate
,
928 v3f imp_co
, v3f imp_v
)
930 world_global
.text_particle_count
= 0;
932 for( u32 i
=0; i
<world_global
.timer_text_count
; i
++ ){
933 struct timer_text
*text
= &world_global
.timer_texts
[i
];
935 if( text
->gate
!= gate
) continue;
938 m4x3_mul( gate
->transport
, text
->transform
, transform
);
942 m4x3_decompose( transform
, co
, q
, s
);
948 float brightness
= 0.3f
+ world
->ub_lighting
.g_day_phase
;
949 v3_muls( text
->route
->colour
, brightness
, colour
);
950 colour
[3] = 1.0f
-text
->route
->factive
;
952 for( u32 j
=0;; j
++ ){
953 char c
= text
->text
[j
];
956 ent_glyph
*glyph
= font3d_glyph( &world_global
.font
, 0, c
);
957 if( !glyph
) continue;
959 if( c
>= (u32
)'0' && c
<= (u32
)'9' && glyph
->indice_count
){
960 struct text_particle
*particle
=
961 &world_global
.text_particles
[world_global
.text_particle_count
++];
963 particle
->glyph
= glyph
;
964 v4_copy( colour
, particle
->colour
);
967 v2_muls( glyph
->size
, 0.5f
, origin
);
972 v3_add( offset
, origin
, world_co
);
973 m4x3_mulv( transform
, world_co
, world_co
);
975 float r
= vg_maxf( s
[0]*glyph
->size
[0], s
[1]*glyph
->size
[1] )*0.5f
;
977 m3x3_identity( particle
->mlocal
);
978 m3x3_scale( particle
->mlocal
, s
);
980 v3_muls( origin
, -1.0f
, particle
->mlocal
[3] );
982 v3_copy( world_co
, particle
->obj
.rb
.co
);
983 v3_muls( imp_v
, 1.0f
+vg_randf64(), particle
->obj
.rb
.v
);
984 particle
->obj
.rb
.v
[1] += 2.0f
;
986 v4_copy( q
, particle
->obj
.rb
.q
);
987 particle
->obj
.rb
.w
[0] = vg_randf64()*2.0f
-1.0f
;
988 particle
->obj
.rb
.w
[1] = vg_randf64()*2.0f
-1.0f
;
989 particle
->obj
.rb
.w
[2] = vg_randf64()*2.0f
-1.0f
;
991 particle
->obj
.type
= k_rb_shape_sphere
;
992 particle
->obj
.inf
.sphere
.radius
= r
*0.6f
;
994 rb_init_object( &particle
->obj
);
996 offset
[0] += glyph
->size
[0];
1001 VG_STATIC
void render_world_routes( world_instance
*world
, camera
*cam
,
1004 m4x3f identity_matrix
;
1005 m4x3_identity( identity_matrix
);
1007 shader_scene_route_use();
1008 shader_scene_route_uTexGarbage(0);
1009 world_link_lighting_ub( world
, _shader_scene_route
.id
);
1010 world_bind_position_texture( world
, _shader_scene_route
.id
,
1011 _uniform_scene_route_g_world_depth
, 2 );
1012 world_bind_light_array( world
, _shader_scene_route
.id
,
1013 _uniform_scene_route_uLightsArray
, 3 );
1014 world_bind_light_index( world
, _shader_scene_route
.id
,
1015 _uniform_scene_route_uLightsIndex
, 4 );
1016 bind_terrain_noise();
1018 shader_scene_route_uPv( cam
->mtx
.pv
);
1019 shader_scene_route_uPvmPrev( cam
->mtx_prev
.pv
);
1020 shader_scene_route_uMdl( identity_matrix
);
1021 shader_scene_route_uCamera( cam
->transform
[3] );
1023 mesh_bind( &world
->mesh_route_lines
);
1025 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
1026 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
1029 v3_lerp( (v3f
){0.7f
,0.7f
,0.7f
}, route
->colour
, route
->factive
, colour
);
1030 colour
[3] = route
->factive
*0.2f
;
1032 shader_scene_route_uColour( colour
);
1033 mdl_draw_submesh( &route
->sm
);
1037 * ---------------------------------------------------- */
1038 if( layer_depth
== 0 ){
1039 font3d_bind( &world_global
.font
, cam
);
1041 for( u32 i
=0; i
<world_global
.timer_text_count
; i
++ ){
1042 struct timer_text
*text
= &world_global
.timer_texts
[i
];
1045 float brightness
= 0.3f
+ world
->ub_lighting
.g_day_phase
;
1046 v3_muls( text
->route
->colour
, brightness
, colour
);
1047 colour
[3] = 1.0f
-text
->route
->factive
;
1049 shader_model_font_uColour( colour
);
1050 font3d_simple_draw( &world_global
.font
, 0, text
->text
,
1051 cam
, text
->transform
);
1054 shader_model_font_uOffset( (v4f
){0.0f
,0.0f
,0.0f
,1.0f
} );
1056 for( u32 i
=0; i
<world_global
.text_particle_count
; i
++ ){
1057 struct text_particle
*particle
= &world_global
.text_particles
[i
];
1061 m4x3_expand( particle
->mdl
, prev_mtx
);
1062 m4x4_mul( cam
->mtx_prev
.pv
, prev_mtx
, prev_mtx
);
1064 shader_model_font_uPvmPrev( prev_mtx
);
1068 rb_extrapolate( &particle
->obj
.rb
, model
[3], q
);
1071 m4x3_mul( model
, particle
->mlocal
, particle
->mdl
);
1072 shader_model_font_uMdl( particle
->mdl
);
1073 shader_model_font_uColour( particle
->colour
);
1075 mesh_drawn( particle
->glyph
->indice_start
,
1076 particle
->glyph
->indice_count
);
1081 * ---------------------------------------------------- */
1083 shader_model_gate_use();
1084 shader_model_gate_uPv( cam
->mtx
.pv
);
1085 shader_model_gate_uCam( cam
->pos
);
1086 shader_model_gate_uTime( vg
.time
*0.25f
);
1087 shader_model_gate_uInvRes( (v2f
){
1088 1.0f
/ (float)vg
.window_x
,
1089 1.0f
/ (float)vg
.window_y
});
1091 mesh_bind( &world_global
.mesh_gate
);
1093 /* skip writing into the motion vectors for this */
1094 glDrawBuffers( 1, (GLenum
[]){ GL_COLOR_ATTACHMENT0
} );
1096 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
1097 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
1099 if( route
->active_checkpoint
!= 0xffff ){
1101 float brightness
= 0.3f
+ world
->ub_lighting
.g_day_phase
;
1102 v3_muls( route
->colour
, brightness
, colour
);
1103 colour
[3] = 1.0f
-route
->factive
;
1105 shader_model_gate_uColour( colour
);
1107 u32 next
= route
->active_checkpoint
+1+layer_depth
;
1108 next
= next
% route
->checkpoints_count
;
1109 next
+= route
->checkpoints_start
;
1111 ent_checkpoint
*cp
= mdl_arritm( &world
->ent_checkpoint
, next
);
1112 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, cp
->gate_index
);
1113 shader_model_gate_uMdl( gate
->to_world
);
1115 for( u32 j
=0; j
<4; j
++ ){
1116 if( gate
->routes
[j
] == i
){
1117 mdl_draw_submesh( &world_global
.sm_gate_marker
[j
] );
1123 glDrawBuffers( 2, (GLenum
[]){ GL_COLOR_ATTACHMENT0
, GL_COLOR_ATTACHMENT1
} );
1126 #endif /* ROUTES_H */