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 );
57 for( int i
=0;i
<100000;i
++ )
60 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
62 v3_add( pos
, world
.scene_geo
->bbx
[0], pos
);
67 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
69 struct world_material
*m1
= ray_hit_material( &hit
);
70 if((hit
.normal
[1] > 0.8f
) && (m1
== mat
) && (hit
.pos
[1] > 0.0f
+10.0f
))
72 v4f qsurface
, qrandom
;
75 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
.normal
, axis
);
77 float angle
= v3_dot(hit
.normal
,(v3f
){0.0f
,1.0f
,0.0f
});
78 q_axis_angle( qsurface
, axis
, angle
);
79 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
80 q_mul( qsurface
, qrandom
, qsurface
);
81 q_m3x3( qsurface
, transform
);
83 v3_copy( hit
.pos
, transform
[3] );
84 scene_add_submesh( world
.scene_no_collide
, mfoliage
,
91 VG_STATIC
void world_ents_allocate(void)
93 vg_info( "Allocating entities\n" );
95 /* count entites to allocate buffers for them.
96 * maybe in the future we just store these numbers in the model file...
98 * TODO: use this in world_routes too */
111 (void*)&world
.spawns
,
112 sizeof(struct respawn_point
)
116 (void*)&world
.audio_things
,
117 sizeof(struct world_audio_thing
)
121 (void*)&world
.triggers
,
122 sizeof(struct trigger_zone
)
125 k_classtype_logic_relay
,
126 (void*)&world
.logic_relays
,
127 sizeof(struct logic_relay
)
130 k_classtype_logic_achievement
,
131 (void*)&world
.logic_achievements
,
132 sizeof(struct logic_achievement
)
136 for( int i
=0; i
<vg_list_size(entity_counts
); i
++ )
137 entity_counts
[i
].count
= 0;
139 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
141 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
143 for( int j
=0; j
<vg_list_size(entity_counts
); j
++ )
145 if( pnode
->classtype
== entity_counts
[j
].ct
)
147 pnode
->sub_uid
= entity_counts
[j
].count
;
148 entity_counts
[j
].count
++;
154 for( int i
=0; i
<vg_list_size(entity_counts
); i
++ )
156 struct countable
*counter
= &entity_counts
[i
];
158 u32 bufsize
= counter
->item_size
*counter
->count
;
159 *counter
->to_allocate
= vg_linear_alloc( world
.dynamic_vgl
, bufsize
);
163 VG_STATIC
void world_pct_spawn( mdl_node
*pnode
)
165 struct respawn_point
*rp
= &world
.spawns
[ world
.spawn_count
++ ];
167 v3_copy( pnode
->co
, rp
->co
);
168 v4_copy( pnode
->q
, rp
->q
);
169 rp
->name
= mdl_pstr( world
.meta
, pnode
->pstr_name
);
172 VG_STATIC
void world_pct_water( mdl_node
*pnode
)
174 if( world
.water
.enabled
)
176 vg_warn( "Multiple water surfaces in level! ('%s')\n",
177 mdl_pstr( world
.meta
, pnode
->pstr_name
));
181 world
.water
.enabled
= 1;
182 water_set_surface( pnode
->co
[1] );
185 VG_STATIC
void world_pct_audio( mdl_node
*pnode
)
187 struct world_audio_thing
*thing
= &world
.audio_things
[
188 world
.audio_things_count
];
190 memset( thing
, 0, sizeof(struct world_audio_thing
) );
191 struct classtype_audio
*aud
= mdl_get_entdata( world
.meta
, pnode
);
193 v3_copy( pnode
->co
, thing
->pos
);
195 if( aud
->flags
& AUDIO_FLAG_SPACIAL_3D
)
196 thing
->volume
= aud
->volume
* pnode
->s
[0];
198 thing
->volume
= aud
->volume
;
200 thing
->flags
= aud
->flags
;
201 thing
->temp_embedded_clip
.path
= mdl_pstr( world
.meta
, aud
->pstr_file
);
202 thing
->temp_embedded_clip
.source_mode
= k_audio_source_mono
;
204 audio_clip_load( &thing
->temp_embedded_clip
, world
.audio_vgl
);
205 thing
->player
.name
= mdl_pstr( world
.meta
, pnode
->pstr_name
);
206 thing
->player
.enqued
= 0;
208 pnode
->sub_uid
= world
.audio_things_count
;
209 world
.audio_things_count
++;
213 VG_STATIC
void world_pct_trigger( mdl_node
*pnode
)
215 struct trigger_zone
*trigger
= &world
.triggers
[ world
.trigger_count
];
216 struct classtype_trigger
*inf
= mdl_get_entdata( world
.meta
, pnode
);
220 mdl_node
*target_node
= mdl_node_from_id( world
.meta
, inf
->target
);
222 trigger
->target
.sub_id
= target_node
->sub_uid
;
223 trigger
->target
.classtype
= target_node
->classtype
;
227 vg_warn( "Trigger with no target...\n" );
231 mdl_node_transform( pnode
, trigger
->transform
);
232 m4x3_invert_full( trigger
->transform
, trigger
->inv_transform
);
234 world
.trigger_count
++;
238 VG_STATIC
void world_pct_relay( mdl_node
*pnode
)
240 struct logic_relay
*relay
= &world
.logic_relays
[ world
.relay_count
];
241 struct classtype_logic_relay
*inf
= mdl_get_entdata( world
.meta
, pnode
);
243 relay
->target_count
= 0;
245 for( int i
=0; i
<vg_list_size(relay
->targets
); i
++ )
247 if( inf
->targets
[i
] )
249 struct relay_target
*target
= &relay
->targets
[relay
->target_count
++];
250 mdl_node
*other
= mdl_node_from_id( world
.meta
, inf
->targets
[i
] );
252 target
->classtype
= other
->classtype
;
253 target
->sub_id
= other
->sub_uid
;
257 v3_copy( pnode
->co
, relay
->pos
);
258 world
.relay_count
++;
262 VG_STATIC
void world_pct_achievement( mdl_node
*pnode
)
264 struct logic_achievement
*ach
=
265 &world
.logic_achievements
[ world
.achievement_count
];
266 struct classtype_logic_achievement
*inf
=
267 mdl_get_entdata( world
.meta
, pnode
);
269 v3_copy( pnode
->co
, ach
->pos
);
270 ach
->achievement_id
= mdl_pstr( world
.meta
, inf
->pstr_name
);
273 world
.achievement_count
++;
277 VG_STATIC
void world_entities_process(void)
279 struct entity_instruction
282 void (*process
)( mdl_node
*pnode
);
284 entity_instructions
[] =
286 { k_classtype_spawn
, world_pct_spawn
},
287 { k_classtype_water
, world_pct_water
},
288 { k_classtype_audio
, world_pct_audio
},
289 { k_classtype_trigger
, world_pct_trigger
},
290 { k_classtype_logic_relay
, world_pct_relay
},
291 { k_classtype_logic_achievement
, world_pct_achievement
}
294 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
296 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
298 for( int j
=0; j
<vg_list_size(entity_instructions
); j
++ )
300 struct entity_instruction
*instr
= &entity_instructions
[j
];
302 if( pnode
->classtype
== instr
->ct
)
304 instr
->process( pnode
);
311 VG_STATIC
void edge_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
313 struct grind_edge
*edge_array
= user
,
314 *edge
= &edge_array
[ item_index
];
316 box_addpt( bound
, edge
->p0
);
317 box_addpt( bound
, edge
->p1
);
320 VG_STATIC
float edge_bh_centroid( void *user
, u32 item_index
, int axis
)
322 struct grind_edge
*edge_array
= user
,
323 *edge
= &edge_array
[ item_index
];
325 return (edge
->p0
[axis
] + edge
->p1
[axis
]) * 0.5f
;
328 VG_STATIC
void edge_bh_swap( void *user
, u32 ia
, u32 ib
)
330 struct grind_edge
*edge_array
= user
,
331 *e0
= &edge_array
[ ia
],
332 *e1
= &edge_array
[ ib
],
339 VG_STATIC
void edge_bh_closest( void *user
, u32 index
, v3f point
, v3f closest
)
341 struct grind_edge
*edge_array
= user
,
342 *edge
= &edge_array
[ index
];
344 closest_point_segment( edge
->p0
, edge
->p1
, point
, closest
);
347 VG_STATIC bh_system bh_system_edges
=
349 .expand_bound
= edge_bh_expand_bound
,
350 .item_centroid
= edge_bh_centroid
,
351 .item_closest
= edge_bh_closest
,
352 .item_swap
= edge_bh_swap
,
357 VG_STATIC
void world_generate_edges(void)
359 vg_info( "Generating edge array\n" );
360 world
.grind_edges
= vg_linear_alloc( world
.dynamic_vgl
,
361 5000*sizeof(struct grind_edge
) );
362 world
.grind_edge_count
= 0;
365 for( u32 i
=0; i
<world
.scene_geo
->vertex_count
; i
++ )
366 if( world
.scene_geo
->arrvertices
[i
].weights
[0] )
369 vg_info( "Grind verts: %u\n", fs_count
);
371 for( u32 i
=0; i
<world
.scene_geo
->indice_count
/3; i
++ )
373 u32
*ptri
= &world
.scene_geo
->arrindices
[ i
*3 ];
375 for( int j
=0; j
<3; j
++ )
380 mdl_vert
*v0
= &world
.scene_geo
->arrvertices
[ i0
],
381 *v1
= &world
.scene_geo
->arrvertices
[ i1
];
385 if( world
.grind_edge_count
== 5000 )
386 vg_fatal_exit_loop( "Edge capacity exceeded" );
388 struct grind_edge
*ge
=
389 &world
.grind_edges
[ world
.grind_edge_count
++ ];
391 v3_copy( v0
->co
, ge
->p0
);
392 v3_copy( v1
->co
, ge
->p1
);
397 vg_info( "Grind edge count: %u\n", world
.grind_edge_count
);
399 world
.grind_edges
= vg_linear_resize( world
.dynamic_vgl
, world
.grind_edges
,
400 world
.grind_edge_count
*sizeof(struct grind_edge
) );
402 world
.grind_bh
= bh_create( world
.dynamic_vgl
, &bh_system_edges
,
403 world
.grind_edges
, world
.grind_edge_count
,
407 VG_STATIC
void world_generate(void)
410 * Compile meshes into the world scenes
412 world
.scene_geo
= scene_init( world
.dynamic_vgl
, 320000, 1200000 );
415 m4x3_identity( midentity
);
418 * Generate scene: collidable geometry
419 * ----------------------------------------------------------------
422 vg_info( "Generating collidable geometry\n" );
425 for( int i
=0; i
<world
.material_count
; i
++ )
427 struct world_material
*mat
= &world
.materials
[ i
];
429 if( mat
->info
.flags
& k_material_flag_collision
)
430 world_add_all_if_material( midentity
, world
.scene_geo
, world
.meta
, i
);
432 scene_copy_slice( world
.scene_geo
, &mat
->sm_geo
);
438 /* compress that bad boy */
439 world
.scene_geo
= scene_fix( world
.dynamic_vgl
, world
.scene_geo
);
441 vg_acquire_thread_sync();
443 scene_upload( world
.scene_geo
, &world
.mesh_geo
);
445 vg_release_thread_sync();
447 /* setup spacial mapping and rigidbody */
448 world
.geo_bh
= scene_bh_create( world
.dynamic_vgl
, world
.scene_geo
);
450 v3_zero( world
.rb_geo
.co
);
451 q_identity( world
.rb_geo
.q
);
453 world
.rb_geo
.type
= k_rb_shape_scene
;
454 world
.rb_geo
.inf
.scene
.bh_scene
= world
.geo_bh
;
455 world
.rb_geo
.is_world
= 1;
456 rb_init( &world
.rb_geo
);
459 * Generate scene: non-collidable geometry
460 * ----------------------------------------------------------------
462 vg_info( "Generating non-collidable geometry\n" );
464 world
.scene_no_collide
= scene_init( world
.dynamic_vgl
, 200000, 500000 );
466 for( int i
=0; i
<world
.material_count
; i
++ )
468 struct world_material
*mat
= &world
.materials
[ i
];
470 if( !(mat
->info
.flags
& k_material_flag_collision
) )
472 world_add_all_if_material( midentity
, world
.scene_no_collide
,
476 if( mat
->info
.flags
& k_material_flag_grow_grass
)
477 world_apply_procedural_foliage( mat
);
479 scene_copy_slice( world
.scene_no_collide
, &mat
->sm_no_collide
);
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
;
492 world_generate_edges();
495 VG_STATIC
int reset_player( int argc
, char const *argv
[] );
496 VG_STATIC
void world_post_process(void)
498 /* initialize audio if need be */
500 for( int i
=0; i
<world
.audio_things_count
; i
++ )
502 struct world_audio_thing
*thingy
= &world
.audio_things
[ i
];
504 audio_player_init( &thingy
->player
);
505 audio_player_set_flags( &thingy
->player
, thingy
->flags
);
506 audio_player_set_vol( &thingy
->player
, thingy
->volume
);
507 audio_player_set_pan( &thingy
->player
, 0.0f
);
509 if( thingy
->flags
& AUDIO_FLAG_SPACIAL_3D
)
510 audio_player_set_position( &thingy
->player
, thingy
->pos
);
512 if( thingy
->flags
& AUDIO_FLAG_AUTO_START
)
513 audio_player_playclip( &thingy
->player
, &thingy
->temp_embedded_clip
);
517 vg_acquire_thread_sync();
520 * Rendering the depth map
525 v3_sub( world
.scene_geo
->bbx
[1], world
.scene_geo
->bbx
[0], extent
);
527 float fl
= world
.scene_geo
->bbx
[0][0],
528 fr
= world
.scene_geo
->bbx
[1][0],
529 fb
= world
.scene_geo
->bbx
[0][2],
530 ft
= world
.scene_geo
->bbx
[1][2],
534 m4x4_zero( ortho
.mtx
.p
);
535 ortho
.mtx
.p
[0][0] = 2.0f
* rl
;
536 ortho
.mtx
.p
[2][1] = 2.0f
* tb
;
537 ortho
.mtx
.p
[3][0] = (fr
+ fl
) * -rl
;
538 ortho
.mtx
.p
[3][1] = (ft
+ fb
) * -tb
;
539 ortho
.mtx
.p
[3][3] = 1.0f
;
540 m4x3_identity( ortho
.transform
);
541 camera_update_view( &ortho
);
542 camera_finalize( &ortho
);
544 glDisable(GL_DEPTH_TEST
);
546 glDisable(GL_CULL_FACE
);
547 render_fb_bind( gpipeline
.fb_heightmap
);
548 shader_blitcolour_use();
549 shader_blitcolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
554 glBlendFunc(GL_ONE
, GL_ONE
);
555 glBlendEquation(GL_MAX
);
557 render_world_depth( &ortho
);
559 glEnable(GL_DEPTH_TEST
);
560 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
563 * TODO: World settings entity
565 struct ub_world_lighting
*winfo
= &gpipeline
.ub_world_lighting
;
567 if( world
.water
.enabled
)
568 v4_copy( world
.water
.plane
, winfo
->g_water_plane
);
571 v3f
*bounds
= world
.scene_geo
->bbx
;
573 info_vec
[0] = bounds
[0][0];
574 info_vec
[1] = bounds
[0][2];
575 info_vec
[2] = 1.0f
/ (bounds
[1][0]-bounds
[0][0]);
576 info_vec
[3] = 1.0f
/ (bounds
[1][2]-bounds
[0][2]);
577 v4_copy( info_vec
, winfo
->g_depth_bounds
);
579 winfo
->g_water_fog
= 0.04f
;
580 render_update_lighting_ub();
583 vg_release_thread_sync();
586 * Setup scene collider
589 reset_player( 1, (const char *[]){"start"} );
592 VG_STATIC
void world_process_resources(void)
594 vg_info( "Loading textures\n" );
595 world
.texture_count
= world
.meta
->info
.texture_count
;
597 vg_linear_alloc( world
.dynamic_vgl
, sizeof(GLuint
)*world
.texture_count
);
599 vg_acquire_thread_sync();
602 world
.textures
[0] = vg_tex2d_new();
603 vg_tex2d_set_error();
607 for( int i
=1; i
<world
.texture_count
; i
++ )
609 mdl_texture
*tex
= &world
.meta
->texture_buffer
[i
];
611 if( !tex
->pack_offset
)
613 vg_release_thread_sync();
614 vg_fatal_exit_loop( "World models must have packed textures!" );
617 vg_linear_clear( vg_mem
.scratch
);
618 world
.textures
[i
] = vg_tex2d_new();
619 vg_tex2d_set_error();
620 vg_tex2d_qoi( world
.meta
->pack
+ tex
->pack_offset
, tex
->pack_length
,
621 mdl_pstr( world
.meta
, tex
->pstr_name
));
626 vg_release_thread_sync();
628 vg_info( "Loading materials\n" );
630 u32 size
= sizeof(struct world_material
) * world
.meta
->info
.material_count
;
631 world
.materials
= vg_linear_alloc( world
.dynamic_vgl
, size
);
633 world
.material_count
= world
.meta
->info
.material_count
;
634 memset( world
.materials
, 0, size
);
636 for( int i
=1; i
<world
.material_count
; i
++ )
637 world
.materials
[i
].info
= world
.meta
->material_buffer
[i
];
640 struct world_material
*errmat
= &world
.materials
[0];
641 v4_copy( (v4f
){ 1.0f
,0.0f
,0.0f
,1.0f
}, errmat
->info
.colour
);
642 v4_copy( (v4f
){ 1.0f
,0.0f
,0.0f
,1.0f
}, errmat
->info
.colour1
);
643 errmat
->info
.flags
= 0x00;
644 errmat
->info
.pstr_name
= 0; /* useless? */
645 errmat
->info
.shader
= -1;
646 errmat
->info
.tex_decal
= 0;
647 errmat
->info
.tex_diffuse
= 0;
648 errmat
->info
.tex_normal
= 0;
651 VG_STATIC
void world_unload(void)
653 vg_acquire_thread_sync();
656 mesh_free( &world
.mesh_route_lines
);
657 mesh_free( &world
.mesh_geo
);
658 mesh_free( &world
.mesh_no_collide
);
661 world
.rewind_from
= 0.0;
662 world
.rewind_to
= 0.0;
663 world
.last_use
= 0.0;
664 world
.active_gate
= 0;
665 world
.current_run_version
= 2;
666 world
.active_route_board
= 0;
667 v3_zero( world
.render_gate_pos
);
669 for( int i
=0; i
<vg_list_size(world
.ui_bars
); i
++ )
671 struct route_ui_bar
*uib
= &world
.ui_bars
[i
];
672 uib
->segment_start
= 0;
673 uib
->segment_count
= 0;
676 uib
->fade_timer_start
= 0.0;
680 /* delete textures and meshes */
681 glDeleteTextures( world
.texture_count
, world
.textures
);
683 /* delete the entire block of memory */
684 vg_linear_clear( world
.dynamic_vgl
);
685 vg_linear_clear( world
.audio_vgl
);
687 /* clean dangling pointers */
690 world
.textures
= NULL
;
691 world
.texture_count
= 0;
692 world
.materials
= NULL
;
693 world
.material_count
= 0;
695 world
.scene_geo
= NULL
;
696 world
.scene_no_collide
= NULL
;
697 world
.scene_lines
= NULL
;
698 world
.grind_edges
= NULL
;
699 world
.grind_edge_count
= 0;
701 world
.grind_bh
= NULL
;
703 world
.trigger_bh
= NULL
;
704 world
.audio_bh
= NULL
;
707 world
.spawn_count
= 0;
709 world
.audio_things
= NULL
;
710 world
.audio_things_count
= 0;
712 world
.triggers
= NULL
;
713 world
.trigger_count
= 0;
715 world
.logic_relays
= NULL
;
716 world
.relay_count
= 0;
718 world
.logic_achievements
= NULL
;
719 world
.achievement_count
= 0;
722 world
.node_count
= 0;
725 world
.route_count
= 0;
728 world
.gate_count
= 0;
730 world
.collectors
= NULL
;
731 world
.collector_count
= 0;
733 world
.water
.enabled
= 0;
735 vg_release_thread_sync();
738 VG_STATIC
void world_load(void)
742 world
.meta
= mdl_load_full( world
.dynamic_vgl
, world
.world_name
);
743 vg_info( "Loading world: %s\n", world
.world_name
);
745 /* process resources from pack */
746 world_process_resources();
748 /* dynamic allocations */
749 world_ents_allocate();
750 world_routes_allocate();
752 /* meta processing */
753 world_routes_process();
754 world_entities_process();
758 world_routes_generate();
759 world_post_process();
762 #endif /* WORLD_GEN_H */