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