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