i hope your hapy
[carveJwlIkooP6JGAAIwe30JlM.git] / world_routes.h
1 /*
2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef ROUTES_H
6 #define ROUTES_H
7
8 #include <time.h>
9 #include "world.h"
10 #include "world_gate.h"
11
12 #if 0
13 #include "shaders/vblend.h"
14 #endif
15
16 #include "shaders/scene_route.h"
17 #include "shaders/routeui.h"
18
19
20 VG_STATIC void world_routes_local_set_record( world_instance *world,
21 u32 route, double lap_time )
22 {
23 #if 0
24 vg_success( " NEW LAP TIME: %f\n", lap_time );
25
26 if( pr->track_id != 0xffffffff )
27 {
28 double time_centiseconds = lap_time * 100.0;
29 if( time_centiseconds > (float)0xfffe )
30 return;
31
32 highscore_record temp;
33 temp.trackid = pr->track_id;
34 temp.datetime = time(NULL);
35 temp.playerid = 0;
36 temp.points = 0;
37 temp.time = time_centiseconds;
38
39 highscores_push_record( &temp );
40
41 struct track_info *pti = &track_infos[ pr->track_id ];
42 pti->push = 1;
43
44 if( pti->achievement_id )
45 {
46 steam_set_achievement( pti->achievement_id );
47 steam_store_achievements();
48 }
49 }
50 else
51 {
52 vg_warn( "There is no associated track for this record...\n" );
53 }
54 #endif
55 vg_warn( "set_record unimplemented\n" );
56 }
57
58
59 VG_STATIC void world_routes_clear( world_instance *world )
60 {
61 for( u32 i=0; i<mdl_arrcount( &world->ent_route ); i++ ){
62 ent_route *route = mdl_arritm( &world->ent_route, i );
63 route->active_checkpoint = 0xffffffff;
64 }
65
66 world_global.current_run_version += 4;
67 world_global.last_use = 0.0;
68 }
69
70 /*
71 * When going through a gate this is called for bookkeeping purposes
72 */
73 VG_STATIC void world_routes_activate_entry_gate( world_instance *world,
74 ent_gate *rg )
75 {
76 ent_gate *dest = mdl_arritm( &world->ent_gate, rg->target );
77
78 world_global.last_use = world_global.time;
79 rg->timing_version = world_global.current_run_version;
80 rg->timing_time = world_global.time;
81
82 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
83 ent_route *route = mdl_arritm( &world->ent_route, i );
84
85 u32 active_prev = route->active_checkpoint;
86 route->active_checkpoint = 0xffffffff;
87
88 for( u32 j=0; j<4; j++ ){
89 if( dest->routes[j] == i ){
90 for( u32 k=0; k<route->checkpoints_count; k++ ){
91 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint,
92 route->checkpoints_start+k );
93
94 ent_gate *gk = mdl_arritm( &world->ent_gate, cp->gate_index );
95 if( gk == rg ){
96 route->active_checkpoint = k;
97 break;
98 }
99 }
100 break;
101 }
102 }
103 }
104
105 world_global.current_run_version ++;
106
107 dest->timing_version = world_global.current_run_version;
108 dest->timing_time = world_global.time;
109 world_global.current_run_version ++;
110 }
111
112 /* draw lines along the paths */
113 VG_STATIC void world_routes_debug( world_instance *world )
114 {
115 for( u32 i=0; i<mdl_arrcount(&world->ent_route_node); i++ ){
116 ent_route_node *rn = mdl_arritm(&world->ent_route_node,i);
117 vg_line_pt3( rn->co, 0.25f, VG__WHITE );
118 }
119
120 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
121 ent_route *route = mdl_arritm(&world->ent_route, i);
122
123 u32 colours[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
124 0xff5442f5 };
125
126 u32 cc = 0xffcccccc;
127 if( route->active_checkpoint != 0xffffffff ){
128 cc = colours[i%vg_list_size(colours)];
129 }
130
131 for( int i=0; i<route->checkpoints_count; i++ ){
132 int i0 = route->checkpoints_start+i,
133 i1 = route->checkpoints_start+((i+1)%route->checkpoints_count);
134
135 ent_checkpoint *c0 = mdl_arritm(&world->ent_checkpoint, i0),
136 *c1 = mdl_arritm(&world->ent_checkpoint, i1);
137
138 ent_gate *start_gate = mdl_arritm( &world->ent_gate, c0->gate_index );
139 ent_gate *end_gate = mdl_arritm( &world->ent_gate, c1->gate_index );
140
141 v3f p0, p1;
142 v3_copy( start_gate->co[1], p0 );
143
144 for( int j=0; j<c0->path_count; j ++ ){
145 ent_path_index *index = mdl_arritm( &world->ent_path_index,
146 c0->path_start+j );
147
148 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
149 index->index );
150
151 v3_copy( rn->co, p1 );
152 vg_line( p0, p1, cc );
153 v3_copy( p1, p0 );
154 }
155
156 v3_copy( end_gate->co[0], p1 );
157 vg_line( p0, p1, cc );
158 }
159 }
160 }
161
162 VG_STATIC void world_routes_place_curve( world_instance *world,
163 v4f h[4], v3f n0, v3f n2 )
164 {
165 float t;
166 v3f p, pd;
167 int last_valid;
168
169 float total_length = 0.0f,
170 travel_length = 0.0;
171
172 v3f last;
173 v3_copy( h[0], last );
174 for( int it=0; it<128; it ++ ){
175 t = (float)(it+1) * (1.0f/128.0f);
176 eval_bezier3( h[0], h[1], h[2], t, p );
177 total_length += v3_dist( p, last );
178 v3_copy( p, last );
179 }
180
181 float patch_size = 4.0f,
182 patch_count = ceilf( total_length / patch_size );
183
184 t = 0.0f;
185 v3_copy( h[0], last );
186
187 for( int it=0; it<128; it ++ ){
188 float const k_sample_dist = 0.0025f,
189 k_line_width = 1.5f;
190
191 eval_bezier3( h[0], h[1], h[2], t, p );
192 eval_bezier3( h[0], h[1], h[2], t+k_sample_dist, pd );
193
194 travel_length += v3_dist( p, last );
195
196 float mod = k_sample_dist / v3_dist( p, pd );
197
198 v3f v0,up, right;
199
200 v3_muls( n0, -(1.0f-t), up );
201 v3_muladds( up, n2, -t, up );
202 v3_normalize( up );
203
204 v3_sub( pd,p,v0 );
205 v3_cross( up, v0, right );
206 v3_normalize( right );
207
208 float cur_x = (1.0f-t)*h[0][3] + t*h[2][3];
209
210 v3f sc, sa, sb, down;
211 v3_muladds( p, right, cur_x * k_line_width, sc );
212 v3_muladds( sc, up, 1.5f, sc );
213 v3_muladds( sc, right, k_line_width*0.95f, sa );
214 v3_muladds( sc, right, 0.0f, sb );
215 v3_muls( up, -1.0f, down );
216
217 ray_hit ha, hb;
218 ha.dist = 8.0f;
219 hb.dist = 8.0f;
220 if( ray_world( world, sa, down, &ha ) &&
221 ray_world( world, sb, down, &hb ))
222 {
223 scene_vert va, vb;
224
225 v3_muladds( ha.pos, up, 0.06f, va.co );
226 v3_muladds( hb.pos, up, 0.06f, vb.co );
227
228 scene_vert_pack_norm( &va, up );
229 scene_vert_pack_norm( &vb, up );
230
231 float t1 = (travel_length / total_length) * patch_count;
232 va.uv[0] = t1;
233 va.uv[1] = 0.0f;
234 vb.uv[0] = t1;
235 vb.uv[1] = 1.0f;
236
237 scene_push_vert( world->scene_lines, &va );
238 scene_push_vert( world->scene_lines, &vb );
239
240 if( last_valid ){
241 /* Connect them with triangles */
242 scene_push_tri( world->scene_lines, (u32[3]){
243 last_valid+0-2, last_valid+1-2, last_valid+2-2} );
244 scene_push_tri( world->scene_lines, (u32[3]){
245 last_valid+1-2, last_valid+3-2, last_valid+2-2} );
246 }
247
248 last_valid = world->scene_lines->vertex_count;
249 }
250 else
251 last_valid = 0;
252
253 if( t == 1.0f )
254 return;
255
256 t += 1.0f*mod;
257 if( t > 1.0f )
258 t = 1.0f;
259
260 v3_copy( p, last );
261 }
262 }
263
264 VG_STATIC void world_routes_create_mesh( world_instance *world, u32 route_id )
265 {
266 ent_route *route = mdl_arritm( &world->ent_route, route_id );
267 u32 last_valid = 0;
268
269 for( int i=0; i<route->checkpoints_count; i++ ){
270 int i0 = route->checkpoints_start+i,
271 i1 = route->checkpoints_start+((i+1)%route->checkpoints_count);
272
273 ent_checkpoint *c0 = mdl_arritm(&world->ent_checkpoint, i0),
274 *c1 = mdl_arritm(&world->ent_checkpoint, i1);
275
276 ent_gate *start_gate = mdl_arritm( &world->ent_gate, c0->gate_index );
277 start_gate = mdl_arritm( &world->ent_gate, start_gate->target );
278
279 ent_gate *end_gate = mdl_arritm( &world->ent_gate, c1->gate_index );
280
281 v4f p[3];
282
283 v3_add( (v3f){0.0f,0.1f,0.0f}, start_gate->co[0], p[0] );
284 p[0][3] = start_gate->ref_count;
285 p[0][3] -= (float)start_gate->ref_total * 0.5f;
286 start_gate->ref_count ++;
287
288 if( !c0->path_count )
289 continue;
290
291 /* this is so that we get nice flow through the gates */
292 v3f temp_alignments[2];
293 ent_gate *both[] = { start_gate, end_gate };
294
295 for( int j=0; j<2; j++ ){
296 int pi = c0->path_start + ((j==1)? c0->path_count-1: 0);
297
298 ent_path_index *index = mdl_arritm( &world->ent_path_index, pi );
299 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
300 index->index );
301 v3f v0;
302 v3_sub( rn->co, both[j]->co[0], v0 );
303 float d = v3_dot( v0, both[j]->to_world[2] );
304
305 v3_muladds( both[j]->co[0], both[j]->to_world[2], d,
306 temp_alignments[j] );
307 v3_add( (v3f){0.0f,0.1f,0.0f}, temp_alignments[j], temp_alignments[j]);
308 }
309
310
311 for( int j=0; j<c0->path_count; j ++ ){
312 ent_path_index *index = mdl_arritm( &world->ent_path_index,
313 c0->path_start+j );
314 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
315 index->index );
316 if( j==0 || j==c0->path_count-1 )
317 if( j == 0 )
318 v3_copy( temp_alignments[0], p[1] );
319 else
320 v3_copy( temp_alignments[1], p[1] );
321 else
322 v3_copy( rn->co, p[1] );
323
324 p[1][3] = rn->ref_count;
325 p[1][3] -= (float)rn->ref_total * 0.5f;
326 rn->ref_count ++;
327
328 if( j+1 < c0->path_count ){
329 index = mdl_arritm( &world->ent_path_index,
330 c0->path_start+j+1 );
331 rn = mdl_arritm( &world->ent_route_node, index->index );
332
333 if( j+1 == c0->path_count-1 )
334 v3_lerp( p[1], temp_alignments[1], 0.5f, p[2] );
335 else
336 v3_lerp( p[1], rn->co, 0.5f, p[2] );
337
338 p[2][3] = rn->ref_count;
339 p[2][3] -= (float)rn->ref_total * 0.5f;
340 }
341 else{
342 v3_copy( end_gate->co[0], p[2] );
343 v3_add( (v3f){0.0f,0.1f,0.0f}, p[2], p[2] );
344 p[2][3] = end_gate->ref_count;
345 p[2][3] -= (float)end_gate->ref_total * 0.5f;
346 end_gate->ref_count ++;
347 }
348
349 /* p0,p1,p2 bezier patch is complete
350 * --------------------------------------*/
351 v3f surf0, surf2, n0, n2;
352
353 if( bh_closest_point( world->geo_bh, p[0], surf0, 5.0f ) == -1 )
354 v3_add( (v3f){0.0f,-0.1f,0.0f}, p[0], surf0 );
355
356 if( bh_closest_point( world->geo_bh, p[2], surf2, 5.0f ) == -1 )
357 v3_add( (v3f){0.0f,-0.1f,0.0f}, p[2], surf2 );
358
359 v3_sub( surf0, p[0], n0 );
360 v3_sub( surf2, p[2], n2 );
361 v3_normalize( n0 );
362 v3_normalize( n2 );
363
364 world_routes_place_curve( world, p, n0, n2 );
365
366 /* --- */
367 v4_copy( p[2], p[0] );
368 }
369 }
370
371 scene_copy_slice( world->scene_lines, &route->sm );
372 }
373
374 /*
375 * Create the strips of colour that run through the world along course paths
376 */
377 VG_STATIC void world_routes_generate( world_instance *world )
378 {
379 vg_info( "Generating route meshes\n" );
380 world->scene_lines = scene_init( world_global.generic_heap, 200000, 300000 );
381
382 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
383 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
384 gate->ref_count = 0;
385 gate->ref_total = 0;
386 }
387
388 for( u32 i=0; i<mdl_arrcount(&world->ent_route_node); i++ ){
389 ent_route_node *rn = mdl_arritm( &world->ent_route, i );
390 rn->ref_count = 0;
391 rn->ref_total = 0;
392 }
393
394 for( u32 k=0; k<mdl_arrcount(&world->ent_route); k++ ){
395 ent_route *route = mdl_arritm( &world->ent_route, k );
396
397 for( int i=0; i<route->checkpoints_count; i++ ){
398 int i0 = route->checkpoints_start+i,
399 i1 = route->checkpoints_start+((i+1)%route->checkpoints_count);
400
401 ent_checkpoint *c0 = mdl_arritm(&world->ent_checkpoint, i0),
402 *c1 = mdl_arritm(&world->ent_checkpoint, i1);
403
404 ent_gate *start_gate = mdl_arritm( &world->ent_gate, c0->gate_index );
405 start_gate = mdl_arritm( &world->ent_gate, start_gate->target );
406
407 ent_gate *end_gate = mdl_arritm( &world->ent_gate, c1->gate_index );
408 start_gate->ref_total ++;
409
410 if( !c0->path_count )
411 continue;
412
413 for( int j=0; j<c0->path_count; j ++ ){
414 ent_path_index *index = mdl_arritm( &world->ent_path_index,
415 c0->path_start+j );
416 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
417 index->index );
418 rn->ref_total ++;
419
420 if( j+1 < c0->path_count ){
421 }
422 else{
423 end_gate->ref_total ++;
424 }
425 }
426 }
427 }
428
429 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ )
430 world_routes_create_mesh( world, i );
431
432 vg_acquire_thread_sync();
433 {
434 scene_upload( world->scene_lines, &world->mesh_route_lines );
435 }
436 vg_release_thread_sync();
437 vg_linear_del( world_global.generic_heap, world->scene_lines );
438 }
439
440 /* load all routes from model header */
441 VG_STATIC void world_routes_ent_init( world_instance *world )
442 {
443 vg_info( "Initializing routes\n" );
444
445 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
446 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
447 for( u32 j=0; j<4; j++ ){
448 gate->routes[j] = 0xffff;
449 }
450 }
451
452 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
453 ent_route *route = mdl_arritm(&world->ent_route,i);
454
455 for( u32 j=0; j<route->checkpoints_count; j++ ){
456 u32 id = route->checkpoints_start + j;
457 ent_checkpoint *cp = mdl_arritm(&world->ent_checkpoint,id);
458
459 ent_gate *gate = mdl_arritm( &world->ent_gate, cp->gate_index );
460
461 for( u32 k=0; k<4; k++ ){
462 if( gate->routes[k] == 0xffff ){
463 gate->routes[k] = i;
464 break;
465 }
466 }
467
468 if( gate->type == k_gate_type_teleport ){
469 gate = mdl_arritm(&world->ent_gate, gate->target );
470
471 for( u32 k=0; k<4; k++ ){
472 if( gate->routes[k] == 0xffff ){
473 gate->routes[k] = i;
474 break;
475 }
476 }
477 }
478 }
479 }
480
481 world_routes_clear( world );
482 }
483
484 /*
485 * -----------------------------------------------------------------------------
486 * Events
487 * -----------------------------------------------------------------------------
488 */
489
490 VG_STATIC void world_routes_init(void)
491 {
492 world_global.current_run_version = 200;
493 world_global.time = RESET_MAX_TIME*2.0;
494 world_global.last_use = 0.0;
495
496 shader_scene_route_register();
497 shader_routeui_register();
498 }
499
500 VG_STATIC void world_routes_update( world_instance *world )
501 {
502 world_global.time += vg.time_delta;
503
504 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
505 ent_route *route = mdl_arritm( &world->ent_route, i );
506
507 int target = route->active_checkpoint == 0xffffffff? 0: 1;
508 route->factive = vg_lerpf( route->factive, target, 0.6f*vg.time_delta );
509 }
510 }
511
512 VG_STATIC void bind_terrain_noise(void);
513 VG_STATIC void world_bind_light_array( world_instance *world,
514 GLuint shader, GLuint location,
515 int slot );
516 VG_STATIC void world_bind_light_index( world_instance *world,
517 GLuint shader, GLuint location,
518 int slot );
519
520 VG_STATIC void render_world_routes( world_instance *world, camera *cam )
521 {
522 m4x3f identity_matrix;
523 m4x3_identity( identity_matrix );
524
525 shader_scene_route_use();
526 shader_scene_route_uTexGarbage(0);
527 world_link_lighting_ub( world, _shader_scene_route.id );
528 world_bind_position_texture( world, _shader_scene_route.id,
529 _uniform_scene_route_g_world_depth, 2 );
530 world_bind_light_array( world, _shader_scene_route.id,
531 _uniform_scene_route_uLightsArray, 3 );
532 world_bind_light_index( world, _shader_scene_route.id,
533 _uniform_scene_route_uLightsIndex, 4 );
534 bind_terrain_noise();
535
536 shader_scene_route_uPv( cam->mtx.pv );
537 shader_scene_route_uPvmPrev( cam->mtx_prev.pv );
538 shader_scene_route_uMdl( identity_matrix );
539 shader_scene_route_uCamera( cam->transform[3] );
540 shader_scene_route_uBoard0( TEMP_BOARD_0 );
541 shader_scene_route_uBoard1( TEMP_BOARD_1 );
542
543 mesh_bind( &world->mesh_route_lines );
544
545 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
546 ent_route *route = mdl_arritm( &world->ent_route, i );
547
548 v4f colour;
549 v3_lerp( (v3f){0.7f,0.7f,0.7f}, route->colour, route->factive, colour );
550 colour[3] = route->factive*0.2f;
551
552 shader_scene_route_uColour( colour );
553 mdl_draw_submesh( &route->sm );
554 }
555
556 shader_model_gate_use();
557 shader_model_gate_uPv( cam->mtx.pv );
558 shader_model_gate_uCam( cam->pos );
559 shader_model_gate_uTime( vg.time*0.25f );
560 shader_model_gate_uInvRes( (v2f){
561 1.0f / (float)vg.window_x,
562 1.0f / (float)vg.window_y });
563
564 mesh_bind( &world_global.mesh_gate );
565
566 /* skip writing into the motion vectors for this */
567 glDrawBuffers( 1, (GLenum[]){ GL_COLOR_ATTACHMENT0 } );
568
569 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
570 ent_route *route = mdl_arritm( &world->ent_route, i );
571
572 if( route->active_checkpoint != 0xffffffff ){
573 v4f colour;
574 float brightness = 0.3f + world->ub_lighting.g_day_phase;
575 v3_muls( route->colour, brightness, colour );
576 colour[3] = 1.0f-route->factive;
577
578 shader_model_gate_uColour( colour );
579
580 u32 next = route->checkpoints_start +
581 (route->active_checkpoint+1) % route->checkpoints_count;
582
583 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint, next );
584 ent_gate *gate = mdl_arritm( &world->ent_gate, cp->gate_index );
585 shader_model_gate_uMdl( gate->to_world );
586
587 for( u32 j=0; j<4; j++ ){
588 if( gate->routes[j] == i ){
589 mdl_draw_submesh( &world_global.sm_gate_marker[j] );
590 break;
591 }
592 }
593 }
594 }
595 glDrawBuffers( 2, (GLenum[]){ GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 } );
596 }
597
598 #endif /* ROUTES_H */