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
;
85 u32 version
; /* Incremented on every teleport */
92 struct route_collector
94 struct route_timing timing
;
109 static struct subworld_routes
*subworld_routes(void);
111 static void debug_sbpath( struct route_node
*rna
, struct route_node
*rnb
,
112 u32 colour
, float xoffset
)
114 v3f p0
, h0
, p1
, h1
, l
, p
;
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
);
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
);
128 for( int i
=0; i
<5; i
++ )
130 float t
= (float)(i
+1)/5.0f
;
131 eval_bezier_time( p0
, p1
, h0
, h1
, t
, p
);
132 vg_line( p
, l
, colour
);
138 * Get a list of node ids in stack, and return how many there is
140 static u32
world_routes_get_path( u32 starter
, u32 stack
[64] )
142 struct subworld_routes
*r
= subworld_routes();
149 int loop_complete
= 0;
153 if( stack_i
[si
-1] == 2 )
159 struct route_node
*rn
= &r
->nodes
[stack
[si
-1]];
160 u32 nextid
= rn
->next
[stack_i
[si
-1]];
163 if( nextid
!= 0xffffffff )
165 if( nextid
== stack
[0] )
172 for( int sj
=0; sj
<si
; sj
++ )
174 if( stack
[sj
] == nextid
)
198 * Free a segment from the UI bar to be reused later
200 static void world_routes_ui_popfirst( u32 route
)
202 struct subworld_routes
*r
= subworld_routes();
203 struct route
*pr
= &r
->routes
[route
];
205 if( pr
->ui
.segment_count
)
207 pr
->ui
.segment_start
++;
209 if( pr
->ui
.segment_start
== 32 )
210 pr
->ui
.segment_start
= 0;
212 pr
->ui
.segment_count
--;
217 * Reset ui bar completely
219 static void world_routes_ui_clear( u32 route
)
221 struct subworld_routes
*r
= subworld_routes();
222 struct route
*pr
= &r
->routes
[route
];
223 pr
->ui
.segment_start
= (pr
->ui
.segment_start
+ pr
->ui
.segment_count
) %
225 pr
->ui
.segment_count
= 0;
229 * Break a index range into two pieces over the edge of the maximum it can
230 * store. s1 is 0 always, so its a ring buffer.
232 static void world_routes_ui_split_indices( u32 s0
, u32 count
, u32
*c0
, u32
*c1
)
234 *c0
= (VG_MIN( s0
+count
, k_route_ui_max_indices
)) - s0
;
239 * Place a set of indices into gpu array automatically splits
242 static void world_routes_ui_set_indices( struct route
*pr
,
243 u16
*indices
, u32 count
)
246 world_routes_ui_split_indices( pr
->ui
.indices_head
, count
, &c0
, &c1
);
248 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, pr
->ui
.ebo
);
252 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, pr
->ui
.indices_head
*sizeof(u16
),
253 c0
*sizeof(u16
), indices
);
258 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, 0, c1
*sizeof(u16
), indices
+c0
);
259 pr
->ui
.indices_head
= c1
;
262 pr
->ui
.indices_head
+= c0
;
266 * Place a set of vertices into gpu array
268 static u32
world_routes_ui_set_verts( struct route
*pr
, v2f
*verts
, u32 count
)
270 if( pr
->ui
.vertex_head
+ count
>= k_route_ui_max_verts
)
271 pr
->ui
.vertex_head
= 0;
273 u32 vert_start
= pr
->ui
.vertex_head
;
274 pr
->ui
.vertex_head
+= count
;
276 glBindBuffer( GL_ARRAY_BUFFER
, pr
->ui
.vbo
);
277 glBufferSubData( GL_ARRAY_BUFFER
, (GLintptr
)(vert_start
*sizeof(v2f
)),
278 sizeof(v2f
)*count
, verts
);
284 * Update the last (count) vertices positions, does not add any.
285 * Data must already be written to, and not cross either array boundaries.
287 static u32
world_routes_ui_update_verts( struct route
*pr
,
288 v2f
*verts
, u32 count
)
290 u32 vert_start
= pr
->ui
.vertex_head
-count
;
292 glBindBuffer( GL_ARRAY_BUFFER
, pr
->ui
.vbo
);
293 glBufferSubData( GL_ARRAY_BUFFER
, (GLintptr
)(vert_start
*sizeof(v2f
)),
294 sizeof(v2f
)*count
, verts
);
300 * Current/active segment of this UI bar
302 static struct route_ui_segment
*world_routes_ui_curseg( struct route
*pr
)
304 u32 index
= (pr
->ui
.segment_start
+pr
->ui
.segment_count
-1)%k_max_ui_segments
;
305 return &pr
->ui
.segments
[ index
];
309 * Start a new segment in the UI bar, will create a split on the last one if
310 * there is one active currently. (api)
312 static void world_routes_ui_newseg( u32 route
)
314 struct subworld_routes
*r
= subworld_routes();
315 struct route
*pr
= &r
->routes
[route
];
317 pr
->ui
.last_notch
= 0.0;
319 glBindVertexArray( pr
->ui
.vao
);
320 if( pr
->ui
.segment_count
)
322 float const k_gap_width
= 1.0f
;
324 struct route_ui_segment
*cseg
= world_routes_ui_curseg(pr
);
327 verts
[0][0] = cseg
->length
-k_gap_width
;
329 verts
[1][0] = cseg
->length
-k_gap_width
;
332 world_routes_ui_update_verts( pr
, verts
, 2 );
335 pr
->ui
.segment_count
++;
336 struct route_ui_segment
*segment
= world_routes_ui_curseg(pr
);
348 u32 vert_start
= world_routes_ui_set_verts( pr
, verts
, 4 );
351 indices
[0] = vert_start
+ 0;
352 indices
[1] = vert_start
+ 1;
353 indices
[2] = vert_start
+ 3;
354 indices
[3] = vert_start
+ 0;
355 indices
[4] = vert_start
+ 3;
356 indices
[5] = vert_start
+ 2;
358 segment
->vertex_start
= vert_start
;
359 segment
->vertex_count
= 4;
360 segment
->index_start
= pr
->ui
.indices_head
;
361 segment
->index_count
= 6;
363 world_routes_ui_set_indices( pr
, indices
, 6 );
367 * Extend the end of the bar
369 static void world_routes_ui_updatetime( u32 route
, float time
)
371 struct subworld_routes
*r
= subworld_routes();
372 struct route
*pr
= &r
->routes
[route
];
380 u32 vert_start
= pr
->ui
.vertex_head
-2;
382 glBindVertexArray( pr
->ui
.vao
);
383 world_routes_ui_update_verts( pr
, verts
, 2 );
385 struct route_ui_segment
*cseg
= world_routes_ui_curseg(pr
);
390 * Create a notch in the bar, used when a reset is triggered by the user
392 static void world_routes_ui_notch( u32 route
, float time
)
394 struct subworld_routes
*r
= subworld_routes();
395 struct route
*pr
= &r
->routes
[route
];
397 if( (time
- pr
->ui
.last_notch
) > 1.0 )
401 float const k_notch_width
= 1.0f
;
403 float xa
= time
-k_notch_width
,
404 xb
= time
-k_notch_width
* 0.5f
,
415 verts
[3][1] = -0.25f
;
427 glBindVertexArray( pr
->ui
.vao
);
428 u32 vert_start_mod
= world_routes_ui_update_verts( pr
, verts
, 2 ),
429 vert_start_new
= world_routes_ui_set_verts( pr
, verts
+2, 6 );
432 indices
[ 0] = vert_start_mod
+1;
433 indices
[ 1] = vert_start_new
+0;
434 indices
[ 2] = vert_start_mod
+0;
435 indices
[ 3] = vert_start_mod
+1;
436 indices
[ 4] = vert_start_new
+1;
437 indices
[ 5] = vert_start_new
+0;
439 indices
[ 6] = vert_start_new
+0;
440 indices
[ 7] = vert_start_new
+1;
441 indices
[ 8] = vert_start_new
+3;
442 indices
[ 9] = vert_start_new
+0;
443 indices
[10] = vert_start_new
+3;
444 indices
[11] = vert_start_new
+2;
446 indices
[12] = vert_start_new
+3;
447 indices
[13] = vert_start_new
+4;
448 indices
[14] = vert_start_new
+2;
449 indices
[15] = vert_start_new
+3;
450 indices
[16] = vert_start_new
+5;
451 indices
[17] = vert_start_new
+4;
453 world_routes_ui_set_indices( pr
, indices
, 18 );
455 pr
->ui
.last_notch
= time
;
457 struct route_ui_segment
*segment
= world_routes_ui_curseg(pr
);
458 segment
->vertex_count
+= 6;
459 segment
->index_count
+= 18;
463 static void world_routes_ui_draw( u32 route
, v4f colour
, float offset
)
465 struct subworld_routes
*r
= subworld_routes();
466 struct route
*pr
= &r
->routes
[route
];
470 shader_routeui_use();
471 glBindVertexArray( pr
->ui
.vao
);
473 float fade_amt
= vg_time
- pr
->ui
.fade_timer_start
;
474 fade_amt
= vg_clampf( fade_amt
/ 1.0f
, 0.0f
, 1.0f
);
476 float fade_block_size
= 0.0f
;
477 for( u32 i
=0; i
<pr
->ui
.fade_count
; i
++ )
479 u32 j
= (pr
->ui
.fade_start
+ i
) % k_max_ui_segments
;
480 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
482 fade_block_size
+= segment
->length
;
485 cx
-= fade_block_size
* fade_amt
;
488 v4_copy( colour
, fade_colour
);
489 fade_colour
[3] *= 1.0f
-fade_amt
;
491 float height
= pr
->factive
*0.1f
;
493 shader_routeui_uColour( fade_colour
);
494 for( u32 i
=0; i
<pr
->ui
.fade_count
; i
++ )
496 u32 j
= (pr
->ui
.fade_start
+ i
) % k_max_ui_segments
;
497 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
500 world_routes_ui_split_indices( segment
->index_start
,
501 segment
->index_count
, &c0
, &c1
);
503 shader_routeui_uOffset( (v4f
){ cx
, offset
, 0.005f
, height
} );
505 glDrawElements( GL_TRIANGLES
, c0
, GL_UNSIGNED_SHORT
,
506 (void *)(segment
->index_start
*sizeof(u16
)));
508 glDrawElements( GL_TRIANGLES
, c1
, GL_UNSIGNED_SHORT
, (void *)(0) );
510 cx
+= segment
->length
;
513 shader_routeui_uColour( colour
);
514 for( u32 i
=0; i
<pr
->ui
.segment_count
; i
++ )
516 u32 j
= (pr
->ui
.segment_start
+ i
) % k_max_ui_segments
;
517 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
520 world_routes_ui_split_indices( segment
->index_start
,
521 segment
->index_count
, &c0
, &c1
);
523 shader_routeui_uOffset( (v4f
){ cx
, offset
, 0.005f
, height
} );
525 glDrawElements( GL_TRIANGLES
, c0
, GL_UNSIGNED_SHORT
,
526 (void *)(segment
->index_start
*sizeof(u16
)));
528 glDrawElements( GL_TRIANGLES
, c1
, GL_UNSIGNED_SHORT
, (void *)(0) );
530 cx
+= segment
->length
;
535 * Will scan the whole run for two things;
536 * 1: we set a new record for the total, complete loop around the course
537 * 2: the time of each segment will be recorded into the data buffer
538 * (not implemented: TODO)
540 static void world_routes_verify_run( u32 route
)
542 struct subworld_routes
*r
= subworld_routes();
543 struct route
*pr
= &r
->routes
[route
];
546 u32 si
= world_routes_get_path( r
->routes
[route
].start
, stack
);
549 * we only care about gates that ref gates, so shuffle down the array
551 struct route_timing
*timings
[64];
552 u32 sj
= 0, maxv
= 0, begin
= 0;
553 for( u32 i
=0; i
<si
; i
++ )
555 if( r
->nodes
[stack
[i
]].special_type
== k_route_special_type_collector
)
556 timings
[sj
++] = &r
->collectors
[r
->nodes
[stack
[i
]].special_id
].timing
;
557 else if( r
->nodes
[stack
[i
]].special_type
== k_route_special_type_gate
)
558 timings
[sj
++] = &r
->gates
[r
->nodes
[stack
[i
]].special_id
].timing
;
561 for( u32 i
=0; i
<sj
; i
++ )
563 if( timings
[i
]->version
> maxv
)
565 maxv
= timings
[i
]->version
;
570 vg_info( "== begin verification (%u) ==\n", route
);
571 vg_info( " current version: %u\n", r
->current_run_version
);
574 if( timings
[begin
]->version
== r
->current_run_version
)
577 int valid_segment_count
= 0;
579 double lap_time
= 0.0;
581 for( u32 i
=0; i
<sj
; i
++ )
583 u32 j
= (sj
+begin
-i
-1) % sj
,
590 /* j1v should equal jv+1 */
591 if( timings
[j1
]->version
== timings
[j
]->version
+1 )
593 diff
= timings
[j1
]->time
- timings
[j
]->time
;
596 if( verified
&& diff
> 0.0 ) valid_segment_count
++;
603 vg_success( " [ %u %f ] %f\n", timings
[j1
]->time
,
604 timings
[j1
]->version
, diff
);
606 vg_warn( " [ %u %f ]\n", timings
[j1
]->time
, timings
[j1
]->version
);
609 pr
->ui
.fade_start
= pr
->ui
.segment_start
;
610 pr
->ui
.fade_count
= 0;
611 pr
->ui
.fade_timer_start
= vg_time
;
613 int orig_seg_count
= pr
->ui
.segment_count
;
615 world_routes_ui_newseg( route
);
619 vg_success( " NEW LAP TIME: %f\n", lap_time
);
620 world_routes_ui_popfirst(route
);
621 pr
->ui
.fade_count
++;
624 vg_info( " ctime: %f\n", lap_time
);
626 /* remove any excess we had from previous runs */
627 int to_remove
= orig_seg_count
-valid_segment_count
;
628 for( int i
=0; i
<to_remove
; i
++ )
630 world_routes_ui_popfirst(route
);
631 pr
->ui
.fade_count
++;
634 r
->routes
[route
].latest_pass
= vg_time
;
638 * When going through a gate this is called for bookkeeping purposes
640 static void world_routes_activate_gate( u32 id
)
642 struct subworld_routes
*r
= subworld_routes();
643 struct route_gate
*rg
= &r
->gates
[id
];
644 struct route_node
*pnode
= &r
->nodes
[rg
->node_id
],
645 *pdest
= &r
->nodes
[pnode
->next
[0]];
647 struct route_collector
*rc
= &r
->collectors
[ pdest
->special_id
];
650 rg
->timing
.version
= r
->current_run_version
;
651 rg
->timing
.time
= vg_time
;
652 for( u32 i
=0; i
<r
->route_count
; i
++ )
654 struct route
*route
= &r
->routes
[i
];
656 int was_active
= route
->active
;
659 for( u32 j
=0; j
<pdest
->ref_count
; j
++ )
661 if( pdest
->route_ids
[j
] == i
)
663 world_routes_verify_run( i
);
669 if( was_active
&& !route
->active
)
671 route
->ui
.fade_start
= route
->ui
.segment_start
;
672 route
->ui
.fade_count
= route
->ui
.segment_count
;
673 route
->ui
.fade_timer_start
= vg_time
;
674 world_routes_ui_clear(i
);
676 vg_success( "CLEARING -> %u %u \n", route
->ui
.fade_start
,
677 route
->ui
.fade_count
);
681 r
->current_run_version
++;
683 rc
->timing
.version
= r
->current_run_version
;
684 rc
->timing
.time
= vg_time
;
685 r
->current_run_version
++;
689 * Notify the UI system that we've reset the player
691 static void world_routes_notify_reset(void)
693 struct subworld_routes
*r
= subworld_routes();
695 for( int i
=0; i
<r
->route_count
; i
++ )
697 struct route
*route
= &r
->routes
[i
];
700 world_routes_ui_notch( i
, vg_time
- route
->latest_pass
);
704 static void world_routes_debug(void)
706 struct subworld_routes
*r
= subworld_routes();
708 for( int i
=0; i
<r
->node_count
; i
++ )
710 struct route_node
*rn
= &r
->nodes
[i
];
711 vg_line_pt3( rn
->co
, 1.0f
, rn
->special_type
? 0xffffff00: 0xff00b2ff );
714 for( int i
=0; i
<r
->route_count
; i
++ )
716 struct route
*route
= &r
->routes
[i
];
719 u32 si
= world_routes_get_path( route
->start
, stack
);
721 u32 colours
[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
724 u32 cc
= colours
[i
%vg_list_size(colours
)];
726 for( int sj
=0; sj
<si
; sj
++ )
729 debug_sbpath( &r
->nodes
[stack
[sj
]], &r
->nodes
[stack
[sk
]], cc
,
734 for( int i
=0; i
<r
->node_count
; i
++ )
736 struct route_node
*ri
= &r
->nodes
[i
],
739 for( int j
=0; j
<2; j
++ )
741 if( ri
->next
[j
] != 0xffffffff )
743 rj
= &r
->nodes
[ri
->next
[j
]];
744 vg_line( ri
->co
, rj
->co
, 0x20ffffff );
750 static void world_routes_free(void)
752 struct subworld_routes
*r
= subworld_routes();
759 static void world_id_fixup( u32
*uid
, mdl_header
*mdl
)
762 *uid
= mdl_node_from_id( mdl
, *uid
)->sub_uid
;
768 * Create the strips of colour that run through the world along course paths
770 static void world_routes_gen_meshes(void)
772 struct subworld_routes
*r
= subworld_routes();
773 scene_init( &r
->scene_lines
);
775 for( int i
=0; i
<r
->route_count
; i
++ )
777 struct route
*route
= &r
->routes
[i
];
780 u32 si
= world_routes_get_path( route
->start
, stack
);
784 for( int sj
=0; sj
<si
; sj
++ )
788 struct route_node
*rnj
= &r
->nodes
[ stack
[sj
] ],
789 *rnk
= &r
->nodes
[ stack
[sk
] ],
792 if( rnj
->special_type
&& rnk
->special_type
)
798 float base_x0
= (float)rnj
->ref_count
*-0.5f
+ (float)rnj
->current_refs
,
799 base_x1
= (float)rnk
->ref_count
*-0.5f
+ (float)rnk
->current_refs
;
801 if( rnk
->special_type
)
803 rnl
= &r
->nodes
[ rnk
->next
[0] ];
804 base_x1
= (float)rnl
->ref_count
*-0.5f
+ (float)rnl
->current_refs
;
812 v3f p0
, h0
, p1
, h1
, p
, pd
;
814 v3_copy( rnj
->co
, p0
);
815 v3_muladds( rnj
->co
, rnj
->h
, 1.0f
, h0
);
816 v3_copy( rnk
->co
, p1
);
817 v3_muladds( rnk
->co
, rnk
->h
, -1.0f
, h1
);
822 for( int it
=0; it
<256; it
++ )
824 float const k_sample_dist
= 0.02f
;
825 eval_bezier_time( p0
,p1
,h0
,h1
, t
,p
);
826 eval_bezier_time( p0
,p1
,h0
,h1
, t
+k_sample_dist
,pd
);
828 float mod
= k_sample_dist
/ v3_dist( p
, pd
);
831 v3_muls( rnj
->up
, 1.0f
-t
, up
);
832 v3_muladds( up
, rnk
->up
, t
, up
);
835 v3_cross( up
, v0
, right
);
836 v3_normalize( right
);
838 float cur_x
= (1.0f
-t
)*base_x0
+ t
*base_x1
;
840 v3f sc
, sa
, sb
, down
;
841 v3_muladds( p
, right
, cur_x
, sc
);
842 v3_muladds( sc
, up
, 1.5f
, sc
);
843 v3_muladds( sc
, right
, 0.45f
, sa
);
844 v3_muladds( sc
, right
, -0.45f
, sb
);
845 v3_muls( up
, -1.0f
, down
);
850 if(ray_world( sa
, down
, &ha
) &&
851 ray_world( sb
, down
, &hb
))
855 v3_muladds( ha
.pos
, up
, 0.06f
, va
.co
);
856 v3_muladds( hb
.pos
, up
, 0.06f
, vb
.co
);
857 v3_copy( up
, va
.norm
);
858 v3_copy( up
, vb
.norm
);
859 v3_zero( va
.colour
);
860 v3_zero( vb
.colour
);
864 scene_push_vert( &r
->scene_lines
, &va
);
865 scene_push_vert( &r
->scene_lines
, &vb
);
869 /* Connect them with triangles */
870 scene_push_tri( &r
->scene_lines
, (u32
[3]){
871 last_valid
+0-2, last_valid
+1-2, last_valid
+2-2} );
872 scene_push_tri( &r
->scene_lines
, (u32
[3]){
873 last_valid
+1-2, last_valid
+3-2, last_valid
+2-2} );
876 last_valid
= r
->scene_lines
.vertex_count
;
885 /* TODO special case for end of loop, need to add triangles
886 * between first and last rungs */
891 rnj
->current_refs
++;
894 scene_copy_slice( &r
->scene_lines
, &route
->sm
);
897 scene_upload( &r
->scene_lines
);
898 scene_free_offline_buffers( &r
->scene_lines
);
901 static void world_routes_update(void)
903 struct subworld_routes
*r
= subworld_routes();
905 for( int i
=0; i
<r
->route_count
; i
++ )
907 struct route
*route
= &r
->routes
[i
];
908 route
->factive
= vg_lerpf( route
->factive
, route
->active
, 0.01f
);
912 world_routes_ui_updatetime( i
, vg_time
- route
->latest_pass
);
917 static void bind_terrain_textures(void);
918 static void render_world_routes( m4x4f projection
, v3f camera
)
920 struct subworld_routes
*r
= subworld_routes();
922 m4x3f identity_matrix
;
923 m4x3_identity( identity_matrix
);
926 shader_route_uTexGarbage(0);
927 shader_link_standard_ub( _shader_route
.id
, 2 );
928 bind_terrain_textures();
930 shader_route_uPv( projection
);
931 shader_route_uMdl( identity_matrix
);
932 shader_route_uCamera( camera
);
934 scene_bind( &r
->scene_lines
);
936 for( int i
=0; i
<r
->route_count
; i
++ )
938 struct route
*route
= &r
->routes
[i
];
941 v3_lerp( (v3f
){0.7f
,0.7f
,0.7f
}, route
->colour
, route
->factive
, colour
);
944 shader_route_uColour( colour
);
945 mdl_draw_submesh( &route
->sm
);
949 static void render_world_routes_ui(void)
952 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
953 glBlendEquation(GL_FUNC_ADD
);
955 struct subworld_routes
*r
= subworld_routes();
957 float active_offset
= 0.0f
;
958 for( int i
=0; i
<r
->route_count
; i
++ )
960 struct route
*route
= &r
->routes
[i
];
961 world_routes_ui_draw( i
, route
->colour
, active_offset
*0.1f
);
962 active_offset
+= route
->factive
;
968 static void world_routes_register(void)
970 struct subworld_routes
*r
= subworld_routes();
971 r
->current_run_version
= 2;
973 shader_route_register();
974 shader_routeui_register();
977 static void world_routes_loadfrom( mdl_header
*mdl
)
979 struct subworld_routes
*r
= subworld_routes();
990 for( int i
=0; i
<mdl
->node_count
; i
++ )
992 mdl_node
*pnode
= mdl_node_from_id(mdl
,i
);
995 if( pnode
->classtype
== k_classtype_route_node
||
996 pnode
->classtype
== k_classtype_gate
)
998 mdl_node_transform( pnode
, transform
);
999 pnode
->sub_uid
= r
->node_count
;
1001 r
->nodes
= buffer_reserve( r
->nodes
, r
->node_count
, &r
->node_cap
, 1,
1002 sizeof( struct route_node
) );
1004 struct route_node
*rn
= &r
->nodes
[r
->node_count
];
1006 v3_copy( transform
[0], rn
->right
);
1007 v3_normalize( rn
->right
);
1008 v3_copy( transform
[1], rn
->up
);
1009 v3_normalize( rn
->up
);
1010 v3_muls( transform
[2], -1.0f
, rn
->h
);
1011 v3_copy( transform
[3], rn
->co
);
1013 rn
->current_refs
= 0;
1014 rn
->special_type
= 0;
1017 if( pnode
->classtype
== k_classtype_gate
)
1019 struct classtype_gate
*inf
= mdl_get_entdata( mdl
, pnode
);
1021 /* H is later scaled based on link distance */
1022 v3_normalize( rn
->h
);
1023 rn
->next
[0] = inf
->target
;
1029 mdl_node
*pother
= mdl_node_from_id( mdl
, inf
->target
);
1031 if( pother
->classtype
== k_classtype_gate
)
1033 r
->gates
= buffer_reserve( r
->gates
, r
->gate_count
,
1035 1, sizeof( struct route_gate
) );
1037 struct route_gate
*rg
= &r
->gates
[r
->gate_count
];
1038 rg
->node_id
= r
->node_count
;
1039 rg
->timing
.time
= 0.0;
1040 rg
->timing
.version
= 0;
1042 v3_copy( pnode
->co
, rg
->gate
.co
[0] );
1043 v3_copy( pother
->co
, rg
->gate
.co
[1] );
1044 v4_copy( pnode
->q
, rg
->gate
.q
[0] );
1045 v4_copy( pother
->q
, rg
->gate
.q
[1] );
1046 v2_copy( inf
->dims
, rg
->gate
.dims
);
1048 gate_transform_update( &rg
->gate
);
1049 rn
->special_type
= k_route_special_type_gate
;
1050 rn
->special_id
= r
->gate_count
;
1056 if( rn
->special_type
== 0 )
1058 r
->collectors
= buffer_reserve(
1059 r
->collectors
, r
->collector_count
, &r
->collector_cap
,
1060 1, sizeof( struct route_collector
));
1062 struct route_collector
*rc
= &r
->collectors
[r
->collector_count
];
1063 rc
->timing
.time
= 0.0;
1064 rc
->timing
.version
= 0;
1066 rn
->special_type
= k_route_special_type_collector
;
1067 rn
->special_id
= r
->collector_count
;
1069 r
->collector_count
++;
1074 struct classtype_route_node
*inf
= mdl_get_entdata( mdl
, pnode
);
1075 rn
->next
[0] = inf
->target
;
1076 rn
->next
[1] = inf
->target1
;
1081 else if( pnode
->classtype
== k_classtype_route
)
1083 struct classtype_route
*inf
= mdl_get_entdata( mdl
, pnode
);
1084 r
->routes
= buffer_reserve( r
->routes
, r
->route_count
, &r
->route_cap
,
1085 1, sizeof( struct route
) );
1087 struct route
*route
= &r
->routes
[r
->route_count
];
1089 v3_copy( inf
->colour
, route
->colour
);
1090 route
->colour
[3] = 1.0f
;
1093 route
->start
= inf
->id_start
;
1095 route
->factive
= 0.0f
;
1098 glGenVertexArrays( 1, &route
->ui
.vao
);
1099 glGenBuffers( 1, &route
->ui
.vbo
);
1100 glGenBuffers( 1, &route
->ui
.ebo
);
1101 glBindVertexArray( route
->ui
.vao
);
1103 size_t stride
= sizeof(v2f
);
1105 glBindBuffer( GL_ARRAY_BUFFER
, route
->ui
.vbo
);
1106 glBufferData( GL_ARRAY_BUFFER
, k_route_ui_max_verts
*stride
,
1107 NULL
, GL_DYNAMIC_DRAW
);
1108 glBindVertexArray( route
->ui
.vao
);
1109 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, route
->ui
.ebo
);
1110 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
1111 k_route_ui_max_indices
*sizeof(u16
), NULL
,
1114 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, stride
, (void *)0 );
1115 glEnableVertexAttribArray( 0 );
1118 route
->ui
.indices_head
= k_route_ui_max_indices
- 9;
1119 route
->ui
.vertex_head
= k_route_ui_max_verts
- 200;
1120 route
->ui
.segment_start
= 0;
1121 route
->ui
.segment_count
= 0;
1122 route
->ui
.last_notch
= 0.0;
1123 route
->ui
.fade_start
= 0;
1124 route
->ui
.fade_count
= 0;
1125 route
->ui
.fade_timer_start
= 0.0;
1132 * Apply correct system-local ids
1134 for( int i
=0; i
<r
->node_count
; i
++ )
1136 struct route_node
*rn
= &r
->nodes
[i
];
1138 for( int j
=0; j
<2; j
++ )
1139 world_id_fixup( &rn
->next
[j
], mdl
);
1142 for( int i
=0; i
<r
->route_count
; i
++ )
1144 struct route
*route
= &r
->routes
[i
];
1145 world_id_fixup( &route
->start
, mdl
);
1151 for( int i
=0; i
<r
->route_count
; i
++ )
1153 struct route
*route
= &r
->routes
[i
];
1156 u32 si
= world_routes_get_path( route
->start
, stack
);
1158 for( int sj
=0; sj
<si
; sj
++ )
1160 struct route_node
*rn
= &r
->nodes
[ stack
[sj
] ];
1161 rn
->route_ids
[ rn
->ref_count
++ ] = i
;
1163 if( rn
->ref_count
> 4 )
1164 vg_warn( "Too many references on route node %i\n", i
);
1168 world_routes_gen_meshes();
1171 #endif /* ROUTES_H */