2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
9 #include "world_gate.h"
11 #include "shaders/vblend.h"
12 #include "shaders/route.h"
13 #include "shaders/routeui.h"
16 enum route_special_type
18 k_route_special_type_none
= 0,
19 k_route_special_type_gate
= 1,
20 k_route_special_type_collector
= 2
23 VG_STATIC
void debug_sbpath( struct route_node
*rna
, struct route_node
*rnb
,
24 u32 colour
, float xoffset
)
26 v3f p0
, h0
, p1
, h1
, l
, p
;
28 v3_copy( rna
->co
, p0
);
29 v3_muladds( rna
->co
, rna
->h
, 1.0f
, h0
);
30 v3_copy( rnb
->co
, p1
);
31 v3_muladds( rnb
->co
, rnb
->h
, -1.0f
, h1
);
33 v3_muladds( p0
, rna
->right
, xoffset
, p0
);
34 v3_muladds( h0
, rna
->right
, xoffset
, h0
);
35 v3_muladds( p1
, rnb
->right
, xoffset
, p1
);
36 v3_muladds( h1
, rnb
->right
, xoffset
, h1
);
40 for( int i
=0; i
<5; i
++ )
42 float t
= (float)(i
+1)/5.0f
;
43 eval_bezier_time( p0
, p1
, h0
, h1
, t
, p
);
44 vg_line( p
, l
, colour
);
50 * Get a list of node ids in stack, and return how many there is
52 VG_STATIC u32
world_routes_get_path( u32 starter
, u32 stack
[64] )
60 int loop_complete
= 0;
64 if( stack_i
[si
-1] == 2 )
70 struct route_node
*rn
= &world
.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 VG_STATIC
void world_routes_ui_popfirst( struct route_ui_bar
*pui
)
113 if( pui
->segment_count
)
115 pui
->segment_start
++;
117 if( pui
->segment_start
== 32 )
118 pui
->segment_start
= 0;
120 pui
->segment_count
--;
125 * Reset ui bar completely
127 VG_STATIC
void world_routes_ui_clear( struct route_ui_bar
*pui
)
129 pui
->segment_start
= (pui
->segment_start
+ pui
->segment_count
) %
131 pui
->segment_count
= 0;
135 * Break a index range into two pieces over the edge of the maximum it can
136 * store. s1 is 0 always, so its a ring buffer.
138 VG_STATIC
void world_routes_ui_split_indices( u32 s0
, u32 count
,
141 *c0
= (VG_MIN( s0
+count
, k_route_ui_max_indices
)) - s0
;
146 * Place a set of indices into gpu array automatically splits
149 VG_STATIC
void world_routes_ui_set_indices( struct route_ui_bar
*pui
,
150 u16
*indices
, u32 count
)
153 world_routes_ui_split_indices( pui
->indices_head
, count
, &c0
, &c1
);
155 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, pui
->ebo
);
159 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, pui
->indices_head
*sizeof(u16
),
160 c0
*sizeof(u16
), indices
);
165 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, 0, c1
*sizeof(u16
), indices
+c0
);
166 pui
->indices_head
= c1
;
169 pui
->indices_head
+= c0
;
173 * Place a set of vertices into gpu array
175 VG_STATIC u32
world_routes_ui_set_verts( struct route_ui_bar
*pui
,
176 v2f
*verts
, u32 count
)
178 if( pui
->vertex_head
+ count
>= k_route_ui_max_verts
)
179 pui
->vertex_head
= 0;
181 u32 vert_start
= pui
->vertex_head
;
182 pui
->vertex_head
+= count
;
184 glBindBuffer( GL_ARRAY_BUFFER
, pui
->vbo
);
185 glBufferSubData( GL_ARRAY_BUFFER
, (GLintptr
)(vert_start
*sizeof(v2f
)),
186 sizeof(v2f
)*count
, verts
);
192 * Update the last (count) vertices positions, does not add any.
193 * Data must already be written to, and not cross either array boundaries.
195 VG_STATIC u32
world_routes_ui_update_verts( struct route_ui_bar
*pui
,
196 v2f
*verts
, u32 count
)
198 u32 vert_start
= pui
->vertex_head
-count
;
200 glBindBuffer( GL_ARRAY_BUFFER
, pui
->vbo
);
201 glBufferSubData( GL_ARRAY_BUFFER
, (GLintptr
)(vert_start
*sizeof(v2f
)),
202 sizeof(v2f
)*count
, verts
);
208 * Current/active segment of this UI bar
210 VG_STATIC
struct route_ui_segment
*world_routes_ui_curseg(
211 struct route_ui_bar
*pui
)
213 u32 index
= (pui
->segment_start
+pui
->segment_count
-1)%k_max_ui_segments
;
214 return &pui
->segments
[ index
];
218 * Start a new segment in the UI bar, will create a split on the last one if
219 * there is one active currently. (api)
221 VG_STATIC
void world_routes_ui_newseg( u32 route
)
223 struct route_ui_bar
*pui
= &world
.ui_bars
[route
];
225 glBindVertexArray( pui
->vao
);
226 if( pui
->segment_count
)
228 float const k_gap_width
= 1.0f
;
230 struct route_ui_segment
*cseg
= world_routes_ui_curseg( pui
);
233 verts
[0][0] = cseg
->length
-k_gap_width
;
235 verts
[1][0] = cseg
->length
-k_gap_width
;
238 world_routes_ui_update_verts( pui
, verts
, 2 );
241 pui
->segment_count
++;
242 struct route_ui_segment
*segment
= world_routes_ui_curseg( pui
);
254 u32 vert_start
= world_routes_ui_set_verts( pui
, verts
, 4 );
257 indices
[0] = vert_start
+ 0;
258 indices
[1] = vert_start
+ 1;
259 indices
[2] = vert_start
+ 3;
260 indices
[3] = vert_start
+ 0;
261 indices
[4] = vert_start
+ 3;
262 indices
[5] = vert_start
+ 2;
264 segment
->vertex_start
= vert_start
;
265 segment
->vertex_count
= 4;
266 segment
->index_start
= pui
->indices_head
;
267 segment
->index_count
= 6;
268 segment
->notches
= 0;
270 world_routes_ui_set_indices( pui
, indices
, 6 );
274 * Extend the end of the bar
276 VG_STATIC
void world_routes_ui_updatetime( u32 route
, float time
)
278 struct route_ui_bar
*pui
= &world
.ui_bars
[route
];
286 u32 vert_start
= pui
->vertex_head
-2;
288 glBindVertexArray( pui
->vao
);
289 world_routes_ui_update_verts( pui
, verts
, 2 );
291 struct route_ui_segment
*cseg
= world_routes_ui_curseg( pui
);
295 VG_STATIC
void world_routes_ui_draw_segment( struct route_ui_segment
*segment
)
298 world_routes_ui_split_indices( segment
->index_start
,
299 segment
->index_count
, &c0
, &c1
);
301 glDrawElements( GL_TRIANGLES
, c0
, GL_UNSIGNED_SHORT
,
302 (void *)(segment
->index_start
*sizeof(u16
)));
304 glDrawElements( GL_TRIANGLES
, c1
, GL_UNSIGNED_SHORT
, (void *)(0) );
308 * Draws full bar at Y offset(offset).
310 VG_STATIC
void world_routes_ui_draw( u32 route
, v4f colour
, float offset
)
312 float const k_bar_height
= 0.05f
,
313 k_bar_scale_x
= 0.005f
;
315 struct route
*pr
= &world
.routes
[route
];
316 struct route_ui_bar
*pui
= &world
.ui_bars
[route
];
318 float cx
= pui
->xpos
;
320 shader_routeui_use();
321 glBindVertexArray( pui
->vao
);
323 float fade_amt
= world
.time
- pui
->fade_timer_start
;
324 fade_amt
= vg_clampf( fade_amt
/ 1.0f
, 0.0f
, 1.0f
);
326 float fade_block_size
= 0.0f
,
327 main_block_size
= 0.0f
;
329 for( u32 i
=0; i
<pui
->fade_count
; i
++ )
331 u32 j
= (pui
->fade_start
+ i
) % k_max_ui_segments
;
332 struct route_ui_segment
*segment
= &pui
->segments
[j
];
334 fade_block_size
+= segment
->length
;
337 cx
-= fade_block_size
* fade_amt
;
340 v4_copy( colour
, fade_colour
);
341 fade_colour
[3] *= 1.0f
-fade_amt
;
344 float timer_delta
= (world
.time
- world
.last_use
) * (1.0/45.0),
345 timer_scale
= 1.0f
- vg_minf( timer_delta
, 1.0f
);
351 float height
= pr
->factive
*k_bar_height
* timer_scale
,
352 base
= -1.0f
+ (offset
+0.5f
)*k_bar_height
* timer_scale
;
354 shader_routeui_uColour( fade_colour
);
355 for( u32 i
=0; i
<pui
->fade_count
; i
++ )
357 u32 j
= (pui
->fade_start
+ i
) % k_max_ui_segments
;
358 struct route_ui_segment
*segment
= &pui
->segments
[j
];
360 shader_routeui_uOffset( (v4f
){ cx
*k_bar_scale_x
, base
,
361 k_bar_scale_x
, height
} );
363 world_routes_ui_draw_segment( segment
);
364 cx
+= segment
->length
;
370 shader_routeui_uColour( colour
);
371 for( u32 i
=0; i
<pui
->segment_count
; i
++ )
373 u32 j
= (pui
->segment_start
+ i
) % k_max_ui_segments
;
374 struct route_ui_segment
*segment
= &pui
->segments
[j
];
376 shader_routeui_uOffset( (v4f
){ cx
*k_bar_scale_x
, base
,
377 k_bar_scale_x
, height
} );
379 world_routes_ui_draw_segment( segment
);
380 cx
+= segment
->length
;
382 main_block_size
+= segment
->length
;
385 pui
->xpos
= vg_lerpf( pui
->xpos
, -main_block_size
* 0.5f
, 0.03f
);
388 VG_STATIC
void world_routes_local_set_record( u32 route
, double lap_time
)
390 vg_success( " NEW LAP TIME: %f\n", lap_time
);
392 struct route
*pr
= &world
.routes
[route
];
394 if( pr
->track_id
!= 0xffffffff )
396 double time_centiseconds
= lap_time
* 100.0;
397 if( time_centiseconds
> (float)0xfffe )
400 highscore_record temp
;
401 temp
.trackid
= pr
->track_id
;
402 temp
.datetime
= time(NULL
);
405 temp
.time
= time_centiseconds
;
407 highscores_push_record( &temp
);
409 struct track_info
*pti
= &track_infos
[ pr
->track_id
];
412 if( pti
->achievement_id
)
414 steam_set_achievement( pti
->achievement_id
);
415 steam_store_achievements();
420 vg_warn( "There is no associated track for this record...\n" );
425 * Will scan the whole run for two things;
426 * 1: we set a new record for the total, complete loop around the course
427 * 2: the time of each segment will be recorded into the data buffer
428 * (not implemented: TODO)
430 VG_STATIC
void world_routes_verify_run( u32 route
)
432 struct route
*pr
= &world
.routes
[route
];
433 struct route_ui_bar
*pui
= &world
.ui_bars
[route
];
436 u32 si
= world_routes_get_path( world
.routes
[route
].start
, stack
);
439 * we only care about gates that ref gates, so shuffle down the array
441 struct route_timing
*timings
[64];
442 u32 sj
= 0, maxv
= 0, begin
= 0;
443 for( u32 i
=0; i
<si
; i
++ )
445 struct route_node
*inode
= &world
.nodes
[stack
[i
]];
447 if( inode
->special_type
== k_route_special_type_collector
)
449 timings
[sj
++] = &world
.collectors
[ inode
->special_id
].timing
;
451 else if( inode
->special_type
== k_route_special_type_gate
)
453 timings
[sj
++] = &world
.gates
[inode
->special_id
].timing
;
457 for( u32 i
=0; i
<sj
; i
++ )
459 if( timings
[i
]->version
> maxv
)
461 maxv
= timings
[i
]->version
;
466 vg_info( "== begin verification (%u) ==\n", route
);
467 vg_info( " current version: %u\n", world
.current_run_version
);
470 if( timings
[begin
]->version
== world
.current_run_version
)
473 int valid_segment_count
= 0;
475 double lap_time
= 0.0;
477 for( u32 i
=0; i
<sj
; i
++ )
479 u32 j
= (sj
+begin
-i
-1) % sj
,
486 /* j1v should equal jv+1 */
487 if( timings
[j1
]->version
== timings
[j
]->version
+1 )
489 diff
= timings
[j1
]->time
- timings
[j
]->time
;
492 if( verified
&& diff
> 0.0 ) valid_segment_count
++;
499 vg_success( " [ %u %f ] %f\n", timings
[j1
]->time
,
500 timings
[j1
]->version
, diff
);
502 vg_warn( " [ %u %f ]\n", timings
[j1
]->time
, timings
[j1
]->version
);
505 pui
->fade_start
= pui
->segment_start
;
507 pui
->fade_timer_start
= world
.time
;
509 int orig_seg_count
= pui
->segment_count
;
511 world_routes_ui_newseg( route
);
515 world_routes_local_set_record( route
, lap_time
);
516 world_routes_ui_popfirst( pui
);
520 vg_info( " ctime: %f\n", lap_time
);
522 /* remove any excess we had from previous runs */
523 int to_remove
= orig_seg_count
-valid_segment_count
;
524 for( int i
=0; i
<to_remove
; i
++ )
526 world_routes_ui_popfirst( pui
);
530 world
.routes
[route
].latest_pass
= world
.time
;
534 * When going through a gate this is called for bookkeeping purposes
536 VG_STATIC
void world_routes_activate_gate( u32 id
)
538 struct route_gate
*rg
= &world
.gates
[id
];
539 struct route_node
*pnode
= &world
.nodes
[rg
->node_id
],
540 *pdest
= &world
.nodes
[pnode
->next
[0]];
542 world
.last_use
= world
.time
;
544 struct route_collector
*rc
= &world
.collectors
[ pdest
->special_id
];
546 world
.active_gate
= id
;
547 rg
->timing
.version
= world
.current_run_version
;
548 rg
->timing
.time
= world
.time
;
550 for( u32 i
=0; i
<world
.route_count
; i
++ )
552 struct route
*route
= &world
.routes
[i
];
554 int was_active
= route
->active
;
557 for( u32 j
=0; j
<pdest
->ref_count
; j
++ )
559 if( pdest
->route_ids
[j
] == i
)
561 world_routes_verify_run( i
);
567 if( was_active
&& !route
->active
)
569 struct route_ui_bar
*pui
= &world
.ui_bars
[i
];
570 pui
->fade_start
= pui
->segment_start
;
571 pui
->fade_count
= pui
->segment_count
;
572 pui
->fade_timer_start
= world
.time
;
574 world_routes_ui_clear( pui
);
575 vg_success( "CLEARING -> %u %u \n", pui
->fade_start
,
580 world
.current_run_version
++;
582 rc
->timing
.version
= world
.current_run_version
;
583 rc
->timing
.time
= world
.time
;
584 world
.current_run_version
++;
588 * Notify the UI system that we've reset the player
590 VG_STATIC
void world_routes_notify_reset(void)
592 world
.rewind_from
= world
.time
;
593 world
.rewind_to
= world
.last_use
;
596 for( int i
=0; i
<r
->route_count
; i
++ )
598 struct route
*route
= &r
->routes
[i
];
601 world_routes_ui_notch( i
, r
->time
- route
->latest_pass
);
606 /* Rewind between the saved points in time */
607 VG_STATIC
void world_routes_rollback_time( double t
)
609 world
.time
= vg_lerp( world
.rewind_to
, world
.rewind_from
, t
);
612 /* draw lines along the paths */
613 VG_STATIC
void world_routes_debug(void)
615 for( int i
=0; i
<world
.node_count
; i
++ )
617 struct route_node
*rn
= &world
.nodes
[i
];
618 vg_line_pt3( rn
->co
, 1.0f
, rn
->special_type
? 0xffffff00: 0xff00b2ff );
621 for( int i
=0; i
<world
.route_count
; i
++ )
623 struct route
*route
= &world
.routes
[i
];
626 u32 si
= world_routes_get_path( route
->start
, stack
);
628 u32 colours
[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
631 u32 cc
= colours
[i
%vg_list_size(colours
)];
633 for( int sj
=0; sj
<si
; sj
++ )
637 struct route_node
*pj
= &world
.nodes
[stack
[sj
]],
638 *pk
= &world
.nodes
[stack
[sk
]];
639 debug_sbpath( pj
, pk
, cc
, (float)i
);
643 for( int i
=0; i
<world
.node_count
; i
++ )
645 struct route_node
*ri
= &world
.nodes
[i
],
648 for( int j
=0; j
<2; j
++ )
650 if( ri
->next
[j
] != 0xffffffff )
652 rj
= &world
.nodes
[ri
->next
[j
]];
653 vg_line( ri
->co
, rj
->co
, 0x20ffffff );
659 VG_STATIC
void world_routes_create_mesh( u32 route_id
)
661 struct route
*route
= &world
.routes
[ route_id
];
664 u32 si
= world_routes_get_path( route
->start
, stack
);
668 for( int sj
=0; sj
<si
; sj
++ )
672 struct route_node
*rnj
= &world
.nodes
[ stack
[sj
] ],
673 *rnk
= &world
.nodes
[ stack
[sk
] ],
676 if( rnj
->special_type
&& rnk
->special_type
)
682 float base_x0
= (float)rnj
->ref_count
*-0.5f
+ (float)rnj
->current_refs
,
683 base_x1
= (float)rnk
->ref_count
*-0.5f
+ (float)rnk
->current_refs
;
685 if( rnk
->special_type
)
687 rnl
= &world
.nodes
[ rnk
->next
[0] ];
688 base_x1
= (float)rnl
->ref_count
*-0.5f
+ (float)rnl
->current_refs
;
696 v3f p0
, h0
, p1
, h1
, p
, pd
;
698 v3_copy( rnj
->co
, p0
);
699 v3_muladds( rnj
->co
, rnj
->h
, 1.0f
, h0
);
700 v3_copy( rnk
->co
, p1
);
701 v3_muladds( rnk
->co
, rnk
->h
, -1.0f
, h1
);
706 for( int it
=0; it
<256; it
++ )
708 float const k_sample_dist
= 0.02f
;
709 eval_bezier_time( p0
,p1
,h0
,h1
, t
,p
);
710 eval_bezier_time( p0
,p1
,h0
,h1
, t
+k_sample_dist
,pd
);
712 float mod
= k_sample_dist
/ v3_dist( p
, pd
);
715 v3_muls( rnj
->up
, 1.0f
-t
, up
);
716 v3_muladds( up
, rnk
->up
, t
, up
);
719 v3_cross( up
, v0
, right
);
720 v3_normalize( right
);
722 float cur_x
= (1.0f
-t
)*base_x0
+ t
*base_x1
;
724 v3f sc
, sa
, sb
, down
;
725 v3_muladds( p
, right
, cur_x
, sc
);
726 v3_muladds( sc
, up
, 1.5f
, sc
);
727 v3_muladds( sc
, right
, 0.45f
, sa
);
728 v3_muladds( sc
, right
, -0.45f
, sb
);
729 v3_muls( up
, -1.0f
, down
);
734 if( ray_world( sa
, down
, &ha
) &&
735 ray_world( sb
, down
, &hb
))
739 v3_muladds( ha
.pos
, up
, 0.06f
, va
.co
);
740 v3_muladds( hb
.pos
, up
, 0.06f
, vb
.co
);
741 v3_copy( up
, va
.norm
);
742 v3_copy( up
, vb
.norm
);
746 scene_push_vert( world
.scene_lines
, &va
);
747 scene_push_vert( world
.scene_lines
, &vb
);
751 /* Connect them with triangles */
752 scene_push_tri( world
.scene_lines
, (u32
[3]){
753 last_valid
+0-2, last_valid
+1-2, last_valid
+2-2} );
754 scene_push_tri( world
.scene_lines
, (u32
[3]){
755 last_valid
+1-2, last_valid
+3-2, last_valid
+2-2} );
758 last_valid
= world
.scene_lines
->vertex_count
;
767 /* TODO special case for end of loop, need to add triangles
768 * between first and last rungs */
773 rnj
->current_refs
++;
776 scene_copy_slice( world
.scene_lines
, &route
->sm
);
780 * Create the strips of colour that run through the world along course paths
782 VG_STATIC
void world_routes_generate(void)
784 vg_info( "Generating route meshes\n" );
785 world
.scene_lines
= scene_init( world
.dynamic_vgl
, 200000, 300000 );
787 for( u32 i
=0; i
<world
.route_count
; i
++ )
788 world_routes_create_mesh( i
);
790 vg_acquire_thread_sync();
792 scene_upload( world
.scene_lines
, &world
.mesh_route_lines
);
794 vg_release_thread_sync();
795 vg_linear_del( world
.dynamic_vgl
, world
.scene_lines
);
798 /* determine if special type is required for this gate */
799 VG_STATIC
enum route_special_type
world_route_node_type( mdl_node
*pnode
)
801 if( pnode
->classtype
== k_classtype_gate
)
803 struct classtype_gate
*inf
= mdl_get_entdata( world
.meta
, pnode
);
807 mdl_node
*pother
= mdl_node_from_id( world
.meta
, inf
->target
);
809 if( pother
->classtype
== k_classtype_gate
)
811 return k_route_special_type_gate
;
815 return k_route_special_type_collector
;
818 return k_route_special_type_none
;
821 /* count entities and allocate correct amount of memory in advance */
822 VG_STATIC
void world_routes_allocate(void)
824 vg_info( "Allocating routes\n" );
832 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
834 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
836 if( pnode
->classtype
== k_classtype_route_node
||
837 pnode
->classtype
== k_classtype_gate
)
839 pnode
->sub_uid
= node_count
;
841 enum route_special_type type
= world_route_node_type( pnode
);
843 if( type
== k_route_special_type_gate
)
845 else if( type
== k_route_special_type_collector
)
850 else if( pnode
->classtype
== k_classtype_route
)
857 u32 node_size
= node_count
* sizeof(struct route_node
),
858 route_size
= route_count
* sizeof(struct route
),
859 gate_size
= gate_count
* sizeof(struct route_gate
),
860 collector_size
= collector_count
* sizeof(struct route_collector
);
862 world
.nodes
= vg_linear_alloc( world
.dynamic_vgl
, node_size
);
863 world
.routes
= vg_linear_alloc( world
.dynamic_vgl
, route_size
);
864 world
.gates
= vg_linear_alloc( world
.dynamic_vgl
, gate_size
);
865 world
.collectors
= vg_linear_alloc( world
.dynamic_vgl
, collector_size
);
868 /* create node from mdl node */
869 VG_STATIC
struct route_node
*world_routes_create_node( mdl_node
*pnode
)
871 struct route_node
*rn
= &world
.nodes
[ world
.node_count
++ ];
874 mdl_node_transform( pnode
, transform
);
876 v3_copy( transform
[3], rn
->co
);
877 v3_copy( transform
[0], rn
->right
);
878 v3_copy( transform
[1], rn
->up
);
879 v3_muls( transform
[2], -1.0f
, rn
->h
);
880 v3_normalize( rn
->right
);
881 v3_normalize( rn
->up
);
883 rn
->next
[0] = 0xffffffff;
884 rn
->next
[1] = 0xffffffff;
886 rn
->special_type
= 0;
888 rn
->current_refs
= 0;
894 /* retrieve the correct node id from mdl subuid */
895 VG_STATIC u32
world_routes_get_subuid( u32 target
)
900 return mdl_node_from_id( world
.meta
, target
)->sub_uid
;
904 VG_STATIC
void world_id_fixup( u32
*uid
, mdl_context
*mdl
)
907 *uid
= mdl_node_from_id( mdl
, *uid
)->sub_uid
;
913 /* process gate attachement onto node */
914 VG_STATIC
void world_routes_process_gate( struct route_node
*rn
,
917 struct classtype_gate
*inf
= mdl_get_entdata( world
.meta
, pnode
);
919 /* H is later scaled based on link distance */
920 v3_normalize( rn
->h
);
922 rn
->next
[0] = world_routes_get_subuid( inf
->target
);
923 rn
->next
[1] = 0xffffffff;
924 rn
->special_type
= world_route_node_type( pnode
);
926 /* process gate type */
927 if( rn
->special_type
== k_route_special_type_gate
)
929 mdl_node
*pother
= mdl_node_from_id( world
.meta
, inf
->target
);
931 struct route_gate
*rg
= &world
.gates
[ world
.gate_count
];
933 rg
->node_id
= world
.node_count
-1;
934 rg
->timing
.time
= 0.0;
935 rg
->timing
.version
= 0;
937 v3_copy( pnode
->co
, rg
->gate
.co
[0] );
938 v3_copy( pother
->co
, rg
->gate
.co
[1] );
939 v4_copy( pnode
->q
, rg
->gate
.q
[0] );
940 v4_copy( pother
->q
, rg
->gate
.q
[1] );
941 v2_copy( inf
->dims
, rg
->gate
.dims
);
943 gate_transform_update( &rg
->gate
);
944 rn
->special_id
= world
.gate_count
;
949 /* process collector type */
950 else if( rn
->special_type
== k_route_special_type_collector
)
952 struct route_collector
*rc
=
953 &world
.collectors
[ world
.collector_count
];
955 rc
->timing
.time
= 0.0;
956 rc
->timing
.version
= 0;
958 rn
->special_id
= world
.collector_count
;
959 world
.collector_count
++;
962 vg_fatal_exit_loop( "Invalid state" );
965 /* create route from node description */
966 VG_STATIC
void world_routes_create_route( mdl_node
*pnode
)
968 mdl_context
*mdl
= world
.meta
;
970 struct classtype_route
*inf
= mdl_get_entdata( mdl
, pnode
);
971 struct route
*route
= &world
.routes
[ world
.route_count
];
972 memset( route
, 0, sizeof(struct route
) );
974 v3_copy( inf
->colour
, route
->colour
);
975 route
->colour
[3] = 1.0f
;
976 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
= world_routes_get_subuid( inf
->id_start
);
989 route
->factive
= 0.0f
;
990 mdl_node_transform( pnode
, route
->scoreboard_transform
);
992 struct route_ui_bar
*pui
= &world
.ui_bars
[ world
.route_count
];
993 pui
->indices_head
= k_route_ui_max_indices
- 9;
994 pui
->vertex_head
= k_route_ui_max_verts
- 200;
995 pui
->segment_start
= 0;
996 pui
->segment_count
= 0;
999 pui
->fade_timer_start
= 0.0;
1001 world
.route_count
++;
1004 /* load all routes from model header */
1005 VG_STATIC
void world_routes_process(void)
1007 vg_info( "Initializing routes\n" );
1008 mdl_context
*mdl
= world
.meta
;
1010 for( int i
=0; i
<mdl
->info
.node_count
; i
++ )
1012 mdl_node
*pnode
= mdl_node_from_id(mdl
,i
);
1014 if( pnode
->classtype
== k_classtype_route_node
||
1015 pnode
->classtype
== k_classtype_gate
)
1017 struct route_node
*rn
= world_routes_create_node( pnode
);
1019 if( pnode
->classtype
== k_classtype_gate
)
1021 world_routes_process_gate( rn
, pnode
);
1025 struct classtype_route_node
*inf
= mdl_get_entdata( mdl
, pnode
);
1026 rn
->next
[0] = world_routes_get_subuid( inf
->target
);
1027 rn
->next
[1] = world_routes_get_subuid( inf
->target1
);
1030 else if( pnode
->classtype
== k_classtype_route
)
1032 world_routes_create_route( pnode
);
1039 for( int i
=0; i
<world
.route_count
; i
++ )
1041 struct route
*route
= &world
.routes
[i
];
1044 u32 si
= world_routes_get_path( route
->start
, stack
);
1046 for( int sj
=0; sj
<si
; sj
++ )
1048 struct route_node
*rn
= &world
.nodes
[ stack
[sj
] ];
1049 rn
->route_ids
[ rn
->ref_count
++ ] = i
;
1051 if( rn
->ref_count
> 4 )
1052 vg_warn( "Too many references on route node %i\n", i
);
1058 * -----------------------------------------------------------------------------
1060 * -----------------------------------------------------------------------------
1063 VG_STATIC
void world_routes_init(void)
1065 world
.current_run_version
= 2;
1066 world
.time
= RESET_MAX_TIME
*2.0;
1067 world
.last_use
= 0.0;
1069 shader_route_register();
1070 shader_routeui_register();
1072 vg_acquire_thread_sync();
1075 for( int i
=0; i
<vg_list_size(world
.ui_bars
); i
++ )
1078 struct route_ui_bar
*pui
= &world
.ui_bars
[i
];
1080 glGenVertexArrays( 1, &pui
->vao
);
1081 glGenBuffers( 1, &pui
->vbo
);
1082 glGenBuffers( 1, &pui
->ebo
);
1083 glBindVertexArray( pui
->vao
);
1085 size_t stride
= sizeof(v2f
);
1087 glBindBuffer( GL_ARRAY_BUFFER
, pui
->vbo
);
1088 glBufferData( GL_ARRAY_BUFFER
, k_route_ui_max_verts
*stride
,
1089 NULL
, GL_DYNAMIC_DRAW
);
1091 glBindVertexArray( pui
->vao
);
1092 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, pui
->ebo
);
1093 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
1094 k_route_ui_max_indices
*sizeof(u16
), NULL
,
1097 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, stride
, (void *)0 );
1098 glEnableVertexAttribArray( 0 );
1102 vg_release_thread_sync();
1105 VG_STATIC
void world_routes_update(void)
1107 world
.time
+= vg
.time_delta
;
1109 for( int i
=0; i
<world
.route_count
; i
++ )
1111 struct route
*route
= &world
.routes
[i
];
1112 route
->factive
= vg_lerpf( route
->factive
, route
->active
,
1113 0.6f
*vg
.time_delta
);
1117 world_routes_ui_updatetime(i
, world
.time
- route
->latest_pass
);
1122 VG_STATIC
void bind_terrain_noise(void);
1123 VG_STATIC
void render_world_routes( m4x4f projection
, v3f camera
)
1125 m4x3f identity_matrix
;
1126 m4x3_identity( identity_matrix
);
1129 shader_route_uTexGarbage(0);
1130 shader_link_standard_ub( _shader_route
.id
, 2 );
1131 bind_terrain_noise();
1133 shader_route_uPv( projection
);
1134 shader_route_uMdl( identity_matrix
);
1135 shader_route_uCamera( camera
);
1137 mesh_bind( &world
.mesh_route_lines
);
1139 for( int i
=0; i
<world
.route_count
; i
++ )
1141 struct route
*route
= &world
.routes
[i
];
1144 v3_lerp( (v3f
){0.7f
,0.7f
,0.7f
}, route
->colour
, route
->factive
, colour
);
1147 shader_route_uColour( colour
);
1148 mdl_draw_submesh( &route
->sm
);
1152 VG_STATIC
void render_world_routes_ui(void)
1155 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1156 glBlendEquation(GL_FUNC_ADD
);
1158 float active_offset
= 0.0f
;
1159 for( int i
=0; i
<world
.route_count
; i
++ )
1161 struct route
*route
= &world
.routes
[i
];
1162 world_routes_ui_draw( i
, route
->colour
, active_offset
);
1163 active_offset
+= route
->factive
;
1166 glDisable(GL_BLEND
);
1169 #endif /* ROUTES_H */