checkin
[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
345 VG_STATIC void world_pct_nonlocal_gate( world_instance *world, mdl_node *pnode )
346 {
347 struct nonlocal_gate *gate = &world->nonlocal_gates[
348 world->nonlocalgate_count ++ ];
349 struct classtype_gate *inf = mdl_get_entdata( world->meta, pnode );
350
351 gate->working = 0;
352 gate->node = pnode;
353 gate->target_map_index = 0;
354 v2_copy( inf->dims, gate->gate.dims );
355 }
356
357 VG_STATIC void world_entities_process( world_instance *world )
358 {
359 struct entity_instruction
360 {
361 enum classtype ct;
362 void (*process)( world_instance *world, mdl_node *pnode );
363 }
364 entity_instructions[] =
365 {
366 { k_classtype_spawn, world_pct_spawn },
367 { k_classtype_water, world_pct_water },
368 { k_classtype_audio, world_pct_audio },
369 { k_classtype_trigger, world_pct_trigger },
370 { k_classtype_logic_relay, world_pct_relay },
371 { k_classtype_logic_achievement, world_pct_achievement },
372 { k_classtype_world_light, world_pct_world_light },
373 { k_classtype_nonlocal_gate, world_pct_nonlocal_gate }
374 };
375
376 for( int i=0; i<world->meta->info.node_count; i++ )
377 {
378 mdl_node *pnode = mdl_node_from_id( world->meta, i );
379
380 for( int j=0; j<vg_list_size(entity_instructions); j++ )
381 {
382 struct entity_instruction *instr = &entity_instructions[j];
383
384 if( pnode->classtype == instr->ct )
385 {
386 instr->process( world, pnode );
387 break;
388 }
389 }
390 }
391 }
392
393 VG_STATIC void world_link_nonlocal_gates( int index_a, int index_b )
394 {
395 vg_info( "Linking non-local gates\n" );
396 world_instance *a = &world_global.worlds[ index_a ],
397 *b = &world_global.worlds[ index_b ];
398
399 for( int i=0; i<a->nonlocalgate_count; i++ )
400 {
401 struct nonlocal_gate *ga = &a->nonlocal_gates[i];
402 struct classtype_gate *ga_inf = mdl_get_entdata( a->meta, ga->node );
403 const char *ga_name = mdl_pstr( a->meta, ga_inf->target );
404
405 for( int j=0; j<b->nonlocalgate_count; j++ )
406 {
407 struct nonlocal_gate *gb = &b->nonlocal_gates[j];
408 struct classtype_gate *gb_inf = mdl_get_entdata( b->meta, gb->node );
409 const char *gb_name = mdl_pstr( b->meta, gb_inf->target );
410
411 if( !strcmp( ga_name, gb_name ) )
412 {
413 vg_success( "Created longjump for ID '%s'\n", ga_name );
414
415 v4f qYflip;
416 q_axis_angle( qYflip, (v3f){0.0f,1.0f,0.0f}, VG_PIf );
417
418 /* TODO: Gates are created very wonkily. refactor. */
419 ga->target_map_index = index_b;
420 gb->target_map_index = index_a;
421 ga->working = 1;
422 gb->working = 1;
423
424 v4_copy( ga->node->q, ga->gate.q[0] );
425 v4_copy( gb->node->q, ga->gate.q[1] );
426 v3_copy( ga->node->co, ga->gate.co[0] );
427 v3_copy( gb->node->co, ga->gate.co[1] );
428
429 v4_copy( gb->node->q, gb->gate.q[0] );
430 v4_copy( ga->node->q, gb->gate.q[1] );
431 v3_copy( gb->node->co, gb->gate.co[0] );
432 v3_copy( ga->node->co, gb->gate.co[1] );
433
434 /* reverse B's direction */
435 q_mul( gb->gate.q[0], qYflip, gb->gate.q[0] );
436 q_mul( gb->gate.q[1], qYflip, gb->gate.q[1] );
437 q_normalize( gb->gate.q[0] );
438 q_normalize( gb->gate.q[1] );
439
440 gate_transform_update( &ga->gate );
441 gate_transform_update( &gb->gate );
442 }
443 }
444 }
445 }
446
447 VG_STATIC float colour_luminance( v3f v )
448 {
449 return v3_dot( v, (v3f){0.2126f, 0.7152f, 0.0722f} );
450 }
451
452 VG_STATIC float calc_light_influence( world_instance *world, v3f position,
453 int light )
454 {
455 struct world_light *world_light = &world->lights[ light ];
456 struct classtype_world_light *inf = world_light->inf;
457
458 v3f light_delta;
459 v3_sub( world_light->node->co, position, light_delta );
460 v3_muls( light_delta, 10.0f, light_delta );
461
462 float quadratic = v3_dot( light_delta, light_delta ),
463 attenuation = 1.0f/( 1.0f + quadratic );
464
465 float quadratic_light = attenuation * colour_luminance( inf->colour );
466
467 if( (inf->type == k_light_type_point) ||
468 (inf->type == k_light_type_point_nighttime_only) )
469 {
470 return quadratic_light;
471 }
472 else if( (inf->type == k_light_type_spot) ||
473 (inf->type == k_light_type_spot_nighttime_only) )
474 {
475 v3f dir;
476 q_mulv( world_light->node->q, (v3f){0.0f,1.0f,0.0f}, dir );
477
478 float spot_theta = vg_maxf( 0.0f, v3_dot( light_delta, dir ) ),
479 falloff = spot_theta >= 0.0f? 1.0f: 0.0f;
480
481 return quadratic_light * falloff;
482 }
483 else
484 return 0.0f;
485 }
486
487 VG_STATIC void world_scene_compute_light_clusters( world_instance *world,
488 scene *sc )
489 {
490 for( int i=0; i<sc->vertex_count; i++ )
491 {
492 scene_vert *vert = &sc->arrvertices[i];
493 vert->lights[0] = 0;
494 vert->lights[1] = 1;
495 vert->lights[2] = 2;
496 vert->lights[3] = 0;
497
498 float influences[3] = { 0.0f, 0.0f, 0.0f };
499 const int N = vg_list_size( influences );
500
501 for( int j=0; j<world->light_count; j ++ )
502 {
503 float influence = calc_light_influence( world, vert->co, j );
504
505 int best_pos = N;
506 for( int k=best_pos-1; k>=0; k -- )
507 if( influence > influences[k] )
508 best_pos = k;
509
510 if( best_pos < N )
511 {
512 for( int k=N-1; k>best_pos; k -- )
513 {
514 influences[k] = influences[k-1];
515 vert->lights[k] = vert->lights[k-1];
516 }
517
518 influences[best_pos] = influence;
519 vert->lights[best_pos] = j;
520 }
521 }
522
523 for( int j=0; j<N; j++ )
524 {
525 if( influences[j] > 0.00000125f )
526 vert->lights[3] ++ ;
527 }
528 }
529 }
530
531 VG_STATIC void world_generate( world_instance *world )
532 {
533 /*
534 * Compile meshes into the world scenes
535 */
536 world->scene_geo = scene_init( world_global.generic_heap, 320000, 1200000 );
537
538 m4x3f midentity;
539 m4x3_identity( midentity );
540
541 /*
542 * Generate scene: collidable geometry
543 * ----------------------------------------------------------------
544 */
545
546 vg_info( "Generating collidable geometry\n" );
547
548
549 for( int i=0; i<world->material_count; i++ )
550 {
551 struct world_material *mat = &world->materials[ i ];
552
553 if( mat->info.flags & k_material_flag_collision )
554 world_add_all_if_material( midentity, world->scene_geo, world->meta, i );
555
556 scene_copy_slice( world->scene_geo, &mat->sm_geo );
557 }
558 world_scene_compute_light_clusters( world, world->scene_geo );
559
560 /* compress that bad boy */
561 world->scene_geo = scene_fix( world_global.generic_heap, world->scene_geo );
562
563 vg_acquire_thread_sync();
564 {
565 scene_upload( world->scene_geo, &world->mesh_geo );
566 }
567 vg_release_thread_sync();
568
569 /* setup spacial mapping and rigidbody */
570 world->geo_bh = scene_bh_create( world_global.generic_heap,
571 world->scene_geo );
572
573 v3_zero( world->rb_geo.co );
574 q_identity( world->rb_geo.q );
575
576 world->rb_geo.type = k_rb_shape_scene;
577 world->rb_geo.inf.scene.bh_scene = world->geo_bh;
578 world->rb_geo.is_world = 1;
579 rb_init( &world->rb_geo );
580
581 /*
582 * Generate scene: non-collidable geometry
583 * ----------------------------------------------------------------
584 */
585 vg_info( "Generating non-collidable geometry\n" );
586
587 world->scene_no_collide = scene_init( world_global.generic_heap,
588 200000, 500000 );
589
590 for( int i=0; i<world->material_count; i++ )
591 {
592 struct world_material *mat = &world->materials[ i ];
593
594 if( !(mat->info.flags & k_material_flag_collision) )
595 {
596 world_add_all_if_material( midentity, world->scene_no_collide,
597 world->meta, i );
598 }
599
600 if( mat->info.flags & k_material_flag_grow_grass )
601 world_apply_procedural_foliage( world, mat );
602
603 scene_copy_slice( world->scene_no_collide, &mat->sm_no_collide );
604 }
605 world_scene_compute_light_clusters( world, world->scene_no_collide );
606
607 /* upload and free that */
608 vg_acquire_thread_sync();
609 {
610 scene_upload( world->scene_no_collide, &world->mesh_no_collide );
611 }
612 vg_release_thread_sync();
613
614 vg_linear_del( world_global.generic_heap, world->scene_no_collide );
615 world->scene_no_collide = NULL;
616 }
617
618 VG_STATIC int reset_player( int argc, char const *argv[] );
619 VG_STATIC void world_post_process( world_instance *world )
620 {
621 /* initialize audio if need be */
622 audio_lock();
623 for( int i=0; i<world->audio_things_count; i++ )
624 {
625 struct world_audio_thing *thingy = &world->audio_things[ i ];
626
627 audio_player_init( &thingy->player );
628 audio_player_set_flags( &thingy->player, thingy->flags );
629 audio_player_set_vol( &thingy->player, thingy->volume );
630 audio_player_set_pan( &thingy->player, 0.0f );
631
632 if( thingy->flags & AUDIO_FLAG_SPACIAL_3D )
633 audio_player_set_position( &thingy->player, thingy->pos );
634
635 if( thingy->flags & AUDIO_FLAG_AUTO_START )
636 audio_player_playclip( &thingy->player, &thingy->temp_embedded_clip );
637 }
638 audio_unlock();
639
640 vg_acquire_thread_sync();
641 {
642 /* create scene lighting buffer */
643
644 u32 size = VG_MAX(world->light_count,1) * sizeof(float)*12;
645
646 vg_info( "Upload %ubytes (lighting)\n", size );
647
648 glGenBuffers( 1, &world->tbo_light_entities );
649 glBindBuffer( GL_TEXTURE_BUFFER, world->tbo_light_entities );
650 glBufferData( GL_TEXTURE_BUFFER, size, NULL, GL_DYNAMIC_DRAW );
651
652 /* buffer layout
653 *
654 * colour position direction (spots)
655 * | . . . . | . . . . | . . . . |
656 * | Re Ge Be Night | Xco Yco Zco | Dx Dy Dz Da |
657 *
658 */
659
660 v4f *light_dst = glMapBuffer( GL_TEXTURE_BUFFER, GL_WRITE_ONLY );
661
662 for( int i=0; i<world->light_count; i++ )
663 {
664 struct world_light *light = &world->lights[i];
665 struct classtype_world_light *inf = light->inf;
666
667 /* colour + night */
668 v3_muls( inf->colour, inf->colour[3] * 2.0f, light_dst[i*3+0] );
669 light_dst[i*3+0][3] = 0.0f;
670
671 if( (inf->type == k_light_type_spot_nighttime_only) ||
672 (inf->type == k_light_type_point_nighttime_only ) )
673 {
674 u32 hash = (i * 29986577) & 0xff;
675 float switch_on = hash;
676 switch_on *= (1.0f/255.0f);
677
678 light_dst[i*3+0][3] = 0.44f + switch_on * 0.015f;
679 }
680
681 /* position + nothing */
682 v3_copy( light->node->co, light_dst[i*3+1] );
683
684 /* direction + angle */
685 q_mulv( light->node->q, (v3f){0.0f,-1.0f,0.0f}, light_dst[i*3+2]);
686 light_dst[i*3+2][3] = inf->angle;
687 }
688
689 glUnmapBuffer( GL_TEXTURE_BUFFER );
690
691 glGenTextures( 1, &world->tex_light_entities );
692 glBindTexture( GL_TEXTURE_BUFFER, world->tex_light_entities );
693 glTexBuffer( GL_TEXTURE_BUFFER, GL_RGBA32F, world->tbo_light_entities );
694
695
696 /* Upload lighting uniform buffer */
697 if( world->water.enabled )
698 v4_copy( world->water.plane, world->ub_lighting.g_water_plane );
699
700 v4f info_vec;
701 v3f *bounds = world->scene_geo->bbx;
702
703 info_vec[0] = bounds[0][0];
704 info_vec[1] = bounds[0][2];
705 info_vec[2] = 1.0f/ (bounds[1][0]-bounds[0][0]);
706 info_vec[3] = 1.0f/ (bounds[1][2]-bounds[0][2]);
707 v4_copy( info_vec, world->ub_lighting.g_depth_bounds );
708
709 /* upload full buffer */
710 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
711 glBufferSubData( GL_UNIFORM_BUFFER, 0,
712 sizeof(struct ub_world_lighting), &world->ub_lighting );
713
714
715 /*
716 * Rendering the depth map
717 */
718 camera ortho;
719
720 v3f extent;
721 v3_sub( world->scene_geo->bbx[1], world->scene_geo->bbx[0], extent );
722
723 float fl = world->scene_geo->bbx[0][0],
724 fr = world->scene_geo->bbx[1][0],
725 fb = world->scene_geo->bbx[0][2],
726 ft = world->scene_geo->bbx[1][2],
727 rl = 1.0f / (fr-fl),
728 tb = 1.0f / (ft-fb);
729
730 m4x4_zero( ortho.mtx.p );
731 ortho.mtx.p[0][0] = 2.0f * rl;
732 ortho.mtx.p[2][1] = 2.0f * tb;
733 ortho.mtx.p[3][0] = (fr + fl) * -rl;
734 ortho.mtx.p[3][1] = (ft + fb) * -tb;
735 ortho.mtx.p[3][3] = 1.0f;
736 m4x3_identity( ortho.transform );
737 camera_update_view( &ortho );
738 camera_finalize( &ortho );
739
740 glDisable(GL_DEPTH_TEST);
741 glDisable(GL_BLEND);
742 glDisable(GL_CULL_FACE);
743 render_fb_bind( &world->heightmap );
744 shader_blitcolour_use();
745 shader_blitcolour_uColour( (v4f){-9999.0f,-9999.0f,-9999.0f,-9999.0f} );
746 render_fsquad();
747
748 /* todo: hmm?? */
749 glEnable(GL_BLEND);
750 glBlendFunc(GL_ONE, GL_ONE);
751 glBlendEquation(GL_MAX);
752
753 render_world_position( world, &ortho );
754 glDisable(GL_BLEND);
755 glEnable(GL_DEPTH_TEST);
756 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
757
758 }
759
760 vg_release_thread_sync();
761
762 #if 0
763 /*
764 * Setup scene collider
765 */
766 reset_player( 1, (const char *[]){"start"} );
767 #endif
768 }
769
770 VG_STATIC void world_process_resources( world_instance *world )
771 {
772 vg_info( "Loading textures\n" );
773 world->texture_count = world->meta->info.texture_count;
774 world->textures = vg_linear_alloc( world_global.generic_heap,
775 sizeof(GLuint)*world->texture_count );
776
777 vg_acquire_thread_sync();
778 {
779 /* error texture */
780 world->textures[0] = vg_tex2d_new();
781 vg_tex2d_set_error();
782 vg_tex2d_nearest();
783 vg_tex2d_repeat();
784
785 for( int i=1; i<world->texture_count; i++ )
786 {
787 mdl_texture *tex = &world->meta->texture_buffer[i];
788
789 if( !tex->pack_offset )
790 {
791 vg_release_thread_sync();
792 vg_fatal_exit_loop( "World models must have packed textures!" );
793 }
794
795 vg_linear_clear( vg_mem.scratch );
796 world->textures[i] = vg_tex2d_new();
797 vg_tex2d_set_error();
798 vg_tex2d_qoi( world->meta->pack + tex->pack_offset, tex->pack_length,
799 mdl_pstr( world->meta, tex->pstr_name ));
800 vg_tex2d_nearest();
801 vg_tex2d_repeat();
802 }
803 }
804 vg_release_thread_sync();
805
806 vg_info( "Loading materials\n" );
807
808 u32 size = sizeof(struct world_material) * world->meta->info.material_count;
809 world->materials = vg_linear_alloc( world_global.generic_heap, size );
810
811 world->material_count = world->meta->info.material_count;
812 memset( world->materials, 0, size );
813
814 for( int i=1; i<world->material_count; i++ )
815 world->materials[i].info = world->meta->material_buffer[i];
816
817 /* error material */
818 struct world_material *errmat = &world->materials[0];
819 v4_copy( (v4f){ 1.0f,0.0f,0.0f,1.0f }, errmat->info.colour );
820 v4_copy( (v4f){ 1.0f,0.0f,0.0f,1.0f }, errmat->info.colour1 );
821 errmat->info.flags = 0x00;
822 errmat->info.pstr_name = 0; /* useless? */
823 errmat->info.shader = -1;
824 errmat->info.tex_decal = 0;
825 errmat->info.tex_diffuse = 0;
826 errmat->info.tex_normal = 0;
827 }
828
829 VG_STATIC void world_unload( world_instance *world )
830 {
831 vg_acquire_thread_sync();
832
833 /* free meshes */
834 mesh_free( &world->mesh_route_lines );
835 mesh_free( &world->mesh_geo );
836 mesh_free( &world->mesh_no_collide );
837
838 glDeleteBuffers( 1, &world->tbo_light_entities );
839 glDeleteTextures( 1, &world->tex_light_entities );
840
841 /* FIXME: CANT DO THIS HERE */
842 world_global.time = 0.0;
843 world_global.rewind_from = 0.0;
844 world_global.rewind_to = 0.0;
845 world_global.last_use = 0.0;
846 world_global.active_gate = 0;
847 world_global.current_run_version = 2;
848 world_global.active_route_board = 0;
849
850 for( int i=0; i<vg_list_size(world_global.ui_bars); i++ )
851 {
852 struct route_ui_bar *uib = &world_global.ui_bars[i];
853 uib->segment_start = 0;
854 uib->segment_count = 0;
855 uib->fade_start = 0;
856 uib->fade_count = 0;
857 uib->fade_timer_start = 0.0;
858 uib->xpos = 0.0f;
859 }
860
861 /* delete textures and meshes */
862 glDeleteTextures( world->texture_count, world->textures );
863
864 /* delete the entire block of memory */
865 /* FIXME: WE CANT DO THIS SHIT ANYMORE, NEED TO DEALLOC FROM ABOVE */
866 #if 0
867 vg_linear_clear( world->dynamic_vgl );
868 vg_linear_clear( world->audio_vgl );
869 #endif
870
871
872 vg_release_thread_sync();
873 }
874
875 VG_STATIC void world_add_global_light( world_instance *world,
876 v2f dir, v3f colour )
877 {
878 int id = world->ub_lighting.g_light_count;
879
880 v3_copy( colour, world->ub_lighting.g_light_colours[id] );
881 v3_copy( (v3f){ cosf(dir[1]) * cosf(dir[0]),
882 sinf(dir[0]),
883 sinf(dir[1]) * cosf(dir[0]) },
884 world->ub_lighting.g_light_directions[id] );
885
886 world->ub_lighting.g_light_count ++;
887 }
888
889 VG_STATIC void world_clean( world_instance *world )
890 {
891 /* clean dangling pointers */
892 world->meta = NULL;
893
894 world->textures = NULL;
895 world->texture_count = 0;
896 world->materials = NULL;
897 world->material_count = 0;
898
899 world->scene_geo = NULL;
900 world->scene_no_collide = NULL;
901 world->scene_lines = NULL;
902
903 world->geo_bh = NULL;
904 world->trigger_bh = NULL;
905 world->audio_bh = NULL;
906
907 world->spawns = NULL;
908 world->spawn_count = 0;
909
910 world->audio_things = NULL;
911 world->audio_things_count = 0;
912
913 world->triggers = NULL;
914 world->trigger_count = 0;
915
916 world->lights = NULL;
917 world->light_count = 0;
918
919 world->logic_relays = NULL;
920 world->relay_count = 0;
921
922 world->logic_achievements = NULL;
923 world->achievement_count = 0;
924
925 world->nodes = NULL;
926 world->node_count = 0;
927
928 world->routes = NULL;
929 world->route_count = 0;
930
931 world->gates = NULL;
932 world->gate_count = 0;
933
934 world->collectors = NULL;
935 world->collector_count = 0;
936
937 world->nonlocal_gates = NULL;
938 world->nonlocalgate_count = 0;
939
940 world->water.enabled = 0;
941
942
943 /* default lighting conditions
944 * -------------------------------------------------------------*/
945 world->ub_lighting.g_light_count = 0;
946 world->ub_lighting.g_light_preview = 0;
947 world->ub_lighting.g_shadow_samples = 8;
948 world->ub_lighting.g_water_fog = 0.04f;
949
950 v4_zero( world->ub_lighting.g_water_plane );
951 v4_zero( world->ub_lighting.g_depth_bounds );
952 v4_zero( world->ub_lighting.g_ambient_colour );
953
954 v3_copy( (v3f){ 0.09f, 0.03f, 0.07f }, world->ub_lighting.g_ambient_colour );
955
956 world_add_global_light( world, (v2f){ 0.63f, -0.08f },
957 (v3f){ 1.36f, 1.35f, 1.01f } );
958
959 world_add_global_light( world, (v2f){ -2.60f, -0.13f },
960 (v3f){ 0.33f, 0.56f, 0.64f } );
961
962 world_add_global_light( world, (v2f){ 2.60f, -0.84f },
963 (v3f){ 0.05f, 0.05f, 0.23f } );
964
965 world->ub_lighting.g_light_directions[0][3] = 9.50f;
966 world->ub_lighting.g_light_colours[0][3] = 0.65f;
967 }
968
969 VG_STATIC void world_load( world_instance *world, const char *path )
970 {
971 world_unload( world );
972 world_clean( world );
973
974 world->meta = mdl_load_full( world_global.generic_heap, path );
975 vg_info( "Loading world: %s\n", path );
976
977 /* process resources from pack */
978 world_process_resources( world );
979
980 /* dynamic allocations */
981 world_ents_allocate( world );
982 world_routes_allocate( world );
983
984 /* meta processing */
985 world_routes_process( world );
986 world_entities_process( world );
987
988 /* main bulk */
989 world_generate( world );
990 world_routes_generate( world );
991 world_post_process( world );
992 }
993
994 #endif /* WORLD_GEN_H */