2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
9 #include "world_routes.h"
10 #include "world_gate.h"
11 #include "world_load.h"
12 #include "highscores.h"
15 #include "pointcloud.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_static
.current_run_version
+= 4;
74 world_static
.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_static
.current_run_version
== last_version
+1 ){
113 if( route
->checkpoints_count
== 1 ){
114 route
->timing_base
= world_static
.time
;
117 else valid_count
= 0;
119 vg_info( "%u %f\n", world_static
.current_run_version
, world_static
.time
);
121 if( valid_count
==route
->checkpoints_count
){
122 double lap_time
= world_static
.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_static
.last_use
= world_static
.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_static
.current_run_version
;
177 dest
->timing_time
= world_static
.time
;
179 world_static
.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_pointcloud_spot( world_instance
*world
,
234 pointcloud_buffer
*pcbuf
,
235 v3f co
, f32 radius
, u32 samples
, v4f colour
)
238 v3_sub( pcbuf
->boundary
[1], pcbuf
->boundary
[0], inv_ext
);
239 v3_div( (v3f
){1.0f
,1.0f
,1.0f
}, inv_ext
, inv_ext
);
241 for( u32 j
=0; j
<samples
; j
++ ){
242 if( pcbuf
->count
>= pcbuf
->max
)
245 pointcloud_vert
*vert
= &pcbuf
->buf
[ pcbuf
->count
++ ];
247 v3f sample
, jitter
, point
;
248 vg_rand_sphere( jitter
);
249 v3_muladds( co
, jitter
, radius
, sample
);
251 if( bh_closest_point( world
->geo_bh
, sample
, point
, radius
*1.5f
) == -1 ){
252 v3_copy( sample
, point
);
256 v3_sub( point
, pcbuf
->boundary
[0], pos
);
257 v3_mul( pos
, inv_ext
, pos
);
259 for( u32 i
=0; i
<3; i
++ ){
260 vert
->pos
[i
] = (pos
[i
]-0.5f
) * 32767.0f
;
263 float dist
= 1.0f
-(v3_length(jitter
));
265 for( u32 i
=0; i
<4; i
++ ){
266 vert
->colour
[i
] = colour
[i
] * 255.0f
* dist
*dist
;
280 void world_routes_pointcloud_tower( world_instance
*world
,
281 pointcloud_buffer
*pcbuf
,
282 v3f co
, f32 radius
, f32 height
,
283 u32 samples
, v4f colour
)
286 v3_sub( pcbuf
->boundary
[1], pcbuf
->boundary
[0], inv_ext
);
287 v3_div( (v3f
){1.0f
,1.0f
,1.0f
}, inv_ext
, inv_ext
);
289 for( u32 j
=0; j
<samples
; j
++ ){
290 if( pcbuf
->count
>= pcbuf
->max
)
293 pointcloud_vert
*vert
= &pcbuf
->buf
[ pcbuf
->count
++ ];
296 point
[0] = vg_randf64()*2.0f
-1.0f
;
298 point
[2] = vg_randf64()*2.0f
-1.0f
;
299 v3_normalize( point
);
300 v3_muls( point
, sqrtf(vg_randf64()), point
);
302 f32 h
= vg_randf64();
303 point
[1] = h
*h
*h
*height
;
307 v3_add( point
, co
, point
);
308 v3_sub( point
, pcbuf
->boundary
[0], point
);
309 v3_mul( point
, inv_ext
, point
);
312 for( u32 i
=0; i
<3; i
++ ){
313 vert
->pos
[i
] = (point
[i
]-0.5f
) * 32767.0f
;
316 for( u32 i
=0; i
<4; i
++ ){
317 vert
->colour
[i
] = colour
[i
] * 255.0f
;
323 void world_routes_place_curve( world_instance
*world
, ent_route
*route
,
324 v4f h
[3], v3f n0
, v3f n2
, scene_context
*scene
,
325 pointcloud_buffer
*pcbuf
)
331 float total_length
= 0.0f
,
335 v3_copy( h
[0], last
);
336 for( int it
=0; it
<128; it
++ ){
337 t
= (float)(it
+1) * (1.0f
/128.0f
);
338 eval_bezier3( h
[0], h
[1], h
[2], t
, p
);
339 total_length
+= v3_dist( p
, last
);
343 float patch_size
= 4.0f
,
344 patch_count
= ceilf( total_length
/ patch_size
);
347 v3_copy( h
[0], last
);
349 for( int it
=0; it
<128; it
++ ){
350 float const k_sample_dist
= 0.0025f
,
353 eval_bezier3( h
[0], h
[1], h
[2], t
, p
);
354 eval_bezier3( h
[0], h
[1], h
[2], t
+k_sample_dist
, pd
);
356 travel_length
+= v3_dist( p
, last
);
358 float mod
= k_sample_dist
/ v3_dist( p
, pd
);
362 v3_muls( n0
, -(1.0f
-t
), up
);
363 v3_muladds( up
, n2
, -t
, up
);
367 v3_cross( up
, v0
, right
);
368 v3_normalize( right
);
370 float cur_x
= (1.0f
-t
)*h
[0][3] + t
*h
[2][3];
372 v3f sc
, sa
, sb
, down
;
373 v3_muladds( p
, right
, cur_x
* k_line_width
, sc
);
374 v3_muladds( sc
, up
, 1.5f
, sc
);
375 v3_muladds( sc
, right
, k_line_width
*0.95f
, sa
);
376 v3_muladds( sc
, right
, 0.0f
, sb
);
377 v3_muls( up
, -1.0f
, down
);
383 int resa
= ray_world( world
, sa
, down
, &ha
),
384 resb
= ray_world( world
, sb
, down
, &hb
);
387 world_routes_pointcloud_spot( world
, pcbuf
, ha
.pos
,
388 12.0f
, 10, route
->colour
);
392 struct world_surface
*surfa
= ray_hit_surface( world
, &ha
),
393 *surfb
= ray_hit_surface( world
, &hb
);
395 if( (surfa
->info
.flags
& k_material_flag_skate_target
) &&
396 (surfb
->info
.flags
& k_material_flag_skate_target
) )
400 float gap
= vg_fractf(cur_x
*0.5f
)*0.02f
;
402 v3_muladds( ha
.pos
, up
, 0.06f
+gap
, va
.co
);
403 v3_muladds( hb
.pos
, up
, 0.06f
+gap
, vb
.co
);
405 scene_vert_pack_norm( &va
, up
);
406 scene_vert_pack_norm( &vb
, up
);
408 float t1
= (travel_length
/ total_length
) * patch_count
;
414 scene_push_vert( scene
, &va
);
415 scene_push_vert( scene
, &vb
);
418 /* Connect them with triangles */
419 scene_push_tri( scene
, (u32
[3]){
420 last_valid
+0-2, last_valid
+1-2, last_valid
+2-2} );
421 scene_push_tri( scene
, (u32
[3]){
422 last_valid
+1-2, last_valid
+3-2, last_valid
+2-2} );
425 last_valid
= scene
->vertex_count
;
444 VG_STATIC
void world_routes_gen_meshes( world_instance
*world
, u32 route_id
,
446 pointcloud_buffer
*pcbuf
)
448 ent_route
*route
= mdl_arritm( &world
->ent_route
, route_id
);
450 colour
[0] = route
->colour
[0] * 255.0f
;
451 colour
[1] = route
->colour
[1] * 255.0f
;
452 colour
[2] = route
->colour
[2] * 255.0f
;
453 colour
[3] = route
->colour
[3] * 255.0f
;
457 for( int i
=0; i
<route
->checkpoints_count
; i
++ ){
458 int i0
= route
->checkpoints_start
+i
,
459 i1
= route
->checkpoints_start
+((i
+1)%route
->checkpoints_count
);
461 ent_checkpoint
*c0
= mdl_arritm(&world
->ent_checkpoint
, i0
),
462 *c1
= mdl_arritm(&world
->ent_checkpoint
, i1
);
464 ent_gate
*start_gate
= mdl_arritm( &world
->ent_gate
, c0
->gate_index
);
465 start_gate
= mdl_arritm( &world
->ent_gate
, start_gate
->target
);
467 ent_gate
*end_gate
= mdl_arritm( &world
->ent_gate
, c1
->gate_index
),
468 *collector
= mdl_arritm( &world
->ent_gate
, end_gate
->target
);
472 v3_add( (v3f
){0.0f
,0.1f
,0.0f
}, start_gate
->co
[0], p
[0] );
473 p
[0][3] = start_gate
->ref_count
;
474 p
[0][3] -= (float)start_gate
->route_count
* 0.5f
;
475 start_gate
->ref_count
++;
477 if( !c0
->path_count
)
480 /* this is so that we get nice flow through the gates */
481 v3f temp_alignments
[2];
482 ent_gate
*both
[] = { start_gate
, end_gate
};
484 for( int j
=0; j
<2; j
++ ){
485 int pi
= c0
->path_start
+ ((j
==1)? c0
->path_count
-1: 0);
487 ent_path_index
*index
= mdl_arritm( &world
->ent_path_index
, pi
);
488 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
,
491 v3_sub( rn
->co
, both
[j
]->co
[0], v0
);
492 float d
= v3_dot( v0
, both
[j
]->to_world
[2] );
494 v3_muladds( both
[j
]->co
[0], both
[j
]->to_world
[2], d
,
495 temp_alignments
[j
] );
496 v3_add( (v3f
){0.0f
,0.1f
,0.0f
}, temp_alignments
[j
], temp_alignments
[j
]);
500 for( int j
=0; j
<c0
->path_count
; j
++ ){
501 ent_path_index
*index
= mdl_arritm( &world
->ent_path_index
,
503 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
,
505 if( j
==0 || j
==c0
->path_count
-1 )
507 v3_copy( temp_alignments
[0], p
[1] );
509 v3_copy( temp_alignments
[1], p
[1] );
511 v3_copy( rn
->co
, p
[1] );
513 p
[1][3] = rn
->ref_count
;
514 p
[1][3] -= (float)rn
->ref_total
* 0.5f
;
517 if( j
+1 < c0
->path_count
){
518 index
= mdl_arritm( &world
->ent_path_index
,
519 c0
->path_start
+j
+1 );
520 rn
= mdl_arritm( &world
->ent_route_node
, index
->index
);
522 if( j
+1 == c0
->path_count
-1 )
523 v3_lerp( p
[1], temp_alignments
[1], 0.5f
, p
[2] );
525 v3_lerp( p
[1], rn
->co
, 0.5f
, p
[2] );
527 p
[2][3] = rn
->ref_count
;
528 p
[2][3] -= (float)rn
->ref_total
* 0.5f
;
531 v3_copy( end_gate
->co
[0], p
[2] );
532 v3_add( (v3f
){0.0f
,0.1f
,0.0f
}, p
[2], p
[2] );
533 p
[2][3] = collector
->ref_count
;
535 if( i
== route
->checkpoints_count
-1)
538 p
[2][3] -= (float)collector
->route_count
* 0.5f
;
539 //collector->ref_count ++;
542 /* p0,p1,p2 bezier patch is complete
543 * --------------------------------------*/
544 v3f surf0
, surf2
, n0
, n2
;
546 if( bh_closest_point( world
->geo_bh
, p
[0], surf0
, 5.0f
) == -1 )
547 v3_add( (v3f
){0.0f
,-0.1f
,0.0f
}, p
[0], surf0
);
549 if( bh_closest_point( world
->geo_bh
, p
[2], surf2
, 5.0f
) == -1 )
550 v3_add( (v3f
){0.0f
,-0.1f
,0.0f
}, p
[2], surf2
);
552 v3_sub( surf0
, p
[0], n0
);
553 v3_sub( surf2
, p
[2], n2
);
557 world_routes_place_curve( world
, route
, p
, n0
, n2
, sc
, pcbuf
);
560 v4_copy( p
[2], p
[0] );
564 scene_copy_slice( sc
, &route
->sm
);
568 struct world_surface
*world_tri_index_surface( world_instance
*world
,
571 VG_STATIC f64
world_routes_scatter_surface_points( world_instance
*world
,
572 pointcloud_buffer
*pcbuf
,
575 static f32 densities
[] = {
576 [k_surface_prop_concrete
] = 2.0f
,
577 [k_surface_prop_grass
] = 0.8f
,
578 [k_surface_prop_metal
] = 1.0f
,
579 [k_surface_prop_wood
] = 2.5f
,
580 [k_surface_prop_tiles
] = 4.0f
583 /* calculate total area */
584 f64 total_area
= 0.0f
;
585 for( u32 i
=0; i
<world
->scene_geo
.indice_count
/3; i
++ ){
586 u32
*tri
= &world
->scene_geo
.arrindices
[i
*3];
587 struct world_surface
*surf
= world_tri_index_surface( world
, tri
[0] );
589 if( surf
->info
.shader
== k_shader_boundary
||
590 surf
->info
.shader
== k_shader_invisible
) continue;
592 if( !(surf
->info
.flags
& k_material_flag_preview_visibile
) ) continue;
594 scene_vert
*va
= &world
->scene_geo
.arrvertices
[tri
[0]],
595 *vb
= &world
->scene_geo
.arrvertices
[tri
[1]],
596 *vc
= &world
->scene_geo
.arrvertices
[tri
[2]];
599 v3_sub( vb
->co
, va
->co
, v0
);
600 v3_sub( vc
->co
, va
->co
, v1
);
601 v3_cross( v0
, v1
, vn
);
602 if( vn
[1] < 0.0f
) continue;
605 if( surf
->info
.surface_prop
< vg_list_size(densities
) )
606 density
= densities
[surf
->info
.surface_prop
];
607 total_area
+= v3_length(vn
)*0.5f
*density
;
612 u8 colour
[] = { 80,80,80,255 };
613 v3f light_dir
= {0.3f
,0.8f
,0.1f
};
614 v3_normalize( light_dir
);
617 v3_sub( pcbuf
->boundary
[1], pcbuf
->boundary
[0], inv_ext
);
618 v3_div( (v3f
){1.0f
,1.0f
,1.0f
}, inv_ext
, inv_ext
);
620 for( u32 i
=0; i
<world
->scene_geo
.indice_count
/3; i
++ ){
621 u32
*tri
= &world
->scene_geo
.arrindices
[i
*3];
622 struct world_surface
*surf
= world_tri_index_surface( world
, tri
[0] );
624 if( surf
->info
.shader
== k_shader_boundary
||
625 surf
->info
.shader
== k_shader_invisible
) continue;
627 if( !(surf
->info
.flags
& k_material_flag_preview_visibile
) ) continue;
629 scene_vert
*va
= &world
->scene_geo
.arrvertices
[tri
[0]],
630 *vb
= &world
->scene_geo
.arrvertices
[tri
[1]],
631 *vc
= &world
->scene_geo
.arrvertices
[tri
[2]];
634 v3_sub( vb
->co
, va
->co
, v0
);
635 v3_sub( vc
->co
, va
->co
, v1
);
636 v3_cross( v0
, v1
, vn
);
637 if( vn
[1] < 0.0f
) continue;
640 if( surf
->info
.surface_prop
< vg_list_size(densities
) )
641 density
= densities
[surf
->info
.surface_prop
];
643 f32 area
= v3_length(vn
)*0.5f
*density
;
648 while( accum
> rate
){
651 if( pcbuf
->count
>= pcbuf
->max
) return total_area
;
653 v2f co
= { vg_randf64(), vg_randf64() };
654 if( v2_length2(co
) > 0.5f
){
660 v3_muls( v0
, co
[0], pt
);
661 v3_muladds( pt
, v1
, co
[1], pt
);
662 v3_add( va
->co
, pt
, pt
);
664 if( pt
[1] < world
->water
.height
) continue;
665 pointcloud_vert
*vert
= &pcbuf
->buf
[ pcbuf
->count
++ ];
668 v3_sub( pt
, pcbuf
->boundary
[0], pos
);
669 v3_mul( pos
, inv_ext
, pos
);
671 for( u32 i
=0; i
<3; i
++ ){
672 vert
->pos
[i
] = (pos
[i
]-0.5f
) * 32767.0f
;
675 static v4f colours
[] = {
676 [k_surface_prop_concrete
] = { 0.13, 0.15, 0.17, 1.0 },
677 [k_surface_prop_grass
] = { 0.07, 0.1, 0.14, 1.0 },
678 [k_surface_prop_metal
] = { 0.15, 0.19, 0.22, 1.0 },
679 [k_surface_prop_wood
] = { 0.1, 0.13, 0.17, 1.0 },
680 [k_surface_prop_tiles
] = { 0.05, 0.06, 0.07, 1.0 },
683 v4f col
= {0.0f
,0.0f
,0.0f
,0.0f
};
684 if( surf
->info
.surface_prop
< vg_list_size(colours
) ){
685 v4_copy( colours
[surf
->info
.surface_prop
], col
);
688 f32 brightness
= v3_dot(vn
,light_dir
)*0.5f
+0.5f
;
689 v3_muls( col
, brightness
, col
);
691 for( u32 j
=0; j
<4; j
++ ){
692 vert
->colour
[j
] = col
[j
] * 255.0f
;
700 VG_STATIC
void world_routes_surface_grid( world_instance
*world
,
701 pointcloud_buffer
*pcbuf
)
703 i32
const k_gridlines
= 32,
707 v3_sub( pcbuf
->boundary
[1], pcbuf
->boundary
[0], inv_ext
);
708 v3_div( (v3f
){1.0f
,1.0f
,1.0f
}, inv_ext
, inv_ext
);
709 v4f colour
= {0.2f
,0.2f
,0.2f
,1.0f
};
710 v3f dir
= {0.0f
,-1.0f
,0.0f
};
712 for( u32 k
=0; k
<2; k
++ ){
716 for( i32 x
=0; x
<=k_gridlines
; x
++ ){
717 f32 t
= (float)x
/ (float)k_gridlines
,
718 px
= vg_lerpf( pcbuf
->boundary
[0][a
], pcbuf
->boundary
[1][a
], t
);
720 for( i32 z
=0; z
<=k_gridres
; z
++ ){
721 f32 tz
= (float)z
/ (float)k_gridres
,
722 pz
= vg_lerpf(pcbuf
->boundary
[0][b
],pcbuf
->boundary
[1][b
], tz
);
730 bh_iter_init_ray( 0, &it
, ro
, dir
, INFINITY
);
733 while( bh_next( world
->geo_bh
, &it
, &idx
) ){
734 u32
*tri
= &world
->scene_geo
.arrindices
[ idx
*3 ];
737 for( u32 i
=0; i
<3; i
++ ){
738 v3_copy( world
->scene_geo
.arrvertices
[tri
[i
]].co
, vs
[i
] );
742 if( ray_tri( vs
, ro
, dir
, &t
) ){
743 v3_muladds( ro
, dir
, t
, hit
);
744 struct world_surface
*m1
=
745 world_tri_index_surface( world
, tri
[0] );
747 if( !(m1
->info
.flags
& k_material_flag_preview_visibile
) )
750 if( world
->water
.enabled
)
751 if( hit
[1] < world
->water
.height
)
754 if( pcbuf
->count
>= pcbuf
->max
) return;
756 pointcloud_vert
*vert
= &pcbuf
->buf
[ pcbuf
->count
++ ];
759 v3_sub( hit
, pcbuf
->boundary
[0], co
);
760 v3_mul( co
, inv_ext
, co
);
762 for( u32 i
=0; i
<3; i
++ ){
763 vert
->pos
[i
] = (co
[i
]-0.5f
) * 32767.0f
;
766 for( u32 i
=0; i
<4; i
++ ){
767 vert
->colour
[i
] = colour
[i
] * 255.0f
;
777 * Create the strips of colour that run through the world along course paths
779 VG_STATIC
void world_gen_routes_generate(void)
781 world_instance
*world
= world_loading_instance();
782 vg_info( "Generating route meshes\n" );
785 vg_rand_seed( 2000 );
786 vg_async_item
*call_scene
= scene_alloc_async( &world
->scene_lines
,
787 &world
->mesh_route_lines
,
790 vg_async_item
*call_pointcloud
= NULL
;
791 pointcloud_buffer
*pcbuf
= NULL
;
793 if( world_loader
.generate_point_cloud
){
794 call_pointcloud
= vg_async_alloc(
795 sizeof(pointcloud_buffer
) +
796 sizeof(pointcloud_vert
)*POINTCLOUD_POINTS
);
797 pcbuf
= call_pointcloud
->payload
;
799 pcbuf
->max
= POINTCLOUD_POINTS
;
800 pcbuf
->op
= k_pointcloud_op_clear
;
803 v3_sub( world
->scene_geo
.bbx
[1], world
->scene_geo
.bbx
[0], ext
);
804 f32 maxe
= v3_maxf( ext
);
805 v3_fill( v0
, maxe
* 0.5f
);
806 v3_muladds( world
->scene_geo
.bbx
[0], ext
, 0.5f
, mid
);
807 v3_add( mid
, v0
, pcbuf
->boundary
[1] );
808 v3_sub( mid
, v0
, pcbuf
->boundary
[0] );
811 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
812 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, i
);
814 gate
->route_count
= 0;
817 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route_node
); i
++ ){
818 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
, i
);
823 for( u32 k
=0; k
<mdl_arrcount(&world
->ent_route
); k
++ ){
824 ent_route
*route
= mdl_arritm( &world
->ent_route
, k
);
826 for( int i
=0; i
<route
->checkpoints_count
; i
++ ){
827 int i0
= route
->checkpoints_start
+i
,
828 i1
= route
->checkpoints_start
+((i
+1)%route
->checkpoints_count
);
830 ent_checkpoint
*c0
= mdl_arritm(&world
->ent_checkpoint
, i0
),
831 *c1
= mdl_arritm(&world
->ent_checkpoint
, i1
);
833 ent_gate
*start_gate
= mdl_arritm( &world
->ent_gate
, c0
->gate_index
);
834 start_gate
= mdl_arritm( &world
->ent_gate
, start_gate
->target
);
835 start_gate
->route_count
++;
837 if( !c0
->path_count
)
840 for( int j
=0; j
<c0
->path_count
; j
++ ){
841 ent_path_index
*index
= mdl_arritm( &world
->ent_path_index
,
843 ent_route_node
*rn
= mdl_arritm( &world
->ent_route_node
,
850 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
851 world_routes_gen_meshes( world
, i
, &world
->scene_lines
, pcbuf
);
854 if( world_loader
.generate_point_cloud
){
856 area
= world_routes_scatter_surface_points( world
, pcbuf
, 16.0f
);
857 world_routes_surface_grid( world
, pcbuf
);
859 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_gate
); i
++ ){
860 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, i
);
862 world_routes_pointcloud_tower( world
, pcbuf
, gate
->co
[0],
864 (v4f
){0.2f
,0.2f
,0.2f
,1.0f
} );
867 vg_info( "Distrubuted %u points over %fkm^2!\n",
868 pcbuf
->count
, area
/1e6f
);
870 if( world_loader
.location
== k_world_load_type_local
){
873 vg_strnull( &path
, path_buf
, 4096 );
874 vg_strcat( &path
, "maps/" );
875 vg_strcat( &path
, world_loader
.name
);
876 vg_strcat( &path
, "/preview.bin" );
878 if( !vg_strgood( &path
) ) vg_fatal_error( "Path too long\n" );
879 FILE *fp
= fopen( path_buf
, "wb" );
880 if( !fp
) vg_fatal_error( "Cannot open '%s' for writing\n", path_buf
);
882 fwrite( pcbuf
, sizeof(pcbuf
) +
883 sizeof(struct pointcloud_vert
)*pcbuf
->count
, 1, fp
);
887 vg_async_dispatch( call_pointcloud
, async_pointcloud_sub
);
890 vg_async_dispatch( call_scene
, async_scene_upload
);
891 world_routes_clear( world
);
894 /* load all routes from model header */
895 VG_STATIC
void world_gen_routes_ent_init(void)
897 world_instance
*world
= world_loading_instance();
898 vg_info( "Initializing routes\n" );
900 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
901 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, i
);
902 for( u32 j
=0; j
<4; j
++ ){
903 gate
->routes
[j
] = 0xffff;
907 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
908 ent_route
*route
= mdl_arritm(&world
->ent_route
,i
);
909 mdl_transform_m4x3( &route
->transform
, route
->board_transform
);
911 route
->official_track_id
= 0xffffffff;
912 for( u32 j
=0; j
<vg_list_size(track_infos
); j
++ ){
913 if( !strcmp(track_infos
[j
].name
,
914 mdl_pstr(&world
->meta
,route
->pstr_name
))){
915 route
->official_track_id
= j
;
919 for( u32 j
=0; j
<route
->checkpoints_count
; j
++ ){
920 u32 id
= route
->checkpoints_start
+ j
;
921 ent_checkpoint
*cp
= mdl_arritm(&world
->ent_checkpoint
,id
);
923 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, cp
->gate_index
);
925 for( u32 k
=0; k
<4; k
++ ){
926 if( gate
->routes
[k
] == 0xffff ){
932 if( gate
->type
== k_gate_type_teleport
){
933 gate
= mdl_arritm(&world
->ent_gate
, gate
->target
);
935 for( u32 k
=0; k
<4; k
++ ){
936 if( gate
->routes
[k
] == i
){
937 vg_error( "already assigned route to gate\n" );
940 if( gate
->routes
[k
] == 0xffff ){
949 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
950 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, i
);
953 world_routes_clear( world
);
957 * -----------------------------------------------------------------------------
959 * -----------------------------------------------------------------------------
962 VG_STATIC
void world_routes_init(void)
964 world_static
.current_run_version
= 200;
965 world_static
.time
= 300.0;
966 world_static
.last_use
= 0.0;
968 shader_scene_route_register();
969 shader_routeui_register();
972 VG_STATIC
void world_routes_update( world_instance
*world
)
974 world_static
.time
+= vg
.time_delta
;
976 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
977 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
979 int target
= route
->active_checkpoint
== 0xffff? 0: 1;
980 route
->factive
= vg_lerpf( route
->factive
, target
, 0.6f
*vg
.time_delta
);
983 for( u32 i
=0; i
<world_render
.text_particle_count
; i
++ ){
984 struct text_particle
*particle
= &world_render
.text_particles
[i
];
985 rb_object_debug( &particle
->obj
, VG__RED
);
989 VG_STATIC
void world_routes_fixedupdate( world_instance
*world
)
993 for( u32 i
=0; i
<world_render
.text_particle_count
; i
++ ){
994 struct text_particle
*particle
= &world_render
.text_particles
[i
];
996 if( rb_global_has_space() ){
997 rb_ct
*buf
= rb_global_buffer();
999 int l
= rb_sphere__scene( particle
->obj
.rb
.to_world
,
1000 &particle
->obj
.inf
.sphere
,
1001 NULL
, &world
->rb_geo
.inf
.scene
, buf
);
1003 for( int j
=0; j
<l
; j
++ ){
1004 buf
[j
].rba
= &particle
->obj
.rb
;
1005 buf
[j
].rbb
= &world
->rb_geo
.rb
;
1008 rb_contact_count
+= l
;
1012 rb_presolve_contacts( rb_contact_buffer
, rb_contact_count
);
1014 for( int i
=0; i
<rb_contact_count
; i
++ ){
1015 rb_contact_restitution( rb_contact_buffer
+i
, vg_randf64() );
1018 for( int i
=0; i
<6; i
++ ){
1019 rb_solve_contacts( rb_contact_buffer
, rb_contact_count
);
1022 for( u32 i
=0; i
<world_render
.text_particle_count
; i
++ ){
1023 struct text_particle
*particle
= &world_render
.text_particles
[i
];
1024 rb_iter( &particle
->obj
.rb
);
1027 for( u32 i
=0; i
<world_render
.text_particle_count
; i
++ ){
1028 struct text_particle
*particle
= &world_render
.text_particles
[i
];
1029 rb_update_transform( &particle
->obj
.rb
);
1033 VG_STATIC
void bind_terrain_noise(void);
1034 VG_STATIC
void world_bind_light_array( world_instance
*world
,
1035 GLuint shader
, GLuint location
,
1037 VG_STATIC
void world_bind_light_index( world_instance
*world
,
1038 GLuint shader
, GLuint location
,
1041 VG_STATIC
void world_routes_update_timer_texts( world_instance
*world
)
1043 world_render
.timer_text_count
= 0;
1045 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
1046 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
1048 if( route
->active_checkpoint
!= 0xffff ){
1049 u32 next
= route
->active_checkpoint
+1;
1050 next
= next
% route
->checkpoints_count
;
1051 next
+= route
->checkpoints_start
;
1053 ent_checkpoint
*cp
= mdl_arritm( &world
->ent_checkpoint
, next
);
1054 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, cp
->gate_index
);
1055 ent_gate
*dest
= mdl_arritm( &world
->ent_gate
, gate
->target
);
1059 if( dest
->routes
[j
] == i
){
1069 struct timer_text
*text
=
1070 &world_render
.timer_texts
[ world_render
.timer_text_count
++ ];
1073 text
->route
= route
;
1075 if( route
->valid_checkpoints
>= route
->checkpoints_count
){
1076 double lap_time
= world_static
.time
- route
->timing_base
,
1077 time_centiseconds
= lap_time
* 100.0;
1079 if( time_centiseconds
> (float)0xfffe ) time_centiseconds
= 0.0;
1081 u16 centiseconds
= time_centiseconds
,
1082 seconds
= centiseconds
/ 100,
1083 minutes
= seconds
/ 60;
1085 centiseconds
%= 100;
1089 if( minutes
> 9 ) minutes
= 9;
1093 highscore_intr( text
->text
, minutes
, 1, ' ' ); j
++;
1094 text
->text
[j
++] = ':';
1097 if( seconds
>= 10 || minutes
){
1098 highscore_intr( text
->text
+j
, seconds
, 2, '0' ); j
+=2;
1101 highscore_intr( text
->text
+j
, seconds
, 1, '0' ); j
++;
1104 text
->text
[j
++] = '.';
1105 highscore_intr( text
->text
+j
, centiseconds
, 1, '0' ); j
++;
1106 text
->text
[j
] = '\0';
1109 highscore_intr( text
->text
, route
->valid_checkpoints
, 1, ' ' );
1110 text
->text
[1] = '/';
1111 highscore_intr( text
->text
+2, route
->checkpoints_count
+1, 1, ' ' );
1112 text
->text
[3] = '\0';
1115 float align_r
= font3d_string_width( &gui
.font
, 0, text
->text
);
1119 { -0.92f
, h0
, depth
},
1120 { 0.92f
- align_r
, h0
, depth
},
1121 { -0.92f
, h1
, depth
},
1122 { 0.92f
- align_r
, h1
, depth
},
1125 if( dest
->route_count
== 1 ){
1126 positions
[0][0] = -align_r
*0.5f
;
1127 positions
[0][1] = h1
;
1130 m3x3_copy( gate
->to_world
, text
->transform
);
1131 float ratio
= v3_length(text
->transform
[0]) /
1132 v3_length(text
->transform
[1]);
1134 m3x3_scale( text
->transform
, (v3f
){ size
, size
*ratio
, 0.1f
} );
1135 m4x3_mulv( gate
->to_world
, positions
[j
], text
->transform
[3] );
1140 VG_STATIC
void world_routes_fracture( world_instance
*world
, ent_gate
*gate
,
1141 v3f imp_co
, v3f imp_v
)
1143 world_render
.text_particle_count
= 0;
1145 for( u32 i
=0; i
<world_render
.timer_text_count
; i
++ ){
1146 struct timer_text
*text
= &world_render
.timer_texts
[i
];
1148 if( text
->gate
!= gate
) continue;
1151 m4x3_mul( gate
->transport
, text
->transform
, transform
);
1155 m4x3_decompose( transform
, co
, q
, s
);
1161 float brightness
= 0.3f
+ world
->ub_lighting
.g_day_phase
;
1162 v3_muls( text
->route
->colour
, brightness
, colour
);
1163 colour
[3] = 1.0f
-text
->route
->factive
;
1165 for( u32 j
=0;; j
++ ){
1166 char c
= text
->text
[j
];
1169 ent_glyph
*glyph
= font3d_glyph( &gui
.font
, 0, c
);
1170 if( !glyph
) continue;
1172 if( c
>= (u32
)'0' && c
<= (u32
)'9' && glyph
->indice_count
){
1173 struct text_particle
*particle
=
1174 &world_render
.text_particles
[world_render
.text_particle_count
++];
1176 particle
->glyph
= glyph
;
1177 v4_copy( colour
, particle
->colour
);
1180 v2_muls( glyph
->size
, 0.5f
, origin
);
1185 v3_add( offset
, origin
, world_co
);
1186 m4x3_mulv( transform
, world_co
, world_co
);
1188 float r
= vg_maxf( s
[0]*glyph
->size
[0], s
[1]*glyph
->size
[1] )*0.5f
;
1190 m3x3_identity( particle
->mlocal
);
1191 m3x3_scale( particle
->mlocal
, s
);
1193 v3_muls( origin
, -1.0f
, particle
->mlocal
[3] );
1195 v3_copy( world_co
, particle
->obj
.rb
.co
);
1196 v3_muls( imp_v
, 1.0f
+vg_randf64(), particle
->obj
.rb
.v
);
1197 particle
->obj
.rb
.v
[1] += 2.0f
;
1199 v4_copy( q
, particle
->obj
.rb
.q
);
1200 particle
->obj
.rb
.w
[0] = vg_randf64()*2.0f
-1.0f
;
1201 particle
->obj
.rb
.w
[1] = vg_randf64()*2.0f
-1.0f
;
1202 particle
->obj
.rb
.w
[2] = vg_randf64()*2.0f
-1.0f
;
1204 particle
->obj
.type
= k_rb_shape_sphere
;
1205 particle
->obj
.inf
.sphere
.radius
= r
*0.6f
;
1207 rb_init_object( &particle
->obj
);
1209 offset
[0] += glyph
->size
[0];
1214 VG_STATIC
void render_world_routes( world_instance
*world
, camera
*cam
,
1217 m4x3f identity_matrix
;
1218 m4x3_identity( identity_matrix
);
1220 shader_scene_route_use();
1221 shader_scene_route_uTexGarbage(0);
1222 world_link_lighting_ub( world
, _shader_scene_route
.id
);
1223 world_bind_position_texture( world
, _shader_scene_route
.id
,
1224 _uniform_scene_route_g_world_depth
, 2 );
1225 world_bind_light_array( world
, _shader_scene_route
.id
,
1226 _uniform_scene_route_uLightsArray
, 3 );
1227 world_bind_light_index( world
, _shader_scene_route
.id
,
1228 _uniform_scene_route_uLightsIndex
, 4 );
1229 bind_terrain_noise();
1231 shader_scene_route_uPv( cam
->mtx
.pv
);
1232 shader_scene_route_uPvmPrev( cam
->mtx_prev
.pv
);
1233 shader_scene_route_uMdl( identity_matrix
);
1234 shader_scene_route_uCamera( cam
->transform
[3] );
1236 mesh_bind( &world
->mesh_route_lines
);
1238 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
1239 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
1242 v3_lerp( (v3f
){0.7f
,0.7f
,0.7f
}, route
->colour
, route
->factive
, colour
);
1243 colour
[3] = route
->factive
*0.2f
;
1245 shader_scene_route_uColour( colour
);
1246 mdl_draw_submesh( &route
->sm
);
1250 * ---------------------------------------------------- */
1251 if( layer_depth
== 0 ){
1252 font3d_bind( &gui
.font
, cam
);
1254 for( u32 i
=0; i
<world_render
.timer_text_count
; i
++ ){
1255 struct timer_text
*text
= &world_render
.timer_texts
[i
];
1258 float brightness
= 0.3f
+ world
->ub_lighting
.g_day_phase
;
1259 v3_muls( text
->route
->colour
, brightness
, colour
);
1260 colour
[3] = 1.0f
-text
->route
->factive
;
1262 shader_model_font_uColour( colour
);
1263 font3d_simple_draw( &gui
.font
, 0, text
->text
, cam
, text
->transform
);
1266 shader_model_font_uOffset( (v4f
){0.0f
,0.0f
,0.0f
,1.0f
} );
1268 for( u32 i
=0; i
<world_render
.text_particle_count
; i
++ ){
1269 struct text_particle
*particle
= &world_render
.text_particles
[i
];
1273 m4x3_expand( particle
->mdl
, prev_mtx
);
1274 m4x4_mul( cam
->mtx_prev
.pv
, prev_mtx
, prev_mtx
);
1276 shader_model_font_uPvmPrev( prev_mtx
);
1280 rb_extrapolate( &particle
->obj
.rb
, model
[3], q
);
1283 m4x3_mul( model
, particle
->mlocal
, particle
->mdl
);
1284 shader_model_font_uMdl( particle
->mdl
);
1285 shader_model_font_uColour( particle
->colour
);
1287 mesh_drawn( particle
->glyph
->indice_start
,
1288 particle
->glyph
->indice_count
);
1293 * ---------------------------------------------------- */
1295 shader_model_gate_use();
1296 shader_model_gate_uPv( cam
->mtx
.pv
);
1297 shader_model_gate_uCam( cam
->pos
);
1298 shader_model_gate_uTime( vg
.time
*0.25f
);
1299 shader_model_gate_uInvRes( (v2f
){
1300 1.0f
/ (float)vg
.window_x
,
1301 1.0f
/ (float)vg
.window_y
});
1303 mesh_bind( &world_gates
.mesh
);
1305 /* skip writing into the motion vectors for this */
1306 glDrawBuffers( 1, (GLenum
[]){ GL_COLOR_ATTACHMENT0
} );
1308 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_route
); i
++ ){
1309 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
1311 if( route
->active_checkpoint
!= 0xffff ){
1313 float brightness
= 0.3f
+ world
->ub_lighting
.g_day_phase
;
1314 v3_muls( route
->colour
, brightness
, colour
);
1315 colour
[3] = 1.0f
-route
->factive
;
1317 shader_model_gate_uColour( colour
);
1319 u32 next
= route
->active_checkpoint
+1+layer_depth
;
1320 next
= next
% route
->checkpoints_count
;
1321 next
+= route
->checkpoints_start
;
1323 ent_checkpoint
*cp
= mdl_arritm( &world
->ent_checkpoint
, next
);
1324 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, cp
->gate_index
);
1325 shader_model_gate_uMdl( gate
->to_world
);
1327 for( u32 j
=0; j
<4; j
++ ){
1328 if( gate
->routes
[j
] == i
){
1329 mdl_draw_submesh( &world_gates
.sm_marker
[j
] );
1335 glDrawBuffers( 2, (GLenum
[]){ GL_COLOR_ATTACHMENT0
, GL_COLOR_ATTACHMENT1
} );
1338 #endif /* ROUTES_C */