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
)
120 k_classtype_logic_relay
,
121 (void*)&world
.logic_relays
,
122 sizeof(struct logic_relay
)
125 k_classtype_logic_achievement
,
126 (void*)&world
.logic_achievements
,
127 sizeof(struct logic_achievement
)
131 for( int i
=0; i
<vg_list_size(entity_counts
); i
++ )
132 entity_counts
[i
].count
= 0;
134 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
136 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
138 for( int j
=0; j
<vg_list_size(entity_counts
); j
++ )
140 if( pnode
->classtype
== entity_counts
[j
].ct
)
142 pnode
->sub_uid
= entity_counts
[j
].count
;
143 entity_counts
[j
].count
++;
149 for( int i
=0; i
<vg_list_size(entity_counts
); i
++ )
151 struct countable
*counter
= &entity_counts
[i
];
153 u32 bufsize
= counter
->item_size
*counter
->count
;
154 *counter
->to_allocate
= vg_linear_alloc( world
.dynamic_vgl
, bufsize
);
158 VG_STATIC
void world_pct_spawn( mdl_node
*pnode
)
160 struct respawn_point
*rp
= &world
.spawns
[ world
.spawn_count
++ ];
162 v3_copy( pnode
->co
, rp
->co
);
163 v4_copy( pnode
->q
, rp
->q
);
164 rp
->name
= mdl_pstr( world
.meta
, pnode
->pstr_name
);
167 VG_STATIC
void world_pct_water( mdl_node
*pnode
)
169 if( world
.water
.enabled
)
171 vg_warn( "Multiple water surfaces in level! ('%s')\n",
172 mdl_pstr( world
.meta
, pnode
->pstr_name
));
176 mdl_submesh
*sm
= mdl_node_submesh( world
.meta
, pnode
, 0 );
180 vg_acquire_thread_sync();
182 mdl_unpack_submesh( world
.meta
, &world
.mesh_water
, sm
);
183 world
.water
.enabled
= 1;
184 water_set_surface( pnode
->co
[1] );
186 vg_release_thread_sync();
190 vg_warn( "Water entity has no submeshes!\n" );
194 VG_STATIC
void world_pct_audio( mdl_node
*pnode
)
196 struct world_audio_thing
*thing
= &world
.audio_things
[
197 world
.audio_things_count
];
199 memset( thing
, 0, sizeof(struct world_audio_thing
) );
200 struct classtype_audio
*aud
= mdl_get_entdata( world
.meta
, pnode
);
202 v3_copy( pnode
->co
, thing
->pos
);
204 if( aud
->flags
& AUDIO_FLAG_SPACIAL_3D
)
205 thing
->volume
= aud
->volume
* pnode
->s
[0];
207 thing
->volume
= aud
->volume
;
209 thing
->flags
= aud
->flags
;
210 thing
->temp_embedded_clip
.path
= mdl_pstr( world
.meta
, aud
->pstr_file
);
211 thing
->temp_embedded_clip
.source_mode
= k_audio_source_mono
;
213 audio_clip_load( &thing
->temp_embedded_clip
, world
.audio_vgl
);
214 thing
->player
.name
= mdl_pstr( world
.meta
, pnode
->pstr_name
);
215 thing
->player
.enqued
= 0;
217 pnode
->sub_uid
= world
.audio_things_count
;
218 world
.audio_things_count
++;
222 VG_STATIC
void world_pct_trigger( mdl_node
*pnode
)
224 struct trigger_zone
*trigger
= &world
.triggers
[ world
.trigger_count
];
225 struct classtype_trigger
*inf
= mdl_get_entdata( world
.meta
, pnode
);
229 mdl_node
*target_node
= mdl_node_from_id( world
.meta
, inf
->target
);
231 trigger
->target
.sub_id
= target_node
->sub_uid
;
232 trigger
->target
.classtype
= target_node
->classtype
;
236 vg_warn( "Trigger with no target...\n" );
240 mdl_node_transform( pnode
, trigger
->transform
);
241 m4x3_invert_full( trigger
->transform
, trigger
->inv_transform
);
243 world
.trigger_count
++;
247 VG_STATIC
void world_pct_relay( mdl_node
*pnode
)
249 struct logic_relay
*relay
= &world
.logic_relays
[ world
.relay_count
];
250 struct classtype_logic_relay
*inf
= mdl_get_entdata( world
.meta
, pnode
);
252 relay
->target_count
= 0;
254 for( int i
=0; i
<vg_list_size(relay
->targets
); i
++ )
256 if( inf
->targets
[i
] )
258 struct relay_target
*target
= &relay
->targets
[relay
->target_count
++];
259 mdl_node
*other
= mdl_node_from_id( world
.meta
, inf
->targets
[i
] );
261 target
->classtype
= other
->classtype
;
262 target
->sub_id
= other
->sub_uid
;
266 v3_copy( pnode
->co
, relay
->pos
);
267 world
.relay_count
++;
271 VG_STATIC
void world_pct_achievement( mdl_node
*pnode
)
273 struct logic_achievement
*ach
=
274 &world
.logic_achievements
[ world
.achievement_count
];
275 struct classtype_logic_achievement
*inf
=
276 mdl_get_entdata( world
.meta
, pnode
);
278 v3_copy( pnode
->co
, ach
->pos
);
279 ach
->achievement_id
= mdl_pstr( world
.meta
, inf
->pstr_name
);
282 world
.achievement_count
++;
286 VG_STATIC
void world_entities_process(void)
288 struct entity_instruction
291 void (*process
)( mdl_node
*pnode
);
293 entity_instructions
[] =
295 { k_classtype_spawn
, world_pct_spawn
},
296 { k_classtype_water
, world_pct_water
},
297 { k_classtype_audio
, world_pct_audio
},
298 { k_classtype_trigger
, world_pct_trigger
},
299 { k_classtype_logic_relay
, world_pct_relay
},
300 { k_classtype_logic_achievement
, world_pct_achievement
}
303 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
305 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
307 for( int j
=0; j
<vg_list_size(entity_instructions
); j
++ )
309 struct entity_instruction
*instr
= &entity_instructions
[j
];
311 if( pnode
->classtype
== instr
->ct
)
313 instr
->process( pnode
);
320 VG_STATIC
void world_generate(void)
323 * Compile meshes into the world scenes
325 world
.scene_geo
= scene_init( world
.dynamic_vgl
, 350000, 1200000 );
328 m4x3_identity( midentity
);
331 * Generate scene: collidable geometry
332 * ----------------------------------------------------------------
335 vg_info( "Generating collidable geometry\n" );
338 for( int i
=0; i
<world
.material_count
; i
++ )
340 struct world_material
*mat
= &world
.materials
[ i
];
342 if( mat
->info
.flags
& k_material_flag_collision
)
344 world_add_all_if_material( midentity
, world
.scene_geo
, world
.meta
, i
);
345 scene_copy_slice( world
.scene_geo
, &mat
->sm_geo
);
354 /* compress that bad boy */
355 world
.scene_geo
= scene_fix( world
.dynamic_vgl
, world
.scene_geo
);
357 vg_acquire_thread_sync();
359 scene_upload( world
.scene_geo
, &world
.mesh_geo
);
361 vg_release_thread_sync();
363 /* setup spacial mapping and rigidbody */
364 world
.geo_bh
= scene_bh_create( world
.dynamic_vgl
, world
.scene_geo
);
366 v3_zero( world
.rb_geo
.co
);
367 q_identity( world
.rb_geo
.q
);
369 world
.rb_geo
.type
= k_rb_shape_scene
;
370 world
.rb_geo
.inf
.scene
.bh_scene
= world
.geo_bh
;
371 world
.rb_geo
.is_world
= 1;
372 rb_init( &world
.rb_geo
);
375 * Generate scene: non-collidable geometry
376 * ----------------------------------------------------------------
378 vg_info( "Generating non-collidable geometry\n" );
380 world
.scene_no_collide
= scene_init( world
.dynamic_vgl
, 200000, 500000 );
383 vg_info( "Applying foliage\n" );
385 world_apply_procedural_foliage();
386 scene_copy_slice( world
.scene_no_collide
, &world
.sm_foliage_main
);
391 /* upload and free that */
392 vg_acquire_thread_sync();
394 scene_upload( world
.scene_no_collide
, &world
.mesh_no_collide
);
396 vg_release_thread_sync();
398 vg_linear_del( world
.dynamic_vgl
, world
.scene_no_collide
);
399 world
.scene_no_collide
= NULL
;
402 VG_STATIC
int reset_player( int argc
, char const *argv
[] );
403 VG_STATIC
void world_post_process(void)
405 /* initialize audio if need be */
407 for( int i
=0; i
<world
.audio_things_count
; i
++ )
409 struct world_audio_thing
*thingy
= &world
.audio_things
[ i
];
411 audio_player_init( &thingy
->player
);
412 audio_player_set_flags( &thingy
->player
, thingy
->flags
);
413 audio_player_set_vol( &thingy
->player
, thingy
->volume
);
414 audio_player_set_pan( &thingy
->player
, 0.0f
);
416 if( thingy
->flags
& AUDIO_FLAG_SPACIAL_3D
)
417 audio_player_set_position( &thingy
->player
, thingy
->pos
);
419 if( thingy
->flags
& AUDIO_FLAG_AUTO_START
)
420 audio_player_playclip( &thingy
->player
, &thingy
->temp_embedded_clip
);
424 vg_acquire_thread_sync();
427 * Rendering the depth map
433 v3_sub( world
.scene_geo
->bbx
[1], world
.scene_geo
->bbx
[0], extent
);
435 float fl
= world
.scene_geo
->bbx
[0][0],
436 fr
= world
.scene_geo
->bbx
[1][0],
437 fb
= world
.scene_geo
->bbx
[0][2],
438 ft
= world
.scene_geo
->bbx
[1][2],
443 ortho
[0][0] = 2.0f
* rl
;
444 ortho
[2][1] = 2.0f
* tb
;
445 ortho
[3][0] = (fr
+ fl
) * -rl
;
446 ortho
[3][1] = (ft
+ fb
) * -tb
;
448 m4x3_identity( camera
);
450 glDisable(GL_DEPTH_TEST
);
452 glDisable(GL_CULL_FACE
);
453 glBindFramebuffer( GL_FRAMEBUFFER
, gpipeline
.fb_depthmap
);
454 glViewport( 0, 0, 1024, 1024 );
455 shader_fscolour_use();
456 shader_fscolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
461 glBlendFunc(GL_ONE
, GL_ONE
);
462 glBlendEquation(GL_MAX
);
463 render_world_depth( ortho
, camera
);
465 glEnable(GL_DEPTH_TEST
);
466 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
470 * TODO: World settings entity
472 struct ub_world_lighting
*winfo
= &gpipeline
.ub_world_lighting
;
474 if( world
.water
.enabled
)
475 v4_copy( world
.water
.plane
, winfo
->g_water_plane
);
478 v3f
*bounds
= world
.scene_geo
->bbx
;
480 info_vec
[0] = bounds
[0][0];
481 info_vec
[1] = bounds
[0][2];
482 info_vec
[2] = 1.0f
/ (bounds
[1][0]-bounds
[0][0]);
483 info_vec
[3] = 1.0f
/ (bounds
[1][2]-bounds
[0][2]);
484 v4_copy( info_vec
, winfo
->g_depth_bounds
);
486 winfo
->g_water_fog
= 0.04f
;
487 render_update_lighting_ub();
490 vg_release_thread_sync();
493 * Setup scene collider
496 reset_player( 1, (const char *[]){"start"} );
499 VG_STATIC
void world_process_resources(void)
501 vg_info( "Loading textures\n" );
502 world
.texture_count
= world
.meta
->info
.texture_count
;
504 vg_linear_alloc( world
.dynamic_vgl
, sizeof(GLuint
)*world
.texture_count
);
506 vg_acquire_thread_sync();
509 world
.textures
[0] = vg_tex2d_new();
510 vg_tex2d_set_error();
514 for( int i
=1; i
<world
.texture_count
; i
++ )
516 mdl_texture
*tex
= &world
.meta
->texture_buffer
[i
];
518 if( !tex
->pack_offset
)
520 vg_release_thread_sync();
521 vg_fatal_exit_loop( "World models must have packed textures!" );
524 vg_linear_clear( vg_mem
.scratch
);
525 world
.textures
[i
] = vg_tex2d_new();
526 vg_tex2d_set_error();
527 vg_tex2d_qoi( world
.meta
->pack
+ tex
->pack_offset
, tex
->pack_length
,
528 mdl_pstr( world
.meta
, tex
->pstr_name
));
533 vg_release_thread_sync();
535 vg_info( "Loading materials\n" );
537 u32 size
= sizeof(struct world_material
) * world
.meta
->info
.material_count
;
538 world
.materials
= vg_linear_alloc( world
.dynamic_vgl
, size
);
540 world
.material_count
= world
.meta
->info
.material_count
;
541 memset( world
.materials
, 0, size
);
543 for( int i
=1; i
<world
.material_count
; i
++ )
544 world
.materials
[i
].info
= world
.meta
->material_buffer
[i
];
547 struct world_material
*errmat
= &world
.materials
[0];
548 v4_copy( (v4f
){ 1.0f
,0.0f
,0.0f
,1.0f
}, errmat
->info
.colour
);
549 v4_copy( (v4f
){ 1.0f
,0.0f
,0.0f
,1.0f
}, errmat
->info
.colour1
);
550 errmat
->info
.flags
= 0x00;
551 errmat
->info
.pstr_name
= 0; /* useless? */
552 errmat
->info
.shader
= -1;
553 errmat
->info
.tex_decal
= 0;
554 errmat
->info
.tex_diffuse
= 0;
555 errmat
->info
.tex_normal
= 0;
558 VG_STATIC
void world_unload(void)
561 mesh_free( &world
.mesh_route_lines
);
562 mesh_free( &world
.mesh_geo
);
563 mesh_free( &world
.mesh_no_collide
);
564 mesh_free( &world
.mesh_water
);
567 world
.rewind_from
= 0.0;
568 world
.rewind_to
= 0.0;
569 world
.last_use
= 0.0;
570 world
.active_gate
= 0;
571 world
.current_run_version
= 2;
572 world
.active_route_board
= 0;
573 v3_zero( world
.render_gate_pos
);
575 for( int i
=0; i
<vg_list_size(world
.ui_bars
); i
++ )
577 struct route_ui_bar
*uib
= &world
.ui_bars
[i
];
578 uib
->segment_start
= 0;
579 uib
->segment_count
= 0;
582 uib
->fade_timer_start
= 0.0;
586 /* delete textures and meshes */
587 glDeleteTextures( world
.texture_count
, world
.textures
);
589 /* delete the entire block of memory */
590 vg_linear_clear( world
.dynamic_vgl
);
591 vg_linear_clear( world
.audio_vgl
);
593 /* clean dangling pointers */
596 world
.textures
= NULL
;
597 world
.texture_count
= 0;
598 world
.materials
= NULL
;
599 world
.material_count
= 0;
601 world
.scene_geo
= NULL
;
602 world
.scene_no_collide
= NULL
;
603 world
.scene_lines
= NULL
;
606 world
.trigger_bh
= NULL
;
607 world
.audio_bh
= NULL
;
610 world
.spawn_count
= 0;
612 world
.audio_things
= NULL
;
613 world
.audio_things_count
= 0;
615 world
.triggers
= NULL
;
616 world
.trigger_count
= 0;
618 world
.logic_relays
= NULL
;
619 world
.relay_count
= 0;
621 world
.logic_achievements
= NULL
;
622 world
.achievement_count
= 0;
625 world
.node_count
= 0;
628 world
.route_count
= 0;
631 world
.gate_count
= 0;
633 world
.collectors
= NULL
;
634 world
.collector_count
= 0;
636 world
.water
.enabled
= 0;
639 VG_STATIC
void world_load(void)
643 world
.meta
= mdl_load_full( world
.dynamic_vgl
, world
.world_name
);
644 vg_info( "Loading world: %s\n", world
.world_name
);
646 /* process resources from pack */
647 world_process_resources();
649 /* dynamic allocations */
650 world_ents_allocate();
651 world_routes_allocate();
653 /* meta processing */
654 world_routes_process();
655 world_entities_process();
659 world_routes_generate();
660 world_post_process();
663 #endif /* WORLD_GEN_H */