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
)
187 k_classtype_point_light
,
188 (void*)&world
.lights
,
189 sizeof(struct world_light
)
193 for( int i
=0; i
<vg_list_size(entity_counts
); i
++ )
194 entity_counts
[i
].count
= 0;
196 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
198 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
200 for( int j
=0; j
<vg_list_size(entity_counts
); j
++ )
202 if( pnode
->classtype
== entity_counts
[j
].ct
)
204 pnode
->sub_uid
= entity_counts
[j
].count
;
205 entity_counts
[j
].count
++;
211 for( int i
=0; i
<vg_list_size(entity_counts
); i
++ )
213 struct countable
*counter
= &entity_counts
[i
];
215 u32 bufsize
= counter
->item_size
*counter
->count
;
216 *counter
->to_allocate
= vg_linear_alloc( world
.dynamic_vgl
, bufsize
);
220 VG_STATIC
void world_pct_spawn( mdl_node
*pnode
)
222 struct respawn_point
*rp
= &world
.spawns
[ world
.spawn_count
++ ];
224 v3_copy( pnode
->co
, rp
->co
);
225 v4_copy( pnode
->q
, rp
->q
);
226 rp
->name
= mdl_pstr( world
.meta
, pnode
->pstr_name
);
229 VG_STATIC
void world_pct_water( mdl_node
*pnode
)
231 if( world
.water
.enabled
)
233 vg_warn( "Multiple water surfaces in level! ('%s')\n",
234 mdl_pstr( world
.meta
, pnode
->pstr_name
));
238 world
.water
.enabled
= 1;
239 water_set_surface( pnode
->co
[1] );
242 VG_STATIC
void world_pct_audio( mdl_node
*pnode
)
244 struct world_audio_thing
*thing
= &world
.audio_things
[
245 world
.audio_things_count
];
247 memset( thing
, 0, sizeof(struct world_audio_thing
) );
248 struct classtype_audio
*aud
= mdl_get_entdata( world
.meta
, pnode
);
250 v3_copy( pnode
->co
, thing
->pos
);
252 if( aud
->flags
& AUDIO_FLAG_SPACIAL_3D
)
253 thing
->volume
= aud
->volume
* pnode
->s
[0];
255 thing
->volume
= aud
->volume
;
257 thing
->flags
= aud
->flags
;
258 thing
->temp_embedded_clip
.path
= mdl_pstr( world
.meta
, aud
->pstr_file
);
259 thing
->temp_embedded_clip
.source_mode
= k_audio_source_mono
;
261 audio_clip_load( &thing
->temp_embedded_clip
, world
.audio_vgl
);
262 thing
->player
.name
= mdl_pstr( world
.meta
, pnode
->pstr_name
);
263 thing
->player
.enqued
= 0;
265 pnode
->sub_uid
= world
.audio_things_count
;
266 world
.audio_things_count
++;
270 VG_STATIC
void world_pct_trigger( mdl_node
*pnode
)
272 struct trigger_zone
*trigger
= &world
.triggers
[ world
.trigger_count
];
273 struct classtype_trigger
*inf
= mdl_get_entdata( world
.meta
, pnode
);
277 mdl_node
*target_node
= mdl_node_from_id( world
.meta
, inf
->target
);
279 trigger
->target
.sub_id
= target_node
->sub_uid
;
280 trigger
->target
.classtype
= target_node
->classtype
;
284 vg_warn( "Trigger with no target...\n" );
288 mdl_node_transform( pnode
, trigger
->transform
);
289 m4x3_invert_full( trigger
->transform
, trigger
->inv_transform
);
291 world
.trigger_count
++;
295 VG_STATIC
void world_pct_relay( mdl_node
*pnode
)
297 struct logic_relay
*relay
= &world
.logic_relays
[ world
.relay_count
];
298 struct classtype_logic_relay
*inf
= mdl_get_entdata( world
.meta
, pnode
);
300 relay
->target_count
= 0;
302 for( int i
=0; i
<vg_list_size(relay
->targets
); i
++ )
304 if( inf
->targets
[i
] )
306 struct relay_target
*target
= &relay
->targets
[relay
->target_count
++];
307 mdl_node
*other
= mdl_node_from_id( world
.meta
, inf
->targets
[i
] );
309 target
->classtype
= other
->classtype
;
310 target
->sub_id
= other
->sub_uid
;
314 v3_copy( pnode
->co
, relay
->pos
);
315 world
.relay_count
++;
319 VG_STATIC
void world_pct_achievement( mdl_node
*pnode
)
321 struct logic_achievement
*ach
=
322 &world
.logic_achievements
[ world
.achievement_count
];
323 struct classtype_logic_achievement
*inf
=
324 mdl_get_entdata( world
.meta
, pnode
);
326 v3_copy( pnode
->co
, ach
->pos
);
327 ach
->achievement_id
= mdl_pstr( world
.meta
, inf
->pstr_name
);
330 world
.achievement_count
++;
333 VG_STATIC
void world_pct_point_light( mdl_node
*pnode
)
335 struct world_light
*light
= &world
.lights
[ world
.light_count
];
336 v3_copy( pnode
->co
, light
->co
);
338 struct classtype_point_light
*inf
= mdl_get_entdata( world
.meta
, pnode
);
339 v4_copy( inf
->colour
, light
->colour
);
341 world
.light_count
++;
344 VG_STATIC
void world_entities_process(void)
346 struct entity_instruction
349 void (*process
)( mdl_node
*pnode
);
351 entity_instructions
[] =
353 { k_classtype_spawn
, world_pct_spawn
},
354 { k_classtype_water
, world_pct_water
},
355 { k_classtype_audio
, world_pct_audio
},
356 { k_classtype_trigger
, world_pct_trigger
},
357 { k_classtype_logic_relay
, world_pct_relay
},
358 { k_classtype_logic_achievement
, world_pct_achievement
},
359 { k_classtype_point_light
, world_pct_point_light
}
362 for( int i
=0; i
<world
.meta
->info
.node_count
; i
++ )
364 mdl_node
*pnode
= mdl_node_from_id( world
.meta
, i
);
366 for( int j
=0; j
<vg_list_size(entity_instructions
); j
++ )
368 struct entity_instruction
*instr
= &entity_instructions
[j
];
370 if( pnode
->classtype
== instr
->ct
)
372 instr
->process( pnode
);
379 VG_STATIC
void world_scene_compute_light_clusters( scene
*sc
)
381 for( int i
=0; i
<sc
->vertex_count
; i
++ )
383 scene_vert
*vert
= &sc
->arrvertices
[i
];
384 vert
->lights
[0] = 255;
385 vert
->lights
[1] = 255;
386 vert
->lights
[2] = 255;
387 vert
->lights
[3] = 255;
389 float distances
[4] = { INFINITY
, INFINITY
, INFINITY
, INFINITY
};
391 for( int j
=0; j
<world
.light_count
; j
++ )
393 float dist
= v3_dist2( world
.lights
[j
].co
, vert
->co
);
396 for( int k
=best_pos
-1; k
>=0; k
-- )
397 if( dist
< distances
[k
] )
402 for( int k
=3; k
>best_pos
; k
-- )
404 distances
[k
] = distances
[k
-1];
405 vert
->lights
[k
] = vert
->lights
[k
-1];
408 distances
[best_pos
] = dist
;
409 vert
->lights
[best_pos
] = j
;
415 VG_STATIC
void world_generate(void)
418 * Compile meshes into the world scenes
420 world
.scene_geo
= scene_init( world
.dynamic_vgl
, 320000, 1200000 );
423 m4x3_identity( midentity
);
426 * Generate scene: collidable geometry
427 * ----------------------------------------------------------------
430 vg_info( "Generating collidable geometry\n" );
433 for( int i
=0; i
<world
.material_count
; i
++ )
435 struct world_material
*mat
= &world
.materials
[ i
];
437 if( mat
->info
.flags
& k_material_flag_collision
)
438 world_add_all_if_material( midentity
, world
.scene_geo
, world
.meta
, i
);
440 scene_copy_slice( world
.scene_geo
, &mat
->sm_geo
);
442 world_scene_compute_light_clusters( world
.scene_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
);
487 world_scene_compute_light_clusters( world
.scene_no_collide
);
489 /* upload and free that */
490 vg_acquire_thread_sync();
492 scene_upload( world
.scene_no_collide
, &world
.mesh_no_collide
);
494 vg_release_thread_sync();
496 vg_linear_del( world
.dynamic_vgl
, world
.scene_no_collide
);
497 world
.scene_no_collide
= NULL
;
500 VG_STATIC
int reset_player( int argc
, char const *argv
[] );
501 VG_STATIC
void world_post_process(void)
503 /* initialize audio if need be */
505 for( int i
=0; i
<world
.audio_things_count
; i
++ )
507 struct world_audio_thing
*thingy
= &world
.audio_things
[ i
];
509 audio_player_init( &thingy
->player
);
510 audio_player_set_flags( &thingy
->player
, thingy
->flags
);
511 audio_player_set_vol( &thingy
->player
, thingy
->volume
);
512 audio_player_set_pan( &thingy
->player
, 0.0f
);
514 if( thingy
->flags
& AUDIO_FLAG_SPACIAL_3D
)
515 audio_player_set_position( &thingy
->player
, thingy
->pos
);
517 if( thingy
->flags
& AUDIO_FLAG_AUTO_START
)
518 audio_player_playclip( &thingy
->player
, &thingy
->temp_embedded_clip
);
522 vg_acquire_thread_sync();
525 * Rendering the depth map
530 v3_sub( world
.scene_geo
->bbx
[1], world
.scene_geo
->bbx
[0], extent
);
532 float fl
= world
.scene_geo
->bbx
[0][0],
533 fr
= world
.scene_geo
->bbx
[1][0],
534 fb
= world
.scene_geo
->bbx
[0][2],
535 ft
= world
.scene_geo
->bbx
[1][2],
539 m4x4_zero( ortho
.mtx
.p
);
540 ortho
.mtx
.p
[0][0] = 2.0f
* rl
;
541 ortho
.mtx
.p
[2][1] = 2.0f
* tb
;
542 ortho
.mtx
.p
[3][0] = (fr
+ fl
) * -rl
;
543 ortho
.mtx
.p
[3][1] = (ft
+ fb
) * -tb
;
544 ortho
.mtx
.p
[3][3] = 1.0f
;
545 m4x3_identity( ortho
.transform
);
546 camera_update_view( &ortho
);
547 camera_finalize( &ortho
);
549 glDisable(GL_DEPTH_TEST
);
551 glDisable(GL_CULL_FACE
);
552 render_fb_bind( gpipeline
.fb_heightmap
);
553 shader_blitcolour_use();
554 shader_blitcolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
559 glBlendFunc(GL_ONE
, GL_ONE
);
560 glBlendEquation(GL_MAX
);
562 render_world_depth( &ortho
);
564 glEnable(GL_DEPTH_TEST
);
565 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
568 * TODO: World settings entity
570 struct ub_world_lighting
*winfo
= &gpipeline
.ub_world_lighting
;
572 if( world
.water
.enabled
)
573 v4_copy( world
.water
.plane
, winfo
->g_water_plane
);
576 v3f
*bounds
= world
.scene_geo
->bbx
;
578 info_vec
[0] = bounds
[0][0];
579 info_vec
[1] = bounds
[0][2];
580 info_vec
[2] = 1.0f
/ (bounds
[1][0]-bounds
[0][0]);
581 info_vec
[3] = 1.0f
/ (bounds
[1][2]-bounds
[0][2]);
582 v4_copy( info_vec
, winfo
->g_depth_bounds
);
584 winfo
->g_water_fog
= 0.04f
;
586 for( int i
=0; i
<world
.light_count
; i
++ )
588 struct world_light
*light
= &world
.lights
[i
];
590 v3_muls( light
->colour
, light
->colour
[3] * 2.0f
,
591 gpipeline
.ub_world_lighting
.g_point_light_colours
[i
] );
593 gpipeline
.ub_world_lighting
.g_point_light_positions
[i
] );
596 render_update_lighting_ub();
599 vg_release_thread_sync();
603 * Setup scene collider
605 reset_player( 1, (const char *[]){"start"} );
609 VG_STATIC
void world_process_resources(void)
611 vg_info( "Loading textures\n" );
612 world
.texture_count
= world
.meta
->info
.texture_count
;
614 vg_linear_alloc( world
.dynamic_vgl
, sizeof(GLuint
)*world
.texture_count
);
616 vg_acquire_thread_sync();
619 world
.textures
[0] = vg_tex2d_new();
620 vg_tex2d_set_error();
624 for( int i
=1; i
<world
.texture_count
; i
++ )
626 mdl_texture
*tex
= &world
.meta
->texture_buffer
[i
];
628 if( !tex
->pack_offset
)
630 vg_release_thread_sync();
631 vg_fatal_exit_loop( "World models must have packed textures!" );
634 vg_linear_clear( vg_mem
.scratch
);
635 world
.textures
[i
] = vg_tex2d_new();
636 vg_tex2d_set_error();
637 vg_tex2d_qoi( world
.meta
->pack
+ tex
->pack_offset
, tex
->pack_length
,
638 mdl_pstr( world
.meta
, tex
->pstr_name
));
643 vg_release_thread_sync();
645 vg_info( "Loading materials\n" );
647 u32 size
= sizeof(struct world_material
) * world
.meta
->info
.material_count
;
648 world
.materials
= vg_linear_alloc( world
.dynamic_vgl
, size
);
650 world
.material_count
= world
.meta
->info
.material_count
;
651 memset( world
.materials
, 0, size
);
653 for( int i
=1; i
<world
.material_count
; i
++ )
654 world
.materials
[i
].info
= world
.meta
->material_buffer
[i
];
657 struct world_material
*errmat
= &world
.materials
[0];
658 v4_copy( (v4f
){ 1.0f
,0.0f
,0.0f
,1.0f
}, errmat
->info
.colour
);
659 v4_copy( (v4f
){ 1.0f
,0.0f
,0.0f
,1.0f
}, errmat
->info
.colour1
);
660 errmat
->info
.flags
= 0x00;
661 errmat
->info
.pstr_name
= 0; /* useless? */
662 errmat
->info
.shader
= -1;
663 errmat
->info
.tex_decal
= 0;
664 errmat
->info
.tex_diffuse
= 0;
665 errmat
->info
.tex_normal
= 0;
668 VG_STATIC
void world_unload(void)
670 vg_acquire_thread_sync();
673 mesh_free( &world
.mesh_route_lines
);
674 mesh_free( &world
.mesh_geo
);
675 mesh_free( &world
.mesh_no_collide
);
678 world
.rewind_from
= 0.0;
679 world
.rewind_to
= 0.0;
680 world
.last_use
= 0.0;
681 world
.active_gate
= 0;
682 world
.current_run_version
= 2;
683 world
.active_route_board
= 0;
684 v3_zero( world
.render_gate_pos
);
686 for( int i
=0; i
<vg_list_size(world
.ui_bars
); i
++ )
688 struct route_ui_bar
*uib
= &world
.ui_bars
[i
];
689 uib
->segment_start
= 0;
690 uib
->segment_count
= 0;
693 uib
->fade_timer_start
= 0.0;
697 /* delete textures and meshes */
698 glDeleteTextures( world
.texture_count
, world
.textures
);
700 /* delete the entire block of memory */
701 vg_linear_clear( world
.dynamic_vgl
);
702 vg_linear_clear( world
.audio_vgl
);
704 /* clean dangling pointers */
707 world
.textures
= NULL
;
708 world
.texture_count
= 0;
709 world
.materials
= NULL
;
710 world
.material_count
= 0;
712 world
.scene_geo
= NULL
;
713 world
.scene_no_collide
= NULL
;
714 world
.scene_lines
= NULL
;
717 world
.trigger_bh
= NULL
;
718 world
.audio_bh
= NULL
;
721 world
.spawn_count
= 0;
723 world
.audio_things
= NULL
;
724 world
.audio_things_count
= 0;
726 world
.triggers
= NULL
;
727 world
.trigger_count
= 0;
730 world
.light_count
= 0;
732 world
.logic_relays
= NULL
;
733 world
.relay_count
= 0;
735 world
.logic_achievements
= NULL
;
736 world
.achievement_count
= 0;
739 world
.node_count
= 0;
742 world
.route_count
= 0;
745 world
.gate_count
= 0;
747 world
.collectors
= NULL
;
748 world
.collector_count
= 0;
750 world
.water
.enabled
= 0;
752 vg_release_thread_sync();
755 VG_STATIC
void world_load(void)
759 world
.meta
= mdl_load_full( world
.dynamic_vgl
, world
.world_name
);
760 vg_info( "Loading world: %s\n", world
.world_name
);
762 /* process resources from pack */
763 world_process_resources();
765 /* dynamic allocations */
766 world_ents_allocate();
767 world_routes_allocate();
769 /* meta processing */
770 world_routes_process();
771 world_entities_process();
775 world_routes_generate();
776 world_post_process();
779 #endif /* WORLD_GEN_H */