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(void);
14 VG_STATIC
void world_add_all_if_material( m4x3f transform
, scene
*pscene
,
15 mdl_context
*mdl
, u32 id
)
17 for( int i
=0; i
<mdl
->info
.node_count
; i
++ )
19 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
21 for( int j
=0; j
<pnode
->submesh_count
; j
++ )
23 mdl_submesh
*sm
= mdl_node_submesh( mdl
, pnode
, j
);
24 if( sm
->material_id
== id
)
27 mdl_node_transform( pnode
, transform2
);
28 m4x3_mul( transform
, transform2
, transform2
);
30 scene_add_submesh( pscene
, mdl
, sm
, transform2
);
36 /* Sprinkle foliage models over the map on terrain material */
37 VG_STATIC
void world_apply_procedural_foliage(void)
39 vg_linear_clear( vg_mem
.scratch
);
41 mdl_context
*mfoliage
=
42 mdl_load_full( vg_mem
.scratch
, "models/rs_foliage.mdl");
45 v3_sub( world
.scene_geo
->bbx
[1], world
.scene_geo
->bbx
[0], volume
);
49 mdl_node
*mblob
= mdl_node_from_name( mfoliage
, "blob" );
50 mdl_submesh
*sm_blob
= mdl_node_submesh( mfoliage
, mblob
, 0 );
52 for( int i
=0;i
<100000;i
++ )
55 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
57 v3_add( pos
, world
.scene_geo
->bbx
[0], pos
);
62 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
64 if( (hit
.normal
[1] > 0.8f
) && ray_hit_is_terrain(&hit
) &&
65 (hit
.pos
[1] > 0.0f
+10.0f
) )
67 v4f qsurface
, qrandom
;
70 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
.normal
, axis
);
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
);
78 v3_copy( hit
.pos
, transform
[3] );
79 scene_add_submesh( world
.scene_no_collide
, mfoliage
,
86 VG_STATIC
void world_ents_allocate(void)
88 vg_info( "Allocating entities\n" );
90 /* count entites to allocate buffers for them.
91 * maybe in the future we just store these numbers in the model file...
93 * TODO: use this in world_routes too */
106 (void*)&world
.spawns
,
107 sizeof(struct respawn_point
)
111 (void*)&world
.audio_things
,
112 sizeof(struct world_audio_thing
)
116 (void*)&world
.triggers
,
117 sizeof(struct trigger_zone
)
121 for( int i
=0; i
<vg_list_size(entity_counts
); i
++ )
122 entity_counts
[i
].count
= 0;
124 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
126 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
128 for( int j
=0; j
<vg_list_size(entity_counts
); j
++ )
130 if( pnode
->classtype
== entity_counts
[j
].ct
)
132 entity_counts
[j
].count
++;
138 for( int i
=0; i
<vg_list_size(entity_counts
); i
++ )
140 struct countable
*counter
= &entity_counts
[i
];
142 u32 bufsize
= counter
->item_size
*counter
->count
;
143 *counter
->to_allocate
= vg_linear_alloc( world
.dynamic_vgl
, bufsize
);
147 VG_STATIC
void world_pct_spawn( mdl_node
*pnode
)
149 struct respawn_point
*rp
= &world
.spawns
[ world
.spawn_count
++ ];
151 v3_copy( pnode
->co
, rp
->co
);
152 v4_copy( pnode
->q
, rp
->q
);
153 rp
->name
= mdl_pstr( world
.meta
, pnode
->pstr_name
);
156 VG_STATIC
void world_pct_water( mdl_node
*pnode
)
158 if( world
.water
.enabled
)
160 vg_warn( "Multiple water surfaces in level! ('%s')\n",
161 mdl_pstr( world
.meta
, pnode
->pstr_name
));
165 mdl_submesh
*sm
= mdl_node_submesh( world
.meta
, pnode
, 0 );
169 vg_acquire_thread_sync();
171 mdl_unpack_submesh( world
.meta
, &world
.mesh_water
, sm
);
172 world
.water
.enabled
= 1;
173 water_set_surface( pnode
->co
[1] );
175 vg_release_thread_sync();
179 vg_warn( "Water entity has no submeshes!\n" );
183 VG_STATIC
void world_pct_audio( mdl_node
*pnode
)
185 struct world_audio_thing
*thing
= &world
.audio_things
[
186 world
.audio_things_count
];
188 memset( thing
, 0, sizeof(struct world_audio_thing
) );
189 struct classtype_audio
*aud
= mdl_get_entdata( world
.meta
, pnode
);
191 v3_copy( pnode
->co
, thing
->pos
);
193 if( aud
->flags
& AUDIO_FLAG_SPACIAL_3D
)
194 thing
->volume
= aud
->volume
* pnode
->s
[0];
196 thing
->volume
= aud
->volume
;
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
;
202 audio_clip_load( &thing
->temp_embedded_clip
);
203 thing
->player
.name
= mdl_pstr( world
.meta
, pnode
->pstr_name
);
204 thing
->player
.enqued
= 0;
206 pnode
->sub_uid
= world
.audio_things_count
;
207 world
.audio_things_count
++;
210 VG_STATIC
void world_entities_process(void)
212 struct entity_instruction
215 void (*process
)( mdl_node
*pnode
);
217 entity_instructions
[] =
219 { k_classtype_spawn
, world_pct_spawn
},
220 { k_classtype_water
, world_pct_water
},
221 { k_classtype_audio
, world_pct_audio
},
224 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
226 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
228 for( int j
=0; j
<vg_list_size(entity_instructions
); j
++ )
230 struct entity_instruction
*instr
= &entity_instructions
[j
];
232 if( pnode
->classtype
== instr
->ct
)
234 instr
->process( pnode
);
240 else if( pnode
->classtype
== k_classtype_achievement_box
)
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
) );
248 struct achievement_zone
*zone
= &world
.achievement_zones
[
249 world
.achievement_zones_count
++ ];
252 struct classtype_achievement_box
*box
= mdl_get_entdata(mworld
,pnode
);
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;
261 zone
->ptarget_delegated
= mdl_node_from_id( mworld
, box
->trigger
);
263 zone
->ptarget_delegated
= NULL
;
270 for( int i
=0; i
<world
.achievement_zones_count
; i
++ )
272 struct achievement_zone
*ach
= &world
.achievement_zones
[ i
];
273 if( ach
->ptarget_delegated
)
275 u32 id
= ach
->ptarget_delegated
->sub_uid
;
276 ach
->ptarget
= &world
.audio_things
[ id
];
284 VG_STATIC
void world_generate(void)
287 * Compile meshes into the world scenes
289 world
.scene_geo
= scene_init( world
.dynamic_vgl
, 350000, 1200000 );
292 * TODO: System to dynamically allocate these
296 mat_vertex_blend
= 0,
302 for( int i
=1; i
<world
.meta
->info
.material_count
; i
++ )
304 mdl_material
*mat
= &world
.meta
->material_buffer
[ i
];
305 const char *mat_name
= mdl_pstr( world
.meta
, mat
->pstr_name
);
307 if( !strcmp( "surf", mat_name
))
309 else if( !strcmp( "surf_oob", mat_name
))
311 else if( !strcmp( "vertex_blend", mat_name
))
312 mat_vertex_blend
= i
;
313 else if( !strcmp( "alphatest", mat_name
))
315 else if( !strcmp( "graffitibox", mat_name
))
317 else if( !strcmp( "terrain", mat_name
) )
322 m4x3_identity( midentity
);
325 * Generate scene: collidable geometry
326 * ----------------------------------------------------------------
329 vg_info( "Generating collidable geometry\n" );
330 vg_info( "terrain...\n" );
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
);
338 vg_info( "oob...\n" );
340 world_add_all_if_material( midentity
, world
.scene_geo
,
341 world
.meta
, mat_surf_oob
);
343 vg_warn( "No OOB surface\n" );
344 scene_copy_slice( world
.scene_geo
, &world
.sm_geo_std_oob
);
348 vg_info( "surface...\n" );
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
);
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
);
361 /* compress that bad boy */
362 world
.scene_geo
= scene_fix( world
.dynamic_vgl
, world
.scene_geo
);
364 vg_acquire_thread_sync();
366 scene_upload( world
.scene_geo
, &world
.mesh_geo
);
368 vg_release_thread_sync();
370 /* setup spacial mapping and rigidbody */
371 world
.geo_bh
= scene_bh_create( world
.dynamic_vgl
, world
.scene_geo
);
373 v3_zero( world
.rb_geo
.co
);
374 q_identity( world
.rb_geo
.q
);
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
);
382 * Generate scene: non-collidable geometry
383 * ----------------------------------------------------------------
385 vg_info( "Generating non-collidable geometry\n" );
387 world
.scene_no_collide
= scene_init( world
.dynamic_vgl
, 200000, 500000 );
389 vg_info( "Applying foliage\n" );
391 world_apply_procedural_foliage();
392 scene_copy_slice( world
.scene_no_collide
, &world
.sm_foliage_main
);
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
);
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
);
405 /* upload and free that */
406 vg_acquire_thread_sync();
408 scene_upload( world
.scene_no_collide
, &world
.mesh_no_collide
);
410 vg_release_thread_sync();
412 vg_linear_del( world
.dynamic_vgl
, world
.scene_no_collide
);
413 world
.scene_no_collide
= NULL
;
416 VG_STATIC
void world_post_process(void)
418 /* initialize audio if need be */
420 for( int i
=0; i
<world
.audio_things_count
; i
++ )
422 struct world_audio_thing
*thingy
= &world
.audio_things
[ i
];
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
);
429 if( thingy
->flags
& AUDIO_FLAG_SPACIAL_3D
)
430 audio_player_set_position( &thingy
->player
, thingy
->pos
);
432 if( thingy
->flags
& AUDIO_FLAG_AUTO_START
)
433 audio_player_playclip( &thingy
->player
, &thingy
->temp_embedded_clip
);
437 vg_acquire_thread_sync();
440 * Rendering the depth map
446 v3_sub( world
.scene_geo
->bbx
[1], world
.scene_geo
->bbx
[0], extent
);
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],
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
;
461 m4x3_identity( camera
);
463 glDisable(GL_DEPTH_TEST
);
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
} );
474 glBlendFunc(GL_ONE
, GL_ONE
);
475 glBlendEquation(GL_MAX
);
476 render_world_depth( ortho
, camera
);
478 glEnable(GL_DEPTH_TEST
);
479 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
483 * TODO: World settings entity
485 struct ub_world_lighting
*winfo
= &gpipeline
.ub_world_lighting
;
487 if( world
.water
.enabled
)
488 v4_copy( world
.water
.plane
, winfo
->g_water_plane
);
491 v3f
*bounds
= world
.scene_geo
->bbx
;
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
);
499 winfo
->g_water_fog
= 0.04f
;
500 render_update_lighting_ub();
503 vg_release_thread_sync();
506 * Setup scene collider
511 VG_STATIC
void world_unload(void)
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
);
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
);
528 for( int i
=0; i
<vg_list_size(world
.ui_bars
); i
++ )
530 struct route_ui_bar
*uib
= &world
.ui_bars
[i
];
531 uib
->segment_start
= 0;
532 uib
->segment_count
= 0;
535 uib
->fade_timer_start
= 0.0;
539 /* delete the entire block of memory */
540 vg_linear_clear( world
.dynamic_vgl
);
542 /* clean dangling pointers */
545 world
.scene_geo
= NULL
;
546 world
.scene_lines
= NULL
;
547 world
.scene_no_collide
= NULL
;
550 world
.trigger_bh
= NULL
;
551 world
.audio_bh
= NULL
;
554 world
.spawn_count
= 0;
556 world
.audio_things
= NULL
;
557 world
.audio_things_count
= 0;
559 world
.logic_entities
= NULL
;
560 world
.logic_entity_count
= 0;
562 world
.logic_actions
= NULL
;
563 world
.logic_action_count
= 0;
565 world
.triggers
= NULL
;
566 world
.trigger_count
= 0;
569 world
.node_count
= 0;
572 world
.route_count
= 0;
575 world
.gate_count
= 0;
577 world
.collectors
= NULL
;
578 world
.collector_count
= 0;
580 world
.water
.enabled
= 0;
583 VG_STATIC
void world_load(void)
587 world
.meta
= mdl_load_full( world
.dynamic_vgl
, world
.world_name
);
588 vg_info( "Loading world: %s\n", world
.world_name
);
590 /* dynamic allocations */
591 world_ents_allocate();
592 world_routes_allocate();
594 /* meta processing */
595 world_routes_process();
596 world_entities_process();
600 world_routes_generate();
601 world_post_process();
604 #endif /* WORLD_GEN_H */