grid based
[carveJwlIkooP6JGAAIwe30JlM.git] / world_routes.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef ROUTES_H
6 #define ROUTES_H
7
8 #include <time.h>
9 #include "world.h"
10 #include "world_gate.h"
11
12 #if 0
13 #include "shaders/vblend.h"
14 #endif
15
16 #include "shaders/scene_route.h"
17 #include "shaders/routeui.h"
18
19
20 enum route_special_type
21 {
22 k_route_special_type_none = 0,
23 k_route_special_type_gate = 1,
24 k_route_special_type_collector = 2
25 };
26
27 VG_STATIC void debug_sbpath( struct route_node *rna, struct route_node *rnb,
28 u32 colour, float xoffset )
29 {
30 v3f p0, h0, p1, h1, l, p;
31
32 v3_copy( rna->co, p0 );
33 v3_muladds( rna->co, rna->h, 1.0f, h0 );
34 v3_copy( rnb->co, p1 );
35 v3_muladds( rnb->co, rnb->h, -1.0f, h1 );
36
37 v3_muladds( p0, rna->right, xoffset, p0 );
38 v3_muladds( h0, rna->right, xoffset, h0 );
39 v3_muladds( p1, rnb->right, xoffset, p1 );
40 v3_muladds( h1, rnb->right, xoffset, h1 );
41
42 v3_copy( p0, l );
43
44 for( int i=0; i<5; i++ )
45 {
46 float t = (float)(i+1)/5.0f;
47 eval_bezier_time( p0, p1, h0, h1, t, p );
48 vg_line( p, l, colour );
49 v3_copy( p, l );
50 }
51 }
52
53 /*
54 * Get a list of node ids in stack, and return how many there is
55 */
56 VG_STATIC u32 world_routes_get_path( world_instance *world,
57 u32 starter, u32 stack[64] )
58 {
59 u32 stack_i[64];
60
61 stack[0] = starter;
62 stack_i[0] = 0;
63
64 u32 si = 1;
65 int loop_complete = 0;
66
67 while( si )
68 {
69 if( stack_i[si-1] == 2 )
70 {
71 si --;
72 continue;
73 }
74
75 struct route_node *rn = &world->nodes[stack[si-1]];
76 u32 nextid = rn->next[stack_i[si-1]];
77 stack_i[si-1] ++;
78
79 if( nextid != 0xffffffff )
80 {
81 if( nextid == stack[0] )
82 {
83 loop_complete = 1;
84 break;
85 }
86
87 int valid = 1;
88 for( int sj=0; sj<si; sj++ )
89 {
90 if( stack[sj] == nextid )
91 {
92 valid = 0;
93 break;
94 }
95 }
96
97 if( valid )
98 {
99 stack_i[si] = 0;
100 stack[si] = nextid;
101 si ++;
102 continue;
103 }
104 }
105 }
106
107 if( loop_complete )
108 return si;
109
110 return 0;
111 }
112
113 /*
114 * Free a segment from the UI bar to be reused later
115 */
116 VG_STATIC void world_routes_ui_popfirst( struct route_ui_bar *pui )
117 {
118 if( pui->segment_count )
119 {
120 pui->segment_start ++;
121
122 if( pui->segment_start == 32 )
123 pui->segment_start = 0;
124
125 pui->segment_count --;
126 }
127 }
128
129 /*
130 * Reset ui bar completely
131 */
132 VG_STATIC void world_routes_ui_clear( struct route_ui_bar *pui )
133 {
134 pui->segment_start = (pui->segment_start + pui->segment_count) %
135 k_max_ui_segments;
136 pui->segment_count = 0;
137 }
138
139 /*
140 * Break a index range into two pieces over the edge of the maximum it can
141 * store. s1 is 0 always, so its a ring buffer.
142 */
143 VG_STATIC void world_routes_ui_split_indices( u32 s0, u32 count,
144 u32 *c0, u32 *c1 )
145 {
146 *c0 = (VG_MIN( s0+count, k_route_ui_max_indices )) - s0;
147 *c1 = count-(*c0);
148 }
149
150 /*
151 * Place a set of indices into gpu array automatically splits
152 * across bounds
153 */
154 VG_STATIC void world_routes_ui_set_indices( struct route_ui_bar *pui,
155 u16 *indices, u32 count )
156 {
157 u32 c0, c1;
158 world_routes_ui_split_indices( pui->indices_head, count, &c0, &c1 );
159
160 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, pui->ebo );
161
162 if( c0 )
163 {
164 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, pui->indices_head*sizeof(u16),
165 c0*sizeof(u16), indices );
166 }
167
168 if( c1 )
169 {
170 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, c1*sizeof(u16), indices+c0 );
171 pui->indices_head = c1;
172 }
173 else
174 pui->indices_head += c0;
175 }
176
177 /*
178 * Place a set of vertices into gpu array
179 */
180 VG_STATIC u32 world_routes_ui_set_verts( struct route_ui_bar *pui,
181 v2f *verts, u32 count )
182 {
183 if( pui->vertex_head + count >= k_route_ui_max_verts )
184 pui->vertex_head = 0;
185
186 u32 vert_start = pui->vertex_head;
187 pui->vertex_head += count;
188
189 glBindBuffer( GL_ARRAY_BUFFER, pui->vbo );
190 glBufferSubData( GL_ARRAY_BUFFER, (GLintptr)(vert_start*sizeof(v2f)),
191 sizeof(v2f)*count, verts );
192
193 return vert_start;
194 }
195
196 /*
197 * Update the last (count) vertices positions, does not add any.
198 * Data must already be written to, and not cross either array boundaries.
199 */
200 VG_STATIC u32 world_routes_ui_update_verts( struct route_ui_bar *pui,
201 v2f *verts, u32 count )
202 {
203 u32 vert_start = pui->vertex_head-count;
204
205 glBindBuffer( GL_ARRAY_BUFFER, pui->vbo );
206 glBufferSubData( GL_ARRAY_BUFFER, (GLintptr)(vert_start*sizeof(v2f)),
207 sizeof(v2f)*count, verts );
208
209 return vert_start;
210 }
211
212 /*
213 * Current/active segment of this UI bar
214 */
215 VG_STATIC struct route_ui_segment *world_routes_ui_curseg(
216 struct route_ui_bar *pui )
217 {
218 u32 index = (pui->segment_start+pui->segment_count-1)%k_max_ui_segments;
219 return &pui->segments[ index ];
220 }
221
222 /*
223 * Start a new segment in the UI bar, will create a split on the last one if
224 * there is one active currently. (api)
225 */
226 VG_STATIC void world_routes_ui_newseg( u32 route )
227 {
228 struct route_ui_bar *pui = &world_global.ui_bars[route];
229
230 glBindVertexArray( pui->vao );
231 if( pui->segment_count )
232 {
233 float const k_gap_width = 1.0f;
234
235 struct route_ui_segment *cseg = world_routes_ui_curseg( pui );
236
237 v2f verts[2];
238 verts[0][0] = cseg->length-k_gap_width;
239 verts[0][1] = 0.5f;
240 verts[1][0] = cseg->length-k_gap_width;
241 verts[1][1] = -0.5f;
242
243 world_routes_ui_update_verts( pui, verts, 2 );
244 }
245
246 pui->segment_count ++;
247 struct route_ui_segment *segment = world_routes_ui_curseg( pui );
248
249 v2f verts[4];
250 verts[0][0] = 0.0f;
251 verts[0][1] = 0.5f;
252 verts[1][0] = 0.0f;
253 verts[1][1] = -0.5f;
254 verts[2][0] = 0.0f;
255 verts[2][1] = 0.5f;
256 verts[3][0] = 0.0f;
257 verts[3][1] = -0.5f;
258
259 u32 vert_start = world_routes_ui_set_verts( pui, verts, 4 );
260
261 u16 indices[6];
262 indices[0] = vert_start + 0;
263 indices[1] = vert_start + 1;
264 indices[2] = vert_start + 3;
265 indices[3] = vert_start + 0;
266 indices[4] = vert_start + 3;
267 indices[5] = vert_start + 2;
268
269 segment->vertex_start = vert_start;
270 segment->vertex_count = 4;
271 segment->index_start = pui->indices_head;
272 segment->index_count = 6;
273 segment->notches = 0;
274
275 world_routes_ui_set_indices( pui, indices, 6 );
276 }
277
278 /*
279 * Extend the end of the bar
280 */
281 VG_STATIC void world_routes_ui_updatetime( u32 route, float time )
282 {
283 struct route_ui_bar *pui = &world_global.ui_bars[route];
284
285 v2f verts[2];
286 verts[0][0] = time;
287 verts[0][1] = 0.5f;
288 verts[1][0] = time;
289 verts[1][1] = -0.5f;
290
291 u32 vert_start = pui->vertex_head-2;
292
293 glBindVertexArray( pui->vao );
294 world_routes_ui_update_verts( pui, verts, 2 );
295
296 struct route_ui_segment *cseg = world_routes_ui_curseg( pui );
297 cseg->length = time;
298 }
299
300 VG_STATIC void world_routes_ui_draw_segment( struct route_ui_segment *segment )
301 {
302 u32 c0, c1;
303 world_routes_ui_split_indices( segment->index_start,
304 segment->index_count, &c0, &c1 );
305 if( c0 )
306 glDrawElements( GL_TRIANGLES, c0, GL_UNSIGNED_SHORT,
307 (void *)(segment->index_start*sizeof(u16)));
308 if( c1 )
309 glDrawElements( GL_TRIANGLES, c1, GL_UNSIGNED_SHORT, (void *)(0) );
310 }
311
312 /*
313 * Draws full bar at Y offset(offset).
314 */
315 VG_STATIC void world_routes_ui_draw( world_instance *world,
316 u32 route, v4f colour, float offset )
317 {
318 float const k_bar_height = 0.05f,
319 k_bar_scale_x = 0.005f;
320
321 /* FIXME(10) ID mishmatch */
322 struct route *pr = &world->routes[route];
323 struct route_ui_bar *pui = &world_global.ui_bars[route];
324
325 float cx = pui->xpos;
326
327 shader_routeui_use();
328 glBindVertexArray( pui->vao );
329
330 float fade_amt = world_global.time - pui->fade_timer_start;
331 fade_amt = vg_clampf( fade_amt / 1.0f, 0.0f, 1.0f );
332
333 float fade_block_size = 0.0f,
334 main_block_size = 0.0f;
335
336 for( u32 i=0; i<pui->fade_count; i++ )
337 {
338 u32 j = (pui->fade_start + i) % k_max_ui_segments;
339 struct route_ui_segment *segment = &pui->segments[j];
340
341 fade_block_size += segment->length;
342 }
343
344 cx -= fade_block_size * fade_amt;
345
346 v4f fade_colour;
347 v4_copy( colour, fade_colour );
348 fade_colour[3] *= 1.0f-fade_amt;
349
350 /* 1 minute timer */
351 float timer_delta = (world_global.time - world_global.last_use) * (1.0/45.0),
352 timer_scale = 1.0f - vg_minf( timer_delta, 1.0f );
353
354 /*
355 * Draw fadeout bar
356 */
357
358 float height = pr->factive*k_bar_height * timer_scale,
359 base = -1.0f + (offset+0.5f)*k_bar_height * timer_scale;
360
361 shader_routeui_uColour( fade_colour );
362 for( u32 i=0; i<pui->fade_count; i++ )
363 {
364 u32 j = (pui->fade_start + i) % k_max_ui_segments;
365 struct route_ui_segment *segment = &pui->segments[j];
366
367 shader_routeui_uOffset( (v4f){ cx*k_bar_scale_x, base,
368 k_bar_scale_x, height } );
369
370 world_routes_ui_draw_segment( segment );
371 cx += segment->length;
372 }
373
374 /*
375 * Draw main bar
376 */
377 shader_routeui_uColour( colour );
378 for( u32 i=0; i<pui->segment_count; i++ )
379 {
380 u32 j = (pui->segment_start + i) % k_max_ui_segments;
381 struct route_ui_segment *segment = &pui->segments[j];
382
383 shader_routeui_uOffset( (v4f){ cx*k_bar_scale_x, base,
384 k_bar_scale_x, height } );
385
386 world_routes_ui_draw_segment( segment );
387 cx += segment->length;
388
389 main_block_size += segment->length;
390 }
391
392 pui->xpos = vg_lerpf( pui->xpos, -main_block_size * 0.5f, 0.03f );
393 }
394
395 VG_STATIC void world_routes_local_set_record( world_instance *world,
396 u32 route, double lap_time )
397 {
398 vg_success( " NEW LAP TIME: %f\n", lap_time );
399
400 /* FIXME(10): ID mishmatch */
401 struct route *pr = &world->routes[route];
402
403 if( pr->track_id != 0xffffffff )
404 {
405 double time_centiseconds = lap_time * 100.0;
406 if( time_centiseconds > (float)0xfffe )
407 return;
408
409 highscore_record temp;
410 temp.trackid = pr->track_id;
411 temp.datetime = time(NULL);
412 temp.playerid = 0;
413 temp.points = 0;
414 temp.time = time_centiseconds;
415
416 highscores_push_record( &temp );
417
418 struct track_info *pti = &track_infos[ pr->track_id ];
419 pti->push = 1;
420
421 if( pti->achievement_id )
422 {
423 steam_set_achievement( pti->achievement_id );
424 steam_store_achievements();
425 }
426 }
427 else
428 {
429 vg_warn( "There is no associated track for this record...\n" );
430 }
431 }
432
433 /*
434 * Will scan the whole run for two things;
435 * 1: we set a new record for the total, complete loop around the course
436 * 2: the time of each segment will be recorded into the data buffer
437 * (not implemented: TODO)
438 */
439 VG_STATIC void world_routes_verify_run( world_instance *world, u32 route )
440 {
441 /* FIXME(10): ID mishmatch */
442 struct route *pr = &world->routes[route];
443 struct route_ui_bar *pui = &world_global.ui_bars[route];
444
445 u32 stack[64];
446 u32 si = world_routes_get_path( world, world->routes[route].start, stack );
447
448 /*
449 * we only care about gates that ref gates, so shuffle down the array
450 */
451 struct route_timing *timings[64];
452 u32 sj = 0, maxv = 0, begin = 0;
453 for( u32 i=0; i<si; i++ )
454 {
455 struct route_node *inode = &world->nodes[stack[i]];
456
457 if( inode->special_type == k_route_special_type_collector )
458 {
459 timings[sj ++] = &world->collectors[ inode->special_id ].timing;
460 }
461 else if( inode->special_type == k_route_special_type_gate )
462 {
463 timings[sj ++] = &world->gates[inode->special_id].timing;
464 }
465 }
466
467 for( u32 i=0; i<sj; i++ )
468 {
469 if( timings[i]->version > maxv )
470 {
471 maxv = timings[i]->version;
472 begin = i;
473 }
474 }
475
476 vg_info( "== begin verification (%u) ==\n", route );
477 vg_info( " current version: %u\n", world_global.current_run_version );
478
479 int verified = 0;
480 if( timings[begin]->version == world_global.current_run_version )
481 verified = 1;
482
483 int valid_segment_count = 0;
484
485 double lap_time = 0.0;
486
487 for( u32 i=0; i<sj; i++ )
488 {
489 u32 j = (sj+begin-i-1) % sj,
490 j1 = (j+1) % sj;
491
492 double diff = 0.0;
493
494 if( i<sj-1 )
495 {
496 /* j1v should equal jv+1 */
497 if( timings[j1]->version == timings[j]->version+1 )
498 {
499 diff = timings[j1]->time - timings[j]->time;
500 lap_time += diff;
501
502 if( verified && diff > 0.0 ) valid_segment_count ++;
503 }
504 else
505 verified = 0;
506 }
507
508 if( verified )
509 vg_success( " [ %u %f ] %f\n", timings[j1]->time,
510 timings[j1]->version, diff );
511 else
512 vg_warn( " [ %u %f ]\n", timings[j1]->time, timings[j1]->version );
513 }
514
515 pui->fade_start = pui->segment_start;
516 pui->fade_count = 0;
517 pui->fade_timer_start = world_global.time;
518
519 int orig_seg_count = pui->segment_count;
520
521 world_routes_ui_newseg( route );
522
523 if( verified )
524 {
525 world_routes_local_set_record( world, route, lap_time );
526 world_routes_ui_popfirst( pui );
527 pui->fade_count ++;
528 }
529 else
530 vg_info( " ctime: %f\n", lap_time );
531
532 /* remove any excess we had from previous runs */
533 int to_remove = orig_seg_count-valid_segment_count;
534 for( int i=0; i<to_remove; i++ )
535 {
536 world_routes_ui_popfirst( pui );
537 pui->fade_count ++;
538 }
539
540 world->routes[route].latest_pass = world_global.time;
541 }
542
543 VG_STATIC void world_routes_clear( world_instance *world )
544 {
545 for( u32 i=0; i<world->route_count; i++ )
546 {
547 struct route *route = &world->routes[i];
548 route->active = 0;
549 }
550 world_global.current_run_version += 4;
551 world_global.last_use = 0.0;
552 }
553
554 /*
555 * When going through a gate this is called for bookkeeping purposes
556 */
557 VG_STATIC void world_routes_activate_gate( world_instance *world, u32 id )
558 {
559 struct route_gate *rg = &world->gates[id];
560 struct route_node *pnode = &world->nodes[rg->node_id],
561 *pdest = &world->nodes[pnode->next[0]];
562
563 world_global.last_use = world_global.time;
564
565 struct route_collector *rc = &world->collectors[ pdest->special_id ];
566
567 world_global.active_gate = id;
568 rg->timing.version = world_global.current_run_version;
569 rg->timing.time = world_global.time;
570
571 for( u32 i=0; i<world->route_count; i++ )
572 {
573 struct route *route = &world->routes[i];
574
575 int was_active = route->active;
576
577 route->active = 0;
578 for( u32 j=0; j<pdest->ref_count; j++ )
579 {
580 if( pdest->route_ids[j] == i )
581 {
582 world_routes_verify_run( world, i );
583 route->active = 1;
584 break;
585 }
586 }
587
588 if( was_active && !route->active )
589 {
590 struct route_ui_bar *pui = &world_global.ui_bars[i];
591 pui->fade_start = pui->segment_start;
592 pui->fade_count = pui->segment_count;
593 pui->fade_timer_start = world_global.time;
594
595 world_routes_ui_clear( pui );
596 vg_success( "CLEARING -> %u %u \n", pui->fade_start,
597 pui->fade_count );
598 }
599 }
600
601 world_global.current_run_version ++;
602
603 rc->timing.version = world_global.current_run_version;
604 rc->timing.time = world_global.time;
605 world_global.current_run_version ++;
606 }
607
608 /*
609 * Notify the UI system that we've reset the player
610 */
611 VG_STATIC void world_routes_notify_reset(void)
612 {
613 world_global.rewind_from = world_global.time;
614 world_global.rewind_to = world_global.last_use;
615 }
616
617 /* Rewind between the saved points in time */
618 VG_STATIC void world_routes_rollback_time( double t )
619 {
620 world_global.time = vg_lerp( world_global.rewind_to,
621 world_global.rewind_from, t );
622 }
623
624 /* draw lines along the paths */
625 VG_STATIC void world_routes_debug( world_instance *world )
626 {
627 for( int i=0; i<world->node_count; i++ )
628 {
629 struct route_node *rn = &world->nodes[i];
630 vg_line_pt3( rn->co, 1.0f, rn->special_type? 0xffffff00: 0xff00b2ff );
631 }
632
633 for( int i=0; i<world->route_count; i++ )
634 {
635 struct route *route = &world->routes[i];
636
637 u32 stack[64];
638 u32 si = world_routes_get_path( world, route->start, stack );
639
640 u32 colours[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
641 0xff5442f5 };
642
643 u32 cc = colours[i%vg_list_size(colours)];
644
645 for( int sj=0; sj<si; sj++ )
646 {
647 int sk = (sj+1)%si;
648
649 struct route_node *pj = &world->nodes[stack[sj]],
650 *pk = &world->nodes[stack[sk]];
651 debug_sbpath( pj, pk, cc, (float)i );
652 }
653 }
654
655 for( int i=0; i<world->node_count; i++ )
656 {
657 struct route_node *ri = &world->nodes[i],
658 *rj = NULL;
659
660 for( int j=0; j<2; j++ )
661 {
662 if( ri->next[j] != 0xffffffff )
663 {
664 rj = &world->nodes[ri->next[j]];
665 vg_line( ri->co, rj->co, 0x20ffffff );
666 }
667 }
668 }
669 }
670
671 VG_STATIC void world_routes_create_mesh( world_instance *world, u32 route_id )
672 {
673 struct route *route = &world->routes[ route_id ];
674
675 u32 stack[64];
676 u32 si = world_routes_get_path( world, route->start, stack );
677
678 u32 last_valid = 0;
679
680 for( int sj=0; sj<si; sj++ )
681 {
682 int sk=(sj+1)%si;
683
684 struct route_node *rnj = &world->nodes[ stack[sj] ],
685 *rnk = &world->nodes[ stack[sk] ],
686 *rnl;
687
688 if( rnj->special_type && rnk->special_type )
689 {
690 last_valid = 0;
691 continue;
692 }
693
694 float base_x0 = (float)rnj->ref_count*-0.5f + (float)rnj->current_refs,
695 base_x1 = (float)rnk->ref_count*-0.5f + (float)rnk->current_refs;
696
697 if( rnk->special_type )
698 {
699 rnl = &world->nodes[ rnk->next[0] ];
700 base_x1 = (float)rnl->ref_count*-0.5f + (float)rnl->current_refs;
701 }
702
703 if( sk == 0 )
704 {
705 base_x1 -= 1.0f;
706 }
707
708 v3f p0, h0, p1, h1, p, pd;
709
710 v3_copy( rnj->co, p0 );
711 v3_muladds( rnj->co, rnj->h, 1.0f, h0 );
712 v3_copy( rnk->co, p1 );
713 v3_muladds( rnk->co, rnk->h, -1.0f, h1 );
714
715 float t=0.0f;
716 int it = 0;
717
718 for( int it=0; it<256; it ++ )
719 {
720 float const k_sample_dist = 0.02f;
721 eval_bezier_time( p0,p1,h0,h1, t,p );
722 eval_bezier_time( p0,p1,h0,h1, t+k_sample_dist,pd );
723
724 float mod = k_sample_dist / v3_dist( p, pd );
725
726 v3f v0,up, right;
727 v3_muls( rnj->up, 1.0f-t, up );
728 v3_muladds( up, rnk->up, t, up );
729
730 v3_sub( pd,p,v0 );
731 v3_cross( up, v0, right );
732 v3_normalize( right );
733
734 float cur_x = (1.0f-t)*base_x0 + t*base_x1;
735
736 v3f sc, sa, sb, down;
737 v3_muladds( p, right, cur_x, sc );
738 v3_muladds( sc, up, 1.5f, sc );
739 v3_muladds( sc, right, 0.45f, sa );
740 v3_muladds( sc, right, -0.45f, sb );
741 v3_muls( up, -1.0f, down );
742
743 ray_hit ha, hb;
744 ha.dist = 8.0f;
745 hb.dist = 8.0f;
746 if( ray_world( world, sa, down, &ha ) &&
747 ray_world( world, sb, down, &hb ))
748 {
749 scene_vert va, vb;
750
751 v3_muladds( ha.pos, up, 0.06f, va.co );
752 v3_muladds( hb.pos, up, 0.06f, vb.co );
753
754 scene_vert_pack_norm( &va, up );
755 scene_vert_pack_norm( &vb, up );
756 v2_zero( va.uv );
757 v2_zero( vb.uv );
758
759 scene_push_vert( world->scene_lines, &va );
760 scene_push_vert( world->scene_lines, &vb );
761
762 if( last_valid )
763 {
764 /* Connect them with triangles */
765 scene_push_tri( world->scene_lines, (u32[3]){
766 last_valid+0-2, last_valid+1-2, last_valid+2-2} );
767 scene_push_tri( world->scene_lines, (u32[3]){
768 last_valid+1-2, last_valid+3-2, last_valid+2-2} );
769 }
770
771 last_valid = world->scene_lines->vertex_count;
772 }
773 else
774 last_valid = 0;
775
776 t += 1.0f*mod;
777
778 if( t >= 1.0f )
779 {
780 /* TODO special case for end of loop, need to add triangles
781 * between first and last rungs */
782 break;
783 }
784 }
785
786 rnj->current_refs ++;
787 }
788
789 scene_copy_slice( world->scene_lines, &route->sm );
790 }
791
792 VG_STATIC void world_scene_compute_light_clusters( world_instance *world,
793 scene *sc );
794 /*
795 * Create the strips of colour that run through the world along course paths
796 */
797 VG_STATIC void world_routes_generate( world_instance *world )
798 {
799 vg_info( "Generating route meshes\n" );
800 world->scene_lines = scene_init( world_global.generic_heap, 200000, 300000 );
801
802 for( u32 i=0; i<world->route_count; i++ )
803 world_routes_create_mesh( world, i );
804
805 world_scene_compute_light_clusters( world, world->scene_lines );
806
807 vg_acquire_thread_sync();
808 {
809 scene_upload( world->scene_lines, &world->mesh_route_lines );
810 }
811 vg_release_thread_sync();
812 vg_linear_del( world_global.generic_heap, world->scene_lines );
813 }
814
815 /* determine if special type is required for this gate */
816 VG_STATIC enum route_special_type world_route_node_type( world_instance *world,
817 mdl_node *pnode )
818 {
819 if( pnode->classtype == k_classtype_gate )
820 {
821 struct classtype_gate *inf = mdl_get_entdata( world->meta, pnode );
822
823 if( inf->target )
824 {
825 mdl_node *pother = mdl_node_from_id( world->meta, inf->target );
826
827 if( pother->classtype == k_classtype_gate )
828 {
829 return k_route_special_type_gate;
830 }
831 }
832
833 return k_route_special_type_collector;
834 }
835
836 return k_route_special_type_none;
837 }
838
839 /* count entities and allocate correct amount of memory in advance */
840 VG_STATIC void world_routes_allocate( world_instance *world )
841 {
842 vg_info( "Allocating routes\n" );
843
844 /* count */
845 u32 node_count = 0,
846 route_count = 0,
847 gate_count = 0,
848 collector_count = 0;
849
850 for( int i=0; i<world->meta->info.node_count; i++ )
851 {
852 mdl_node *pnode = mdl_node_from_id( world->meta, i );
853
854 if( pnode->classtype == k_classtype_route_node ||
855 pnode->classtype == k_classtype_gate )
856 {
857 pnode->sub_uid = node_count;
858
859 enum route_special_type type = world_route_node_type( world, pnode );
860
861 if( type == k_route_special_type_gate )
862 gate_count ++;
863 else if( type == k_route_special_type_collector )
864 collector_count ++;
865
866 node_count ++;
867 }
868 else if( pnode->classtype == k_classtype_route )
869 {
870 route_count ++;
871 }
872 }
873
874 /* allocate */
875 u32 node_size = node_count * sizeof(struct route_node),
876 route_size = route_count * sizeof(struct route),
877 gate_size = gate_count * sizeof(struct route_gate),
878 collector_size = collector_count * sizeof(struct route_collector);
879
880 world->nodes = vg_linear_alloc( world_global.generic_heap, node_size );
881 world->routes = vg_linear_alloc( world_global.generic_heap, route_size );
882 world->gates = vg_linear_alloc( world_global.generic_heap, gate_size );
883 world->collectors = vg_linear_alloc( world_global.generic_heap,
884 collector_size );
885 }
886
887 /* create node from mdl node */
888 VG_STATIC struct route_node *world_routes_create_node( world_instance *world,
889 mdl_node *pnode )
890 {
891 struct route_node *rn = &world->nodes[ world->node_count ++ ];
892
893 m4x3f transform;
894 mdl_node_transform( pnode, transform );
895
896 v3_copy( transform[3], rn->co );
897 v3_copy( transform[0], rn->right );
898 v3_copy( transform[1], rn->up );
899 v3_muls( transform[2], -1.0f, rn->h );
900 v3_normalize( rn->right );
901 v3_normalize( rn->up );
902
903 rn->next[0] = 0xffffffff;
904 rn->next[1] = 0xffffffff;
905
906 rn->special_type = 0;
907 rn->special_id = 0;
908 rn->current_refs = 0;
909 rn->ref_count = 0;
910
911 return rn;
912 }
913
914 /* retrieve the correct node id from mdl subuid */
915 VG_STATIC u32 world_routes_get_subuid( world_instance *world, u32 target )
916 {
917 if( target == 0 )
918 return 0xffffffff;
919 else
920 return mdl_node_from_id( world->meta, target )->sub_uid;
921 }
922
923 #if 0
924 VG_STATIC void world_id_fixup( u32 *uid, mdl_context *mdl )
925 {
926 if( *uid )
927 *uid = mdl_node_from_id( mdl, *uid )->sub_uid;
928 else
929 *uid = 0xffffffff;
930 }
931 #endif
932
933 /* process gate attachement onto node */
934 VG_STATIC void world_routes_process_gate( world_instance *world,
935 struct route_node *rn,
936 mdl_node *pnode )
937 {
938 struct classtype_gate *inf = mdl_get_entdata( world->meta, pnode );
939
940 /* H is later scaled based on link distance */
941 v3_normalize( rn->h );
942
943 rn->next[0] = world_routes_get_subuid( world, inf->target );
944 rn->next[1] = 0xffffffff;
945 rn->special_type = world_route_node_type( world, pnode );
946
947 /* process gate type */
948 if( rn->special_type == k_route_special_type_gate )
949 {
950 mdl_node *pother = mdl_node_from_id( world->meta, inf->target );
951
952 struct route_gate *rg = &world->gates[ world->gate_count ];
953
954 rg->node_id = world->node_count-1;
955 rg->timing.time = 0.0;
956 rg->timing.version = 0;
957
958 v3_copy( pnode->co, rg->gate.co[0] );
959 v3_copy( pother->co, rg->gate.co[1] );
960 v4_copy( pnode->q, rg->gate.q[0] );
961 v4_copy( pother->q, rg->gate.q[1] );
962 v2_copy( inf->dims, rg->gate.dims );
963
964 gate_transform_update( &rg->gate );
965 rn->special_id = world->gate_count;
966
967 world->gate_count ++;
968 }
969
970 /* process collector type */
971 else if( rn->special_type == k_route_special_type_collector )
972 {
973 struct route_collector *rc =
974 &world->collectors[ world->collector_count ];
975
976 rc->timing.time = 0.0;
977 rc->timing.version = 0;
978
979 rn->special_id = world->collector_count;
980 world->collector_count ++;
981 }
982 else
983 vg_fatal_exit_loop( "Invalid state" );
984 }
985
986 /* create route from node description */
987 VG_STATIC void world_routes_create_route( world_instance *world,
988 mdl_node *pnode )
989 {
990 mdl_context *mdl = world->meta;
991
992 struct classtype_route *inf = mdl_get_entdata( mdl, pnode );
993 struct route *route = &world->routes[ world->route_count ];
994 memset( route, 0, sizeof(struct route) );
995
996 v3_copy( inf->colour, route->colour );
997 route->colour[3] = 1.0f;
998 route->track_id = 0xffffffff;
999
1000 for( u32 j=0; j<vg_list_size(track_infos); j++ )
1001 {
1002 if( !strcmp( mdl_pstr(mdl,pnode->pstr_name), track_infos[j].name ))
1003 {
1004 route->track_id = j;
1005 break;
1006 }
1007 }
1008
1009 route->start = world_routes_get_subuid( world, inf->id_start );
1010 route->active = 0;
1011 route->factive = 0.0f;
1012 mdl_node_transform( pnode, route->scoreboard_transform );
1013
1014 struct route_ui_bar *pui = &world_global.ui_bars[ world->route_count ];
1015 pui->indices_head = k_route_ui_max_indices - 9;
1016 pui->vertex_head = k_route_ui_max_verts - 200;
1017 pui->segment_start = 0;
1018 pui->segment_count = 0;
1019 pui->fade_start = 0;
1020 pui->fade_count = 0;
1021 pui->fade_timer_start = 0.0;
1022
1023 world->route_count ++;
1024 }
1025
1026 /* load all routes from model header */
1027 VG_STATIC void world_routes_process( world_instance *world )
1028 {
1029 vg_info( "Initializing routes\n" );
1030 mdl_context *mdl = world->meta;
1031
1032 for( int i=0; i<mdl->info.node_count; i++ )
1033 {
1034 mdl_node *pnode = mdl_node_from_id(mdl,i);
1035
1036 if( pnode->classtype == k_classtype_route_node ||
1037 pnode->classtype == k_classtype_gate )
1038 {
1039 struct route_node *rn = world_routes_create_node( world, pnode );
1040
1041 if( pnode->classtype == k_classtype_gate )
1042 {
1043 world_routes_process_gate( world, rn, pnode );
1044 }
1045 else
1046 {
1047 struct classtype_route_node *inf = mdl_get_entdata( mdl, pnode );
1048 rn->next[0] = world_routes_get_subuid( world, inf->target );
1049 rn->next[1] = world_routes_get_subuid( world, inf->target1 );
1050 }
1051 }
1052 else if( pnode->classtype == k_classtype_route )
1053 {
1054 world_routes_create_route( world, pnode );
1055 }
1056 }
1057
1058 /*
1059 * Gather references
1060 */
1061 for( int i=0; i<world->route_count; i++ )
1062 {
1063 struct route *route = &world->routes[i];
1064
1065 u32 stack[64];
1066 u32 si = world_routes_get_path( world, route->start, stack );
1067
1068 for( int sj=0; sj<si; sj++ )
1069 {
1070 struct route_node *rn = &world->nodes[ stack[sj] ];
1071 rn->route_ids[ rn->ref_count ++ ] = i;
1072
1073 if( rn->ref_count > 4 )
1074 vg_warn( "Too many references on route node %i\n", i );
1075 }
1076 }
1077 }
1078
1079 /*
1080 * -----------------------------------------------------------------------------
1081 * Events
1082 * -----------------------------------------------------------------------------
1083 */
1084
1085 VG_STATIC void world_routes_init(void)
1086 {
1087 world_global.current_run_version = 2;
1088 world_global.time = RESET_MAX_TIME*2.0;
1089 world_global.last_use = 0.0;
1090
1091 shader_scene_route_register();
1092 shader_routeui_register();
1093
1094 vg_acquire_thread_sync();
1095 {
1096 /* UI buffers */
1097 for( int i=0; i<vg_list_size(world_global.ui_bars); i++ )
1098 {
1099 /* OpenGL strips */
1100 struct route_ui_bar *pui = &world_global.ui_bars[i];
1101
1102 glGenVertexArrays( 1, &pui->vao );
1103 glGenBuffers( 1, &pui->vbo );
1104 glGenBuffers( 1, &pui->ebo );
1105 glBindVertexArray( pui->vao );
1106
1107 size_t stride = sizeof(v2f);
1108
1109 glBindBuffer( GL_ARRAY_BUFFER, pui->vbo );
1110 glBufferData( GL_ARRAY_BUFFER, k_route_ui_max_verts*stride,
1111 NULL, GL_DYNAMIC_DRAW );
1112
1113 glBindVertexArray( pui->vao );
1114 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, pui->ebo );
1115 glBufferData( GL_ELEMENT_ARRAY_BUFFER,
1116 k_route_ui_max_indices*sizeof(u16), NULL,
1117 GL_DYNAMIC_DRAW );
1118
1119 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, stride, (void *)0 );
1120 glEnableVertexAttribArray( 0 );
1121 VG_CHECK_GL_ERR();
1122 }
1123 }
1124 vg_release_thread_sync();
1125 }
1126
1127 VG_STATIC void world_routes_update( world_instance *world )
1128 {
1129 world_global.time += vg.time_delta;
1130
1131 for( int i=0; i<world->route_count; i++ )
1132 {
1133 struct route *route = &world->routes[i];
1134 route->factive = vg_lerpf( route->factive, route->active,
1135 0.6f*vg.time_delta );
1136
1137 if( route->active )
1138 {
1139 world_routes_ui_updatetime(i, world_global.time - route->latest_pass );
1140 }
1141 }
1142 }
1143
1144 VG_STATIC void bind_terrain_noise(void);
1145 VG_STATIC void world_bind_light_array( world_instance *world,
1146 GLuint shader, GLuint location,
1147 int slot );
1148 VG_STATIC void world_bind_light_index( world_instance *world,
1149 GLuint shader, GLuint location,
1150 int slot );
1151
1152 VG_STATIC void render_world_routes( world_instance *world, camera *cam )
1153 {
1154 m4x3f identity_matrix;
1155 m4x3_identity( identity_matrix );
1156
1157 shader_scene_route_use();
1158 shader_scene_route_uTexGarbage(0);
1159 world_link_lighting_ub( world, _shader_scene_route.id );
1160 world_bind_position_texture( world, _shader_scene_route.id,
1161 _uniform_scene_route_g_world_depth, 2 );
1162 world_bind_light_array( world, _shader_scene_route.id,
1163 _uniform_scene_route_uLightsArray, 3 );
1164 world_bind_light_index( world, _shader_scene_route.id,
1165 _uniform_scene_route_uLightsIndex, 4 );
1166 bind_terrain_noise();
1167
1168 shader_scene_route_uPv( cam->mtx.pv );
1169 shader_scene_route_uPvmPrev( cam->mtx_prev.pv );
1170 shader_scene_route_uMdl( identity_matrix );
1171 shader_scene_route_uCamera( cam->transform[3] );
1172 shader_scene_route_uBoard0( TEMP_BOARD_0 );
1173 shader_scene_route_uBoard1( TEMP_BOARD_1 );
1174
1175 mesh_bind( &world->mesh_route_lines );
1176
1177 for( int i=0; i<world->route_count; i++ )
1178 {
1179 struct route *route = &world->routes[i];
1180
1181 v4f colour;
1182 v3_lerp( (v3f){0.7f,0.7f,0.7f}, route->colour, route->factive, colour );
1183 colour[3] = 1.0f;
1184
1185 shader_scene_route_uColour( colour );
1186 mdl_draw_submesh( &route->sm );
1187 }
1188 }
1189
1190 VG_STATIC void render_world_routes_ui( world_instance *world )
1191 {
1192 glEnable(GL_BLEND);
1193 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1194 glBlendEquation(GL_FUNC_ADD);
1195
1196 float active_offset = 0.0f;
1197 for( int i=0; i<world->route_count; i++ )
1198 {
1199 struct route *route = &world->routes[i];
1200 world_routes_ui_draw( world, i, route->colour, active_offset );
1201 active_offset += route->factive;
1202 }
1203
1204 glDisable(GL_BLEND);
1205 }
1206
1207 #endif /* ROUTES_H */