update vg
[carveJwlIkooP6JGAAIwe30JlM.git] / world_routes.h
1 /*
2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef ROUTES_H
6 #define ROUTES_H
7
8 #include <time.h>
9 #include "world.h"
10 #include "world_gate.h"
11 #include "font.h"
12
13 #if 0
14 #include "shaders/vblend.h"
15 #endif
16
17 #include "shaders/scene_route.h"
18 #include "shaders/routeui.h"
19
20
21 VG_STATIC
22 void world_routes_local_set_record( world_instance *world, ent_route *route,
23 double lap_time )
24 {
25 vg_success( " NEW LAP TIME: %f\n", lap_time );
26
27 if( route->official_track_id != 0xffffffff ){
28 double time_centiseconds = lap_time * 100.0;
29 if( time_centiseconds > (float)0xfffe ) /* skill issue */
30 return;
31
32 highscore_record temp;
33 temp.trackid = route->official_track_id;
34 temp.datetime = time(NULL);
35 temp.playerid = 0;
36 temp.points = 0;
37 temp.time = time_centiseconds;
38
39 #if 0
40 highscores_push_record( &temp );
41 #endif
42
43 struct track_info *ti = &track_infos[ route->official_track_id ];
44 ti->push = 1;
45
46 if( ti->achievement_id ){
47 #if 0
48 steam_set_achievement( ti->achievement_id );
49 steam_store_achievements();
50 #endif
51 }
52 }
53 else{
54 vg_warn( "There is no associated track for this record...\n" );
55 }
56 }
57
58
59 VG_STATIC void world_routes_clear( world_instance *world )
60 {
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 = 0xffff;
64 }
65
66 for( u32 i=0; i<mdl_arrcount( &world->ent_gate ); i++ ){
67 ent_gate *rg = mdl_arritm( &world->ent_gate, i );
68 rg->timing_version = 0;
69 rg->timing_time = 0.0;
70 }
71
72 world_global.current_run_version += 4;
73 world_global.last_use = 0.0;
74 }
75
76 VG_STATIC void world_routes_time_lap( world_instance *world, ent_route *route )
77 {
78 vg_info( "------- time lap %s -------\n",
79 mdl_pstr(&world->meta,route->pstr_name) );
80
81 double start_time = 0.0;
82 u32 last_version=0;
83
84 u32 valid_count=0;
85
86 for( u32 i=0; i<route->checkpoints_count; i++ ){
87 u32 cpid = (i+route->active_checkpoint) % route->checkpoints_count;
88 cpid += route->checkpoints_start;
89
90 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint, cpid );
91 ent_gate *rg = mdl_arritm( &world->ent_gate, cp->gate_index );
92 rg = mdl_arritm( &world->ent_gate, rg->target );
93
94 if( i == 1 ){
95 route->timing_base = rg->timing_time;
96 }
97
98 if( i == 0 )
99 start_time = rg->timing_time;
100 else{
101 if( last_version+1 == rg->timing_version ) valid_count ++;
102 else valid_count = 0;
103 }
104
105 last_version = rg->timing_version;
106 vg_info( "%u %f\n", rg->timing_version, rg->timing_time );
107 }
108
109 if( world_global.current_run_version == last_version+1 ){
110 valid_count ++;
111
112 if( route->checkpoints_count == 1 ){
113 route->timing_base = world_global.time;
114 }
115 }
116 else valid_count = 0;
117
118 vg_info( "%u %f\n", world_global.current_run_version, world_global.time );
119
120 if( valid_count==route->checkpoints_count ){
121 double lap_time = world_global.time - start_time;
122 world_routes_local_set_record( world, route, lap_time );
123 }
124
125 route->valid_checkpoints = valid_count+1;
126
127 vg_info( "valid: %u\n", valid_count );
128 vg_info( "----------------------------\n" );
129 }
130
131 /*
132 * When going through a gate this is called for bookkeeping purposes
133 */
134 VG_STATIC void world_routes_activate_entry_gate( world_instance *world,
135 ent_gate *rg )
136 {
137 world_global.last_use = world_global.time;
138
139 /* disable all routes and leave the world */
140 if( rg->type == k_gate_type_nonlocel ){
141 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
142 ent_route *route = mdl_arritm( &world->ent_route, i );
143 route->active_checkpoint = 0xffff;
144 }
145 return;
146 }
147
148 ent_gate *dest = mdl_arritm( &world->ent_gate, rg->target );
149
150 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
151 ent_route *route = mdl_arritm( &world->ent_route, i );
152
153 u32 active_prev = route->active_checkpoint;
154 route->active_checkpoint = 0xffff;
155
156 for( u32 j=0; j<4; j++ ){
157 if( dest->routes[j] == i ){
158 for( u32 k=0; k<route->checkpoints_count; k++ ){
159 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint,
160 route->checkpoints_start+k );
161
162 ent_gate *gk = mdl_arritm( &world->ent_gate, cp->gate_index );
163 gk = mdl_arritm( &world->ent_gate, gk->target );
164 if( gk == dest ){
165 route->active_checkpoint = k;
166 world_routes_time_lap( world, route );
167 break;
168 }
169 }
170 break;
171 }
172 }
173 }
174
175 dest->timing_version = world_global.current_run_version;
176 dest->timing_time = world_global.time;
177
178 world_global.current_run_version ++;
179 }
180
181 /* draw lines along the paths */
182 VG_STATIC void world_routes_debug( world_instance *world )
183 {
184 for( u32 i=0; i<mdl_arrcount(&world->ent_route_node); i++ ){
185 ent_route_node *rn = mdl_arritm(&world->ent_route_node,i);
186 vg_line_pt3( rn->co, 0.25f, VG__WHITE );
187 }
188
189 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
190 ent_route *route = mdl_arritm(&world->ent_route, i);
191
192 u32 colours[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
193 0xff5442f5 };
194
195 u32 cc = 0xffcccccc;
196 if( route->active_checkpoint != 0xffff ){
197 cc = colours[i%vg_list_size(colours)];
198 }
199
200 for( int i=0; i<route->checkpoints_count; i++ ){
201 int i0 = route->checkpoints_start+i,
202 i1 = route->checkpoints_start+((i+1)%route->checkpoints_count);
203
204 ent_checkpoint *c0 = mdl_arritm(&world->ent_checkpoint, i0),
205 *c1 = mdl_arritm(&world->ent_checkpoint, i1);
206
207 ent_gate *start_gate = mdl_arritm( &world->ent_gate, c0->gate_index );
208 ent_gate *end_gate = mdl_arritm( &world->ent_gate, c1->gate_index );
209
210 v3f p0, p1;
211 v3_copy( start_gate->co[1], p0 );
212
213 for( int j=0; j<c0->path_count; j ++ ){
214 ent_path_index *index = mdl_arritm( &world->ent_path_index,
215 c0->path_start+j );
216
217 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
218 index->index );
219
220 v3_copy( rn->co, p1 );
221 vg_line( p0, p1, cc );
222 v3_copy( p1, p0 );
223 }
224
225 v3_copy( end_gate->co[0], p1 );
226 vg_line( p0, p1, cc );
227 }
228 }
229 }
230
231 VG_STATIC void world_routes_place_curve( world_instance *world,
232 v4f h[3], v3f n0, v3f n2 )
233 {
234 float t;
235 v3f p, pd;
236 int last_valid=0;
237
238 float total_length = 0.0f,
239 travel_length = 0.0;
240
241 v3f last;
242 v3_copy( h[0], last );
243 for( int it=0; it<128; it ++ ){
244 t = (float)(it+1) * (1.0f/128.0f);
245 eval_bezier3( h[0], h[1], h[2], t, p );
246 total_length += v3_dist( p, last );
247 v3_copy( p, last );
248 }
249
250 float patch_size = 4.0f,
251 patch_count = ceilf( total_length / patch_size );
252
253 t = 0.0f;
254 v3_copy( h[0], last );
255
256 for( int it=0; it<128; it ++ ){
257 float const k_sample_dist = 0.0025f,
258 k_line_width = 1.5f;
259
260 eval_bezier3( h[0], h[1], h[2], t, p );
261 eval_bezier3( h[0], h[1], h[2], t+k_sample_dist, pd );
262
263 travel_length += v3_dist( p, last );
264
265 float mod = k_sample_dist / v3_dist( p, pd );
266
267 v3f v0,up, right;
268
269 v3_muls( n0, -(1.0f-t), up );
270 v3_muladds( up, n2, -t, up );
271 v3_normalize( up );
272
273 v3_sub( pd,p,v0 );
274 v3_cross( up, v0, right );
275 v3_normalize( right );
276
277 float cur_x = (1.0f-t)*h[0][3] + t*h[2][3];
278
279 v3f sc, sa, sb, down;
280 v3_muladds( p, right, cur_x * k_line_width, sc );
281 v3_muladds( sc, up, 1.5f, sc );
282 v3_muladds( sc, right, k_line_width*0.95f, sa );
283 v3_muladds( sc, right, 0.0f, sb );
284 v3_muls( up, -1.0f, down );
285
286 ray_hit ha, hb;
287 ha.dist = 8.0f;
288 hb.dist = 8.0f;
289
290 int resa = ray_world( world, sa, down, &ha ),
291 resb = ray_world( world, sb, down, &hb );
292
293 if( resa && resb ){
294 struct world_surface *surfa = ray_hit_surface( world, &ha ),
295 *surfb = ray_hit_surface( world, &hb );
296
297 if( (surfa->info.flags & k_material_flag_skate_surface) &&
298 (surfb->info.flags & k_material_flag_skate_surface) )
299 {
300 scene_vert va, vb;
301
302 float gap = vg_fractf(cur_x*0.5f)*0.02f;
303
304 v3_muladds( ha.pos, up, 0.06f+gap, va.co );
305 v3_muladds( hb.pos, up, 0.06f+gap, vb.co );
306
307 scene_vert_pack_norm( &va, up );
308 scene_vert_pack_norm( &vb, up );
309
310 float t1 = (travel_length / total_length) * patch_count;
311 va.uv[0] = t1;
312 va.uv[1] = 0.0f;
313 vb.uv[0] = t1;
314 vb.uv[1] = 1.0f;
315
316 scene_push_vert( world->scene_lines, &va );
317 scene_push_vert( world->scene_lines, &vb );
318
319 if( last_valid ){
320 /* Connect them with triangles */
321 scene_push_tri( world->scene_lines, (u32[3]){
322 last_valid+0-2, last_valid+1-2, last_valid+2-2} );
323 scene_push_tri( world->scene_lines, (u32[3]){
324 last_valid+1-2, last_valid+3-2, last_valid+2-2} );
325 }
326
327 last_valid = world->scene_lines->vertex_count;
328 }
329 else
330 last_valid = 0;
331 }
332 else
333 last_valid = 0;
334
335 if( t == 1.0f )
336 return;
337
338 t += 1.0f*mod;
339 if( t > 1.0f )
340 t = 1.0f;
341
342 v3_copy( p, last );
343 }
344 }
345
346 VG_STATIC void world_routes_create_mesh( world_instance *world, u32 route_id )
347 {
348 ent_route *route = mdl_arritm( &world->ent_route, route_id );
349 u32 last_valid = 0;
350
351 for( int i=0; i<route->checkpoints_count; i++ ){
352 int i0 = route->checkpoints_start+i,
353 i1 = route->checkpoints_start+((i+1)%route->checkpoints_count);
354
355 ent_checkpoint *c0 = mdl_arritm(&world->ent_checkpoint, i0),
356 *c1 = mdl_arritm(&world->ent_checkpoint, i1);
357
358 ent_gate *start_gate = mdl_arritm( &world->ent_gate, c0->gate_index );
359 start_gate = mdl_arritm( &world->ent_gate, start_gate->target );
360
361 ent_gate *end_gate = mdl_arritm( &world->ent_gate, c1->gate_index ),
362 *collector = mdl_arritm( &world->ent_gate, end_gate->target );
363
364 v4f p[3];
365
366 v3_add( (v3f){0.0f,0.1f,0.0f}, start_gate->co[0], p[0] );
367 p[0][3] = start_gate->ref_count;
368 p[0][3] -= (float)start_gate->route_count * 0.5f;
369 start_gate->ref_count ++;
370
371 if( !c0->path_count )
372 continue;
373
374 /* this is so that we get nice flow through the gates */
375 v3f temp_alignments[2];
376 ent_gate *both[] = { start_gate, end_gate };
377
378 for( int j=0; j<2; j++ ){
379 int pi = c0->path_start + ((j==1)? c0->path_count-1: 0);
380
381 ent_path_index *index = mdl_arritm( &world->ent_path_index, pi );
382 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
383 index->index );
384 v3f v0;
385 v3_sub( rn->co, both[j]->co[0], v0 );
386 float d = v3_dot( v0, both[j]->to_world[2] );
387
388 v3_muladds( both[j]->co[0], both[j]->to_world[2], d,
389 temp_alignments[j] );
390 v3_add( (v3f){0.0f,0.1f,0.0f}, temp_alignments[j], temp_alignments[j]);
391 }
392
393
394 for( int j=0; j<c0->path_count; j ++ ){
395 ent_path_index *index = mdl_arritm( &world->ent_path_index,
396 c0->path_start+j );
397 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
398 index->index );
399 if( j==0 || j==c0->path_count-1 )
400 if( j == 0 )
401 v3_copy( temp_alignments[0], p[1] );
402 else
403 v3_copy( temp_alignments[1], p[1] );
404 else
405 v3_copy( rn->co, p[1] );
406
407 p[1][3] = rn->ref_count;
408 p[1][3] -= (float)rn->ref_total * 0.5f;
409 rn->ref_count ++;
410
411 if( j+1 < c0->path_count ){
412 index = mdl_arritm( &world->ent_path_index,
413 c0->path_start+j+1 );
414 rn = mdl_arritm( &world->ent_route_node, index->index );
415
416 if( j+1 == c0->path_count-1 )
417 v3_lerp( p[1], temp_alignments[1], 0.5f, p[2] );
418 else
419 v3_lerp( p[1], rn->co, 0.5f, p[2] );
420
421 p[2][3] = rn->ref_count;
422 p[2][3] -= (float)rn->ref_total * 0.5f;
423 }
424 else{
425 v3_copy( end_gate->co[0], p[2] );
426 v3_add( (v3f){0.0f,0.1f,0.0f}, p[2], p[2] );
427 p[2][3] = collector->ref_count;
428
429 if( i == route->checkpoints_count-1)
430 p[2][3] -= 1.0f;
431
432 p[2][3] -= (float)collector->route_count * 0.5f;
433 //collector->ref_count ++;
434 }
435
436 /* p0,p1,p2 bezier patch is complete
437 * --------------------------------------*/
438 v3f surf0, surf2, n0, n2;
439
440 if( bh_closest_point( world->geo_bh, p[0], surf0, 5.0f ) == -1 )
441 v3_add( (v3f){0.0f,-0.1f,0.0f}, p[0], surf0 );
442
443 if( bh_closest_point( world->geo_bh, p[2], surf2, 5.0f ) == -1 )
444 v3_add( (v3f){0.0f,-0.1f,0.0f}, p[2], surf2 );
445
446 v3_sub( surf0, p[0], n0 );
447 v3_sub( surf2, p[2], n2 );
448 v3_normalize( n0 );
449 v3_normalize( n2 );
450
451 world_routes_place_curve( world, p, n0, n2 );
452
453 /* --- */
454 v4_copy( p[2], p[0] );
455 }
456 }
457
458 scene_copy_slice( world->scene_lines, &route->sm );
459 }
460
461 /*
462 * Create the strips of colour that run through the world along course paths
463 */
464 VG_STATIC void world_routes_generate( world_instance *world )
465 {
466 vg_info( "Generating route meshes\n" );
467 world->scene_lines = scene_init( world->heap, 200000, 300000 );
468
469 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
470 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
471 gate->ref_count = 0;
472 gate->route_count = 0;
473 }
474
475 for( u32 i=0; i<mdl_arrcount(&world->ent_route_node); i++ ){
476 ent_route_node *rn = mdl_arritm( &world->ent_route_node, i );
477 rn->ref_count = 0;
478 rn->ref_total = 0;
479 }
480
481 for( u32 k=0; k<mdl_arrcount(&world->ent_route); k++ ){
482 ent_route *route = mdl_arritm( &world->ent_route, k );
483
484 for( int i=0; i<route->checkpoints_count; i++ ){
485 int i0 = route->checkpoints_start+i,
486 i1 = route->checkpoints_start+((i+1)%route->checkpoints_count);
487
488 ent_checkpoint *c0 = mdl_arritm(&world->ent_checkpoint, i0),
489 *c1 = mdl_arritm(&world->ent_checkpoint, i1);
490
491 ent_gate *start_gate = mdl_arritm( &world->ent_gate, c0->gate_index );
492 start_gate = mdl_arritm( &world->ent_gate, start_gate->target );
493 start_gate->route_count ++;
494
495 if( !c0->path_count )
496 continue;
497
498 for( int j=0; j<c0->path_count; j ++ ){
499 ent_path_index *index = mdl_arritm( &world->ent_path_index,
500 c0->path_start+j );
501 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
502 index->index );
503 rn->ref_total ++;
504 }
505 }
506 }
507
508 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ )
509 world_routes_create_mesh( world, i );
510
511 vg_acquire_thread_sync();
512 {
513 scene_upload( world->scene_lines, &world->mesh_route_lines );
514 }
515 vg_release_thread_sync();
516 vg_linear_del( world->heap, world->scene_lines );
517
518 world_routes_clear( world );
519 }
520
521 /* load all routes from model header */
522 VG_STATIC void world_routes_ent_init( world_instance *world )
523 {
524 vg_info( "Initializing routes\n" );
525
526 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
527 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
528 for( u32 j=0; j<4; j++ ){
529 gate->routes[j] = 0xffff;
530 }
531 }
532
533 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
534 ent_route *route = mdl_arritm(&world->ent_route,i);
535 mdl_transform_m4x3( &route->transform, route->board_transform );
536
537 route->official_track_id = 0xffffffff;
538 for( u32 j=0; j<vg_list_size(track_infos); j ++ ){
539 if( !strcmp(track_infos[j].name,
540 mdl_pstr(&world->meta,route->pstr_name))){
541 route->official_track_id = j;
542 }
543 }
544
545 for( u32 j=0; j<route->checkpoints_count; j++ ){
546 u32 id = route->checkpoints_start + j;
547 ent_checkpoint *cp = mdl_arritm(&world->ent_checkpoint,id);
548
549 ent_gate *gate = mdl_arritm( &world->ent_gate, cp->gate_index );
550
551 for( u32 k=0; k<4; k++ ){
552 if( gate->routes[k] == 0xffff ){
553 gate->routes[k] = i;
554 break;
555 }
556 }
557
558 if( gate->type == k_gate_type_teleport ){
559 gate = mdl_arritm(&world->ent_gate, gate->target );
560
561 for( u32 k=0; k<4; k++ ){
562 if( gate->routes[k] == i ){
563 vg_error( "already assigned route to gate\n" );
564 break;
565 }
566 if( gate->routes[k] == 0xffff ){
567 gate->routes[k] = i;
568 break;
569 }
570 }
571 }
572 }
573 }
574
575 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
576 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
577 }
578
579 world_routes_clear( world );
580 }
581
582 /*
583 * -----------------------------------------------------------------------------
584 * Events
585 * -----------------------------------------------------------------------------
586 */
587
588 VG_STATIC void world_routes_init(void)
589 {
590 world_global.current_run_version = 200;
591 world_global.time = 300.0;
592 world_global.last_use = 0.0;
593
594 shader_scene_route_register();
595 shader_routeui_register();
596 }
597
598 VG_STATIC void world_routes_update( world_instance *world )
599 {
600 world_global.time += vg.time_delta;
601
602 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
603 ent_route *route = mdl_arritm( &world->ent_route, i );
604
605 int target = route->active_checkpoint == 0xffff? 0: 1;
606 route->factive = vg_lerpf( route->factive, target, 0.6f*vg.time_delta );
607 }
608
609 for( u32 i=0; i<world_global.text_particle_count; i++ ){
610 struct text_particle *particle = &world_global.text_particles[i];
611 rb_object_debug( &particle->obj, VG__RED );
612 }
613 }
614
615 VG_STATIC void world_routes_fixedupdate( world_instance *world )
616 {
617 rb_solver_reset();
618
619 for( u32 i=0; i<world_global.text_particle_count; i++ ){
620 struct text_particle *particle = &world_global.text_particles[i];
621
622 if( rb_global_has_space() ){
623 rb_ct *buf = rb_global_buffer();
624
625 int l = rb_sphere__scene( particle->obj.rb.to_world,
626 &particle->obj.inf.sphere,
627 NULL, &world->rb_geo.inf.scene, buf );
628
629 for( int j=0; j<l; j++ ){
630 buf[j].rba = &particle->obj.rb;
631 buf[j].rbb = &world->rb_geo.rb;
632 }
633
634 rb_contact_count += l;
635 }
636 }
637
638 rb_presolve_contacts( rb_contact_buffer, rb_contact_count );
639
640 for( int i=0; i<rb_contact_count; i++ ){
641 rb_contact_restitution( rb_contact_buffer+i, vg_randf() );
642 }
643
644 for( int i=0; i<6; i++ ){
645 rb_solve_contacts( rb_contact_buffer, rb_contact_count );
646 }
647
648 for( u32 i=0; i<world_global.text_particle_count; i++ ){
649 struct text_particle *particle = &world_global.text_particles[i];
650 rb_iter( &particle->obj.rb );
651 }
652
653 for( u32 i=0; i<world_global.text_particle_count; i++ ){
654 struct text_particle *particle = &world_global.text_particles[i];
655 rb_update_transform( &particle->obj.rb );
656 }
657 }
658
659 VG_STATIC void bind_terrain_noise(void);
660 VG_STATIC void world_bind_light_array( world_instance *world,
661 GLuint shader, GLuint location,
662 int slot );
663 VG_STATIC void world_bind_light_index( world_instance *world,
664 GLuint shader, GLuint location,
665 int slot );
666
667 VG_STATIC void world_routes_update_timer_texts( world_instance *world )
668 {
669 world_global.timer_text_count = 0;
670
671 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
672 ent_route *route = mdl_arritm( &world->ent_route, i );
673
674 if( route->active_checkpoint != 0xffff ){
675 u32 next = route->active_checkpoint+1;
676 next = next % route->checkpoints_count;
677 next += route->checkpoints_start;
678
679 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint, next );
680 ent_gate *gate = mdl_arritm( &world->ent_gate, cp->gate_index );
681 ent_gate *dest = mdl_arritm( &world->ent_gate, gate->target );
682
683 u32 j=0;
684 for( ; j<4; j++ ){
685 if( dest->routes[j] == i ){
686 break;
687 }
688 }
689
690 float h0 = 0.8f,
691 h1 = 1.2f,
692 depth = 0.4f,
693 size = 0.4f;
694
695 struct timer_text *text =
696 &world_global.timer_texts[ world_global.timer_text_count ++ ];
697
698 text->gate = gate;
699 text->route = route;
700
701 if( route->valid_checkpoints >= route->checkpoints_count ){
702 double lap_time = world_global.time - route->timing_base,
703 time_centiseconds = lap_time * 100.0;
704
705 if( time_centiseconds > (float)0xfffe ) time_centiseconds = 0.0;
706
707 u16 centiseconds = time_centiseconds,
708 seconds = centiseconds / 100,
709 minutes = seconds / 60;
710
711 centiseconds %= 100;
712 seconds %= 60;
713 minutes %= 60;
714
715 if( minutes > 9 ) minutes = 9;
716
717 int j=0;
718 if( minutes ){
719 highscore_intr( text->text, minutes, 1, ' ' ); j++;
720 text->text[j++] = ':';
721 }
722
723 if( seconds >= 10 || minutes ){
724 highscore_intr( text->text+j, seconds, 2, '0' ); j+=2;
725 }
726 else{
727 highscore_intr( text->text+j, seconds, 1, '0' ); j++;
728 }
729
730 text->text[j++] = '.';
731 highscore_intr( text->text+j, centiseconds, 1, '0' ); j++;
732 text->text[j] = '\0';
733 }
734 else{
735 highscore_intr( text->text, route->valid_checkpoints, 1, ' ' );
736 text->text[1] = '/';
737 highscore_intr( text->text+2, route->checkpoints_count+1, 1, ' ' );
738 text->text[3] = '\0';
739 }
740
741 float align_r = font3d_string_width( &world_global.font, 0,
742 text->text );
743 align_r *= size;
744
745 v3f positions[] = {
746 { -0.92f, h0, depth },
747 { 0.92f - align_r, h0, depth },
748 { -0.92f, h1, depth },
749 { 0.92f - align_r, h1, depth },
750 };
751
752 if( dest->route_count == 1 ){
753 positions[0][0] = -align_r*0.5f;
754 positions[0][1] = h1;
755 }
756
757 m3x3_copy( gate->to_world, text->transform );
758 float ratio = v3_length(text->transform[0]) /
759 v3_length(text->transform[1]);
760
761 m3x3_scale( text->transform, (v3f){ size, size*ratio, 0.1f } );
762 m4x3_mulv( gate->to_world, positions[j], text->transform[3] );
763 }
764 }
765 }
766
767 VG_STATIC void world_routes_fracture( world_instance *world, ent_gate *gate,
768 v3f imp_co, v3f imp_v )
769 {
770 world_global.text_particle_count = 0;
771
772 for( u32 i=0; i<world_global.timer_text_count; i++ ){
773 struct timer_text *text = &world_global.timer_texts[i];
774
775 if( text->gate != gate ) continue;
776
777 m4x3f transform;
778 m4x3_mul( gate->transport, text->transform, transform );
779
780 v3f co, s;
781 v4f q;
782 m4x3_decompose( transform, co, q, s );
783
784 v3f offset;
785 v3_zero( offset );
786
787 v4f colour;
788 float brightness = 0.3f + world->ub_lighting.g_day_phase;
789 v3_muls( text->route->colour, brightness, colour );
790 colour[3] = 1.0f-text->route->factive;
791
792 for( u32 j=0;; j++ ){
793 char c = text->text[j];
794 if( !c ) break;
795
796 ent_glyph *glyph = font3d_glyph( &world_global.font, 0, c );
797 if( !glyph ) continue;
798
799 if( c >= (u32)'0' && c <= (u32)'9' && glyph->indice_count ){
800 struct text_particle *particle =
801 &world_global.text_particles[world_global.text_particle_count++];
802
803 particle->glyph = glyph;
804 v4_copy( colour, particle->colour );
805
806 v3f origin;
807 v2_muls( glyph->size, 0.5f, origin );
808 origin[2] = -0.5f;
809
810 v3f world_co;
811
812 v3_add( offset, origin, world_co );
813 m4x3_mulv( transform, world_co, world_co );
814
815 float r = vg_maxf( s[0]*glyph->size[0], s[1]*glyph->size[1] )*0.5f;
816
817 m3x3_identity( particle->mlocal );
818 m3x3_scale( particle->mlocal, s );
819 origin[2] *= s[2];
820 v3_muls( origin, -1.0f, particle->mlocal[3] );
821
822 v3_copy( world_co, particle->obj.rb.co );
823 v3_muls( imp_v, 1.0f+vg_randf(), particle->obj.rb.v );
824 particle->obj.rb.v[1] += 2.0f;
825
826 v4_copy( q, particle->obj.rb.q );
827 particle->obj.rb.w[0] = vg_randf()*2.0f-1.0f;
828 particle->obj.rb.w[1] = vg_randf()*2.0f-1.0f;
829 particle->obj.rb.w[2] = vg_randf()*2.0f-1.0f;
830
831 particle->obj.type = k_rb_shape_sphere;
832 particle->obj.inf.sphere.radius = r*0.6f;
833
834 rb_init_object( &particle->obj );
835 }
836 offset[0] += glyph->size[0];
837 }
838 }
839 }
840
841 VG_STATIC void render_world_routes( world_instance *world, camera *cam,
842 int layer_depth )
843 {
844 m4x3f identity_matrix;
845 m4x3_identity( identity_matrix );
846
847 shader_scene_route_use();
848 shader_scene_route_uTexGarbage(0);
849 world_link_lighting_ub( world, _shader_scene_route.id );
850 world_bind_position_texture( world, _shader_scene_route.id,
851 _uniform_scene_route_g_world_depth, 2 );
852 world_bind_light_array( world, _shader_scene_route.id,
853 _uniform_scene_route_uLightsArray, 3 );
854 world_bind_light_index( world, _shader_scene_route.id,
855 _uniform_scene_route_uLightsIndex, 4 );
856 bind_terrain_noise();
857
858 shader_scene_route_uPv( cam->mtx.pv );
859 shader_scene_route_uPvmPrev( cam->mtx_prev.pv );
860 shader_scene_route_uMdl( identity_matrix );
861 shader_scene_route_uCamera( cam->transform[3] );
862
863 mesh_bind( &world->mesh_route_lines );
864
865 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
866 ent_route *route = mdl_arritm( &world->ent_route, i );
867
868 v4f colour;
869 v3_lerp( (v3f){0.7f,0.7f,0.7f}, route->colour, route->factive, colour );
870 colour[3] = route->factive*0.2f;
871
872 shader_scene_route_uColour( colour );
873 mdl_draw_submesh( &route->sm );
874 }
875
876 /* timers
877 * ---------------------------------------------------- */
878 if( layer_depth == 0 ){
879 font3d_bind( &world_global.font, cam );
880
881 for( u32 i=0; i<world_global.timer_text_count; i++ ){
882 struct timer_text *text = &world_global.timer_texts[i];
883
884 v4f colour;
885 float brightness = 0.3f + world->ub_lighting.g_day_phase;
886 v3_muls( text->route->colour, brightness, colour );
887 colour[3] = 1.0f-text->route->factive;
888
889 shader_model_font_uColour( colour );
890 font3d_simple_draw( &world_global.font, 0, text->text,
891 cam, text->transform );
892 }
893
894 shader_model_font_uOffset( (v3f){0.0f,0.0f,0.0f} );
895
896 for( u32 i=0; i<world_global.text_particle_count; i++ ){
897 struct text_particle *particle = &world_global.text_particles[i];
898
899 m4x4f prev_mtx;
900
901 m4x3_expand( particle->mdl, prev_mtx );
902 m4x4_mul( cam->mtx_prev.pv, prev_mtx, prev_mtx );
903
904 shader_model_font_uPvmPrev( prev_mtx );
905
906 v4f q;
907 m4x3f model;
908 rb_extrapolate( &particle->obj.rb, model[3], q );
909 q_m3x3( q, model );
910
911 m4x3_mul( model, particle->mlocal, particle->mdl );
912 shader_model_font_uMdl( particle->mdl );
913 shader_model_font_uColour( particle->colour );
914
915 mesh_drawn( particle->glyph->indice_start,
916 particle->glyph->indice_count );
917 }
918 }
919
920 /* gate markers
921 * ---------------------------------------------------- */
922
923 shader_model_gate_use();
924 shader_model_gate_uPv( cam->mtx.pv );
925 shader_model_gate_uCam( cam->pos );
926 shader_model_gate_uTime( vg.time*0.25f );
927 shader_model_gate_uInvRes( (v2f){
928 1.0f / (float)vg.window_x,
929 1.0f / (float)vg.window_y });
930
931 mesh_bind( &world_global.mesh_gate );
932
933 /* skip writing into the motion vectors for this */
934 glDrawBuffers( 1, (GLenum[]){ GL_COLOR_ATTACHMENT0 } );
935
936 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
937 ent_route *route = mdl_arritm( &world->ent_route, i );
938
939 if( route->active_checkpoint != 0xffff ){
940 v4f colour;
941 float brightness = 0.3f + world->ub_lighting.g_day_phase;
942 v3_muls( route->colour, brightness, colour );
943 colour[3] = 1.0f-route->factive;
944
945 shader_model_gate_uColour( colour );
946
947 u32 next = route->active_checkpoint+1+layer_depth;
948 next = next % route->checkpoints_count;
949 next += route->checkpoints_start;
950
951 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint, next );
952 ent_gate *gate = mdl_arritm( &world->ent_gate, cp->gate_index );
953 shader_model_gate_uMdl( gate->to_world );
954
955 for( u32 j=0; j<4; j++ ){
956 if( gate->routes[j] == i ){
957 mdl_draw_submesh( &world_global.sm_gate_marker[j] );
958 break;
959 }
960 }
961 }
962 }
963 glDrawBuffers( 2, (GLenum[]){ GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 } );
964 }
965
966 #endif /* ROUTES_H */