2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 VG_STATIC
void world_load( u32 index
, const char *path
);
12 VG_STATIC
void world_add_all_if_material( m4x3f transform
, scene_context
*scene
,
13 mdl_context
*mdl
, u32 id
)
15 for( u32 i
=0; i
<mdl_arrcount(&mdl
->meshs
); i
++ ){
16 mdl_mesh
*mesh
= mdl_arritm( &mdl
->meshs
, i
);
18 for( u32 j
=0; j
<mesh
->submesh_count
; j
++ ){
19 mdl_submesh
*sm
= mdl_arritm( &mdl
->submeshs
, mesh
->submesh_start
+j
);
20 if( sm
->material_id
== id
){
22 mdl_transform_m4x3( &mesh
->transform
, transform2
);
23 m4x3_mul( transform
, transform2
, transform2
);
25 scene_add_mdl_submesh( scene
, mdl
, sm
, transform2
);
31 VG_STATIC
void world_add_blob( world_instance
*world
,
32 scene_context
*scene
, ray_hit
*hit
)
35 v4f qsurface
, qrandom
;
38 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
->normal
, axis
);
40 float angle
= v3_dot(hit
->normal
,(v3f
){0.0f
,1.0f
,0.0f
});
41 q_axis_angle( qsurface
, axis
, angle
);
42 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
43 q_mul( qsurface
, qrandom
, qsurface
);
44 q_m3x3( qsurface
, transform
);
45 v3_copy( hit
->pos
, transform
[3] );
49 { .co
= { -1.00f
, 0.0f
, 0.0f
} },
50 { .co
= { 1.00f
, 0.0f
, 0.0f
} },
51 { .co
= { -1.00f
, 1.2f
, 0.0f
} },
52 { .co
= { 1.00f
, 1.2f
, 0.0f
} },
53 { .co
= { -0.25f
, 2.0f
, 0.0f
} },
54 { .co
= { 0.25f
, 2.0f
, 0.0f
} }
57 const u32 indices
[] = { 0,1,3, 0,3,2, 2,3,5, 2,5,4 };
59 if( scene
->vertex_count
+ vg_list_size(verts
) > scene
->max_vertices
)
60 vg_fatal_error( "Scene vertex buffer overflow" );
62 if( scene
->indice_count
+ vg_list_size(indices
) > scene
->max_indices
)
63 vg_fatal_error( "Scene index buffer overflow" );
65 scene_vert
*dst_verts
= &scene
->arrvertices
[ scene
->vertex_count
];
66 u32
*dst_indices
= &scene
->arrindices
[ scene
->indice_count
];
68 scene_vert
*ref
= &world
->scene_geo
.arrvertices
[ hit
->tri
[0] ];
70 for( u32 i
=0; i
<vg_list_size(verts
); i
++ )
72 scene_vert
*pvert
= &dst_verts
[ i
],
75 m4x3_mulv( transform
, src
->co
, pvert
->co
);
76 scene_vert_pack_norm( pvert
, transform
[1] );
78 v2_copy( ref
->uv
, pvert
->uv
);
81 for( u32 i
=0; i
<vg_list_size(indices
); i
++ )
82 dst_indices
[i
] = indices
[i
] + scene
->vertex_count
;
84 scene
->vertex_count
+= vg_list_size(verts
);
85 scene
->indice_count
+= vg_list_size(indices
);
88 /* Sprinkle foliage models over the map on terrain material */
89 VG_STATIC
void world_apply_procedural_foliage( world_instance
*world
,
91 struct world_surface
*mat
)
93 if( vg
.quality_profile
== k_quality_profile_low
)
96 vg_info( "Applying foliage (%u)\n", mat
->info
.pstr_name
);
99 v3_sub( world
->scene_geo
.bbx
[1], world
->scene_geo
.bbx
[0], volume
);
104 float area
= volume
[0]*volume
[2];
105 u32 particles
= 0.08f
* area
;
107 vg_info( "Map area: %f. Max particles: %u\n", area
, particles
);
109 for( u32 i
=0; i
<particles
; i
++ ){
111 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
113 v3_add( pos
, world
->scene_geo
.bbx
[0], pos
);
118 if( ray_world( world
, pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
)){
119 struct world_surface
*m1
= ray_hit_surface( world
, &hit
);
120 if((hit
.normal
[1] > 0.8f
) && (m1
== mat
) && (hit
.pos
[1] > 0.0f
+10.0f
)){
121 world_add_blob( world
, scene
, &hit
);
127 vg_info( "%d foliage models added\n", count
);
130 VG_STATIC
void world_generate( world_instance
*world
)
133 * Compile meshes into the world scenes
135 scene_init( &world
->scene_geo
, 320000, 1200000 );
136 u32 buf_size
= scene_mem_required( &world
->scene_geo
);
137 u8
*buffer
= vg_linear_alloc( world
->heap
, buf_size
);
138 scene_supply_buffer( &world
->scene_geo
, buffer
);
141 m4x3_identity( midentity
);
144 * Generate scene: collidable geometry
145 * ----------------------------------------------------------------
148 vg_info( "Generating collidable geometry\n" );
150 for( u32 i
=0; i
<world
->surface_count
; i
++ ){
151 struct world_surface
*surf
= &world
->surfaces
[ i
];
153 if( surf
->info
.flags
& k_material_flag_collision
)
154 world_add_all_if_material( midentity
, &world
->scene_geo
,
157 scene_copy_slice( &world
->scene_geo
, &surf
->sm_geo
);
160 /* compress that bad boy */
161 u32 new_vert_max
= world
->scene_geo
.vertex_count
,
162 new_vert_size
= vg_align8(new_vert_max
*sizeof(scene_vert
)),
163 new_indice_len
= world
->scene_geo
.indice_count
*sizeof(u32
);
165 u32
*src_indices
= world
->scene_geo
.arrindices
,
166 *dst_indices
= (u32
*)(buffer
+ new_vert_size
);
168 memmove( dst_indices
, src_indices
, new_indice_len
);
170 world
->scene_geo
.max_indices
= world
->scene_geo
.indice_count
;
171 world
->scene_geo
.max_vertices
= world
->scene_geo
.vertex_count
;
172 buf_size
= scene_mem_required( &world
->scene_geo
);
174 buffer
= vg_linear_resize( world
->heap
, buffer
, buf_size
);
176 world
->scene_geo
.arrvertices
= (scene_vert
*)(buffer
);
177 world
->scene_geo
.arrindices
= (u32
*)(buffer
+ new_vert_size
);
179 scene_upload_async( &world
->scene_geo
, &world
->mesh_geo
);
181 /* need send off the memory to the gpu before we can create the bvh. */
183 vg_info( "creating bvh\n" );
185 /* setup spacial mapping and rigidbody */
186 world
->geo_bh
= scene_bh_create( world
->heap
, &world
->scene_geo
);
188 v3_zero( world
->rb_geo
.rb
.co
);
189 v3_zero( world
->rb_geo
.rb
.v
);
190 q_identity( world
->rb_geo
.rb
.q
);
191 v3_zero( world
->rb_geo
.rb
.w
);
193 world
->rb_geo
.type
= k_rb_shape_scene
;
194 world
->rb_geo
.inf
.scene
.bh_scene
= world
->geo_bh
;
195 rb_init_object( &world
->rb_geo
);
198 * Generate scene: non-collidable geometry
199 * ----------------------------------------------------------------
201 vg_info( "Generating non-collidable geometry\n" );
203 vg_async_item
*call
= scene_alloc_async( &world
->scene_no_collide
,
204 &world
->mesh_no_collide
,
207 for( u32 i
=0; i
<world
->surface_count
; i
++ ){
208 struct world_surface
*surf
= &world
->surfaces
[ i
];
210 if( !(surf
->info
.flags
& k_material_flag_collision
) ){
211 world_add_all_if_material( midentity
,
212 &world
->scene_no_collide
, &world
->meta
, i
);
215 if( surf
->info
.flags
& k_material_flag_grow_grass
)
216 world_apply_procedural_foliage( world
,
217 &world
->scene_no_collide
, surf
);
219 scene_copy_slice( &world
->scene_no_collide
, &surf
->sm_no_collide
);
222 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_traffic
); i
++ ){
223 ent_traffic
*vehc
= mdl_arritm( &world
->ent_traffic
, i
);
225 for( u32 j
=0; j
<vehc
->submesh_count
; j
++ ){
226 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
227 vehc
->submesh_start
+j
);
229 if( sm
->flags
& k_submesh_flag_consumed
){
234 m4x3_identity( identity
);
235 scene_add_mdl_submesh( &world
->scene_no_collide
,
236 &world
->meta
, sm
, identity
);
238 scene_copy_slice( &world
->scene_no_collide
, sm
);
239 sm
->flags
|= k_submesh_flag_consumed
;
243 vg_async_dispatch( call
, async_scene_upload
);
246 float fsd_cone_infinite( v3f p
, v2f c
)
248 v2f q
= { v2_length( (v2f
){ p
[0], p
[2] } ), -p
[1] };
249 float s
= vg_maxf( 0.0f
, v2_dot( q
, c
) );
255 float d
= v2_length( v0
);
256 return d
* ((q
[0]*c
[1]-q
[1]*c
[0]<0.0f
)?-1.0f
:1.0f
);
259 struct light_indices_upload_info
{
260 world_instance
*world
;
266 VG_STATIC
void async_upload_light_indices( void *payload
, u32 size
)
268 struct light_indices_upload_info
*info
= payload
;
270 glGenTextures( 1, &info
->world
->tex_light_cubes
);
271 glBindTexture( GL_TEXTURE_3D
, info
->world
->tex_light_cubes
);
272 glTexImage3D( GL_TEXTURE_3D
, 0, GL_RG32UI
,
273 info
->count
[0], info
->count
[1], info
->count
[2],
274 0, GL_RG_INTEGER
, GL_UNSIGNED_INT
, info
->data
);
275 glTexParameteri( GL_TEXTURE_3D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
276 glTexParameteri( GL_TEXTURE_3D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
279 VG_STATIC
void world_compute_light_indices( world_instance
*world
)
282 v3f cubes_min
, cubes_max
;
283 v3_muls( world
->scene_geo
.bbx
[0], 1.0f
/k_light_cube_size
, cubes_min
);
284 v3_muls( world
->scene_geo
.bbx
[1], 1.0f
/k_light_cube_size
, cubes_max
);
286 v3_sub( cubes_min
, (v3f
){ 0.5f
, 0.5f
, 0.5f
}, cubes_min
);
287 v3_add( cubes_max
, (v3f
){ 0.5f
, 0.5f
, 0.5f
}, cubes_max
);
289 v3_floor( cubes_min
, cubes_min
);
290 v3_floor( cubes_max
, cubes_max
);
292 v3i icubes_min
, icubes_max
;
294 for( int i
=0; i
<3; i
++ ){
295 icubes_min
[i
] = cubes_min
[i
];
296 icubes_max
[i
] = cubes_max
[i
];
302 v3i_sub( icubes_max
, icubes_min
, icubes_count
);
304 for( int i
=0; i
<3; i
++ ){
305 int clamped_count
= VG_MIN( 128, icubes_count
[i
]+1 );
306 float clamped_max
= icubes_min
[i
] + clamped_count
,
307 max
= icubes_min
[i
] + icubes_count
[i
]+1;
309 icubes_count
[i
] = clamped_count
;
310 cube_size
[i
] = (max
/ clamped_max
) * k_light_cube_size
;
311 cubes_max
[i
] = clamped_max
;
314 v3_mul( cubes_min
, cube_size
, cubes_min
);
315 v3_mul( cubes_max
, cube_size
, cubes_max
);
317 for( int i
=0; i
<3; i
++ ){
318 float range
= cubes_max
[i
]-cubes_min
[i
];
319 world
->ub_lighting
.g_cube_inv_range
[i
] = 1.0f
/ range
;
320 world
->ub_lighting
.g_cube_inv_range
[i
] *= (float)icubes_count
[i
];
322 vg_info( "cubes[%d]: %d\n", i
, icubes_count
[i
] );
325 int total_cubes
= icubes_count
[0]*icubes_count
[1]*icubes_count
[2];
327 u32 data_size
= vg_align8(total_cubes
*sizeof(u32
)*2),
328 hdr_size
= vg_align8(sizeof(struct light_indices_upload_info
));
330 vg_async_item
*call
= vg_async_alloc( data_size
+ hdr_size
);
331 struct light_indices_upload_info
*info
= call
->payload
;
332 info
->data
= ((u8
*)call
->payload
) + hdr_size
;
334 u32
*cubes_index
= info
->data
;
336 for( int i
=0; i
<3; i
++ )
337 info
->count
[i
] = icubes_count
[i
];
339 vg_info( "Computing light cubes (%d) [%f %f %f] -> [%f %f %f]\n",
340 total_cubes
, cubes_min
[0], -cubes_min
[2], cubes_min
[1],
341 cubes_max
[0], -cubes_max
[2], cubes_max
[1] );
342 v3_copy( cubes_min
, world
->ub_lighting
.g_cube_min
);
344 float bound_radius
= v3_length( cube_size
);
346 for( int iz
= 0; iz
<icubes_count
[2]; iz
++ ){
347 for( int iy
= 0; iy
<icubes_count
[1]; iy
++ ){
348 for( int ix
= 0; ix
<icubes_count
[0]; ix
++ ){
350 v3_div( (v3f
){ ix
, iy
, iz
}, world
->ub_lighting
.g_cube_inv_range
,
352 v3_div( (v3f
){ ix
+1, iy
+1, iz
+1 },
353 world
->ub_lighting
.g_cube_inv_range
,
356 v3_add( bbx
[0], world
->ub_lighting
.g_cube_min
, bbx
[0] );
357 v3_add( bbx
[1], world
->ub_lighting
.g_cube_min
, bbx
[1] );
360 v3_add( bbx
[0], bbx
[1], center
);
361 v3_muls( center
, 0.5f
, center
);
363 u32 indices
[6] = { 0, 0, 0, 0, 0, 0 };
366 float influences
[6] = { 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, 0.0f
};
367 const int N
= vg_list_size( influences
);
369 for( u32 j
=0; j
<mdl_arrcount(&world
->ent_light
); j
++ ){
370 ent_light
*light
= mdl_arritm( &world
->ent_light
, j
);
372 closest_point_aabb( light
->transform
.co
, bbx
, closest
);
374 float dist
= v3_dist( closest
, light
->transform
.co
),
375 influence
= 1.0f
/(dist
+1.0f
);
377 if( dist
> light
->range
)
380 if( light
->type
== k_light_type_spot
){
382 m4x3_mulv( light
->inverse_world
, center
, local
);
384 float r
= fsd_cone_infinite( local
, light
->angle_sin_cos
);
386 if( r
> bound_radius
)
391 for( int k
=best_pos
-1; k
>=0; k
-- )
392 if( influence
> influences
[k
] )
396 for( int k
=N
-1; k
>best_pos
; k
-- ){
397 influences
[k
] = influences
[k
-1];
398 indices
[k
] = indices
[k
-1];
401 influences
[best_pos
] = influence
;
402 indices
[best_pos
] = j
;
406 for( int j
=0; j
<N
; j
++ )
407 if( influences
[j
] > 0.0f
)
410 int base_index
= iz
* (icubes_count
[0]*icubes_count
[1]) +
411 iy
* (icubes_count
[0]) +
414 int lower_count
= VG_MIN( 3, count
);
415 u32 packed_index_lower
= lower_count
;
416 packed_index_lower
|= indices
[0]<<2;
417 packed_index_lower
|= indices
[1]<<12;
418 packed_index_lower
|= indices
[2]<<22;
420 int upper_count
= VG_MAX( 0, count
- lower_count
);
421 u32 packed_index_upper
= upper_count
;
422 packed_index_upper
|= indices
[3]<<2;
423 packed_index_upper
|= indices
[4]<<12;
424 packed_index_upper
|= indices
[5]<<22;
426 cubes_index
[ base_index
*2 + 0 ] = packed_index_lower
;
427 cubes_index
[ base_index
*2 + 1 ] = packed_index_upper
;
432 vg_async_dispatch( call
, async_upload_light_indices
);
435 VG_STATIC
void async_world_postprocess_render( void *payload
, u32 _size
)
437 /* create scene lighting buffer */
438 world_instance
*world
= payload
;
440 u32 size
= VG_MAX(mdl_arrcount(&world
->ent_light
),1) * sizeof(float)*12;
441 vg_info( "Upload %ubytes (lighting)\n", size
);
443 glGenBuffers( 1, &world
->tbo_light_entities
);
444 glBindBuffer( GL_TEXTURE_BUFFER
, world
->tbo_light_entities
);
445 glBufferData( GL_TEXTURE_BUFFER
, size
, NULL
, GL_DYNAMIC_DRAW
);
449 * colour position direction (spots)
450 * | . . . . | . . . . | . . . . |
451 * | Re Ge Be Night | Xco Yco Zco Range | Dx Dy Dz Da |
455 v4f
*light_dst
= glMapBuffer( GL_TEXTURE_BUFFER
, GL_WRITE_ONLY
);
456 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_light
); i
++ ){
457 ent_light
*light
= mdl_arritm( &world
->ent_light
, i
);
460 v3_muls( light
->colour
, light
->colour
[3] * 2.0f
, light_dst
[i
*3+0] );
461 light_dst
[i
*3+0][3] = 2.0f
;
463 if( !light
->daytime
){
464 u32 hash
= (i
* 29986577u) & 0xffu
;
465 float switch_on
= hash
;
466 switch_on
*= (1.0f
/255.0f
);
468 light_dst
[i
*3+0][3] = 0.44f
+ switch_on
* 0.015f
;
471 /* position + 1/range^2 */
472 v3_copy( light
->transform
.co
, light_dst
[i
*3+1] );
473 light_dst
[i
*3+1][3] = 1.0f
/(light
->range
*light
->range
);
475 /* direction + angle */
476 q_mulv( light
->transform
.q
, (v3f
){0.0f
,-1.0f
,0.0f
}, light_dst
[i
*3+2]);
477 light_dst
[i
*3+2][3] = cosf( light
->angle
);
480 glUnmapBuffer( GL_TEXTURE_BUFFER
);
482 glGenTextures( 1, &world
->tex_light_entities
);
483 glBindTexture( GL_TEXTURE_BUFFER
, world
->tex_light_entities
);
484 glTexBuffer( GL_TEXTURE_BUFFER
, GL_RGBA32F
, world
->tbo_light_entities
);
486 /* Upload lighting uniform buffer */
487 if( world
->water
.enabled
)
488 v4_copy( world
->water
.plane
, world
->ub_lighting
.g_water_plane
);
491 v3f
*bounds
= world
->scene_geo
.bbx
;
493 info_vec
[0] = bounds
[0][0];
494 info_vec
[1] = bounds
[0][2];
495 info_vec
[2] = 1.0f
/ (bounds
[1][0]-bounds
[0][0]);
496 info_vec
[3] = 1.0f
/ (bounds
[1][2]-bounds
[0][2]);
497 v4_copy( info_vec
, world
->ub_lighting
.g_depth_bounds
);
500 * Rendering the depth map
505 v3_sub( world
->scene_geo
.bbx
[1], world
->scene_geo
.bbx
[0], extent
);
507 float fl
= world
->scene_geo
.bbx
[0][0],
508 fr
= world
->scene_geo
.bbx
[1][0],
509 fb
= world
->scene_geo
.bbx
[0][2],
510 ft
= world
->scene_geo
.bbx
[1][2],
514 m4x4_zero( ortho
.mtx
.p
);
515 ortho
.mtx
.p
[0][0] = 2.0f
* rl
;
516 ortho
.mtx
.p
[2][1] = 2.0f
* tb
;
517 ortho
.mtx
.p
[3][0] = (fr
+ fl
) * -rl
;
518 ortho
.mtx
.p
[3][1] = (ft
+ fb
) * -tb
;
519 ortho
.mtx
.p
[3][3] = 1.0f
;
520 m4x3_identity( ortho
.transform
);
521 camera_update_view( &ortho
);
522 camera_finalize( &ortho
);
524 glDisable(GL_DEPTH_TEST
);
526 glDisable(GL_CULL_FACE
);
527 render_fb_bind( &world
->heightmap
, 0 );
528 shader_blitcolour_use();
529 shader_blitcolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
533 glBlendFunc(GL_ONE
, GL_ONE
);
534 glBlendEquation(GL_MAX
);
536 render_world_position( world
, &ortho
);
538 glEnable(GL_DEPTH_TEST
);
539 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
541 /* upload full buffer */
542 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
543 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
544 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
547 VG_STATIC
int reset_player( int argc
, char const *argv
[] );
548 VG_STATIC
void world_post_process( world_instance
*world
)
550 world_compute_light_indices( world
);
552 vg_async_item
*call
= vg_async_alloc(0);
553 call
->payload
= world
;
554 vg_async_dispatch( call
, async_world_postprocess_render
);
557 VG_STATIC
void world_process_resources( world_instance
*world
)
559 vg_info( "Loading textures\n" );
561 world
->texture_count
= 0;
563 world
->texture_count
= world
->meta
.textures
.count
+1;
564 world
->textures
= vg_linear_alloc( world
->heap
,
565 vg_align8(sizeof(GLuint
)*world
->texture_count
) );
567 vg_tex2d_replace_with_error( &world
->textures
[0] );
569 for( u32 i
=0; i
<mdl_arrcount(&world
->meta
.textures
); i
++ ){
570 mdl_texture
*tex
= mdl_arritm( &world
->meta
.textures
, i
);
572 if( !tex
->file
.pack_size
){
573 vg_fatal_error( "World models must have packed textures!" );
576 vg_linear_clear( vg_mem
.scratch
);
577 void *src_data
= vg_linear_alloc( vg_mem
.scratch
,
578 tex
->file
.pack_size
);
579 mdl_fread_pack_file( &world
->meta
, &tex
->file
, src_data
);
581 vg_tex2d_load_qoi_async( src_data
, tex
->file
.pack_size
,
582 VG_TEX2D_NEAREST
|VG_TEX2D_REPEAT
,
583 &world
->textures
[i
+1] );
586 vg_info( "Loading materials\n" );
588 world
->surface_count
= world
->meta
.materials
.count
+1;
589 world
->surfaces
= vg_linear_alloc( world
->heap
,
590 vg_align8(sizeof(struct world_surface
)*world
->surface_count
) );
593 struct world_surface
*errmat
= &world
->surfaces
[0];
594 memset( errmat
, 0, sizeof(struct world_surface
) );
596 for( u32 i
=0; i
<mdl_arrcount(&world
->meta
.materials
); i
++ ){
597 world
->surfaces
[i
+1].info
=
598 *(mdl_material
*)mdl_arritm( &world
->meta
.materials
, i
);
603 VG_STATIC
void world_free( world_instance
*world
)
605 vg_acquire_thread_sync();
608 mesh_free( &world
->mesh_route_lines
);
609 mesh_free( &world
->mesh_geo
);
610 mesh_free( &world
->mesh_no_collide
);
612 /* glDeleteBuffers silently ignores 0's and names that do not correspond to
613 * existing buffer objects.
615 glDeleteBuffers( 1, &world
->tbo_light_entities
);
616 glDeleteTextures( 1, &world
->tex_light_entities
);
617 glDeleteTextures( 1, &world
->tex_light_cubes
);
619 /* delete textures and meshes */
620 glDeleteTextures( world
->texture_count
, world
->textures
);
622 vg_release_thread_sync();
624 world
->status
= k_world_status_unloaded
;
628 VG_STATIC
void world_init_blank( world_instance
*world
)
630 memset( &world
->meta
, 0, sizeof(mdl_context
) );
632 world
->textures
= NULL
;
633 world
->texture_count
= 0;
634 world
->surfaces
= NULL
;
635 world
->surface_count
= 0;
637 world
->geo_bh
= NULL
;
638 world
->volume_bh
= NULL
;
639 world
->audio_bh
= NULL
;
640 world
->rendering_gate
= NULL
;
642 world
->water
.enabled
= 0;
644 /* default lighting conditions
645 * -------------------------------------------------------------*/
646 struct ub_world_lighting
*state
= &world
->ub_lighting
;
648 state
->g_light_preview
= 0;
649 state
->g_shadow_samples
= 8;
650 state
->g_water_fog
= 0.04f
;
652 v4_zero( state
->g_water_plane
);
653 v4_zero( state
->g_depth_bounds
);
655 state
->g_shadow_length
= 9.50f
;
656 state
->g_shadow_spread
= 0.65f
;
658 v3_copy( (v3f
){0.37f
, 0.54f
, 0.97f
}, state
->g_daysky_colour
);
659 v3_copy( (v3f
){0.03f
, 0.05f
, 0.20f
}, state
->g_nightsky_colour
);
660 v3_copy( (v3f
){1.00f
, 0.32f
, 0.01f
}, state
->g_sunset_colour
);
661 v3_copy( (v3f
){0.13f
, 0.17f
, 0.35f
}, state
->g_ambient_colour
);
662 v3_copy( (v3f
){0.25f
, 0.17f
, 0.51f
}, state
->g_sunset_ambient
);
663 v3_copy( (v3f
){1.10f
, 0.89f
, 0.35f
}, state
->g_sun_colour
);
666 VG_STATIC
void world_entities_init( u32 world_id
)
668 world_instance
*world
= &world_global
.worlds
[world_id
];
671 for( u32 j
=0; j
<mdl_arrcount(&world
->ent_light
); j
++ ){
672 ent_light
*light
= mdl_arritm( &world
->ent_light
, j
);
675 q_m3x3( light
->transform
.q
, to_world
);
676 v3_copy( light
->transform
.co
, to_world
[3] );
677 m4x3_invert_affine( to_world
, light
->inverse_world
);
679 light
->angle_sin_cos
[0] = sinf( light
->angle
* 0.5f
);
680 light
->angle_sin_cos
[1] = cosf( light
->angle
* 0.5f
);
684 for( u32 j
=0; j
<mdl_arrcount(&world
->ent_gate
); j
++ ){
685 ent_gate
*gate
= mdl_arritm( &world
->ent_gate
, j
);
687 if( gate
->type
== k_gate_type_teleport
){
688 gate_transform_update( gate
);
690 else if( gate
->type
== k_gate_type_nonlocal_unlinked
){
691 const char *key
= mdl_pstr( &world
->meta
, gate
->target
);
692 vg_info( "key: %s\n", key
);
694 for( u32 i
=0; i
<vg_list_size(world_global
.worlds
); i
++ ){
695 world_instance
*other
= &world_global
.worlds
[i
];
696 if( other
== world
) continue;
697 if( other
->status
!= k_world_status_loaded
) continue;
698 vg_info( "Checking world %u for key matches\n", i
);
700 for( u32 j
=0; j
<mdl_arrcount( &other
->ent_gate
); j
++ ){
701 ent_gate
*gate2
= mdl_arritm( &other
->ent_gate
, j
);
702 if( gate2
->type
!= k_gate_type_nonlocal_unlinked
) continue;
704 const char *key2
= mdl_pstr( &other
->meta
, gate2
->target
);
705 vg_info( " key2: %s\n", key2
);
707 if( strcmp( key
, key2
) ) continue;
709 vg_success( "Non-local matching pair '%s' found. (%u:%u)\n",
712 gate
->type
= k_gate_type_nonlocel
;
713 gate2
->type
= k_gate_type_nonlocel
;
715 gate2
->target
= world_id
;
717 v3_copy( gate
->co
[0], gate2
->co
[1] );
718 v3_copy( gate2
->co
[0], gate
->co
[1] );
719 v4_copy( gate
->q
[0], gate2
->q
[1] );
720 v4_copy( gate2
->q
[0], gate
->q
[1] );
723 q_axis_angle( qflip
, (v3f
){0.0f
,1.0f
,0.0f
}, VG_PIf
);
724 q_mul( gate
->q
[0], qflip
, gate
->q
[0] );
725 q_mul( gate
->q
[1], qflip
, gate
->q
[1] );
727 gate_transform_update( gate
);
728 gate_transform_update( gate2
);
738 for( u32 j
=0; j
<mdl_arrcount(&world
->ent_water
); j
++ ){
739 ent_water
*water
= mdl_arritm( &world
->ent_water
, j
);
740 if( world
->water
.enabled
){
741 vg_warn( "Multiple water surfaces in level!\n" );
745 world
->water
.enabled
= 1;
746 water_set_surface( world
, water
->transform
.co
[1] );
750 for( u32 j
=0; j
<mdl_arrcount(&world
->ent_volume
); j
++ ){
751 ent_volume
*volume
= mdl_arritm( &world
->ent_volume
, j
);
752 mdl_transform_m4x3( &volume
->transform
, volume
->to_world
);
753 m4x3_invert_full( volume
->to_world
, volume
->to_local
);
757 for( u32 j
=0; j
<mdl_arrcount(&world
->ent_audio
); j
++ ){
758 ent_audio
*audio
= mdl_arritm( &world
->ent_audio
, j
);
760 for( u32 k
=0; k
<audio
->clip_count
; k
++ ){
761 ent_audio_clip
*clip
= mdl_arritm( &world
->ent_audio_clip
,
762 audio
->clip_start
+k
);
764 if( clip
->file
.pack_size
){
765 u32 size
= clip
->file
.pack_size
,
766 offset
= clip
->file
.pack_offset
;
768 /* embedded files are fine to clear the scratch buffer, only
769 * external audio uses it */
771 vg_linear_clear( vg_mem
.scratch
);
772 void *data
= vg_linear_alloc( vg_mem
.scratch
,
773 clip
->file
.pack_size
);
775 mdl_fread_pack_file( &world
->meta
, &clip
->file
, data
);
777 clip
->clip
.path
= NULL
;
778 clip
->clip
.flags
= audio
->flags
;
779 clip
->clip
.data
= data
;
780 clip
->clip
.size
= size
;
783 clip
->clip
.path
= mdl_pstr( &world
->meta
, clip
->file
.pstr_path
);
784 clip
->clip
.flags
= audio
->flags
;
785 clip
->clip
.data
= NULL
;
789 audio_clip_load( &clip
->clip
, world
->heap
);
794 VG_STATIC
void world_load( u32 index
, const char *path
)
796 world_instance
*world
= &world_global
.worlds
[index
];
797 world_init_blank( world
);
798 world
->status
= k_world_status_loading
;
800 vg_info( "Loading world: %s\n", path
);
802 void *allocator
= NULL
;
803 if( index
== 0 ) allocator
= world_global
.heap
;
804 else allocator
= world_global
.worlds
[index
-1].heap
;
806 u32 heap_availible
= vg_linear_remaining( allocator
);
807 u32 min_overhead
= sizeof(vg_linear_allocator
);
809 if( heap_availible
< (min_overhead
+1024) ){
810 vg_fatal_error( "out of memory" );
813 u32 size
= heap_availible
- min_overhead
;
814 void *heap
= vg_create_linear_allocator( allocator
, size
,
818 mdl_context
*meta
= &world
->meta
;
820 mdl_open( meta
, path
, world
->heap
);
821 mdl_load_metadata_block( meta
, world
->heap
);
822 mdl_load_animation_block( meta
, world
->heap
);
823 mdl_load_mesh_block( meta
, world
->heap
);
825 mdl_load_array( meta
, &world
->ent_gate
, "ent_gate", heap
);
826 mdl_load_array( meta
, &world
->ent_spawn
, "ent_spawn", heap
);
827 mdl_load_array( meta
, &world
->ent_light
, "ent_light", heap
);
828 mdl_load_array( meta
, &world
->ent_route_node
,"ent_route_node", heap
);
829 mdl_load_array( meta
, &world
->ent_path_index
,"ent_path_index", heap
);
830 mdl_load_array( meta
, &world
->ent_checkpoint
,"ent_checkpoint", heap
);
831 mdl_load_array( meta
, &world
->ent_route
, "ent_route", heap
);
832 mdl_load_array( meta
, &world
->ent_water
, "ent_water", heap
);
833 mdl_load_array( meta
, &world
->ent_audio_clip
,"ent_audio_clip", heap
);
834 mdl_load_array( meta
, &world
->ent_audio
, "ent_audio", heap
);
835 mdl_load_array( meta
, &world
->ent_volume
, "ent_volume", heap
);
836 mdl_load_array( meta
, &world
->ent_traffic
, "ent_traffic", heap
);
838 /* process resources from pack */
839 world_process_resources( world
);
841 world_routes_ent_init( world
);
842 world_entities_init( index
);
843 world
->volume_bh
= bh_create( heap
, &bh_system_volumes
, world
,
844 mdl_arrcount( &world
->ent_volume
), 1 );
847 world_generate( world
);
848 world_routes_generate( world
);
849 world_post_process( world
);
853 world
->status
= k_world_status_loaded
;
856 #endif /* WORLD_GEN_H */