much improve
[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; /* 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 time;
62
63 u32 vertex_start, vertex_count,
64 index_start, index_count;
65 }
66 segments[k_max_ui_segments];
67
68 u32 segment_start, segment_count;
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 * Break a index range into two pieces over the edge of the maximum it can
218 * store. s1 is 0 always, so its a ring buffer.
219 */
220 static void world_routes_ui_split_indices( u32 s0, u32 count, u32 *c0, u32 *c1 )
221 {
222 *c0 = (VG_MIN( s0+count, k_route_ui_max_indices )) - s0;
223 *c1 = count-(*c0);
224 }
225
226 /*
227 * Place a set of indices into gpu array automatically splits
228 * across bounds
229 */
230 static void world_routes_ui_set_indices( struct route *pr,
231 u16 *indices, u32 count )
232 {
233 u32 c0, c1;
234 world_routes_ui_split_indices( pr->ui.indices_head, count, &c0, &c1 );
235
236 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, pr->ui.ebo );
237
238 if( c0 )
239 {
240 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, pr->ui.indices_head*sizeof(u16),
241 c0*sizeof(u16), indices );
242 }
243
244 if( c1 )
245 {
246 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, c1*sizeof(u16), indices+c0 );
247 pr->ui.indices_head = c1;
248 }
249 else
250 pr->ui.indices_head += c0;
251 }
252
253 /*
254 * Place a set of vertices into gpu array
255 */
256 static u32 world_routes_ui_set_verts( struct route *pr, v2f *verts, u32 count )
257 {
258 if( pr->ui.vertex_head + count >= k_route_ui_max_verts )
259 pr->ui.vertex_head = 0;
260
261 u32 vert_start = pr->ui.vertex_head;
262 pr->ui.vertex_head += count;
263
264 glBindBuffer( GL_ARRAY_BUFFER, pr->ui.vbo );
265 glBufferSubData( GL_ARRAY_BUFFER, (GLintptr)(vert_start*sizeof(v2f)),
266 sizeof(v2f)*count, verts );
267
268 return vert_start;
269 }
270
271 /*
272 * Update the last (count) vertices positions, does not add any.
273 * Data must already be written to, and not cross either array boundaries.
274 */
275 static u32 world_routes_ui_update_verts( struct route *pr,
276 v2f *verts, u32 count )
277 {
278 u32 vert_start = pr->ui.vertex_head-count;
279
280 glBindBuffer( GL_ARRAY_BUFFER, pr->ui.vbo );
281 glBufferSubData( GL_ARRAY_BUFFER, (GLintptr)(vert_start*sizeof(v2f)),
282 sizeof(v2f)*count, verts );
283
284 return vert_start;
285 }
286
287 /*
288 * Current/active segment of this UI bar
289 */
290 static struct route_ui_segment *world_routes_ui_curseg( struct route *pr )
291 {
292 u32 index = (pr->ui.segment_start+pr->ui.segment_count-1)%k_max_ui_segments;
293 return &pr->ui.segments[ index ];
294 }
295
296 /*
297 * Start a new segment in the UI bar, will create a split on the last one if
298 * there is one active currently. (api)
299 */
300 static void world_routes_ui_newseg( u32 route, float time )
301 {
302 struct subworld_routes *r = subworld_routes();
303 struct route *pr = &r->routes[route];
304
305 pr->ui.last_notch = 0.0;
306
307 glBindVertexArray( pr->ui.vao );
308 if( pr->ui.segment_count )
309 {
310 float const k_gap_width = 1.0f;
311
312 v2f verts[2];
313 verts[0][0] = time-k_gap_width;
314 verts[0][1] = 0.5f;
315 verts[1][0] = time-k_gap_width;
316 verts[1][1] = -0.5f;
317
318 world_routes_ui_update_verts( pr, verts, 2 );
319 }
320
321 pr->ui.segment_count ++;
322 struct route_ui_segment *segment = world_routes_ui_curseg(pr);
323
324 v2f verts[4];
325 verts[0][0] = 0.0f;
326 verts[0][1] = 0.5f;
327 verts[1][0] = 0.0f;
328 verts[1][1] = -0.5f;
329 verts[2][0] = 0.0f;
330 verts[2][1] = 0.5f;
331 verts[3][0] = 0.0f;
332 verts[3][1] = -0.5f;
333
334 u32 vert_start = world_routes_ui_set_verts( pr, verts, 4 );
335
336 u16 indices[6];
337 indices[0] = vert_start + 0;
338 indices[1] = vert_start + 1;
339 indices[2] = vert_start + 3;
340 indices[3] = vert_start + 0;
341 indices[4] = vert_start + 3;
342 indices[5] = vert_start + 2;
343
344 segment->vertex_start = vert_start;
345 segment->vertex_count = 4;
346 segment->index_start = pr->ui.indices_head;
347 segment->index_count = 6;
348
349 world_routes_ui_set_indices( pr, indices, 6 );
350 }
351
352 /*
353 * Extend the end of the bar
354 */
355 static void world_routes_ui_updatetime( u32 route, float time )
356 {
357 struct subworld_routes *r = subworld_routes();
358 struct route *pr = &r->routes[route];
359
360 v2f verts[2];
361 verts[0][0] = time;
362 verts[0][1] = 0.5f;
363 verts[1][0] = time;
364 verts[1][1] = -0.5f;
365
366 u32 vert_start = pr->ui.vertex_head-2;
367
368 glBindVertexArray( pr->ui.vao );
369 world_routes_ui_update_verts( pr, verts, 2 );
370 }
371
372 /*
373 * Create a notch in the bar, used when a reset is triggered by the user
374 */
375 static void world_routes_ui_notch( u32 route, float time )
376 {
377 struct subworld_routes *r = subworld_routes();
378 struct route *pr = &r->routes[route];
379
380 if( (time - pr->ui.last_notch) > 1.0 )
381 {
382 v2f verts[8];
383
384 float const k_notch_width = 1.0f;
385
386 float xa = time-k_notch_width,
387 xb = time-k_notch_width * 0.5f,
388 xc = time;
389
390 verts[0][0] = xa;
391 verts[0][1] = 0.5f;
392 verts[1][0] = xa;
393 verts[1][1] = -0.5f;
394
395 verts[2][0] = xb;
396 verts[2][1] = 0.25f;
397 verts[3][0] = xb;
398 verts[3][1] = -0.25f;
399
400 verts[4][0] = xc;
401 verts[4][1] = 0.5f;
402 verts[5][0] = xc;
403 verts[5][1] = -0.5f;
404
405 verts[6][0] = xc;
406 verts[6][1] = 0.5f;
407 verts[7][0] = xc;
408 verts[7][1] = -0.5f;
409
410 glBindVertexArray( pr->ui.vao );
411 u32 vert_start_mod = world_routes_ui_update_verts( pr, verts, 2 ),
412 vert_start_new = world_routes_ui_set_verts( pr, verts+2, 6 );
413
414 u16 indices[18];
415 indices[ 0] = vert_start_mod+1;
416 indices[ 1] = vert_start_new+0;
417 indices[ 2] = vert_start_mod+0;
418 indices[ 3] = vert_start_mod+1;
419 indices[ 4] = vert_start_new+1;
420 indices[ 5] = vert_start_new+0;
421
422 indices[ 6] = vert_start_new+0;
423 indices[ 7] = vert_start_new+1;
424 indices[ 8] = vert_start_new+3;
425 indices[ 9] = vert_start_new+0;
426 indices[10] = vert_start_new+3;
427 indices[11] = vert_start_new+2;
428
429 indices[12] = vert_start_new+3;
430 indices[13] = vert_start_new+4;
431 indices[14] = vert_start_new+2;
432 indices[15] = vert_start_new+3;
433 indices[16] = vert_start_new+5;
434 indices[17] = vert_start_new+4;
435
436 world_routes_ui_set_indices( pr, indices, 18 );
437
438 pr->ui.last_notch = time;
439
440 struct route_ui_segment *segment = world_routes_ui_curseg(pr);
441 segment->vertex_count += 6;
442 segment->index_count += 18;
443 }
444 }
445
446 static void world_routes_ui_draw( u32 route )
447 {
448 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
449
450 struct subworld_routes *r = subworld_routes();
451 struct route *pr = &r->routes[route];
452
453 shader_routeui_use();
454 glBindVertexArray( pr->ui.vao );
455
456 for( u32 i=0; i<pr->ui.segment_count; i++ )
457 {
458 u32 j = (pr->ui.segment_start + i) % k_max_ui_segments;
459 struct route_ui_segment *segment = &pr->ui.segments[j];
460
461 u32 c0, c1;
462 world_routes_ui_split_indices( segment->index_start,
463 segment->index_count, &c0, &c1 );
464
465 if( c0 )
466 glDrawElements( GL_TRIANGLES, c0, GL_UNSIGNED_SHORT,
467 (void *)(segment->index_start*sizeof(u16)));
468 if( c1 )
469 glDrawElements( GL_TRIANGLES, c1, GL_UNSIGNED_SHORT, (void *)(0) );
470 }
471
472 //glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
473 }
474
475 /*
476 * Will scan the whole run for two things;
477 * 1: we set a new record for the total, complete loop around the course
478 * 2: the time of each segment will be recorded into the data buffer
479 * (not implemented: TODO)
480 */
481 static void world_routes_verify_run( u32 route )
482 {
483 struct subworld_routes *r = subworld_routes();
484
485 u32 stack[64];
486 u32 si = world_routes_get_path( r->routes[route].start, stack );
487
488 /*
489 * we only care about gates that ref gates, so shuffle down the array
490 */
491 struct route_timing *timings[64];
492 u32 sj = 0, maxv = 0, begin = 0;
493 for( u32 i=0; i<si; i++ )
494 {
495 if( r->nodes[stack[i]].special_type == k_route_special_type_collector )
496 timings[sj ++] = &r->collectors[r->nodes[stack[i]].special_id].timing;
497 else if( r->nodes[stack[i]].special_type == k_route_special_type_gate )
498 timings[sj ++] = &r->gates[r->nodes[stack[i]].special_id].timing;
499 }
500
501 for( u32 i=0; i<sj; i++ )
502 {
503 if( timings[i]->version > maxv )
504 {
505 maxv = timings[i]->version;
506 begin = i;
507 }
508 }
509
510 vg_info( "== begin verification (%u) ==\n", route );
511 vg_info( " current version: %u\n", r->current_run_version );
512
513 int verified = 0;
514 if( timings[begin]->version == r->current_run_version )
515 verified = 1;
516
517 double lap_time = 0.0;
518
519 for( u32 i=0; i<sj; i++ )
520 {
521 u32 j = (sj+begin-i-1) % sj,
522 j1 = (j+1) % sj;
523
524 double diff = 0.0;
525
526 if( i<sj-1 )
527 {
528 /* j1v should equal jv+1 */
529 if( timings[j1]->version == timings[j]->version+1 )
530 {
531 diff = timings[j1]->time - timings[j]->time;
532 lap_time += diff;
533 }
534 else
535 verified = 0;
536 }
537
538 if( verified )
539 vg_success( " [ %u %f ] %f\n", timings[j1]->time,
540 timings[j1]->version, diff );
541 else
542 vg_warn( " [ %u %f ]\n", timings[j1]->time, timings[j1]->version );
543 }
544
545 if( verified )
546 vg_success( " NEW LAP TIME: %f\n", lap_time );
547 else
548 vg_info( " ctime: %f\n", lap_time );
549 }
550
551 /*
552 * When going through a gate this is called for bookkeeping purposes
553 */
554 static void world_routes_activate_gate( u32 id )
555 {
556 struct subworld_routes *r = subworld_routes();
557 struct route_gate *rg = &r->gates[id];
558 struct route_node *pnode = &r->nodes[rg->node_id],
559 *pdest = &r->nodes[pnode->next[0]];
560
561 struct route_collector *rc = &r->collectors[ pdest->special_id ];
562
563 r->active_gate = id;
564 rg->timing.version = r->current_run_version;
565 rg->timing.time = vg_time;
566 for( u32 i=0; i<r->route_count; i++ )
567 {
568 struct route *route = &r->routes[i];
569
570 route->active = 0;
571 for( u32 j=0; j<pdest->ref_count; j++ )
572 {
573 if( pdest->route_ids[j] == i )
574 {
575 world_routes_verify_run( i );
576 route->active = 1;
577 break;
578 }
579 }
580 }
581
582 r->current_run_version ++;
583
584 rc->timing.version = r->current_run_version;
585 rc->timing.time = vg_time;
586 r->current_run_version ++;
587 }
588
589 static void world_routes_debug(void)
590 {
591 struct subworld_routes *r = subworld_routes();
592
593 for( int i=0; i<r->node_count; i++ )
594 {
595 struct route_node *rn = &r->nodes[i];
596 vg_line_pt3( rn->co, 1.0f, rn->special_type? 0xffffff00: 0xff00b2ff );
597 }
598
599 for( int i=0; i<r->route_count; i++ )
600 {
601 struct route *route = &r->routes[i];
602
603 u32 stack[64];
604 u32 si = world_routes_get_path( route->start, stack );
605
606 u32 colours[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
607 0xff5442f5 };
608
609 u32 cc = colours[i%vg_list_size(colours)];
610
611 for( int sj=0; sj<si; sj++ )
612 {
613 int sk = (sj+1)%si;
614 debug_sbpath( &r->nodes[stack[sj]], &r->nodes[stack[sk]], cc,
615 (float)i );
616 }
617 }
618
619 for( int i=0; i<r->node_count; i++ )
620 {
621 struct route_node *ri = &r->nodes[i],
622 *rj = NULL;
623
624 for( int j=0; j<2; j++ )
625 {
626 if( ri->next[j] != 0xffffffff )
627 {
628 rj = &r->nodes[ri->next[j]];
629 vg_line( ri->co, rj->co, 0x20ffffff );
630 }
631 }
632 }
633 }
634
635 static void world_routes_free(void)
636 {
637 struct subworld_routes *r = subworld_routes();
638
639 free( r->nodes );
640 free( r->routes );
641 free( r->gates );
642 }
643
644 static void world_id_fixup( u32 *uid, mdl_header *mdl )
645 {
646 if( *uid )
647 *uid = mdl_node_from_id( mdl, *uid )->sub_uid;
648 else
649 *uid = 0xffffffff;
650 }
651
652 /*
653 * Create the strips of colour that run through the world along course paths
654 */
655 static void world_routes_gen_meshes(void)
656 {
657 struct subworld_routes *r = subworld_routes();
658 scene_init( &r->scene_lines );
659
660 for( int i=0; i<r->route_count; i++ )
661 {
662 struct route *route = &r->routes[i];
663
664 u32 stack[64];
665 u32 si = world_routes_get_path( route->start, stack );
666
667 u32 last_valid = 0;
668
669 for( int sj=0; sj<si; sj++ )
670 {
671 int sk=(sj+1)%si;
672
673 struct route_node *rnj = &r->nodes[ stack[sj] ],
674 *rnk = &r->nodes[ stack[sk] ],
675 *rnl;
676
677 if( rnj->special_type && rnk->special_type )
678 {
679 last_valid = 0;
680 continue;
681 }
682
683 float base_x0 = (float)rnj->ref_count*-0.5f + (float)rnj->current_refs,
684 base_x1 = (float)rnk->ref_count*-0.5f + (float)rnk->current_refs;
685
686 if( rnk->special_type )
687 {
688 rnl = &r->nodes[ rnk->next[0] ];
689 base_x1 = (float)rnl->ref_count*-0.5f + (float)rnl->current_refs;
690 }
691
692 if( sk == 0 )
693 {
694 base_x1 -= 1.0f;
695 }
696
697 v3f p0, h0, p1, h1, p, pd;
698
699 v3_copy( rnj->co, p0 );
700 v3_muladds( rnj->co, rnj->h, 1.0f, h0 );
701 v3_copy( rnk->co, p1 );
702 v3_muladds( rnk->co, rnk->h, -1.0f, h1 );
703
704 float t=0.0f;
705 int it = 0;
706
707 for( int it=0; it<256; it ++ )
708 {
709 float const k_sample_dist = 0.02f;
710 eval_bezier_time( p0,p1,h0,h1, t,p );
711 eval_bezier_time( p0,p1,h0,h1, t+k_sample_dist,pd );
712
713 float mod = k_sample_dist / v3_dist( p, pd );
714
715 v3f v0,up, right;
716 v3_muls( rnj->up, 1.0f-t, up );
717 v3_muladds( up, rnk->up, t, up );
718
719 v3_sub( pd,p,v0 );
720 v3_cross( up, v0, right );
721 v3_normalize( right );
722
723 float cur_x = (1.0f-t)*base_x0 + t*base_x1;
724
725 v3f sc, sa, sb, down;
726 v3_muladds( p, right, cur_x, sc );
727 v3_muladds( sc, up, 1.5f, sc );
728 v3_muladds( sc, right, 0.45f, sa );
729 v3_muladds( sc, right, -0.45f, sb );
730 v3_muls( up, -1.0f, down );
731
732 ray_hit ha, hb;
733 ha.dist = 8.0f;
734 hb.dist = 8.0f;
735 if(ray_world( sa, down, &ha ) &&
736 ray_world( sb, down, &hb ))
737 {
738 mdl_vert va, vb;
739
740 v3_muladds( ha.pos, up, 0.06f, va.co );
741 v3_muladds( hb.pos, up, 0.06f, vb.co );
742 v3_copy( up, va.norm );
743 v3_copy( up, vb.norm );
744 v3_zero( va.colour );
745 v3_zero( vb.colour );
746 v2_zero( va.uv );
747 v2_zero( vb.uv );
748
749 scene_push_vert( &r->scene_lines, &va );
750 scene_push_vert( &r->scene_lines, &vb );
751
752 if( last_valid )
753 {
754 /* Connect them with triangles */
755 scene_push_tri( &r->scene_lines, (u32[3]){
756 last_valid+0-2, last_valid+1-2, last_valid+2-2} );
757 scene_push_tri( &r->scene_lines, (u32[3]){
758 last_valid+1-2, last_valid+3-2, last_valid+2-2} );
759 }
760
761 last_valid = r->scene_lines.vertex_count;
762 }
763 else
764 last_valid = 0;
765
766 t += 1.0f*mod;
767
768 if( t >= 1.0f )
769 {
770 /* TODO special case for end of loop, need to add triangles
771 * between first and last rungs */
772 break;
773 }
774 }
775
776 rnj->current_refs ++;
777 }
778
779 scene_copy_slice( &r->scene_lines, &route->sm );
780 }
781
782 scene_upload( &r->scene_lines );
783 scene_free_offline_buffers( &r->scene_lines );
784 }
785
786 static void bind_terrain_textures(void);
787 static void render_world_routes( m4x4f projection, v3f camera )
788 {
789 struct subworld_routes *r = subworld_routes();
790
791 m4x3f identity_matrix;
792 m4x3_identity( identity_matrix );
793
794 shader_route_use();
795 shader_route_uTexGarbage(0);
796 shader_link_standard_ub( _shader_route.id, 2 );
797 bind_terrain_textures();
798
799 shader_route_uPv( projection );
800 shader_route_uMdl( identity_matrix );
801 shader_route_uCamera( camera );
802
803 scene_bind( &r->scene_lines );
804
805 for( int i=0; i<r->route_count; i++ )
806 {
807 struct route *route = &r->routes[i];
808 route->factive = vg_lerpf( route->factive, route->active, 0.01f );
809
810 v4f colour;
811 v3_lerp( (v3f){0.7f,0.7f,0.7f}, route->colour, route->factive, colour );
812 colour[3] = 1.0f;
813
814 shader_route_uColour( colour );
815 mdl_draw_submesh( &route->sm );
816 }
817 }
818
819 static void world_routes_register(void)
820 {
821 struct subworld_routes *r = subworld_routes();
822 r->current_run_version = 2;
823
824 shader_route_register();
825 shader_routeui_register();
826 }
827
828 static void world_routes_loadfrom( mdl_header *mdl )
829 {
830 struct subworld_routes *r = subworld_routes();
831 r->nodes = NULL;
832 r->node_count = 0;
833 r->node_cap = 0;
834 r->routes = NULL;
835 r->route_count = 0;
836 r->route_cap = 0;
837 r->gates = NULL;
838 r->gate_count = 0;
839 r->gate_cap = 0;
840
841 for( int i=0; i<mdl->node_count; i++ )
842 {
843 mdl_node *pnode = mdl_node_from_id(mdl,i);
844 m4x3f transform;
845
846 if( pnode->classtype == k_classtype_route_node ||
847 pnode->classtype == k_classtype_gate )
848 {
849 mdl_node_transform( pnode, transform );
850 pnode->sub_uid = r->node_count;
851
852 r->nodes = buffer_reserve( r->nodes, r->node_count, &r->node_cap, 1,
853 sizeof( struct route_node ) );
854
855 struct route_node *rn = &r->nodes[r->node_count];
856
857 v3_copy( transform[0], rn->right );
858 v3_normalize( rn->right );
859 v3_copy( transform[1], rn->up );
860 v3_normalize( rn->up );
861 v3_muls( transform[2], -1.0f, rn->h );
862 v3_copy( transform[3], rn->co );
863 rn->ref_count = 0;
864 rn->current_refs = 0;
865 rn->special_type = 0;
866 rn->special_id = 0;
867
868 if( pnode->classtype == k_classtype_gate )
869 {
870 struct classtype_gate *inf = mdl_get_entdata( mdl, pnode );
871
872 /* H is later scaled based on link distance */
873 v3_normalize( rn->h );
874 rn->next[0] = inf->target;
875 rn->next[1] = 0;
876
877 /* TODO */
878 if( inf->target )
879 {
880 mdl_node *pother = mdl_node_from_id( mdl, inf->target );
881
882 if( pother->classtype == k_classtype_gate )
883 {
884 r->gates = buffer_reserve( r->gates, r->gate_count,
885 &r->gate_cap,
886 1, sizeof( struct route_gate ) );
887
888 struct route_gate *rg = &r->gates[r->gate_count];
889 rg->node_id = r->node_count;
890 rg->timing.time = 0.0;
891 rg->timing.version = 0;
892
893 v3_copy( pnode->co, rg->gate.co[0] );
894 v3_copy( pother->co, rg->gate.co[1] );
895 v4_copy( pnode->q, rg->gate.q[0] );
896 v4_copy( pother->q, rg->gate.q[1] );
897 v2_copy( inf->dims, rg->gate.dims );
898
899 gate_transform_update( &rg->gate );
900 rn->special_type = k_route_special_type_gate;
901 rn->special_id = r->gate_count;
902
903 r->gate_count ++;
904 }
905 }
906
907 if( rn->special_type == 0 )
908 {
909 r->collectors = buffer_reserve(
910 r->collectors, r->collector_count, &r->collector_cap,
911 1, sizeof( struct route_collector ));
912
913 struct route_collector *rc = &r->collectors[r->collector_count];
914 rc->timing.time = 0.0;
915 rc->timing.version = 0;
916
917 rn->special_type = k_route_special_type_collector;
918 rn->special_id = r->collector_count;
919
920 r->collector_count ++;
921 }
922 }
923 else
924 {
925 struct classtype_route_node *inf = mdl_get_entdata( mdl, pnode );
926 rn->next[0] = inf->target;
927 rn->next[1] = inf->target1;
928 }
929
930 r->node_count ++;
931 }
932 else if( pnode->classtype == k_classtype_route )
933 {
934 struct classtype_route *inf = mdl_get_entdata( mdl, pnode );
935 r->routes = buffer_reserve( r->routes, r->route_count, &r->route_cap,
936 1, sizeof( struct route ) );
937
938 struct route *route = &r->routes[r->route_count];
939
940 v3_copy( inf->colour, route->colour );
941 route->colour[3] = 1.0f;
942
943 route->name = NULL;
944 route->start = inf->id_start;
945 route->active = 0;
946 route->factive = 0.0f;
947
948 /* OpenGL strips */
949 glGenVertexArrays( 1, &route->ui.vao );
950 glGenBuffers( 1, &route->ui.vbo );
951 glGenBuffers( 1, &route->ui.ebo );
952 glBindVertexArray( route->ui.vao );
953
954 size_t stride = sizeof(v2f);
955
956 glBindBuffer( GL_ARRAY_BUFFER, route->ui.vbo );
957 glBufferData( GL_ARRAY_BUFFER, k_route_ui_max_verts*stride,
958 NULL, GL_DYNAMIC_DRAW );
959 glBindVertexArray( route->ui.vao );
960 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, route->ui.ebo );
961 glBufferData( GL_ELEMENT_ARRAY_BUFFER,
962 k_route_ui_max_indices*sizeof(u16), NULL,
963 GL_DYNAMIC_DRAW );
964
965 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, stride, (void *)0 );
966 glEnableVertexAttribArray( 0 );
967 VG_CHECK_GL();
968
969 route->ui.indices_head = k_route_ui_max_indices - 9;
970 route->ui.vertex_head = k_route_ui_max_verts - 200;
971 route->ui.segment_start = 0;
972 route->ui.segment_count = 0;
973 route->ui.last_notch = 0.0;
974
975 r->route_count ++;
976 }
977 }
978
979 /*
980 * Apply correct system-local ids
981 */
982 for( int i=0; i<r->node_count; i++ )
983 {
984 struct route_node *rn = &r->nodes[i];
985
986 for( int j=0; j<2; j++ )
987 world_id_fixup( &rn->next[j], mdl );
988 }
989
990 for( int i=0; i<r->route_count; i++ )
991 {
992 struct route *route = &r->routes[i];
993 world_id_fixup( &route->start, mdl );
994 }
995
996 /*
997 * Gather references
998 */
999 for( int i=0; i<r->route_count; i++ )
1000 {
1001 struct route *route = &r->routes[i];
1002
1003 u32 stack[64];
1004 u32 si = world_routes_get_path( route->start, stack );
1005
1006 for( int sj=0; sj<si; sj++ )
1007 {
1008 struct route_node *rn = &r->nodes[ stack[sj] ];
1009 rn->route_ids[ rn->ref_count ++ ] = i;
1010
1011 if( rn->ref_count > 4 )
1012 vg_warn( "Too many references on route node %i\n", i );
1013 }
1014 }
1015
1016 world_routes_gen_meshes();
1017 }
1018
1019 #endif /* ROUTES_H */