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