model fmt & heisenbug
[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 /* load world TODO: Put back vg back in loading state while this happens */
11 VG_STATIC void world_load(void);
12
13
14 VG_STATIC void world_add_all_if_material( m4x3f transform, scene *pscene,
15 mdl_context *mdl, u32 id )
16 {
17 for( int i=0; i<mdl->info.node_count; i++ )
18 {
19 mdl_node *pnode = mdl_node_from_id( mdl, i );
20
21 for( int j=0; j<pnode->submesh_count; j++ )
22 {
23 mdl_submesh *sm = mdl_node_submesh( mdl, pnode, j );
24 if( sm->material_id == id )
25 {
26 m4x3f transform2;
27 mdl_node_transform( pnode, transform2 );
28 m4x3_mul( transform, transform2, transform2 );
29
30 scene_add_submesh( pscene, mdl, sm, transform2 );
31 }
32 }
33 }
34 }
35
36 /* Sprinkle foliage models over the map on terrain material */
37 VG_STATIC void world_apply_procedural_foliage(void)
38 {
39 vg_linear_clear( vg_mem.scratch );
40
41 mdl_context *mfoliage =
42 mdl_load_full( vg_mem.scratch, "models/rs_foliage.mdl");
43
44 v3f volume;
45 v3_sub( world.scene_geo->bbx[1], world.scene_geo->bbx[0], volume );
46 volume[1] = 1.0f;
47
48 m4x3f transform;
49 mdl_node *mblob = mdl_node_from_name( mfoliage, "blob" );
50 mdl_submesh *sm_blob = mdl_node_submesh( mfoliage, mblob, 0 );
51
52 for( int i=0;i<100000;i++ )
53 {
54 v3f pos;
55 v3_mul( volume, (v3f){ vg_randf(), 1000.0f, vg_randf() }, pos );
56 pos[1] = 1000.0f;
57 v3_add( pos, world.scene_geo->bbx[0], pos );
58
59 ray_hit hit;
60 hit.dist = INFINITY;
61
62 if( ray_world( pos, (v3f){0.0f,-1.0f,0.0f}, &hit ))
63 {
64 if( (hit.normal[1] > 0.8f) && ray_hit_is_terrain(&hit) &&
65 (hit.pos[1] > 0.0f+10.0f) )
66 {
67 v4f qsurface, qrandom;
68 v3f axis;
69
70 v3_cross( (v3f){0.0f,1.0f,0.0f}, hit.normal, axis );
71
72 float angle = v3_dot(hit.normal,(v3f){0.0f,1.0f,0.0f});
73 q_axis_angle( qsurface, axis, angle );
74 q_axis_angle( qrandom, (v3f){0.0f,1.0f,0.0f}, vg_randf()*VG_TAUf );
75 q_mul( qsurface, qrandom, qsurface );
76 q_m3x3( qsurface, transform );
77
78 v3_copy( hit.pos, transform[3] );
79 scene_add_submesh( world.scene_no_collide, mfoliage,
80 sm_blob, transform);
81 }
82 }
83 }
84 }
85
86 VG_STATIC void world_ents_allocate(void)
87 {
88 vg_info( "Allocating entities\n" );
89
90 /* count entites to allocate buffers for them.
91 * maybe in the future we just store these numbers in the model file...
92 *
93 * TODO: use this in world_routes too */
94
95 struct countable
96 {
97 enum classtype ct;
98 void **to_allocate;
99 u32 item_size;
100 int count;
101 }
102 entity_counts[] =
103 {
104 {
105 k_classtype_spawn,
106 (void*)&world.spawns,
107 sizeof(struct respawn_point)
108 },
109 {
110 k_classtype_audio,
111 (void*)&world.audio_things,
112 sizeof(struct world_audio_thing)
113 },
114 {
115 k_classtype_trigger,
116 (void*)&world.triggers,
117 sizeof(struct trigger_zone)
118 }
119 };
120
121 for( int i=0; i<vg_list_size(entity_counts); i++ )
122 entity_counts[i].count = 0;
123
124 for( int i=0; i<world.meta->info.node_count; i++ )
125 {
126 mdl_node *pnode = mdl_node_from_id( world.meta, i );
127
128 for( int j=0; j<vg_list_size(entity_counts); j ++ )
129 {
130 if( pnode->classtype == entity_counts[j].ct )
131 {
132 entity_counts[j].count ++;
133 break;
134 }
135 }
136 }
137
138 for( int i=0; i<vg_list_size(entity_counts); i++ )
139 {
140 struct countable *counter = &entity_counts[i];
141
142 u32 bufsize = counter->item_size*counter->count;
143 *counter->to_allocate = vg_linear_alloc( world.dynamic_vgl, bufsize );
144 }
145 }
146
147 VG_STATIC void world_pct_spawn( mdl_node *pnode )
148 {
149 struct respawn_point *rp = &world.spawns[ world.spawn_count ++ ];
150
151 v3_copy( pnode->co, rp->co );
152 v4_copy( pnode->q, rp->q );
153 rp->name = mdl_pstr( world.meta, pnode->pstr_name );
154 }
155
156 VG_STATIC void world_pct_water( mdl_node *pnode )
157 {
158 if( world.water.enabled )
159 {
160 vg_warn( "Multiple water surfaces in level! ('%s')\n",
161 mdl_pstr( world.meta, pnode->pstr_name ));
162 return;
163 }
164
165 mdl_submesh *sm = mdl_node_submesh( world.meta, pnode, 0 );
166
167 if( sm )
168 {
169 vg_acquire_thread_sync();
170 {
171 mdl_unpack_submesh( world.meta, &world.mesh_water, sm );
172 world.water.enabled = 1;
173 water_set_surface( pnode->co[1] );
174 }
175 vg_release_thread_sync();
176 }
177 else
178 {
179 vg_warn( "Water entity has no submeshes!\n" );
180 }
181 }
182
183 VG_STATIC void world_pct_audio( mdl_node *pnode )
184 {
185 struct world_audio_thing *thing = &world.audio_things[
186 world.audio_things_count ];
187
188 memset( thing, 0, sizeof(struct world_audio_thing) );
189 struct classtype_audio *aud = mdl_get_entdata( world.meta, pnode );
190
191 v3_copy( pnode->co, thing->pos );
192
193 if( aud->flags & AUDIO_FLAG_SPACIAL_3D )
194 thing->volume = aud->volume * pnode->s[0];
195 else
196 thing->volume = aud->volume;
197
198 thing->flags = aud->flags;
199 thing->temp_embedded_clip.path = mdl_pstr( world.meta, aud->pstr_file );
200 thing->temp_embedded_clip.source_mode = k_audio_source_mono;
201
202 audio_clip_load( &thing->temp_embedded_clip );
203 thing->player.name = mdl_pstr( world.meta, pnode->pstr_name );
204 thing->player.enqued = 0;
205
206 pnode->sub_uid = world.audio_things_count;
207 world.audio_things_count ++;
208 }
209
210 VG_STATIC void world_entities_process(void)
211 {
212 struct entity_instruction
213 {
214 enum classtype ct;
215 void (*process)( mdl_node *pnode );
216 }
217 entity_instructions[] =
218 {
219 { k_classtype_spawn, world_pct_spawn },
220 { k_classtype_water, world_pct_water },
221 { k_classtype_audio, world_pct_audio },
222 };
223
224 for( int i=0; i<world.meta->info.node_count; i++ )
225 {
226 mdl_node *pnode = mdl_node_from_id( world.meta, i );
227
228 for( int j=0; j<vg_list_size(entity_instructions); j++ )
229 {
230 struct entity_instruction *instr = &entity_instructions[j];
231
232 if( pnode->classtype == instr->ct )
233 {
234 instr->process( pnode );
235 break;
236 }
237 }
238
239 #if 0
240 else if( pnode->classtype == k_classtype_achievement_box )
241 {
242 world.achievement_zones =
243 buffer_reserve( world.achievement_zones,
244 world.achievement_zones_count,
245 &world.achievement_zones_cap, 1,
246 sizeof(struct achievement_zone) );
247
248 struct achievement_zone *zone = &world.achievement_zones[
249 world.achievement_zones_count ++ ];
250
251
252 struct classtype_achievement_box *box = mdl_get_entdata(mworld,pnode);
253
254 mdl_node_transform( pnode, zone->transform );
255 m4x3_invert_full( zone->transform, zone->inv_transform );
256 vg_strncpy( mdl_pstr(mworld, box->pstr_name), zone->name, 31 );
257 zone->name[31] = 0x00;
258 zone->triggered = 0;
259
260 if( box->trigger )
261 zone->ptarget_delegated = mdl_node_from_id( mworld, box->trigger );
262 else
263 zone->ptarget_delegated = NULL;
264 }
265 #endif
266 }
267
268 #if 0
269 /* fixup links */
270 for( int i=0; i<world.achievement_zones_count; i ++ )
271 {
272 struct achievement_zone *ach = &world.achievement_zones[ i ];
273 if( ach->ptarget_delegated )
274 {
275 u32 id = ach->ptarget_delegated->sub_uid;
276 ach->ptarget = &world.audio_things[ id ];
277 }
278 else
279 ach->ptarget = NULL;
280 }
281 #endif
282 }
283
284 VG_STATIC void world_generate(void)
285 {
286 /*
287 * Compile meshes into the world scenes
288 */
289 world.scene_geo = scene_init( world.dynamic_vgl, 350000, 1200000 );
290
291 /*
292 * TODO: System to dynamically allocate these
293 */
294 u32 mat_surf = 0,
295 mat_surf_oob = 0,
296 mat_vertex_blend = 0,
297 mat_alphatest = 0,
298 mat_graffiti = 0,
299 mat_subworld = 0,
300 mat_terrain = 0;
301
302 for( int i=1; i<world.meta->info.material_count; i++ )
303 {
304 mdl_material *mat = &world.meta->material_buffer[ i ];
305 const char *mat_name = mdl_pstr( world.meta, mat->pstr_name );
306
307 if( !strcmp( "surf", mat_name ))
308 mat_surf = i;
309 else if( !strcmp( "surf_oob", mat_name ))
310 mat_surf_oob = i;
311 else if( !strcmp( "vertex_blend", mat_name ))
312 mat_vertex_blend = i;
313 else if( !strcmp( "alphatest", mat_name ))
314 mat_alphatest = i;
315 else if( !strcmp( "graffitibox", mat_name ))
316 mat_graffiti = i;
317 else if( !strcmp( "terrain", mat_name ) )
318 mat_terrain = i;
319 }
320
321 m4x3f midentity;
322 m4x3_identity( midentity );
323
324 /*
325 * Generate scene: collidable geometry
326 * ----------------------------------------------------------------
327 */
328
329 vg_info( "Generating collidable geometry\n" );
330 vg_info( "terrain...\n" );
331 /* terrain */
332 if( mat_terrain )
333 world_add_all_if_material( midentity, world.scene_geo,
334 world.meta, mat_terrain );
335 scene_copy_slice( world.scene_geo, &world.sm_terrain );
336
337 /* oob */
338 vg_info( "oob...\n" );
339 if( mat_surf_oob )
340 world_add_all_if_material( midentity, world.scene_geo,
341 world.meta, mat_surf_oob );
342 else
343 vg_warn( "No OOB surface\n" );
344 scene_copy_slice( world.scene_geo, &world.sm_geo_std_oob );
345
346
347 /* surface */
348 vg_info( "surface...\n" );
349 if( mat_surf )
350 world_add_all_if_material( midentity, world.scene_geo,
351 world.meta, mat_surf );
352 scene_copy_slice( world.scene_geo, &world.sm_geo_std );
353
354 /* vertex_blend */
355 vg_info( "vertex blend...\n" );
356 if( mat_vertex_blend )
357 world_add_all_if_material( midentity, world.scene_geo,
358 world.meta, mat_vertex_blend);
359 scene_copy_slice( world.scene_geo, &world.sm_geo_vb );
360
361 /* compress that bad boy */
362 world.scene_geo = scene_fix( world.dynamic_vgl, world.scene_geo );
363
364 vg_acquire_thread_sync();
365 {
366 scene_upload( world.scene_geo, &world.mesh_geo );
367 }
368 vg_release_thread_sync();
369
370 /* setup spacial mapping and rigidbody */
371 world.geo_bh = scene_bh_create( world.dynamic_vgl, world.scene_geo );
372
373 v3_zero( world.rb_geo.co );
374 q_identity( world.rb_geo.q );
375
376 world.rb_geo.type = k_rb_shape_scene;
377 world.rb_geo.inf.scene.bh_scene = world.geo_bh;
378 world.rb_geo.is_world = 1;
379 rb_init( &world.rb_geo );
380
381 /*
382 * Generate scene: non-collidable geometry
383 * ----------------------------------------------------------------
384 */
385 vg_info( "Generating non-collidable geometry\n" );
386
387 world.scene_no_collide = scene_init( world.dynamic_vgl, 200000, 500000 );
388
389 vg_info( "Applying foliage\n" );
390 srand(0);
391 world_apply_procedural_foliage();
392 scene_copy_slice( world.scene_no_collide, &world.sm_foliage_main );
393
394 vg_info( "alphatest...\n" );
395 world_add_all_if_material( midentity, world.scene_no_collide,
396 world.meta, mat_alphatest );
397 scene_copy_slice( world.scene_no_collide, &world.sm_foliage_alphatest );
398
399 vg_info( "graffiti...\n" );
400 world_add_all_if_material( midentity, world.scene_no_collide,
401 world.meta, mat_graffiti );
402 scene_copy_slice( world.scene_no_collide, &world.sm_graffiti );
403
404
405 /* upload and free that */
406 vg_acquire_thread_sync();
407 {
408 scene_upload( world.scene_no_collide, &world.mesh_no_collide );
409 }
410 vg_release_thread_sync();
411
412 vg_linear_del( world.dynamic_vgl, world.scene_no_collide );
413 world.scene_no_collide = NULL;
414 }
415
416 VG_STATIC void world_post_process(void)
417 {
418 /* initialize audio if need be */
419 audio_lock();
420 for( int i=0; i<world.audio_things_count; i++ )
421 {
422 struct world_audio_thing *thingy = &world.audio_things[ i ];
423
424 audio_player_init( &thingy->player );
425 audio_player_set_flags( &thingy->player, thingy->flags );
426 audio_player_set_vol( &thingy->player, thingy->volume );
427 audio_player_set_pan( &thingy->player, 0.0f );
428
429 if( thingy->flags & AUDIO_FLAG_SPACIAL_3D )
430 audio_player_set_position( &thingy->player, thingy->pos );
431
432 if( thingy->flags & AUDIO_FLAG_AUTO_START )
433 audio_player_playclip( &thingy->player, &thingy->temp_embedded_clip );
434 }
435 audio_unlock();
436
437 vg_acquire_thread_sync();
438 {
439 /*
440 * Rendering the depth map
441 */
442 m4x4f ortho;
443 m4x3f camera;
444
445 v3f extent;
446 v3_sub( world.scene_geo->bbx[1], world.scene_geo->bbx[0], extent );
447
448 float fl = world.scene_geo->bbx[0][0],
449 fr = world.scene_geo->bbx[1][0],
450 fb = world.scene_geo->bbx[0][2],
451 ft = world.scene_geo->bbx[1][2],
452 rl = 1.0f / (fr-fl),
453 tb = 1.0f / (ft-fb);
454
455 m4x4_zero( ortho );
456 ortho[0][0] = 2.0f * rl;
457 ortho[2][1] = 2.0f * tb;
458 ortho[3][0] = (fr + fl) * -rl;
459 ortho[3][1] = (ft + fb) * -tb;
460 ortho[3][3] = 1.0f;
461 m4x3_identity( camera );
462
463 glDisable(GL_DEPTH_TEST);
464 glDisable(GL_BLEND);
465 glDisable(GL_CULL_FACE);
466 glBindFramebuffer( GL_FRAMEBUFFER, gpipeline.fb_depthmap );
467 glViewport( 0, 0, 1024, 1024 );
468 shader_fscolour_use();
469 shader_fscolour_uColour( (v4f){-9999.0f,-9999.0f,-9999.0f,-9999.0f} );
470 render_fsquad();
471
472 /* todo: hmm?? */
473 glEnable(GL_BLEND);
474 glBlendFunc(GL_ONE, GL_ONE);
475 glBlendEquation(GL_MAX);
476 render_world_depth( ortho, camera );
477 glDisable(GL_BLEND);
478 glEnable(GL_DEPTH_TEST);
479 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
480
481
482 /*
483 * TODO: World settings entity
484 */
485 struct ub_world_lighting *winfo = &gpipeline.ub_world_lighting;
486
487 if( world.water.enabled )
488 v4_copy( world.water.plane, winfo->g_water_plane );
489
490 v4f info_vec;
491 v3f *bounds = world.scene_geo->bbx;
492
493 info_vec[0] = bounds[0][0];
494 info_vec[1] = bounds[0][2];
495 info_vec[2] = 1.0f/ (bounds[1][0]-bounds[0][0]);
496 info_vec[3] = 1.0f/ (bounds[1][2]-bounds[0][2]);
497 v4_copy( info_vec, winfo->g_depth_bounds );
498
499 winfo->g_water_fog = 0.04f;
500 render_update_lighting_ub();
501 }
502
503 vg_release_thread_sync();
504
505 /*
506 * Setup scene collider
507 */
508 }
509
510
511 VG_STATIC void world_unload(void)
512 {
513 /* free meshes */
514 mesh_free( &world.mesh_geo );
515 mesh_free( &world.mesh_no_collide );
516 mesh_free( &world.mesh_route_lines );
517 mesh_free( &world.mesh_water );
518
519 world.time = 0.0;
520 world.rewind_from = 0.0;
521 world.rewind_to = 0.0;
522 world.last_use = 0.0;
523 world.active_gate = 0;
524 world.current_run_version = 2;
525 world.active_route_board = 0;
526 v3_zero( world.render_gate_pos );
527
528 for( int i=0; i<vg_list_size(world.ui_bars); i++ )
529 {
530 struct route_ui_bar *uib = &world.ui_bars[i];
531 uib->segment_start = 0;
532 uib->segment_count = 0;
533 uib->fade_start = 0;
534 uib->fade_count = 0;
535 uib->fade_timer_start = 0.0;
536 uib->xpos = 0.0f;
537 }
538
539 /* delete the entire block of memory */
540 vg_linear_clear( world.dynamic_vgl );
541
542 /* clean dangling pointers */
543 world.meta = NULL;
544
545 world.scene_geo = NULL;
546 world.scene_lines = NULL;
547 world.scene_no_collide = NULL;
548
549 world.geo_bh = NULL;
550 world.trigger_bh = NULL;
551 world.audio_bh = NULL;
552
553 world.spawns = NULL;
554 world.spawn_count = 0;
555
556 world.audio_things = NULL;
557 world.audio_things_count = 0;
558
559 world.logic_entities = NULL;
560 world.logic_entity_count = 0;
561
562 world.logic_actions = NULL;
563 world.logic_action_count = 0;
564
565 world.triggers = NULL;
566 world.trigger_count = 0;
567
568 world.nodes = NULL;
569 world.node_count = 0;
570
571 world.routes = NULL;
572 world.route_count = 0;
573
574 world.gates = NULL;
575 world.gate_count = 0;
576
577 world.collectors = NULL;
578 world.collector_count = 0;
579
580 world.water.enabled = 0;
581 }
582
583 VG_STATIC void world_load(void)
584 {
585 world_unload();
586
587 world.meta = mdl_load_full( world.dynamic_vgl, world.world_name );
588 vg_info( "Loading world: %s\n", world.world_name );
589
590 /* dynamic allocations */
591 world_ents_allocate();
592 world_routes_allocate();
593
594 /* meta processing */
595 world_routes_process();
596 world_entities_process();
597
598 /* main bulk */
599 world_generate();
600 world_routes_generate();
601 world_post_process();
602 }
603
604 #endif /* WORLD_GEN_H */