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