5 #include "world_info.h"
6 #include "world_gate.h"
8 #include "shaders/vblend.h"
9 #include "shaders/route.h"
10 #include "shaders/routeui.h"
12 enum route_special_type
14 k_route_special_type_gate
= 1,
15 k_route_special_type_collector
= 2
18 static void world_routes_interact(void)
20 world
.routes
.last_interaction
= vg_time
;
23 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 static u32
world_routes_get_path( u32 starter
, u32 stack
[64] )
54 struct subworld_routes
*r
= &world
.routes
;
61 int loop_complete
= 0;
65 if( stack_i
[si
-1] == 2 )
71 struct route_node
*rn
= &r
->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 static void world_routes_ui_popfirst( u32 route
)
114 struct subworld_routes
*r
= &world
.routes
;
115 struct route
*pr
= &r
->routes
[route
];
117 if( pr
->ui
.segment_count
)
119 pr
->ui
.segment_start
++;
121 if( pr
->ui
.segment_start
== 32 )
122 pr
->ui
.segment_start
= 0;
124 pr
->ui
.segment_count
--;
129 * Reset ui bar completely
131 static void world_routes_ui_clear( u32 route
)
133 struct subworld_routes
*r
= &world
.routes
;
134 struct route
*pr
= &r
->routes
[route
];
135 pr
->ui
.segment_start
= (pr
->ui
.segment_start
+ pr
->ui
.segment_count
) %
137 pr
->ui
.segment_count
= 0;
141 * Break a index range into two pieces over the edge of the maximum it can
142 * store. s1 is 0 always, so its a ring buffer.
144 static void world_routes_ui_split_indices( u32 s0
, u32 count
, u32
*c0
, u32
*c1
)
146 *c0
= (VG_MIN( s0
+count
, k_route_ui_max_indices
)) - s0
;
151 * Place a set of indices into gpu array automatically splits
154 static void world_routes_ui_set_indices( struct route
*pr
,
155 u16
*indices
, u32 count
)
158 world_routes_ui_split_indices( pr
->ui
.indices_head
, count
, &c0
, &c1
);
160 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, pr
->ui
.ebo
);
164 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, pr
->ui
.indices_head
*sizeof(u16
),
165 c0
*sizeof(u16
), indices
);
170 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER
, 0, c1
*sizeof(u16
), indices
+c0
);
171 pr
->ui
.indices_head
= c1
;
174 pr
->ui
.indices_head
+= c0
;
178 * Place a set of vertices into gpu array
180 static u32
world_routes_ui_set_verts( struct route
*pr
, v2f
*verts
, u32 count
)
182 if( pr
->ui
.vertex_head
+ count
>= k_route_ui_max_verts
)
183 pr
->ui
.vertex_head
= 0;
185 u32 vert_start
= pr
->ui
.vertex_head
;
186 pr
->ui
.vertex_head
+= count
;
188 glBindBuffer( GL_ARRAY_BUFFER
, pr
->ui
.vbo
);
189 glBufferSubData( GL_ARRAY_BUFFER
, (GLintptr
)(vert_start
*sizeof(v2f
)),
190 sizeof(v2f
)*count
, verts
);
196 * Update the last (count) vertices positions, does not add any.
197 * Data must already be written to, and not cross either array boundaries.
199 static u32
world_routes_ui_update_verts( struct route
*pr
,
200 v2f
*verts
, u32 count
)
202 u32 vert_start
= pr
->ui
.vertex_head
-count
;
204 glBindBuffer( GL_ARRAY_BUFFER
, pr
->ui
.vbo
);
205 glBufferSubData( GL_ARRAY_BUFFER
, (GLintptr
)(vert_start
*sizeof(v2f
)),
206 sizeof(v2f
)*count
, verts
);
212 * Current/active segment of this UI bar
214 static struct route_ui_segment
*world_routes_ui_curseg( struct route
*pr
)
216 u32 index
= (pr
->ui
.segment_start
+pr
->ui
.segment_count
-1)%k_max_ui_segments
;
217 return &pr
->ui
.segments
[ index
];
221 * Start a new segment in the UI bar, will create a split on the last one if
222 * there is one active currently. (api)
224 static void world_routes_ui_newseg( u32 route
)
226 struct subworld_routes
*r
= &world
.routes
;
227 struct route
*pr
= &r
->routes
[route
];
229 pr
->ui
.last_notch
= 0.0;
231 glBindVertexArray( pr
->ui
.vao
);
232 if( pr
->ui
.segment_count
)
234 float const k_gap_width
= 1.0f
;
236 struct route_ui_segment
*cseg
= world_routes_ui_curseg(pr
);
239 verts
[0][0] = cseg
->length
-k_gap_width
;
241 verts
[1][0] = cseg
->length
-k_gap_width
;
244 world_routes_ui_update_verts( pr
, verts
, 2 );
247 pr
->ui
.segment_count
++;
248 struct route_ui_segment
*segment
= world_routes_ui_curseg(pr
);
260 u32 vert_start
= world_routes_ui_set_verts( pr
, verts
, 4 );
263 indices
[0] = vert_start
+ 0;
264 indices
[1] = vert_start
+ 1;
265 indices
[2] = vert_start
+ 3;
266 indices
[3] = vert_start
+ 0;
267 indices
[4] = vert_start
+ 3;
268 indices
[5] = vert_start
+ 2;
270 segment
->vertex_start
= vert_start
;
271 segment
->vertex_count
= 4;
272 segment
->index_start
= pr
->ui
.indices_head
;
273 segment
->index_count
= 6;
274 segment
->notches
= 0;
276 world_routes_ui_set_indices( pr
, indices
, 6 );
280 * Extend the end of the bar
282 static void world_routes_ui_updatetime( u32 route
, float time
)
284 struct subworld_routes
*r
= &world
.routes
;
285 struct route
*pr
= &r
->routes
[route
];
293 u32 vert_start
= pr
->ui
.vertex_head
-2;
295 glBindVertexArray( pr
->ui
.vao
);
296 world_routes_ui_update_verts( pr
, verts
, 2 );
298 struct route_ui_segment
*cseg
= world_routes_ui_curseg(pr
);
303 * Create a notch in the bar, used when a reset is triggered by the user
305 static void world_routes_ui_notch( u32 route
, float time
)
307 struct subworld_routes
*r
= &world
.routes
;
308 struct route
*pr
= &r
->routes
[route
];
310 if( (time
- pr
->ui
.last_notch
) > 1.0 )
312 struct route_ui_segment
*segment
= world_routes_ui_curseg(pr
);
313 if( segment
->notches
== k_max_ui_splits_per_segment
)
320 float const k_notch_width
= 1.0f
;
322 float xa
= time
-k_notch_width
,
323 xb
= time
-k_notch_width
* 0.5f
,
334 verts
[3][1] = -0.25f
;
346 glBindVertexArray( pr
->ui
.vao
);
347 u32 vert_start_mod
= world_routes_ui_update_verts( pr
, verts
, 2 ),
348 vert_start_new
= world_routes_ui_set_verts( pr
, verts
+2, 6 );
351 indices
[ 0] = vert_start_mod
+1;
352 indices
[ 1] = vert_start_new
+0;
353 indices
[ 2] = vert_start_mod
+0;
354 indices
[ 3] = vert_start_mod
+1;
355 indices
[ 4] = vert_start_new
+1;
356 indices
[ 5] = vert_start_new
+0;
358 indices
[ 6] = vert_start_new
+0;
359 indices
[ 7] = vert_start_new
+1;
360 indices
[ 8] = vert_start_new
+3;
361 indices
[ 9] = vert_start_new
+0;
362 indices
[10] = vert_start_new
+3;
363 indices
[11] = vert_start_new
+2;
365 indices
[12] = vert_start_new
+3;
366 indices
[13] = vert_start_new
+4;
367 indices
[14] = vert_start_new
+2;
368 indices
[15] = vert_start_new
+3;
369 indices
[16] = vert_start_new
+5;
370 indices
[17] = vert_start_new
+4;
372 world_routes_ui_set_indices( pr
, indices
, 18 );
374 pr
->ui
.last_notch
= time
;
376 segment
->vertex_count
+= 6;
377 segment
->index_count
+= 18;
381 static void world_routes_ui_draw_segment( struct route_ui_segment
*segment
)
384 world_routes_ui_split_indices( segment
->index_start
,
385 segment
->index_count
, &c0
, &c1
);
387 glDrawElements( GL_TRIANGLES
, c0
, GL_UNSIGNED_SHORT
,
388 (void *)(segment
->index_start
*sizeof(u16
)));
390 glDrawElements( GL_TRIANGLES
, c1
, GL_UNSIGNED_SHORT
, (void *)(0) );
394 * Draws full bar at Y offset(offset).
396 static void world_routes_ui_draw( u32 route
, v4f colour
, float offset
)
398 float const k_bar_height
= 0.05f
,
399 k_bar_scale_x
= 0.005f
;
401 struct subworld_routes
*r
= &world
.routes
;
402 struct route
*pr
= &r
->routes
[route
];
404 float cx
= pr
->ui
.xpos
;
406 shader_routeui_use();
407 glBindVertexArray( pr
->ui
.vao
);
409 float fade_amt
= vg_time
- pr
->ui
.fade_timer_start
;
410 fade_amt
= vg_clampf( fade_amt
/ 1.0f
, 0.0f
, 1.0f
);
412 float fade_block_size
= 0.0f
,
413 main_block_size
= 0.0f
;
415 for( u32 i
=0; i
<pr
->ui
.fade_count
; i
++ )
417 u32 j
= (pr
->ui
.fade_start
+ i
) % k_max_ui_segments
;
418 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
420 fade_block_size
+= segment
->length
;
423 cx
-= fade_block_size
* fade_amt
;
426 v4_copy( colour
, fade_colour
);
427 fade_colour
[3] *= 1.0f
-fade_amt
;
429 float timer_delta
= (vg_time
- world
.routes
.last_interaction
) * (1.0/60.0),
430 timer_scale
= 1.0f
- vg_minf( timer_delta
, 1.0f
);
436 float height
= pr
->factive
*k_bar_height
* timer_scale
,
437 base
= -1.0f
+ (offset
+0.5f
)*k_bar_height
* timer_scale
;
439 shader_routeui_uColour( fade_colour
);
440 for( u32 i
=0; i
<pr
->ui
.fade_count
; i
++ )
442 u32 j
= (pr
->ui
.fade_start
+ i
) % k_max_ui_segments
;
443 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
445 shader_routeui_uOffset( (v4f
){ cx
*k_bar_scale_x
, base
,
446 k_bar_scale_x
, height
} );
448 world_routes_ui_draw_segment( segment
);
449 cx
+= segment
->length
;
455 shader_routeui_uColour( colour
);
456 for( u32 i
=0; i
<pr
->ui
.segment_count
; i
++ )
458 u32 j
= (pr
->ui
.segment_start
+ i
) % k_max_ui_segments
;
459 struct route_ui_segment
*segment
= &pr
->ui
.segments
[j
];
461 shader_routeui_uOffset( (v4f
){ cx
*k_bar_scale_x
, base
,
462 k_bar_scale_x
, height
} );
464 world_routes_ui_draw_segment( segment
);
465 cx
+= segment
->length
;
467 main_block_size
+= segment
->length
;
470 pr
->ui
.xpos
= vg_lerpf( pr
->ui
.xpos
, -main_block_size
* 0.5f
, 0.03f
);
473 static void world_routes_local_set_record( u32 route
, double lap_time
)
475 vg_success( " NEW LAP TIME: %f\n", lap_time
);
477 struct subworld_routes
*r
= &world
.routes
;
478 struct route
*pr
= &r
->routes
[route
];
480 if( pr
->track_id
!= 0xffffffff )
482 double time_centiseconds
= lap_time
* 100.0;
483 if( time_centiseconds
> (float)0xfffe )
486 highscore_record temp
;
487 temp
.trackid
= pr
->track_id
;
488 temp
.datetime
= time(NULL
);
491 temp
.time
= time_centiseconds
;
493 highscores_push_record( &temp
);
494 track_infos
[ pr
->track_id
].push
= 1;
498 vg_warn( "There is no associated track for this record...\n" );
503 * Will scan the whole run for two things;
504 * 1: we set a new record for the total, complete loop around the course
505 * 2: the time of each segment will be recorded into the data buffer
506 * (not implemented: TODO)
508 static void world_routes_verify_run( u32 route
)
510 struct subworld_routes
*r
= &world
.routes
;
511 struct route
*pr
= &r
->routes
[route
];
514 u32 si
= world_routes_get_path( r
->routes
[route
].start
, stack
);
517 * we only care about gates that ref gates, so shuffle down the array
519 struct route_timing
*timings
[64];
520 u32 sj
= 0, maxv
= 0, begin
= 0;
521 for( u32 i
=0; i
<si
; i
++ )
523 if( r
->nodes
[stack
[i
]].special_type
== k_route_special_type_collector
)
524 timings
[sj
++] = &r
->collectors
[r
->nodes
[stack
[i
]].special_id
].timing
;
525 else if( r
->nodes
[stack
[i
]].special_type
== k_route_special_type_gate
)
526 timings
[sj
++] = &r
->gates
[r
->nodes
[stack
[i
]].special_id
].timing
;
529 for( u32 i
=0; i
<sj
; i
++ )
531 if( timings
[i
]->version
> maxv
)
533 maxv
= timings
[i
]->version
;
538 vg_info( "== begin verification (%u) ==\n", route
);
539 vg_info( " current version: %u\n", r
->current_run_version
);
542 if( timings
[begin
]->version
== r
->current_run_version
)
545 int valid_segment_count
= 0;
547 double lap_time
= 0.0;
549 for( u32 i
=0; i
<sj
; i
++ )
551 u32 j
= (sj
+begin
-i
-1) % sj
,
558 /* j1v should equal jv+1 */
559 if( timings
[j1
]->version
== timings
[j
]->version
+1 )
561 diff
= timings
[j1
]->time
- timings
[j
]->time
;
564 if( verified
&& diff
> 0.0 ) valid_segment_count
++;
571 vg_success( " [ %u %f ] %f\n", timings
[j1
]->time
,
572 timings
[j1
]->version
, diff
);
574 vg_warn( " [ %u %f ]\n", timings
[j1
]->time
, timings
[j1
]->version
);
577 pr
->ui
.fade_start
= pr
->ui
.segment_start
;
578 pr
->ui
.fade_count
= 0;
579 pr
->ui
.fade_timer_start
= vg_time
;
581 int orig_seg_count
= pr
->ui
.segment_count
;
583 world_routes_ui_newseg( route
);
587 world_routes_local_set_record( route
, lap_time
);
588 world_routes_ui_popfirst(route
);
589 pr
->ui
.fade_count
++;
592 vg_info( " ctime: %f\n", lap_time
);
594 /* remove any excess we had from previous runs */
595 int to_remove
= orig_seg_count
-valid_segment_count
;
596 for( int i
=0; i
<to_remove
; i
++ )
598 world_routes_ui_popfirst(route
);
599 pr
->ui
.fade_count
++;
602 r
->routes
[route
].latest_pass
= vg_time
;
606 * When going through a gate this is called for bookkeeping purposes
608 static void world_routes_activate_gate( u32 id
)
610 world_routes_interact();
612 struct subworld_routes
*r
= &world
.routes
;
613 struct route_gate
*rg
= &r
->gates
[id
];
614 struct route_node
*pnode
= &r
->nodes
[rg
->node_id
],
615 *pdest
= &r
->nodes
[pnode
->next
[0]];
617 struct route_collector
*rc
= &r
->collectors
[ pdest
->special_id
];
620 rg
->timing
.version
= r
->current_run_version
;
621 rg
->timing
.time
= vg_time
;
622 for( u32 i
=0; i
<r
->route_count
; i
++ )
624 struct route
*route
= &r
->routes
[i
];
626 int was_active
= route
->active
;
629 for( u32 j
=0; j
<pdest
->ref_count
; j
++ )
631 if( pdest
->route_ids
[j
] == i
)
633 world_routes_verify_run( i
);
639 if( was_active
&& !route
->active
)
641 route
->ui
.fade_start
= route
->ui
.segment_start
;
642 route
->ui
.fade_count
= route
->ui
.segment_count
;
643 route
->ui
.fade_timer_start
= vg_time
;
644 world_routes_ui_clear(i
);
646 vg_success( "CLEARING -> %u %u \n", route
->ui
.fade_start
,
647 route
->ui
.fade_count
);
651 r
->current_run_version
++;
653 rc
->timing
.version
= r
->current_run_version
;
654 rc
->timing
.time
= vg_time
;
655 r
->current_run_version
++;
659 * Notify the UI system that we've reset the player
661 static void world_routes_notify_reset(void)
663 struct subworld_routes
*r
= &world
.routes
;
664 world_routes_interact();
666 for( int i
=0; i
<r
->route_count
; i
++ )
668 struct route
*route
= &r
->routes
[i
];
671 world_routes_ui_notch( i
, vg_time
- route
->latest_pass
);
675 static void world_routes_debug(void)
677 struct subworld_routes
*r
= &world
.routes
;
679 for( int i
=0; i
<r
->node_count
; i
++ )
681 struct route_node
*rn
= &r
->nodes
[i
];
682 vg_line_pt3( rn
->co
, 1.0f
, rn
->special_type
? 0xffffff00: 0xff00b2ff );
685 for( int i
=0; i
<r
->route_count
; i
++ )
687 struct route
*route
= &r
->routes
[i
];
690 u32 si
= world_routes_get_path( route
->start
, stack
);
692 u32 colours
[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
695 u32 cc
= colours
[i
%vg_list_size(colours
)];
697 for( int sj
=0; sj
<si
; sj
++ )
700 debug_sbpath( &r
->nodes
[stack
[sj
]], &r
->nodes
[stack
[sk
]], cc
,
705 for( int i
=0; i
<r
->node_count
; i
++ )
707 struct route_node
*ri
= &r
->nodes
[i
],
710 for( int j
=0; j
<2; j
++ )
712 if( ri
->next
[j
] != 0xffffffff )
714 rj
= &r
->nodes
[ri
->next
[j
]];
715 vg_line( ri
->co
, rj
->co
, 0x20ffffff );
721 static void world_id_fixup( u32
*uid
, mdl_header
*mdl
)
724 *uid
= mdl_node_from_id( mdl
, *uid
)->sub_uid
;
729 static void world_routes_create_mesh( u32 route_id
)
731 struct subworld_routes
*r
= &world
.routes
;
732 struct route
*route
= &r
->routes
[ route_id
];
735 u32 si
= world_routes_get_path( route
->start
, stack
);
739 for( int sj
=0; sj
<si
; sj
++ )
743 struct route_node
*rnj
= &r
->nodes
[ stack
[sj
] ],
744 *rnk
= &r
->nodes
[ stack
[sk
] ],
747 if( rnj
->special_type
&& rnk
->special_type
)
753 float base_x0
= (float)rnj
->ref_count
*-0.5f
+ (float)rnj
->current_refs
,
754 base_x1
= (float)rnk
->ref_count
*-0.5f
+ (float)rnk
->current_refs
;
756 if( rnk
->special_type
)
758 rnl
= &r
->nodes
[ rnk
->next
[0] ];
759 base_x1
= (float)rnl
->ref_count
*-0.5f
+ (float)rnl
->current_refs
;
767 v3f p0
, h0
, p1
, h1
, p
, pd
;
769 v3_copy( rnj
->co
, p0
);
770 v3_muladds( rnj
->co
, rnj
->h
, 1.0f
, h0
);
771 v3_copy( rnk
->co
, p1
);
772 v3_muladds( rnk
->co
, rnk
->h
, -1.0f
, h1
);
777 for( int it
=0; it
<256; it
++ )
779 float const k_sample_dist
= 0.02f
;
780 eval_bezier_time( p0
,p1
,h0
,h1
, t
,p
);
781 eval_bezier_time( p0
,p1
,h0
,h1
, t
+k_sample_dist
,pd
);
783 float mod
= k_sample_dist
/ v3_dist( p
, pd
);
786 v3_muls( rnj
->up
, 1.0f
-t
, up
);
787 v3_muladds( up
, rnk
->up
, t
, up
);
790 v3_cross( up
, v0
, right
);
791 v3_normalize( right
);
793 float cur_x
= (1.0f
-t
)*base_x0
+ t
*base_x1
;
795 v3f sc
, sa
, sb
, down
;
796 v3_muladds( p
, right
, cur_x
, sc
);
797 v3_muladds( sc
, up
, 1.5f
, sc
);
798 v3_muladds( sc
, right
, 0.45f
, sa
);
799 v3_muladds( sc
, right
, -0.45f
, sb
);
800 v3_muls( up
, -1.0f
, down
);
805 if( ray_world( sa
, down
, &ha
) &&
806 ray_world( sb
, down
, &hb
))
810 v3_muladds( ha
.pos
, up
, 0.06f
, va
.co
);
811 v3_muladds( hb
.pos
, up
, 0.06f
, vb
.co
);
812 v3_copy( up
, va
.norm
);
813 v3_copy( up
, vb
.norm
);
817 scene_push_vert( &r
->scene_lines
, &va
);
818 scene_push_vert( &r
->scene_lines
, &vb
);
822 /* Connect them with triangles */
823 scene_push_tri( &r
->scene_lines
, (u32
[3]){
824 last_valid
+0-2, last_valid
+1-2, last_valid
+2-2} );
825 scene_push_tri( &r
->scene_lines
, (u32
[3]){
826 last_valid
+1-2, last_valid
+3-2, last_valid
+2-2} );
829 last_valid
= r
->scene_lines
.vertex_count
;
838 /* TODO special case for end of loop, need to add triangles
839 * between first and last rungs */
844 rnj
->current_refs
++;
847 scene_copy_slice( &r
->scene_lines
, &route
->sm
);
851 * Create the strips of colour that run through the world along course paths
853 static int world_routes_create_all_meshes(void)
855 vg_info( "Generating route meshes\n" );
857 struct subworld_routes
*r
= &world
.routes
;
858 scene_init( &r
->scene_lines
);
860 for( u32 i
=0; i
<r
->route_count
; i
++ )
861 world_routes_create_mesh( i
);
863 vg_acquire_thread_sync();
865 scene_upload( &r
->scene_lines
);
868 for( int i
=0; i
<r
->route_count
; i
++ )
871 struct route
*route
= &r
->routes
[i
];
873 glGenVertexArrays( 1, &route
->ui
.vao
);
874 glGenBuffers( 1, &route
->ui
.vbo
);
875 glGenBuffers( 1, &route
->ui
.ebo
);
876 glBindVertexArray( route
->ui
.vao
);
878 size_t stride
= sizeof(v2f
);
880 glBindBuffer( GL_ARRAY_BUFFER
, route
->ui
.vbo
);
881 glBufferData( GL_ARRAY_BUFFER
, k_route_ui_max_verts
*stride
,
882 NULL
, GL_DYNAMIC_DRAW
);
884 glBindVertexArray( route
->ui
.vao
);
885 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER
, route
->ui
.ebo
);
886 glBufferData( GL_ELEMENT_ARRAY_BUFFER
,
887 k_route_ui_max_indices
*sizeof(u16
), NULL
,
890 glVertexAttribPointer( 0, 2, GL_FLOAT
, GL_FALSE
, stride
, (void *)0 );
891 glEnableVertexAttribArray( 0 );
895 vg_release_thread_sync();
897 scene_free_offline_buffers( &r
->scene_lines
);
902 static void world_routes_loadfrom( mdl_header
*mdl
)
904 vg_info( "Initializing routes\n" );
906 struct subworld_routes
*r
= &world
.routes
;
908 r
->node_count
= 0; r
->node_cap
= 0; r
->routes
= NULL
;
915 /* TODO Break this up */
916 for( int i
=0; i
<mdl
->node_count
; i
++ )
918 mdl_node
*pnode
= mdl_node_from_id(mdl
,i
);
921 if( pnode
->classtype
== k_classtype_route_node
||
922 pnode
->classtype
== k_classtype_gate
)
924 mdl_node_transform( pnode
, transform
);
925 pnode
->sub_uid
= r
->node_count
;
927 r
->nodes
= buffer_reserve( r
->nodes
, r
->node_count
, &r
->node_cap
, 1,
928 sizeof( struct route_node
) );
930 struct route_node
*rn
= &r
->nodes
[r
->node_count
];
932 v3_copy( transform
[0], rn
->right
);
933 v3_normalize( rn
->right
);
934 v3_copy( transform
[1], rn
->up
);
935 v3_normalize( rn
->up
);
936 v3_muls( transform
[2], -1.0f
, rn
->h
);
937 v3_copy( transform
[3], rn
->co
);
939 rn
->current_refs
= 0;
940 rn
->special_type
= 0;
943 if( pnode
->classtype
== k_classtype_gate
)
945 struct classtype_gate
*inf
= mdl_get_entdata( mdl
, pnode
);
947 /* H is later scaled based on link distance */
948 v3_normalize( rn
->h
);
949 rn
->next
[0] = inf
->target
;
955 mdl_node
*pother
= mdl_node_from_id( mdl
, inf
->target
);
957 if( pother
->classtype
== k_classtype_gate
)
959 r
->gates
= buffer_reserve( r
->gates
, r
->gate_count
,
961 1, sizeof( struct route_gate
) );
963 struct route_gate
*rg
= &r
->gates
[r
->gate_count
];
964 rg
->node_id
= r
->node_count
;
965 rg
->timing
.time
= 0.0;
966 rg
->timing
.version
= 0;
968 v3_copy( pnode
->co
, rg
->gate
.co
[0] );
969 v3_copy( pother
->co
, rg
->gate
.co
[1] );
970 v4_copy( pnode
->q
, rg
->gate
.q
[0] );
971 v4_copy( pother
->q
, rg
->gate
.q
[1] );
972 v2_copy( inf
->dims
, rg
->gate
.dims
);
974 gate_transform_update( &rg
->gate
);
975 rn
->special_type
= k_route_special_type_gate
;
976 rn
->special_id
= r
->gate_count
;
982 if( rn
->special_type
== 0 )
984 r
->collectors
= buffer_reserve(
985 r
->collectors
, r
->collector_count
, &r
->collector_cap
,
986 1, sizeof( struct route_collector
));
988 struct route_collector
*rc
= &r
->collectors
[r
->collector_count
];
989 rc
->timing
.time
= 0.0;
990 rc
->timing
.version
= 0;
992 rn
->special_type
= k_route_special_type_collector
;
993 rn
->special_id
= r
->collector_count
;
995 r
->collector_count
++;
1000 struct classtype_route_node
*inf
= mdl_get_entdata( mdl
, pnode
);
1001 rn
->next
[0] = inf
->target
;
1002 rn
->next
[1] = inf
->target1
;
1007 else if( pnode
->classtype
== k_classtype_route
)
1009 struct classtype_route
*inf
= mdl_get_entdata( mdl
, pnode
);
1010 r
->routes
= buffer_reserve( r
->routes
, r
->route_count
, &r
->route_cap
,
1011 1, sizeof( struct route
) );
1013 struct route
*route
= &r
->routes
[r
->route_count
];
1015 v3_copy( inf
->colour
, route
->colour
);
1016 route
->colour
[3] = 1.0f
;
1019 route
->track_id
= 0xffffffff;
1020 for( u32 j
=0; j
<vg_list_size(track_infos
); j
++ )
1022 if( !strcmp( mdl_pstr(mdl
,pnode
->pstr_name
), track_infos
[j
].name
))
1024 route
->track_id
= j
;
1029 route
->start
= inf
->id_start
;
1031 route
->factive
= 0.0f
;
1032 mdl_node_transform( pnode
, route
->scoreboard_transform
);
1034 route
->ui
.indices_head
= k_route_ui_max_indices
- 9;
1035 route
->ui
.vertex_head
= k_route_ui_max_verts
- 200;
1036 route
->ui
.segment_start
= 0;
1037 route
->ui
.segment_count
= 0;
1038 route
->ui
.last_notch
= 0.0;
1039 route
->ui
.fade_start
= 0;
1040 route
->ui
.fade_count
= 0;
1041 route
->ui
.fade_timer_start
= 0.0;
1048 * Apply correct system-local ids
1050 for( int i
=0; i
<r
->node_count
; i
++ )
1052 struct route_node
*rn
= &r
->nodes
[i
];
1054 for( int j
=0; j
<2; j
++ )
1055 world_id_fixup( &rn
->next
[j
], mdl
);
1058 for( int i
=0; i
<r
->route_count
; i
++ )
1060 struct route
*route
= &r
->routes
[i
];
1061 world_id_fixup( &route
->start
, mdl
);
1067 for( int i
=0; i
<r
->route_count
; i
++ )
1069 struct route
*route
= &r
->routes
[i
];
1072 u32 si
= world_routes_get_path( route
->start
, stack
);
1074 for( int sj
=0; sj
<si
; sj
++ )
1076 struct route_node
*rn
= &r
->nodes
[ stack
[sj
] ];
1077 rn
->route_ids
[ rn
->ref_count
++ ] = i
;
1079 if( rn
->ref_count
> 4 )
1080 vg_warn( "Too many references on route node %i\n", i
);
1084 world_routes_create_all_meshes();
1088 * -----------------------------------------------------------------------------
1090 * -----------------------------------------------------------------------------
1093 static void world_routes_init(void)
1095 struct subworld_routes
*r
= &world
.routes
;
1096 r
->current_run_version
= 2;
1098 shader_route_register();
1099 shader_routeui_register();
1102 static void world_routes_free(void*_
)
1104 struct subworld_routes
*r
= &world
.routes
;
1106 vg_free( r
->nodes
);
1107 vg_free( r
->routes
);
1108 vg_free( r
->gates
);
1111 static void world_routes_update(void)
1113 struct subworld_routes
*r
= &world
.routes
;
1115 for( int i
=0; i
<r
->route_count
; i
++ )
1117 struct route
*route
= &r
->routes
[i
];
1118 route
->factive
= vg_lerpf( route
->factive
, route
->active
, 0.01f
);
1122 world_routes_ui_updatetime( i
, vg_time
- route
->latest_pass
);
1127 static void bind_terrain_textures(void);
1128 static void render_world_routes( m4x4f projection
, v3f camera
)
1130 struct subworld_routes
*r
= &world
.routes
;
1132 m4x3f identity_matrix
;
1133 m4x3_identity( identity_matrix
);
1136 shader_route_uTexGarbage(0);
1137 shader_link_standard_ub( _shader_route
.id
, 2 );
1138 bind_terrain_textures();
1140 shader_route_uPv( projection
);
1141 shader_route_uMdl( identity_matrix
);
1142 shader_route_uCamera( camera
);
1144 scene_bind( &r
->scene_lines
);
1146 for( int i
=0; i
<r
->route_count
; i
++ )
1148 struct route
*route
= &r
->routes
[i
];
1151 v3_lerp( (v3f
){0.7f
,0.7f
,0.7f
}, route
->colour
, route
->factive
, colour
);
1154 shader_route_uColour( colour
);
1155 mdl_draw_submesh( &route
->sm
);
1159 static void render_world_routes_ui(void)
1162 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1163 glBlendEquation(GL_FUNC_ADD
);
1165 struct subworld_routes
*r
= &world
.routes
;
1167 float active_offset
= 0.0f
;
1168 for( int i
=0; i
<r
->route_count
; i
++ )
1170 struct route
*route
= &r
->routes
[i
];
1171 world_routes_ui_draw( i
, route
->colour
, active_offset
);
1172 active_offset
+= route
->factive
;
1175 glDisable(GL_BLEND
);
1178 #endif /* ROUTES_H */