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