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