8 #include "shaders/vblend.h"
9 #include "shaders/route.h"
11 enum route_special_type
13 k_route_special_type_gate
= 1,
14 k_route_special_type_collector
= 2
17 struct subworld_routes
24 u32 special_type
, special_id
, current_refs
, ref_count
;
25 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
53 u32 passed_version
; /* Incremented on every teleport */
57 struct route_collector
59 double time_passed
; /* When did we last pass this? */
74 static struct subworld_routes
*subworld_routes(void);
76 static void debug_sbpath( struct route_node
*rna
, struct route_node
*rnb
,
77 u32 colour
, float xoffset
)
79 v3f p0
, h0
, p1
, h1
, l
, p
;
81 v3_copy( rna
->co
, p0
);
82 v3_muladds( rna
->co
, rna
->h
, 1.0f
, h0
);
83 v3_copy( rnb
->co
, p1
);
84 v3_muladds( rnb
->co
, rnb
->h
, -1.0f
, h1
);
86 v3_muladds( p0
, rna
->right
, xoffset
, p0
);
87 v3_muladds( h0
, rna
->right
, xoffset
, h0
);
88 v3_muladds( p1
, rnb
->right
, xoffset
, p1
);
89 v3_muladds( h1
, rnb
->right
, xoffset
, h1
);
93 for( int i
=0; i
<5; i
++ )
95 float t
= (float)(i
+1)/5.0f
;
96 eval_bezier_time( p0
, p1
, h0
, h1
, t
, p
);
97 vg_line( p
, l
, colour
);
102 static u32
world_routes_get_path( u32 starter
, u32 stack
[64] )
104 struct subworld_routes
*r
= subworld_routes();
111 int loop_complete
= 0;
115 if( stack_i
[si
-1] == 2 )
121 struct route_node
*rn
= &r
->nodes
[stack
[si
-1]];
122 u32 nextid
= rn
->next
[stack_i
[si
-1]];
125 if( nextid
!= 0xffffffff )
127 if( nextid
== stack
[0] )
134 for( int sj
=0; sj
<si
; sj
++ )
136 if( stack
[sj
] == nextid
)
159 static void world_routes_verify_run( u32 route
, double new_pass_time
)
161 struct subworld_routes
*r
= subworld_routes();
164 u32 si
= world_routes_get_path( r
->routes
[route
].start
, stack
);
167 * we only care about gates that ref gates, so shuffle down the array
171 for( u32 i
=0; i
<si
; i
++ )
172 if( r
->nodes
[stack
[i
]].special_type
== k_route_special_type_gate
)
173 gates
[sj
++] = r
->nodes
[stack
[i
]].special_id
;
176 * run versions & times must always ASCEND apart from exactly once, where
177 * the tail connects to the head
180 vg_info("Verifying run (%u)\n", route
);
182 u32 offset
= 0xffffffff;
184 vg_info( " ver: %u\n", r
->current_run_version
);
186 for( u32 i
=0; i
<sj
; i
++ )
188 struct route_gate
*pa
= &r
->gates
[gates
[i
]];
189 u32 na
= r
->nodes
[r
->nodes
[pa
->node_id
].next
[0]].special_id
;
190 struct route_collector
*pca
= &r
->collectors
[ na
];
192 if( pa
->passed_version
!= r
->current_run_version
)
194 vg_info( " [%u]: %u (%f)\n", gates
[i
], pa
->passed_version
,
199 vg_info( " [%u]: %u (%f & %f)\n", gates
[i
], pa
->passed_version
,
206 if( offset
== 0xffffffff )
208 /* If we don't find the emitter we passed through, we've only just
209 * started this route, so no blocks need to be emmited */
213 double lap_time
= 0.0;
214 int verify_count
= 0;
216 for( u32 i
=0; i
<sj
; i
++ )
219 struct route_gate
*pa
= &r
->gates
[gates
[j
%sj
]],
220 *pb
= &r
->gates
[gates
[(j
+1) % sj
]];
222 u32 na
= r
->nodes
[r
->nodes
[pa
->node_id
].next
[0]].special_id
,
223 nb
= r
->nodes
[r
->nodes
[pb
->node_id
].next
[0]].special_id
;
225 struct route_collector
*pca
= &r
->collectors
[ na
],
226 *pcb
= &r
->collectors
[ nb
];
230 /* Verifying the path: either of this conditions must be true */
232 if( pa
->passed_version
== r
->current_run_version
)
234 /* The version should drop back down to pa+1-sj */
238 /* The version should be pa+1 */
241 verify_count
+= verified
;
243 if( pb
->passed_version
== r
->current_run_version
)
245 /* we need to use new val */
246 diff
= new_pass_time
- pca
->time_passed
;
247 vg_info( " LOOP %f\n", diff
);
251 diff
= pcb
->time_passed
- pca
->time_passed
;
252 vg_info( " %f\n", diff
);
258 /* Verify count is how many blocks we want to emit */
261 static void world_routes_activate_gate( u32 id
)
263 struct subworld_routes
*r
= subworld_routes();
264 struct route_gate
*rg
= &r
->gates
[id
];
265 struct route_node
*pnode
= &r
->nodes
[rg
->node_id
],
266 *pdest
= &r
->nodes
[pnode
->next
[0]];
268 struct route_collector
*rc
= &r
->collectors
[ pdest
->special_id
];
271 rg
->passed_version
= r
->current_run_version
;
273 vg_info( "collector updated: %u\n", pdest
->special_id
);
275 for( u32 i
=0; i
<r
->route_count
; i
++ )
277 struct route
*route
= &r
->routes
[i
];
280 for( u32 j
=0; j
<pdest
->ref_count
; j
++ )
282 if( pdest
->route_ids
[j
] == i
)
284 world_routes_verify_run( i
, vg_time
);
291 rc
->time_passed
= vg_time
;
292 r
->current_run_version
++;
295 static void world_routes_debug(void)
297 struct subworld_routes
*r
= subworld_routes();
299 for( int i
=0; i
<r
->node_count
; i
++ )
301 struct route_node
*rn
= &r
->nodes
[i
];
302 vg_line_pt3( rn
->co
, 1.0f
, rn
->special_type
? 0xffffff00: 0xff00b2ff );
305 for( int i
=0; i
<r
->route_count
; i
++ )
307 struct route
*route
= &r
->routes
[i
];
310 u32 si
= world_routes_get_path( route
->start
, stack
);
312 u32 colours
[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
315 u32 cc
= colours
[i
%vg_list_size(colours
)];
317 for( int sj
=0; sj
<si
; sj
++ )
320 debug_sbpath( &r
->nodes
[stack
[sj
]], &r
->nodes
[stack
[sk
]], cc
,
325 for( int i
=0; i
<r
->node_count
; i
++ )
327 struct route_node
*ri
= &r
->nodes
[i
],
330 for( int j
=0; j
<2; j
++ )
332 if( ri
->next
[j
] != 0xffffffff )
334 rj
= &r
->nodes
[ri
->next
[j
]];
335 vg_line( ri
->co
, rj
->co
, 0x20ffffff );
341 static void world_routes_free(void)
343 struct subworld_routes
*r
= subworld_routes();
350 static void world_id_fixup( u32
*uid
, mdl_header
*mdl
)
353 *uid
= mdl_node_from_id( mdl
, *uid
)->sub_uid
;
358 static void world_routes_gen_meshes(void)
360 struct subworld_routes
*r
= subworld_routes();
361 scene_init( &r
->scene_lines
);
363 for( int i
=0; i
<r
->route_count
; i
++ )
365 struct route
*route
= &r
->routes
[i
];
368 u32 si
= world_routes_get_path( route
->start
, stack
);
372 for( int sj
=0; sj
<si
; sj
++ )
376 struct route_node
*rnj
= &r
->nodes
[ stack
[sj
] ],
377 *rnk
= &r
->nodes
[ stack
[sk
] ],
380 if( rnj
->special_type
&& rnk
->special_type
)
386 float base_x0
= (float)rnj
->ref_count
*-0.5f
+ (float)rnj
->current_refs
,
387 base_x1
= (float)rnk
->ref_count
*-0.5f
+ (float)rnk
->current_refs
;
389 if( rnk
->special_type
)
391 rnl
= &r
->nodes
[ rnk
->next
[0] ];
392 base_x1
= (float)rnl
->ref_count
*-0.5f
+ (float)rnl
->current_refs
;
400 v3f p0
, h0
, p1
, h1
, p
, pd
;
402 v3_copy( rnj
->co
, p0
);
403 v3_muladds( rnj
->co
, rnj
->h
, 1.0f
, h0
);
404 v3_copy( rnk
->co
, p1
);
405 v3_muladds( rnk
->co
, rnk
->h
, -1.0f
, h1
);
410 for( int it
=0; it
<256; it
++ )
412 float const k_sample_dist
= 0.02f
;
413 eval_bezier_time( p0
,p1
,h0
,h1
, t
,p
);
414 eval_bezier_time( p0
,p1
,h0
,h1
, t
+k_sample_dist
,pd
);
416 float mod
= k_sample_dist
/ v3_dist( p
, pd
);
419 v3_muls( rnj
->up
, 1.0f
-t
, up
);
420 v3_muladds( up
, rnk
->up
, t
, up
);
423 v3_cross( up
, v0
, right
);
424 v3_normalize( right
);
426 float cur_x
= (1.0f
-t
)*base_x0
+ t
*base_x1
;
428 v3f sc
, sa
, sb
, down
;
429 v3_muladds( p
, right
, cur_x
, sc
);
430 v3_muladds( sc
, up
, 1.5f
, sc
);
431 v3_muladds( sc
, right
, 0.45f
, sa
);
432 v3_muladds( sc
, right
, -0.45f
, sb
);
433 v3_muls( up
, -1.0f
, down
);
438 if(ray_world( sa
, down
, &ha
) &&
439 ray_world( sb
, down
, &hb
))
443 v3_muladds( ha
.pos
, up
, 0.06f
, va
.co
);
444 v3_muladds( hb
.pos
, up
, 0.06f
, vb
.co
);
445 v3_copy( up
, va
.norm
);
446 v3_copy( up
, vb
.norm
);
447 v3_zero( va
.colour
);
448 v3_zero( vb
.colour
);
452 scene_push_vert( &r
->scene_lines
, &va
);
453 scene_push_vert( &r
->scene_lines
, &vb
);
457 /* Connect them with triangles */
458 scene_push_tri( &r
->scene_lines
, (u32
[3]){
459 last_valid
+0-2, last_valid
+1-2, last_valid
+2-2} );
460 scene_push_tri( &r
->scene_lines
, (u32
[3]){
461 last_valid
+1-2, last_valid
+3-2, last_valid
+2-2} );
464 last_valid
= r
->scene_lines
.vertex_count
;
473 /* TODO special case for end of loop, need to add triangles
474 * between first and last rungs */
479 rnj
->current_refs
++;
482 scene_copy_slice( &r
->scene_lines
, &route
->sm
);
485 scene_upload( &r
->scene_lines
);
486 scene_free_offline_buffers( &r
->scene_lines
);
489 static void bind_terrain_textures(void);
490 static void render_world_routes( m4x4f projection
, v3f camera
)
492 struct subworld_routes
*r
= subworld_routes();
494 m4x3f identity_matrix
;
495 m4x3_identity( identity_matrix
);
498 shader_route_uTexGarbage(0);
499 shader_link_standard_ub( _shader_route
.id
, 2 );
500 bind_terrain_textures();
502 shader_route_uPv( projection
);
503 shader_route_uMdl( identity_matrix
);
504 shader_route_uCamera( camera
);
506 scene_bind( &r
->scene_lines
);
508 for( int i
=0; i
<r
->route_count
; i
++ )
510 struct route
*route
= &r
->routes
[i
];
511 route
->factive
= vg_lerpf( route
->factive
, route
->active
, 0.01f
);
514 v3_lerp( (v3f
){0.7f
,0.7f
,0.7f
}, route
->colour
, route
->factive
, colour
);
517 shader_route_uColour( colour
);
518 mdl_draw_submesh( &route
->sm
);
522 static void world_routes_register(void)
524 shader_route_register();
527 static void world_routes_loadfrom( mdl_header
*mdl
)
529 struct subworld_routes
*r
= subworld_routes();
540 for( int i
=0; i
<mdl
->node_count
; i
++ )
542 mdl_node
*pnode
= mdl_node_from_id(mdl
,i
);
545 if( pnode
->classtype
== k_classtype_route_node
||
546 pnode
->classtype
== k_classtype_gate
)
548 mdl_node_transform( pnode
, transform
);
549 pnode
->sub_uid
= r
->node_count
;
551 r
->nodes
= buffer_reserve( r
->nodes
, r
->node_count
, &r
->node_cap
, 1,
552 sizeof( struct route_node
) );
554 struct route_node
*rn
= &r
->nodes
[r
->node_count
];
556 v3_copy( transform
[0], rn
->right
);
557 v3_normalize( rn
->right
);
558 v3_copy( transform
[1], rn
->up
);
559 v3_normalize( rn
->up
);
560 v3_muls( transform
[2], -1.0f
, rn
->h
);
561 v3_copy( transform
[3], rn
->co
);
563 rn
->current_refs
= 0;
564 rn
->special_type
= 0;
567 if( pnode
->classtype
== k_classtype_gate
)
569 struct classtype_gate
*inf
= mdl_get_entdata( mdl
, pnode
);
571 /* H is later scaled based on link distance */
572 v3_normalize( rn
->h
);
573 rn
->next
[0] = inf
->target
;
579 mdl_node
*pother
= mdl_node_from_id( mdl
, inf
->target
);
581 if( pother
->classtype
== k_classtype_gate
)
583 r
->gates
= buffer_reserve( r
->gates
, r
->gate_count
,
585 1, sizeof( struct route_gate
) );
587 struct route_gate
*rg
= &r
->gates
[r
->gate_count
];
588 rg
->node_id
= r
->node_count
;
589 rg
->passed_version
= 0xffffffff;
591 v3_copy( pnode
->co
, rg
->gate
.co
[0] );
592 v3_copy( pother
->co
, rg
->gate
.co
[1] );
593 v4_copy( pnode
->q
, rg
->gate
.q
[0] );
594 v4_copy( pother
->q
, rg
->gate
.q
[1] );
595 v2_copy( inf
->dims
, rg
->gate
.dims
);
597 gate_transform_update( &rg
->gate
);
598 rn
->special_type
= k_route_special_type_gate
;
599 rn
->special_id
= r
->gate_count
;
605 if( rn
->special_type
== 0 )
607 r
->collectors
= buffer_reserve(
608 r
->collectors
, r
->collector_count
, &r
->collector_cap
,
609 1, sizeof( struct route_collector
));
611 struct route_collector
*rc
= &r
->collectors
[r
->collector_count
];
612 rc
->time_passed
= 0.0;
614 rn
->special_type
= k_route_special_type_collector
;
615 rn
->special_id
= r
->collector_count
;
617 r
->collector_count
++;
622 struct classtype_route_node
*inf
= mdl_get_entdata( mdl
, pnode
);
623 rn
->next
[0] = inf
->target
;
624 rn
->next
[1] = inf
->target1
;
629 else if( pnode
->classtype
== k_classtype_route
)
631 struct classtype_route
*inf
= mdl_get_entdata( mdl
, pnode
);
632 r
->routes
= buffer_reserve( r
->routes
, r
->route_count
, &r
->route_cap
,
633 1, sizeof( struct route
) );
635 struct route
*route
= &r
->routes
[r
->route_count
];
637 v3_copy( inf
->colour
, route
->colour
);
638 route
->colour
[3] = 1.0f
;
641 route
->start
= inf
->id_start
;
643 route
->factive
= 0.0f
;
650 * Apply correct system-local ids
652 for( int i
=0; i
<r
->node_count
; i
++ )
654 struct route_node
*rn
= &r
->nodes
[i
];
656 for( int j
=0; j
<2; j
++ )
657 world_id_fixup( &rn
->next
[j
], mdl
);
660 for( int i
=0; i
<r
->route_count
; i
++ )
662 struct route
*route
= &r
->routes
[i
];
663 world_id_fixup( &route
->start
, mdl
);
669 for( int i
=0; i
<r
->route_count
; i
++ )
671 struct route
*route
= &r
->routes
[i
];
674 u32 si
= world_routes_get_path( route
->start
, stack
);
676 for( int sj
=0; sj
<si
; sj
++ )
678 struct route_node
*rn
= &r
->nodes
[ stack
[sj
] ];
679 rn
->route_ids
[ rn
->ref_count
++ ] = i
;
681 if( rn
->ref_count
> 4 )
682 vg_warn( "Too many references on route node %i\n", i
);
686 world_routes_gen_meshes();
689 #endif /* ROUTES_H */