bricks
[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, ct1;
155 void **to_allocate;
156 u32 item_size;
157 int count;
158 }
159 entity_counts[] =
160 {
161 {
162 k_classtype_spawn,
163 k_classtype_none,
164 (void*)&world->spawns,
165 sizeof(struct respawn_point)
166 },
167 {
168 k_classtype_audio,
169 k_classtype_none,
170 (void*)&world->audio_things,
171 sizeof(struct world_audio_thing)
172 },
173 {
174 k_classtype_trigger,
175 k_classtype_particle_box,
176 (void*)&world->triggers,
177 sizeof(struct trigger_zone)
178 },
179
180 #if 0
181 {
182 k_classtype_logic_relay,
183 (void*)&world->logic_relays,
184 sizeof(struct logic_relay)
185 },
186 #endif
187
188 {
189 k_classtype_logic_achievement,
190 k_classtype_none,
191 (void*)&world->logic_achievements,
192 sizeof(struct logic_achievement)
193 },
194 {
195 k_classtype_world_light,
196 k_classtype_none,
197 (void*)&world->lights,
198 sizeof(struct world_light)
199 },
200 {
201 k_classtype_nonlocal_gate,
202 k_classtype_none,
203 (void*)&world->nonlocal_gates,
204 sizeof(struct nonlocal_gate)
205 },
206 {
207 k_classtype_soundscape,
208 k_classtype_none,
209 (void*)&world->soundscapes,
210 sizeof(struct soundscape)
211 }
212 };
213
214 for( int i=0; i<vg_list_size(entity_counts); i++ )
215 entity_counts[i].count = 0;
216
217 for( int i=0; i<world->meta->info.node_count; i++ )
218 {
219 mdl_node *pnode = mdl_node_from_id( world->meta, i );
220
221 for( int j=0; j<vg_list_size(entity_counts); j ++ )
222 {
223 if( (pnode->classtype == entity_counts[j].ct) ||
224 (pnode->classtype == entity_counts[j].ct1) )
225 {
226 pnode->sub_uid = entity_counts[j].count;
227 entity_counts[j].count ++;
228 break;
229 }
230 }
231 }
232
233 for( int i=0; i<vg_list_size(entity_counts); i++ )
234 {
235 struct countable *counter = &entity_counts[i];
236
237 u32 bufsize = counter->item_size*counter->count;
238 *counter->to_allocate = vg_linear_alloc( world_global.generic_heap,
239 bufsize );
240 memset( *counter->to_allocate, 0, bufsize );
241 }
242
243 logic_bricks_world_gen_allocate( world );
244
245 world->trigger_bh = bh_create( world_global.generic_heap,
246 &bh_system_triggers,
247 world,
248 world->trigger_count,
249 1 );
250 }
251
252 VG_STATIC void world_pct_spawn( world_instance *world, mdl_node *pnode )
253 {
254 struct respawn_point *rp = &world->spawns[ world->spawn_count ++ ];
255
256 v3_copy( pnode->co, rp->co );
257 v4_copy( pnode->q, rp->q );
258 rp->name = mdl_pstr( world->meta, pnode->pstr_name );
259 }
260
261 VG_STATIC void world_pct_water( world_instance *world, mdl_node *pnode )
262 {
263 if( world->water.enabled )
264 {
265 vg_warn( "Multiple water surfaces in level! ('%s')\n",
266 mdl_pstr( world->meta, pnode->pstr_name ));
267 return;
268 }
269
270 world->water.enabled = 1;
271 water_set_surface( world, pnode->co[1] );
272 }
273
274 VG_STATIC void world_pct_audio( world_instance *world, mdl_node *pnode )
275 {
276 struct world_audio_thing *thing = &world->audio_things[
277 world->audio_things_count ];
278
279 memset( thing, 0, sizeof(struct world_audio_thing) );
280 struct classtype_audio *aud = mdl_get_entdata( world->meta, pnode );
281
282 v3_copy( pnode->co, thing->pos );
283
284 thing->volume = aud->volume;
285 thing->range = pnode->s[0];
286
287 thing->flags = aud->flags;
288 thing->temp_embedded_clip.path = mdl_pstr( world->meta, aud->pstr_file );
289 thing->temp_embedded_clip.flags = aud->flags;
290
291 audio_clip_load( &thing->temp_embedded_clip, world_global.generic_heap );
292
293 pnode->sub_uid = world->audio_things_count;
294 world->audio_things_count ++;
295 }
296
297 #if 0
298 VG_STATIC void world_pct_relay( world_instance *world, mdl_node *pnode )
299 {
300 struct logic_relay *relay = &world->logic_relays[ world->relay_count ];
301 struct classtype_logic_relay *inf = mdl_get_entdata( world->meta, pnode );
302
303 relay->target_count = 0;
304
305 for( int i=0; i<vg_list_size(relay->targets); i++ )
306 {
307 if( inf->targets[i] )
308 {
309 struct relay_target *target = &relay->targets[relay->target_count ++];
310 mdl_node *other = mdl_node_from_id( world->meta, inf->targets[i] );
311
312 target->classtype = other->classtype;
313 target->sub_id = other->sub_uid;
314 }
315 }
316
317 v3_copy( pnode->co, relay->pos );
318 world->relay_count ++;
319 }
320 #endif
321
322
323 VG_STATIC void world_pct_achievement( world_instance *world, mdl_node *pnode )
324 {
325 struct logic_achievement *ach =
326 &world->logic_achievements[ world->achievement_count ];
327 struct classtype_logic_achievement *inf =
328 mdl_get_entdata( world->meta, pnode );
329
330 v3_copy( pnode->co, ach->pos );
331 ach->achievement_id = mdl_pstr( world->meta, inf->pstr_name );
332 ach->achieved = 0;
333
334 world->achievement_count ++;
335 }
336
337 VG_STATIC void world_pct_world_light( world_instance *world, mdl_node *pnode )
338 {
339 struct world_light *light = &world->lights[ world->light_count ++ ];
340 light->node = pnode;
341 light->inf = mdl_get_entdata( world->meta, pnode );
342
343 q_m3x3( pnode->q, light->inverse_world );
344 v3_copy( pnode->co, light->inverse_world[3] );
345 m4x3_invert_affine( light->inverse_world, light->inverse_world );
346
347 light->angle_sin_cos[0] = sinf( light->inf->angle * 0.5f );
348 light->angle_sin_cos[1] = cosf( light->inf->angle * 0.5f );
349 }
350
351 VG_STATIC void world_pct_nonlocal_gate( world_instance *world, mdl_node *pnode )
352 {
353 struct nonlocal_gate *gate = &world->nonlocal_gates[
354 world->nonlocalgate_count ++ ];
355 struct classtype_gate *inf = mdl_get_entdata( world->meta, pnode );
356
357 gate->working = 0;
358 gate->node = pnode;
359 gate->target_map_index = 0;
360 v2_copy( inf->dims, gate->gate.dims );
361 }
362
363 VG_STATIC void world_entities_process( world_instance *world )
364 {
365 struct entity_instruction
366 {
367 enum classtype ct;
368 void (*process)( world_instance *world, mdl_node *pnode );
369 }
370 entity_instructions[] =
371 {
372 { k_classtype_spawn, world_pct_spawn },
373 { k_classtype_water, world_pct_water },
374 { k_classtype_audio, world_pct_audio },
375 #if 0
376 { k_classtype_logic_relay, world_pct_relay },
377 #endif
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_generate( world_instance *world )
495 {
496 /*
497 * Compile meshes into the world scenes
498 */
499 world->scene_geo = scene_init( world_global.generic_heap, 320000, 1200000 );
500
501 m4x3f midentity;
502 m4x3_identity( midentity );
503
504 /*
505 * Generate scene: collidable geometry
506 * ----------------------------------------------------------------
507 */
508
509 vg_info( "Generating collidable geometry\n" );
510
511
512 for( int i=0; i<world->material_count; i++ )
513 {
514 struct world_material *mat = &world->materials[ i ];
515
516 if( mat->info.flags & k_material_flag_collision )
517 world_add_all_if_material( midentity, world->scene_geo, world->meta, i );
518
519 scene_copy_slice( world->scene_geo, &mat->sm_geo );
520 }
521
522 /* compress that bad boy */
523 world->scene_geo = scene_fix( world_global.generic_heap, world->scene_geo );
524
525 vg_acquire_thread_sync();
526 {
527 scene_upload( world->scene_geo, &world->mesh_geo );
528 }
529 vg_release_thread_sync();
530
531 /* setup spacial mapping and rigidbody */
532 world->geo_bh = scene_bh_create( world_global.generic_heap,
533 world->scene_geo );
534
535 v3_zero( world->rb_geo.co );
536 q_identity( world->rb_geo.q );
537
538 world->rb_geo.type = k_rb_shape_scene;
539 world->rb_geo.inf.scene.bh_scene = world->geo_bh;
540 world->rb_geo.is_world = 1;
541 rb_init( &world->rb_geo );
542
543 /*
544 * Generate scene: non-collidable geometry
545 * ----------------------------------------------------------------
546 */
547 vg_info( "Generating non-collidable geometry\n" );
548
549 world->scene_no_collide = scene_init( world_global.generic_heap,
550 200000, 500000 );
551
552 for( int i=0; i<world->material_count; i++ )
553 {
554 struct world_material *mat = &world->materials[ i ];
555
556 if( !(mat->info.flags & k_material_flag_collision) )
557 {
558 world_add_all_if_material( midentity, world->scene_no_collide,
559 world->meta, i );
560 }
561
562 if( mat->info.flags & k_material_flag_grow_grass )
563 world_apply_procedural_foliage( world, mat );
564
565 scene_copy_slice( world->scene_no_collide, &mat->sm_no_collide );
566 }
567
568 /* upload and free that */
569 vg_acquire_thread_sync();
570 {
571 scene_upload( world->scene_no_collide, &world->mesh_no_collide );
572 }
573 vg_release_thread_sync();
574
575 vg_linear_del( world_global.generic_heap, world->scene_no_collide );
576 world->scene_no_collide = NULL;
577 }
578
579 float fsd_cone_infinite( v3f p, v2f c )
580 {
581 v2f q = { v2_length( (v2f){ p[0], p[2] } ), -p[1] };
582 float s = vg_maxf( 0.0f, v2_dot( q, c ) );
583
584 v2f v0;
585 v2_muls( c, s, v0 );
586 v2_sub( q, v0, v0 );
587
588 float d = v2_length( v0 );
589 return d * ((q[0]*c[1]-q[1]*c[0]<0.0f)?-1.0f:1.0f);
590 }
591
592 VG_STATIC void world_compute_light_indices( world_instance *world )
593 {
594 /* light cubes */
595 v3f cubes_min, cubes_max;
596 v3_muls( world->scene_geo->bbx[0], 1.0f/k_light_cube_size, cubes_min );
597 v3_muls( world->scene_geo->bbx[1], 1.0f/k_light_cube_size, cubes_max );
598
599 v3_sub( cubes_min, (v3f){ 0.5f, 0.5f, 0.5f }, cubes_min );
600 v3_add( cubes_max, (v3f){ 0.5f, 0.5f, 0.5f }, cubes_max );
601
602 v3_floor( cubes_min, cubes_min );
603 v3_floor( cubes_max, cubes_max );
604
605 v3i icubes_min, icubes_max;
606
607 for( int i=0; i<3; i++ )
608 {
609 icubes_min[i] = cubes_min[i];
610 icubes_max[i] = cubes_max[i];
611 }
612
613 v3i icubes_count;
614 v3i_sub( icubes_max, icubes_min, icubes_count );
615
616 for( int i=0; i<3; i++ )
617 {
618 icubes_count[i] = VG_MIN( 128, icubes_count[i]+1 );
619 cubes_max[i] = icubes_min[i] + icubes_count[i];
620 }
621
622 v3_muls( cubes_min, k_light_cube_size, cubes_min );
623 v3_muls( cubes_max, k_light_cube_size, cubes_max );
624
625 for( int i=0; i<3; i++ )
626 {
627 float range = cubes_max[i]-cubes_min[i];
628 world->ub_lighting.g_cube_inv_range[i] = 1.0f / range;
629 world->ub_lighting.g_cube_inv_range[i] *= (float)icubes_count[i];
630
631 vg_info( "cubes[%d]: %d\n", i, icubes_count[i] );
632 }
633
634 int total_cubes = icubes_count[0]*icubes_count[1]*icubes_count[2];
635
636 u32 *cubes_index = vg_linear_alloc( world_global.generic_heap,
637 total_cubes * sizeof(u32) * 2.0f );
638
639 vg_info( "Computing light cubes (%d) [%f %f %f] -> [%f %f %f]\n",
640 total_cubes, cubes_min[0], -cubes_min[2], cubes_min[1],
641 cubes_max[0], -cubes_max[2], cubes_max[1] );
642
643 v3_copy( cubes_min, world->ub_lighting.g_cube_min );
644
645 v3f cube_size;
646 v3_div( (v3f){1.0f,1.0f,1.0f}, world->ub_lighting.g_cube_inv_range,
647 cube_size );
648 float bound_radius = v3_length( cube_size );
649
650 for( int iz = 0; iz<icubes_count[2]; iz ++ )
651 {
652 for( int iy = 0; iy<icubes_count[1]; iy++ )
653 {
654 for( int ix = 0; ix<icubes_count[0]; ix++ )
655 {
656 boxf bbx;
657 v3_div( (v3f){ ix, iy, iz }, world->ub_lighting.g_cube_inv_range,
658 bbx[0] );
659 v3_div( (v3f){ ix+1, iy+1, iz+1 },
660 world->ub_lighting.g_cube_inv_range,
661 bbx[1] );
662
663 v3_add( bbx[0], world->ub_lighting.g_cube_min, bbx[0] );
664 v3_add( bbx[1], world->ub_lighting.g_cube_min, bbx[1] );
665
666 v3f center;
667 v3_add( bbx[0], bbx[1], center );
668 v3_muls( center, 0.5f, center );
669
670 u32 indices[6] = { 0, 0, 0, 0, 0, 0 };
671 u32 count = 0;
672
673 float influences[6] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
674 const int N = vg_list_size( influences );
675
676 for( int j=0; j<world->light_count; j ++ )
677 {
678 struct world_light *light = &world->lights[j];
679 v3f closest;
680 closest_point_aabb( light->node->co, bbx, closest );
681
682 float dist = v3_dist( closest, light->node->co ),
683 influence = 1.0f/(dist+1.0f);
684
685 if( dist > light->inf->range )
686 continue;
687
688 if( (light->inf->type == k_light_type_spot) ||
689 (light->inf->type == k_light_type_spot_nighttime_only) )
690 {
691 v3f local;
692 m4x3_mulv( light->inverse_world, center, local );
693
694 float r = fsd_cone_infinite( local, light->angle_sin_cos );
695
696 if( r > bound_radius )
697 continue;
698 }
699
700 int best_pos = N;
701 for( int k=best_pos-1; k>=0; k -- )
702 if( influence > influences[k] )
703 best_pos = k;
704
705 if( best_pos < N )
706 {
707 for( int k=N-1; k>best_pos; k -- )
708 {
709 influences[k] = influences[k-1];
710 indices[k] = indices[k-1];
711 }
712
713 influences[best_pos] = influence;
714 indices[best_pos] = j;
715 }
716 }
717
718 for( int j=0; j<N; j++ )
719 if( influences[j] > 0.0f )
720 count ++;
721
722 int base_index = iz * (icubes_count[0]*icubes_count[1]) +
723 iy * (icubes_count[0]) +
724 ix;
725
726 int lower_count = VG_MIN( 3, count );
727 u32 packed_index_lower = lower_count;
728 packed_index_lower |= indices[0]<<2;
729 packed_index_lower |= indices[1]<<12;
730 packed_index_lower |= indices[2]<<22;
731
732 int upper_count = VG_MAX( 0, count - lower_count );
733 u32 packed_index_upper = upper_count;
734 packed_index_upper |= indices[3]<<2;
735 packed_index_upper |= indices[4]<<12;
736 packed_index_upper |= indices[5]<<22;
737
738 cubes_index[ base_index*2 + 0 ] = packed_index_lower;
739 cubes_index[ base_index*2 + 1 ] = packed_index_upper;
740 }
741 }
742 }
743
744 vg_acquire_thread_sync();
745
746 glGenTextures( 1, &world->tex_light_cubes );
747 glBindTexture( GL_TEXTURE_3D, world->tex_light_cubes );
748 glTexImage3D( GL_TEXTURE_3D, 0, GL_RG32UI,
749 icubes_count[0], icubes_count[1], icubes_count[2],
750 0, GL_RG_INTEGER, GL_UNSIGNED_INT, cubes_index );
751 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
752 glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
753
754 vg_linear_del( world_global.generic_heap, cubes_index );
755
756 vg_release_thread_sync();
757 }
758
759 VG_STATIC int reset_player( int argc, char const *argv[] );
760 VG_STATIC void world_post_process( world_instance *world )
761 {
762 /* initialize audio if need be */
763 audio_lock();
764 for( int i=0; i<world->audio_things_count; i++ )
765 {
766 struct world_audio_thing *thingy = &world->audio_things[ i ];
767
768 if( thingy->flags & AUDIO_FLAG_AUTO_START )
769 {
770 audio_channel *ch =
771 audio_request_channel( &thingy->temp_embedded_clip, thingy->flags );
772
773 audio_channel_edit_volume( ch, thingy->volume, 1 );
774 audio_channel_set_spacial( ch, thingy->pos, thingy->range );
775
776 if( !(ch->flags & AUDIO_FLAG_LOOP) )
777 ch = audio_relinquish_channel( ch );
778 }
779 }
780 audio_unlock();
781
782 world_compute_light_indices( world );
783
784 vg_acquire_thread_sync();
785 {
786 /* create scene lighting buffer */
787
788 u32 size = VG_MAX(world->light_count,1) * sizeof(float)*12;
789
790 vg_info( "Upload %ubytes (lighting)\n", size );
791
792 glGenBuffers( 1, &world->tbo_light_entities );
793 glBindBuffer( GL_TEXTURE_BUFFER, world->tbo_light_entities );
794 glBufferData( GL_TEXTURE_BUFFER, size, NULL, GL_DYNAMIC_DRAW );
795
796 /* buffer layout
797 *
798 * colour position direction (spots)
799 * | . . . . | . . . . | . . . . |
800 * | Re Ge Be Night | Xco Yco Zco Range | Dx Dy Dz Da |
801 *
802 */
803
804 v4f *light_dst = glMapBuffer( GL_TEXTURE_BUFFER, GL_WRITE_ONLY );
805
806 for( int i=0; i<world->light_count; i++ )
807 {
808 struct world_light *light = &world->lights[i];
809 struct classtype_world_light *inf = light->inf;
810
811 /* colour + night */
812 v3_muls( inf->colour, inf->colour[3] * 2.0f, light_dst[i*3+0] );
813 light_dst[i*3+0][3] = -1.0f;
814
815 if( (inf->type == k_light_type_spot_nighttime_only) ||
816 (inf->type == k_light_type_point_nighttime_only ) )
817 {
818 u32 hash = (i * 29986577) & 0xff;
819 float switch_on = hash;
820 switch_on *= (1.0f/255.0f);
821
822 light_dst[i*3+0][3] = 0.44f + switch_on * 0.015f;
823 }
824
825 /* position + 1/range^2 */
826 v3_copy( light->node->co, light_dst[i*3+1] );
827 light_dst[i*3+1][3] = 1.0f/(inf->range*inf->range);
828
829 /* direction + angle */
830 q_mulv( light->node->q, (v3f){0.0f,-1.0f,0.0f}, light_dst[i*3+2]);
831 light_dst[i*3+2][3] = cosf( inf->angle );
832 }
833
834 glUnmapBuffer( GL_TEXTURE_BUFFER );
835
836 glGenTextures( 1, &world->tex_light_entities );
837 glBindTexture( GL_TEXTURE_BUFFER, world->tex_light_entities );
838 glTexBuffer( GL_TEXTURE_BUFFER, GL_RGBA32F, world->tbo_light_entities );
839
840
841 /* Upload lighting uniform buffer */
842 if( world->water.enabled )
843 v4_copy( world->water.plane, world->ub_lighting.g_water_plane );
844
845 v4f info_vec;
846 v3f *bounds = world->scene_geo->bbx;
847
848 info_vec[0] = bounds[0][0];
849 info_vec[1] = bounds[0][2];
850 info_vec[2] = 1.0f/ (bounds[1][0]-bounds[0][0]);
851 info_vec[3] = 1.0f/ (bounds[1][2]-bounds[0][2]);
852 v4_copy( info_vec, world->ub_lighting.g_depth_bounds );
853
854
855 /*
856 * Rendering the depth map
857 */
858 camera ortho;
859
860 v3f extent;
861 v3_sub( world->scene_geo->bbx[1], world->scene_geo->bbx[0], extent );
862
863 float fl = world->scene_geo->bbx[0][0],
864 fr = world->scene_geo->bbx[1][0],
865 fb = world->scene_geo->bbx[0][2],
866 ft = world->scene_geo->bbx[1][2],
867 rl = 1.0f / (fr-fl),
868 tb = 1.0f / (ft-fb);
869
870 m4x4_zero( ortho.mtx.p );
871 ortho.mtx.p[0][0] = 2.0f * rl;
872 ortho.mtx.p[2][1] = 2.0f * tb;
873 ortho.mtx.p[3][0] = (fr + fl) * -rl;
874 ortho.mtx.p[3][1] = (ft + fb) * -tb;
875 ortho.mtx.p[3][3] = 1.0f;
876 m4x3_identity( ortho.transform );
877 camera_update_view( &ortho );
878 camera_finalize( &ortho );
879
880 glDisable(GL_DEPTH_TEST);
881 glDisable(GL_BLEND);
882 glDisable(GL_CULL_FACE);
883 render_fb_bind( &world->heightmap );
884 shader_blitcolour_use();
885 shader_blitcolour_uColour( (v4f){-9999.0f,-9999.0f,-9999.0f,-9999.0f} );
886 render_fsquad();
887
888 glEnable(GL_BLEND);
889 glBlendFunc(GL_ONE, GL_ONE);
890 glBlendEquation(GL_MAX);
891
892 render_world_position( world, &ortho );
893 glDisable(GL_BLEND);
894 glEnable(GL_DEPTH_TEST);
895 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
896
897 /* upload full buffer */
898 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
899 glBufferSubData( GL_UNIFORM_BUFFER, 0,
900 sizeof(struct ub_world_lighting), &world->ub_lighting );
901 }
902
903 vg_release_thread_sync();
904
905 #if 0
906 /*
907 * Setup scene collider
908 */
909 reset_player( 1, (const char *[]){"start"} );
910 #endif
911 }
912
913 VG_STATIC void world_process_resources( world_instance *world )
914 {
915 vg_info( "Loading textures\n" );
916 world->texture_count = world->meta->info.texture_count;
917 world->textures = vg_linear_alloc( world_global.generic_heap,
918 sizeof(GLuint)*world->texture_count );
919
920 vg_acquire_thread_sync();
921 {
922 /* error texture */
923 world->textures[0] = vg_tex2d_new();
924 vg_tex2d_set_error();
925 vg_tex2d_nearest();
926 vg_tex2d_repeat();
927
928 for( int i=1; i<world->texture_count; i++ )
929 {
930 mdl_texture *tex = &world->meta->texture_buffer[i];
931
932 if( !tex->pack_offset )
933 {
934 vg_release_thread_sync();
935 vg_fatal_exit_loop( "World models must have packed textures!" );
936 }
937
938 vg_linear_clear( vg_mem.scratch );
939 world->textures[i] = vg_tex2d_new();
940 vg_tex2d_set_error();
941 vg_tex2d_qoi( world->meta->pack + tex->pack_offset, tex->pack_length,
942 mdl_pstr( world->meta, tex->pstr_name ));
943 vg_tex2d_nearest();
944 vg_tex2d_repeat();
945 }
946 }
947 vg_release_thread_sync();
948
949 vg_info( "Loading materials\n" );
950
951 u32 size = sizeof(struct world_material) * world->meta->info.material_count;
952 world->materials = vg_linear_alloc( world_global.generic_heap, size );
953
954 world->material_count = world->meta->info.material_count;
955 memset( world->materials, 0, size );
956
957 for( int i=1; i<world->material_count; i++ )
958 world->materials[i].info = world->meta->material_buffer[i];
959
960 /* error material */
961 struct world_material *errmat = &world->materials[0];
962 v4_copy( (v4f){ 1.0f,0.0f,0.0f,1.0f }, errmat->info.colour );
963 v4_copy( (v4f){ 1.0f,0.0f,0.0f,1.0f }, errmat->info.colour1 );
964 errmat->info.flags = 0x00;
965 errmat->info.pstr_name = 0; /* useless? */
966 errmat->info.shader = -1;
967 errmat->info.tex_decal = 0;
968 errmat->info.tex_diffuse = 0;
969 errmat->info.tex_normal = 0;
970 }
971
972 VG_STATIC void world_unload( world_instance *world )
973 {
974 vg_acquire_thread_sync();
975
976 /* free meshes */
977 mesh_free( &world->mesh_route_lines );
978 mesh_free( &world->mesh_geo );
979 mesh_free( &world->mesh_no_collide );
980
981 glDeleteBuffers( 1, &world->tbo_light_entities );
982 glDeleteTextures( 1, &world->tex_light_entities );
983 glDeleteTextures( 1, &world->tex_light_cubes );
984
985 /* FIXME: CANT DO THIS HERE */
986 world_global.time = 0.0;
987 world_global.rewind_from = 0.0;
988 world_global.rewind_to = 0.0;
989 world_global.last_use = 0.0;
990 world_global.active_gate = 0;
991 world_global.current_run_version = 2;
992 world_global.active_route_board = 0;
993
994 for( int i=0; i<vg_list_size(world_global.ui_bars); i++ )
995 {
996 struct route_ui_bar *uib = &world_global.ui_bars[i];
997 uib->segment_start = 0;
998 uib->segment_count = 0;
999 uib->fade_start = 0;
1000 uib->fade_count = 0;
1001 uib->fade_timer_start = 0.0;
1002 uib->xpos = 0.0f;
1003 }
1004
1005 /* delete textures and meshes */
1006 glDeleteTextures( world->texture_count, world->textures );
1007
1008 /* delete the entire block of memory */
1009 /* FIXME: WE CANT DO THIS SHIT ANYMORE, NEED TO DEALLOC FROM ABOVE */
1010 #if 0
1011 vg_linear_clear( world->dynamic_vgl );
1012 vg_linear_clear( world->audio_vgl );
1013 #endif
1014
1015
1016 vg_release_thread_sync();
1017 }
1018
1019 VG_STATIC void world_clean( world_instance *world )
1020 {
1021 /* clean dangling pointers */
1022 world->meta = NULL;
1023
1024 /*
1025 * TODO: Theres probably a better way to do this?
1026 */
1027
1028 world->textures = NULL;
1029 world->texture_count = 0;
1030 world->materials = NULL;
1031 world->material_count = 0;
1032
1033 world->scene_geo = NULL;
1034 world->scene_no_collide = NULL;
1035 world->scene_lines = NULL;
1036
1037 world->geo_bh = NULL;
1038 world->trigger_bh = NULL;
1039 world->audio_bh = NULL;
1040
1041 world->spawns = NULL;
1042 world->spawn_count = 0;
1043
1044 world->audio_things = NULL;
1045 world->audio_things_count = 0;
1046
1047 world->triggers = NULL;
1048 world->trigger_count = 0;
1049
1050 world->lights = NULL;
1051 world->light_count = 0;
1052
1053 #if 0
1054 world->logic_relays = NULL;
1055 world->relay_count = 0;
1056 #endif
1057
1058 world->logic_achievements = NULL;
1059 world->achievement_count = 0;
1060
1061 world->nodes = NULL;
1062 world->node_count = 0;
1063
1064 world->routes = NULL;
1065 world->route_count = 0;
1066
1067 world->gates = NULL;
1068 world->gate_count = 0;
1069
1070 world->collectors = NULL;
1071 world->collector_count = 0;
1072
1073 world->soundscapes = NULL;
1074 world->soundscape_count = 0;
1075
1076 world->logic_bricks = NULL;
1077 world->logic_brick_count = 0;
1078
1079 world->nonlocal_gates = NULL;
1080 world->nonlocalgate_count = 0;
1081
1082 world->water.enabled = 0;
1083
1084
1085 /* default lighting conditions
1086 * -------------------------------------------------------------*/
1087 struct ub_world_lighting *state = &world->ub_lighting;
1088
1089 state->g_light_preview = 0;
1090 state->g_shadow_samples = 8;
1091 state->g_water_fog = 0.04f;
1092
1093 v4_zero( state->g_water_plane );
1094 v4_zero( state->g_depth_bounds );
1095
1096 state->g_shadow_length = 9.50f;
1097 state->g_shadow_spread = 0.65f;
1098
1099 v3_copy( (v3f){0.37f, 0.54f, 0.97f}, state->g_daysky_colour );
1100 v3_copy( (v3f){0.03f, 0.05f, 0.20f}, state->g_nightsky_colour );
1101 v3_copy( (v3f){1.00f, 0.32f, 0.01f}, state->g_sunset_colour );
1102 v3_copy( (v3f){0.13f, 0.17f, 0.35f}, state->g_ambient_colour );
1103 v3_copy( (v3f){0.25f, 0.17f, 0.51f}, state->g_sunset_ambient );
1104 v3_copy( (v3f){1.10f, 0.89f, 0.35f}, state->g_sun_colour );
1105 }
1106
1107 VG_STATIC void world_load( world_instance *world, const char *path )
1108 {
1109 world_unload( world );
1110 world_clean( world );
1111
1112 world->meta = mdl_load_full( world_global.generic_heap, path );
1113 vg_info( "Loading world: %s\n", path );
1114
1115 /* process resources from pack */
1116 world_process_resources( world );
1117
1118 /* dynamic allocations */
1119 world_ents_allocate( world );
1120 world_routes_allocate( world );
1121
1122 /* meta processing */
1123 world_routes_process( world );
1124 world_entities_process( world );
1125
1126 /* main bulk */
1127 world_generate( world );
1128 world_routes_generate( world );
1129 world_post_process( world );
1130 }
1131
1132 #endif /* WORLD_GEN_H */