view weekly/all-time
[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 "pointcloud.h"
18 #include "gui.h"
19 #include "steam.h"
20 #include "network_msg.h"
21 #include "network_common.h"
22
23 #include "shaders/scene_route.h"
24 #include "shaders/routeui.h"
25
26 #if 0
27 static
28 void world_routes_local_set_record( world_instance *world, ent_route *route,
29 f64 lap_time )
30 {
31 vg_success( " NEW LAP TIME: %f\n", lap_time );
32
33 if( route->anon.official_track_id != 0xffffffff ){
34 double time_centiseconds = lap_time * 100.0;
35 if( time_centiseconds > (float)0xfffe ) /* skill issue */
36 return;
37
38 struct track_info *ti = &track_infos[ route->anon.official_track_id ];
39 highscore_record *record = &ti->record;
40 record->trackid = route->anon.official_track_id;
41 record->datetime = time(NULL);
42 record->playerid = 0;
43 record->points = 0;
44 record->time = time_centiseconds;
45 ti->push = 1;
46
47 if( ti->achievement_id ){
48 steam_set_achievement( ti->achievement_id );
49 steam_store_achievements();
50 }
51 }
52 else{
53 vg_warn( "There is no associated track for this record...\n" );
54 }
55 }
56 #endif
57
58 static void world_routes_clear( world_instance *world )
59 {
60 for( u32 i=0; i<mdl_arrcount( &world->ent_route ); i++ ){
61 ent_route *route = mdl_arritm( &world->ent_route, i );
62 route->active_checkpoint = 0xffff;
63 }
64
65 for( u32 i=0; i<mdl_arrcount( &world->ent_gate ); i++ ){
66 ent_gate *rg = mdl_arritm( &world->ent_gate, i );
67 rg->timing_version = 0;
68 rg->timing_time = 0.0;
69 }
70
71 world_static.current_run_version += 4;
72 world_static.last_use = 0.0;
73 }
74
75 static void world_routes_time_lap( world_instance *world, ent_route *route )
76 {
77 vg_info( "------- time lap %s -------\n",
78 mdl_pstr(&world->meta,route->pstr_name) );
79
80 double start_time = 0.0;
81 u32 last_version=0;
82
83 u32 valid_count=0;
84
85 for( u32 i=0; i<route->checkpoints_count; i++ ){
86 u32 cpid = (i+route->active_checkpoint) % route->checkpoints_count;
87 cpid += route->checkpoints_start;
88
89 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint, cpid );
90 ent_gate *rg = mdl_arritm( &world->ent_gate, cp->gate_index );
91 rg = mdl_arritm( &world->ent_gate, rg->target );
92
93 if( i == 1 ){
94 route->timing_base = rg->timing_time;
95 }
96
97 if( i == 0 )
98 start_time = rg->timing_time;
99 else{
100 if( last_version+1 == rg->timing_version ) valid_count ++;
101 else valid_count = 0;
102 }
103
104 last_version = rg->timing_version;
105 vg_info( "%u %f\n", rg->timing_version, rg->timing_time );
106 }
107
108 if( world_static.current_run_version == last_version+1 ){
109 valid_count ++;
110
111 if( route->checkpoints_count == 1 ){
112 route->timing_base = world_static.time;
113 }
114 }
115 else valid_count = 0;
116
117 vg_info( "%u %f\n", world_static.current_run_version, world_static.time );
118
119 if( valid_count==route->checkpoints_count ){
120 f64 lap_time = world_static.time - start_time;
121 //world_routes_local_set_record( world, route, lap_time );
122
123 if( route->anon.official_track_id != 0xffffffff ){
124 struct track_info *ti = &track_infos[ route->anon.official_track_id ];
125 if( ti->achievement_id ){
126 steam_set_achievement( ti->achievement_id );
127 steam_store_achievements();
128 }
129 }
130
131 addon_alias *alias =
132 &world_static.instance_addons[ world_static.active_instance ]->alias;
133
134 char mod_uid[ ADDON_UID_MAX ];
135 addon_alias_uid( alias, mod_uid );
136 network_publish_laptime( mod_uid,
137 mdl_pstr( &world->meta, route->pstr_name ),
138 lap_time );
139 }
140
141 route->valid_checkpoints = valid_count+1;
142
143 vg_info( "valid: %u\n", valid_count );
144 vg_info( "----------------------------\n" );
145 }
146
147 /*
148 * When going through a gate this is called for bookkeeping purposes
149 */
150 static void world_routes_activate_entry_gate( world_instance *world,
151 ent_gate *rg )
152 {
153 world_static.last_use = world_static.time;
154
155 /* disable all routes and leave the world */
156 if( rg->flags & k_ent_gate_nonlocal ){
157 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
158 ent_route *route = mdl_arritm( &world->ent_route, i );
159 route->active_checkpoint = 0xffff;
160 }
161 return;
162 }
163
164 ent_gate *dest = mdl_arritm( &world->ent_gate, rg->target );
165
166 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
167 ent_route *route = mdl_arritm( &world->ent_route, i );
168
169 u32 active_prev = route->active_checkpoint;
170 route->active_checkpoint = 0xffff;
171
172 for( u32 j=0; j<4; j++ ){
173 if( dest->routes[j] == i ){
174 for( u32 k=0; k<route->checkpoints_count; k++ ){
175 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint,
176 route->checkpoints_start+k );
177
178 ent_gate *gk = mdl_arritm( &world->ent_gate, cp->gate_index );
179 gk = mdl_arritm( &world->ent_gate, gk->target );
180 if( gk == dest ){
181 route->active_checkpoint = k;
182 world_routes_time_lap( world, route );
183 break;
184 }
185 }
186 break;
187 }
188 }
189 }
190
191 dest->timing_version = world_static.current_run_version;
192 dest->timing_time = world_static.time;
193
194 world_static.current_run_version ++;
195 }
196
197 /* draw lines along the paths */
198 static void world_routes_debug( world_instance *world )
199 {
200 for( u32 i=0; i<mdl_arrcount(&world->ent_route_node); i++ ){
201 ent_route_node *rn = mdl_arritm(&world->ent_route_node,i);
202 vg_line_point( rn->co, 0.25f, VG__WHITE );
203 }
204
205 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
206 ent_route *route = mdl_arritm(&world->ent_route, i);
207
208 u32 colours[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
209 0xff5442f5 };
210
211 u32 cc = 0xffcccccc;
212 if( route->active_checkpoint != 0xffff ){
213 cc = colours[i%vg_list_size(colours)];
214 }
215
216 for( int i=0; i<route->checkpoints_count; i++ ){
217 int i0 = route->checkpoints_start+i,
218 i1 = route->checkpoints_start+((i+1)%route->checkpoints_count);
219
220 ent_checkpoint *c0 = mdl_arritm(&world->ent_checkpoint, i0),
221 *c1 = mdl_arritm(&world->ent_checkpoint, i1);
222
223 ent_gate *start_gate = mdl_arritm( &world->ent_gate, c0->gate_index );
224 ent_gate *end_gate = mdl_arritm( &world->ent_gate, c1->gate_index );
225
226 v3f p0, p1;
227 v3_copy( start_gate->co[1], p0 );
228
229 for( int j=0; j<c0->path_count; j ++ ){
230 ent_path_index *index = mdl_arritm( &world->ent_path_index,
231 c0->path_start+j );
232
233 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
234 index->index );
235
236 v3_copy( rn->co, p1 );
237 vg_line( p0, p1, cc );
238 v3_copy( p1, p0 );
239 }
240
241 v3_copy( end_gate->co[0], p1 );
242 vg_line( p0, p1, cc );
243 }
244 }
245 }
246
247 static
248 void world_routes_pointcloud_spot( world_instance *world,
249 pointcloud_buffer *pcbuf,
250 v3f co, f32 radius, u32 samples, v4f colour )
251 {
252 v3f inv_ext;
253 v3_sub( pcbuf->boundary[1], pcbuf->boundary[0], inv_ext );
254 v3_div( (v3f){1.0f,1.0f,1.0f}, inv_ext, inv_ext );
255
256 for( u32 j=0; j<samples; j++ ){
257 if( pcbuf->count >= pcbuf->max )
258 return;
259
260 pointcloud_vert *vert = &pcbuf->buf[ pcbuf->count ++ ];
261
262 v3f sample, jitter, point;
263 vg_rand_sphere( jitter );
264 v3_muladds( co, jitter, radius, sample );
265
266 if( bh_closest_point( world->geo_bh, sample, point, radius*1.5f ) == -1 ){
267 v3_copy( sample, point );
268 }
269
270 v3f pos;
271 v3_sub( point, pcbuf->boundary[0], pos );
272 v3_mul( pos, inv_ext, pos );
273
274 float dist = 1.0f-(v3_length(jitter));
275
276 v4f final_colour;
277 v4_muls( colour, dist*dist, final_colour );
278
279 pointcloud_packvert( vert, pos, final_colour );
280 }
281 }
282
283 /*
284 * '
285 * .
286 * |
287 * |
288 * /#\
289 * -'###`-
290 */
291 static
292 void world_routes_pointcloud_tower( world_instance *world,
293 pointcloud_buffer *pcbuf,
294 v3f co, f32 radius, f32 height,
295 u32 samples, v4f colour )
296 {
297 v3f inv_ext;
298 v3_sub( pcbuf->boundary[1], pcbuf->boundary[0], inv_ext );
299 v3_div( (v3f){1.0f,1.0f,1.0f}, inv_ext, inv_ext );
300
301 for( u32 j=0; j<samples; j++ ){
302 if( pcbuf->count >= pcbuf->max )
303 return;
304
305 pointcloud_vert *vert = &pcbuf->buf[ pcbuf->count ++ ];
306
307 v3f point;
308 point[0] = vg_randf64()*2.0f-1.0f;
309 point[1] = 0.0f;
310 point[2] = vg_randf64()*2.0f-1.0f;
311 v3_normalize( point );
312 v3_muls( point, sqrtf(vg_randf64()), point );
313
314 f32 h = vg_randf64();
315 point[1] = h*h*h*height;
316 point[0] *= radius;
317 point[2] *= radius;
318
319 v3_add( point, co, point );
320 v3_sub( point, pcbuf->boundary[0], point );
321 v3_mul( point, inv_ext, point );
322
323 pointcloud_packvert( vert, point, colour );
324 }
325 }
326
327 static
328 void world_routes_place_curve( world_instance *world, ent_route *route,
329 v4f h[3], v3f n0, v3f n2, scene_context *scene,
330 pointcloud_buffer *pcbuf )
331 {
332 float t;
333 v3f p, pd;
334 int last_valid=0;
335
336 float total_length = 0.0f,
337 travel_length = 0.0;
338
339 v3f last;
340 v3_copy( h[0], last );
341 for( int it=0; it<128; it ++ ){
342 t = (float)(it+1) * (1.0f/128.0f);
343 eval_bezier3( h[0], h[1], h[2], t, p );
344 total_length += v3_dist( p, last );
345 v3_copy( p, last );
346 }
347
348 float patch_size = 4.0f,
349 patch_count = ceilf( total_length / patch_size );
350
351 t = 0.0f;
352 v3_copy( h[0], last );
353
354 for( int it=0; it<128; it ++ ){
355 float const k_sample_dist = 0.0025f,
356 k_line_width = 1.5f;
357
358 eval_bezier3( h[0], h[1], h[2], t, p );
359 eval_bezier3( h[0], h[1], h[2], t+k_sample_dist, pd );
360
361 travel_length += v3_dist( p, last );
362
363 float mod = k_sample_dist / v3_dist( p, pd );
364
365 v3f v0,up, right;
366
367 v3_muls( n0, -(1.0f-t), up );
368 v3_muladds( up, n2, -t, up );
369 v3_normalize( up );
370
371 v3_sub( pd,p,v0 );
372 v3_cross( up, v0, right );
373 v3_normalize( right );
374
375 float cur_x = (1.0f-t)*h[0][3] + t*h[2][3];
376
377 v3f sc, sa, sb, down;
378 v3_muladds( p, right, cur_x * k_line_width, sc );
379 v3_muladds( sc, up, 1.5f, sc );
380 v3_muladds( sc, right, k_line_width*0.95f, sa );
381 v3_muladds( sc, right, 0.0f, sb );
382 v3_muls( up, -1.0f, down );
383
384 ray_hit ha, hb;
385 ha.dist = 8.0f;
386 hb.dist = 8.0f;
387
388 int resa = ray_world( world, sa, down, &ha, k_material_flag_ghosts ),
389 resb = ray_world( world, sb, down, &hb, k_material_flag_ghosts );
390
391 if( pcbuf && resa ){
392 world_routes_pointcloud_spot( world, pcbuf, ha.pos,
393 12.0f, 10, route->colour );
394 }
395
396 if( resa && resb ){
397 struct world_surface *surfa = ray_hit_surface( world, &ha ),
398 *surfb = ray_hit_surface( world, &hb );
399
400 if( (surfa->info.flags & k_material_flag_skate_target) &&
401 (surfb->info.flags & k_material_flag_skate_target) )
402 {
403 scene_vert va, vb;
404
405 float gap = vg_fractf(cur_x*0.5f)*0.02f;
406
407 v3_muladds( ha.pos, up, 0.06f+gap, va.co );
408 v3_muladds( hb.pos, up, 0.06f+gap, vb.co );
409
410 scene_vert_pack_norm( &va, up );
411 scene_vert_pack_norm( &vb, up );
412
413 float t1 = (travel_length / total_length) * patch_count;
414 va.uv[0] = t1;
415 va.uv[1] = 0.0f;
416 vb.uv[0] = t1;
417 vb.uv[1] = 1.0f;
418
419 scene_push_vert( scene, &va );
420 scene_push_vert( scene, &vb );
421
422 if( last_valid ){
423 /* Connect them with triangles */
424 scene_push_tri( scene, (u32[3]){
425 last_valid+0-2, last_valid+1-2, last_valid+2-2} );
426 scene_push_tri( scene, (u32[3]){
427 last_valid+1-2, last_valid+3-2, last_valid+2-2} );
428 }
429
430 last_valid = scene->vertex_count;
431 }
432 else
433 last_valid = 0;
434 }
435 else
436 last_valid = 0;
437
438 if( t == 1.0f )
439 return;
440
441 t += 1.0f*mod;
442 if( t > 1.0f )
443 t = 1.0f;
444
445 v3_copy( p, last );
446 }
447 }
448
449 static void world_routes_gen_meshes( world_instance *world, u32 route_id,
450 scene_context *sc,
451 pointcloud_buffer *pcbuf )
452 {
453 ent_route *route = mdl_arritm( &world->ent_route, route_id );
454 u8 colour[4];
455 colour[0] = route->colour[0] * 255.0f;
456 colour[1] = route->colour[1] * 255.0f;
457 colour[2] = route->colour[2] * 255.0f;
458 colour[3] = route->colour[3] * 255.0f;
459
460 u32 last_valid = 0;
461
462 for( int i=0; i<route->checkpoints_count; i++ ){
463 int i0 = route->checkpoints_start+i,
464 i1 = route->checkpoints_start+((i+1)%route->checkpoints_count);
465
466 ent_checkpoint *c0 = mdl_arritm(&world->ent_checkpoint, i0),
467 *c1 = mdl_arritm(&world->ent_checkpoint, i1);
468
469 ent_gate *start_gate = mdl_arritm( &world->ent_gate, c0->gate_index );
470 start_gate = mdl_arritm( &world->ent_gate, start_gate->target );
471
472 ent_gate *end_gate = mdl_arritm( &world->ent_gate, c1->gate_index ),
473 *collector = mdl_arritm( &world->ent_gate, end_gate->target );
474
475 v4f p[3];
476
477 v3_add( (v3f){0.0f,0.1f,0.0f}, start_gate->co[0], p[0] );
478 p[0][3] = start_gate->ref_count;
479 p[0][3] -= (float)start_gate->route_count * 0.5f;
480 start_gate->ref_count ++;
481
482 if( !c0->path_count )
483 continue;
484
485 /* this is so that we get nice flow through the gates */
486 v3f temp_alignments[2];
487 ent_gate *both[] = { start_gate, end_gate };
488
489 for( int j=0; j<2; j++ ){
490 int pi = c0->path_start + ((j==1)? c0->path_count-1: 0);
491
492 ent_path_index *index = mdl_arritm( &world->ent_path_index, pi );
493 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
494 index->index );
495 v3f v0;
496 v3_sub( rn->co, both[j]->co[0], v0 );
497 float d = v3_dot( v0, both[j]->to_world[2] );
498
499 v3_muladds( both[j]->co[0], both[j]->to_world[2], d,
500 temp_alignments[j] );
501 v3_add( (v3f){0.0f,0.1f,0.0f}, temp_alignments[j], temp_alignments[j]);
502 }
503
504
505 for( int j=0; j<c0->path_count; j ++ ){
506 ent_path_index *index = mdl_arritm( &world->ent_path_index,
507 c0->path_start+j );
508 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
509 index->index );
510 if( j==0 || j==c0->path_count-1 )
511 if( j == 0 )
512 v3_copy( temp_alignments[0], p[1] );
513 else
514 v3_copy( temp_alignments[1], p[1] );
515 else
516 v3_copy( rn->co, p[1] );
517
518 p[1][3] = rn->ref_count;
519 p[1][3] -= (float)rn->ref_total * 0.5f;
520 rn->ref_count ++;
521
522 if( j+1 < c0->path_count ){
523 index = mdl_arritm( &world->ent_path_index,
524 c0->path_start+j+1 );
525 rn = mdl_arritm( &world->ent_route_node, index->index );
526
527 if( j+1 == c0->path_count-1 )
528 v3_lerp( p[1], temp_alignments[1], 0.5f, p[2] );
529 else
530 v3_lerp( p[1], rn->co, 0.5f, p[2] );
531
532 p[2][3] = rn->ref_count;
533 p[2][3] -= (float)rn->ref_total * 0.5f;
534 }
535 else{
536 v3_copy( end_gate->co[0], p[2] );
537 v3_add( (v3f){0.0f,0.1f,0.0f}, p[2], p[2] );
538 p[2][3] = collector->ref_count;
539
540 if( i == route->checkpoints_count-1)
541 p[2][3] -= 1.0f;
542
543 p[2][3] -= (float)collector->route_count * 0.5f;
544 //collector->ref_count ++;
545 }
546
547 /* p0,p1,p2 bezier patch is complete
548 * --------------------------------------*/
549 v3f surf0, surf2, n0, n2;
550
551 if( bh_closest_point( world->geo_bh, p[0], surf0, 5.0f ) == -1 )
552 v3_add( (v3f){0.0f,-0.1f,0.0f}, p[0], surf0 );
553
554 if( bh_closest_point( world->geo_bh, p[2], surf2, 5.0f ) == -1 )
555 v3_add( (v3f){0.0f,-0.1f,0.0f}, p[2], surf2 );
556
557 v3_sub( surf0, p[0], n0 );
558 v3_sub( surf2, p[2], n2 );
559 v3_normalize( n0 );
560 v3_normalize( n2 );
561
562 world_routes_place_curve( world, route, p, n0, n2, sc, pcbuf );
563
564 /* --- */
565 v4_copy( p[2], p[0] );
566 }
567 }
568
569 scene_copy_slice( sc, &route->sm );
570 }
571
572 static
573 struct world_surface *world_tri_index_surface( world_instance *world,
574 u32 index );
575
576 static f64 world_routes_scatter_surface_points( world_instance *world,
577 pointcloud_buffer *pcbuf,
578 f32 rate )
579 {
580 static f32 densities[] = {
581 [k_surface_prop_concrete] = 2.0f,
582 [k_surface_prop_grass] = 0.8f,
583 [k_surface_prop_metal] = 1.0f,
584 [k_surface_prop_wood] = 2.5f,
585 [k_surface_prop_tiles] = 4.0f
586 };
587
588 /* calculate total area */
589 f64 total_area = 0.0f;
590 for( u32 i=0; i<world->scene_geo.indice_count/3; i++ ){
591 u32 *tri = &world->scene_geo.arrindices[i*3];
592 struct world_surface *surf = world_tri_index_surface( world, tri[0] );
593
594 if( surf->info.shader == k_shader_boundary ||
595 surf->info.shader == k_shader_invisible ) continue;
596
597 if( !(surf->info.flags & k_material_flag_preview_visibile) ) continue;
598
599 scene_vert *va = &world->scene_geo.arrvertices[tri[0]],
600 *vb = &world->scene_geo.arrvertices[tri[1]],
601 *vc = &world->scene_geo.arrvertices[tri[2]];
602
603 v3f v0, v1, vn;
604 v3_sub( vb->co, va->co, v0 );
605 v3_sub( vc->co, va->co, v1 );
606 v3_cross( v0, v1, vn );
607 if( vn[1] < 0.0f ) continue;
608
609 f32 density = 1.0f;
610 if( surf->info.surface_prop < vg_list_size(densities) )
611 density = densities[surf->info.surface_prop];
612 total_area += v3_length(vn)*0.5f*density;
613 }
614
615 f32 accum = 0.0f;
616
617 u8 colour[] = { 80,80,80,255 };
618 v3f light_dir = {0.3f,0.8f,0.1f};
619 v3_normalize( light_dir );
620
621 v3f inv_ext;
622 v3_sub( pcbuf->boundary[1], pcbuf->boundary[0], inv_ext );
623 v3_div( (v3f){1.0f,1.0f,1.0f}, inv_ext, inv_ext );
624
625 for( u32 i=0; i<world->scene_geo.indice_count/3; i++ ){
626 u32 *tri = &world->scene_geo.arrindices[i*3];
627 struct world_surface *surf = world_tri_index_surface( world, tri[0] );
628
629 if( surf->info.shader == k_shader_boundary ||
630 surf->info.shader == k_shader_invisible ) continue;
631
632 if( !(surf->info.flags & k_material_flag_preview_visibile) ) continue;
633
634 scene_vert *va = &world->scene_geo.arrvertices[tri[0]],
635 *vb = &world->scene_geo.arrvertices[tri[1]],
636 *vc = &world->scene_geo.arrvertices[tri[2]];
637
638 v3f v0, v1, vn;
639 v3_sub( vb->co, va->co, v0 );
640 v3_sub( vc->co, va->co, v1 );
641 v3_cross( v0, v1, vn );
642 if( vn[1] < 0.0f ) continue;
643
644 f32 density = 1.0f;
645 if( surf->info.surface_prop < vg_list_size(densities) )
646 density = densities[surf->info.surface_prop];
647
648 f32 area = v3_length(vn)*0.5f*density;
649 accum += area;
650
651 v3_normalize( vn );
652
653 while( accum > rate ){
654 accum -= rate;
655
656 if( pcbuf->count >= pcbuf->max ) return total_area;
657
658 v2f co = { vg_randf64(), vg_randf64() };
659 if( v2_length2(co) > 0.5f ){
660 co[0] = 1.0f-co[0];
661 co[1] = 1.0f-co[1];
662 }
663
664 v3f pt;
665 v3_muls( v0, co[0], pt );
666 v3_muladds( pt, v1, co[1], pt );
667 v3_add( va->co, pt, pt );
668
669 if( pt[1] < world->water.height ) continue;
670 pointcloud_vert *vert = &pcbuf->buf[ pcbuf->count ++ ];
671
672 v3f pos;
673 v3_sub( pt, pcbuf->boundary[0], pos );
674 v3_mul( pos, inv_ext, pos );
675
676 static v4f colours[] = {
677 [k_surface_prop_concrete] = { 0.13, 0.15, 0.17, 1.0 },
678 [k_surface_prop_grass] = { 0.07, 0.1, 0.14, 1.0 },
679 [k_surface_prop_metal] = { 0.15, 0.19, 0.22, 1.0 },
680 [k_surface_prop_wood] = { 0.1, 0.13, 0.17, 1.0 },
681 [k_surface_prop_tiles] = { 0.05, 0.06, 0.07, 1.0 },
682 };
683
684 v4f col = {0.0f,0.0f,0.0f,0.0f};
685 if( surf->info.surface_prop < vg_list_size(colours) )
686 v4_copy( colours[surf->info.surface_prop], col );
687
688 f32 brightness = v3_dot(vn,light_dir)*0.5f+0.5f;
689 v3_muls( col, brightness, col );
690
691 pointcloud_packvert( vert, pos, col );
692 }
693 }
694
695 return total_area;
696 }
697
698 static void world_routes_surface_grid( world_instance *world,
699 pointcloud_buffer *pcbuf )
700 {
701 i32 const k_gridlines = 32,
702 k_gridres = 255;
703
704 v3f inv_ext;
705 v3_sub( pcbuf->boundary[1], pcbuf->boundary[0], inv_ext );
706 v3_div( (v3f){1.0f,1.0f,1.0f}, inv_ext, inv_ext );
707 v4f colour = {0.2f,0.2f,0.2f,1.0f};
708 v3f dir = {0.0f,-1.0f,0.0f};
709
710 for( u32 k=0; k<2; k++ ){
711 u32 a = k*2,
712 b = (k^0x1)*2;
713
714 for( i32 x=0; x<=k_gridlines; x++ ){
715 f32 t = (float)x / (float)k_gridlines,
716 px = vg_lerpf( pcbuf->boundary[0][a], pcbuf->boundary[1][a], t );
717
718 for( i32 z=0; z<=k_gridres; z++ ){
719 f32 tz = (float)z / (float)k_gridres,
720 pz = vg_lerpf(pcbuf->boundary[0][b],pcbuf->boundary[1][b], tz);
721
722 v3f ro, hit;
723 ro[a] = px;
724 ro[1] = 1000.0f;
725 ro[b] = pz;
726
727 bh_iter it;
728 bh_iter_init_ray( 0, &it, ro, dir, INFINITY );
729 i32 idx;
730
731 while( bh_next( world->geo_bh, &it, &idx ) ){
732 u32 *tri = &world->scene_geo.arrindices[ idx*3 ];
733 v3f vs[3];
734
735 u16 mask = k_material_flag_preview_visibile;
736 if( !(world->scene_geo.arrvertices[tri[0]].flags & mask) )
737 continue;
738
739 for( u32 i=0; i<3; i++ ){
740 v3_copy( world->scene_geo.arrvertices[tri[i]].co, vs[i] );
741 }
742
743 f32 t;
744 if( ray_tri( vs, ro, dir, &t ) ){
745 v3_muladds( ro, dir, t, hit );
746
747 if( world->water.enabled )
748 if( hit[1] < world->water.height )
749 continue;
750
751 if( pcbuf->count >= pcbuf->max ) return;
752
753 pointcloud_vert *vert = &pcbuf->buf[ pcbuf->count ++ ];
754
755 v3f co;
756 v3_sub( hit, pcbuf->boundary[0], co );
757 v3_mul( co, inv_ext, co );
758
759 pointcloud_packvert( vert, co, colour );
760 }
761 }
762 }
763 }
764 }
765 }
766
767 static void world_write_preview( addon_reg *reg, pointcloud_buffer *pcbuf ){
768 if( reg->alias.workshop_id ) return;
769
770 /*
771 * FIXME: BUG: cannot correctly handle workshop because there is a stalling
772 * call below, which deadlocks the scene upload. TODO: improve the async
773 * stack to handle out of order execution. MAYBE
774 */
775
776 char path_buf[4096];
777 vg_str path;
778 vg_strnull( &path, path_buf, 4096 );
779
780 addon_get_content_folder( reg, &path );
781 vg_strcat( &path, "/preview.bin" );
782
783 if( !vg_strgood( &path ) ) vg_fatal_error( "Path too long\n" );
784 FILE *fp = fopen( path_buf, "wb" );
785 if( !fp ) vg_fatal_error( "Cannot open '%s' for writing\n", path_buf );
786
787 fwrite( pcbuf, sizeof(struct pointcloud_buffer) +
788 sizeof(struct pointcloud_vert)*pcbuf->count, 1, fp );
789 fclose( fp );
790 vg_info( "written %s\n", path_buf );
791 }
792
793 /*
794 * Create the strips of colour that run through the world along course paths
795 */
796 static void world_gen_routes_generate( u32 instance_id ){
797 world_instance *world = &world_static.instances[ instance_id ];
798 vg_info( "Generating route meshes\n" );
799 vg_async_stall();
800
801 vg_rand_seed( 2000 );
802 vg_async_item *call_scene = scene_alloc_async( &world->scene_lines,
803 &world->mesh_route_lines,
804 200000, 300000 );
805
806 vg_async_item *call_pointcloud = NULL;
807 pointcloud_buffer *pcbuf = NULL;
808
809 if( instance_id <= 1 /*world_loader.generate_point_cloud*/ ){
810 call_pointcloud = vg_async_alloc(
811 sizeof(pointcloud_buffer) +
812 sizeof(pointcloud_vert)*POINTCLOUD_POINTS );
813 pcbuf = call_pointcloud->payload;
814 pcbuf->count = 0;
815 pcbuf->max = POINTCLOUD_POINTS;
816 pcbuf->op = k_pointcloud_op_clear;
817
818 v3f ext, mid, v0;
819 v3_sub( world->scene_geo.bbx[1], world->scene_geo.bbx[0], ext );
820 f32 maxe = v3_maxf( ext );
821 v3_fill( v0, maxe * 0.5f );
822 v3_muladds( world->scene_geo.bbx[0], ext, 0.5f, mid );
823 v3_add( mid, v0, pcbuf->boundary[1] );
824 v3_sub( mid, v0, pcbuf->boundary[0] );
825 }
826
827 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
828 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
829 gate->ref_count = 0;
830 gate->route_count = 0;
831 }
832
833 for( u32 i=0; i<mdl_arrcount(&world->ent_route_node); i++ ){
834 ent_route_node *rn = mdl_arritm( &world->ent_route_node, i );
835 rn->ref_count = 0;
836 rn->ref_total = 0;
837 }
838
839 for( u32 k=0; k<mdl_arrcount(&world->ent_route); k++ ){
840 ent_route *route = mdl_arritm( &world->ent_route, k );
841
842 for( int i=0; i<route->checkpoints_count; i++ ){
843 int i0 = route->checkpoints_start+i,
844 i1 = route->checkpoints_start+((i+1)%route->checkpoints_count);
845
846 ent_checkpoint *c0 = mdl_arritm(&world->ent_checkpoint, i0),
847 *c1 = mdl_arritm(&world->ent_checkpoint, i1);
848
849 ent_gate *start_gate = mdl_arritm( &world->ent_gate, c0->gate_index );
850 start_gate = mdl_arritm( &world->ent_gate, start_gate->target );
851 start_gate->route_count ++;
852
853 if( !c0->path_count )
854 continue;
855
856 for( int j=0; j<c0->path_count; j ++ ){
857 ent_path_index *index = mdl_arritm( &world->ent_path_index,
858 c0->path_start+j );
859 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
860 index->index );
861 rn->ref_total ++;
862 }
863 }
864 }
865
866 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
867 world_routes_gen_meshes( world, i, &world->scene_lines, pcbuf );
868 }
869
870 if( instance_id <= 1 /*world_loader.generate_point_cloud*/ ){
871 f64 area = 0.0;
872
873 #if VG_RELEASE
874 area = world_routes_scatter_surface_points( world, pcbuf, 16.0f );
875 world_routes_surface_grid( world, pcbuf );
876
877 for( u32 i=0; i<mdl_arrcount( &world->ent_gate ); i++ ){
878 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
879
880 world_routes_pointcloud_tower( world, pcbuf, gate->co[0],
881 2.0f, 50.0f, 128,
882 (v4f){0.2f,0.2f,0.2f,1.0f} );
883 }
884 #endif
885
886 vg_info( "Distributed %u points over %fkm^2!\n",
887 pcbuf->count, area/1e6f );
888
889 world_write_preview( world_static.instance_addons[ instance_id ], pcbuf );
890 vg_async_dispatch( call_pointcloud, async_pointcloud_sub );
891 }
892
893 vg_async_dispatch( call_scene, async_scene_upload );
894 world_routes_clear( world );
895 }
896
897 /* load all routes from model header */
898 static void world_gen_routes_ent_init( world_instance *world ){
899 vg_info( "Initializing routes\n" );
900
901 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
902 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
903 for( u32 j=0; j<4; j++ ){
904 gate->routes[j] = 0xffff;
905 }
906 }
907
908 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
909 ent_route *route = mdl_arritm(&world->ent_route,i);
910 mdl_transform_m4x3( &route->anon.transform, route->board_transform );
911
912 route->anon.official_track_id = 0xffffffff;
913 for( u32 j=0; j<vg_list_size(track_infos); j ++ ){
914 if( !strcmp(track_infos[j].name,
915 mdl_pstr(&world->meta,route->pstr_name))){
916 route->anon.official_track_id = j;
917 }
918 }
919
920 for( u32 j=0; j<route->checkpoints_count; j++ ){
921 u32 id = route->checkpoints_start + j;
922 ent_checkpoint *cp = mdl_arritm(&world->ent_checkpoint,id);
923
924 ent_gate *gate = mdl_arritm( &world->ent_gate, cp->gate_index );
925
926 for( u32 k=0; k<4; k++ ){
927 if( gate->routes[k] == 0xffff ){
928 gate->routes[k] = i;
929 break;
930 }
931 }
932
933 if( (gate->flags & k_ent_gate_linked) &
934 !(gate->flags & k_ent_gate_nonlocal) ){
935 gate = mdl_arritm(&world->ent_gate, gate->target );
936
937 for( u32 k=0; k<4; k++ ){
938 if( gate->routes[k] == i ){
939 vg_error( "already assigned route to gate\n" );
940 break;
941 }
942 if( gate->routes[k] == 0xffff ){
943 gate->routes[k] = i;
944 break;
945 }
946 }
947 }
948 }
949 }
950
951 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
952 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
953 }
954
955 world_routes_clear( world );
956 }
957
958 static void world_routes_recv_scoreboard( world_instance *world,
959 vg_msg *body, u32 route_id,
960 enum request_status status ){
961 if( route_id >= mdl_arrcount( &world->ent_route ) ){
962 vg_error( "Scoreboard route_id out of range (%u)\n", route_id );
963 return;
964 }
965
966 struct leaderboard_cache *board = &world->leaderboard_cache[ route_id ];
967 board->status = status;
968
969 if( body == NULL )
970 board->data_len = 0;
971
972 if( body->max > NETWORK_LEADERBOARD_MAX_SIZE ){
973 vg_error( "Scoreboard leaderboard too big (%u>%u)\n", body->max,
974 NETWORK_LEADERBOARD_MAX_SIZE );
975 return;
976 }
977
978 memcpy( board->data, body->buf, body->max );
979 board->data_len = body->max;
980 }
981
982 /*
983 * -----------------------------------------------------------------------------
984 * Events
985 * -----------------------------------------------------------------------------
986 */
987
988 static void world_routes_init(void){
989 world_static.current_run_version = 200;
990 world_static.time = 300.0;
991 world_static.last_use = 0.0;
992
993 shader_scene_route_register();
994 shader_routeui_register();
995 }
996
997 static void world_routes_update( world_instance *world ){
998 world_static.time += vg.time_delta;
999
1000 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
1001 ent_route *route = mdl_arritm( &world->ent_route, i );
1002
1003 int target = (route->active_checkpoint == 0xffff? 0: 1) ||
1004 skaterift.activity == k_skaterift_respawning;
1005 route->factive = vg_lerpf( route->factive, target,
1006 0.6f*vg.time_frame_delta );
1007 }
1008
1009 for( u32 i=0; i<world_render.text_particle_count; i++ ){
1010 struct text_particle *particle = &world_render.text_particles[i];
1011 rb_object_debug( &particle->obj, VG__RED );
1012 }
1013 }
1014
1015 static void world_routes_fixedupdate( world_instance *world ){
1016 rb_solver_reset();
1017
1018 for( u32 i=0; i<world_render.text_particle_count; i++ ){
1019 struct text_particle *particle = &world_render.text_particles[i];
1020
1021 if( rb_global_has_space() ){
1022 rb_ct *buf = rb_global_buffer();
1023
1024 int l = rb_sphere__scene( particle->obj.rb.to_world,
1025 &particle->obj.inf.sphere,
1026 NULL, &world->rb_geo.inf.scene, buf,
1027 k_material_flag_ghosts );
1028
1029 for( int j=0; j<l; j++ ){
1030 buf[j].rba = &particle->obj.rb;
1031 buf[j].rbb = &world->rb_geo.rb;
1032 }
1033
1034 rb_contact_count += l;
1035 }
1036 }
1037
1038 rb_presolve_contacts( rb_contact_buffer, rb_contact_count );
1039
1040 for( int i=0; i<rb_contact_count; i++ ){
1041 rb_contact_restitution( rb_contact_buffer+i, vg_randf64() );
1042 }
1043
1044 for( int i=0; i<6; i++ ){
1045 rb_solve_contacts( rb_contact_buffer, rb_contact_count );
1046 }
1047
1048 for( u32 i=0; i<world_render.text_particle_count; i++ ){
1049 struct text_particle *particle = &world_render.text_particles[i];
1050 rb_iter( &particle->obj.rb );
1051 }
1052
1053 for( u32 i=0; i<world_render.text_particle_count; i++ ){
1054 struct text_particle *particle = &world_render.text_particles[i];
1055 rb_update_transform( &particle->obj.rb );
1056 }
1057 }
1058
1059 static void bind_terrain_noise(void);
1060 static void world_bind_light_array( world_instance *world,
1061 GLuint shader, GLuint location,
1062 int slot );
1063 static void world_bind_light_index( world_instance *world,
1064 GLuint shader, GLuint location,
1065 int slot );
1066
1067 static void world_routes_update_timer_texts( world_instance *world ){
1068 world_render.timer_text_count = 0;
1069
1070 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
1071 ent_route *route = mdl_arritm( &world->ent_route, i );
1072
1073 if( route->active_checkpoint != 0xffff ){
1074 u32 next = route->active_checkpoint+1;
1075 next = next % route->checkpoints_count;
1076 next += route->checkpoints_start;
1077
1078 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint, next );
1079 ent_gate *gate = mdl_arritm( &world->ent_gate, cp->gate_index );
1080 ent_gate *dest = mdl_arritm( &world->ent_gate, gate->target );
1081
1082 u32 j=0;
1083 for( ; j<4; j++ ){
1084 if( dest->routes[j] == i ){
1085 break;
1086 }
1087 }
1088
1089 float h0 = 0.8f,
1090 h1 = 1.2f,
1091 depth = 0.4f,
1092 size = 0.4f;
1093
1094 struct timer_text *text =
1095 &world_render.timer_texts[ world_render.timer_text_count ++ ];
1096
1097 text->gate = gate;
1098 text->route = route;
1099
1100 if( route->valid_checkpoints >= route->checkpoints_count ){
1101 double lap_time = world_static.time - route->timing_base,
1102 time_centiseconds = lap_time * 100.0;
1103
1104 if( time_centiseconds > (float)0xfffe ) time_centiseconds = 0.0;
1105
1106 u16 centiseconds = time_centiseconds,
1107 seconds = centiseconds / 100,
1108 minutes = seconds / 60;
1109
1110 centiseconds %= 100;
1111 seconds %= 60;
1112 minutes %= 60;
1113
1114 if( minutes > 9 ) minutes = 9;
1115
1116 int j=0;
1117 if( minutes ){
1118 highscore_intr( text->text, minutes, 1, ' ' ); j++;
1119 text->text[j++] = ':';
1120 }
1121
1122 if( seconds >= 10 || minutes ){
1123 highscore_intr( text->text+j, seconds, 2, '0' ); j+=2;
1124 }
1125 else{
1126 highscore_intr( text->text+j, seconds, 1, '0' ); j++;
1127 }
1128
1129 text->text[j++] = '.';
1130 highscore_intr( text->text+j, centiseconds, 1, '0' ); j++;
1131 text->text[j] = '\0';
1132 }
1133 else{
1134 highscore_intr( text->text, route->valid_checkpoints, 1, ' ' );
1135 text->text[1] = '/';
1136 highscore_intr( text->text+2, route->checkpoints_count+1, 1, ' ' );
1137 text->text[3] = '\0';
1138 }
1139
1140 gui_font3d.font = &gui.font;
1141 float align_r = font3d_string_width( 0, text->text );
1142 align_r *= size;
1143
1144 v3f positions[] = {
1145 { -0.92f, h0, depth },
1146 { 0.92f - align_r, h0, depth },
1147 { -0.92f, h1, depth },
1148 { 0.92f - align_r, h1, depth },
1149 };
1150
1151 if( dest->route_count == 1 ){
1152 positions[0][0] = -align_r*0.5f;
1153 positions[0][1] = h1;
1154 }
1155
1156 m4x3f mmdl;
1157 ent_gate_get_mdl_mtx( gate, mmdl );
1158
1159 m3x3_copy( mmdl, text->transform );
1160 float ratio = v3_length(text->transform[0]) /
1161 v3_length(text->transform[1]);
1162
1163 m3x3_scale( text->transform, (v3f){ size, size*ratio, 0.1f } );
1164 m4x3_mulv( mmdl, positions[j], text->transform[3] );
1165 }
1166 }
1167 }
1168
1169 static void world_routes_fracture( world_instance *world, ent_gate *gate,
1170 v3f imp_co, v3f imp_v )
1171 {
1172 world_render.text_particle_count = 0;
1173
1174 for( u32 i=0; i<world_render.timer_text_count; i++ ){
1175 struct timer_text *text = &world_render.timer_texts[i];
1176
1177 if( text->gate != gate ) continue;
1178
1179 m4x3f transform;
1180 m4x3_mul( gate->transport, text->transform, transform );
1181
1182 v3f co, s;
1183 v4f q;
1184 m4x3_decompose( transform, co, q, s );
1185
1186 v3f offset;
1187 v3_zero( offset );
1188
1189 v4f colour;
1190 float brightness = 0.3f + world->ub_lighting.g_day_phase;
1191 v3_muls( text->route->colour, brightness, colour );
1192 colour[3] = 1.0f-text->route->factive;
1193
1194 for( u32 j=0;; j++ ){
1195 char c = text->text[j];
1196 if( !c ) break;
1197
1198 ent_glyph *glyph = font3d_glyph( &gui.font, 0, c );
1199 if( !glyph ) continue;
1200
1201 if( c >= (u32)'0' && c <= (u32)'9' && glyph->indice_count ){
1202 struct text_particle *particle =
1203 &world_render.text_particles[world_render.text_particle_count++];
1204
1205 particle->glyph = glyph;
1206 v4_copy( colour, particle->colour );
1207
1208 v3f origin;
1209 v2_muls( glyph->size, 0.5f, origin );
1210 origin[2] = -0.5f;
1211
1212 v3f world_co;
1213
1214 v3_add( offset, origin, world_co );
1215 m4x3_mulv( transform, world_co, world_co );
1216
1217 float r = vg_maxf( s[0]*glyph->size[0], s[1]*glyph->size[1] )*0.5f;
1218
1219 m3x3_identity( particle->mlocal );
1220 m3x3_scale( particle->mlocal, s );
1221 origin[2] *= s[2];
1222 v3_muls( origin, -1.0f, particle->mlocal[3] );
1223
1224 v3_copy( world_co, particle->obj.rb.co );
1225 v3_muls( imp_v, 1.0f+vg_randf64(), particle->obj.rb.v );
1226 particle->obj.rb.v[1] += 2.0f;
1227
1228 v4_copy( q, particle->obj.rb.q );
1229 particle->obj.rb.w[0] = vg_randf64()*2.0f-1.0f;
1230 particle->obj.rb.w[1] = vg_randf64()*2.0f-1.0f;
1231 particle->obj.rb.w[2] = vg_randf64()*2.0f-1.0f;
1232
1233 particle->obj.type = k_rb_shape_sphere;
1234 particle->obj.inf.sphere.radius = r*0.6f;
1235
1236 rb_init_object( &particle->obj );
1237 }
1238 offset[0] += glyph->size[0];
1239 }
1240 }
1241 }
1242
1243 static void render_gate_markers( int run_id, ent_gate *gate ){
1244 for( u32 j=0; j<4; j++ ){
1245 if( gate->routes[j] == run_id ){
1246 m4x3f mmdl;
1247 ent_gate_get_mdl_mtx( gate, mmdl );
1248 shader_model_gate_uMdl( mmdl );
1249 mdl_draw_submesh( &world_gates.sm_marker[j] );
1250 break;
1251 }
1252 }
1253 }
1254
1255 static void render_world_routes( world_instance *world, camera *cam,
1256 int layer_depth ){
1257 m4x3f identity_matrix;
1258 m4x3_identity( identity_matrix );
1259
1260 shader_scene_route_use();
1261 shader_scene_route_uTexGarbage(0);
1262 world_link_lighting_ub( world, _shader_scene_route.id );
1263 world_bind_position_texture( world, _shader_scene_route.id,
1264 _uniform_scene_route_g_world_depth, 2 );
1265 world_bind_light_array( world, _shader_scene_route.id,
1266 _uniform_scene_route_uLightsArray, 3 );
1267 world_bind_light_index( world, _shader_scene_route.id,
1268 _uniform_scene_route_uLightsIndex, 4 );
1269 bind_terrain_noise();
1270
1271 shader_scene_route_uPv( cam->mtx.pv );
1272 shader_scene_route_uPvmPrev( cam->mtx_prev.pv );
1273 shader_scene_route_uMdl( identity_matrix );
1274 shader_scene_route_uCamera( cam->transform[3] );
1275
1276 mesh_bind( &world->mesh_route_lines );
1277
1278 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
1279 ent_route *route = mdl_arritm( &world->ent_route, i );
1280
1281 v4f colour;
1282 v3_lerp( (v3f){0.7f,0.7f,0.7f}, route->colour, route->factive, colour );
1283 colour[3] = route->factive*0.2f;
1284
1285 shader_scene_route_uColour( colour );
1286 mdl_draw_submesh( &route->sm );
1287 }
1288
1289 /* timers
1290 * ---------------------------------------------------- */
1291 if( layer_depth == 0 ){
1292 font3d_bind( &gui.font, k_font_shader_default, 0, world, cam );
1293
1294 for( u32 i=0; i<world_render.timer_text_count; i++ ){
1295 struct timer_text *text = &world_render.timer_texts[i];
1296
1297 v4f colour;
1298 float brightness = 0.3f + world->ub_lighting.g_day_phase;
1299 v3_muls( text->route->colour, brightness, colour );
1300 colour[3] = 1.0f-text->route->factive;
1301
1302 shader_model_font_uColour( colour );
1303 font3d_simple_draw( 0, text->text, cam, text->transform );
1304 }
1305
1306 shader_model_font_uOffset( (v4f){0.0f,0.0f,0.0f,1.0f} );
1307
1308 for( u32 i=0; i<world_render.text_particle_count; i++ ){
1309 struct text_particle *particle = &world_render.text_particles[i];
1310
1311 m4x4f prev_mtx;
1312
1313 m4x3_expand( particle->mdl, prev_mtx );
1314 m4x4_mul( cam->mtx_prev.pv, prev_mtx, prev_mtx );
1315
1316 shader_model_font_uPvmPrev( prev_mtx );
1317
1318 v4f q;
1319 m4x3f model;
1320 rb_extrapolate( &particle->obj.rb, model[3], q );
1321 q_m3x3( q, model );
1322
1323 m4x3_mul( model, particle->mlocal, particle->mdl );
1324 shader_model_font_uMdl( particle->mdl );
1325 shader_model_font_uColour( particle->colour );
1326
1327 mesh_drawn( particle->glyph->indice_start,
1328 particle->glyph->indice_count );
1329 }
1330 }
1331
1332 /* gate markers
1333 * ---------------------------------------------------- */
1334
1335 shader_model_gate_use();
1336 shader_model_gate_uPv( cam->mtx.pv );
1337 shader_model_gate_uCam( cam->pos );
1338 shader_model_gate_uTime( vg.time*0.25f );
1339 shader_model_gate_uInvRes( (v2f){
1340 1.0f / (float)vg.window_x,
1341 1.0f / (float)vg.window_y });
1342
1343 mesh_bind( &world_gates.mesh );
1344
1345 /* skip writing into the motion vectors for this */
1346 glDrawBuffers( 1, (GLenum[]){ GL_COLOR_ATTACHMENT0 } );
1347 glDisable( GL_CULL_FACE );
1348
1349 if( skaterift.activity == k_skaterift_respawning ){
1350 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
1351 ent_route *route = mdl_arritm( &world->ent_route, i );
1352
1353 v4f colour;
1354 v3_muls( route->colour, 1.6666f, colour );
1355 colour[3] = 0.0f;
1356
1357 shader_model_gate_uColour( colour );
1358
1359 for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ ){
1360 ent_gate *gate = mdl_arritm( &world->ent_gate, j );
1361 if( !(gate->flags & k_ent_gate_nonlocal) )
1362 render_gate_markers( i, gate );
1363 }
1364 }
1365 }
1366 else{
1367 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
1368 ent_route *route = mdl_arritm( &world->ent_route, i );
1369
1370 if( route->active_checkpoint != 0xffff ){
1371 v4f colour;
1372 float brightness = 0.3f + world->ub_lighting.g_day_phase;
1373 v3_muls( route->colour, brightness, colour );
1374 colour[3] = 1.0f-route->factive;
1375
1376 shader_model_gate_uColour( colour );
1377
1378 u32 next = route->active_checkpoint+1+layer_depth;
1379 next = next % route->checkpoints_count;
1380 next += route->checkpoints_start;
1381
1382 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint, next );
1383 ent_gate *gate = mdl_arritm( &world->ent_gate, cp->gate_index );
1384 render_gate_markers( i, gate );
1385 }
1386 }
1387 }
1388 glEnable( GL_CULL_FACE );
1389 glDrawBuffers( 2, (GLenum[]){ GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 } );
1390 }
1391
1392 #endif /* ROUTES_C */