2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 /* load world TODO: Put back vg back in loading state while this happens */
11 VG_STATIC
void world_load( const char *mdl_file
);
15 VG_STATIC
void world_add_all_if_material( m4x3f transform
, scene
*pscene
,
16 mdl_context
*mdl
, u32 id
)
18 for( int i
=0; i
<mdl
->info
.node_count
; i
++ )
20 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
22 for( int j
=0; j
<pnode
->submesh_count
; j
++ )
24 mdl_submesh
*sm
= mdl_node_submesh( mdl
, pnode
, j
);
25 if( sm
->material_id
== id
)
28 mdl_node_transform( pnode
, transform2
);
29 m4x3_mul( transform
, transform2
, transform2
);
31 scene_add_submesh( pscene
, mdl
, sm
, transform2
);
36 if( pnode
->classtype
== k_classtype_instance
)
40 u32 instance_id
= pnode
->sub_uid
-1;
41 struct instance_cache
*cache
= &world
.instance_cache
[instance_id
];
42 mdl_context
*mdl2
= cache
->mdl
;
45 mdl_node_transform( pnode
, transform2
);
46 m4x3_mul( transform
, transform2
, transform2
);
48 world_add_all_if_material( transform2
, pscene
, mdl2
, id
);
55 /* Sprinkle foliage models over the map on terrain material */
56 VG_STATIC
void world_apply_procedural_foliage(void)
58 vg_linear_clear( vg_mem
.scratch
);
60 mdl_context
*mfoliage
=
61 mdl_load_full( vg_mem
.scratch
, "models/rs_foliage.mdl");
64 v3_sub( world
.scene_geo
->bbx
[1], world
.scene_geo
->bbx
[0], volume
);
68 mdl_node
*mblob
= mdl_node_from_name( mfoliage
, "blob" );
69 mdl_submesh
*sm_blob
= mdl_node_submesh( mfoliage
, mblob
, 0 );
71 for( int i
=0;i
<100000;i
++ )
74 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
76 v3_add( pos
, world
.scene_geo
->bbx
[0], pos
);
81 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
83 if( (hit
.normal
[1] > 0.8f
) && ray_hit_is_terrain(&hit
) &&
84 (hit
.pos
[1] > 0.0f
+10.0f
) )
86 v4f qsurface
, qrandom
;
89 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
.normal
, axis
);
91 float angle
= v3_dot(hit
.normal
,(v3f
){0.0f
,1.0f
,0.0f
});
92 q_axis_angle( qsurface
, axis
, angle
);
93 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
94 q_mul( qsurface
, qrandom
, qsurface
);
95 q_m3x3( qsurface
, transform
);
97 v3_copy( hit
.pos
, transform
[3] );
98 scene_add_submesh( world
.scene_no_collide
, mfoliage
,
105 VG_STATIC
void world_ents_allocate(void)
107 vg_info( "Allocating entities\n" );
109 /* count entites to allocate buffers for them.
110 * maybe in the future we just store these numbers in the model file...
112 * TODO: use this in world_routes too */
125 (void*)&world
.spawns
,
126 sizeof(struct respawn_point
)
130 (void*)&world
.audio_things
,
131 sizeof(struct world_audio_thing
)
135 (void*)&world
.triggers
,
136 sizeof(struct trigger_zone
)
140 for( int i
=0; i
<vg_list_size(entity_counts
); i
++ )
141 entity_counts
[i
].count
= 0;
143 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
145 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
147 for( int j
=0; j
<vg_list_size(entity_counts
); j
++ )
149 if( pnode
->classtype
== entity_counts
[j
].ct
)
151 entity_counts
[j
].count
++;
157 for( int i
=0; i
<vg_list_size(entity_counts
); i
++ )
159 struct countable
*counter
= &entity_counts
[i
];
161 u32 bufsize
= counter
->item_size
*counter
->count
;
162 *counter
->to_allocate
= vg_linear_alloc( world
.dynamic_vgl
, bufsize
);
166 VG_STATIC
void world_pct_spawn( mdl_node
*pnode
)
168 struct respawn_point
*rp
= &world
.spawns
[ world
.spawn_count
++ ];
170 v3_copy( pnode
->co
, rp
->co
);
171 v4_copy( pnode
->q
, rp
->q
);
172 rp
->name
= mdl_pstr( world
.meta
, pnode
->pstr_name
);
175 VG_STATIC
void world_pct_water( mdl_node
*pnode
)
177 if( world
.water
.enabled
)
179 vg_warn( "Multiple water surfaces in level! ('%s')\n",
180 mdl_pstr( world
.meta
, pnode
->pstr_name
));
184 mdl_submesh
*sm
= mdl_node_submesh( world
.meta
, pnode
, 0 );
188 vg_acquire_thread_sync();
190 mdl_unpack_submesh( world
.meta
, &world
.mesh_water
, sm
);
191 world
.water
.enabled
= 1;
192 water_set_surface( pnode
->co
[1] );
194 vg_release_thread_sync();
198 vg_warn( "Water entity has no submeshes!\n" );
202 VG_STATIC
void world_pct_instance( mdl_node
*pnode
)
204 struct classtype_instance
*inst
= mdl_get_entdata( world
.meta
, pnode
);
208 for( int i
=0; i
<world
.instance_cache_count
; i
++ )
210 struct instance_cache
*cache
= &world
.instance_cache
[i
];
211 if( inst
->pstr_file
== cache
->pstr_file
)
214 pnode
->sub_uid
= i
+1;
221 if( world
.instance_cache_count
== vg_list_size(world
.instance_cache
) )
222 vg_fatal_exit_loop( "Instance cache is full!" );
224 struct instance_cache
*cache
=
225 &world
.instance_cache
[world
.instance_cache_count
++ ];
227 cache
->pstr_file
= inst
->pstr_file
;
230 cache
->mdl
= mdl_load( filename
);
234 world
.instance_cache_count
++;
235 pnode
->sub_uid
= world
.instance_cache_count
;
236 mdl_link_materials( mworld
, cache
->mdl
);
237 vg_success( "Cached %s\n", filename
);
241 vg_warn( "Failed to cache %s\n", filename
);
247 VG_STATIC
void world_pct_audio( mdl_node
*pnode
)
249 struct world_audio_thing
*thing
= &world
.audio_things
[
250 world
.audio_things_count
];
252 memset( thing
, 0, sizeof(struct world_audio_thing
) );
253 struct classtype_audio
*aud
= mdl_get_entdata( world
.meta
, pnode
);
255 v3_copy( pnode
->co
, thing
->pos
);
257 if( aud
->flags
& AUDIO_FLAG_SPACIAL_3D
)
258 thing
->volume
= aud
->volume
* pnode
->s
[0];
260 thing
->volume
= aud
->volume
;
262 thing
->flags
= aud
->flags
;
263 thing
->temp_embedded_clip
.path
= mdl_pstr( world
.meta
, aud
->pstr_file
);
264 audio_clip_load( &thing
->temp_embedded_clip
);
265 thing
->player
.name
= mdl_pstr( world
.meta
, pnode
->pstr_name
);
266 thing
->player
.enqued
= 0;
268 pnode
->sub_uid
= world
.audio_things_count
;
269 world
.audio_things_count
++;
272 VG_STATIC
void world_entities_process(void)
274 struct entity_instruction
277 void (*process
)( mdl_node
*pnode
);
279 entity_instructions
[] =
281 { k_classtype_spawn
, world_pct_spawn
},
282 { k_classtype_water
, world_pct_water
},
283 { k_classtype_instance
, world_pct_instance
},
284 { k_classtype_audio
, world_pct_audio
},
287 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
289 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
291 for( int j
=0; j
<vg_list_size(entity_instructions
); j
++ )
293 struct entity_instruction
*instr
= &entity_instructions
[j
];
295 if( pnode
->classtype
== instr
->ct
)
297 instr
->process( pnode
);
303 else if( pnode
->classtype
== k_classtype_achievement_box
)
305 world
.achievement_zones
=
306 buffer_reserve( world
.achievement_zones
,
307 world
.achievement_zones_count
,
308 &world
.achievement_zones_cap
, 1,
309 sizeof(struct achievement_zone
) );
311 struct achievement_zone
*zone
= &world
.achievement_zones
[
312 world
.achievement_zones_count
++ ];
315 struct classtype_achievement_box
*box
= mdl_get_entdata(mworld
,pnode
);
317 mdl_node_transform( pnode
, zone
->transform
);
318 m4x3_invert_full( zone
->transform
, zone
->inv_transform
);
319 vg_strncpy( mdl_pstr(mworld
, box
->pstr_name
), zone
->name
, 31 );
320 zone
->name
[31] = 0x00;
324 zone
->ptarget_delegated
= mdl_node_from_id( mworld
, box
->trigger
);
326 zone
->ptarget_delegated
= NULL
;
333 for( int i
=0; i
<world
.achievement_zones_count
; i
++ )
335 struct achievement_zone
*ach
= &world
.achievement_zones
[ i
];
336 if( ach
->ptarget_delegated
)
338 u32 id
= ach
->ptarget_delegated
->sub_uid
;
339 ach
->ptarget
= &world
.audio_things
[ id
];
347 VG_STATIC
void world_load_instance_cache(void)
349 vg_linear_clear( vg_mem
.scratch
);
351 for( int i
=0; i
<world
.instance_cache_count
; i
++ )
353 struct instance_cache
*inst
= &world
.instance_cache
[i
];
355 const char *filename
= mdl_pstr( world
.meta
, inst
->pstr_file
);
356 inst
->mdl
= mdl_load_full( vg_mem
.scratch
, filename
);
360 VG_STATIC
void world_generate(void)
363 * Compile meshes into the world scenes
365 world
.scene_geo
= scene_init( world
.dynamic_vgl
, 500000, 1200000 );
368 * TODO: System to dynamically allocate these
372 mat_vertex_blend
= 0,
378 for( int i
=1; i
<world
.meta
->info
.material_count
; i
++ )
380 mdl_material
*mat
= &world
.meta
->material_buffer
[ i
];
381 const char *mat_name
= mdl_pstr( world
.meta
, mat
->pstr_name
);
383 if( !strcmp( "surf", mat_name
))
385 else if( !strcmp( "surf_oob", mat_name
))
387 else if( !strcmp( "vertex_blend", mat_name
))
388 mat_vertex_blend
= i
;
389 else if( !strcmp( "alphatest", mat_name
))
391 else if( !strcmp( "graffitibox", mat_name
))
393 else if( !strcmp( "terrain", mat_name
) )
398 m4x3_identity( midentity
);
400 world_load_instance_cache();
403 * Generate scene: collidable geometry
404 * ----------------------------------------------------------------
407 vg_info( "Generating collidable geometry\n" );
408 vg_info( "terrain...\n" );
411 world_add_all_if_material( midentity
, world
.scene_geo
,
412 world
.meta
, mat_terrain
);
413 scene_copy_slice( world
.scene_geo
, &world
.sm_terrain
);
416 vg_info( "oob...\n" );
418 world_add_all_if_material( midentity
, world
.scene_geo
,
419 world
.meta
, mat_surf_oob
);
421 vg_warn( "No OOB surface\n" );
422 scene_copy_slice( world
.scene_geo
, &world
.sm_geo_std_oob
);
426 vg_info( "surface...\n" );
428 world_add_all_if_material( midentity
, world
.scene_geo
,
429 world
.meta
, mat_surf
);
430 scene_copy_slice( world
.scene_geo
, &world
.sm_geo_std
);
433 vg_info( "vertex blend...\n" );
434 if( mat_vertex_blend
)
435 world_add_all_if_material( midentity
, world
.scene_geo
,
436 world
.meta
, mat_vertex_blend
);
437 scene_copy_slice( world
.scene_geo
, &world
.sm_geo_vb
);
439 /* compress that bad boy */
440 world
.scene_geo
= scene_fix( world
.dynamic_vgl
, world
.scene_geo
);
442 vg_acquire_thread_sync();
444 scene_upload( world
.scene_geo
, &world
.mesh_geo
);
446 vg_release_thread_sync();
448 /* setup spacial mapping and rigidbody */
449 world
.geo_bh
= scene_bh_create( world
.dynamic_vgl
, world
.scene_geo
);
451 v3_zero( world
.rb_geo
.co
);
452 q_identity( world
.rb_geo
.q
);
454 world
.rb_geo
.type
= k_rb_shape_scene
;
455 world
.rb_geo
.inf
.scene
.bh_scene
= world
.geo_bh
;
456 world
.rb_geo
.is_world
= 1;
457 rb_init( &world
.rb_geo
);
460 * Generate scene: non-collidable geometry
461 * ----------------------------------------------------------------
463 vg_info( "Generating non-collidable geometry\n" );
465 world
.scene_no_collide
= scene_init( world
.dynamic_vgl
, 200000, 500000 );
467 vg_info( "Applying foliage\n" );
468 world_apply_procedural_foliage();
469 scene_copy_slice( world
.scene_no_collide
, &world
.sm_foliage_main
);
471 vg_info( "alphatest...\n" );
472 world_add_all_if_material( midentity
, world
.scene_no_collide
,
473 world
.meta
, mat_alphatest
);
474 scene_copy_slice( world
.scene_no_collide
, &world
.sm_foliage_alphatest
);
476 vg_info( "graffiti...\n" );
477 world_add_all_if_material( midentity
, world
.scene_no_collide
,
478 world
.meta
, mat_graffiti
);
479 scene_copy_slice( world
.scene_no_collide
, &world
.sm_graffiti
);
482 /* upload and free that */
483 vg_acquire_thread_sync();
485 scene_upload( world
.scene_no_collide
, &world
.mesh_no_collide
);
487 vg_release_thread_sync();
489 vg_linear_del( world
.dynamic_vgl
, world
.scene_no_collide
);
490 world
.scene_no_collide
= NULL
;
493 VG_STATIC
void world_post_process(void)
495 /* initialize audio if need be */
497 for( int i
=0; i
<world
.audio_things_count
; i
++ )
499 struct world_audio_thing
*thingy
= &world
.audio_things
[ i
];
501 audio_player_init( &thingy
->player
);
502 audio_player_set_flags( &thingy
->player
, thingy
->flags
);
503 audio_player_set_vol( &thingy
->player
, thingy
->volume
);
504 audio_player_set_pan( &thingy
->player
, 0.0f
);
506 if( thingy
->flags
& AUDIO_FLAG_SPACIAL_3D
)
507 audio_player_set_position( &thingy
->player
, thingy
->pos
);
509 if( thingy
->flags
& AUDIO_FLAG_AUTO_START
)
510 audio_player_playclip( &thingy
->player
, &thingy
->temp_embedded_clip
);
514 vg_acquire_thread_sync();
517 * Rendering the depth map
523 v3_sub( world
.scene_geo
->bbx
[1], world
.scene_geo
->bbx
[0], extent
);
525 float fl
= world
.scene_geo
->bbx
[0][0],
526 fr
= world
.scene_geo
->bbx
[1][0],
527 fb
= world
.scene_geo
->bbx
[0][2],
528 ft
= world
.scene_geo
->bbx
[1][2],
533 ortho
[0][0] = 2.0f
* rl
;
534 ortho
[2][1] = 2.0f
* tb
;
535 ortho
[3][0] = (fr
+ fl
) * -rl
;
536 ortho
[3][1] = (ft
+ fb
) * -tb
;
538 m4x3_identity( camera
);
540 glViewport( 0, 0, 1024, 1024 );
541 glDisable(GL_DEPTH_TEST
);
542 glBindFramebuffer( GL_FRAMEBUFFER
, gpipeline
.fb_depthmap
);
543 shader_fscolour_use();
544 shader_fscolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
548 glBlendFunc(GL_ONE
, GL_ONE
);
549 glBlendEquation(GL_MAX
);
550 render_world_depth( ortho
, camera
);
552 glEnable(GL_DEPTH_TEST
);
553 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
557 * TODO: World settings entity
559 struct ub_world_lighting
*winfo
= &gpipeline
.ub_world_lighting
;
561 if( world
.water
.enabled
)
562 v4_copy( world
.water
.plane
, winfo
->g_water_plane
);
565 v3f
*bounds
= world
.scene_geo
->bbx
;
567 info_vec
[0] = bounds
[0][0];
568 info_vec
[1] = bounds
[0][2];
569 info_vec
[2] = 1.0f
/ (bounds
[1][0]-bounds
[0][0]);
570 info_vec
[3] = 1.0f
/ (bounds
[1][2]-bounds
[0][2]);
571 v4_copy( info_vec
, winfo
->g_depth_bounds
);
573 winfo
->g_water_fog
= 0.04f
;
574 render_update_lighting_ub();
577 vg_release_thread_sync();
580 * Setup scene collider
585 VG_STATIC
void world_unload(void)
588 mesh_free( &world
.mesh_geo
);
589 mesh_free( &world
.mesh_no_collide
);
590 mesh_free( &world
.mesh_route_lines
);
591 mesh_free( &world
.mesh_water
);
593 /* delete the entire block of memory */
594 vg_linear_clear( world
.dynamic_vgl
);
596 /* clean dangling pointers */
599 world
.scene_geo
= NULL
;
600 world
.scene_lines
= NULL
;
601 world
.scene_no_collide
= NULL
;
604 world
.trigger_bh
= NULL
;
605 world
.audio_bh
= NULL
;
608 world
.spawn_count
= 0;
610 world
.audio_things
= NULL
;
611 world
.audio_things_count
= 0;
613 world
.logic_entities
= NULL
;
614 world
.logic_entity_count
= 0;
616 world
.logic_actions
= NULL
;
617 world
.logic_action_count
= 0;
619 world
.triggers
= NULL
;
620 world
.trigger_count
= 0;
623 world
.node_count
= 0;
626 world
.route_count
= 0;
629 world
.gate_count
= 0;
631 world
.collectors
= NULL
;
632 world
.collector_count
= 0;
634 world
.instance_cache_count
= 0;
635 world
.water
.enabled
= 0;
638 VG_STATIC
void world_load( const char *mdl_file
)
642 world
.meta
= mdl_load_full( world
.dynamic_vgl
, mdl_file
);
643 vg_info( "Loading world: %s\n", mdl_file
);
645 /* dynamic allocations */
646 world_ents_allocate();
647 world_routes_allocate();
649 /* meta processing */
650 world_routes_process();
651 world_entities_process();
655 world_routes_generate();
656 world_post_process();
659 #endif /* WORLD_GEN_H */