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