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