8 #include "shaders/vblend.h"
9 #include "shaders/route.h"
10 #include "shaders/routeui.h"
12 enum route_special_type
14 k_route_special_type_gate
= 1,
15 k_route_special_type_collector
= 2
18 enum { k_max_ui_segments
= 32 };
19 enum { k_route_ui_max_verts
= 2000 };
20 enum { k_route_ui_max_indices
= 3000 };
22 struct subworld_routes
29 u32 special_type
, special_id
, current_refs
, ref_count
;
30 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
48 double best_lap
, latest_pass
; /* Session */
59 struct route_ui_segment
62 u32 vertex_start
, vertex_count
,
63 index_start
, index_count
;
65 segments
[k_max_ui_segments
];
67 u32 segment_start
, segment_count
, fade_start
, fade_count
;
68 double fade_timer_start
;
86 u32 version
; /* Incremented on every teleport */
93 struct route_collector
95 struct route_timing timing
;
110 static struct subworld_routes
*subworld_routes(void);
112 static void debug_sbpath( struct route_node
*rna
, struct route_node
*rnb
,
113 u32 colour
, float xoffset
)
115 v3f p0
, h0
, p1
, h1
, l
, p
;
117 v3_copy( rna
->co
, p0
);
118 v3_muladds( rna
->co
, rna
->h
, 1.0f
, h0
);
119 v3_copy( rnb
->co
, p1
);
120 v3_muladds( rnb
->co
, rnb
->h
, -1.0f
, h1
);
122 v3_muladds( p0
, rna
->right
, xoffset
, p0
);
123 v3_muladds( h0
, rna
->right
, xoffset
, h0
);
124 v3_muladds( p1
, rnb
->right
, xoffset
, p1
);
125 v3_muladds( h1
, rnb
->right
, xoffset
, h1
);
129 for( int i
=0; i
<5; i
++ )
131 float t
= (float)(i
+1)/5.0f
;
132 eval_bezier_time( p0
, p1
, h0
, h1
, t
, p
);
133 vg_line( p
, l
, colour
);
139 * Get a list of node ids in stack, and return how many there is
141 static u32
world_routes_get_path( u32 starter
, u32 stack
[64] )
143 struct subworld_routes
*r
= subworld_routes();
150 int loop_complete
= 0;
154 if( stack_i
[si
-1] == 2 )
160 struct route_node
*rn
= &r
->nodes
[stack
[si
-1]];
161 u32 nextid
= rn
->next
[stack_i
[si
-1]];
164 if( nextid
!= 0xffffffff )
166 if( nextid
== stack
[0] )
173 for( int sj
=0; sj
<si
; sj
++ )
175 if( stack
[sj
] == nextid
)
199 * Free a segment from the UI bar to be reused later
201 static void world_routes_ui_popfirst( u32 route
)
203 struct subworld_routes
*r
= subworld_routes();
204 struct route
*pr
= &r
->routes
[route
];
206 if( pr
->ui
.segment_count
)
208 pr
->ui
.segment_start
++;
210 if( pr
->ui
.segment_start
== 32 )
211 pr
->ui
.segment_start
= 0;
213 pr
->ui
.segment_count
--;
218 * Reset ui bar completely
220 static void world_routes_ui_clear( u32 route
)
222 struct subworld_routes
*r
= subworld_routes();
223 struct route
*pr
= &r
->routes
[route
];
224 pr
->ui
.segment_start
= (pr
->ui
.segment_start
+ pr
->ui
.segment_count
) %
226 pr
->ui
.segment_count
= 0;
230 * Break a index range into two pieces over the edge of the maximum it can
231 * store. s1 is 0 always, so its a ring buffer.
233 static void world_routes_ui_split_indices( u32 s0
, u32 count
, u32
*c0
, u32
*c1
)
235 *c0
= (VG_MIN( s0
+count
, k_route_ui_max_indices
)) - s0
;
240 * Place a set of indices into gpu array automatically splits
243 static void world_routes_ui_set_indices( struct route
*pr
,
244 u16
*indices
, u32 count
)
247 world_routes_ui_split_indices( pr
->ui
.indices_head
, count
, &c0
, &c1
);
249 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, pr
->ui
.ebo
);
253 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, pr
->ui
.indices_head
*sizeof(u16
),
254 c0
*sizeof(u16
), indices
);
259 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, 0, c1
*sizeof(u16
), indices
+c0
);
260 pr
->ui
.indices_head
= c1
;
263 pr
->ui
.indices_head
+= c0
;
267 * Place a set of vertices into gpu array
269 static u32
world_routes_ui_set_verts( struct route
*pr
, v2f
*verts
, u32 count
)
271 if( pr
->ui
.vertex_head
+ count
>= k_route_ui_max_verts
)
272 pr
->ui
.vertex_head
= 0;
274 u32 vert_start
= pr
->ui
.vertex_head
;
275 pr
->ui
.vertex_head
+= count
;
277 glBindBuffer( GL_ARRAY_BUFFER
, pr
->ui
.vbo
);
278 glBufferSubData( GL_ARRAY_BUFFER
, (GLintptr
)(vert_start
*sizeof(v2f
)),
279 sizeof(v2f
)*count
, verts
);
285 * Update the last (count) vertices positions, does not add any.
286 * Data must already be written to, and not cross either array boundaries.
288 static u32
world_routes_ui_update_verts( struct route
*pr
,
289 v2f
*verts
, u32 count
)
291 u32 vert_start
= pr
->ui
.vertex_head
-count
;
293 glBindBuffer( GL_ARRAY_BUFFER
, pr
->ui
.vbo
);
294 glBufferSubData( GL_ARRAY_BUFFER
, (GLintptr
)(vert_start
*sizeof(v2f
)),
295 sizeof(v2f
)*count
, verts
);
301 * Current/active segment of this UI bar
303 static struct route_ui_segment
*world_routes_ui_curseg( struct route
*pr
)
305 u32 index
= (pr
->ui
.segment_start
+pr
->ui
.segment_count
-1)%k_max_ui_segments
;
306 return &pr
->ui
.segments
[ index
];
310 * Start a new segment in the UI bar, will create a split on the last one if
311 * there is one active currently. (api)
313 static void world_routes_ui_newseg( u32 route
)
315 struct subworld_routes
*r
= subworld_routes();
316 struct route
*pr
= &r
->routes
[route
];
318 pr
->ui
.last_notch
= 0.0;
320 glBindVertexArray( pr
->ui
.vao
);
321 if( pr
->ui
.segment_count
)
323 float const k_gap_width
= 1.0f
;
325 struct route_ui_segment
*cseg
= world_routes_ui_curseg(pr
);
328 verts
[0][0] = cseg
->length
-k_gap_width
;
330 verts
[1][0] = cseg
->length
-k_gap_width
;
333 world_routes_ui_update_verts( pr
, verts
, 2 );
336 pr
->ui
.segment_count
++;
337 struct route_ui_segment
*segment
= world_routes_ui_curseg(pr
);
349 u32 vert_start
= world_routes_ui_set_verts( pr
, verts
, 4 );
352 indices
[0] = vert_start
+ 0;
353 indices
[1] = vert_start
+ 1;
354 indices
[2] = vert_start
+ 3;
355 indices
[3] = vert_start
+ 0;
356 indices
[4] = vert_start
+ 3;
357 indices
[5] = vert_start
+ 2;
359 segment
->vertex_start
= vert_start
;
360 segment
->vertex_count
= 4;
361 segment
->index_start
= pr
->ui
.indices_head
;
362 segment
->index_count
= 6;
364 world_routes_ui_set_indices( pr
, indices
, 6 );
368 * Extend the end of the bar
370 static void world_routes_ui_updatetime( u32 route
, float time
)
372 struct subworld_routes
*r
= subworld_routes();
373 struct route
*pr
= &r
->routes
[route
];
381 u32 vert_start
= pr
->ui
.vertex_head
-2;
383 glBindVertexArray( pr
->ui
.vao
);
384 world_routes_ui_update_verts( pr
, verts
, 2 );
386 struct route_ui_segment
*cseg
= world_routes_ui_curseg(pr
);
391 * Create a notch in the bar, used when a reset is triggered by the user
393 static void world_routes_ui_notch( u32 route
, float time
)
395 struct subworld_routes
*r
= subworld_routes();
396 struct route
*pr
= &r
->routes
[route
];
398 if( (time
- pr
->ui
.last_notch
) > 1.0 )
402 float const k_notch_width
= 1.0f
;
404 float xa
= time
-k_notch_width
,
405 xb
= time
-k_notch_width
* 0.5f
,
416 verts
[3][1] = -0.25f
;
428 glBindVertexArray( pr
->ui
.vao
);
429 u32 vert_start_mod
= world_routes_ui_update_verts( pr
, verts
, 2 ),
430 vert_start_new
= world_routes_ui_set_verts( pr
, verts
+2, 6 );
433 indices
[ 0] = vert_start_mod
+1;
434 indices
[ 1] = vert_start_new
+0;
435 indices
[ 2] = vert_start_mod
+0;
436 indices
[ 3] = vert_start_mod
+1;
437 indices
[ 4] = vert_start_new
+1;
438 indices
[ 5] = vert_start_new
+0;
440 indices
[ 6] = vert_start_new
+0;
441 indices
[ 7] = vert_start_new
+1;
442 indices
[ 8] = vert_start_new
+3;
443 indices
[ 9] = vert_start_new
+0;
444 indices
[10] = vert_start_new
+3;
445 indices
[11] = vert_start_new
+2;
447 indices
[12] = vert_start_new
+3;
448 indices
[13] = vert_start_new
+4;
449 indices
[14] = vert_start_new
+2;
450 indices
[15] = vert_start_new
+3;
451 indices
[16] = vert_start_new
+5;
452 indices
[17] = vert_start_new
+4;
454 world_routes_ui_set_indices( pr
, indices
, 18 );
456 pr
->ui
.last_notch
= time
;
458 struct route_ui_segment
*segment
= world_routes_ui_curseg(pr
);
459 segment
->vertex_count
+= 6;
460 segment
->index_count
+= 18;
464 static void world_routes_ui_draw_segment( struct route_ui_segment
*segment
)
467 world_routes_ui_split_indices( segment
->index_start
,
468 segment
->index_count
, &c0
, &c1
);
470 glDrawElements( GL_TRIANGLES
, c0
, GL_UNSIGNED_SHORT
,
471 (void *)(segment
->index_start
*sizeof(u16
)));
473 glDrawElements( GL_TRIANGLES
, c1
, GL_UNSIGNED_SHORT
, (void *)(0) );
477 * Draws full bar at Y offset(offset).
479 static void world_routes_ui_draw( u32 route
, v4f colour
, float offset
)
481 float const k_bar_height
= 0.05f
,
482 k_bar_scale_x
= 0.005f
;
484 struct subworld_routes
*r
= subworld_routes();
485 struct route
*pr
= &r
->routes
[route
];
487 float cx
= pr
->ui
.xpos
;
489 shader_routeui_use();
490 glBindVertexArray( pr
->ui
.vao
);
492 float fade_amt
= vg_time
- pr
->ui
.fade_timer_start
;
493 fade_amt
= vg_clampf( fade_amt
/ 1.0f
, 0.0f
, 1.0f
);
495 float fade_block_size
= 0.0f
,
496 main_block_size
= 0.0f
;
498 for( u32 i
=0; i
<pr
->ui
.fade_count
; i
++ )
500 u32 j
= (pr
->ui
.fade_start
+ i
) % k_max_ui_segments
;
501 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
503 fade_block_size
+= segment
->length
;
506 cx
-= fade_block_size
* fade_amt
;
509 v4_copy( colour
, fade_colour
);
510 fade_colour
[3] *= 1.0f
-fade_amt
;
516 float height
= pr
->factive
*k_bar_height
,
517 base
= -1.0f
+ (offset
+0.5f
)*k_bar_height
;
519 shader_routeui_uColour( fade_colour
);
520 for( u32 i
=0; i
<pr
->ui
.fade_count
; i
++ )
522 u32 j
= (pr
->ui
.fade_start
+ i
) % k_max_ui_segments
;
523 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
525 shader_routeui_uOffset( (v4f
){ cx
*k_bar_scale_x
, base
,
526 k_bar_scale_x
, height
} );
528 world_routes_ui_draw_segment( segment
);
529 cx
+= segment
->length
;
535 shader_routeui_uColour( colour
);
536 for( u32 i
=0; i
<pr
->ui
.segment_count
; i
++ )
538 u32 j
= (pr
->ui
.segment_start
+ i
) % k_max_ui_segments
;
539 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
541 shader_routeui_uOffset( (v4f
){ cx
*k_bar_scale_x
, base
,
542 k_bar_scale_x
, height
} );
544 world_routes_ui_draw_segment( segment
);
545 cx
+= segment
->length
;
547 main_block_size
+= segment
->length
;
550 pr
->ui
.xpos
= vg_lerpf( pr
->ui
.xpos
, -main_block_size
* 0.5f
, 0.03f
);
554 * Will scan the whole run for two things;
555 * 1: we set a new record for the total, complete loop around the course
556 * 2: the time of each segment will be recorded into the data buffer
557 * (not implemented: TODO)
559 static void world_routes_verify_run( u32 route
)
561 struct subworld_routes
*r
= subworld_routes();
562 struct route
*pr
= &r
->routes
[route
];
565 u32 si
= world_routes_get_path( r
->routes
[route
].start
, stack
);
568 * we only care about gates that ref gates, so shuffle down the array
570 struct route_timing
*timings
[64];
571 u32 sj
= 0, maxv
= 0, begin
= 0;
572 for( u32 i
=0; i
<si
; i
++ )
574 if( r
->nodes
[stack
[i
]].special_type
== k_route_special_type_collector
)
575 timings
[sj
++] = &r
->collectors
[r
->nodes
[stack
[i
]].special_id
].timing
;
576 else if( r
->nodes
[stack
[i
]].special_type
== k_route_special_type_gate
)
577 timings
[sj
++] = &r
->gates
[r
->nodes
[stack
[i
]].special_id
].timing
;
580 for( u32 i
=0; i
<sj
; i
++ )
582 if( timings
[i
]->version
> maxv
)
584 maxv
= timings
[i
]->version
;
589 vg_info( "== begin verification (%u) ==\n", route
);
590 vg_info( " current version: %u\n", r
->current_run_version
);
593 if( timings
[begin
]->version
== r
->current_run_version
)
596 int valid_segment_count
= 0;
598 double lap_time
= 0.0;
600 for( u32 i
=0; i
<sj
; i
++ )
602 u32 j
= (sj
+begin
-i
-1) % sj
,
609 /* j1v should equal jv+1 */
610 if( timings
[j1
]->version
== timings
[j
]->version
+1 )
612 diff
= timings
[j1
]->time
- timings
[j
]->time
;
615 if( verified
&& diff
> 0.0 ) valid_segment_count
++;
622 vg_success( " [ %u %f ] %f\n", timings
[j1
]->time
,
623 timings
[j1
]->version
, diff
);
625 vg_warn( " [ %u %f ]\n", timings
[j1
]->time
, timings
[j1
]->version
);
628 pr
->ui
.fade_start
= pr
->ui
.segment_start
;
629 pr
->ui
.fade_count
= 0;
630 pr
->ui
.fade_timer_start
= vg_time
;
632 int orig_seg_count
= pr
->ui
.segment_count
;
634 world_routes_ui_newseg( route
);
638 vg_success( " NEW LAP TIME: %f\n", lap_time
);
639 world_routes_ui_popfirst(route
);
640 pr
->ui
.fade_count
++;
643 vg_info( " ctime: %f\n", lap_time
);
645 /* remove any excess we had from previous runs */
646 int to_remove
= orig_seg_count
-valid_segment_count
;
647 for( int i
=0; i
<to_remove
; i
++ )
649 world_routes_ui_popfirst(route
);
650 pr
->ui
.fade_count
++;
653 r
->routes
[route
].latest_pass
= vg_time
;
657 * When going through a gate this is called for bookkeeping purposes
659 static void world_routes_activate_gate( u32 id
)
661 struct subworld_routes
*r
= subworld_routes();
662 struct route_gate
*rg
= &r
->gates
[id
];
663 struct route_node
*pnode
= &r
->nodes
[rg
->node_id
],
664 *pdest
= &r
->nodes
[pnode
->next
[0]];
666 struct route_collector
*rc
= &r
->collectors
[ pdest
->special_id
];
669 rg
->timing
.version
= r
->current_run_version
;
670 rg
->timing
.time
= vg_time
;
671 for( u32 i
=0; i
<r
->route_count
; i
++ )
673 struct route
*route
= &r
->routes
[i
];
675 int was_active
= route
->active
;
678 for( u32 j
=0; j
<pdest
->ref_count
; j
++ )
680 if( pdest
->route_ids
[j
] == i
)
682 world_routes_verify_run( i
);
688 if( was_active
&& !route
->active
)
690 route
->ui
.fade_start
= route
->ui
.segment_start
;
691 route
->ui
.fade_count
= route
->ui
.segment_count
;
692 route
->ui
.fade_timer_start
= vg_time
;
693 world_routes_ui_clear(i
);
695 vg_success( "CLEARING -> %u %u \n", route
->ui
.fade_start
,
696 route
->ui
.fade_count
);
700 r
->current_run_version
++;
702 rc
->timing
.version
= r
->current_run_version
;
703 rc
->timing
.time
= vg_time
;
704 r
->current_run_version
++;
708 * Notify the UI system that we've reset the player
710 static void world_routes_notify_reset(void)
712 struct subworld_routes
*r
= subworld_routes();
714 for( int i
=0; i
<r
->route_count
; i
++ )
716 struct route
*route
= &r
->routes
[i
];
719 world_routes_ui_notch( i
, vg_time
- route
->latest_pass
);
723 static void world_routes_debug(void)
725 struct subworld_routes
*r
= subworld_routes();
727 for( int i
=0; i
<r
->node_count
; i
++ )
729 struct route_node
*rn
= &r
->nodes
[i
];
730 vg_line_pt3( rn
->co
, 1.0f
, rn
->special_type
? 0xffffff00: 0xff00b2ff );
733 for( int i
=0; i
<r
->route_count
; i
++ )
735 struct route
*route
= &r
->routes
[i
];
738 u32 si
= world_routes_get_path( route
->start
, stack
);
740 u32 colours
[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
743 u32 cc
= colours
[i
%vg_list_size(colours
)];
745 for( int sj
=0; sj
<si
; sj
++ )
748 debug_sbpath( &r
->nodes
[stack
[sj
]], &r
->nodes
[stack
[sk
]], cc
,
753 for( int i
=0; i
<r
->node_count
; i
++ )
755 struct route_node
*ri
= &r
->nodes
[i
],
758 for( int j
=0; j
<2; j
++ )
760 if( ri
->next
[j
] != 0xffffffff )
762 rj
= &r
->nodes
[ri
->next
[j
]];
763 vg_line( ri
->co
, rj
->co
, 0x20ffffff );
769 static void world_routes_free(void)
771 struct subworld_routes
*r
= subworld_routes();
778 static void world_id_fixup( u32
*uid
, mdl_header
*mdl
)
781 *uid
= mdl_node_from_id( mdl
, *uid
)->sub_uid
;
787 * Create the strips of colour that run through the world along course paths
789 static void world_routes_gen_meshes(void)
791 struct subworld_routes
*r
= subworld_routes();
792 scene_init( &r
->scene_lines
);
794 for( int i
=0; i
<r
->route_count
; i
++ )
796 struct route
*route
= &r
->routes
[i
];
799 u32 si
= world_routes_get_path( route
->start
, stack
);
803 for( int sj
=0; sj
<si
; sj
++ )
807 struct route_node
*rnj
= &r
->nodes
[ stack
[sj
] ],
808 *rnk
= &r
->nodes
[ stack
[sk
] ],
811 if( rnj
->special_type
&& rnk
->special_type
)
817 float base_x0
= (float)rnj
->ref_count
*-0.5f
+ (float)rnj
->current_refs
,
818 base_x1
= (float)rnk
->ref_count
*-0.5f
+ (float)rnk
->current_refs
;
820 if( rnk
->special_type
)
822 rnl
= &r
->nodes
[ rnk
->next
[0] ];
823 base_x1
= (float)rnl
->ref_count
*-0.5f
+ (float)rnl
->current_refs
;
831 v3f p0
, h0
, p1
, h1
, p
, pd
;
833 v3_copy( rnj
->co
, p0
);
834 v3_muladds( rnj
->co
, rnj
->h
, 1.0f
, h0
);
835 v3_copy( rnk
->co
, p1
);
836 v3_muladds( rnk
->co
, rnk
->h
, -1.0f
, h1
);
841 for( int it
=0; it
<256; it
++ )
843 float const k_sample_dist
= 0.02f
;
844 eval_bezier_time( p0
,p1
,h0
,h1
, t
,p
);
845 eval_bezier_time( p0
,p1
,h0
,h1
, t
+k_sample_dist
,pd
);
847 float mod
= k_sample_dist
/ v3_dist( p
, pd
);
850 v3_muls( rnj
->up
, 1.0f
-t
, up
);
851 v3_muladds( up
, rnk
->up
, t
, up
);
854 v3_cross( up
, v0
, right
);
855 v3_normalize( right
);
857 float cur_x
= (1.0f
-t
)*base_x0
+ t
*base_x1
;
859 v3f sc
, sa
, sb
, down
;
860 v3_muladds( p
, right
, cur_x
, sc
);
861 v3_muladds( sc
, up
, 1.5f
, sc
);
862 v3_muladds( sc
, right
, 0.45f
, sa
);
863 v3_muladds( sc
, right
, -0.45f
, sb
);
864 v3_muls( up
, -1.0f
, down
);
869 if(ray_world( sa
, down
, &ha
) &&
870 ray_world( sb
, down
, &hb
))
874 v3_muladds( ha
.pos
, up
, 0.06f
, va
.co
);
875 v3_muladds( hb
.pos
, up
, 0.06f
, vb
.co
);
876 v3_copy( up
, va
.norm
);
877 v3_copy( up
, vb
.norm
);
878 v3_zero( va
.colour
);
879 v3_zero( vb
.colour
);
883 scene_push_vert( &r
->scene_lines
, &va
);
884 scene_push_vert( &r
->scene_lines
, &vb
);
888 /* Connect them with triangles */
889 scene_push_tri( &r
->scene_lines
, (u32
[3]){
890 last_valid
+0-2, last_valid
+1-2, last_valid
+2-2} );
891 scene_push_tri( &r
->scene_lines
, (u32
[3]){
892 last_valid
+1-2, last_valid
+3-2, last_valid
+2-2} );
895 last_valid
= r
->scene_lines
.vertex_count
;
904 /* TODO special case for end of loop, need to add triangles
905 * between first and last rungs */
910 rnj
->current_refs
++;
913 scene_copy_slice( &r
->scene_lines
, &route
->sm
);
916 scene_upload( &r
->scene_lines
);
917 scene_free_offline_buffers( &r
->scene_lines
);
920 static void world_routes_update(void)
922 struct subworld_routes
*r
= subworld_routes();
924 for( int i
=0; i
<r
->route_count
; i
++ )
926 struct route
*route
= &r
->routes
[i
];
927 route
->factive
= vg_lerpf( route
->factive
, route
->active
, 0.01f
);
931 world_routes_ui_updatetime( i
, vg_time
- route
->latest_pass
);
936 static void bind_terrain_textures(void);
937 static void render_world_routes( m4x4f projection
, v3f camera
)
939 struct subworld_routes
*r
= subworld_routes();
941 m4x3f identity_matrix
;
942 m4x3_identity( identity_matrix
);
945 shader_route_uTexGarbage(0);
946 shader_link_standard_ub( _shader_route
.id
, 2 );
947 bind_terrain_textures();
949 shader_route_uPv( projection
);
950 shader_route_uMdl( identity_matrix
);
951 shader_route_uCamera( camera
);
953 scene_bind( &r
->scene_lines
);
955 for( int i
=0; i
<r
->route_count
; i
++ )
957 struct route
*route
= &r
->routes
[i
];
960 v3_lerp( (v3f
){0.7f
,0.7f
,0.7f
}, route
->colour
, route
->factive
, colour
);
963 shader_route_uColour( colour
);
964 mdl_draw_submesh( &route
->sm
);
968 static void render_world_routes_ui(void)
971 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
972 glBlendEquation(GL_FUNC_ADD
);
974 struct subworld_routes
*r
= subworld_routes();
976 float active_offset
= 0.0f
;
977 for( int i
=0; i
<r
->route_count
; i
++ )
979 struct route
*route
= &r
->routes
[i
];
980 world_routes_ui_draw( i
, route
->colour
, active_offset
);
981 active_offset
+= route
->factive
;
987 static void world_routes_register(void)
989 struct subworld_routes
*r
= subworld_routes();
990 r
->current_run_version
= 2;
992 shader_route_register();
993 shader_routeui_register();
996 static void world_routes_loadfrom( mdl_header
*mdl
)
998 struct subworld_routes
*r
= subworld_routes();
1009 for( int i
=0; i
<mdl
->node_count
; i
++ )
1011 mdl_node
*pnode
= mdl_node_from_id(mdl
,i
);
1014 if( pnode
->classtype
== k_classtype_route_node
||
1015 pnode
->classtype
== k_classtype_gate
)
1017 mdl_node_transform( pnode
, transform
);
1018 pnode
->sub_uid
= r
->node_count
;
1020 r
->nodes
= buffer_reserve( r
->nodes
, r
->node_count
, &r
->node_cap
, 1,
1021 sizeof( struct route_node
) );
1023 struct route_node
*rn
= &r
->nodes
[r
->node_count
];
1025 v3_copy( transform
[0], rn
->right
);
1026 v3_normalize( rn
->right
);
1027 v3_copy( transform
[1], rn
->up
);
1028 v3_normalize( rn
->up
);
1029 v3_muls( transform
[2], -1.0f
, rn
->h
);
1030 v3_copy( transform
[3], rn
->co
);
1032 rn
->current_refs
= 0;
1033 rn
->special_type
= 0;
1036 if( pnode
->classtype
== k_classtype_gate
)
1038 struct classtype_gate
*inf
= mdl_get_entdata( mdl
, pnode
);
1040 /* H is later scaled based on link distance */
1041 v3_normalize( rn
->h
);
1042 rn
->next
[0] = inf
->target
;
1048 mdl_node
*pother
= mdl_node_from_id( mdl
, inf
->target
);
1050 if( pother
->classtype
== k_classtype_gate
)
1052 r
->gates
= buffer_reserve( r
->gates
, r
->gate_count
,
1054 1, sizeof( struct route_gate
) );
1056 struct route_gate
*rg
= &r
->gates
[r
->gate_count
];
1057 rg
->node_id
= r
->node_count
;
1058 rg
->timing
.time
= 0.0;
1059 rg
->timing
.version
= 0;
1061 v3_copy( pnode
->co
, rg
->gate
.co
[0] );
1062 v3_copy( pother
->co
, rg
->gate
.co
[1] );
1063 v4_copy( pnode
->q
, rg
->gate
.q
[0] );
1064 v4_copy( pother
->q
, rg
->gate
.q
[1] );
1065 v2_copy( inf
->dims
, rg
->gate
.dims
);
1067 gate_transform_update( &rg
->gate
);
1068 rn
->special_type
= k_route_special_type_gate
;
1069 rn
->special_id
= r
->gate_count
;
1075 if( rn
->special_type
== 0 )
1077 r
->collectors
= buffer_reserve(
1078 r
->collectors
, r
->collector_count
, &r
->collector_cap
,
1079 1, sizeof( struct route_collector
));
1081 struct route_collector
*rc
= &r
->collectors
[r
->collector_count
];
1082 rc
->timing
.time
= 0.0;
1083 rc
->timing
.version
= 0;
1085 rn
->special_type
= k_route_special_type_collector
;
1086 rn
->special_id
= r
->collector_count
;
1088 r
->collector_count
++;
1093 struct classtype_route_node
*inf
= mdl_get_entdata( mdl
, pnode
);
1094 rn
->next
[0] = inf
->target
;
1095 rn
->next
[1] = inf
->target1
;
1100 else if( pnode
->classtype
== k_classtype_route
)
1102 struct classtype_route
*inf
= mdl_get_entdata( mdl
, pnode
);
1103 r
->routes
= buffer_reserve( r
->routes
, r
->route_count
, &r
->route_cap
,
1104 1, sizeof( struct route
) );
1106 struct route
*route
= &r
->routes
[r
->route_count
];
1108 v3_copy( inf
->colour
, route
->colour
);
1109 route
->colour
[3] = 1.0f
;
1112 route
->start
= inf
->id_start
;
1114 route
->factive
= 0.0f
;
1117 glGenVertexArrays( 1, &route
->ui
.vao
);
1118 glGenBuffers( 1, &route
->ui
.vbo
);
1119 glGenBuffers( 1, &route
->ui
.ebo
);
1120 glBindVertexArray( route
->ui
.vao
);
1122 size_t stride
= sizeof(v2f
);
1124 glBindBuffer( GL_ARRAY_BUFFER
, route
->ui
.vbo
);
1125 glBufferData( GL_ARRAY_BUFFER
, k_route_ui_max_verts
*stride
,
1126 NULL
, GL_DYNAMIC_DRAW
);
1127 glBindVertexArray( route
->ui
.vao
);
1128 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, route
->ui
.ebo
);
1129 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
1130 k_route_ui_max_indices
*sizeof(u16
), NULL
,
1133 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, stride
, (void *)0 );
1134 glEnableVertexAttribArray( 0 );
1137 route
->ui
.indices_head
= k_route_ui_max_indices
- 9;
1138 route
->ui
.vertex_head
= k_route_ui_max_verts
- 200;
1139 route
->ui
.segment_start
= 0;
1140 route
->ui
.segment_count
= 0;
1141 route
->ui
.last_notch
= 0.0;
1142 route
->ui
.fade_start
= 0;
1143 route
->ui
.fade_count
= 0;
1144 route
->ui
.fade_timer_start
= 0.0;
1151 * Apply correct system-local ids
1153 for( int i
=0; i
<r
->node_count
; i
++ )
1155 struct route_node
*rn
= &r
->nodes
[i
];
1157 for( int j
=0; j
<2; j
++ )
1158 world_id_fixup( &rn
->next
[j
], mdl
);
1161 for( int i
=0; i
<r
->route_count
; i
++ )
1163 struct route
*route
= &r
->routes
[i
];
1164 world_id_fixup( &route
->start
, mdl
);
1170 for( int i
=0; i
<r
->route_count
; i
++ )
1172 struct route
*route
= &r
->routes
[i
];
1175 u32 si
= world_routes_get_path( route
->start
, stack
);
1177 for( int sj
=0; sj
<si
; sj
++ )
1179 struct route_node
*rn
= &r
->nodes
[ stack
[sj
] ];
1180 rn
->route_ids
[ rn
->ref_count
++ ] = i
;
1182 if( rn
->ref_count
> 4 )
1183 vg_warn( "Too many references on route node %i\n", i
);
1187 world_routes_gen_meshes();
1190 #endif /* ROUTES_H */