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_mdl_submesh( pscene
, mdl
, sm
, transform2
);
36 VG_STATIC
void world_add_blob( scene
*pscene
, ray_hit
*hit
)
39 v4f qsurface
, qrandom
;
42 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
->normal
, axis
);
44 float angle
= v3_dot(hit
->normal
,(v3f
){0.0f
,1.0f
,0.0f
});
45 q_axis_angle( qsurface
, axis
, angle
);
46 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
47 q_mul( qsurface
, qrandom
, qsurface
);
48 q_m3x3( qsurface
, transform
);
49 v3_copy( hit
->pos
, transform
[3] );
53 { .co
= { -1.00f
, 0.0f
, 0.0f
} },
54 { .co
= { 1.00f
, 0.0f
, 0.0f
} },
55 { .co
= { -1.00f
, 1.2f
, 0.0f
} },
56 { .co
= { 1.00f
, 1.2f
, 0.0f
} },
57 { .co
= { -0.25f
, 2.0f
, 0.0f
} },
58 { .co
= { 0.25f
, 2.0f
, 0.0f
} }
61 const u32 indices
[] = { 0,1,3, 0,3,2, 2,3,5, 2,5,4 };
63 if( pscene
->vertex_count
+ vg_list_size(verts
) > pscene
->max_vertices
)
64 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
66 if( pscene
->indice_count
+ vg_list_size(indices
) > pscene
->max_indices
)
67 vg_fatal_exit_loop( "Scene index buffer overflow" );
69 scene_vert
*dst_verts
= &pscene
->arrvertices
[ pscene
->vertex_count
];
70 u32
*dst_indices
= &pscene
->arrindices
[ pscene
->indice_count
];
72 scene_vert
*ref
= &world
.scene_geo
->arrvertices
[ hit
->tri
[0] ];
74 for( u32 i
=0; i
<vg_list_size(verts
); i
++ )
76 scene_vert
*pvert
= &dst_verts
[ i
],
79 m4x3_mulv( transform
, src
->co
, pvert
->co
);
80 scene_vert_pack_norm( pvert
, transform
[1] );
82 v2_copy( ref
->uv
, pvert
->uv
);
85 for( u32 i
=0; i
<vg_list_size(indices
); i
++ )
86 dst_indices
[i
] = indices
[i
] + pscene
->vertex_count
;
88 pscene
->vertex_count
+= vg_list_size(verts
);
89 pscene
->indice_count
+= vg_list_size(indices
);
92 /* Sprinkle foliage models over the map on terrain material */
93 VG_STATIC
void world_apply_procedural_foliage( struct world_material
*mat
)
95 if( vg
.quality_profile
== k_quality_profile_low
)
98 vg_info( "Applying foliage (%u)\n", mat
->info
.pstr_name
);
100 vg_linear_clear( vg_mem
.scratch
);
102 mdl_context
*mfoliage
=
103 mdl_load_full( vg_mem
.scratch
, "models/rs_foliage.mdl");
106 v3_sub( world
.scene_geo
->bbx
[1], world
.scene_geo
->bbx
[0], volume
);
109 mdl_node
*mblob
= mdl_node_from_name( mfoliage
, "blob" );
110 mdl_submesh
*sm_blob
= mdl_node_submesh( mfoliage
, mblob
, 0 );
114 float area
= volume
[0]*volume
[2];
115 u32 particles
= 0.08f
* area
;
117 vg_info( "Map area: %f. Max particles: %u\n", area
, particles
);
119 for( int i
=0;i
<particles
;i
++ )
122 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
124 v3_add( pos
, world
.scene_geo
->bbx
[0], pos
);
129 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
131 struct world_material
*m1
= ray_hit_material( &hit
);
132 if((hit
.normal
[1] > 0.8f
) && (m1
== mat
) && (hit
.pos
[1] > 0.0f
+10.0f
))
134 world_add_blob( world
.scene_no_collide
, &hit
);
140 vg_info( "%d foliage models added\n", count
);
143 VG_STATIC
void world_ents_allocate(void)
145 vg_info( "Allocating entities\n" );
147 /* count entites to allocate buffers for them.
148 * maybe in the future we just store these numbers in the model file...
150 * TODO: use this in world_routes too */
163 (void*)&world
.spawns
,
164 sizeof(struct respawn_point
)
168 (void*)&world
.audio_things
,
169 sizeof(struct world_audio_thing
)
173 (void*)&world
.triggers
,
174 sizeof(struct trigger_zone
)
177 k_classtype_logic_relay
,
178 (void*)&world
.logic_relays
,
179 sizeof(struct logic_relay
)
182 k_classtype_logic_achievement
,
183 (void*)&world
.logic_achievements
,
184 sizeof(struct logic_achievement
)
188 for( int i
=0; i
<vg_list_size(entity_counts
); i
++ )
189 entity_counts
[i
].count
= 0;
191 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
193 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
195 for( int j
=0; j
<vg_list_size(entity_counts
); j
++ )
197 if( pnode
->classtype
== entity_counts
[j
].ct
)
199 pnode
->sub_uid
= entity_counts
[j
].count
;
200 entity_counts
[j
].count
++;
206 for( int i
=0; i
<vg_list_size(entity_counts
); i
++ )
208 struct countable
*counter
= &entity_counts
[i
];
210 u32 bufsize
= counter
->item_size
*counter
->count
;
211 *counter
->to_allocate
= vg_linear_alloc( world
.dynamic_vgl
, bufsize
);
215 VG_STATIC
void world_pct_spawn( mdl_node
*pnode
)
217 struct respawn_point
*rp
= &world
.spawns
[ world
.spawn_count
++ ];
219 v3_copy( pnode
->co
, rp
->co
);
220 v4_copy( pnode
->q
, rp
->q
);
221 rp
->name
= mdl_pstr( world
.meta
, pnode
->pstr_name
);
224 VG_STATIC
void world_pct_water( mdl_node
*pnode
)
226 if( world
.water
.enabled
)
228 vg_warn( "Multiple water surfaces in level! ('%s')\n",
229 mdl_pstr( world
.meta
, pnode
->pstr_name
));
233 world
.water
.enabled
= 1;
234 water_set_surface( pnode
->co
[1] );
237 VG_STATIC
void world_pct_audio( mdl_node
*pnode
)
239 struct world_audio_thing
*thing
= &world
.audio_things
[
240 world
.audio_things_count
];
242 memset( thing
, 0, sizeof(struct world_audio_thing
) );
243 struct classtype_audio
*aud
= mdl_get_entdata( world
.meta
, pnode
);
245 v3_copy( pnode
->co
, thing
->pos
);
247 if( aud
->flags
& AUDIO_FLAG_SPACIAL_3D
)
248 thing
->volume
= aud
->volume
* pnode
->s
[0];
250 thing
->volume
= aud
->volume
;
252 thing
->flags
= aud
->flags
;
253 thing
->temp_embedded_clip
.path
= mdl_pstr( world
.meta
, aud
->pstr_file
);
254 thing
->temp_embedded_clip
.source_mode
= k_audio_source_mono
;
256 audio_clip_load( &thing
->temp_embedded_clip
, world
.audio_vgl
);
257 thing
->player
.name
= mdl_pstr( world
.meta
, pnode
->pstr_name
);
258 thing
->player
.enqued
= 0;
260 pnode
->sub_uid
= world
.audio_things_count
;
261 world
.audio_things_count
++;
265 VG_STATIC
void world_pct_trigger( mdl_node
*pnode
)
267 struct trigger_zone
*trigger
= &world
.triggers
[ world
.trigger_count
];
268 struct classtype_trigger
*inf
= mdl_get_entdata( world
.meta
, pnode
);
272 mdl_node
*target_node
= mdl_node_from_id( world
.meta
, inf
->target
);
274 trigger
->target
.sub_id
= target_node
->sub_uid
;
275 trigger
->target
.classtype
= target_node
->classtype
;
279 vg_warn( "Trigger with no target...\n" );
283 mdl_node_transform( pnode
, trigger
->transform
);
284 m4x3_invert_full( trigger
->transform
, trigger
->inv_transform
);
286 world
.trigger_count
++;
290 VG_STATIC
void world_pct_relay( mdl_node
*pnode
)
292 struct logic_relay
*relay
= &world
.logic_relays
[ world
.relay_count
];
293 struct classtype_logic_relay
*inf
= mdl_get_entdata( world
.meta
, pnode
);
295 relay
->target_count
= 0;
297 for( int i
=0; i
<vg_list_size(relay
->targets
); i
++ )
299 if( inf
->targets
[i
] )
301 struct relay_target
*target
= &relay
->targets
[relay
->target_count
++];
302 mdl_node
*other
= mdl_node_from_id( world
.meta
, inf
->targets
[i
] );
304 target
->classtype
= other
->classtype
;
305 target
->sub_id
= other
->sub_uid
;
309 v3_copy( pnode
->co
, relay
->pos
);
310 world
.relay_count
++;
314 VG_STATIC
void world_pct_achievement( mdl_node
*pnode
)
316 struct logic_achievement
*ach
=
317 &world
.logic_achievements
[ world
.achievement_count
];
318 struct classtype_logic_achievement
*inf
=
319 mdl_get_entdata( world
.meta
, pnode
);
321 v3_copy( pnode
->co
, ach
->pos
);
322 ach
->achievement_id
= mdl_pstr( world
.meta
, inf
->pstr_name
);
325 world
.achievement_count
++;
329 VG_STATIC
void world_entities_process(void)
331 struct entity_instruction
334 void (*process
)( mdl_node
*pnode
);
336 entity_instructions
[] =
338 { k_classtype_spawn
, world_pct_spawn
},
339 { k_classtype_water
, world_pct_water
},
340 { k_classtype_audio
, world_pct_audio
},
341 { k_classtype_trigger
, world_pct_trigger
},
342 { k_classtype_logic_relay
, world_pct_relay
},
343 { k_classtype_logic_achievement
, world_pct_achievement
}
346 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
348 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
350 for( int j
=0; j
<vg_list_size(entity_instructions
); j
++ )
352 struct entity_instruction
*instr
= &entity_instructions
[j
];
354 if( pnode
->classtype
== instr
->ct
)
356 instr
->process( pnode
);
363 VG_STATIC
void world_generate(void)
366 * Compile meshes into the world scenes
368 world
.scene_geo
= scene_init( world
.dynamic_vgl
, 320000, 1200000 );
371 m4x3_identity( midentity
);
374 * Generate scene: collidable geometry
375 * ----------------------------------------------------------------
378 vg_info( "Generating collidable geometry\n" );
381 for( int i
=0; i
<world
.material_count
; i
++ )
383 struct world_material
*mat
= &world
.materials
[ i
];
385 if( mat
->info
.flags
& k_material_flag_collision
)
386 world_add_all_if_material( midentity
, world
.scene_geo
, world
.meta
, i
);
388 scene_copy_slice( world
.scene_geo
, &mat
->sm_geo
);
394 /* compress that bad boy */
395 world
.scene_geo
= scene_fix( world
.dynamic_vgl
, world
.scene_geo
);
397 vg_acquire_thread_sync();
399 scene_upload( world
.scene_geo
, &world
.mesh_geo
);
401 vg_release_thread_sync();
403 /* setup spacial mapping and rigidbody */
404 world
.geo_bh
= scene_bh_create( world
.dynamic_vgl
, world
.scene_geo
);
406 v3_zero( world
.rb_geo
.co
);
407 q_identity( world
.rb_geo
.q
);
409 world
.rb_geo
.type
= k_rb_shape_scene
;
410 world
.rb_geo
.inf
.scene
.bh_scene
= world
.geo_bh
;
411 world
.rb_geo
.is_world
= 1;
412 rb_init( &world
.rb_geo
);
415 * Generate scene: non-collidable geometry
416 * ----------------------------------------------------------------
418 vg_info( "Generating non-collidable geometry\n" );
420 world
.scene_no_collide
= scene_init( world
.dynamic_vgl
, 200000, 500000 );
422 for( int i
=0; i
<world
.material_count
; i
++ )
424 struct world_material
*mat
= &world
.materials
[ i
];
426 if( !(mat
->info
.flags
& k_material_flag_collision
) )
428 world_add_all_if_material( midentity
, world
.scene_no_collide
,
432 if( mat
->info
.flags
& k_material_flag_grow_grass
)
433 world_apply_procedural_foliage( mat
);
435 scene_copy_slice( world
.scene_no_collide
, &mat
->sm_no_collide
);
438 /* upload and free that */
439 vg_acquire_thread_sync();
441 scene_upload( world
.scene_no_collide
, &world
.mesh_no_collide
);
443 vg_release_thread_sync();
445 vg_linear_del( world
.dynamic_vgl
, world
.scene_no_collide
);
446 world
.scene_no_collide
= NULL
;
449 VG_STATIC
int reset_player( int argc
, char const *argv
[] );
450 VG_STATIC
void world_post_process(void)
452 /* initialize audio if need be */
454 for( int i
=0; i
<world
.audio_things_count
; i
++ )
456 struct world_audio_thing
*thingy
= &world
.audio_things
[ i
];
458 audio_player_init( &thingy
->player
);
459 audio_player_set_flags( &thingy
->player
, thingy
->flags
);
460 audio_player_set_vol( &thingy
->player
, thingy
->volume
);
461 audio_player_set_pan( &thingy
->player
, 0.0f
);
463 if( thingy
->flags
& AUDIO_FLAG_SPACIAL_3D
)
464 audio_player_set_position( &thingy
->player
, thingy
->pos
);
466 if( thingy
->flags
& AUDIO_FLAG_AUTO_START
)
467 audio_player_playclip( &thingy
->player
, &thingy
->temp_embedded_clip
);
471 vg_acquire_thread_sync();
474 * Rendering the depth map
479 v3_sub( world
.scene_geo
->bbx
[1], world
.scene_geo
->bbx
[0], extent
);
481 float fl
= world
.scene_geo
->bbx
[0][0],
482 fr
= world
.scene_geo
->bbx
[1][0],
483 fb
= world
.scene_geo
->bbx
[0][2],
484 ft
= world
.scene_geo
->bbx
[1][2],
488 m4x4_zero( ortho
.mtx
.p
);
489 ortho
.mtx
.p
[0][0] = 2.0f
* rl
;
490 ortho
.mtx
.p
[2][1] = 2.0f
* tb
;
491 ortho
.mtx
.p
[3][0] = (fr
+ fl
) * -rl
;
492 ortho
.mtx
.p
[3][1] = (ft
+ fb
) * -tb
;
493 ortho
.mtx
.p
[3][3] = 1.0f
;
494 m4x3_identity( ortho
.transform
);
495 camera_update_view( &ortho
);
496 camera_finalize( &ortho
);
498 glDisable(GL_DEPTH_TEST
);
500 glDisable(GL_CULL_FACE
);
501 render_fb_bind( gpipeline
.fb_heightmap
);
502 shader_blitcolour_use();
503 shader_blitcolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
508 glBlendFunc(GL_ONE
, GL_ONE
);
509 glBlendEquation(GL_MAX
);
511 render_world_depth( &ortho
);
513 glEnable(GL_DEPTH_TEST
);
514 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
517 * TODO: World settings entity
519 struct ub_world_lighting
*winfo
= &gpipeline
.ub_world_lighting
;
521 if( world
.water
.enabled
)
522 v4_copy( world
.water
.plane
, winfo
->g_water_plane
);
525 v3f
*bounds
= world
.scene_geo
->bbx
;
527 info_vec
[0] = bounds
[0][0];
528 info_vec
[1] = bounds
[0][2];
529 info_vec
[2] = 1.0f
/ (bounds
[1][0]-bounds
[0][0]);
530 info_vec
[3] = 1.0f
/ (bounds
[1][2]-bounds
[0][2]);
531 v4_copy( info_vec
, winfo
->g_depth_bounds
);
533 winfo
->g_water_fog
= 0.04f
;
534 render_update_lighting_ub();
537 vg_release_thread_sync();
541 * Setup scene collider
543 reset_player( 1, (const char *[]){"start"} );
547 VG_STATIC
void world_process_resources(void)
549 vg_info( "Loading textures\n" );
550 world
.texture_count
= world
.meta
->info
.texture_count
;
552 vg_linear_alloc( world
.dynamic_vgl
, sizeof(GLuint
)*world
.texture_count
);
554 vg_acquire_thread_sync();
557 world
.textures
[0] = vg_tex2d_new();
558 vg_tex2d_set_error();
562 for( int i
=1; i
<world
.texture_count
; i
++ )
564 mdl_texture
*tex
= &world
.meta
->texture_buffer
[i
];
566 if( !tex
->pack_offset
)
568 vg_release_thread_sync();
569 vg_fatal_exit_loop( "World models must have packed textures!" );
572 vg_linear_clear( vg_mem
.scratch
);
573 world
.textures
[i
] = vg_tex2d_new();
574 vg_tex2d_set_error();
575 vg_tex2d_qoi( world
.meta
->pack
+ tex
->pack_offset
, tex
->pack_length
,
576 mdl_pstr( world
.meta
, tex
->pstr_name
));
581 vg_release_thread_sync();
583 vg_info( "Loading materials\n" );
585 u32 size
= sizeof(struct world_material
) * world
.meta
->info
.material_count
;
586 world
.materials
= vg_linear_alloc( world
.dynamic_vgl
, size
);
588 world
.material_count
= world
.meta
->info
.material_count
;
589 memset( world
.materials
, 0, size
);
591 for( int i
=1; i
<world
.material_count
; i
++ )
592 world
.materials
[i
].info
= world
.meta
->material_buffer
[i
];
595 struct world_material
*errmat
= &world
.materials
[0];
596 v4_copy( (v4f
){ 1.0f
,0.0f
,0.0f
,1.0f
}, errmat
->info
.colour
);
597 v4_copy( (v4f
){ 1.0f
,0.0f
,0.0f
,1.0f
}, errmat
->info
.colour1
);
598 errmat
->info
.flags
= 0x00;
599 errmat
->info
.pstr_name
= 0; /* useless? */
600 errmat
->info
.shader
= -1;
601 errmat
->info
.tex_decal
= 0;
602 errmat
->info
.tex_diffuse
= 0;
603 errmat
->info
.tex_normal
= 0;
606 VG_STATIC
void world_unload(void)
608 vg_acquire_thread_sync();
611 mesh_free( &world
.mesh_route_lines
);
612 mesh_free( &world
.mesh_geo
);
613 mesh_free( &world
.mesh_no_collide
);
616 world
.rewind_from
= 0.0;
617 world
.rewind_to
= 0.0;
618 world
.last_use
= 0.0;
619 world
.active_gate
= 0;
620 world
.current_run_version
= 2;
621 world
.active_route_board
= 0;
622 v3_zero( world
.render_gate_pos
);
624 for( int i
=0; i
<vg_list_size(world
.ui_bars
); i
++ )
626 struct route_ui_bar
*uib
= &world
.ui_bars
[i
];
627 uib
->segment_start
= 0;
628 uib
->segment_count
= 0;
631 uib
->fade_timer_start
= 0.0;
635 /* delete textures and meshes */
636 glDeleteTextures( world
.texture_count
, world
.textures
);
638 /* delete the entire block of memory */
639 vg_linear_clear( world
.dynamic_vgl
);
640 vg_linear_clear( world
.audio_vgl
);
642 /* clean dangling pointers */
645 world
.textures
= NULL
;
646 world
.texture_count
= 0;
647 world
.materials
= NULL
;
648 world
.material_count
= 0;
650 world
.scene_geo
= NULL
;
651 world
.scene_no_collide
= NULL
;
652 world
.scene_lines
= NULL
;
655 world
.trigger_bh
= NULL
;
656 world
.audio_bh
= NULL
;
659 world
.spawn_count
= 0;
661 world
.audio_things
= NULL
;
662 world
.audio_things_count
= 0;
664 world
.triggers
= NULL
;
665 world
.trigger_count
= 0;
667 world
.logic_relays
= NULL
;
668 world
.relay_count
= 0;
670 world
.logic_achievements
= NULL
;
671 world
.achievement_count
= 0;
674 world
.node_count
= 0;
677 world
.route_count
= 0;
680 world
.gate_count
= 0;
682 world
.collectors
= NULL
;
683 world
.collector_count
= 0;
685 world
.water
.enabled
= 0;
687 vg_release_thread_sync();
690 VG_STATIC
void world_load(void)
694 world
.meta
= mdl_load_full( world
.dynamic_vgl
, world
.world_name
);
695 vg_info( "Loading world: %s\n", world
.world_name
);
697 /* process resources from pack */
698 world_process_resources();
700 /* dynamic allocations */
701 world_ents_allocate();
702 world_routes_allocate();
704 /* meta processing */
705 world_routes_process();
706 world_entities_process();
710 world_routes_generate();
711 world_post_process();
714 #endif /* WORLD_GEN_H */