sfd
[carveJwlIkooP6JGAAIwe30JlM.git] / world_routes.h
1 #ifndef ROUTES_H
2 #define ROUTES_H
3
4 #include "common.h"
5 #include "model.h"
6 #include "gate.h"
7
8 #include "shaders/vblend.h"
9 #include "shaders/route.h"
10
11 struct subworld_routes
12 {
13 struct route_node
14 {
15 v3f co, right, up, h;
16 u32 next[2];
17
18 u32 is_gate, gate_id, current_refs, ref_count;
19 u32 route_ids[4]; /* Gates can be linked into up to four routes */
20 }
21 *nodes;
22
23 u32 node_count,
24 node_cap;
25
26 struct route
27 {
28 const char *name;
29 v4f colour;
30
31 u32 start;
32 mdl_submesh sm;
33
34 int active;
35 float factive;
36 }
37 *routes;
38
39 u32 route_count,
40 route_cap;
41
42 struct route_gate
43 {
44 teleport_gate gate;
45
46 u32 route_count,
47 node_id;
48 }
49 *gates;
50
51 u32 gate_count,
52 gate_cap;
53
54 u32 active_gate;
55
56 scene scene_lines;
57 };
58
59 static struct subworld_routes *subworld_routes(void);
60
61 static void debug_sbpath( struct route_node *rna, struct route_node *rnb,
62 u32 colour, float xoffset )
63 {
64 v3f p0, h0, p1, h1, l, p;
65
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 );
70
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 );
75
76 v3_copy( p0, l );
77
78 for( int i=0; i<5; i++ )
79 {
80 float t = (float)(i+1)/5.0f;
81 eval_bezier_time( p0, p1, h0, h1, t, p );
82 vg_line( p, l, colour );
83 v3_copy( p, l );
84 }
85 }
86
87 static void world_routes_activate_gate( u32 id )
88 {
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]];
93
94 r->active_gate = id;
95
96 for( int i=0; i<r->route_count; i++ )
97 {
98 struct route *route = &r->routes[i];
99
100 route->active = 0;
101 for( int j=0; j<pdest->ref_count; j++ )
102 {
103 if( pdest->route_ids[j] == i )
104 {
105 route->active = 1;
106 break;
107 }
108 }
109 }
110 }
111
112 static u32 world_routes_get_path( struct route *route, u32 stack[64] )
113 {
114 struct subworld_routes *r = subworld_routes();
115 u32 stack_i[64];
116
117 stack[0] = route->start;
118 stack_i[0] = 0;
119
120 u32 si = 1;
121 int loop_complete = 0;
122
123 while( si )
124 {
125 if( stack_i[si-1] == 2 )
126 {
127 si --;
128 continue;
129 }
130
131 struct route_node *rn = &r->nodes[stack[si-1]];
132 u32 nextid = rn->next[stack_i[si-1]];
133 stack_i[si-1] ++;
134
135 if( nextid != 0xffffffff )
136 {
137 if( nextid == stack[0] )
138 {
139 loop_complete = 1;
140 break;
141 }
142
143 int valid = 1;
144 for( int sj=0; sj<si; sj++ )
145 {
146 if( stack[sj] == nextid )
147 {
148 valid = 0;
149 break;
150 }
151 }
152
153 if( valid )
154 {
155 stack_i[si] = 0;
156 stack[si] = nextid;
157 si ++;
158 continue;
159 }
160 }
161 }
162
163 if( loop_complete )
164 return si;
165
166 return 0;
167 }
168
169 static void world_routes_debug(void)
170 {
171 struct subworld_routes *r = subworld_routes();
172
173 for( int i=0; i<r->node_count; i++ )
174 {
175 struct route_node *rn = &r->nodes[i];
176 vg_line_pt3( rn->co, 1.0f, rn->is_gate? 0xffffff00: 0xff00b2ff );
177 }
178
179 for( int i=0; i<r->route_count; i++ )
180 {
181 struct route *route = &r->routes[i];
182
183 u32 stack[64];
184 u32 si = world_routes_get_path( route, stack );
185
186 u32 colours[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
187 0xff5442f5 };
188
189 u32 cc = colours[i%vg_list_size(colours)];
190
191 for( int sj=0; sj<si; sj++ )
192 {
193 int sk = (sj+1)%si;
194 debug_sbpath( &r->nodes[stack[sj]], &r->nodes[stack[sk]], cc,
195 (float)i );
196 }
197 }
198
199 for( int i=0; i<r->node_count; i++ )
200 {
201 struct route_node *ri = &r->nodes[i],
202 *rj = NULL;
203
204 for( int j=0; j<2; j++ )
205 {
206 if( ri->next[j] != 0xffffffff )
207 {
208 rj = &r->nodes[ri->next[j]];
209 vg_line( ri->co, rj->co, 0x20ffffff );
210 }
211 }
212 }
213 }
214
215 static void world_routes_free(void)
216 {
217 struct subworld_routes *r = subworld_routes();
218
219 free( r->nodes );
220 free( r->routes );
221 free( r->gates );
222 }
223
224 static void world_id_fixup( u32 *uid, mdl_header *mdl )
225 {
226 if( *uid )
227 *uid = mdl_node_from_id( mdl, *uid )->sub_uid;
228 else
229 *uid = 0xffffffff;
230 }
231
232 static void world_routes_gen_meshes(void)
233 {
234 struct subworld_routes *r = subworld_routes();
235 scene_init( &r->scene_lines );
236
237 for( int i=0; i<r->route_count; i++ )
238 {
239 struct route *route = &r->routes[i];
240
241 u32 stack[64];
242 u32 si = world_routes_get_path( route, stack );
243
244 u32 last_valid = 0;
245
246 for( int sj=0; sj<si; sj++ )
247 {
248 int sk=(sj+1)%si;
249
250 struct route_node *rnj = &r->nodes[ stack[sj] ],
251 *rnk = &r->nodes[ stack[sk] ],
252 *rnl;
253
254 if( rnj->is_gate && rnk->is_gate )
255 {
256 last_valid = 0;
257 continue;
258 }
259
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;
262
263 if( rnk->is_gate )
264 {
265 rnl = &r->nodes[ rnk->next[0] ];
266 base_x1 = (float)rnl->ref_count*-0.5f + (float)rnl->current_refs;
267 }
268
269 if( sk == 0 )
270 {
271 base_x1 -= 1.0f;
272 }
273
274 v3f p0, h0, p1, h1, p, pd;
275
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 );
280
281 float t=0.0f;
282 int it = 0;
283
284 for( int it=0; it<256; it ++ )
285 {
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 );
289
290 float mod = k_sample_dist / v3_dist( p, pd );
291
292 v3f v0,up, right;
293 v3_muls( rnj->up, 1.0f-t, up );
294 v3_muladds( up, rnk->up, t, up );
295
296 v3_sub( pd,p,v0 );
297 v3_cross( up, v0, right );
298 v3_normalize( right );
299
300 float cur_x = (1.0f-t)*base_x0 + t*base_x1;
301
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 );
308
309 ray_hit ha, hb;
310 ha.dist = 8.0f;
311 hb.dist = 8.0f;
312 if(ray_world( sa, down, &ha ) &&
313 ray_world( sb, down, &hb ))
314 {
315 mdl_vert va, vb;
316
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 );
323 v2_zero( va.uv );
324 v2_zero( vb.uv );
325
326 scene_push_vert( &r->scene_lines, &va );
327 scene_push_vert( &r->scene_lines, &vb );
328
329 if( last_valid )
330 {
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} );
336 }
337
338 last_valid = r->scene_lines.vertex_count;
339 }
340 else
341 last_valid = 0;
342
343 t += 1.0f*mod;
344
345 if( t >= 1.0f )
346 {
347 /* TODO special case for end of loop, need to add triangles
348 * between first and last rungs */
349 break;
350 }
351 }
352
353 rnj->current_refs ++;
354 }
355
356 scene_copy_slice( &r->scene_lines, &route->sm );
357 }
358
359 scene_upload( &r->scene_lines );
360 scene_free_offline_buffers( &r->scene_lines );
361 }
362
363 static void bind_terrain_textures(void);
364 static void render_world_routes( m4x4f projection, v3f camera )
365 {
366 struct subworld_routes *r = subworld_routes();
367
368 m4x3f identity_matrix;
369 m4x3_identity( identity_matrix );
370
371 shader_route_use();
372 shader_route_uTexGarbage(0);
373 shader_link_standard_ub( _shader_route.id, 2 );
374 bind_terrain_textures();
375
376 shader_route_uPv( projection );
377 shader_route_uMdl( identity_matrix );
378 shader_route_uCamera( camera );
379
380 scene_bind( &r->scene_lines );
381
382 for( int i=0; i<r->route_count; i++ )
383 {
384 struct route *route = &r->routes[i];
385 route->factive = vg_lerpf( route->factive, route->active, 0.01f );
386
387 v4f colour;
388 v3_lerp( (v3f){0.7f,0.7f,0.7f}, route->colour, route->factive, colour );
389 colour[3] = 1.0f;
390
391 shader_route_uColour( colour );
392 mdl_draw_submesh( &route->sm );
393 }
394 }
395
396 static void world_routes_register(void)
397 {
398 shader_route_register();
399 }
400
401 static void world_routes_loadfrom( mdl_header *mdl )
402 {
403 struct subworld_routes *r = subworld_routes();
404 r->nodes = NULL;
405 r->node_count = 0;
406 r->node_cap = 0;
407 r->routes = NULL;
408 r->route_count = 0;
409 r->route_cap = 0;
410 r->gates = NULL;
411 r->gate_count = 0;
412 r->gate_cap = 0;
413
414 for( int i=0; i<mdl->node_count; i++ )
415 {
416 mdl_node *pnode = mdl_node_from_id(mdl,i);
417 m4x3f transform;
418
419 if( pnode->classtype == k_classtype_route_node ||
420 pnode->classtype == k_classtype_gate )
421 {
422 mdl_node_transform( pnode, transform );
423 pnode->sub_uid = r->node_count;
424
425 r->nodes = buffer_reserve( r->nodes, r->node_count, &r->node_cap, 1,
426 sizeof( struct route_node ) );
427
428 struct route_node *rn = &r->nodes[r->node_count];
429
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 );
436 rn->ref_count = 0;
437 rn->current_refs = 0;
438
439 if( pnode->classtype == k_classtype_gate )
440 {
441 r->gates = buffer_reserve( r->gates, r->gate_count, &r->gate_cap,
442 1, sizeof( struct route_gate ) );
443
444 struct classtype_gate *inf = mdl_get_entdata( mdl, pnode );
445
446 /* H is later scaled based on link distance */
447 v3_normalize( rn->h );
448 rn->next[0] = inf->target;
449 rn->next[1] = 0;
450 rn->gate_id = r->gate_count;
451 rn->is_gate = 1;
452
453 /* TODO */
454 if( inf->target )
455 {
456 mdl_node *pother = mdl_node_from_id( mdl, inf->target );
457
458 if( pother->classtype == k_classtype_gate )
459 {
460 struct route_gate *rg = &r->gates[r->gate_count];
461 rg->node_id = r->node_count;
462
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 );
468
469 gate_transform_update( &rg->gate );
470
471 r->gate_count ++;
472 }
473 }
474 }
475 else
476 {
477 struct classtype_route_node *inf = mdl_get_entdata( mdl, pnode );
478 rn->next[0] = inf->target;
479 rn->next[1] = inf->target1;
480 rn->is_gate = 0;
481 }
482
483 r->node_count ++;
484 }
485 else if( pnode->classtype == k_classtype_route )
486 {
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 ) );
490
491 struct route *route = &r->routes[r->route_count];
492
493 v3_copy( inf->colour, route->colour );
494 route->colour[3] = 1.0f;
495
496 route->name = NULL;
497 route->start = inf->id_start;
498 route->active = 0;
499 route->factive = 0.0f;
500
501 r->route_count ++;
502 }
503 }
504
505 /*
506 * Apply correct system-local ids
507 */
508 for( int i=0; i<r->node_count; i++ )
509 {
510 struct route_node *rn = &r->nodes[i];
511
512 for( int j=0; j<2; j++ )
513 world_id_fixup( &rn->next[j], mdl );
514 }
515
516 for( int i=0; i<r->route_count; i++ )
517 {
518 struct route *route = &r->routes[i];
519 world_id_fixup( &route->start, mdl );
520 }
521
522 /*
523 * Gather references
524 */
525 for( int i=0; i<r->route_count; i++ )
526 {
527 struct route *route = &r->routes[i];
528
529 u32 stack[64];
530 u32 si = world_routes_get_path( route, stack );
531
532 for( int sj=0; sj<si; sj++ )
533 {
534 struct route_node *rn = &r->nodes[ stack[sj] ];
535 rn->route_ids[ rn->ref_count ++ ] = i;
536
537 if( rn->ref_count > 4 )
538 vg_warn( "Too many references on route node %i\n", i );
539 }
540 }
541
542 world_routes_gen_meshes();
543 }
544
545 #endif /* ROUTES_H */