8 #include "shaders/vblend.h"
9 #include "shaders/route.h"
11 struct subworld_routes
18 u32 is_gate
, gate_id
, current_refs
, ref_count
;
19 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
59 static struct subworld_routes
*subworld_routes(void);
61 static void debug_sbpath( struct route_node
*rna
, struct route_node
*rnb
,
62 u32 colour
, float xoffset
)
64 v3f p0
, h0
, p1
, h1
, l
, p
;
66 v3_copy( rna
->co
, p0
);
67 v3_muladds( rna
->co
, rna
->h
, 1.0f
, h0
);
68 v3_copy( rnb
->co
, p1
);
69 v3_muladds( rnb
->co
, rnb
->h
, -1.0f
, h1
);
71 v3_muladds( p0
, rna
->right
, xoffset
, p0
);
72 v3_muladds( h0
, rna
->right
, xoffset
, h0
);
73 v3_muladds( p1
, rnb
->right
, xoffset
, p1
);
74 v3_muladds( h1
, rnb
->right
, xoffset
, h1
);
78 for( int i
=0; i
<5; i
++ )
80 float t
= (float)(i
+1)/5.0f
;
81 eval_bezier_time( p0
, p1
, h0
, h1
, t
, p
);
82 vg_line( p
, l
, colour
);
87 static void world_routes_activate_gate( u32 id
)
89 struct subworld_routes
*r
= subworld_routes();
90 struct route_gate
*ig
= &r
->gates
[id
];
91 struct route_node
*pnode
= &r
->nodes
[ig
->node_id
],
92 *pdest
= &r
->nodes
[pnode
->next
[0]];
96 for( int i
=0; i
<r
->route_count
; i
++ )
98 struct route
*route
= &r
->routes
[i
];
101 for( int j
=0; j
<pdest
->ref_count
; j
++ )
103 if( pdest
->route_ids
[j
] == i
)
112 static u32
world_routes_get_path( struct route
*route
, u32 stack
[64] )
114 struct subworld_routes
*r
= subworld_routes();
117 stack
[0] = route
->start
;
121 int loop_complete
= 0;
125 if( stack_i
[si
-1] == 2 )
131 struct route_node
*rn
= &r
->nodes
[stack
[si
-1]];
132 u32 nextid
= rn
->next
[stack_i
[si
-1]];
135 if( nextid
!= 0xffffffff )
137 if( nextid
== stack
[0] )
144 for( int sj
=0; sj
<si
; sj
++ )
146 if( stack
[sj
] == nextid
)
169 static void world_routes_debug(void)
171 struct subworld_routes
*r
= subworld_routes();
173 for( int i
=0; i
<r
->node_count
; i
++ )
175 struct route_node
*rn
= &r
->nodes
[i
];
176 vg_line_pt3( rn
->co
, 1.0f
, rn
->is_gate
? 0xffffff00: 0xff00b2ff );
179 for( int i
=0; i
<r
->route_count
; i
++ )
181 struct route
*route
= &r
->routes
[i
];
184 u32 si
= world_routes_get_path( route
, stack
);
186 u32 colours
[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
189 u32 cc
= colours
[i
%vg_list_size(colours
)];
191 for( int sj
=0; sj
<si
; sj
++ )
194 debug_sbpath( &r
->nodes
[stack
[sj
]], &r
->nodes
[stack
[sk
]], cc
,
199 for( int i
=0; i
<r
->node_count
; i
++ )
201 struct route_node
*ri
= &r
->nodes
[i
],
204 for( int j
=0; j
<2; j
++ )
206 if( ri
->next
[j
] != 0xffffffff )
208 rj
= &r
->nodes
[ri
->next
[j
]];
209 vg_line( ri
->co
, rj
->co
, 0x20ffffff );
215 static void world_routes_free(void)
217 struct subworld_routes
*r
= subworld_routes();
224 static void world_id_fixup( u32
*uid
, mdl_header
*mdl
)
227 *uid
= mdl_node_from_id( mdl
, *uid
)->sub_uid
;
232 static void world_routes_gen_meshes(void)
234 struct subworld_routes
*r
= subworld_routes();
235 scene_init( &r
->scene_lines
);
237 for( int i
=0; i
<r
->route_count
; i
++ )
239 struct route
*route
= &r
->routes
[i
];
242 u32 si
= world_routes_get_path( route
, stack
);
246 for( int sj
=0; sj
<si
; sj
++ )
250 struct route_node
*rnj
= &r
->nodes
[ stack
[sj
] ],
251 *rnk
= &r
->nodes
[ stack
[sk
] ],
254 if( rnj
->is_gate
&& rnk
->is_gate
)
260 float base_x0
= (float)rnj
->ref_count
*-0.5f
+ (float)rnj
->current_refs
,
261 base_x1
= (float)rnk
->ref_count
*-0.5f
+ (float)rnk
->current_refs
;
265 rnl
= &r
->nodes
[ rnk
->next
[0] ];
266 base_x1
= (float)rnl
->ref_count
*-0.5f
+ (float)rnl
->current_refs
;
274 v3f p0
, h0
, p1
, h1
, p
, pd
;
276 v3_copy( rnj
->co
, p0
);
277 v3_muladds( rnj
->co
, rnj
->h
, 1.0f
, h0
);
278 v3_copy( rnk
->co
, p1
);
279 v3_muladds( rnk
->co
, rnk
->h
, -1.0f
, h1
);
284 for( int it
=0; it
<256; it
++ )
286 float const k_sample_dist
= 0.02f
;
287 eval_bezier_time( p0
,p1
,h0
,h1
, t
,p
);
288 eval_bezier_time( p0
,p1
,h0
,h1
, t
+k_sample_dist
,pd
);
290 float mod
= k_sample_dist
/ v3_dist( p
, pd
);
293 v3_muls( rnj
->up
, 1.0f
-t
, up
);
294 v3_muladds( up
, rnk
->up
, t
, up
);
297 v3_cross( up
, v0
, right
);
298 v3_normalize( right
);
300 float cur_x
= (1.0f
-t
)*base_x0
+ t
*base_x1
;
302 v3f sc
, sa
, sb
, down
;
303 v3_muladds( p
, right
, cur_x
, sc
);
304 v3_muladds( sc
, up
, 1.5f
, sc
);
305 v3_muladds( sc
, right
, 0.45f
, sa
);
306 v3_muladds( sc
, right
, -0.45f
, sb
);
307 v3_muls( up
, -1.0f
, down
);
312 if(ray_world( sa
, down
, &ha
) &&
313 ray_world( sb
, down
, &hb
))
317 v3_muladds( ha
.pos
, up
, 0.06f
, va
.co
);
318 v3_muladds( hb
.pos
, up
, 0.06f
, vb
.co
);
319 v3_copy( up
, va
.norm
);
320 v3_copy( up
, vb
.norm
);
321 v3_zero( va
.colour
);
322 v3_zero( vb
.colour
);
326 scene_push_vert( &r
->scene_lines
, &va
);
327 scene_push_vert( &r
->scene_lines
, &vb
);
331 /* Connect them with triangles */
332 scene_push_tri( &r
->scene_lines
, (u32
[3]){
333 last_valid
+0-2, last_valid
+1-2, last_valid
+2-2} );
334 scene_push_tri( &r
->scene_lines
, (u32
[3]){
335 last_valid
+1-2, last_valid
+3-2, last_valid
+2-2} );
338 last_valid
= r
->scene_lines
.vertex_count
;
347 /* TODO special case for end of loop, need to add triangles
348 * between first and last rungs */
353 rnj
->current_refs
++;
356 scene_copy_slice( &r
->scene_lines
, &route
->sm
);
359 scene_upload( &r
->scene_lines
);
360 scene_free_offline_buffers( &r
->scene_lines
);
363 static void bind_terrain_textures(void);
364 static void render_world_routes( m4x4f projection
, v3f camera
)
366 struct subworld_routes
*r
= subworld_routes();
368 m4x3f identity_matrix
;
369 m4x3_identity( identity_matrix
);
372 shader_route_uTexGarbage(0);
373 shader_link_standard_ub( _shader_route
.id
, 2 );
374 bind_terrain_textures();
376 shader_route_uPv( projection
);
377 shader_route_uMdl( identity_matrix
);
378 shader_route_uCamera( camera
);
380 scene_bind( &r
->scene_lines
);
382 for( int i
=0; i
<r
->route_count
; i
++ )
384 struct route
*route
= &r
->routes
[i
];
385 route
->factive
= vg_lerpf( route
->factive
, route
->active
, 0.01f
);
388 v3_lerp( (v3f
){0.7f
,0.7f
,0.7f
}, route
->colour
, route
->factive
, colour
);
391 shader_route_uColour( colour
);
392 mdl_draw_submesh( &route
->sm
);
396 static void world_routes_register(void)
398 shader_route_register();
401 static void world_routes_loadfrom( mdl_header
*mdl
)
403 struct subworld_routes
*r
= subworld_routes();
414 for( int i
=0; i
<mdl
->node_count
; i
++ )
416 mdl_node
*pnode
= mdl_node_from_id(mdl
,i
);
419 if( pnode
->classtype
== k_classtype_route_node
||
420 pnode
->classtype
== k_classtype_gate
)
422 mdl_node_transform( pnode
, transform
);
423 pnode
->sub_uid
= r
->node_count
;
425 r
->nodes
= buffer_reserve( r
->nodes
, r
->node_count
, &r
->node_cap
, 1,
426 sizeof( struct route_node
) );
428 struct route_node
*rn
= &r
->nodes
[r
->node_count
];
430 v3_copy( transform
[0], rn
->right
);
431 v3_normalize( rn
->right
);
432 v3_copy( transform
[1], rn
->up
);
433 v3_normalize( rn
->up
);
434 v3_muls( transform
[2], -1.0f
, rn
->h
);
435 v3_copy( transform
[3], rn
->co
);
437 rn
->current_refs
= 0;
439 if( pnode
->classtype
== k_classtype_gate
)
441 r
->gates
= buffer_reserve( r
->gates
, r
->gate_count
, &r
->gate_cap
,
442 1, sizeof( struct route_gate
) );
444 struct classtype_gate
*inf
= mdl_get_entdata( mdl
, pnode
);
446 /* H is later scaled based on link distance */
447 v3_normalize( rn
->h
);
448 rn
->next
[0] = inf
->target
;
450 rn
->gate_id
= r
->gate_count
;
456 mdl_node
*pother
= mdl_node_from_id( mdl
, inf
->target
);
458 if( pother
->classtype
== k_classtype_gate
)
460 struct route_gate
*rg
= &r
->gates
[r
->gate_count
];
461 rg
->node_id
= r
->node_count
;
463 v3_copy( pnode
->co
, rg
->gate
.co
[0] );
464 v3_copy( pother
->co
, rg
->gate
.co
[1] );
465 v4_copy( pnode
->q
, rg
->gate
.q
[0] );
466 v4_copy( pother
->q
, rg
->gate
.q
[1] );
467 v2_copy( inf
->dims
, rg
->gate
.dims
);
469 gate_transform_update( &rg
->gate
);
477 struct classtype_route_node
*inf
= mdl_get_entdata( mdl
, pnode
);
478 rn
->next
[0] = inf
->target
;
479 rn
->next
[1] = inf
->target1
;
485 else if( pnode
->classtype
== k_classtype_route
)
487 struct classtype_route
*inf
= mdl_get_entdata( mdl
, pnode
);
488 r
->routes
= buffer_reserve( r
->routes
, r
->route_count
, &r
->route_cap
,
489 1, sizeof( struct route
) );
491 struct route
*route
= &r
->routes
[r
->route_count
];
493 v3_copy( inf
->colour
, route
->colour
);
494 route
->colour
[3] = 1.0f
;
497 route
->start
= inf
->id_start
;
499 route
->factive
= 0.0f
;
506 * Apply correct system-local ids
508 for( int i
=0; i
<r
->node_count
; i
++ )
510 struct route_node
*rn
= &r
->nodes
[i
];
512 for( int j
=0; j
<2; j
++ )
513 world_id_fixup( &rn
->next
[j
], mdl
);
516 for( int i
=0; i
<r
->route_count
; i
++ )
518 struct route
*route
= &r
->routes
[i
];
519 world_id_fixup( &route
->start
, mdl
);
525 for( int i
=0; i
<r
->route_count
; i
++ )
527 struct route
*route
= &r
->routes
[i
];
530 u32 si
= world_routes_get_path( route
, stack
);
532 for( int sj
=0; sj
<si
; sj
++ )
534 struct route_node
*rn
= &r
->nodes
[ stack
[sj
] ];
535 rn
->route_ids
[ rn
->ref_count
++ ] = i
;
537 if( rn
->ref_count
> 4 )
538 vg_warn( "Too many references on route node %i\n", i
);
542 world_routes_gen_meshes();
545 #endif /* ROUTES_H */