2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
9 #include "world_info.h"
10 #include "world_gate.h"
12 #include "shaders/vblend.h"
13 #include "shaders/route.h"
14 #include "shaders/routeui.h"
16 enum route_special_type
18 k_route_special_type_gate
= 1,
19 k_route_special_type_collector
= 2
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 return; /* FIXME: Temporarily disabled */
308 struct subworld_routes
*r
= &world
.routes
;
309 struct route
*pr
= &r
->routes
[route
];
311 if( (time
- pr
->ui
.last_notch
) > 1.0 )
313 struct route_ui_segment
*segment
= world_routes_ui_curseg(pr
);
314 if( segment
->notches
== k_max_ui_splits_per_segment
)
321 float const k_notch_width
= 1.0f
;
323 float xa
= time
-k_notch_width
,
324 xb
= time
-k_notch_width
* 0.5f
,
335 verts
[3][1] = -0.25f
;
347 glBindVertexArray( pr
->ui
.vao
);
348 u32 vert_start_mod
= world_routes_ui_update_verts( pr
, verts
, 2 ),
349 vert_start_new
= world_routes_ui_set_verts( pr
, verts
+2, 6 );
352 indices
[ 0] = vert_start_mod
+1;
353 indices
[ 1] = vert_start_new
+0;
354 indices
[ 2] = vert_start_mod
+0;
355 indices
[ 3] = vert_start_mod
+1;
356 indices
[ 4] = vert_start_new
+1;
357 indices
[ 5] = vert_start_new
+0;
359 indices
[ 6] = vert_start_new
+0;
360 indices
[ 7] = vert_start_new
+1;
361 indices
[ 8] = vert_start_new
+3;
362 indices
[ 9] = vert_start_new
+0;
363 indices
[10] = vert_start_new
+3;
364 indices
[11] = vert_start_new
+2;
366 indices
[12] = vert_start_new
+3;
367 indices
[13] = vert_start_new
+4;
368 indices
[14] = vert_start_new
+2;
369 indices
[15] = vert_start_new
+3;
370 indices
[16] = vert_start_new
+5;
371 indices
[17] = vert_start_new
+4;
373 world_routes_ui_set_indices( pr
, indices
, 18 );
375 pr
->ui
.last_notch
= time
;
377 segment
->vertex_count
+= 6;
378 segment
->index_count
+= 18;
382 static void world_routes_ui_draw_segment( struct route_ui_segment
*segment
)
385 world_routes_ui_split_indices( segment
->index_start
,
386 segment
->index_count
, &c0
, &c1
);
388 glDrawElements( GL_TRIANGLES
, c0
, GL_UNSIGNED_SHORT
,
389 (void *)(segment
->index_start
*sizeof(u16
)));
391 glDrawElements( GL_TRIANGLES
, c1
, GL_UNSIGNED_SHORT
, (void *)(0) );
395 * Draws full bar at Y offset(offset).
397 static void world_routes_ui_draw( u32 route
, v4f colour
, float offset
)
399 float const k_bar_height
= 0.05f
,
400 k_bar_scale_x
= 0.005f
;
402 struct subworld_routes
*r
= &world
.routes
;
403 struct route
*pr
= &r
->routes
[route
];
405 float cx
= pr
->ui
.xpos
;
407 shader_routeui_use();
408 glBindVertexArray( pr
->ui
.vao
);
410 float fade_amt
= r
->time
- pr
->ui
.fade_timer_start
;
411 fade_amt
= vg_clampf( fade_amt
/ 1.0f
, 0.0f
, 1.0f
);
413 float fade_block_size
= 0.0f
,
414 main_block_size
= 0.0f
;
416 for( u32 i
=0; i
<pr
->ui
.fade_count
; i
++ )
418 u32 j
= (pr
->ui
.fade_start
+ i
) % k_max_ui_segments
;
419 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
421 fade_block_size
+= segment
->length
;
424 cx
-= fade_block_size
* fade_amt
;
427 v4_copy( colour
, fade_colour
);
428 fade_colour
[3] *= 1.0f
-fade_amt
;
431 float timer_delta
= (r
->time
- r
->last_use
) * (1.0/45.0),
432 timer_scale
= 1.0f
- vg_minf( timer_delta
, 1.0f
);
438 float height
= pr
->factive
*k_bar_height
* timer_scale
,
439 base
= -1.0f
+ (offset
+0.5f
)*k_bar_height
* timer_scale
;
441 shader_routeui_uColour( fade_colour
);
442 for( u32 i
=0; i
<pr
->ui
.fade_count
; i
++ )
444 u32 j
= (pr
->ui
.fade_start
+ i
) % k_max_ui_segments
;
445 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
447 shader_routeui_uOffset( (v4f
){ cx
*k_bar_scale_x
, base
,
448 k_bar_scale_x
, height
} );
450 world_routes_ui_draw_segment( segment
);
451 cx
+= segment
->length
;
457 shader_routeui_uColour( colour
);
458 for( u32 i
=0; i
<pr
->ui
.segment_count
; i
++ )
460 u32 j
= (pr
->ui
.segment_start
+ i
) % k_max_ui_segments
;
461 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
463 shader_routeui_uOffset( (v4f
){ cx
*k_bar_scale_x
, base
,
464 k_bar_scale_x
, height
} );
466 world_routes_ui_draw_segment( segment
);
467 cx
+= segment
->length
;
469 main_block_size
+= segment
->length
;
472 pr
->ui
.xpos
= vg_lerpf( pr
->ui
.xpos
, -main_block_size
* 0.5f
, 0.03f
);
475 static void world_routes_local_set_record( u32 route
, double lap_time
)
477 vg_success( " NEW LAP TIME: %f\n", lap_time
);
479 struct subworld_routes
*r
= &world
.routes
;
480 struct route
*pr
= &r
->routes
[route
];
482 if( pr
->track_id
!= 0xffffffff )
484 double time_centiseconds
= lap_time
* 100.0;
485 if( time_centiseconds
> (float)0xfffe )
488 highscore_record temp
;
489 temp
.trackid
= pr
->track_id
;
490 temp
.datetime
= time(NULL
);
493 temp
.time
= time_centiseconds
;
495 highscores_push_record( &temp
);
497 struct track_info
*pti
= &track_infos
[ pr
->track_id
];
500 if( pti
->achievement_id
)
502 steam_set_achievement( pti
->achievement_id
);
503 steam_store_achievements();
508 vg_warn( "There is no associated track for this record...\n" );
513 * Will scan the whole run for two things;
514 * 1: we set a new record for the total, complete loop around the course
515 * 2: the time of each segment will be recorded into the data buffer
516 * (not implemented: TODO)
518 static void world_routes_verify_run( u32 route
)
520 struct subworld_routes
*r
= &world
.routes
;
521 struct route
*pr
= &r
->routes
[route
];
524 u32 si
= world_routes_get_path( r
->routes
[route
].start
, stack
);
527 * we only care about gates that ref gates, so shuffle down the array
529 struct route_timing
*timings
[64];
530 u32 sj
= 0, maxv
= 0, begin
= 0;
531 for( u32 i
=0; i
<si
; i
++ )
533 if( r
->nodes
[stack
[i
]].special_type
== k_route_special_type_collector
)
534 timings
[sj
++] = &r
->collectors
[r
->nodes
[stack
[i
]].special_id
].timing
;
535 else if( r
->nodes
[stack
[i
]].special_type
== k_route_special_type_gate
)
536 timings
[sj
++] = &r
->gates
[r
->nodes
[stack
[i
]].special_id
].timing
;
539 for( u32 i
=0; i
<sj
; i
++ )
541 if( timings
[i
]->version
> maxv
)
543 maxv
= timings
[i
]->version
;
548 vg_info( "== begin verification (%u) ==\n", route
);
549 vg_info( " current version: %u\n", r
->current_run_version
);
552 if( timings
[begin
]->version
== r
->current_run_version
)
555 int valid_segment_count
= 0;
557 double lap_time
= 0.0;
559 for( u32 i
=0; i
<sj
; i
++ )
561 u32 j
= (sj
+begin
-i
-1) % sj
,
568 /* j1v should equal jv+1 */
569 if( timings
[j1
]->version
== timings
[j
]->version
+1 )
571 diff
= timings
[j1
]->time
- timings
[j
]->time
;
574 if( verified
&& diff
> 0.0 ) valid_segment_count
++;
581 vg_success( " [ %u %f ] %f\n", timings
[j1
]->time
,
582 timings
[j1
]->version
, diff
);
584 vg_warn( " [ %u %f ]\n", timings
[j1
]->time
, timings
[j1
]->version
);
587 pr
->ui
.fade_start
= pr
->ui
.segment_start
;
588 pr
->ui
.fade_count
= 0;
589 pr
->ui
.fade_timer_start
= r
->time
;
591 int orig_seg_count
= pr
->ui
.segment_count
;
593 world_routes_ui_newseg( route
);
597 world_routes_local_set_record( route
, lap_time
);
598 world_routes_ui_popfirst(route
);
599 pr
->ui
.fade_count
++;
602 vg_info( " ctime: %f\n", lap_time
);
604 /* remove any excess we had from previous runs */
605 int to_remove
= orig_seg_count
-valid_segment_count
;
606 for( int i
=0; i
<to_remove
; i
++ )
608 world_routes_ui_popfirst(route
);
609 pr
->ui
.fade_count
++;
612 r
->routes
[route
].latest_pass
= r
->time
;
616 * When going through a gate this is called for bookkeeping purposes
618 static void world_routes_activate_gate( u32 id
)
620 struct subworld_routes
*r
= &world
.routes
;
621 struct route_gate
*rg
= &r
->gates
[id
];
622 struct route_node
*pnode
= &r
->nodes
[rg
->node_id
],
623 *pdest
= &r
->nodes
[pnode
->next
[0]];
625 r
->last_use
= r
->time
;
627 struct route_collector
*rc
= &r
->collectors
[ pdest
->special_id
];
630 rg
->timing
.version
= r
->current_run_version
;
631 rg
->timing
.time
= r
->time
;
632 for( u32 i
=0; i
<r
->route_count
; i
++ )
634 struct route
*route
= &r
->routes
[i
];
636 int was_active
= route
->active
;
639 for( u32 j
=0; j
<pdest
->ref_count
; j
++ )
641 if( pdest
->route_ids
[j
] == i
)
643 world_routes_verify_run( i
);
649 if( was_active
&& !route
->active
)
651 route
->ui
.fade_start
= route
->ui
.segment_start
;
652 route
->ui
.fade_count
= route
->ui
.segment_count
;
653 route
->ui
.fade_timer_start
= r
->time
;
654 world_routes_ui_clear(i
);
656 vg_success( "CLEARING -> %u %u \n", route
->ui
.fade_start
,
657 route
->ui
.fade_count
);
661 r
->current_run_version
++;
663 rc
->timing
.version
= r
->current_run_version
;
664 rc
->timing
.time
= r
->time
;
665 r
->current_run_version
++;
669 * Notify the UI system that we've reset the player
671 static void world_routes_notify_reset(void)
673 struct subworld_routes
*r
= &world
.routes
;
674 r
->rewind_from
= r
->time
;
675 r
->rewind_to
= r
->last_use
;
678 for( int i
=0; i
<r
->route_count
; i
++ )
680 struct route
*route
= &r
->routes
[i
];
683 world_routes_ui_notch( i
, r
->time
- route
->latest_pass
);
688 static void world_routes_rollback_time( double t
)
690 struct subworld_routes
*r
= &world
.routes
;
691 r
->time
= vg_lerp( r
->rewind_to
, r
->rewind_from
, t
);
694 static void world_routes_debug(void)
696 struct subworld_routes
*r
= &world
.routes
;
698 for( int i
=0; i
<r
->node_count
; i
++ )
700 struct route_node
*rn
= &r
->nodes
[i
];
701 vg_line_pt3( rn
->co
, 1.0f
, rn
->special_type
? 0xffffff00: 0xff00b2ff );
704 for( int i
=0; i
<r
->route_count
; i
++ )
706 struct route
*route
= &r
->routes
[i
];
709 u32 si
= world_routes_get_path( route
->start
, stack
);
711 u32 colours
[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
714 u32 cc
= colours
[i
%vg_list_size(colours
)];
716 for( int sj
=0; sj
<si
; sj
++ )
719 debug_sbpath( &r
->nodes
[stack
[sj
]], &r
->nodes
[stack
[sk
]], cc
,
724 for( int i
=0; i
<r
->node_count
; i
++ )
726 struct route_node
*ri
= &r
->nodes
[i
],
729 for( int j
=0; j
<2; j
++ )
731 if( ri
->next
[j
] != 0xffffffff )
733 rj
= &r
->nodes
[ri
->next
[j
]];
734 vg_line( ri
->co
, rj
->co
, 0x20ffffff );
740 static void world_id_fixup( u32
*uid
, mdl_header
*mdl
)
743 *uid
= mdl_node_from_id( mdl
, *uid
)->sub_uid
;
748 static void world_routes_create_mesh( u32 route_id
)
750 struct subworld_routes
*r
= &world
.routes
;
751 struct route
*route
= &r
->routes
[ route_id
];
754 u32 si
= world_routes_get_path( route
->start
, stack
);
758 for( int sj
=0; sj
<si
; sj
++ )
762 struct route_node
*rnj
= &r
->nodes
[ stack
[sj
] ],
763 *rnk
= &r
->nodes
[ stack
[sk
] ],
766 if( rnj
->special_type
&& rnk
->special_type
)
772 float base_x0
= (float)rnj
->ref_count
*-0.5f
+ (float)rnj
->current_refs
,
773 base_x1
= (float)rnk
->ref_count
*-0.5f
+ (float)rnk
->current_refs
;
775 if( rnk
->special_type
)
777 rnl
= &r
->nodes
[ rnk
->next
[0] ];
778 base_x1
= (float)rnl
->ref_count
*-0.5f
+ (float)rnl
->current_refs
;
786 v3f p0
, h0
, p1
, h1
, p
, pd
;
788 v3_copy( rnj
->co
, p0
);
789 v3_muladds( rnj
->co
, rnj
->h
, 1.0f
, h0
);
790 v3_copy( rnk
->co
, p1
);
791 v3_muladds( rnk
->co
, rnk
->h
, -1.0f
, h1
);
796 for( int it
=0; it
<256; it
++ )
798 float const k_sample_dist
= 0.02f
;
799 eval_bezier_time( p0
,p1
,h0
,h1
, t
,p
);
800 eval_bezier_time( p0
,p1
,h0
,h1
, t
+k_sample_dist
,pd
);
802 float mod
= k_sample_dist
/ v3_dist( p
, pd
);
805 v3_muls( rnj
->up
, 1.0f
-t
, up
);
806 v3_muladds( up
, rnk
->up
, t
, up
);
809 v3_cross( up
, v0
, right
);
810 v3_normalize( right
);
812 float cur_x
= (1.0f
-t
)*base_x0
+ t
*base_x1
;
814 v3f sc
, sa
, sb
, down
;
815 v3_muladds( p
, right
, cur_x
, sc
);
816 v3_muladds( sc
, up
, 1.5f
, sc
);
817 v3_muladds( sc
, right
, 0.45f
, sa
);
818 v3_muladds( sc
, right
, -0.45f
, sb
);
819 v3_muls( up
, -1.0f
, down
);
824 if( ray_world( sa
, down
, &ha
) &&
825 ray_world( sb
, down
, &hb
))
829 v3_muladds( ha
.pos
, up
, 0.06f
, va
.co
);
830 v3_muladds( hb
.pos
, up
, 0.06f
, vb
.co
);
831 v3_copy( up
, va
.norm
);
832 v3_copy( up
, vb
.norm
);
836 scene_push_vert( &r
->scene_lines
, &va
);
837 scene_push_vert( &r
->scene_lines
, &vb
);
841 /* Connect them with triangles */
842 scene_push_tri( &r
->scene_lines
, (u32
[3]){
843 last_valid
+0-2, last_valid
+1-2, last_valid
+2-2} );
844 scene_push_tri( &r
->scene_lines
, (u32
[3]){
845 last_valid
+1-2, last_valid
+3-2, last_valid
+2-2} );
848 last_valid
= r
->scene_lines
.vertex_count
;
857 /* TODO special case for end of loop, need to add triangles
858 * between first and last rungs */
863 rnj
->current_refs
++;
866 scene_copy_slice( &r
->scene_lines
, &route
->sm
);
870 * Create the strips of colour that run through the world along course paths
872 static int world_routes_create_all_meshes(void)
874 vg_info( "Generating route meshes\n" );
876 struct subworld_routes
*r
= &world
.routes
;
877 scene_init( &r
->scene_lines
);
879 for( u32 i
=0; i
<r
->route_count
; i
++ )
880 world_routes_create_mesh( i
);
882 vg_acquire_thread_sync();
884 scene_upload( &r
->scene_lines
);
887 for( int i
=0; i
<r
->route_count
; i
++ )
890 struct route
*route
= &r
->routes
[i
];
892 glGenVertexArrays( 1, &route
->ui
.vao
);
893 glGenBuffers( 1, &route
->ui
.vbo
);
894 glGenBuffers( 1, &route
->ui
.ebo
);
895 glBindVertexArray( route
->ui
.vao
);
897 size_t stride
= sizeof(v2f
);
899 glBindBuffer( GL_ARRAY_BUFFER
, route
->ui
.vbo
);
900 glBufferData( GL_ARRAY_BUFFER
, k_route_ui_max_verts
*stride
,
901 NULL
, GL_DYNAMIC_DRAW
);
903 glBindVertexArray( route
->ui
.vao
);
904 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, route
->ui
.ebo
);
905 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
906 k_route_ui_max_indices
*sizeof(u16
), NULL
,
909 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, stride
, (void *)0 );
910 glEnableVertexAttribArray( 0 );
914 vg_release_thread_sync();
916 scene_free_offline_buffers( &r
->scene_lines
);
921 static void world_routes_loadfrom( mdl_header
*mdl
)
923 vg_info( "Initializing routes\n" );
925 struct subworld_routes
*r
= &world
.routes
;
927 r
->node_count
= 0; r
->node_cap
= 0; r
->routes
= NULL
;
934 /* TODO Break this up */
935 for( int i
=0; i
<mdl
->node_count
; i
++ )
937 mdl_node
*pnode
= mdl_node_from_id(mdl
,i
);
940 if( pnode
->classtype
== k_classtype_route_node
||
941 pnode
->classtype
== k_classtype_gate
)
943 mdl_node_transform( pnode
, transform
);
944 pnode
->sub_uid
= r
->node_count
;
946 r
->nodes
= buffer_reserve( r
->nodes
, r
->node_count
, &r
->node_cap
, 1,
947 sizeof( struct route_node
) );
949 struct route_node
*rn
= &r
->nodes
[r
->node_count
];
951 v3_copy( transform
[0], rn
->right
);
952 v3_normalize( rn
->right
);
953 v3_copy( transform
[1], rn
->up
);
954 v3_normalize( rn
->up
);
955 v3_muls( transform
[2], -1.0f
, rn
->h
);
956 v3_copy( transform
[3], rn
->co
);
958 rn
->current_refs
= 0;
959 rn
->special_type
= 0;
962 if( pnode
->classtype
== k_classtype_gate
)
964 struct classtype_gate
*inf
= mdl_get_entdata( mdl
, pnode
);
966 /* H is later scaled based on link distance */
967 v3_normalize( rn
->h
);
968 rn
->next
[0] = inf
->target
;
974 mdl_node
*pother
= mdl_node_from_id( mdl
, inf
->target
);
976 if( pother
->classtype
== k_classtype_gate
)
978 r
->gates
= buffer_reserve( r
->gates
, r
->gate_count
,
980 1, sizeof( struct route_gate
) );
982 struct route_gate
*rg
= &r
->gates
[r
->gate_count
];
983 rg
->node_id
= r
->node_count
;
984 rg
->timing
.time
= 0.0;
985 rg
->timing
.version
= 0;
987 v3_copy( pnode
->co
, rg
->gate
.co
[0] );
988 v3_copy( pother
->co
, rg
->gate
.co
[1] );
989 v4_copy( pnode
->q
, rg
->gate
.q
[0] );
990 v4_copy( pother
->q
, rg
->gate
.q
[1] );
991 v2_copy( inf
->dims
, rg
->gate
.dims
);
993 gate_transform_update( &rg
->gate
);
994 rn
->special_type
= k_route_special_type_gate
;
995 rn
->special_id
= r
->gate_count
;
1001 if( rn
->special_type
== 0 )
1003 r
->collectors
= buffer_reserve(
1004 r
->collectors
, r
->collector_count
, &r
->collector_cap
,
1005 1, sizeof( struct route_collector
));
1007 struct route_collector
*rc
= &r
->collectors
[r
->collector_count
];
1008 rc
->timing
.time
= 0.0;
1009 rc
->timing
.version
= 0;
1011 rn
->special_type
= k_route_special_type_collector
;
1012 rn
->special_id
= r
->collector_count
;
1014 r
->collector_count
++;
1019 struct classtype_route_node
*inf
= mdl_get_entdata( mdl
, pnode
);
1020 rn
->next
[0] = inf
->target
;
1021 rn
->next
[1] = inf
->target1
;
1026 else if( pnode
->classtype
== k_classtype_route
)
1028 struct classtype_route
*inf
= mdl_get_entdata( mdl
, pnode
);
1029 r
->routes
= buffer_reserve( r
->routes
, r
->route_count
, &r
->route_cap
,
1030 1, sizeof( struct route
) );
1032 struct route
*route
= &r
->routes
[r
->route_count
];
1033 memset( route
, 0, sizeof(struct route
) );
1035 v3_copy( inf
->colour
, route
->colour
);
1036 route
->colour
[3] = 1.0f
;
1039 route
->track_id
= 0xffffffff;
1040 for( u32 j
=0; j
<vg_list_size(track_infos
); j
++ )
1042 if( !strcmp( mdl_pstr(mdl
,pnode
->pstr_name
), track_infos
[j
].name
))
1044 route
->track_id
= j
;
1049 route
->start
= inf
->id_start
;
1051 route
->factive
= 0.0f
;
1052 mdl_node_transform( pnode
, route
->scoreboard_transform
);
1054 route
->ui
.indices_head
= k_route_ui_max_indices
- 9;
1055 route
->ui
.vertex_head
= k_route_ui_max_verts
- 200;
1056 route
->ui
.segment_start
= 0;
1057 route
->ui
.segment_count
= 0;
1058 route
->ui
.last_notch
= 0.0;
1059 route
->ui
.fade_start
= 0;
1060 route
->ui
.fade_count
= 0;
1061 route
->ui
.fade_timer_start
= 0.0;
1068 * Apply correct system-local ids
1070 for( int i
=0; i
<r
->node_count
; i
++ )
1072 struct route_node
*rn
= &r
->nodes
[i
];
1074 for( int j
=0; j
<2; j
++ )
1075 world_id_fixup( &rn
->next
[j
], mdl
);
1078 for( int i
=0; i
<r
->route_count
; i
++ )
1080 struct route
*route
= &r
->routes
[i
];
1081 world_id_fixup( &route
->start
, mdl
);
1087 for( int i
=0; i
<r
->route_count
; i
++ )
1089 struct route
*route
= &r
->routes
[i
];
1092 u32 si
= world_routes_get_path( route
->start
, stack
);
1094 for( int sj
=0; sj
<si
; sj
++ )
1096 struct route_node
*rn
= &r
->nodes
[ stack
[sj
] ];
1097 rn
->route_ids
[ rn
->ref_count
++ ] = i
;
1099 if( rn
->ref_count
> 4 )
1100 vg_warn( "Too many references on route node %i\n", i
);
1104 world_routes_create_all_meshes();
1108 * -----------------------------------------------------------------------------
1110 * -----------------------------------------------------------------------------
1113 static void world_routes_init(void)
1115 struct subworld_routes
*r
= &world
.routes
;
1116 r
->current_run_version
= 2;
1117 r
->time
= RESET_MAX_TIME
*2.0;
1120 shader_route_register();
1121 shader_routeui_register();
1124 static void world_routes_free(void*_
)
1126 struct subworld_routes
*r
= &world
.routes
;
1128 vg_free( r
->nodes
);
1129 vg_free( r
->routes
);
1130 vg_free( r
->gates
);
1133 static void world_routes_update(void)
1135 struct subworld_routes
*r
= &world
.routes
;
1136 r
->time
+= vg
.time_delta
;
1138 for( int i
=0; i
<r
->route_count
; i
++ )
1140 struct route
*route
= &r
->routes
[i
];
1141 route
->factive
= vg_lerpf( route
->factive
, route
->active
,
1142 0.6f
*vg
.time_delta
);
1146 world_routes_ui_updatetime( i
, r
->time
- route
->latest_pass
);
1151 static void bind_terrain_textures(void);
1152 static void render_world_routes( m4x4f projection
, v3f camera
)
1154 struct subworld_routes
*r
= &world
.routes
;
1156 m4x3f identity_matrix
;
1157 m4x3_identity( identity_matrix
);
1160 shader_route_uTexGarbage(0);
1161 shader_link_standard_ub( _shader_route
.id
, 2 );
1162 bind_terrain_textures();
1164 shader_route_uPv( projection
);
1165 shader_route_uMdl( identity_matrix
);
1166 shader_route_uCamera( camera
);
1168 scene_bind( &r
->scene_lines
);
1170 for( int i
=0; i
<r
->route_count
; i
++ )
1172 struct route
*route
= &r
->routes
[i
];
1175 v3_lerp( (v3f
){0.7f
,0.7f
,0.7f
}, route
->colour
, route
->factive
, colour
);
1178 shader_route_uColour( colour
);
1179 mdl_draw_submesh( &route
->sm
);
1183 static void render_world_routes_ui(void)
1186 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1187 glBlendEquation(GL_FUNC_ADD
);
1189 struct subworld_routes
*r
= &world
.routes
;
1191 float active_offset
= 0.0f
;
1192 for( int i
=0; i
<r
->route_count
; i
++ )
1194 struct route
*route
= &r
->routes
[i
];
1195 world_routes_ui_draw( i
, route
->colour
, active_offset
);
1196 active_offset
+= route
->factive
;
1199 glDisable(GL_BLEND
);
1202 #endif /* ROUTES_H */