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( struct world_material
*mat
)
39 if( vg
.quality_profile
== k_quality_profile_low
)
42 vg_info( "Applying foliage (%u)\n", mat
->info
.pstr_name
);
44 vg_linear_clear( vg_mem
.scratch
);
46 mdl_context
*mfoliage
=
47 mdl_load_full( vg_mem
.scratch
, "models/rs_foliage.mdl");
50 v3_sub( world
.scene_geo
->bbx
[1], world
.scene_geo
->bbx
[0], volume
);
54 mdl_node
*mblob
= mdl_node_from_name( mfoliage
, "blob" );
55 mdl_submesh
*sm_blob
= mdl_node_submesh( mfoliage
, mblob
, 0 );
59 for( int i
=0;i
<100000;i
++ )
62 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
64 v3_add( pos
, world
.scene_geo
->bbx
[0], pos
);
69 if( ray_world( pos
, (v3f
){0.0001f
,-1.0f
,0.0001f
}, &hit
))
71 struct world_material
*m1
= ray_hit_material( &hit
);
72 if((hit
.normal
[1] > 0.8f
) && (m1
== mat
) && (hit
.pos
[1] > 0.0f
+10.0f
))
74 v4f qsurface
, qrandom
;
77 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
.normal
, axis
);
79 float angle
= v3_dot(hit
.normal
,(v3f
){0.0f
,1.0f
,0.0f
});
80 q_axis_angle( qsurface
, axis
, angle
);
81 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
82 q_mul( qsurface
, qrandom
, qsurface
);
83 q_m3x3( qsurface
, transform
);
85 v3_copy( hit
.pos
, transform
[3] );
86 scene_add_submesh( world
.scene_no_collide
, mfoliage
,
94 vg_info( "%d foliage models added\n", count
);
97 VG_STATIC
void world_ents_allocate(void)
99 vg_info( "Allocating entities\n" );
101 /* count entites to allocate buffers for them.
102 * maybe in the future we just store these numbers in the model file...
104 * TODO: use this in world_routes too */
117 (void*)&world
.spawns
,
118 sizeof(struct respawn_point
)
122 (void*)&world
.audio_things
,
123 sizeof(struct world_audio_thing
)
127 (void*)&world
.triggers
,
128 sizeof(struct trigger_zone
)
131 k_classtype_logic_relay
,
132 (void*)&world
.logic_relays
,
133 sizeof(struct logic_relay
)
136 k_classtype_logic_achievement
,
137 (void*)&world
.logic_achievements
,
138 sizeof(struct logic_achievement
)
142 for( int i
=0; i
<vg_list_size(entity_counts
); i
++ )
143 entity_counts
[i
].count
= 0;
145 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
147 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
149 for( int j
=0; j
<vg_list_size(entity_counts
); j
++ )
151 if( pnode
->classtype
== entity_counts
[j
].ct
)
153 pnode
->sub_uid
= entity_counts
[j
].count
;
154 entity_counts
[j
].count
++;
160 for( int i
=0; i
<vg_list_size(entity_counts
); i
++ )
162 struct countable
*counter
= &entity_counts
[i
];
164 u32 bufsize
= counter
->item_size
*counter
->count
;
165 *counter
->to_allocate
= vg_linear_alloc( world
.dynamic_vgl
, bufsize
);
169 VG_STATIC
void world_pct_spawn( mdl_node
*pnode
)
171 struct respawn_point
*rp
= &world
.spawns
[ world
.spawn_count
++ ];
173 v3_copy( pnode
->co
, rp
->co
);
174 v4_copy( pnode
->q
, rp
->q
);
175 rp
->name
= mdl_pstr( world
.meta
, pnode
->pstr_name
);
178 VG_STATIC
void world_pct_water( mdl_node
*pnode
)
180 if( world
.water
.enabled
)
182 vg_warn( "Multiple water surfaces in level! ('%s')\n",
183 mdl_pstr( world
.meta
, pnode
->pstr_name
));
187 world
.water
.enabled
= 1;
188 water_set_surface( pnode
->co
[1] );
191 VG_STATIC
void world_pct_audio( mdl_node
*pnode
)
193 struct world_audio_thing
*thing
= &world
.audio_things
[
194 world
.audio_things_count
];
196 memset( thing
, 0, sizeof(struct world_audio_thing
) );
197 struct classtype_audio
*aud
= mdl_get_entdata( world
.meta
, pnode
);
199 v3_copy( pnode
->co
, thing
->pos
);
201 if( aud
->flags
& AUDIO_FLAG_SPACIAL_3D
)
202 thing
->volume
= aud
->volume
* pnode
->s
[0];
204 thing
->volume
= aud
->volume
;
206 thing
->flags
= aud
->flags
;
207 thing
->temp_embedded_clip
.path
= mdl_pstr( world
.meta
, aud
->pstr_file
);
208 thing
->temp_embedded_clip
.source_mode
= k_audio_source_mono
;
210 audio_clip_load( &thing
->temp_embedded_clip
, world
.audio_vgl
);
211 thing
->player
.name
= mdl_pstr( world
.meta
, pnode
->pstr_name
);
212 thing
->player
.enqued
= 0;
214 pnode
->sub_uid
= world
.audio_things_count
;
215 world
.audio_things_count
++;
219 VG_STATIC
void world_pct_trigger( mdl_node
*pnode
)
221 struct trigger_zone
*trigger
= &world
.triggers
[ world
.trigger_count
];
222 struct classtype_trigger
*inf
= mdl_get_entdata( world
.meta
, pnode
);
226 mdl_node
*target_node
= mdl_node_from_id( world
.meta
, inf
->target
);
228 trigger
->target
.sub_id
= target_node
->sub_uid
;
229 trigger
->target
.classtype
= target_node
->classtype
;
233 vg_warn( "Trigger with no target...\n" );
237 mdl_node_transform( pnode
, trigger
->transform
);
238 m4x3_invert_full( trigger
->transform
, trigger
->inv_transform
);
240 world
.trigger_count
++;
244 VG_STATIC
void world_pct_relay( mdl_node
*pnode
)
246 struct logic_relay
*relay
= &world
.logic_relays
[ world
.relay_count
];
247 struct classtype_logic_relay
*inf
= mdl_get_entdata( world
.meta
, pnode
);
249 relay
->target_count
= 0;
251 for( int i
=0; i
<vg_list_size(relay
->targets
); i
++ )
253 if( inf
->targets
[i
] )
255 struct relay_target
*target
= &relay
->targets
[relay
->target_count
++];
256 mdl_node
*other
= mdl_node_from_id( world
.meta
, inf
->targets
[i
] );
258 target
->classtype
= other
->classtype
;
259 target
->sub_id
= other
->sub_uid
;
263 v3_copy( pnode
->co
, relay
->pos
);
264 world
.relay_count
++;
268 VG_STATIC
void world_pct_achievement( mdl_node
*pnode
)
270 struct logic_achievement
*ach
=
271 &world
.logic_achievements
[ world
.achievement_count
];
272 struct classtype_logic_achievement
*inf
=
273 mdl_get_entdata( world
.meta
, pnode
);
275 v3_copy( pnode
->co
, ach
->pos
);
276 ach
->achievement_id
= mdl_pstr( world
.meta
, inf
->pstr_name
);
279 world
.achievement_count
++;
283 VG_STATIC
void world_entities_process(void)
285 struct entity_instruction
288 void (*process
)( mdl_node
*pnode
);
290 entity_instructions
[] =
292 { k_classtype_spawn
, world_pct_spawn
},
293 { k_classtype_water
, world_pct_water
},
294 { k_classtype_audio
, world_pct_audio
},
295 { k_classtype_trigger
, world_pct_trigger
},
296 { k_classtype_logic_relay
, world_pct_relay
},
297 { k_classtype_logic_achievement
, world_pct_achievement
}
300 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
302 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
304 for( int j
=0; j
<vg_list_size(entity_instructions
); j
++ )
306 struct entity_instruction
*instr
= &entity_instructions
[j
];
308 if( pnode
->classtype
== instr
->ct
)
310 instr
->process( pnode
);
317 VG_STATIC
void edge_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
319 struct grind_edge
*edge_array
= user
,
320 *edge
= &edge_array
[ item_index
];
322 box_addpt( bound
, edge
->p0
);
323 box_addpt( bound
, edge
->p1
);
326 VG_STATIC
float edge_bh_centroid( void *user
, u32 item_index
, int axis
)
328 struct grind_edge
*edge_array
= user
,
329 *edge
= &edge_array
[ item_index
];
331 return (edge
->p0
[axis
] + edge
->p1
[axis
]) * 0.5f
;
334 VG_STATIC
void edge_bh_swap( void *user
, u32 ia
, u32 ib
)
336 struct grind_edge
*edge_array
= user
,
337 *e0
= &edge_array
[ ia
],
338 *e1
= &edge_array
[ ib
],
345 VG_STATIC
void edge_bh_closest( void *user
, u32 index
, v3f point
, v3f closest
)
347 struct grind_edge
*edge_array
= user
,
348 *edge
= &edge_array
[ index
];
350 closest_point_segment( edge
->p0
, edge
->p1
, point
, closest
);
353 VG_STATIC bh_system bh_system_edges
=
355 .expand_bound
= edge_bh_expand_bound
,
356 .item_centroid
= edge_bh_centroid
,
357 .item_closest
= edge_bh_closest
,
358 .item_swap
= edge_bh_swap
,
363 VG_STATIC
void world_generate_edges(void)
365 vg_info( "Generating edge array\n" );
366 world
.grind_edges
= vg_linear_alloc( world
.dynamic_vgl
,
367 5000*sizeof(struct grind_edge
) );
368 world
.grind_edge_count
= 0;
371 for( u32 i
=0; i
<world
.scene_geo
->vertex_count
; i
++ )
372 if( world
.scene_geo
->arrvertices
[i
].weights
[0] )
375 vg_info( "Grind verts: %u\n", fs_count
);
377 for( u32 i
=0; i
<world
.scene_geo
->indice_count
/3; i
++ )
379 u32
*ptri
= &world
.scene_geo
->arrindices
[ i
*3 ];
381 for( int j
=0; j
<3; j
++ )
386 mdl_vert
*v0
= &world
.scene_geo
->arrvertices
[ i0
],
387 *v1
= &world
.scene_geo
->arrvertices
[ i1
];
391 if( world
.grind_edge_count
== 5000 )
392 vg_fatal_exit_loop( "Edge capacity exceeded" );
394 struct grind_edge
*ge
=
395 &world
.grind_edges
[ world
.grind_edge_count
++ ];
397 v3_copy( v0
->co
, ge
->p0
);
398 v3_copy( v1
->co
, ge
->p1
);
403 vg_info( "Grind edge count: %u\n", world
.grind_edge_count
);
405 world
.grind_edges
= vg_linear_resize( world
.dynamic_vgl
, world
.grind_edges
,
406 world
.grind_edge_count
*sizeof(struct grind_edge
) );
408 world
.grind_bh
= bh_create( world
.dynamic_vgl
, &bh_system_edges
,
409 world
.grind_edges
, world
.grind_edge_count
,
413 VG_STATIC
void world_generate(void)
416 * Compile meshes into the world scenes
418 world
.scene_geo
= scene_init( world
.dynamic_vgl
, 320000, 1200000 );
421 m4x3_identity( midentity
);
424 * Generate scene: collidable geometry
425 * ----------------------------------------------------------------
428 vg_info( "Generating collidable geometry\n" );
431 for( int i
=0; i
<world
.material_count
; i
++ )
433 struct world_material
*mat
= &world
.materials
[ i
];
435 if( mat
->info
.flags
& k_material_flag_collision
)
436 world_add_all_if_material( midentity
, world
.scene_geo
, world
.meta
, i
);
438 scene_copy_slice( world
.scene_geo
, &mat
->sm_geo
);
444 /* compress that bad boy */
445 world
.scene_geo
= scene_fix( world
.dynamic_vgl
, world
.scene_geo
);
447 vg_acquire_thread_sync();
449 scene_upload( world
.scene_geo
, &world
.mesh_geo
);
451 vg_release_thread_sync();
453 /* setup spacial mapping and rigidbody */
454 world
.geo_bh
= scene_bh_create( world
.dynamic_vgl
, world
.scene_geo
);
456 v3_zero( world
.rb_geo
.co
);
457 q_identity( world
.rb_geo
.q
);
459 world
.rb_geo
.type
= k_rb_shape_scene
;
460 world
.rb_geo
.inf
.scene
.bh_scene
= world
.geo_bh
;
461 world
.rb_geo
.is_world
= 1;
462 rb_init( &world
.rb_geo
);
465 * Generate scene: non-collidable geometry
466 * ----------------------------------------------------------------
468 vg_info( "Generating non-collidable geometry\n" );
470 world
.scene_no_collide
= scene_init( world
.dynamic_vgl
, 200000, 500000 );
472 for( int i
=0; i
<world
.material_count
; i
++ )
474 struct world_material
*mat
= &world
.materials
[ i
];
476 if( !(mat
->info
.flags
& k_material_flag_collision
) )
478 world_add_all_if_material( midentity
, world
.scene_no_collide
,
482 if( mat
->info
.flags
& k_material_flag_grow_grass
)
483 world_apply_procedural_foliage( mat
);
485 scene_copy_slice( world
.scene_no_collide
, &mat
->sm_no_collide
);
488 /* upload and free that */
489 vg_acquire_thread_sync();
491 scene_upload( world
.scene_no_collide
, &world
.mesh_no_collide
);
493 vg_release_thread_sync();
495 vg_linear_del( world
.dynamic_vgl
, world
.scene_no_collide
);
496 world
.scene_no_collide
= NULL
;
498 world_generate_edges();
501 VG_STATIC
int reset_player( int argc
, char const *argv
[] );
502 VG_STATIC
void world_post_process(void)
504 /* initialize audio if need be */
506 for( int i
=0; i
<world
.audio_things_count
; i
++ )
508 struct world_audio_thing
*thingy
= &world
.audio_things
[ i
];
510 audio_player_init( &thingy
->player
);
511 audio_player_set_flags( &thingy
->player
, thingy
->flags
);
512 audio_player_set_vol( &thingy
->player
, thingy
->volume
);
513 audio_player_set_pan( &thingy
->player
, 0.0f
);
515 if( thingy
->flags
& AUDIO_FLAG_SPACIAL_3D
)
516 audio_player_set_position( &thingy
->player
, thingy
->pos
);
518 if( thingy
->flags
& AUDIO_FLAG_AUTO_START
)
519 audio_player_playclip( &thingy
->player
, &thingy
->temp_embedded_clip
);
523 vg_acquire_thread_sync();
526 * Rendering the depth map
531 v3_sub( world
.scene_geo
->bbx
[1], world
.scene_geo
->bbx
[0], extent
);
533 float fl
= world
.scene_geo
->bbx
[0][0],
534 fr
= world
.scene_geo
->bbx
[1][0],
535 fb
= world
.scene_geo
->bbx
[0][2],
536 ft
= world
.scene_geo
->bbx
[1][2],
540 m4x4_zero( ortho
.mtx
.p
);
541 ortho
.mtx
.p
[0][0] = 2.0f
* rl
;
542 ortho
.mtx
.p
[2][1] = 2.0f
* tb
;
543 ortho
.mtx
.p
[3][0] = (fr
+ fl
) * -rl
;
544 ortho
.mtx
.p
[3][1] = (ft
+ fb
) * -tb
;
545 ortho
.mtx
.p
[3][3] = 1.0f
;
546 m4x3_identity( ortho
.transform
);
547 camera_update_view( &ortho
);
548 camera_finalize( &ortho
);
550 glDisable(GL_DEPTH_TEST
);
552 glDisable(GL_CULL_FACE
);
553 render_fb_bind( gpipeline
.fb_heightmap
);
554 shader_blitcolour_use();
555 shader_blitcolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
560 glBlendFunc(GL_ONE
, GL_ONE
);
561 glBlendEquation(GL_MAX
);
563 render_world_depth( &ortho
);
565 glEnable(GL_DEPTH_TEST
);
566 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
569 * TODO: World settings entity
571 struct ub_world_lighting
*winfo
= &gpipeline
.ub_world_lighting
;
573 if( world
.water
.enabled
)
574 v4_copy( world
.water
.plane
, winfo
->g_water_plane
);
577 v3f
*bounds
= world
.scene_geo
->bbx
;
579 info_vec
[0] = bounds
[0][0];
580 info_vec
[1] = bounds
[0][2];
581 info_vec
[2] = 1.0f
/ (bounds
[1][0]-bounds
[0][0]);
582 info_vec
[3] = 1.0f
/ (bounds
[1][2]-bounds
[0][2]);
583 v4_copy( info_vec
, winfo
->g_depth_bounds
);
585 winfo
->g_water_fog
= 0.04f
;
586 render_update_lighting_ub();
589 vg_release_thread_sync();
592 * Setup scene collider
595 reset_player( 1, (const char *[]){"start"} );
598 VG_STATIC
void world_process_resources(void)
600 vg_info( "Loading textures\n" );
601 world
.texture_count
= world
.meta
->info
.texture_count
;
603 vg_linear_alloc( world
.dynamic_vgl
, sizeof(GLuint
)*world
.texture_count
);
605 vg_acquire_thread_sync();
608 world
.textures
[0] = vg_tex2d_new();
609 vg_tex2d_set_error();
613 for( int i
=1; i
<world
.texture_count
; i
++ )
615 mdl_texture
*tex
= &world
.meta
->texture_buffer
[i
];
617 if( !tex
->pack_offset
)
619 vg_release_thread_sync();
620 vg_fatal_exit_loop( "World models must have packed textures!" );
623 vg_linear_clear( vg_mem
.scratch
);
624 world
.textures
[i
] = vg_tex2d_new();
625 vg_tex2d_set_error();
626 vg_tex2d_qoi( world
.meta
->pack
+ tex
->pack_offset
, tex
->pack_length
,
627 mdl_pstr( world
.meta
, tex
->pstr_name
));
632 vg_release_thread_sync();
634 vg_info( "Loading materials\n" );
636 u32 size
= sizeof(struct world_material
) * world
.meta
->info
.material_count
;
637 world
.materials
= vg_linear_alloc( world
.dynamic_vgl
, size
);
639 world
.material_count
= world
.meta
->info
.material_count
;
640 memset( world
.materials
, 0, size
);
642 for( int i
=1; i
<world
.material_count
; i
++ )
643 world
.materials
[i
].info
= world
.meta
->material_buffer
[i
];
646 struct world_material
*errmat
= &world
.materials
[0];
647 v4_copy( (v4f
){ 1.0f
,0.0f
,0.0f
,1.0f
}, errmat
->info
.colour
);
648 v4_copy( (v4f
){ 1.0f
,0.0f
,0.0f
,1.0f
}, errmat
->info
.colour1
);
649 errmat
->info
.flags
= 0x00;
650 errmat
->info
.pstr_name
= 0; /* useless? */
651 errmat
->info
.shader
= -1;
652 errmat
->info
.tex_decal
= 0;
653 errmat
->info
.tex_diffuse
= 0;
654 errmat
->info
.tex_normal
= 0;
657 VG_STATIC
void world_unload(void)
659 vg_acquire_thread_sync();
662 mesh_free( &world
.mesh_route_lines
);
663 mesh_free( &world
.mesh_geo
);
664 mesh_free( &world
.mesh_no_collide
);
667 world
.rewind_from
= 0.0;
668 world
.rewind_to
= 0.0;
669 world
.last_use
= 0.0;
670 world
.active_gate
= 0;
671 world
.current_run_version
= 2;
672 world
.active_route_board
= 0;
673 v3_zero( world
.render_gate_pos
);
675 for( int i
=0; i
<vg_list_size(world
.ui_bars
); i
++ )
677 struct route_ui_bar
*uib
= &world
.ui_bars
[i
];
678 uib
->segment_start
= 0;
679 uib
->segment_count
= 0;
682 uib
->fade_timer_start
= 0.0;
686 /* delete textures and meshes */
687 glDeleteTextures( world
.texture_count
, world
.textures
);
689 /* delete the entire block of memory */
690 vg_linear_clear( world
.dynamic_vgl
);
691 vg_linear_clear( world
.audio_vgl
);
693 /* clean dangling pointers */
696 world
.textures
= NULL
;
697 world
.texture_count
= 0;
698 world
.materials
= NULL
;
699 world
.material_count
= 0;
701 world
.scene_geo
= NULL
;
702 world
.scene_no_collide
= NULL
;
703 world
.scene_lines
= NULL
;
704 world
.grind_edges
= NULL
;
705 world
.grind_edge_count
= 0;
707 world
.grind_bh
= NULL
;
709 world
.trigger_bh
= NULL
;
710 world
.audio_bh
= NULL
;
713 world
.spawn_count
= 0;
715 world
.audio_things
= NULL
;
716 world
.audio_things_count
= 0;
718 world
.triggers
= NULL
;
719 world
.trigger_count
= 0;
721 world
.logic_relays
= NULL
;
722 world
.relay_count
= 0;
724 world
.logic_achievements
= NULL
;
725 world
.achievement_count
= 0;
728 world
.node_count
= 0;
731 world
.route_count
= 0;
734 world
.gate_count
= 0;
736 world
.collectors
= NULL
;
737 world
.collector_count
= 0;
739 world
.water
.enabled
= 0;
741 vg_release_thread_sync();
744 VG_STATIC
void world_load(void)
748 world
.meta
= mdl_load_full( world
.dynamic_vgl
, world
.world_name
);
749 vg_info( "Loading world: %s\n", world
.world_name
);
751 /* process resources from pack */
752 world_process_resources();
754 /* dynamic allocations */
755 world_ents_allocate();
756 world_routes_allocate();
758 /* meta processing */
759 world_routes_process();
760 world_entities_process();
764 world_routes_generate();
765 world_post_process();
768 #endif /* WORLD_GEN_H */