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