grid based
[carveJwlIkooP6JGAAIwe30JlM.git] / world_gen.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef WORLD_GEN_H
6 #define WORLD_GEN_H
7
8 #include "world.h"
9
10 VG_STATIC void world_load( world_instance *world, const char *path );
11
12 VG_STATIC void world_add_all_if_material( m4x3f transform, scene *pscene,
13 mdl_context *mdl, u32 id )
14 {
15 for( int i=0; i<mdl->info.node_count; i++ )
16 {
17 mdl_node *pnode = mdl_node_from_id( mdl, i );
18
19 for( int j=0; j<pnode->submesh_count; j++ )
20 {
21 mdl_submesh *sm = mdl_node_submesh( mdl, pnode, j );
22 if( sm->material_id == id )
23 {
24 m4x3f transform2;
25 mdl_node_transform( pnode, transform2 );
26 m4x3_mul( transform, transform2, transform2 );
27
28 scene_add_mdl_submesh( pscene, mdl, sm, transform2 );
29 }
30 }
31 }
32 }
33
34 VG_STATIC void world_add_blob( world_instance *world,
35 scene *pscene, ray_hit *hit )
36 {
37 m4x3f transform;
38 v4f qsurface, qrandom;
39 v3f axis;
40
41 v3_cross( (v3f){0.0f,1.0f,0.0f}, hit->normal, axis );
42
43 float angle = v3_dot(hit->normal,(v3f){0.0f,1.0f,0.0f});
44 q_axis_angle( qsurface, axis, angle );
45 q_axis_angle( qrandom, (v3f){0.0f,1.0f,0.0f}, vg_randf()*VG_TAUf );
46 q_mul( qsurface, qrandom, qsurface );
47 q_m3x3( qsurface, transform );
48 v3_copy( hit->pos, transform[3] );
49
50 scene_vert verts[] =
51 {
52 { .co = { -1.00f, 0.0f, 0.0f } },
53 { .co = { 1.00f, 0.0f, 0.0f } },
54 { .co = { -1.00f, 1.2f, 0.0f } },
55 { .co = { 1.00f, 1.2f, 0.0f } },
56 { .co = { -0.25f, 2.0f, 0.0f } },
57 { .co = { 0.25f, 2.0f, 0.0f } }
58 };
59
60 const u32 indices[] = { 0,1,3, 0,3,2, 2,3,5, 2,5,4 };
61
62 if( pscene->vertex_count + vg_list_size(verts) > pscene->max_vertices )
63 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
64
65 if( pscene->indice_count + vg_list_size(indices) > pscene->max_indices )
66 vg_fatal_exit_loop( "Scene index buffer overflow" );
67
68 scene_vert *dst_verts = &pscene->arrvertices[ pscene->vertex_count ];
69 u32 *dst_indices = &pscene->arrindices [ pscene->indice_count ];
70
71 scene_vert *ref = &world->scene_geo->arrvertices[ hit->tri[0] ];
72
73 for( u32 i=0; i<vg_list_size(verts); i++ )
74 {
75 scene_vert *pvert = &dst_verts[ i ],
76 *src = &verts[ i ];
77
78 m4x3_mulv( transform, src->co, pvert->co );
79 scene_vert_pack_norm( pvert, transform[1] );
80
81 v2_copy( ref->uv, pvert->uv );
82 }
83
84 for( u32 i=0; i<vg_list_size(indices); i++ )
85 dst_indices[i] = indices[i] + pscene->vertex_count;
86
87 pscene->vertex_count += vg_list_size(verts);
88 pscene->indice_count += vg_list_size(indices);
89 }
90
91 /* Sprinkle foliage models over the map on terrain material */
92 VG_STATIC void world_apply_procedural_foliage( world_instance *world,
93 struct world_material *mat )
94 {
95 if( vg.quality_profile == k_quality_profile_low )
96 return;
97
98 vg_info( "Applying foliage (%u)\n", mat->info.pstr_name );
99
100 vg_linear_clear( vg_mem.scratch );
101
102 mdl_context *mfoliage =
103 mdl_load_full( vg_mem.scratch, "models/rs_foliage.mdl");
104
105 v3f volume;
106 v3_sub( world->scene_geo->bbx[1], world->scene_geo->bbx[0], volume );
107 volume[1] = 1.0f;
108
109 mdl_node *mblob = mdl_node_from_name( mfoliage, "blob" );
110 mdl_submesh *sm_blob = mdl_node_submesh( mfoliage, mblob, 0 );
111
112 int count = 0;
113
114 float area = volume[0]*volume[2];
115 u32 particles = 0.08f * area;
116
117 vg_info( "Map area: %f. Max particles: %u\n", area, particles );
118
119 for( int i=0;i<particles;i++ )
120 {
121 v3f pos;
122 v3_mul( volume, (v3f){ vg_randf(), 1000.0f, vg_randf() }, pos );
123 pos[1] = 1000.0f;
124 v3_add( pos, world->scene_geo->bbx[0], pos );
125
126 ray_hit hit;
127 hit.dist = INFINITY;
128
129 if( ray_world( world, pos, (v3f){0.0f,-1.0f,0.0f}, &hit ))
130 {
131 struct world_material *m1 = ray_hit_material( world, &hit );
132 if((hit.normal[1] > 0.8f) && (m1 == mat) && (hit.pos[1] > 0.0f+10.0f))
133 {
134 world_add_blob( world, world->scene_no_collide, &hit );
135 count ++;
136 }
137 }
138 }
139
140 vg_info( "%d foliage models added\n", count );
141 }
142
143 VG_STATIC void world_ents_allocate( world_instance *world )
144 {
145 vg_info( "Allocating entities\n" );
146
147 /* count entites to allocate buffers for them.
148 * maybe in the future we just store these numbers in the model file...
149 *
150 * TODO: use this in world_routes too */
151
152 struct countable
153 {
154 enum classtype ct;
155 void **to_allocate;
156 u32 item_size;
157 int count;
158 }
159 entity_counts[] =
160 {
161 {
162 k_classtype_spawn,
163 (void*)&world->spawns,
164 sizeof(struct respawn_point)
165 },
166 {
167 k_classtype_audio,
168 (void*)&world->audio_things,
169 sizeof(struct world_audio_thing)
170 },
171 {
172 k_classtype_trigger,
173 (void*)&world->triggers,
174 sizeof(struct trigger_zone)
175 },
176 {
177 k_classtype_logic_relay,
178 (void*)&world->logic_relays,
179 sizeof(struct logic_relay)
180 },
181 {
182 k_classtype_logic_achievement,
183 (void*)&world->logic_achievements,
184 sizeof(struct logic_achievement)
185 },
186 {
187 k_classtype_world_light,
188 (void*)&world->lights,
189 sizeof(struct world_light)
190 },
191 {
192 k_classtype_nonlocal_gate,
193 (void*)&world->nonlocal_gates,
194 sizeof(struct nonlocal_gate)
195 }
196 };
197
198 for( int i=0; i<vg_list_size(entity_counts); i++ )
199 entity_counts[i].count = 0;
200
201 for( int i=0; i<world->meta->info.node_count; i++ )
202 {
203 mdl_node *pnode = mdl_node_from_id( world->meta, i );
204
205 for( int j=0; j<vg_list_size(entity_counts); j ++ )
206 {
207 if( pnode->classtype == entity_counts[j].ct )
208 {
209 pnode->sub_uid = entity_counts[j].count;
210 entity_counts[j].count ++;
211 break;
212 }
213 }
214 }
215
216 for( int i=0; i<vg_list_size(entity_counts); i++ )
217 {
218 struct countable *counter = &entity_counts[i];
219
220 u32 bufsize = counter->item_size*counter->count;
221 *counter->to_allocate = vg_linear_alloc( world_global.generic_heap,
222 bufsize );
223 }
224 }
225
226 VG_STATIC void world_pct_spawn( world_instance *world, mdl_node *pnode )
227 {
228 struct respawn_point *rp = &world->spawns[ world->spawn_count ++ ];
229
230 v3_copy( pnode->co, rp->co );
231 v4_copy( pnode->q, rp->q );
232 rp->name = mdl_pstr( world->meta, pnode->pstr_name );
233 }
234
235 VG_STATIC void world_pct_water( world_instance *world, mdl_node *pnode )
236 {
237 if( world->water.enabled )
238 {
239 vg_warn( "Multiple water surfaces in level! ('%s')\n",
240 mdl_pstr( world->meta, pnode->pstr_name ));
241 return;
242 }
243
244 world->water.enabled = 1;
245 water_set_surface( world, pnode->co[1] );
246 }
247
248 VG_STATIC void world_pct_audio( world_instance *world, mdl_node *pnode )
249 {
250 struct world_audio_thing *thing = &world->audio_things[
251 world->audio_things_count ];
252
253 memset( thing, 0, sizeof(struct world_audio_thing) );
254 struct classtype_audio *aud = mdl_get_entdata( world->meta, pnode );
255
256 v3_copy( pnode->co, thing->pos );
257
258 if( aud->flags & AUDIO_FLAG_SPACIAL_3D )
259 thing->volume = aud->volume * pnode->s[0];
260 else
261 thing->volume = aud->volume;
262
263 thing->flags = aud->flags;
264 thing->temp_embedded_clip.path = mdl_pstr( world->meta, aud->pstr_file );
265 thing->temp_embedded_clip.source_mode = k_audio_source_mono;
266
267 audio_clip_load( &thing->temp_embedded_clip, world_global.audio_heap );
268 thing->player.name = mdl_pstr( world->meta, pnode->pstr_name );
269 thing->player.enqued = 0;
270
271 pnode->sub_uid = world->audio_things_count;
272 world->audio_things_count ++;
273 }
274
275 VG_STATIC void world_pct_trigger( world_instance *world, mdl_node *pnode )
276 {
277 struct trigger_zone *trigger = &world->triggers[ world->trigger_count ];
278 struct classtype_trigger *inf = mdl_get_entdata( world->meta, pnode );
279
280 if( inf->target )
281 {
282 mdl_node *target_node = mdl_node_from_id( world->meta, inf->target );
283
284 trigger->target.sub_id = target_node->sub_uid;
285 trigger->target.classtype = target_node->classtype;
286 }
287 else
288 {
289 vg_warn( "Trigger with no target...\n" );
290 return;
291 }
292
293 mdl_node_transform( pnode, trigger->transform );
294 m4x3_invert_full( trigger->transform, trigger->inv_transform );
295
296 world->trigger_count ++;
297 }
298
299
300 VG_STATIC void world_pct_relay( world_instance *world, mdl_node *pnode )
301 {
302 struct logic_relay *relay = &world->logic_relays[ world->relay_count ];
303 struct classtype_logic_relay *inf = mdl_get_entdata( world->meta, pnode );
304
305 relay->target_count = 0;
306
307 for( int i=0; i<vg_list_size(relay->targets); i++ )
308 {
309 if( inf->targets[i] )
310 {
311 struct relay_target *target = &relay->targets[relay->target_count ++];
312 mdl_node *other = mdl_node_from_id( world->meta, inf->targets[i] );
313
314 target->classtype = other->classtype;
315 target->sub_id = other->sub_uid;
316 }
317 }
318
319 v3_copy( pnode->co, relay->pos );
320 world->relay_count ++;
321 }
322
323
324 VG_STATIC void world_pct_achievement( world_instance *world, mdl_node *pnode )
325 {
326 struct logic_achievement *ach =
327 &world->logic_achievements[ world->achievement_count ];
328 struct classtype_logic_achievement *inf =
329 mdl_get_entdata( world->meta, pnode );
330
331 v3_copy( pnode->co, ach->pos );
332 ach->achievement_id = mdl_pstr( world->meta, inf->pstr_name );
333 ach->achieved = 0;
334
335 world->achievement_count ++;
336 }
337
338 VG_STATIC void world_pct_world_light( world_instance *world, mdl_node *pnode )
339 {
340 struct world_light *light = &world->lights[ world->light_count ++ ];
341 light->node = pnode;
342 light->inf = mdl_get_entdata( world->meta, pnode );
343
344 q_m3x3( pnode->q, light->inverse_world );
345 v3_copy( pnode->co, light->inverse_world[3] );
346 m4x3_invert_affine( light->inverse_world, light->inverse_world );
347
348 light->angle_sin_cos[0] = sinf( light->inf->angle * 0.5f );
349 light->angle_sin_cos[1] = cosf( light->inf->angle * 0.5f );
350 }
351
352 VG_STATIC void world_pct_nonlocal_gate( world_instance *world, mdl_node *pnode )
353 {
354 struct nonlocal_gate *gate = &world->nonlocal_gates[
355 world->nonlocalgate_count ++ ];
356 struct classtype_gate *inf = mdl_get_entdata( world->meta, pnode );
357
358 gate->working = 0;
359 gate->node = pnode;
360 gate->target_map_index = 0;
361 v2_copy( inf->dims, gate->gate.dims );
362 }
363
364 VG_STATIC void world_entities_process( world_instance *world )
365 {
366 struct entity_instruction
367 {
368 enum classtype ct;
369 void (*process)( world_instance *world, mdl_node *pnode );
370 }
371 entity_instructions[] =
372 {
373 { k_classtype_spawn, world_pct_spawn },
374 { k_classtype_water, world_pct_water },
375 { k_classtype_audio, world_pct_audio },
376 { k_classtype_trigger, world_pct_trigger },
377 { k_classtype_logic_relay, world_pct_relay },
378 { k_classtype_logic_achievement, world_pct_achievement },
379 { k_classtype_world_light, world_pct_world_light },
380 { k_classtype_nonlocal_gate, world_pct_nonlocal_gate }
381 };
382
383 for( int i=0; i<world->meta->info.node_count; i++ )
384 {
385 mdl_node *pnode = mdl_node_from_id( world->meta, i );
386
387 for( int j=0; j<vg_list_size(entity_instructions); j++ )
388 {
389 struct entity_instruction *instr = &entity_instructions[j];
390
391 if( pnode->classtype == instr->ct )
392 {
393 instr->process( world, pnode );
394 break;
395 }
396 }
397 }
398 }
399
400 VG_STATIC void world_link_nonlocal_gates( int index_a, int index_b )
401 {
402 vg_info( "Linking non-local gates\n" );
403 world_instance *a = &world_global.worlds[ index_a ],
404 *b = &world_global.worlds[ index_b ];
405
406 for( int i=0; i<a->nonlocalgate_count; i++ )
407 {
408 struct nonlocal_gate *ga = &a->nonlocal_gates[i];
409 struct classtype_gate *ga_inf = mdl_get_entdata( a->meta, ga->node );
410 const char *ga_name = mdl_pstr( a->meta, ga_inf->target );
411
412 for( int j=0; j<b->nonlocalgate_count; j++ )
413 {
414 struct nonlocal_gate *gb = &b->nonlocal_gates[j];
415 struct classtype_gate *gb_inf = mdl_get_entdata( b->meta, gb->node );
416 const char *gb_name = mdl_pstr( b->meta, gb_inf->target );
417
418 if( !strcmp( ga_name, gb_name ) )
419 {
420 vg_success( "Created longjump for ID '%s'\n", ga_name );
421
422 v4f qYflip;
423 q_axis_angle( qYflip, (v3f){0.0f,1.0f,0.0f}, VG_PIf );
424
425 /* TODO: Gates are created very wonkily. refactor. */
426 ga->target_map_index = index_b;
427 gb->target_map_index = index_a;
428 ga->working = 1;
429 gb->working = 1;
430
431 v4_copy( ga->node->q, ga->gate.q[0] );
432 v4_copy( gb->node->q, ga->gate.q[1] );
433 v3_copy( ga->node->co, ga->gate.co[0] );
434 v3_copy( gb->node->co, ga->gate.co[1] );
435
436 v4_copy( gb->node->q, gb->gate.q[0] );
437 v4_copy( ga->node->q, gb->gate.q[1] );
438 v3_copy( gb->node->co, gb->gate.co[0] );
439 v3_copy( ga->node->co, gb->gate.co[1] );
440
441 /* reverse B's direction */
442 q_mul( gb->gate.q[0], qYflip, gb->gate.q[0] );
443 q_mul( gb->gate.q[1], qYflip, gb->gate.q[1] );
444 q_normalize( gb->gate.q[0] );
445 q_normalize( gb->gate.q[1] );
446
447 gate_transform_update( &ga->gate );
448 gate_transform_update( &gb->gate );
449 }
450 }
451 }
452 }
453
454 VG_STATIC float colour_luminance( v3f v )
455 {
456 return v3_dot( v, (v3f){0.2126f, 0.7152f, 0.0722f} );
457 }
458
459 VG_STATIC float calc_light_influence( world_instance *world, v3f position,
460 int light )
461 {
462 struct world_light *world_light = &world->lights[ light ];
463 struct classtype_world_light *inf = world_light->inf;
464
465 v3f light_delta;
466 v3_sub( world_light->node->co, position, light_delta );
467 v3_muls( light_delta, 10.0f, light_delta );
468
469 float quadratic = v3_dot( light_delta, light_delta ),
470 attenuation = 1.0f/( 1.0f + quadratic );
471
472 float quadratic_light = attenuation * colour_luminance( inf->colour );
473
474 if( (inf->type == k_light_type_point) ||
475 (inf->type == k_light_type_point_nighttime_only) )
476 {
477 return quadratic_light;
478 }
479 else if( (inf->type == k_light_type_spot) ||
480 (inf->type == k_light_type_spot_nighttime_only) )
481 {
482 v3f dir;
483 q_mulv( world_light->node->q, (v3f){0.0f,1.0f,0.0f}, dir );
484
485 float spot_theta = vg_maxf( 0.0f, v3_dot( light_delta, dir ) ),
486 falloff = spot_theta >= 0.0f? 1.0f: 0.0f;
487
488 return quadratic_light * falloff;
489 }
490 else
491 return 0.0f;
492 }
493
494 VG_STATIC void world_scene_compute_light_clusters( world_instance *world,
495 scene *sc )
496 {
497 for( int i=0; i<sc->vertex_count; i++ )
498 {
499 scene_vert *vert = &sc->arrvertices[i];
500 vert->lights[0] = 0;
501 vert->lights[1] = 1;
502 vert->lights[2] = 2;
503 vert->lights[3] = 0;
504
505 float influences[3] = { 0.0f, 0.0f, 0.0f };
506 const int N = vg_list_size( influences );
507
508 for( int j=0; j<world->light_count; j ++ )
509 {
510 float influence = calc_light_influence( world, vert->co, j );
511
512 int best_pos = N;
513 for( int k=best_pos-1; k>=0; k -- )
514 if( influence > influences[k] )
515 best_pos = k;
516
517 if( best_pos < N )
518 {
519 for( int k=N-1; k>best_pos; k -- )
520 {
521 influences[k] = influences[k-1];
522 vert->lights[k] = vert->lights[k-1];
523 }
524
525 influences[best_pos] = influence;
526 vert->lights[best_pos] = j;
527 }
528 }
529
530 for( int j=0; j<N; j++ )
531 {
532 if( influences[j] > 0.00000125f )
533 vert->lights[3] ++ ;
534 }
535 }
536 }
537
538 VG_STATIC void world_generate( world_instance *world )
539 {
540 /*
541 * Compile meshes into the world scenes
542 */
543 world->scene_geo = scene_init( world_global.generic_heap, 320000, 1200000 );
544
545 m4x3f midentity;
546 m4x3_identity( midentity );
547
548 /*
549 * Generate scene: collidable geometry
550 * ----------------------------------------------------------------
551 */
552
553 vg_info( "Generating collidable geometry\n" );
554
555
556 for( int i=0; i<world->material_count; i++ )
557 {
558 struct world_material *mat = &world->materials[ i ];
559
560 if( mat->info.flags & k_material_flag_collision )
561 world_add_all_if_material( midentity, world->scene_geo, world->meta, i );
562
563 scene_copy_slice( world->scene_geo, &mat->sm_geo );
564 }
565 world_scene_compute_light_clusters( world, world->scene_geo );
566
567 /* compress that bad boy */
568 world->scene_geo = scene_fix( world_global.generic_heap, world->scene_geo );
569
570 vg_acquire_thread_sync();
571 {
572 scene_upload( world->scene_geo, &world->mesh_geo );
573 }
574 vg_release_thread_sync();
575
576 /* setup spacial mapping and rigidbody */
577 world->geo_bh = scene_bh_create( world_global.generic_heap,
578 world->scene_geo );
579
580 v3_zero( world->rb_geo.co );
581 q_identity( world->rb_geo.q );
582
583 world->rb_geo.type = k_rb_shape_scene;
584 world->rb_geo.inf.scene.bh_scene = world->geo_bh;
585 world->rb_geo.is_world = 1;
586 rb_init( &world->rb_geo );
587
588 /*
589 * Generate scene: non-collidable geometry
590 * ----------------------------------------------------------------
591 */
592 vg_info( "Generating non-collidable geometry\n" );
593
594 world->scene_no_collide = scene_init( world_global.generic_heap,
595 200000, 500000 );
596
597 for( int i=0; i<world->material_count; i++ )
598 {
599 struct world_material *mat = &world->materials[ i ];
600
601 if( !(mat->info.flags & k_material_flag_collision) )
602 {
603 world_add_all_if_material( midentity, world->scene_no_collide,
604 world->meta, i );
605 }
606
607 if( mat->info.flags & k_material_flag_grow_grass )
608 world_apply_procedural_foliage( world, mat );
609
610 scene_copy_slice( world->scene_no_collide, &mat->sm_no_collide );
611 }
612 world_scene_compute_light_clusters( world, world->scene_no_collide );
613
614 /* upload and free that */
615 vg_acquire_thread_sync();
616 {
617 scene_upload( world->scene_no_collide, &world->mesh_no_collide );
618 }
619 vg_release_thread_sync();
620
621 vg_linear_del( world_global.generic_heap, world->scene_no_collide );
622 world->scene_no_collide = NULL;
623 }
624
625 float fsd_cone_infinite( v3f p, v2f c )
626 {
627 v2f q = { v2_length( (v2f){ p[0], p[2] } ), -p[1] };
628 float s = vg_maxf( 0.0f, v2_dot( q, c ) );
629
630 v2f v0;
631 v2_muls( c, s, v0 );
632 v2_sub( q, v0, v0 );
633
634 float d = v2_length( v0 );
635 return d * ((q[0]*c[1]-q[1]*c[0]<0.0f)?-1.0f:1.0f);
636 }
637
638 VG_STATIC int reset_player( int argc, char const *argv[] );
639 VG_STATIC void world_post_process( world_instance *world )
640 {
641 /* initialize audio if need be */
642 audio_lock();
643 for( int i=0; i<world->audio_things_count; i++ )
644 {
645 struct world_audio_thing *thingy = &world->audio_things[ i ];
646
647 audio_player_init( &thingy->player );
648 audio_player_set_flags( &thingy->player, thingy->flags );
649 audio_player_set_vol( &thingy->player, thingy->volume );
650 audio_player_set_pan( &thingy->player, 0.0f );
651
652 if( thingy->flags & AUDIO_FLAG_SPACIAL_3D )
653 audio_player_set_position( &thingy->player, thingy->pos );
654
655 if( thingy->flags & AUDIO_FLAG_AUTO_START )
656 audio_player_playclip( &thingy->player, &thingy->temp_embedded_clip );
657 }
658 audio_unlock();
659
660 vg_acquire_thread_sync();
661 {
662 /* create scene lighting buffer */
663
664 u32 size = VG_MAX(world->light_count,1) * sizeof(float)*12;
665
666 vg_info( "Upload %ubytes (lighting)\n", size );
667
668 glGenBuffers( 1, &world->tbo_light_entities );
669 glBindBuffer( GL_TEXTURE_BUFFER, world->tbo_light_entities );
670 glBufferData( GL_TEXTURE_BUFFER, size, NULL, GL_DYNAMIC_DRAW );
671
672 /* buffer layout
673 *
674 * colour position direction (spots)
675 * | . . . . | . . . . | . . . . |
676 * | Re Ge Be Night | Xco Yco Zco Range | Dx Dy Dz Da |
677 *
678 */
679
680 v4f *light_dst = glMapBuffer( GL_TEXTURE_BUFFER, GL_WRITE_ONLY );
681
682 for( int i=0; i<world->light_count; i++ )
683 {
684 struct world_light *light = &world->lights[i];
685 struct classtype_world_light *inf = light->inf;
686
687 /* colour + night */
688 v3_muls( inf->colour, inf->colour[3] * 2.0f, light_dst[i*3+0] );
689 light_dst[i*3+0][3] = -1.0f;
690
691 if( (inf->type == k_light_type_spot_nighttime_only) ||
692 (inf->type == k_light_type_point_nighttime_only ) )
693 {
694 u32 hash = (i * 29986577) & 0xff;
695 float switch_on = hash;
696 switch_on *= (1.0f/255.0f);
697
698 light_dst[i*3+0][3] = 0.44f + switch_on * 0.015f;
699 }
700
701 /* position + 1/range^2 */
702 v3_copy( light->node->co, light_dst[i*3+1] );
703 light_dst[i*3+1][3] = 1.0f/(inf->range*inf->range);
704
705 /* direction + angle */
706 q_mulv( light->node->q, (v3f){0.0f,-1.0f,0.0f}, light_dst[i*3+2]);
707 light_dst[i*3+2][3] = cosf( inf->angle );
708 }
709
710 glUnmapBuffer( GL_TEXTURE_BUFFER );
711
712 glGenTextures( 1, &world->tex_light_entities );
713 glBindTexture( GL_TEXTURE_BUFFER, world->tex_light_entities );
714 glTexBuffer( GL_TEXTURE_BUFFER, GL_RGBA32F, world->tbo_light_entities );
715
716
717 /* Upload lighting uniform buffer */
718 if( world->water.enabled )
719 v4_copy( world->water.plane, world->ub_lighting.g_water_plane );
720
721 v4f info_vec;
722 v3f *bounds = world->scene_geo->bbx;
723
724 info_vec[0] = bounds[0][0];
725 info_vec[1] = bounds[0][2];
726 info_vec[2] = 1.0f/ (bounds[1][0]-bounds[0][0]);
727 info_vec[3] = 1.0f/ (bounds[1][2]-bounds[0][2]);
728 v4_copy( info_vec, world->ub_lighting.g_depth_bounds );
729
730
731 /*
732 * Rendering the depth map
733 */
734 camera ortho;
735
736 v3f extent;
737 v3_sub( world->scene_geo->bbx[1], world->scene_geo->bbx[0], extent );
738
739 float fl = world->scene_geo->bbx[0][0],
740 fr = world->scene_geo->bbx[1][0],
741 fb = world->scene_geo->bbx[0][2],
742 ft = world->scene_geo->bbx[1][2],
743 rl = 1.0f / (fr-fl),
744 tb = 1.0f / (ft-fb);
745
746 m4x4_zero( ortho.mtx.p );
747 ortho.mtx.p[0][0] = 2.0f * rl;
748 ortho.mtx.p[2][1] = 2.0f * tb;
749 ortho.mtx.p[3][0] = (fr + fl) * -rl;
750 ortho.mtx.p[3][1] = (ft + fb) * -tb;
751 ortho.mtx.p[3][3] = 1.0f;
752 m4x3_identity( ortho.transform );
753 camera_update_view( &ortho );
754 camera_finalize( &ortho );
755
756 glDisable(GL_DEPTH_TEST);
757 glDisable(GL_BLEND);
758 glDisable(GL_CULL_FACE);
759 render_fb_bind( &world->heightmap );
760 shader_blitcolour_use();
761 shader_blitcolour_uColour( (v4f){-9999.0f,-9999.0f,-9999.0f,-9999.0f} );
762 render_fsquad();
763
764 /* todo: hmm?? */
765 glEnable(GL_BLEND);
766 glBlendFunc(GL_ONE, GL_ONE);
767 glBlendEquation(GL_MAX);
768
769 render_world_position( world, &ortho );
770 glDisable(GL_BLEND);
771 glEnable(GL_DEPTH_TEST);
772 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
773
774
775
776
777 /* light cubes */
778
779 v3f cubes_min, cubes_max;
780 v3_muls( world->scene_geo->bbx[0], 1.0f/k_light_cube_size, cubes_min );
781 v3_muls( world->scene_geo->bbx[1], 1.0f/k_light_cube_size, cubes_max );
782
783 v3_sub( cubes_min, (v3f){ 0.5f, 0.5f, 0.5f }, cubes_min );
784 v3_add( cubes_max, (v3f){ 0.5f, 0.5f, 0.5f }, cubes_max );
785
786 v3_floor( cubes_min, cubes_min );
787 v3_floor( cubes_max, cubes_max );
788
789 v3i icubes_min, icubes_max;
790
791 for( int i=0; i<3; i++ )
792 {
793 icubes_min[i] = cubes_min[i];
794 icubes_max[i] = cubes_max[i];
795 }
796
797 v3i icubes_count;
798 v3i_sub( icubes_max, icubes_min, icubes_count );
799
800 for( int i=0; i<3; i++ )
801 {
802 icubes_count[i] = VG_MIN( 128, icubes_count[i]+1 );
803 cubes_max[i] = icubes_min[i] + icubes_count[i];
804 }
805
806 v3_muls( cubes_min, k_light_cube_size, cubes_min );
807 v3_muls( cubes_max, k_light_cube_size, cubes_max );
808
809 for( int i=0; i<3; i++ )
810 {
811 float range = cubes_max[i]-cubes_min[i];
812 world->ub_lighting.g_cube_inv_range[i] = 1.0f / range;
813 world->ub_lighting.g_cube_inv_range[i] *= (float)icubes_count[i];
814
815 vg_info( "cubes[%d]: %d\n", i, icubes_count[i] );
816 }
817
818 int total_cubes = icubes_count[0]*icubes_count[1]*icubes_count[2];
819
820 u32 *cubes_index = vg_linear_alloc( world_global.generic_heap,
821 total_cubes * sizeof(u32) * 2.0f );
822
823 vg_info( "Computing light cubes (%d) [%f %f %f] -> [%f %f %f]\n",
824 total_cubes, cubes_min[0], -cubes_min[2], cubes_min[1],
825 cubes_max[0], -cubes_max[2], cubes_max[1] );
826
827 v3_copy( cubes_min, world->ub_lighting.g_cube_min );
828
829 vg_info( "g_cube_min[%f %f %f]\n",
830 world->ub_lighting.g_cube_min[0],
831 world->ub_lighting.g_cube_min[1],
832 world->ub_lighting.g_cube_min[2] );
833 vg_info( "g_cube_inv_range[%f %f %f]\n",
834 world->ub_lighting.g_cube_inv_range[0],
835 world->ub_lighting.g_cube_inv_range[1],
836 world->ub_lighting.g_cube_inv_range[2] );
837
838 v3f cube_size;
839 v3_div( (v3f){1.0f,1.0f,1.0f}, world->ub_lighting.g_cube_inv_range,
840 cube_size );
841 float bound_radius = v3_length( cube_size );
842
843 for( int iz = 0; iz<icubes_count[2]; iz ++ )
844 {
845 for( int iy = 0; iy<icubes_count[1]; iy++ )
846 {
847 for( int ix = 0; ix<icubes_count[0]; ix++ )
848 {
849 boxf bbx;
850 v3_div( (v3f){ ix, iy, iz }, world->ub_lighting.g_cube_inv_range,
851 bbx[0] );
852 v3_div( (v3f){ ix+1, iy+1, iz+1 },
853 world->ub_lighting.g_cube_inv_range,
854 bbx[1] );
855
856 v3_add( bbx[0], world->ub_lighting.g_cube_min, bbx[0] );
857 v3_add( bbx[1], world->ub_lighting.g_cube_min, bbx[1] );
858
859 v3f center;
860 v3_add( bbx[0], bbx[1], center );
861 v3_muls( center, 0.5f, center );
862
863 u32 indices[6] = { 0, 0, 0, 0, 0, 0 };
864 u32 count = 0;
865
866 float influences[6] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
867 const int N = vg_list_size( influences );
868
869 for( int j=0; j<world->light_count; j ++ )
870 {
871 struct world_light *light = &world->lights[j];
872 v3f closest;
873 closest_point_aabb( light->node->co, bbx, closest );
874
875 float dist = v3_dist( closest, light->node->co ),
876 influence = 1.0f/(dist+1.0f);
877
878 if( dist > light->inf->range )
879 continue;
880
881 if( (light->inf->type == k_light_type_spot) ||
882 (light->inf->type == k_light_type_spot_nighttime_only) )
883 {
884 v3f local;
885 m4x3_mulv( light->inverse_world, center, local );
886
887 float r = fsd_cone_infinite( local, light->angle_sin_cos );
888
889 if( r > bound_radius )
890 continue;
891 }
892
893 int best_pos = N;
894 for( int k=best_pos-1; k>=0; k -- )
895 if( influence > influences[k] )
896 best_pos = k;
897
898 if( best_pos < N )
899 {
900 for( int k=N-1; k>best_pos; k -- )
901 {
902 influences[k] = influences[k-1];
903 indices[k] = indices[k-1];
904 }
905
906 influences[best_pos] = influence;
907 indices[best_pos] = j;
908 }
909 }
910
911 for( int j=0; j<N; j++ )
912 if( influences[j] > 0.0f )
913 count ++;
914
915 int base_index = iz * (icubes_count[0]*icubes_count[1]) +
916 iy * (icubes_count[0]) +
917 ix;
918
919 int lower_count = VG_MIN( 3, count );
920 u32 packed_index_lower = lower_count;
921 packed_index_lower |= indices[0]<<2;
922 packed_index_lower |= indices[1]<<12;
923 packed_index_lower |= indices[2]<<22;
924
925 int upper_count = VG_MAX( 0, count - lower_count );
926 u32 packed_index_upper = upper_count;
927 packed_index_upper |= indices[3]<<2;
928 packed_index_upper |= indices[4]<<12;
929 packed_index_upper |= indices[5]<<22;
930
931 cubes_index[ base_index * 2 + 0 ] = packed_index_lower;
932 cubes_index[ base_index * 2 + 1 ] = packed_index_upper;
933 }
934 }
935 }
936
937 glGenTextures( 1, &world->tex_light_cubes ); /* FIXME: glDeleteTextures */
938 glBindTexture( GL_TEXTURE_3D, world->tex_light_cubes );
939 glTexImage3D( GL_TEXTURE_3D, 0, GL_RG32UI,
940 icubes_count[0], icubes_count[1], icubes_count[2],
941 0, GL_RG_INTEGER, GL_UNSIGNED_INT, cubes_index );
942 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
943 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
944
945 vg_linear_del( world_global.generic_heap, cubes_index );
946
947
948
949
950
951 /* upload full buffer */
952 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
953 glBufferSubData( GL_UNIFORM_BUFFER, 0,
954 sizeof(struct ub_world_lighting), &world->ub_lighting );
955 }
956
957 vg_release_thread_sync();
958
959 #if 0
960 /*
961 * Setup scene collider
962 */
963 reset_player( 1, (const char *[]){"start"} );
964 #endif
965 }
966
967 VG_STATIC void world_process_resources( world_instance *world )
968 {
969 vg_info( "Loading textures\n" );
970 world->texture_count = world->meta->info.texture_count;
971 world->textures = vg_linear_alloc( world_global.generic_heap,
972 sizeof(GLuint)*world->texture_count );
973
974 vg_acquire_thread_sync();
975 {
976 /* error texture */
977 world->textures[0] = vg_tex2d_new();
978 vg_tex2d_set_error();
979 vg_tex2d_nearest();
980 vg_tex2d_repeat();
981
982 for( int i=1; i<world->texture_count; i++ )
983 {
984 mdl_texture *tex = &world->meta->texture_buffer[i];
985
986 if( !tex->pack_offset )
987 {
988 vg_release_thread_sync();
989 vg_fatal_exit_loop( "World models must have packed textures!" );
990 }
991
992 vg_linear_clear( vg_mem.scratch );
993 world->textures[i] = vg_tex2d_new();
994 vg_tex2d_set_error();
995 vg_tex2d_qoi( world->meta->pack + tex->pack_offset, tex->pack_length,
996 mdl_pstr( world->meta, tex->pstr_name ));
997 vg_tex2d_nearest();
998 vg_tex2d_repeat();
999 }
1000 }
1001 vg_release_thread_sync();
1002
1003 vg_info( "Loading materials\n" );
1004
1005 u32 size = sizeof(struct world_material) * world->meta->info.material_count;
1006 world->materials = vg_linear_alloc( world_global.generic_heap, size );
1007
1008 world->material_count = world->meta->info.material_count;
1009 memset( world->materials, 0, size );
1010
1011 for( int i=1; i<world->material_count; i++ )
1012 world->materials[i].info = world->meta->material_buffer[i];
1013
1014 /* error material */
1015 struct world_material *errmat = &world->materials[0];
1016 v4_copy( (v4f){ 1.0f,0.0f,0.0f,1.0f }, errmat->info.colour );
1017 v4_copy( (v4f){ 1.0f,0.0f,0.0f,1.0f }, errmat->info.colour1 );
1018 errmat->info.flags = 0x00;
1019 errmat->info.pstr_name = 0; /* useless? */
1020 errmat->info.shader = -1;
1021 errmat->info.tex_decal = 0;
1022 errmat->info.tex_diffuse = 0;
1023 errmat->info.tex_normal = 0;
1024 }
1025
1026 VG_STATIC void world_unload( world_instance *world )
1027 {
1028 vg_acquire_thread_sync();
1029
1030 /* free meshes */
1031 mesh_free( &world->mesh_route_lines );
1032 mesh_free( &world->mesh_geo );
1033 mesh_free( &world->mesh_no_collide );
1034
1035 glDeleteBuffers( 1, &world->tbo_light_entities );
1036 glDeleteTextures( 1, &world->tex_light_entities );
1037
1038 /* FIXME: CANT DO THIS HERE */
1039 world_global.time = 0.0;
1040 world_global.rewind_from = 0.0;
1041 world_global.rewind_to = 0.0;
1042 world_global.last_use = 0.0;
1043 world_global.active_gate = 0;
1044 world_global.current_run_version = 2;
1045 world_global.active_route_board = 0;
1046
1047 for( int i=0; i<vg_list_size(world_global.ui_bars); i++ )
1048 {
1049 struct route_ui_bar *uib = &world_global.ui_bars[i];
1050 uib->segment_start = 0;
1051 uib->segment_count = 0;
1052 uib->fade_start = 0;
1053 uib->fade_count = 0;
1054 uib->fade_timer_start = 0.0;
1055 uib->xpos = 0.0f;
1056 }
1057
1058 /* delete textures and meshes */
1059 glDeleteTextures( world->texture_count, world->textures );
1060
1061 /* delete the entire block of memory */
1062 /* FIXME: WE CANT DO THIS SHIT ANYMORE, NEED TO DEALLOC FROM ABOVE */
1063 #if 0
1064 vg_linear_clear( world->dynamic_vgl );
1065 vg_linear_clear( world->audio_vgl );
1066 #endif
1067
1068
1069 vg_release_thread_sync();
1070 }
1071
1072 VG_STATIC void world_add_global_light( world_instance *world,
1073 v2f dir, v3f colour )
1074 {
1075 int id = world->ub_lighting.g_light_count;
1076
1077 v3_copy( colour, world->ub_lighting.g_light_colours[id] );
1078 v3_copy( (v3f){ cosf(dir[1]) * cosf(dir[0]),
1079 sinf(dir[0]),
1080 sinf(dir[1]) * cosf(dir[0]) },
1081 world->ub_lighting.g_light_directions[id] );
1082
1083 world->ub_lighting.g_light_count ++;
1084 }
1085
1086 VG_STATIC void world_clean( world_instance *world )
1087 {
1088 /* clean dangling pointers */
1089 world->meta = NULL;
1090
1091 world->textures = NULL;
1092 world->texture_count = 0;
1093 world->materials = NULL;
1094 world->material_count = 0;
1095
1096 world->scene_geo = NULL;
1097 world->scene_no_collide = NULL;
1098 world->scene_lines = NULL;
1099
1100 world->geo_bh = NULL;
1101 world->trigger_bh = NULL;
1102 world->audio_bh = NULL;
1103
1104 world->spawns = NULL;
1105 world->spawn_count = 0;
1106
1107 world->audio_things = NULL;
1108 world->audio_things_count = 0;
1109
1110 world->triggers = NULL;
1111 world->trigger_count = 0;
1112
1113 world->lights = NULL;
1114 world->light_count = 0;
1115
1116 world->logic_relays = NULL;
1117 world->relay_count = 0;
1118
1119 world->logic_achievements = NULL;
1120 world->achievement_count = 0;
1121
1122 world->nodes = NULL;
1123 world->node_count = 0;
1124
1125 world->routes = NULL;
1126 world->route_count = 0;
1127
1128 world->gates = NULL;
1129 world->gate_count = 0;
1130
1131 world->collectors = NULL;
1132 world->collector_count = 0;
1133
1134 world->nonlocal_gates = NULL;
1135 world->nonlocalgate_count = 0;
1136
1137 world->water.enabled = 0;
1138
1139
1140 /* default lighting conditions
1141 * -------------------------------------------------------------*/
1142 world->ub_lighting.g_light_count = 0;
1143 world->ub_lighting.g_light_preview = 0;
1144 world->ub_lighting.g_shadow_samples = 8;
1145 world->ub_lighting.g_water_fog = 0.04f;
1146
1147 v4_zero( world->ub_lighting.g_water_plane );
1148 v4_zero( world->ub_lighting.g_depth_bounds );
1149 v4_zero( world->ub_lighting.g_ambient_colour );
1150
1151 v3_copy( (v3f){ 0.09f, 0.03f, 0.07f }, world->ub_lighting.g_ambient_colour );
1152
1153 world_add_global_light( world, (v2f){ 0.63f, -0.08f },
1154 (v3f){ 1.36f, 1.35f, 1.01f } );
1155
1156 world_add_global_light( world, (v2f){ -2.60f, -0.13f },
1157 (v3f){ 0.33f, 0.56f, 0.64f } );
1158
1159 world_add_global_light( world, (v2f){ 2.60f, -0.84f },
1160 (v3f){ 0.05f, 0.05f, 0.23f } );
1161
1162 world->ub_lighting.g_light_directions[0][3] = 9.50f;
1163 world->ub_lighting.g_light_colours[0][3] = 0.65f;
1164 }
1165
1166 VG_STATIC void world_load( world_instance *world, const char *path )
1167 {
1168 world_unload( world );
1169 world_clean( world );
1170
1171 world->meta = mdl_load_full( world_global.generic_heap, path );
1172 vg_info( "Loading world: %s\n", path );
1173
1174 /* process resources from pack */
1175 world_process_resources( world );
1176
1177 /* dynamic allocations */
1178 world_ents_allocate( world );
1179 world_routes_allocate( world );
1180
1181 /* meta processing */
1182 world_routes_process( world );
1183 world_entities_process( world );
1184
1185 /* main bulk */
1186 world_generate( world );
1187 world_routes_generate( world );
1188 world_post_process( world );
1189 }
1190
1191 #endif /* WORLD_GEN_H */