5 #include "world_info.h"
7 #include "shaders/vblend.h"
8 #include "shaders/route.h"
9 #include "shaders/routeui.h"
11 enum route_special_type
13 k_route_special_type_gate
= 1,
14 k_route_special_type_collector
= 2
17 static void world_routes_interact(void)
19 world
.routes
.last_interaction
= vg_time
;
22 static void debug_sbpath( struct route_node
*rna
, struct route_node
*rnb
,
23 u32 colour
, float xoffset
)
25 v3f p0
, h0
, p1
, h1
, l
, p
;
27 v3_copy( rna
->co
, p0
);
28 v3_muladds( rna
->co
, rna
->h
, 1.0f
, h0
);
29 v3_copy( rnb
->co
, p1
);
30 v3_muladds( rnb
->co
, rnb
->h
, -1.0f
, h1
);
32 v3_muladds( p0
, rna
->right
, xoffset
, p0
);
33 v3_muladds( h0
, rna
->right
, xoffset
, h0
);
34 v3_muladds( p1
, rnb
->right
, xoffset
, p1
);
35 v3_muladds( h1
, rnb
->right
, xoffset
, h1
);
39 for( int i
=0; i
<5; i
++ )
41 float t
= (float)(i
+1)/5.0f
;
42 eval_bezier_time( p0
, p1
, h0
, h1
, t
, p
);
43 vg_line( p
, l
, colour
);
49 * Get a list of node ids in stack, and return how many there is
51 static u32
world_routes_get_path( u32 starter
, u32 stack
[64] )
53 struct subworld_routes
*r
= &world
.routes
;
60 int loop_complete
= 0;
64 if( stack_i
[si
-1] == 2 )
70 struct route_node
*rn
= &r
->nodes
[stack
[si
-1]];
71 u32 nextid
= rn
->next
[stack_i
[si
-1]];
74 if( nextid
!= 0xffffffff )
76 if( nextid
== stack
[0] )
83 for( int sj
=0; sj
<si
; sj
++ )
85 if( stack
[sj
] == nextid
)
109 * Free a segment from the UI bar to be reused later
111 static void world_routes_ui_popfirst( u32 route
)
113 struct subworld_routes
*r
= &world
.routes
;
114 struct route
*pr
= &r
->routes
[route
];
116 if( pr
->ui
.segment_count
)
118 pr
->ui
.segment_start
++;
120 if( pr
->ui
.segment_start
== 32 )
121 pr
->ui
.segment_start
= 0;
123 pr
->ui
.segment_count
--;
128 * Reset ui bar completely
130 static void world_routes_ui_clear( u32 route
)
132 struct subworld_routes
*r
= &world
.routes
;
133 struct route
*pr
= &r
->routes
[route
];
134 pr
->ui
.segment_start
= (pr
->ui
.segment_start
+ pr
->ui
.segment_count
) %
136 pr
->ui
.segment_count
= 0;
140 * Break a index range into two pieces over the edge of the maximum it can
141 * store. s1 is 0 always, so its a ring buffer.
143 static void world_routes_ui_split_indices( u32 s0
, u32 count
, u32
*c0
, u32
*c1
)
145 *c0
= (VG_MIN( s0
+count
, k_route_ui_max_indices
)) - s0
;
150 * Place a set of indices into gpu array automatically splits
153 static void world_routes_ui_set_indices( struct route
*pr
,
154 u16
*indices
, u32 count
)
157 world_routes_ui_split_indices( pr
->ui
.indices_head
, count
, &c0
, &c1
);
159 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, pr
->ui
.ebo
);
163 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, pr
->ui
.indices_head
*sizeof(u16
),
164 c0
*sizeof(u16
), indices
);
169 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, 0, c1
*sizeof(u16
), indices
+c0
);
170 pr
->ui
.indices_head
= c1
;
173 pr
->ui
.indices_head
+= c0
;
177 * Place a set of vertices into gpu array
179 static u32
world_routes_ui_set_verts( struct route
*pr
, v2f
*verts
, u32 count
)
181 if( pr
->ui
.vertex_head
+ count
>= k_route_ui_max_verts
)
182 pr
->ui
.vertex_head
= 0;
184 u32 vert_start
= pr
->ui
.vertex_head
;
185 pr
->ui
.vertex_head
+= count
;
187 glBindBuffer( GL_ARRAY_BUFFER
, pr
->ui
.vbo
);
188 glBufferSubData( GL_ARRAY_BUFFER
, (GLintptr
)(vert_start
*sizeof(v2f
)),
189 sizeof(v2f
)*count
, verts
);
195 * Update the last (count) vertices positions, does not add any.
196 * Data must already be written to, and not cross either array boundaries.
198 static u32
world_routes_ui_update_verts( struct route
*pr
,
199 v2f
*verts
, u32 count
)
201 u32 vert_start
= pr
->ui
.vertex_head
-count
;
203 glBindBuffer( GL_ARRAY_BUFFER
, pr
->ui
.vbo
);
204 glBufferSubData( GL_ARRAY_BUFFER
, (GLintptr
)(vert_start
*sizeof(v2f
)),
205 sizeof(v2f
)*count
, verts
);
211 * Current/active segment of this UI bar
213 static struct route_ui_segment
*world_routes_ui_curseg( struct route
*pr
)
215 u32 index
= (pr
->ui
.segment_start
+pr
->ui
.segment_count
-1)%k_max_ui_segments
;
216 return &pr
->ui
.segments
[ index
];
220 * Start a new segment in the UI bar, will create a split on the last one if
221 * there is one active currently. (api)
223 static void world_routes_ui_newseg( u32 route
)
225 struct subworld_routes
*r
= &world
.routes
;
226 struct route
*pr
= &r
->routes
[route
];
228 pr
->ui
.last_notch
= 0.0;
230 glBindVertexArray( pr
->ui
.vao
);
231 if( pr
->ui
.segment_count
)
233 float const k_gap_width
= 1.0f
;
235 struct route_ui_segment
*cseg
= world_routes_ui_curseg(pr
);
238 verts
[0][0] = cseg
->length
-k_gap_width
;
240 verts
[1][0] = cseg
->length
-k_gap_width
;
243 world_routes_ui_update_verts( pr
, verts
, 2 );
246 pr
->ui
.segment_count
++;
247 struct route_ui_segment
*segment
= world_routes_ui_curseg(pr
);
259 u32 vert_start
= world_routes_ui_set_verts( pr
, verts
, 4 );
262 indices
[0] = vert_start
+ 0;
263 indices
[1] = vert_start
+ 1;
264 indices
[2] = vert_start
+ 3;
265 indices
[3] = vert_start
+ 0;
266 indices
[4] = vert_start
+ 3;
267 indices
[5] = vert_start
+ 2;
269 segment
->vertex_start
= vert_start
;
270 segment
->vertex_count
= 4;
271 segment
->index_start
= pr
->ui
.indices_head
;
272 segment
->index_count
= 6;
273 segment
->notches
= 0;
275 world_routes_ui_set_indices( pr
, indices
, 6 );
279 * Extend the end of the bar
281 static void world_routes_ui_updatetime( u32 route
, float time
)
283 struct subworld_routes
*r
= &world
.routes
;
284 struct route
*pr
= &r
->routes
[route
];
292 u32 vert_start
= pr
->ui
.vertex_head
-2;
294 glBindVertexArray( pr
->ui
.vao
);
295 world_routes_ui_update_verts( pr
, verts
, 2 );
297 struct route_ui_segment
*cseg
= world_routes_ui_curseg(pr
);
302 * Create a notch in the bar, used when a reset is triggered by the user
304 static void world_routes_ui_notch( u32 route
, float time
)
306 struct subworld_routes
*r
= &world
.routes
;
307 struct route
*pr
= &r
->routes
[route
];
309 if( (time
- pr
->ui
.last_notch
) > 1.0 )
311 struct route_ui_segment
*segment
= world_routes_ui_curseg(pr
);
312 if( segment
->notches
== k_max_ui_splits_per_segment
)
319 float const k_notch_width
= 1.0f
;
321 float xa
= time
-k_notch_width
,
322 xb
= time
-k_notch_width
* 0.5f
,
333 verts
[3][1] = -0.25f
;
345 glBindVertexArray( pr
->ui
.vao
);
346 u32 vert_start_mod
= world_routes_ui_update_verts( pr
, verts
, 2 ),
347 vert_start_new
= world_routes_ui_set_verts( pr
, verts
+2, 6 );
350 indices
[ 0] = vert_start_mod
+1;
351 indices
[ 1] = vert_start_new
+0;
352 indices
[ 2] = vert_start_mod
+0;
353 indices
[ 3] = vert_start_mod
+1;
354 indices
[ 4] = vert_start_new
+1;
355 indices
[ 5] = vert_start_new
+0;
357 indices
[ 6] = vert_start_new
+0;
358 indices
[ 7] = vert_start_new
+1;
359 indices
[ 8] = vert_start_new
+3;
360 indices
[ 9] = vert_start_new
+0;
361 indices
[10] = vert_start_new
+3;
362 indices
[11] = vert_start_new
+2;
364 indices
[12] = vert_start_new
+3;
365 indices
[13] = vert_start_new
+4;
366 indices
[14] = vert_start_new
+2;
367 indices
[15] = vert_start_new
+3;
368 indices
[16] = vert_start_new
+5;
369 indices
[17] = vert_start_new
+4;
371 world_routes_ui_set_indices( pr
, indices
, 18 );
373 pr
->ui
.last_notch
= time
;
375 segment
->vertex_count
+= 6;
376 segment
->index_count
+= 18;
380 static void world_routes_ui_draw_segment( struct route_ui_segment
*segment
)
383 world_routes_ui_split_indices( segment
->index_start
,
384 segment
->index_count
, &c0
, &c1
);
386 glDrawElements( GL_TRIANGLES
, c0
, GL_UNSIGNED_SHORT
,
387 (void *)(segment
->index_start
*sizeof(u16
)));
389 glDrawElements( GL_TRIANGLES
, c1
, GL_UNSIGNED_SHORT
, (void *)(0) );
393 * Draws full bar at Y offset(offset).
395 static void world_routes_ui_draw( u32 route
, v4f colour
, float offset
)
397 float const k_bar_height
= 0.05f
,
398 k_bar_scale_x
= 0.005f
;
400 struct subworld_routes
*r
= &world
.routes
;
401 struct route
*pr
= &r
->routes
[route
];
403 float cx
= pr
->ui
.xpos
;
405 shader_routeui_use();
406 glBindVertexArray( pr
->ui
.vao
);
408 float fade_amt
= vg_time
- pr
->ui
.fade_timer_start
;
409 fade_amt
= vg_clampf( fade_amt
/ 1.0f
, 0.0f
, 1.0f
);
411 float fade_block_size
= 0.0f
,
412 main_block_size
= 0.0f
;
414 for( u32 i
=0; i
<pr
->ui
.fade_count
; i
++ )
416 u32 j
= (pr
->ui
.fade_start
+ i
) % k_max_ui_segments
;
417 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
419 fade_block_size
+= segment
->length
;
422 cx
-= fade_block_size
* fade_amt
;
425 v4_copy( colour
, fade_colour
);
426 fade_colour
[3] *= 1.0f
-fade_amt
;
428 float timer_delta
= (vg_time
- world
.routes
.last_interaction
) * (1.0/60.0),
429 timer_scale
= 1.0f
- vg_minf( timer_delta
, 1.0f
);
435 float height
= pr
->factive
*k_bar_height
* timer_scale
,
436 base
= -1.0f
+ (offset
+0.5f
)*k_bar_height
* timer_scale
;
438 shader_routeui_uColour( fade_colour
);
439 for( u32 i
=0; i
<pr
->ui
.fade_count
; i
++ )
441 u32 j
= (pr
->ui
.fade_start
+ i
) % k_max_ui_segments
;
442 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
444 shader_routeui_uOffset( (v4f
){ cx
*k_bar_scale_x
, base
,
445 k_bar_scale_x
, height
} );
447 world_routes_ui_draw_segment( segment
);
448 cx
+= segment
->length
;
454 shader_routeui_uColour( colour
);
455 for( u32 i
=0; i
<pr
->ui
.segment_count
; i
++ )
457 u32 j
= (pr
->ui
.segment_start
+ i
) % k_max_ui_segments
;
458 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
460 shader_routeui_uOffset( (v4f
){ cx
*k_bar_scale_x
, base
,
461 k_bar_scale_x
, height
} );
463 world_routes_ui_draw_segment( segment
);
464 cx
+= segment
->length
;
466 main_block_size
+= segment
->length
;
469 pr
->ui
.xpos
= vg_lerpf( pr
->ui
.xpos
, -main_block_size
* 0.5f
, 0.03f
);
472 static void world_routes_local_set_record( u32 route
, double lap_time
)
474 vg_success( " NEW LAP TIME: %f\n", lap_time
);
476 struct subworld_routes
*r
= &world
.routes
;
477 struct route
*pr
= &r
->routes
[route
];
479 if( pr
->track_id
!= 0xffffffff )
481 double time_centiseconds
= lap_time
* 100.0;
482 if( time_centiseconds
> (float)0xfffe )
485 highscore_record temp
;
486 temp
.trackid
= pr
->track_id
;
487 temp
.datetime
= time(NULL
);
490 temp
.time
= time_centiseconds
;
492 highscores_push_record( &temp
);
493 track_infos
[ pr
->track_id
].push
= 1;
497 vg_warn( "There is no associated track for this record...\n" );
502 * Will scan the whole run for two things;
503 * 1: we set a new record for the total, complete loop around the course
504 * 2: the time of each segment will be recorded into the data buffer
505 * (not implemented: TODO)
507 static void world_routes_verify_run( u32 route
)
509 struct subworld_routes
*r
= &world
.routes
;
510 struct route
*pr
= &r
->routes
[route
];
513 u32 si
= world_routes_get_path( r
->routes
[route
].start
, stack
);
516 * we only care about gates that ref gates, so shuffle down the array
518 struct route_timing
*timings
[64];
519 u32 sj
= 0, maxv
= 0, begin
= 0;
520 for( u32 i
=0; i
<si
; i
++ )
522 if( r
->nodes
[stack
[i
]].special_type
== k_route_special_type_collector
)
523 timings
[sj
++] = &r
->collectors
[r
->nodes
[stack
[i
]].special_id
].timing
;
524 else if( r
->nodes
[stack
[i
]].special_type
== k_route_special_type_gate
)
525 timings
[sj
++] = &r
->gates
[r
->nodes
[stack
[i
]].special_id
].timing
;
528 for( u32 i
=0; i
<sj
; i
++ )
530 if( timings
[i
]->version
> maxv
)
532 maxv
= timings
[i
]->version
;
537 vg_info( "== begin verification (%u) ==\n", route
);
538 vg_info( " current version: %u\n", r
->current_run_version
);
541 if( timings
[begin
]->version
== r
->current_run_version
)
544 int valid_segment_count
= 0;
546 double lap_time
= 0.0;
548 for( u32 i
=0; i
<sj
; i
++ )
550 u32 j
= (sj
+begin
-i
-1) % sj
,
557 /* j1v should equal jv+1 */
558 if( timings
[j1
]->version
== timings
[j
]->version
+1 )
560 diff
= timings
[j1
]->time
- timings
[j
]->time
;
563 if( verified
&& diff
> 0.0 ) valid_segment_count
++;
570 vg_success( " [ %u %f ] %f\n", timings
[j1
]->time
,
571 timings
[j1
]->version
, diff
);
573 vg_warn( " [ %u %f ]\n", timings
[j1
]->time
, timings
[j1
]->version
);
576 pr
->ui
.fade_start
= pr
->ui
.segment_start
;
577 pr
->ui
.fade_count
= 0;
578 pr
->ui
.fade_timer_start
= vg_time
;
580 int orig_seg_count
= pr
->ui
.segment_count
;
582 world_routes_ui_newseg( route
);
586 world_routes_local_set_record( route
, lap_time
);
587 world_routes_ui_popfirst(route
);
588 pr
->ui
.fade_count
++;
591 vg_info( " ctime: %f\n", lap_time
);
593 /* remove any excess we had from previous runs */
594 int to_remove
= orig_seg_count
-valid_segment_count
;
595 for( int i
=0; i
<to_remove
; i
++ )
597 world_routes_ui_popfirst(route
);
598 pr
->ui
.fade_count
++;
601 r
->routes
[route
].latest_pass
= vg_time
;
605 * When going through a gate this is called for bookkeeping purposes
607 static void world_routes_activate_gate( u32 id
)
609 world_routes_interact();
611 struct subworld_routes
*r
= &world
.routes
;
612 struct route_gate
*rg
= &r
->gates
[id
];
613 struct route_node
*pnode
= &r
->nodes
[rg
->node_id
],
614 *pdest
= &r
->nodes
[pnode
->next
[0]];
616 struct route_collector
*rc
= &r
->collectors
[ pdest
->special_id
];
619 rg
->timing
.version
= r
->current_run_version
;
620 rg
->timing
.time
= vg_time
;
621 for( u32 i
=0; i
<r
->route_count
; i
++ )
623 struct route
*route
= &r
->routes
[i
];
625 int was_active
= route
->active
;
628 for( u32 j
=0; j
<pdest
->ref_count
; j
++ )
630 if( pdest
->route_ids
[j
] == i
)
632 world_routes_verify_run( i
);
638 if( was_active
&& !route
->active
)
640 route
->ui
.fade_start
= route
->ui
.segment_start
;
641 route
->ui
.fade_count
= route
->ui
.segment_count
;
642 route
->ui
.fade_timer_start
= vg_time
;
643 world_routes_ui_clear(i
);
645 vg_success( "CLEARING -> %u %u \n", route
->ui
.fade_start
,
646 route
->ui
.fade_count
);
650 r
->current_run_version
++;
652 rc
->timing
.version
= r
->current_run_version
;
653 rc
->timing
.time
= vg_time
;
654 r
->current_run_version
++;
658 * Notify the UI system that we've reset the player
660 static void world_routes_notify_reset(void)
662 struct subworld_routes
*r
= &world
.routes
;
663 world_routes_interact();
665 for( int i
=0; i
<r
->route_count
; i
++ )
667 struct route
*route
= &r
->routes
[i
];
670 world_routes_ui_notch( i
, vg_time
- route
->latest_pass
);
674 static void world_routes_debug(void)
676 struct subworld_routes
*r
= &world
.routes
;
678 for( int i
=0; i
<r
->node_count
; i
++ )
680 struct route_node
*rn
= &r
->nodes
[i
];
681 vg_line_pt3( rn
->co
, 1.0f
, rn
->special_type
? 0xffffff00: 0xff00b2ff );
684 for( int i
=0; i
<r
->route_count
; i
++ )
686 struct route
*route
= &r
->routes
[i
];
689 u32 si
= world_routes_get_path( route
->start
, stack
);
691 u32 colours
[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
694 u32 cc
= colours
[i
%vg_list_size(colours
)];
696 for( int sj
=0; sj
<si
; sj
++ )
699 debug_sbpath( &r
->nodes
[stack
[sj
]], &r
->nodes
[stack
[sk
]], cc
,
704 for( int i
=0; i
<r
->node_count
; i
++ )
706 struct route_node
*ri
= &r
->nodes
[i
],
709 for( int j
=0; j
<2; j
++ )
711 if( ri
->next
[j
] != 0xffffffff )
713 rj
= &r
->nodes
[ri
->next
[j
]];
714 vg_line( ri
->co
, rj
->co
, 0x20ffffff );
720 static void world_id_fixup( u32
*uid
, mdl_header
*mdl
)
723 *uid
= mdl_node_from_id( mdl
, *uid
)->sub_uid
;
729 * Create the strips of colour that run through the world along course paths
731 static void world_routes_gen_meshes(void)
733 struct subworld_routes
*r
= &world
.routes
;
734 scene_init( &r
->scene_lines
);
736 for( int i
=0; i
<r
->route_count
; i
++ )
738 struct route
*route
= &r
->routes
[i
];
741 u32 si
= world_routes_get_path( route
->start
, stack
);
745 for( int sj
=0; sj
<si
; sj
++ )
749 struct route_node
*rnj
= &r
->nodes
[ stack
[sj
] ],
750 *rnk
= &r
->nodes
[ stack
[sk
] ],
753 if( rnj
->special_type
&& rnk
->special_type
)
759 float base_x0
= (float)rnj
->ref_count
*-0.5f
+ (float)rnj
->current_refs
,
760 base_x1
= (float)rnk
->ref_count
*-0.5f
+ (float)rnk
->current_refs
;
762 if( rnk
->special_type
)
764 rnl
= &r
->nodes
[ rnk
->next
[0] ];
765 base_x1
= (float)rnl
->ref_count
*-0.5f
+ (float)rnl
->current_refs
;
773 v3f p0
, h0
, p1
, h1
, p
, pd
;
775 v3_copy( rnj
->co
, p0
);
776 v3_muladds( rnj
->co
, rnj
->h
, 1.0f
, h0
);
777 v3_copy( rnk
->co
, p1
);
778 v3_muladds( rnk
->co
, rnk
->h
, -1.0f
, h1
);
783 for( int it
=0; it
<256; it
++ )
785 float const k_sample_dist
= 0.02f
;
786 eval_bezier_time( p0
,p1
,h0
,h1
, t
,p
);
787 eval_bezier_time( p0
,p1
,h0
,h1
, t
+k_sample_dist
,pd
);
789 float mod
= k_sample_dist
/ v3_dist( p
, pd
);
792 v3_muls( rnj
->up
, 1.0f
-t
, up
);
793 v3_muladds( up
, rnk
->up
, t
, up
);
796 v3_cross( up
, v0
, right
);
797 v3_normalize( right
);
799 float cur_x
= (1.0f
-t
)*base_x0
+ t
*base_x1
;
801 v3f sc
, sa
, sb
, down
;
802 v3_muladds( p
, right
, cur_x
, sc
);
803 v3_muladds( sc
, up
, 1.5f
, sc
);
804 v3_muladds( sc
, right
, 0.45f
, sa
);
805 v3_muladds( sc
, right
, -0.45f
, sb
);
806 v3_muls( up
, -1.0f
, down
);
811 if(ray_world( sa
, down
, &ha
) &&
812 ray_world( sb
, down
, &hb
))
816 v3_muladds( ha
.pos
, up
, 0.06f
, va
.co
);
817 v3_muladds( hb
.pos
, up
, 0.06f
, vb
.co
);
818 v3_copy( up
, va
.norm
);
819 v3_copy( up
, vb
.norm
);
823 scene_push_vert( &r
->scene_lines
, &va
);
824 scene_push_vert( &r
->scene_lines
, &vb
);
828 /* Connect them with triangles */
829 scene_push_tri( &r
->scene_lines
, (u32
[3]){
830 last_valid
+0-2, last_valid
+1-2, last_valid
+2-2} );
831 scene_push_tri( &r
->scene_lines
, (u32
[3]){
832 last_valid
+1-2, last_valid
+3-2, last_valid
+2-2} );
835 last_valid
= r
->scene_lines
.vertex_count
;
844 /* TODO special case for end of loop, need to add triangles
845 * between first and last rungs */
850 rnj
->current_refs
++;
853 scene_copy_slice( &r
->scene_lines
, &route
->sm
);
856 scene_upload( &r
->scene_lines
);
857 scene_free_offline_buffers( &r
->scene_lines
);
861 static void world_routes_loadfrom( mdl_header
*mdl
)
863 struct subworld_routes
*r
= &world
.routes
;
874 for( int i
=0; i
<mdl
->node_count
; i
++ )
876 mdl_node
*pnode
= mdl_node_from_id(mdl
,i
);
879 if( pnode
->classtype
== k_classtype_route_node
||
880 pnode
->classtype
== k_classtype_gate
)
882 mdl_node_transform( pnode
, transform
);
883 pnode
->sub_uid
= r
->node_count
;
885 r
->nodes
= buffer_reserve( r
->nodes
, r
->node_count
, &r
->node_cap
, 1,
886 sizeof( struct route_node
) );
888 struct route_node
*rn
= &r
->nodes
[r
->node_count
];
890 v3_copy( transform
[0], rn
->right
);
891 v3_normalize( rn
->right
);
892 v3_copy( transform
[1], rn
->up
);
893 v3_normalize( rn
->up
);
894 v3_muls( transform
[2], -1.0f
, rn
->h
);
895 v3_copy( transform
[3], rn
->co
);
897 rn
->current_refs
= 0;
898 rn
->special_type
= 0;
901 if( pnode
->classtype
== k_classtype_gate
)
903 struct classtype_gate
*inf
= mdl_get_entdata( mdl
, pnode
);
905 /* H is later scaled based on link distance */
906 v3_normalize( rn
->h
);
907 rn
->next
[0] = inf
->target
;
913 mdl_node
*pother
= mdl_node_from_id( mdl
, inf
->target
);
915 if( pother
->classtype
== k_classtype_gate
)
917 r
->gates
= buffer_reserve( r
->gates
, r
->gate_count
,
919 1, sizeof( struct route_gate
) );
921 struct route_gate
*rg
= &r
->gates
[r
->gate_count
];
922 rg
->node_id
= r
->node_count
;
923 rg
->timing
.time
= 0.0;
924 rg
->timing
.version
= 0;
926 v3_copy( pnode
->co
, rg
->gate
.co
[0] );
927 v3_copy( pother
->co
, rg
->gate
.co
[1] );
928 v4_copy( pnode
->q
, rg
->gate
.q
[0] );
929 v4_copy( pother
->q
, rg
->gate
.q
[1] );
930 v2_copy( inf
->dims
, rg
->gate
.dims
);
932 gate_transform_update( &rg
->gate
);
933 rn
->special_type
= k_route_special_type_gate
;
934 rn
->special_id
= r
->gate_count
;
940 if( rn
->special_type
== 0 )
942 r
->collectors
= buffer_reserve(
943 r
->collectors
, r
->collector_count
, &r
->collector_cap
,
944 1, sizeof( struct route_collector
));
946 struct route_collector
*rc
= &r
->collectors
[r
->collector_count
];
947 rc
->timing
.time
= 0.0;
948 rc
->timing
.version
= 0;
950 rn
->special_type
= k_route_special_type_collector
;
951 rn
->special_id
= r
->collector_count
;
953 r
->collector_count
++;
958 struct classtype_route_node
*inf
= mdl_get_entdata( mdl
, pnode
);
959 rn
->next
[0] = inf
->target
;
960 rn
->next
[1] = inf
->target1
;
965 else if( pnode
->classtype
== k_classtype_route
)
967 struct classtype_route
*inf
= mdl_get_entdata( mdl
, pnode
);
968 r
->routes
= buffer_reserve( r
->routes
, r
->route_count
, &r
->route_cap
,
969 1, sizeof( struct route
) );
971 struct route
*route
= &r
->routes
[r
->route_count
];
973 v3_copy( inf
->colour
, route
->colour
);
974 route
->colour
[3] = 1.0f
;
977 route
->track_id
= 0xffffffff;
978 for( u32 j
=0; j
<vg_list_size(track_infos
); j
++ )
980 if( !strcmp( mdl_pstr(mdl
,pnode
->pstr_name
), track_infos
[j
].name
))
987 route
->start
= inf
->id_start
;
989 route
->factive
= 0.0f
;
990 mdl_node_transform( pnode
, route
->scoreboard_transform
);
993 glGenVertexArrays( 1, &route
->ui
.vao
);
994 glGenBuffers( 1, &route
->ui
.vbo
);
995 glGenBuffers( 1, &route
->ui
.ebo
);
996 glBindVertexArray( route
->ui
.vao
);
998 size_t stride
= sizeof(v2f
);
1000 glBindBuffer( GL_ARRAY_BUFFER
, route
->ui
.vbo
);
1001 glBufferData( GL_ARRAY_BUFFER
, k_route_ui_max_verts
*stride
,
1002 NULL
, GL_DYNAMIC_DRAW
);
1003 glBindVertexArray( route
->ui
.vao
);
1004 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, route
->ui
.ebo
);
1005 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
1006 k_route_ui_max_indices
*sizeof(u16
), NULL
,
1009 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, stride
, (void *)0 );
1010 glEnableVertexAttribArray( 0 );
1013 route
->ui
.indices_head
= k_route_ui_max_indices
- 9;
1014 route
->ui
.vertex_head
= k_route_ui_max_verts
- 200;
1015 route
->ui
.segment_start
= 0;
1016 route
->ui
.segment_count
= 0;
1017 route
->ui
.last_notch
= 0.0;
1018 route
->ui
.fade_start
= 0;
1019 route
->ui
.fade_count
= 0;
1020 route
->ui
.fade_timer_start
= 0.0;
1027 * Apply correct system-local ids
1029 for( int i
=0; i
<r
->node_count
; i
++ )
1031 struct route_node
*rn
= &r
->nodes
[i
];
1033 for( int j
=0; j
<2; j
++ )
1034 world_id_fixup( &rn
->next
[j
], mdl
);
1037 for( int i
=0; i
<r
->route_count
; i
++ )
1039 struct route
*route
= &r
->routes
[i
];
1040 world_id_fixup( &route
->start
, mdl
);
1046 for( int i
=0; i
<r
->route_count
; i
++ )
1048 struct route
*route
= &r
->routes
[i
];
1051 u32 si
= world_routes_get_path( route
->start
, stack
);
1053 for( int sj
=0; sj
<si
; sj
++ )
1055 struct route_node
*rn
= &r
->nodes
[ stack
[sj
] ];
1056 rn
->route_ids
[ rn
->ref_count
++ ] = i
;
1058 if( rn
->ref_count
> 4 )
1059 vg_warn( "Too many references on route node %i\n", i
);
1063 world_routes_gen_meshes();
1067 * -----------------------------------------------------------------------------
1069 * -----------------------------------------------------------------------------
1072 static void world_routes_register(void)
1074 struct subworld_routes
*r
= &world
.routes
;
1075 r
->current_run_version
= 2;
1077 shader_route_register();
1078 shader_routeui_register();
1081 static void world_routes_free(void)
1083 struct subworld_routes
*r
= &world
.routes
;
1090 static void world_routes_update(void)
1092 struct subworld_routes
*r
= &world
.routes
;
1094 for( int i
=0; i
<r
->route_count
; i
++ )
1096 struct route
*route
= &r
->routes
[i
];
1097 route
->factive
= vg_lerpf( route
->factive
, route
->active
, 0.01f
);
1101 world_routes_ui_updatetime( i
, vg_time
- route
->latest_pass
);
1106 static void bind_terrain_textures(void);
1107 static void render_world_routes( m4x4f projection
, v3f camera
)
1109 struct subworld_routes
*r
= &world
.routes
;
1111 m4x3f identity_matrix
;
1112 m4x3_identity( identity_matrix
);
1115 shader_route_uTexGarbage(0);
1116 shader_link_standard_ub( _shader_route
.id
, 2 );
1117 bind_terrain_textures();
1119 shader_route_uPv( projection
);
1120 shader_route_uMdl( identity_matrix
);
1121 shader_route_uCamera( camera
);
1123 scene_bind( &r
->scene_lines
);
1125 for( int i
=0; i
<r
->route_count
; i
++ )
1127 struct route
*route
= &r
->routes
[i
];
1130 v3_lerp( (v3f
){0.7f
,0.7f
,0.7f
}, route
->colour
, route
->factive
, colour
);
1133 shader_route_uColour( colour
);
1134 mdl_draw_submesh( &route
->sm
);
1138 static void render_world_routes_ui(void)
1141 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1142 glBlendEquation(GL_FUNC_ADD
);
1144 struct subworld_routes
*r
= &world
.routes
;
1146 float active_offset
= 0.0f
;
1147 for( int i
=0; i
<r
->route_count
; i
++ )
1149 struct route
*route
= &r
->routes
[i
];
1150 world_routes_ui_draw( i
, route
->colour
, active_offset
);
1151 active_offset
+= route
->factive
;
1154 glDisable(GL_BLEND
);
1157 #endif /* ROUTES_H */