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