2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 #include "world_gate.h"
12 #include "shaders/vblend.h"
13 #include "shaders/route.h"
14 #include "shaders/routeui.h"
17 enum route_special_type
19 k_route_special_type_none
= 0,
20 k_route_special_type_gate
= 1,
21 k_route_special_type_collector
= 2
24 VG_STATIC
void debug_sbpath( struct route_node
*rna
, struct route_node
*rnb
,
25 u32 colour
, float xoffset
)
27 v3f p0
, h0
, p1
, h1
, l
, p
;
29 v3_copy( rna
->co
, p0
);
30 v3_muladds( rna
->co
, rna
->h
, 1.0f
, h0
);
31 v3_copy( rnb
->co
, p1
);
32 v3_muladds( rnb
->co
, rnb
->h
, -1.0f
, h1
);
34 v3_muladds( p0
, rna
->right
, xoffset
, p0
);
35 v3_muladds( h0
, rna
->right
, xoffset
, h0
);
36 v3_muladds( p1
, rnb
->right
, xoffset
, p1
);
37 v3_muladds( h1
, rnb
->right
, xoffset
, h1
);
41 for( int i
=0; i
<5; i
++ )
43 float t
= (float)(i
+1)/5.0f
;
44 eval_bezier_time( p0
, p1
, h0
, h1
, t
, p
);
45 vg_line( p
, l
, colour
);
51 * Get a list of node ids in stack, and return how many there is
53 VG_STATIC u32
world_routes_get_path( u32 starter
, u32 stack
[64] )
61 int loop_complete
= 0;
65 if( stack_i
[si
-1] == 2 )
71 struct route_node
*rn
= &world
.nodes
[stack
[si
-1]];
72 u32 nextid
= rn
->next
[stack_i
[si
-1]];
75 if( nextid
!= 0xffffffff )
77 if( nextid
== stack
[0] )
84 for( int sj
=0; sj
<si
; sj
++ )
86 if( stack
[sj
] == nextid
)
110 * Free a segment from the UI bar to be reused later
112 VG_STATIC
void world_routes_ui_popfirst( struct route_ui_bar
*pui
)
114 if( pui
->segment_count
)
116 pui
->segment_start
++;
118 if( pui
->segment_start
== 32 )
119 pui
->segment_start
= 0;
121 pui
->segment_count
--;
126 * Reset ui bar completely
128 VG_STATIC
void world_routes_ui_clear( struct route_ui_bar
*pui
)
130 pui
->segment_start
= (pui
->segment_start
+ pui
->segment_count
) %
132 pui
->segment_count
= 0;
136 * Break a index range into two pieces over the edge of the maximum it can
137 * store. s1 is 0 always, so its a ring buffer.
139 VG_STATIC
void world_routes_ui_split_indices( u32 s0
, u32 count
,
142 *c0
= (VG_MIN( s0
+count
, k_route_ui_max_indices
)) - s0
;
147 * Place a set of indices into gpu array automatically splits
150 VG_STATIC
void world_routes_ui_set_indices( struct route_ui_bar
*pui
,
151 u16
*indices
, u32 count
)
154 world_routes_ui_split_indices( pui
->indices_head
, count
, &c0
, &c1
);
156 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, pui
->ebo
);
160 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, pui
->indices_head
*sizeof(u16
),
161 c0
*sizeof(u16
), indices
);
166 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, 0, c1
*sizeof(u16
), indices
+c0
);
167 pui
->indices_head
= c1
;
170 pui
->indices_head
+= c0
;
174 * Place a set of vertices into gpu array
176 VG_STATIC u32
world_routes_ui_set_verts( struct route_ui_bar
*pui
,
177 v2f
*verts
, u32 count
)
179 if( pui
->vertex_head
+ count
>= k_route_ui_max_verts
)
180 pui
->vertex_head
= 0;
182 u32 vert_start
= pui
->vertex_head
;
183 pui
->vertex_head
+= count
;
185 glBindBuffer( GL_ARRAY_BUFFER
, pui
->vbo
);
186 glBufferSubData( GL_ARRAY_BUFFER
, (GLintptr
)(vert_start
*sizeof(v2f
)),
187 sizeof(v2f
)*count
, verts
);
193 * Update the last (count) vertices positions, does not add any.
194 * Data must already be written to, and not cross either array boundaries.
196 VG_STATIC u32
world_routes_ui_update_verts( struct route_ui_bar
*pui
,
197 v2f
*verts
, u32 count
)
199 u32 vert_start
= pui
->vertex_head
-count
;
201 glBindBuffer( GL_ARRAY_BUFFER
, pui
->vbo
);
202 glBufferSubData( GL_ARRAY_BUFFER
, (GLintptr
)(vert_start
*sizeof(v2f
)),
203 sizeof(v2f
)*count
, verts
);
209 * Current/active segment of this UI bar
211 VG_STATIC
struct route_ui_segment
*world_routes_ui_curseg(
212 struct route_ui_bar
*pui
)
214 u32 index
= (pui
->segment_start
+pui
->segment_count
-1)%k_max_ui_segments
;
215 return &pui
->segments
[ index
];
219 * Start a new segment in the UI bar, will create a split on the last one if
220 * there is one active currently. (api)
222 VG_STATIC
void world_routes_ui_newseg( u32 route
)
224 struct route_ui_bar
*pui
= &world
.ui_bars
[route
];
226 glBindVertexArray( pui
->vao
);
227 if( pui
->segment_count
)
229 float const k_gap_width
= 1.0f
;
231 struct route_ui_segment
*cseg
= world_routes_ui_curseg( pui
);
234 verts
[0][0] = cseg
->length
-k_gap_width
;
236 verts
[1][0] = cseg
->length
-k_gap_width
;
239 world_routes_ui_update_verts( pui
, verts
, 2 );
242 pui
->segment_count
++;
243 struct route_ui_segment
*segment
= world_routes_ui_curseg( pui
);
255 u32 vert_start
= world_routes_ui_set_verts( pui
, verts
, 4 );
258 indices
[0] = vert_start
+ 0;
259 indices
[1] = vert_start
+ 1;
260 indices
[2] = vert_start
+ 3;
261 indices
[3] = vert_start
+ 0;
262 indices
[4] = vert_start
+ 3;
263 indices
[5] = vert_start
+ 2;
265 segment
->vertex_start
= vert_start
;
266 segment
->vertex_count
= 4;
267 segment
->index_start
= pui
->indices_head
;
268 segment
->index_count
= 6;
269 segment
->notches
= 0;
271 world_routes_ui_set_indices( pui
, indices
, 6 );
275 * Extend the end of the bar
277 VG_STATIC
void world_routes_ui_updatetime( u32 route
, float time
)
279 struct route_ui_bar
*pui
= &world
.ui_bars
[route
];
287 u32 vert_start
= pui
->vertex_head
-2;
289 glBindVertexArray( pui
->vao
);
290 world_routes_ui_update_verts( pui
, verts
, 2 );
292 struct route_ui_segment
*cseg
= world_routes_ui_curseg( pui
);
296 VG_STATIC
void world_routes_ui_draw_segment( struct route_ui_segment
*segment
)
299 world_routes_ui_split_indices( segment
->index_start
,
300 segment
->index_count
, &c0
, &c1
);
302 glDrawElements( GL_TRIANGLES
, c0
, GL_UNSIGNED_SHORT
,
303 (void *)(segment
->index_start
*sizeof(u16
)));
305 glDrawElements( GL_TRIANGLES
, c1
, GL_UNSIGNED_SHORT
, (void *)(0) );
309 * Draws full bar at Y offset(offset).
311 VG_STATIC
void world_routes_ui_draw( u32 route
, v4f colour
, float offset
)
313 float const k_bar_height
= 0.05f
,
314 k_bar_scale_x
= 0.005f
;
316 struct route
*pr
= &world
.routes
[route
];
317 struct route_ui_bar
*pui
= &world
.ui_bars
[route
];
319 float cx
= pui
->xpos
;
321 shader_routeui_use();
322 glBindVertexArray( pui
->vao
);
324 float fade_amt
= world
.time
- pui
->fade_timer_start
;
325 fade_amt
= vg_clampf( fade_amt
/ 1.0f
, 0.0f
, 1.0f
);
327 float fade_block_size
= 0.0f
,
328 main_block_size
= 0.0f
;
330 for( u32 i
=0; i
<pui
->fade_count
; i
++ )
332 u32 j
= (pui
->fade_start
+ i
) % k_max_ui_segments
;
333 struct route_ui_segment
*segment
= &pui
->segments
[j
];
335 fade_block_size
+= segment
->length
;
338 cx
-= fade_block_size
* fade_amt
;
341 v4_copy( colour
, fade_colour
);
342 fade_colour
[3] *= 1.0f
-fade_amt
;
345 float timer_delta
= (world
.time
- world
.last_use
) * (1.0/45.0),
346 timer_scale
= 1.0f
- vg_minf( timer_delta
, 1.0f
);
352 float height
= pr
->factive
*k_bar_height
* timer_scale
,
353 base
= -1.0f
+ (offset
+0.5f
)*k_bar_height
* timer_scale
;
355 shader_routeui_uColour( fade_colour
);
356 for( u32 i
=0; i
<pui
->fade_count
; i
++ )
358 u32 j
= (pui
->fade_start
+ i
) % k_max_ui_segments
;
359 struct route_ui_segment
*segment
= &pui
->segments
[j
];
361 shader_routeui_uOffset( (v4f
){ cx
*k_bar_scale_x
, base
,
362 k_bar_scale_x
, height
} );
364 world_routes_ui_draw_segment( segment
);
365 cx
+= segment
->length
;
371 shader_routeui_uColour( colour
);
372 for( u32 i
=0; i
<pui
->segment_count
; i
++ )
374 u32 j
= (pui
->segment_start
+ i
) % k_max_ui_segments
;
375 struct route_ui_segment
*segment
= &pui
->segments
[j
];
377 shader_routeui_uOffset( (v4f
){ cx
*k_bar_scale_x
, base
,
378 k_bar_scale_x
, height
} );
380 world_routes_ui_draw_segment( segment
);
381 cx
+= segment
->length
;
383 main_block_size
+= segment
->length
;
386 pui
->xpos
= vg_lerpf( pui
->xpos
, -main_block_size
* 0.5f
, 0.03f
);
389 VG_STATIC
void world_routes_local_set_record( u32 route
, double lap_time
)
391 vg_success( " NEW LAP TIME: %f\n", lap_time
);
393 struct route
*pr
= &world
.routes
[route
];
395 if( pr
->track_id
!= 0xffffffff )
397 double time_centiseconds
= lap_time
* 100.0;
398 if( time_centiseconds
> (float)0xfffe )
401 highscore_record temp
;
402 temp
.trackid
= pr
->track_id
;
403 temp
.datetime
= time(NULL
);
406 temp
.time
= time_centiseconds
;
408 highscores_push_record( &temp
);
410 struct track_info
*pti
= &track_infos
[ pr
->track_id
];
413 if( pti
->achievement_id
)
415 steam_set_achievement( pti
->achievement_id
);
416 steam_store_achievements();
421 vg_warn( "There is no associated track for this record...\n" );
426 * Will scan the whole run for two things;
427 * 1: we set a new record for the total, complete loop around the course
428 * 2: the time of each segment will be recorded into the data buffer
429 * (not implemented: TODO)
431 VG_STATIC
void world_routes_verify_run( u32 route
)
433 struct route
*pr
= &world
.routes
[route
];
434 struct route_ui_bar
*pui
= &world
.ui_bars
[route
];
437 u32 si
= world_routes_get_path( world
.routes
[route
].start
, stack
);
440 * we only care about gates that ref gates, so shuffle down the array
442 struct route_timing
*timings
[64];
443 u32 sj
= 0, maxv
= 0, begin
= 0;
444 for( u32 i
=0; i
<si
; i
++ )
446 struct route_node
*inode
= &world
.nodes
[stack
[i
]];
448 if( inode
->special_type
== k_route_special_type_collector
)
450 timings
[sj
++] = &world
.collectors
[ inode
->special_id
].timing
;
452 else if( inode
->special_type
== k_route_special_type_gate
)
454 timings
[sj
++] = &world
.gates
[inode
->special_id
].timing
;
458 for( u32 i
=0; i
<sj
; i
++ )
460 if( timings
[i
]->version
> maxv
)
462 maxv
= timings
[i
]->version
;
467 vg_info( "== begin verification (%u) ==\n", route
);
468 vg_info( " current version: %u\n", world
.current_run_version
);
471 if( timings
[begin
]->version
== world
.current_run_version
)
474 int valid_segment_count
= 0;
476 double lap_time
= 0.0;
478 for( u32 i
=0; i
<sj
; i
++ )
480 u32 j
= (sj
+begin
-i
-1) % sj
,
487 /* j1v should equal jv+1 */
488 if( timings
[j1
]->version
== timings
[j
]->version
+1 )
490 diff
= timings
[j1
]->time
- timings
[j
]->time
;
493 if( verified
&& diff
> 0.0 ) valid_segment_count
++;
500 vg_success( " [ %u %f ] %f\n", timings
[j1
]->time
,
501 timings
[j1
]->version
, diff
);
503 vg_warn( " [ %u %f ]\n", timings
[j1
]->time
, timings
[j1
]->version
);
506 pui
->fade_start
= pui
->segment_start
;
508 pui
->fade_timer_start
= world
.time
;
510 int orig_seg_count
= pui
->segment_count
;
512 world_routes_ui_newseg( route
);
516 world_routes_local_set_record( route
, lap_time
);
517 world_routes_ui_popfirst( pui
);
521 vg_info( " ctime: %f\n", lap_time
);
523 /* remove any excess we had from previous runs */
524 int to_remove
= orig_seg_count
-valid_segment_count
;
525 for( int i
=0; i
<to_remove
; i
++ )
527 world_routes_ui_popfirst( pui
);
531 world
.routes
[route
].latest_pass
= world
.time
;
534 VG_STATIC
void world_routes_clear(void)
536 for( u32 i
=0; i
<world
.route_count
; i
++ )
538 struct route
*route
= &world
.routes
[i
];
541 world
.current_run_version
+= 4;
542 world
.last_use
= 0.0;
546 * When going through a gate this is called for bookkeeping purposes
548 VG_STATIC
void world_routes_activate_gate( u32 id
)
550 struct route_gate
*rg
= &world
.gates
[id
];
551 struct route_node
*pnode
= &world
.nodes
[rg
->node_id
],
552 *pdest
= &world
.nodes
[pnode
->next
[0]];
554 world
.last_use
= world
.time
;
556 struct route_collector
*rc
= &world
.collectors
[ pdest
->special_id
];
558 world
.active_gate
= id
;
559 rg
->timing
.version
= world
.current_run_version
;
560 rg
->timing
.time
= world
.time
;
562 for( u32 i
=0; i
<world
.route_count
; i
++ )
564 struct route
*route
= &world
.routes
[i
];
566 int was_active
= route
->active
;
569 for( u32 j
=0; j
<pdest
->ref_count
; j
++ )
571 if( pdest
->route_ids
[j
] == i
)
573 world_routes_verify_run( i
);
579 if( was_active
&& !route
->active
)
581 struct route_ui_bar
*pui
= &world
.ui_bars
[i
];
582 pui
->fade_start
= pui
->segment_start
;
583 pui
->fade_count
= pui
->segment_count
;
584 pui
->fade_timer_start
= world
.time
;
586 world_routes_ui_clear( pui
);
587 vg_success( "CLEARING -> %u %u \n", pui
->fade_start
,
592 world
.current_run_version
++;
594 rc
->timing
.version
= world
.current_run_version
;
595 rc
->timing
.time
= world
.time
;
596 world
.current_run_version
++;
600 * Notify the UI system that we've reset the player
602 VG_STATIC
void world_routes_notify_reset(void)
604 world
.rewind_from
= world
.time
;
605 world
.rewind_to
= world
.last_use
;
608 for( int i
=0; i
<r
->route_count
; i
++ )
610 struct route
*route
= &r
->routes
[i
];
613 world_routes_ui_notch( i
, r
->time
- route
->latest_pass
);
618 /* Rewind between the saved points in time */
619 VG_STATIC
void world_routes_rollback_time( double t
)
621 world
.time
= vg_lerp( world
.rewind_to
, world
.rewind_from
, t
);
624 /* draw lines along the paths */
625 VG_STATIC
void world_routes_debug(void)
627 for( int i
=0; i
<world
.node_count
; i
++ )
629 struct route_node
*rn
= &world
.nodes
[i
];
630 vg_line_pt3( rn
->co
, 1.0f
, rn
->special_type
? 0xffffff00: 0xff00b2ff );
633 for( int i
=0; i
<world
.route_count
; i
++ )
635 struct route
*route
= &world
.routes
[i
];
638 u32 si
= world_routes_get_path( route
->start
, stack
);
640 u32 colours
[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
643 u32 cc
= colours
[i
%vg_list_size(colours
)];
645 for( int sj
=0; sj
<si
; sj
++ )
649 struct route_node
*pj
= &world
.nodes
[stack
[sj
]],
650 *pk
= &world
.nodes
[stack
[sk
]];
651 debug_sbpath( pj
, pk
, cc
, (float)i
);
655 for( int i
=0; i
<world
.node_count
; i
++ )
657 struct route_node
*ri
= &world
.nodes
[i
],
660 for( int j
=0; j
<2; j
++ )
662 if( ri
->next
[j
] != 0xffffffff )
664 rj
= &world
.nodes
[ri
->next
[j
]];
665 vg_line( ri
->co
, rj
->co
, 0x20ffffff );
671 VG_STATIC
void world_routes_create_mesh( u32 route_id
)
673 struct route
*route
= &world
.routes
[ route_id
];
676 u32 si
= world_routes_get_path( route
->start
, stack
);
680 for( int sj
=0; sj
<si
; sj
++ )
684 struct route_node
*rnj
= &world
.nodes
[ stack
[sj
] ],
685 *rnk
= &world
.nodes
[ stack
[sk
] ],
688 if( rnj
->special_type
&& rnk
->special_type
)
694 float base_x0
= (float)rnj
->ref_count
*-0.5f
+ (float)rnj
->current_refs
,
695 base_x1
= (float)rnk
->ref_count
*-0.5f
+ (float)rnk
->current_refs
;
697 if( rnk
->special_type
)
699 rnl
= &world
.nodes
[ rnk
->next
[0] ];
700 base_x1
= (float)rnl
->ref_count
*-0.5f
+ (float)rnl
->current_refs
;
708 v3f p0
, h0
, p1
, h1
, p
, pd
;
710 v3_copy( rnj
->co
, p0
);
711 v3_muladds( rnj
->co
, rnj
->h
, 1.0f
, h0
);
712 v3_copy( rnk
->co
, p1
);
713 v3_muladds( rnk
->co
, rnk
->h
, -1.0f
, h1
);
718 for( int it
=0; it
<256; it
++ )
720 float const k_sample_dist
= 0.02f
;
721 eval_bezier_time( p0
,p1
,h0
,h1
, t
,p
);
722 eval_bezier_time( p0
,p1
,h0
,h1
, t
+k_sample_dist
,pd
);
724 float mod
= k_sample_dist
/ v3_dist( p
, pd
);
727 v3_muls( rnj
->up
, 1.0f
-t
, up
);
728 v3_muladds( up
, rnk
->up
, t
, up
);
731 v3_cross( up
, v0
, right
);
732 v3_normalize( right
);
734 float cur_x
= (1.0f
-t
)*base_x0
+ t
*base_x1
;
736 v3f sc
, sa
, sb
, down
;
737 v3_muladds( p
, right
, cur_x
, sc
);
738 v3_muladds( sc
, up
, 1.5f
, sc
);
739 v3_muladds( sc
, right
, 0.45f
, sa
);
740 v3_muladds( sc
, right
, -0.45f
, sb
);
741 v3_muls( up
, -1.0f
, down
);
746 if( ray_world( sa
, down
, &ha
) &&
747 ray_world( sb
, down
, &hb
))
751 v3_muladds( ha
.pos
, up
, 0.06f
, va
.co
);
752 v3_muladds( hb
.pos
, up
, 0.06f
, vb
.co
);
753 v3_copy( up
, va
.norm
);
754 v3_copy( up
, vb
.norm
);
758 scene_push_vert( world
.scene_lines
, &va
);
759 scene_push_vert( world
.scene_lines
, &vb
);
763 /* Connect them with triangles */
764 scene_push_tri( world
.scene_lines
, (u32
[3]){
765 last_valid
+0-2, last_valid
+1-2, last_valid
+2-2} );
766 scene_push_tri( world
.scene_lines
, (u32
[3]){
767 last_valid
+1-2, last_valid
+3-2, last_valid
+2-2} );
770 last_valid
= world
.scene_lines
->vertex_count
;
779 /* TODO special case for end of loop, need to add triangles
780 * between first and last rungs */
785 rnj
->current_refs
++;
788 scene_copy_slice( world
.scene_lines
, &route
->sm
);
792 * Create the strips of colour that run through the world along course paths
794 VG_STATIC
void world_routes_generate(void)
796 vg_info( "Generating route meshes\n" );
797 world
.scene_lines
= scene_init( world
.dynamic_vgl
, 200000, 300000 );
799 for( u32 i
=0; i
<world
.route_count
; i
++ )
800 world_routes_create_mesh( i
);
802 vg_acquire_thread_sync();
804 scene_upload( world
.scene_lines
, &world
.mesh_route_lines
);
806 vg_release_thread_sync();
807 vg_linear_del( world
.dynamic_vgl
, world
.scene_lines
);
810 /* determine if special type is required for this gate */
811 VG_STATIC
enum route_special_type
world_route_node_type( mdl_node
*pnode
)
813 if( pnode
->classtype
== k_classtype_gate
)
815 struct classtype_gate
*inf
= mdl_get_entdata( world
.meta
, pnode
);
819 mdl_node
*pother
= mdl_node_from_id( world
.meta
, inf
->target
);
821 if( pother
->classtype
== k_classtype_gate
)
823 return k_route_special_type_gate
;
827 return k_route_special_type_collector
;
830 return k_route_special_type_none
;
833 /* count entities and allocate correct amount of memory in advance */
834 VG_STATIC
void world_routes_allocate(void)
836 vg_info( "Allocating routes\n" );
844 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
846 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
848 if( pnode
->classtype
== k_classtype_route_node
||
849 pnode
->classtype
== k_classtype_gate
)
851 pnode
->sub_uid
= node_count
;
853 enum route_special_type type
= world_route_node_type( pnode
);
855 if( type
== k_route_special_type_gate
)
857 else if( type
== k_route_special_type_collector
)
862 else if( pnode
->classtype
== k_classtype_route
)
869 u32 node_size
= node_count
* sizeof(struct route_node
),
870 route_size
= route_count
* sizeof(struct route
),
871 gate_size
= gate_count
* sizeof(struct route_gate
),
872 collector_size
= collector_count
* sizeof(struct route_collector
);
874 world
.nodes
= vg_linear_alloc( world
.dynamic_vgl
, node_size
);
875 world
.routes
= vg_linear_alloc( world
.dynamic_vgl
, route_size
);
876 world
.gates
= vg_linear_alloc( world
.dynamic_vgl
, gate_size
);
877 world
.collectors
= vg_linear_alloc( world
.dynamic_vgl
, collector_size
);
880 /* create node from mdl node */
881 VG_STATIC
struct route_node
*world_routes_create_node( mdl_node
*pnode
)
883 struct route_node
*rn
= &world
.nodes
[ world
.node_count
++ ];
886 mdl_node_transform( pnode
, transform
);
888 v3_copy( transform
[3], rn
->co
);
889 v3_copy( transform
[0], rn
->right
);
890 v3_copy( transform
[1], rn
->up
);
891 v3_muls( transform
[2], -1.0f
, rn
->h
);
892 v3_normalize( rn
->right
);
893 v3_normalize( rn
->up
);
895 rn
->next
[0] = 0xffffffff;
896 rn
->next
[1] = 0xffffffff;
898 rn
->special_type
= 0;
900 rn
->current_refs
= 0;
906 /* retrieve the correct node id from mdl subuid */
907 VG_STATIC u32
world_routes_get_subuid( u32 target
)
912 return mdl_node_from_id( world
.meta
, target
)->sub_uid
;
916 VG_STATIC
void world_id_fixup( u32
*uid
, mdl_context
*mdl
)
919 *uid
= mdl_node_from_id( mdl
, *uid
)->sub_uid
;
925 /* process gate attachement onto node */
926 VG_STATIC
void world_routes_process_gate( struct route_node
*rn
,
929 struct classtype_gate
*inf
= mdl_get_entdata( world
.meta
, pnode
);
931 /* H is later scaled based on link distance */
932 v3_normalize( rn
->h
);
934 rn
->next
[0] = world_routes_get_subuid( inf
->target
);
935 rn
->next
[1] = 0xffffffff;
936 rn
->special_type
= world_route_node_type( pnode
);
938 /* process gate type */
939 if( rn
->special_type
== k_route_special_type_gate
)
941 mdl_node
*pother
= mdl_node_from_id( world
.meta
, inf
->target
);
943 struct route_gate
*rg
= &world
.gates
[ world
.gate_count
];
945 rg
->node_id
= world
.node_count
-1;
946 rg
->timing
.time
= 0.0;
947 rg
->timing
.version
= 0;
949 v3_copy( pnode
->co
, rg
->gate
.co
[0] );
950 v3_copy( pother
->co
, rg
->gate
.co
[1] );
951 v4_copy( pnode
->q
, rg
->gate
.q
[0] );
952 v4_copy( pother
->q
, rg
->gate
.q
[1] );
953 v2_copy( inf
->dims
, rg
->gate
.dims
);
955 gate_transform_update( &rg
->gate
);
956 rn
->special_id
= world
.gate_count
;
961 /* process collector type */
962 else if( rn
->special_type
== k_route_special_type_collector
)
964 struct route_collector
*rc
=
965 &world
.collectors
[ world
.collector_count
];
967 rc
->timing
.time
= 0.0;
968 rc
->timing
.version
= 0;
970 rn
->special_id
= world
.collector_count
;
971 world
.collector_count
++;
974 vg_fatal_exit_loop( "Invalid state" );
977 /* create route from node description */
978 VG_STATIC
void world_routes_create_route( mdl_node
*pnode
)
980 mdl_context
*mdl
= world
.meta
;
982 struct classtype_route
*inf
= mdl_get_entdata( mdl
, pnode
);
983 struct route
*route
= &world
.routes
[ world
.route_count
];
984 memset( route
, 0, sizeof(struct route
) );
986 v3_copy( inf
->colour
, route
->colour
);
987 route
->colour
[3] = 1.0f
;
988 route
->track_id
= 0xffffffff;
990 for( u32 j
=0; j
<vg_list_size(track_infos
); j
++ )
992 if( !strcmp( mdl_pstr(mdl
,pnode
->pstr_name
), track_infos
[j
].name
))
999 route
->start
= world_routes_get_subuid( inf
->id_start
);
1001 route
->factive
= 0.0f
;
1002 mdl_node_transform( pnode
, route
->scoreboard_transform
);
1004 struct route_ui_bar
*pui
= &world
.ui_bars
[ world
.route_count
];
1005 pui
->indices_head
= k_route_ui_max_indices
- 9;
1006 pui
->vertex_head
= k_route_ui_max_verts
- 200;
1007 pui
->segment_start
= 0;
1008 pui
->segment_count
= 0;
1009 pui
->fade_start
= 0;
1010 pui
->fade_count
= 0;
1011 pui
->fade_timer_start
= 0.0;
1013 world
.route_count
++;
1016 /* load all routes from model header */
1017 VG_STATIC
void world_routes_process(void)
1019 vg_info( "Initializing routes\n" );
1020 mdl_context
*mdl
= world
.meta
;
1022 for( int i
=0; i
<mdl
->info
.node_count
; i
++ )
1024 mdl_node
*pnode
= mdl_node_from_id(mdl
,i
);
1026 if( pnode
->classtype
== k_classtype_route_node
||
1027 pnode
->classtype
== k_classtype_gate
)
1029 struct route_node
*rn
= world_routes_create_node( pnode
);
1031 if( pnode
->classtype
== k_classtype_gate
)
1033 world_routes_process_gate( rn
, pnode
);
1037 struct classtype_route_node
*inf
= mdl_get_entdata( mdl
, pnode
);
1038 rn
->next
[0] = world_routes_get_subuid( inf
->target
);
1039 rn
->next
[1] = world_routes_get_subuid( inf
->target1
);
1042 else if( pnode
->classtype
== k_classtype_route
)
1044 world_routes_create_route( pnode
);
1051 for( int i
=0; i
<world
.route_count
; i
++ )
1053 struct route
*route
= &world
.routes
[i
];
1056 u32 si
= world_routes_get_path( route
->start
, stack
);
1058 for( int sj
=0; sj
<si
; sj
++ )
1060 struct route_node
*rn
= &world
.nodes
[ stack
[sj
] ];
1061 rn
->route_ids
[ rn
->ref_count
++ ] = i
;
1063 if( rn
->ref_count
> 4 )
1064 vg_warn( "Too many references on route node %i\n", i
);
1070 * -----------------------------------------------------------------------------
1072 * -----------------------------------------------------------------------------
1075 VG_STATIC
void world_routes_init(void)
1077 world
.current_run_version
= 2;
1078 world
.time
= RESET_MAX_TIME
*2.0;
1079 world
.last_use
= 0.0;
1081 shader_route_register();
1082 shader_routeui_register();
1084 vg_acquire_thread_sync();
1087 for( int i
=0; i
<vg_list_size(world
.ui_bars
); i
++ )
1090 struct route_ui_bar
*pui
= &world
.ui_bars
[i
];
1092 glGenVertexArrays( 1, &pui
->vao
);
1093 glGenBuffers( 1, &pui
->vbo
);
1094 glGenBuffers( 1, &pui
->ebo
);
1095 glBindVertexArray( pui
->vao
);
1097 size_t stride
= sizeof(v2f
);
1099 glBindBuffer( GL_ARRAY_BUFFER
, pui
->vbo
);
1100 glBufferData( GL_ARRAY_BUFFER
, k_route_ui_max_verts
*stride
,
1101 NULL
, GL_DYNAMIC_DRAW
);
1103 glBindVertexArray( pui
->vao
);
1104 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, pui
->ebo
);
1105 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
1106 k_route_ui_max_indices
*sizeof(u16
), NULL
,
1109 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, stride
, (void *)0 );
1110 glEnableVertexAttribArray( 0 );
1114 vg_release_thread_sync();
1117 VG_STATIC
void world_routes_update(void)
1119 world
.time
+= vg
.time_delta
;
1121 for( int i
=0; i
<world
.route_count
; i
++ )
1123 struct route
*route
= &world
.routes
[i
];
1124 route
->factive
= vg_lerpf( route
->factive
, route
->active
,
1125 0.6f
*vg
.time_delta
);
1129 world_routes_ui_updatetime(i
, world
.time
- route
->latest_pass
);
1134 VG_STATIC
void bind_terrain_noise(void);
1135 VG_STATIC
void render_world_routes( camera
*cam
)
1137 m4x3f identity_matrix
;
1138 m4x3_identity( identity_matrix
);
1141 shader_route_uTexGarbage(0);
1142 shader_link_standard_ub( _shader_route
.id
, 2 );
1143 bind_terrain_noise();
1145 shader_route_uPv( cam
->mtx
.pv
);
1146 shader_route_uPvmPrev( cam
->mtx_prev
.pv
);
1147 shader_route_uMdl( identity_matrix
);
1148 shader_route_uCamera( cam
->transform
[3] );
1150 mesh_bind( &world
.mesh_route_lines
);
1152 for( int i
=0; i
<world
.route_count
; i
++ )
1154 struct route
*route
= &world
.routes
[i
];
1157 v3_lerp( (v3f
){0.7f
,0.7f
,0.7f
}, route
->colour
, route
->factive
, colour
);
1160 shader_route_uColour( colour
);
1161 mdl_draw_submesh( &route
->sm
);
1165 VG_STATIC
void render_world_routes_ui(void)
1168 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1169 glBlendEquation(GL_FUNC_ADD
);
1171 float active_offset
= 0.0f
;
1172 for( int i
=0; i
<world
.route_count
; i
++ )
1174 struct route
*route
= &world
.routes
[i
];
1175 world_routes_ui_draw( i
, route
->colour
, active_offset
);
1176 active_offset
+= route
->factive
;
1179 glDisable(GL_BLEND
);
1182 #endif /* ROUTES_H */