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 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 struct world_material
*m1
= ray_hit_material( &hit
);
65 if((hit
.normal
[1] > 0.8f
) && (m1
== mat
) && (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 world
.water
.enabled
= 1;
177 water_set_surface( pnode
->co
[1] );
180 VG_STATIC
void world_pct_audio( mdl_node
*pnode
)
182 struct world_audio_thing
*thing
= &world
.audio_things
[
183 world
.audio_things_count
];
185 memset( thing
, 0, sizeof(struct world_audio_thing
) );
186 struct classtype_audio
*aud
= mdl_get_entdata( world
.meta
, pnode
);
188 v3_copy( pnode
->co
, thing
->pos
);
190 if( aud
->flags
& AUDIO_FLAG_SPACIAL_3D
)
191 thing
->volume
= aud
->volume
* pnode
->s
[0];
193 thing
->volume
= aud
->volume
;
195 thing
->flags
= aud
->flags
;
196 thing
->temp_embedded_clip
.path
= mdl_pstr( world
.meta
, aud
->pstr_file
);
197 thing
->temp_embedded_clip
.source_mode
= k_audio_source_mono
;
199 audio_clip_load( &thing
->temp_embedded_clip
, world
.audio_vgl
);
200 thing
->player
.name
= mdl_pstr( world
.meta
, pnode
->pstr_name
);
201 thing
->player
.enqued
= 0;
203 pnode
->sub_uid
= world
.audio_things_count
;
204 world
.audio_things_count
++;
208 VG_STATIC
void world_pct_trigger( mdl_node
*pnode
)
210 struct trigger_zone
*trigger
= &world
.triggers
[ world
.trigger_count
];
211 struct classtype_trigger
*inf
= mdl_get_entdata( world
.meta
, pnode
);
215 mdl_node
*target_node
= mdl_node_from_id( world
.meta
, inf
->target
);
217 trigger
->target
.sub_id
= target_node
->sub_uid
;
218 trigger
->target
.classtype
= target_node
->classtype
;
222 vg_warn( "Trigger with no target...\n" );
226 mdl_node_transform( pnode
, trigger
->transform
);
227 m4x3_invert_full( trigger
->transform
, trigger
->inv_transform
);
229 world
.trigger_count
++;
233 VG_STATIC
void world_pct_relay( mdl_node
*pnode
)
235 struct logic_relay
*relay
= &world
.logic_relays
[ world
.relay_count
];
236 struct classtype_logic_relay
*inf
= mdl_get_entdata( world
.meta
, pnode
);
238 relay
->target_count
= 0;
240 for( int i
=0; i
<vg_list_size(relay
->targets
); i
++ )
242 if( inf
->targets
[i
] )
244 struct relay_target
*target
= &relay
->targets
[relay
->target_count
++];
245 mdl_node
*other
= mdl_node_from_id( world
.meta
, inf
->targets
[i
] );
247 target
->classtype
= other
->classtype
;
248 target
->sub_id
= other
->sub_uid
;
252 v3_copy( pnode
->co
, relay
->pos
);
253 world
.relay_count
++;
257 VG_STATIC
void world_pct_achievement( mdl_node
*pnode
)
259 struct logic_achievement
*ach
=
260 &world
.logic_achievements
[ world
.achievement_count
];
261 struct classtype_logic_achievement
*inf
=
262 mdl_get_entdata( world
.meta
, pnode
);
264 v3_copy( pnode
->co
, ach
->pos
);
265 ach
->achievement_id
= mdl_pstr( world
.meta
, inf
->pstr_name
);
268 world
.achievement_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_audio
, world_pct_audio
},
284 { k_classtype_trigger
, world_pct_trigger
},
285 { k_classtype_logic_relay
, world_pct_relay
},
286 { k_classtype_logic_achievement
, world_pct_achievement
}
289 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
291 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
293 for( int j
=0; j
<vg_list_size(entity_instructions
); j
++ )
295 struct entity_instruction
*instr
= &entity_instructions
[j
];
297 if( pnode
->classtype
== instr
->ct
)
299 instr
->process( pnode
);
306 VG_STATIC
void world_generate(void)
309 * Compile meshes into the world scenes
311 world
.scene_geo
= scene_init( world
.dynamic_vgl
, 320000, 1200000 );
314 m4x3_identity( midentity
);
317 * Generate scene: collidable geometry
318 * ----------------------------------------------------------------
321 vg_info( "Generating collidable geometry\n" );
324 for( int i
=0; i
<world
.material_count
; i
++ )
326 struct world_material
*mat
= &world
.materials
[ i
];
328 if( mat
->info
.flags
& k_material_flag_collision
)
329 world_add_all_if_material( midentity
, world
.scene_geo
, world
.meta
, i
);
331 scene_copy_slice( world
.scene_geo
, &mat
->sm_geo
);
337 /* compress that bad boy */
338 world
.scene_geo
= scene_fix( world
.dynamic_vgl
, world
.scene_geo
);
340 vg_acquire_thread_sync();
342 scene_upload( world
.scene_geo
, &world
.mesh_geo
);
344 vg_release_thread_sync();
346 /* setup spacial mapping and rigidbody */
347 world
.geo_bh
= scene_bh_create( world
.dynamic_vgl
, world
.scene_geo
);
349 v3_zero( world
.rb_geo
.co
);
350 q_identity( world
.rb_geo
.q
);
352 world
.rb_geo
.type
= k_rb_shape_scene
;
353 world
.rb_geo
.inf
.scene
.bh_scene
= world
.geo_bh
;
354 world
.rb_geo
.is_world
= 1;
355 rb_init( &world
.rb_geo
);
358 * Generate scene: non-collidable geometry
359 * ----------------------------------------------------------------
361 vg_info( "Generating non-collidable geometry\n" );
363 world
.scene_no_collide
= scene_init( world
.dynamic_vgl
, 200000, 500000 );
366 vg_info( "Applying foliage\n" );
368 world_apply_procedural_foliage();
369 scene_copy_slice( world
.scene_no_collide
, &world
.sm_foliage_main
);
372 for( int i
=0; i
<world
.material_count
; i
++ )
374 struct world_material
*mat
= &world
.materials
[ i
];
376 if( !(mat
->info
.flags
& k_material_flag_collision
) )
378 world_add_all_if_material( midentity
, world
.scene_no_collide
,
382 if( mat
->info
.flags
& k_material_flag_grow_grass
)
383 world_apply_procedural_foliage( mat
);
385 scene_copy_slice( world
.scene_no_collide
, &mat
->sm_no_collide
);
388 /* upload and free that */
389 vg_acquire_thread_sync();
391 scene_upload( world
.scene_no_collide
, &world
.mesh_no_collide
);
393 vg_release_thread_sync();
395 vg_linear_del( world
.dynamic_vgl
, world
.scene_no_collide
);
396 world
.scene_no_collide
= NULL
;
399 VG_STATIC
int reset_player( int argc
, char const *argv
[] );
400 VG_STATIC
void world_post_process(void)
402 /* initialize audio if need be */
404 for( int i
=0; i
<world
.audio_things_count
; i
++ )
406 struct world_audio_thing
*thingy
= &world
.audio_things
[ i
];
408 audio_player_init( &thingy
->player
);
409 audio_player_set_flags( &thingy
->player
, thingy
->flags
);
410 audio_player_set_vol( &thingy
->player
, thingy
->volume
);
411 audio_player_set_pan( &thingy
->player
, 0.0f
);
413 if( thingy
->flags
& AUDIO_FLAG_SPACIAL_3D
)
414 audio_player_set_position( &thingy
->player
, thingy
->pos
);
416 if( thingy
->flags
& AUDIO_FLAG_AUTO_START
)
417 audio_player_playclip( &thingy
->player
, &thingy
->temp_embedded_clip
);
421 vg_acquire_thread_sync();
424 * Rendering the depth map
430 v3_sub( world
.scene_geo
->bbx
[1], world
.scene_geo
->bbx
[0], extent
);
432 float fl
= world
.scene_geo
->bbx
[0][0],
433 fr
= world
.scene_geo
->bbx
[1][0],
434 fb
= world
.scene_geo
->bbx
[0][2],
435 ft
= world
.scene_geo
->bbx
[1][2],
440 ortho
[0][0] = 2.0f
* rl
;
441 ortho
[2][1] = 2.0f
* tb
;
442 ortho
[3][0] = (fr
+ fl
) * -rl
;
443 ortho
[3][1] = (ft
+ fb
) * -tb
;
445 m4x3_identity( camera
);
447 glDisable(GL_DEPTH_TEST
);
449 glDisable(GL_CULL_FACE
);
450 glBindFramebuffer( GL_FRAMEBUFFER
, gpipeline
.fb_depthmap
);
451 glViewport( 0, 0, 1024, 1024 );
452 shader_fscolour_use();
453 shader_fscolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
458 glBlendFunc(GL_ONE
, GL_ONE
);
459 glBlendEquation(GL_MAX
);
460 render_world_depth( ortho
, camera
);
462 glEnable(GL_DEPTH_TEST
);
463 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
467 * TODO: World settings entity
469 struct ub_world_lighting
*winfo
= &gpipeline
.ub_world_lighting
;
471 if( world
.water
.enabled
)
472 v4_copy( world
.water
.plane
, winfo
->g_water_plane
);
475 v3f
*bounds
= world
.scene_geo
->bbx
;
477 info_vec
[0] = bounds
[0][0];
478 info_vec
[1] = bounds
[0][2];
479 info_vec
[2] = 1.0f
/ (bounds
[1][0]-bounds
[0][0]);
480 info_vec
[3] = 1.0f
/ (bounds
[1][2]-bounds
[0][2]);
481 v4_copy( info_vec
, winfo
->g_depth_bounds
);
483 winfo
->g_water_fog
= 0.04f
;
484 render_update_lighting_ub();
487 vg_release_thread_sync();
490 * Setup scene collider
493 reset_player( 1, (const char *[]){"start"} );
496 VG_STATIC
void world_process_resources(void)
498 vg_info( "Loading textures\n" );
499 world
.texture_count
= world
.meta
->info
.texture_count
;
501 vg_linear_alloc( world
.dynamic_vgl
, sizeof(GLuint
)*world
.texture_count
);
503 vg_acquire_thread_sync();
506 world
.textures
[0] = vg_tex2d_new();
507 vg_tex2d_set_error();
511 for( int i
=1; i
<world
.texture_count
; i
++ )
513 mdl_texture
*tex
= &world
.meta
->texture_buffer
[i
];
515 if( !tex
->pack_offset
)
517 vg_release_thread_sync();
518 vg_fatal_exit_loop( "World models must have packed textures!" );
521 vg_linear_clear( vg_mem
.scratch
);
522 world
.textures
[i
] = vg_tex2d_new();
523 vg_tex2d_set_error();
524 vg_tex2d_qoi( world
.meta
->pack
+ tex
->pack_offset
, tex
->pack_length
,
525 mdl_pstr( world
.meta
, tex
->pstr_name
));
530 vg_release_thread_sync();
532 vg_info( "Loading materials\n" );
534 u32 size
= sizeof(struct world_material
) * world
.meta
->info
.material_count
;
535 world
.materials
= vg_linear_alloc( world
.dynamic_vgl
, size
);
537 world
.material_count
= world
.meta
->info
.material_count
;
538 memset( world
.materials
, 0, size
);
540 for( int i
=1; i
<world
.material_count
; i
++ )
541 world
.materials
[i
].info
= world
.meta
->material_buffer
[i
];
544 struct world_material
*errmat
= &world
.materials
[0];
545 v4_copy( (v4f
){ 1.0f
,0.0f
,0.0f
,1.0f
}, errmat
->info
.colour
);
546 v4_copy( (v4f
){ 1.0f
,0.0f
,0.0f
,1.0f
}, errmat
->info
.colour1
);
547 errmat
->info
.flags
= 0x00;
548 errmat
->info
.pstr_name
= 0; /* useless? */
549 errmat
->info
.shader
= -1;
550 errmat
->info
.tex_decal
= 0;
551 errmat
->info
.tex_diffuse
= 0;
552 errmat
->info
.tex_normal
= 0;
555 VG_STATIC
void world_unload(void)
558 mesh_free( &world
.mesh_route_lines
);
559 mesh_free( &world
.mesh_geo
);
560 mesh_free( &world
.mesh_no_collide
);
563 world
.rewind_from
= 0.0;
564 world
.rewind_to
= 0.0;
565 world
.last_use
= 0.0;
566 world
.active_gate
= 0;
567 world
.current_run_version
= 2;
568 world
.active_route_board
= 0;
569 v3_zero( world
.render_gate_pos
);
571 for( int i
=0; i
<vg_list_size(world
.ui_bars
); i
++ )
573 struct route_ui_bar
*uib
= &world
.ui_bars
[i
];
574 uib
->segment_start
= 0;
575 uib
->segment_count
= 0;
578 uib
->fade_timer_start
= 0.0;
582 /* delete textures and meshes */
583 glDeleteTextures( world
.texture_count
, world
.textures
);
585 /* delete the entire block of memory */
586 vg_linear_clear( world
.dynamic_vgl
);
587 vg_linear_clear( world
.audio_vgl
);
589 /* clean dangling pointers */
592 world
.textures
= NULL
;
593 world
.texture_count
= 0;
594 world
.materials
= NULL
;
595 world
.material_count
= 0;
597 world
.scene_geo
= NULL
;
598 world
.scene_no_collide
= NULL
;
599 world
.scene_lines
= NULL
;
602 world
.trigger_bh
= NULL
;
603 world
.audio_bh
= NULL
;
606 world
.spawn_count
= 0;
608 world
.audio_things
= NULL
;
609 world
.audio_things_count
= 0;
611 world
.triggers
= NULL
;
612 world
.trigger_count
= 0;
614 world
.logic_relays
= NULL
;
615 world
.relay_count
= 0;
617 world
.logic_achievements
= NULL
;
618 world
.achievement_count
= 0;
621 world
.node_count
= 0;
624 world
.route_count
= 0;
627 world
.gate_count
= 0;
629 world
.collectors
= NULL
;
630 world
.collector_count
= 0;
632 world
.water
.enabled
= 0;
635 VG_STATIC
void world_load(void)
639 world
.meta
= mdl_load_full( world
.dynamic_vgl
, world
.world_name
);
640 vg_info( "Loading world: %s\n", world
.world_name
);
642 /* process resources from pack */
643 world_process_resources();
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 */