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 world_routes_interact(void)
24 world
.routes
.last_interaction
= vg
.time
;
27 static void debug_sbpath( struct route_node
*rna
, struct route_node
*rnb
,
28 u32 colour
, float xoffset
)
30 v3f p0
, h0
, p1
, h1
, l
, p
;
32 v3_copy( rna
->co
, p0
);
33 v3_muladds( rna
->co
, rna
->h
, 1.0f
, h0
);
34 v3_copy( rnb
->co
, p1
);
35 v3_muladds( rnb
->co
, rnb
->h
, -1.0f
, h1
);
37 v3_muladds( p0
, rna
->right
, xoffset
, p0
);
38 v3_muladds( h0
, rna
->right
, xoffset
, h0
);
39 v3_muladds( p1
, rnb
->right
, xoffset
, p1
);
40 v3_muladds( h1
, rnb
->right
, xoffset
, h1
);
44 for( int i
=0; i
<5; i
++ )
46 float t
= (float)(i
+1)/5.0f
;
47 eval_bezier_time( p0
, p1
, h0
, h1
, t
, p
);
48 vg_line( p
, l
, colour
);
54 * Get a list of node ids in stack, and return how many there is
56 static u32
world_routes_get_path( u32 starter
, u32 stack
[64] )
58 struct subworld_routes
*r
= &world
.routes
;
65 int loop_complete
= 0;
69 if( stack_i
[si
-1] == 2 )
75 struct route_node
*rn
= &r
->nodes
[stack
[si
-1]];
76 u32 nextid
= rn
->next
[stack_i
[si
-1]];
79 if( nextid
!= 0xffffffff )
81 if( nextid
== stack
[0] )
88 for( int sj
=0; sj
<si
; sj
++ )
90 if( stack
[sj
] == nextid
)
114 * Free a segment from the UI bar to be reused later
116 static void world_routes_ui_popfirst( u32 route
)
118 struct subworld_routes
*r
= &world
.routes
;
119 struct route
*pr
= &r
->routes
[route
];
121 if( pr
->ui
.segment_count
)
123 pr
->ui
.segment_start
++;
125 if( pr
->ui
.segment_start
== 32 )
126 pr
->ui
.segment_start
= 0;
128 pr
->ui
.segment_count
--;
133 * Reset ui bar completely
135 static void world_routes_ui_clear( u32 route
)
137 struct subworld_routes
*r
= &world
.routes
;
138 struct route
*pr
= &r
->routes
[route
];
139 pr
->ui
.segment_start
= (pr
->ui
.segment_start
+ pr
->ui
.segment_count
) %
141 pr
->ui
.segment_count
= 0;
145 * Break a index range into two pieces over the edge of the maximum it can
146 * store. s1 is 0 always, so its a ring buffer.
148 static void world_routes_ui_split_indices( u32 s0
, u32 count
, u32
*c0
, u32
*c1
)
150 *c0
= (VG_MIN( s0
+count
, k_route_ui_max_indices
)) - s0
;
155 * Place a set of indices into gpu array automatically splits
158 static void world_routes_ui_set_indices( struct route
*pr
,
159 u16
*indices
, u32 count
)
162 world_routes_ui_split_indices( pr
->ui
.indices_head
, count
, &c0
, &c1
);
164 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, pr
->ui
.ebo
);
168 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, pr
->ui
.indices_head
*sizeof(u16
),
169 c0
*sizeof(u16
), indices
);
174 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, 0, c1
*sizeof(u16
), indices
+c0
);
175 pr
->ui
.indices_head
= c1
;
178 pr
->ui
.indices_head
+= c0
;
182 * Place a set of vertices into gpu array
184 static u32
world_routes_ui_set_verts( struct route
*pr
, v2f
*verts
, u32 count
)
186 if( pr
->ui
.vertex_head
+ count
>= k_route_ui_max_verts
)
187 pr
->ui
.vertex_head
= 0;
189 u32 vert_start
= pr
->ui
.vertex_head
;
190 pr
->ui
.vertex_head
+= count
;
192 glBindBuffer( GL_ARRAY_BUFFER
, pr
->ui
.vbo
);
193 glBufferSubData( GL_ARRAY_BUFFER
, (GLintptr
)(vert_start
*sizeof(v2f
)),
194 sizeof(v2f
)*count
, verts
);
200 * Update the last (count) vertices positions, does not add any.
201 * Data must already be written to, and not cross either array boundaries.
203 static u32
world_routes_ui_update_verts( struct route
*pr
,
204 v2f
*verts
, u32 count
)
206 u32 vert_start
= pr
->ui
.vertex_head
-count
;
208 glBindBuffer( GL_ARRAY_BUFFER
, pr
->ui
.vbo
);
209 glBufferSubData( GL_ARRAY_BUFFER
, (GLintptr
)(vert_start
*sizeof(v2f
)),
210 sizeof(v2f
)*count
, verts
);
216 * Current/active segment of this UI bar
218 static struct route_ui_segment
*world_routes_ui_curseg( struct route
*pr
)
220 u32 index
= (pr
->ui
.segment_start
+pr
->ui
.segment_count
-1)%k_max_ui_segments
;
221 return &pr
->ui
.segments
[ index
];
225 * Start a new segment in the UI bar, will create a split on the last one if
226 * there is one active currently. (api)
228 static void world_routes_ui_newseg( u32 route
)
230 struct subworld_routes
*r
= &world
.routes
;
231 struct route
*pr
= &r
->routes
[route
];
233 pr
->ui
.last_notch
= 0.0;
235 glBindVertexArray( pr
->ui
.vao
);
236 if( pr
->ui
.segment_count
)
238 float const k_gap_width
= 1.0f
;
240 struct route_ui_segment
*cseg
= world_routes_ui_curseg(pr
);
243 verts
[0][0] = cseg
->length
-k_gap_width
;
245 verts
[1][0] = cseg
->length
-k_gap_width
;
248 world_routes_ui_update_verts( pr
, verts
, 2 );
251 pr
->ui
.segment_count
++;
252 struct route_ui_segment
*segment
= world_routes_ui_curseg(pr
);
264 u32 vert_start
= world_routes_ui_set_verts( pr
, verts
, 4 );
267 indices
[0] = vert_start
+ 0;
268 indices
[1] = vert_start
+ 1;
269 indices
[2] = vert_start
+ 3;
270 indices
[3] = vert_start
+ 0;
271 indices
[4] = vert_start
+ 3;
272 indices
[5] = vert_start
+ 2;
274 segment
->vertex_start
= vert_start
;
275 segment
->vertex_count
= 4;
276 segment
->index_start
= pr
->ui
.indices_head
;
277 segment
->index_count
= 6;
278 segment
->notches
= 0;
280 world_routes_ui_set_indices( pr
, indices
, 6 );
284 * Extend the end of the bar
286 static void world_routes_ui_updatetime( u32 route
, float time
)
288 struct subworld_routes
*r
= &world
.routes
;
289 struct route
*pr
= &r
->routes
[route
];
297 u32 vert_start
= pr
->ui
.vertex_head
-2;
299 glBindVertexArray( pr
->ui
.vao
);
300 world_routes_ui_update_verts( pr
, verts
, 2 );
302 struct route_ui_segment
*cseg
= world_routes_ui_curseg(pr
);
307 * Create a notch in the bar, used when a reset is triggered by the user
309 static void world_routes_ui_notch( u32 route
, float time
)
311 struct subworld_routes
*r
= &world
.routes
;
312 struct route
*pr
= &r
->routes
[route
];
314 if( (time
- pr
->ui
.last_notch
) > 1.0 )
316 struct route_ui_segment
*segment
= world_routes_ui_curseg(pr
);
317 if( segment
->notches
== k_max_ui_splits_per_segment
)
324 float const k_notch_width
= 1.0f
;
326 float xa
= time
-k_notch_width
,
327 xb
= time
-k_notch_width
* 0.5f
,
338 verts
[3][1] = -0.25f
;
350 glBindVertexArray( pr
->ui
.vao
);
351 u32 vert_start_mod
= world_routes_ui_update_verts( pr
, verts
, 2 ),
352 vert_start_new
= world_routes_ui_set_verts( pr
, verts
+2, 6 );
355 indices
[ 0] = vert_start_mod
+1;
356 indices
[ 1] = vert_start_new
+0;
357 indices
[ 2] = vert_start_mod
+0;
358 indices
[ 3] = vert_start_mod
+1;
359 indices
[ 4] = vert_start_new
+1;
360 indices
[ 5] = vert_start_new
+0;
362 indices
[ 6] = vert_start_new
+0;
363 indices
[ 7] = vert_start_new
+1;
364 indices
[ 8] = vert_start_new
+3;
365 indices
[ 9] = vert_start_new
+0;
366 indices
[10] = vert_start_new
+3;
367 indices
[11] = vert_start_new
+2;
369 indices
[12] = vert_start_new
+3;
370 indices
[13] = vert_start_new
+4;
371 indices
[14] = vert_start_new
+2;
372 indices
[15] = vert_start_new
+3;
373 indices
[16] = vert_start_new
+5;
374 indices
[17] = vert_start_new
+4;
376 world_routes_ui_set_indices( pr
, indices
, 18 );
378 pr
->ui
.last_notch
= time
;
380 segment
->vertex_count
+= 6;
381 segment
->index_count
+= 18;
385 static void world_routes_ui_draw_segment( struct route_ui_segment
*segment
)
388 world_routes_ui_split_indices( segment
->index_start
,
389 segment
->index_count
, &c0
, &c1
);
391 glDrawElements( GL_TRIANGLES
, c0
, GL_UNSIGNED_SHORT
,
392 (void *)(segment
->index_start
*sizeof(u16
)));
394 glDrawElements( GL_TRIANGLES
, c1
, GL_UNSIGNED_SHORT
, (void *)(0) );
398 * Draws full bar at Y offset(offset).
400 static void world_routes_ui_draw( u32 route
, v4f colour
, float offset
)
402 float const k_bar_height
= 0.05f
,
403 k_bar_scale_x
= 0.005f
;
405 struct subworld_routes
*r
= &world
.routes
;
406 struct route
*pr
= &r
->routes
[route
];
408 float cx
= pr
->ui
.xpos
;
410 shader_routeui_use();
411 glBindVertexArray( pr
->ui
.vao
);
413 float fade_amt
= vg
.time
- pr
->ui
.fade_timer_start
;
414 fade_amt
= vg_clampf( fade_amt
/ 1.0f
, 0.0f
, 1.0f
);
416 float fade_block_size
= 0.0f
,
417 main_block_size
= 0.0f
;
419 for( u32 i
=0; i
<pr
->ui
.fade_count
; i
++ )
421 u32 j
= (pr
->ui
.fade_start
+ i
) % k_max_ui_segments
;
422 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
424 fade_block_size
+= segment
->length
;
427 cx
-= fade_block_size
* fade_amt
;
430 v4_copy( colour
, fade_colour
);
431 fade_colour
[3] *= 1.0f
-fade_amt
;
434 float timer_delta
= (vg
.time
- world
.routes
.last_interaction
) * (1.0/60.0),
435 timer_scale
= 1.0f
- vg_minf( timer_delta
, 1.0f
);
441 float height
= pr
->factive
*k_bar_height
* timer_scale
,
442 base
= -1.0f
+ (offset
+0.5f
)*k_bar_height
* timer_scale
;
444 shader_routeui_uColour( fade_colour
);
445 for( u32 i
=0; i
<pr
->ui
.fade_count
; i
++ )
447 u32 j
= (pr
->ui
.fade_start
+ i
) % k_max_ui_segments
;
448 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
450 shader_routeui_uOffset( (v4f
){ cx
*k_bar_scale_x
, base
,
451 k_bar_scale_x
, height
} );
453 world_routes_ui_draw_segment( segment
);
454 cx
+= segment
->length
;
460 shader_routeui_uColour( colour
);
461 for( u32 i
=0; i
<pr
->ui
.segment_count
; i
++ )
463 u32 j
= (pr
->ui
.segment_start
+ i
) % k_max_ui_segments
;
464 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
466 shader_routeui_uOffset( (v4f
){ cx
*k_bar_scale_x
, base
,
467 k_bar_scale_x
, height
} );
469 world_routes_ui_draw_segment( segment
);
470 cx
+= segment
->length
;
472 main_block_size
+= segment
->length
;
475 pr
->ui
.xpos
= vg_lerpf( pr
->ui
.xpos
, -main_block_size
* 0.5f
, 0.03f
);
478 static void world_routes_local_set_record( u32 route
, double lap_time
)
480 vg_success( " NEW LAP TIME: %f\n", lap_time
);
482 struct subworld_routes
*r
= &world
.routes
;
483 struct route
*pr
= &r
->routes
[route
];
485 if( pr
->track_id
!= 0xffffffff )
487 double time_centiseconds
= lap_time
* 100.0;
488 if( time_centiseconds
> (float)0xfffe )
491 highscore_record temp
;
492 temp
.trackid
= pr
->track_id
;
493 temp
.datetime
= time(NULL
);
496 temp
.time
= time_centiseconds
;
498 highscores_push_record( &temp
);
499 track_infos
[ pr
->track_id
].push
= 1;
503 vg_warn( "There is no associated track for this record...\n" );
508 * Will scan the whole run for two things;
509 * 1: we set a new record for the total, complete loop around the course
510 * 2: the time of each segment will be recorded into the data buffer
511 * (not implemented: TODO)
513 static void world_routes_verify_run( u32 route
)
515 struct subworld_routes
*r
= &world
.routes
;
516 struct route
*pr
= &r
->routes
[route
];
519 u32 si
= world_routes_get_path( r
->routes
[route
].start
, stack
);
522 * we only care about gates that ref gates, so shuffle down the array
524 struct route_timing
*timings
[64];
525 u32 sj
= 0, maxv
= 0, begin
= 0;
526 for( u32 i
=0; i
<si
; i
++ )
528 if( r
->nodes
[stack
[i
]].special_type
== k_route_special_type_collector
)
529 timings
[sj
++] = &r
->collectors
[r
->nodes
[stack
[i
]].special_id
].timing
;
530 else if( r
->nodes
[stack
[i
]].special_type
== k_route_special_type_gate
)
531 timings
[sj
++] = &r
->gates
[r
->nodes
[stack
[i
]].special_id
].timing
;
534 for( u32 i
=0; i
<sj
; i
++ )
536 if( timings
[i
]->version
> maxv
)
538 maxv
= timings
[i
]->version
;
543 vg_info( "== begin verification (%u) ==\n", route
);
544 vg_info( " current version: %u\n", r
->current_run_version
);
547 if( timings
[begin
]->version
== r
->current_run_version
)
550 int valid_segment_count
= 0;
552 double lap_time
= 0.0;
554 for( u32 i
=0; i
<sj
; i
++ )
556 u32 j
= (sj
+begin
-i
-1) % sj
,
563 /* j1v should equal jv+1 */
564 if( timings
[j1
]->version
== timings
[j
]->version
+1 )
566 diff
= timings
[j1
]->time
- timings
[j
]->time
;
569 if( verified
&& diff
> 0.0 ) valid_segment_count
++;
576 vg_success( " [ %u %f ] %f\n", timings
[j1
]->time
,
577 timings
[j1
]->version
, diff
);
579 vg_warn( " [ %u %f ]\n", timings
[j1
]->time
, timings
[j1
]->version
);
582 pr
->ui
.fade_start
= pr
->ui
.segment_start
;
583 pr
->ui
.fade_count
= 0;
584 pr
->ui
.fade_timer_start
= vg
.time
;
586 int orig_seg_count
= pr
->ui
.segment_count
;
588 world_routes_ui_newseg( route
);
592 world_routes_local_set_record( route
, lap_time
);
593 world_routes_ui_popfirst(route
);
594 pr
->ui
.fade_count
++;
597 vg_info( " ctime: %f\n", lap_time
);
599 /* remove any excess we had from previous runs */
600 int to_remove
= orig_seg_count
-valid_segment_count
;
601 for( int i
=0; i
<to_remove
; i
++ )
603 world_routes_ui_popfirst(route
);
604 pr
->ui
.fade_count
++;
607 r
->routes
[route
].latest_pass
= vg
.time
;
611 * When going through a gate this is called for bookkeeping purposes
613 static void world_routes_activate_gate( u32 id
)
615 world_routes_interact();
617 struct subworld_routes
*r
= &world
.routes
;
618 struct route_gate
*rg
= &r
->gates
[id
];
619 struct route_node
*pnode
= &r
->nodes
[rg
->node_id
],
620 *pdest
= &r
->nodes
[pnode
->next
[0]];
622 struct route_collector
*rc
= &r
->collectors
[ pdest
->special_id
];
625 rg
->timing
.version
= r
->current_run_version
;
626 rg
->timing
.time
= vg
.time
;
627 for( u32 i
=0; i
<r
->route_count
; i
++ )
629 struct route
*route
= &r
->routes
[i
];
631 int was_active
= route
->active
;
634 for( u32 j
=0; j
<pdest
->ref_count
; j
++ )
636 if( pdest
->route_ids
[j
] == i
)
638 world_routes_verify_run( i
);
644 if( was_active
&& !route
->active
)
646 route
->ui
.fade_start
= route
->ui
.segment_start
;
647 route
->ui
.fade_count
= route
->ui
.segment_count
;
648 route
->ui
.fade_timer_start
= vg
.time
;
649 world_routes_ui_clear(i
);
651 vg_success( "CLEARING -> %u %u \n", route
->ui
.fade_start
,
652 route
->ui
.fade_count
);
656 r
->current_run_version
++;
658 rc
->timing
.version
= r
->current_run_version
;
659 rc
->timing
.time
= vg
.time
;
660 r
->current_run_version
++;
664 * Notify the UI system that we've reset the player
666 static void world_routes_notify_reset(void)
668 struct subworld_routes
*r
= &world
.routes
;
669 world_routes_interact();
671 for( int i
=0; i
<r
->route_count
; i
++ )
673 struct route
*route
= &r
->routes
[i
];
676 world_routes_ui_notch( i
, vg
.time
- route
->latest_pass
);
680 static void world_routes_debug(void)
682 struct subworld_routes
*r
= &world
.routes
;
684 for( int i
=0; i
<r
->node_count
; i
++ )
686 struct route_node
*rn
= &r
->nodes
[i
];
687 vg_line_pt3( rn
->co
, 1.0f
, rn
->special_type
? 0xffffff00: 0xff00b2ff );
690 for( int i
=0; i
<r
->route_count
; i
++ )
692 struct route
*route
= &r
->routes
[i
];
695 u32 si
= world_routes_get_path( route
->start
, stack
);
697 u32 colours
[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
700 u32 cc
= colours
[i
%vg_list_size(colours
)];
702 for( int sj
=0; sj
<si
; sj
++ )
705 debug_sbpath( &r
->nodes
[stack
[sj
]], &r
->nodes
[stack
[sk
]], cc
,
710 for( int i
=0; i
<r
->node_count
; i
++ )
712 struct route_node
*ri
= &r
->nodes
[i
],
715 for( int j
=0; j
<2; j
++ )
717 if( ri
->next
[j
] != 0xffffffff )
719 rj
= &r
->nodes
[ri
->next
[j
]];
720 vg_line( ri
->co
, rj
->co
, 0x20ffffff );
726 static void world_id_fixup( u32
*uid
, mdl_header
*mdl
)
729 *uid
= mdl_node_from_id( mdl
, *uid
)->sub_uid
;
734 static void world_routes_create_mesh( u32 route_id
)
736 struct subworld_routes
*r
= &world
.routes
;
737 struct route
*route
= &r
->routes
[ route_id
];
740 u32 si
= world_routes_get_path( route
->start
, stack
);
744 for( int sj
=0; sj
<si
; sj
++ )
748 struct route_node
*rnj
= &r
->nodes
[ stack
[sj
] ],
749 *rnk
= &r
->nodes
[ stack
[sk
] ],
752 if( rnj
->special_type
&& rnk
->special_type
)
758 float base_x0
= (float)rnj
->ref_count
*-0.5f
+ (float)rnj
->current_refs
,
759 base_x1
= (float)rnk
->ref_count
*-0.5f
+ (float)rnk
->current_refs
;
761 if( rnk
->special_type
)
763 rnl
= &r
->nodes
[ rnk
->next
[0] ];
764 base_x1
= (float)rnl
->ref_count
*-0.5f
+ (float)rnl
->current_refs
;
772 v3f p0
, h0
, p1
, h1
, p
, pd
;
774 v3_copy( rnj
->co
, p0
);
775 v3_muladds( rnj
->co
, rnj
->h
, 1.0f
, h0
);
776 v3_copy( rnk
->co
, p1
);
777 v3_muladds( rnk
->co
, rnk
->h
, -1.0f
, h1
);
782 for( int it
=0; it
<256; it
++ )
784 float const k_sample_dist
= 0.02f
;
785 eval_bezier_time( p0
,p1
,h0
,h1
, t
,p
);
786 eval_bezier_time( p0
,p1
,h0
,h1
, t
+k_sample_dist
,pd
);
788 float mod
= k_sample_dist
/ v3_dist( p
, pd
);
791 v3_muls( rnj
->up
, 1.0f
-t
, up
);
792 v3_muladds( up
, rnk
->up
, t
, up
);
795 v3_cross( up
, v0
, right
);
796 v3_normalize( right
);
798 float cur_x
= (1.0f
-t
)*base_x0
+ t
*base_x1
;
800 v3f sc
, sa
, sb
, down
;
801 v3_muladds( p
, right
, cur_x
, sc
);
802 v3_muladds( sc
, up
, 1.5f
, sc
);
803 v3_muladds( sc
, right
, 0.45f
, sa
);
804 v3_muladds( sc
, right
, -0.45f
, sb
);
805 v3_muls( up
, -1.0f
, down
);
810 if( ray_world( sa
, down
, &ha
) &&
811 ray_world( sb
, down
, &hb
))
815 v3_muladds( ha
.pos
, up
, 0.06f
, va
.co
);
816 v3_muladds( hb
.pos
, up
, 0.06f
, vb
.co
);
817 v3_copy( up
, va
.norm
);
818 v3_copy( up
, vb
.norm
);
822 scene_push_vert( &r
->scene_lines
, &va
);
823 scene_push_vert( &r
->scene_lines
, &vb
);
827 /* Connect them with triangles */
828 scene_push_tri( &r
->scene_lines
, (u32
[3]){
829 last_valid
+0-2, last_valid
+1-2, last_valid
+2-2} );
830 scene_push_tri( &r
->scene_lines
, (u32
[3]){
831 last_valid
+1-2, last_valid
+3-2, last_valid
+2-2} );
834 last_valid
= r
->scene_lines
.vertex_count
;
843 /* TODO special case for end of loop, need to add triangles
844 * between first and last rungs */
849 rnj
->current_refs
++;
852 scene_copy_slice( &r
->scene_lines
, &route
->sm
);
856 * Create the strips of colour that run through the world along course paths
858 static int world_routes_create_all_meshes(void)
860 vg_info( "Generating route meshes\n" );
862 struct subworld_routes
*r
= &world
.routes
;
863 scene_init( &r
->scene_lines
);
865 for( u32 i
=0; i
<r
->route_count
; i
++ )
866 world_routes_create_mesh( i
);
868 vg_acquire_thread_sync();
870 scene_upload( &r
->scene_lines
);
873 for( int i
=0; i
<r
->route_count
; i
++ )
876 struct route
*route
= &r
->routes
[i
];
878 glGenVertexArrays( 1, &route
->ui
.vao
);
879 glGenBuffers( 1, &route
->ui
.vbo
);
880 glGenBuffers( 1, &route
->ui
.ebo
);
881 glBindVertexArray( route
->ui
.vao
);
883 size_t stride
= sizeof(v2f
);
885 glBindBuffer( GL_ARRAY_BUFFER
, route
->ui
.vbo
);
886 glBufferData( GL_ARRAY_BUFFER
, k_route_ui_max_verts
*stride
,
887 NULL
, GL_DYNAMIC_DRAW
);
889 glBindVertexArray( route
->ui
.vao
);
890 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, route
->ui
.ebo
);
891 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
892 k_route_ui_max_indices
*sizeof(u16
), NULL
,
895 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, stride
, (void *)0 );
896 glEnableVertexAttribArray( 0 );
900 vg_release_thread_sync();
902 scene_free_offline_buffers( &r
->scene_lines
);
907 static void world_routes_loadfrom( mdl_header
*mdl
)
909 vg_info( "Initializing routes\n" );
911 struct subworld_routes
*r
= &world
.routes
;
913 r
->node_count
= 0; r
->node_cap
= 0; r
->routes
= NULL
;
920 /* TODO Break this up */
921 for( int i
=0; i
<mdl
->node_count
; i
++ )
923 mdl_node
*pnode
= mdl_node_from_id(mdl
,i
);
926 if( pnode
->classtype
== k_classtype_route_node
||
927 pnode
->classtype
== k_classtype_gate
)
929 mdl_node_transform( pnode
, transform
);
930 pnode
->sub_uid
= r
->node_count
;
932 r
->nodes
= buffer_reserve( r
->nodes
, r
->node_count
, &r
->node_cap
, 1,
933 sizeof( struct route_node
) );
935 struct route_node
*rn
= &r
->nodes
[r
->node_count
];
937 v3_copy( transform
[0], rn
->right
);
938 v3_normalize( rn
->right
);
939 v3_copy( transform
[1], rn
->up
);
940 v3_normalize( rn
->up
);
941 v3_muls( transform
[2], -1.0f
, rn
->h
);
942 v3_copy( transform
[3], rn
->co
);
944 rn
->current_refs
= 0;
945 rn
->special_type
= 0;
948 if( pnode
->classtype
== k_classtype_gate
)
950 struct classtype_gate
*inf
= mdl_get_entdata( mdl
, pnode
);
952 /* H is later scaled based on link distance */
953 v3_normalize( rn
->h
);
954 rn
->next
[0] = inf
->target
;
960 mdl_node
*pother
= mdl_node_from_id( mdl
, inf
->target
);
962 if( pother
->classtype
== k_classtype_gate
)
964 r
->gates
= buffer_reserve( r
->gates
, r
->gate_count
,
966 1, sizeof( struct route_gate
) );
968 struct route_gate
*rg
= &r
->gates
[r
->gate_count
];
969 rg
->node_id
= r
->node_count
;
970 rg
->timing
.time
= 0.0;
971 rg
->timing
.version
= 0;
973 v3_copy( pnode
->co
, rg
->gate
.co
[0] );
974 v3_copy( pother
->co
, rg
->gate
.co
[1] );
975 v4_copy( pnode
->q
, rg
->gate
.q
[0] );
976 v4_copy( pother
->q
, rg
->gate
.q
[1] );
977 v2_copy( inf
->dims
, rg
->gate
.dims
);
979 gate_transform_update( &rg
->gate
);
980 rn
->special_type
= k_route_special_type_gate
;
981 rn
->special_id
= r
->gate_count
;
987 if( rn
->special_type
== 0 )
989 r
->collectors
= buffer_reserve(
990 r
->collectors
, r
->collector_count
, &r
->collector_cap
,
991 1, sizeof( struct route_collector
));
993 struct route_collector
*rc
= &r
->collectors
[r
->collector_count
];
994 rc
->timing
.time
= 0.0;
995 rc
->timing
.version
= 0;
997 rn
->special_type
= k_route_special_type_collector
;
998 rn
->special_id
= r
->collector_count
;
1000 r
->collector_count
++;
1005 struct classtype_route_node
*inf
= mdl_get_entdata( mdl
, pnode
);
1006 rn
->next
[0] = inf
->target
;
1007 rn
->next
[1] = inf
->target1
;
1012 else if( pnode
->classtype
== k_classtype_route
)
1014 struct classtype_route
*inf
= mdl_get_entdata( mdl
, pnode
);
1015 r
->routes
= buffer_reserve( r
->routes
, r
->route_count
, &r
->route_cap
,
1016 1, sizeof( struct route
) );
1018 struct route
*route
= &r
->routes
[r
->route_count
];
1019 memset( route
, 0, sizeof(struct route
) );
1021 v3_copy( inf
->colour
, route
->colour
);
1022 route
->colour
[3] = 1.0f
;
1025 route
->track_id
= 0xffffffff;
1026 for( u32 j
=0; j
<vg_list_size(track_infos
); j
++ )
1028 if( !strcmp( mdl_pstr(mdl
,pnode
->pstr_name
), track_infos
[j
].name
))
1030 route
->track_id
= j
;
1035 route
->start
= inf
->id_start
;
1037 route
->factive
= 0.0f
;
1038 mdl_node_transform( pnode
, route
->scoreboard_transform
);
1040 route
->ui
.indices_head
= k_route_ui_max_indices
- 9;
1041 route
->ui
.vertex_head
= k_route_ui_max_verts
- 200;
1042 route
->ui
.segment_start
= 0;
1043 route
->ui
.segment_count
= 0;
1044 route
->ui
.last_notch
= 0.0;
1045 route
->ui
.fade_start
= 0;
1046 route
->ui
.fade_count
= 0;
1047 route
->ui
.fade_timer_start
= 0.0;
1054 * Apply correct system-local ids
1056 for( int i
=0; i
<r
->node_count
; i
++ )
1058 struct route_node
*rn
= &r
->nodes
[i
];
1060 for( int j
=0; j
<2; j
++ )
1061 world_id_fixup( &rn
->next
[j
], mdl
);
1064 for( int i
=0; i
<r
->route_count
; i
++ )
1066 struct route
*route
= &r
->routes
[i
];
1067 world_id_fixup( &route
->start
, mdl
);
1073 for( int i
=0; i
<r
->route_count
; i
++ )
1075 struct route
*route
= &r
->routes
[i
];
1078 u32 si
= world_routes_get_path( route
->start
, stack
);
1080 for( int sj
=0; sj
<si
; sj
++ )
1082 struct route_node
*rn
= &r
->nodes
[ stack
[sj
] ];
1083 rn
->route_ids
[ rn
->ref_count
++ ] = i
;
1085 if( rn
->ref_count
> 4 )
1086 vg_warn( "Too many references on route node %i\n", i
);
1090 world_routes_create_all_meshes();
1094 * -----------------------------------------------------------------------------
1096 * -----------------------------------------------------------------------------
1099 static void world_routes_init(void)
1101 struct subworld_routes
*r
= &world
.routes
;
1102 r
->current_run_version
= 2;
1104 shader_route_register();
1105 shader_routeui_register();
1108 static void world_routes_free(void*_
)
1110 struct subworld_routes
*r
= &world
.routes
;
1112 vg_free( r
->nodes
);
1113 vg_free( r
->routes
);
1114 vg_free( r
->gates
);
1117 static void world_routes_update(void)
1119 struct subworld_routes
*r
= &world
.routes
;
1121 for( int i
=0; i
<r
->route_count
; i
++ )
1123 struct route
*route
= &r
->routes
[i
];
1124 route
->factive
= vg_lerpf( route
->factive
, route
->active
,
1125 0.6f
*vg
.time_delta
);
1129 world_routes_ui_updatetime( i
, vg
.time
- route
->latest_pass
);
1134 static void bind_terrain_textures(void);
1135 static void render_world_routes( m4x4f projection
, v3f camera
)
1137 struct subworld_routes
*r
= &world
.routes
;
1139 m4x3f identity_matrix
;
1140 m4x3_identity( identity_matrix
);
1143 shader_route_uTexGarbage(0);
1144 shader_link_standard_ub( _shader_route
.id
, 2 );
1145 bind_terrain_textures();
1147 shader_route_uPv( projection
);
1148 shader_route_uMdl( identity_matrix
);
1149 shader_route_uCamera( camera
);
1151 scene_bind( &r
->scene_lines
);
1153 for( int i
=0; i
<r
->route_count
; i
++ )
1155 struct route
*route
= &r
->routes
[i
];
1158 v3_lerp( (v3f
){0.7f
,0.7f
,0.7f
}, route
->colour
, route
->factive
, colour
);
1161 shader_route_uColour( colour
);
1162 mdl_draw_submesh( &route
->sm
);
1166 static void render_world_routes_ui(void)
1169 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1170 glBlendEquation(GL_FUNC_ADD
);
1172 struct subworld_routes
*r
= &world
.routes
;
1174 float active_offset
= 0.0f
;
1175 for( int i
=0; i
<r
->route_count
; i
++ )
1177 struct route
*route
= &r
->routes
[i
];
1178 world_routes_ui_draw( i
, route
->colour
, active_offset
);
1179 active_offset
+= route
->factive
;
1182 glDisable(GL_BLEND
);
1185 #endif /* ROUTES_H */