semantics and world reloading
[carveJwlIkooP6JGAAIwe30JlM.git] / world_gen.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef WORLD_GEN_H
6 #define WORLD_GEN_H
7
8 #include "world.h"
9
10 VG_STATIC void world_load( u32 index, const char *path );
11
12 VG_STATIC void world_add_all_if_material( m4x3f transform, scene_context *scene,
13 mdl_context *mdl, u32 id )
14 {
15 for( u32 i=0; i<mdl_arrcount(&mdl->meshs); i++ ){
16 mdl_mesh *mesh = mdl_arritm( &mdl->meshs, i );
17
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 ){
21 m4x3f transform2;
22 mdl_transform_m4x3( &mesh->transform, transform2 );
23 m4x3_mul( transform, transform2, transform2 );
24
25 scene_add_mdl_submesh( scene, mdl, sm, transform2 );
26 }
27 }
28 }
29 }
30
31 VG_STATIC void world_add_blob( world_instance *world,
32 scene_context *scene, ray_hit *hit )
33 {
34 m4x3f transform;
35 v4f qsurface, qrandom;
36 v3f axis;
37
38 v3_cross( (v3f){0.0f,1.0f,0.0f}, hit->normal, axis );
39
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_randf64()*VG_TAUf );
43 q_mul( qsurface, qrandom, qsurface );
44 q_m3x3( qsurface, transform );
45 v3_copy( hit->pos, transform[3] );
46
47 scene_vert verts[] =
48 {
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 } }
55 };
56
57 const u32 indices[] = { 0,1,3, 0,3,2, 2,3,5, 2,5,4 };
58
59 if( scene->vertex_count + vg_list_size(verts) > scene->max_vertices )
60 vg_fatal_error( "Scene vertex buffer overflow" );
61
62 if( scene->indice_count + vg_list_size(indices) > scene->max_indices )
63 vg_fatal_error( "Scene index buffer overflow" );
64
65 scene_vert *dst_verts = &scene->arrvertices[ scene->vertex_count ];
66 u32 *dst_indices = &scene->arrindices [ scene->indice_count ];
67
68 scene_vert *ref = &world->scene_geo.arrvertices[ hit->tri[0] ];
69
70 for( u32 i=0; i<vg_list_size(verts); i++ )
71 {
72 scene_vert *pvert = &dst_verts[ i ],
73 *src = &verts[ i ];
74
75 m4x3_mulv( transform, src->co, pvert->co );
76 scene_vert_pack_norm( pvert, transform[1] );
77
78 v2_copy( ref->uv, pvert->uv );
79 }
80
81 for( u32 i=0; i<vg_list_size(indices); i++ )
82 dst_indices[i] = indices[i] + scene->vertex_count;
83
84 scene->vertex_count += vg_list_size(verts);
85 scene->indice_count += vg_list_size(indices);
86 }
87
88 /* Sprinkle foliage models over the map on terrain material */
89 VG_STATIC void world_apply_procedural_foliage( world_instance *world,
90 scene_context *scene,
91 struct world_surface *mat )
92 {
93 if( vg.quality_profile == k_quality_profile_low )
94 return;
95
96 vg_info( "Applying foliage (%u)\n", mat->info.pstr_name );
97
98 v3f volume;
99 v3_sub( world->scene_geo.bbx[1], world->scene_geo.bbx[0], volume );
100 volume[1] = 1.0f;
101
102 int count = 0;
103
104 float area = volume[0]*volume[2];
105 u32 particles = 0.08f * area;
106
107 vg_info( "Map area: %f. Max particles: %u\n", area, particles );
108
109 for( u32 i=0; i<particles; i++ ){
110 v3f pos;
111 v3_mul( volume, (v3f){ vg_randf64(), 1000.0f, vg_randf64() }, pos );
112 pos[1] = 1000.0f;
113 v3_add( pos, world->scene_geo.bbx[0], pos );
114
115 ray_hit hit;
116 hit.dist = INFINITY;
117
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 );
122 count ++;
123 }
124 }
125 }
126
127 vg_info( "%d foliage models added\n", count );
128 }
129
130 VG_STATIC void world_generate( world_instance *world )
131 {
132 /*
133 * Compile meshes into the world scenes
134 */
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 );
139
140 m4x3f midentity;
141 m4x3_identity( midentity );
142
143 /*
144 * Generate scene: collidable geometry
145 * ----------------------------------------------------------------
146 */
147
148 vg_info( "Generating collidable geometry\n" );
149
150 for( u32 i=0; i<world->surface_count; i++ ){
151 struct world_surface *surf = &world->surfaces[ i ];
152
153 if( surf->info.flags & k_material_flag_collision )
154 world_add_all_if_material( midentity, &world->scene_geo,
155 &world->meta, i );
156
157 scene_copy_slice( &world->scene_geo, &surf->sm_geo );
158 }
159
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);
164
165 u32 *src_indices = world->scene_geo.arrindices,
166 *dst_indices = (u32 *)(buffer + new_vert_size);
167
168 memmove( dst_indices, src_indices, new_indice_len );
169
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 );
173
174 buffer = vg_linear_resize( world->heap, buffer, buf_size );
175
176 world->scene_geo.arrvertices = (scene_vert *)(buffer);
177 world->scene_geo.arrindices = (u32 *)(buffer + new_vert_size);
178
179 scene_upload_async( &world->scene_geo, &world->mesh_geo );
180
181 /* need send off the memory to the gpu before we can create the bvh. */
182 vg_async_stall();
183 vg_info( "creating bvh\n" );
184
185 /* setup spacial mapping and rigidbody */
186 world->geo_bh = scene_bh_create( world->heap, &world->scene_geo );
187
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 );
192
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 );
196
197 /*
198 * Generate scene: non-collidable geometry
199 * ----------------------------------------------------------------
200 */
201 vg_info( "Generating non-collidable geometry\n" );
202
203 vg_async_item *call = scene_alloc_async( &world->scene_no_collide,
204 &world->mesh_no_collide,
205 200000, 500000 );
206
207 for( u32 i=0; i<world->surface_count; i++ ){
208 struct world_surface *surf = &world->surfaces[ i ];
209
210 if( !(surf->info.flags & k_material_flag_collision) ){
211 world_add_all_if_material( midentity,
212 &world->scene_no_collide, &world->meta, i );
213 }
214
215 if( surf->info.flags & k_material_flag_grow_grass )
216 world_apply_procedural_foliage( world,
217 &world->scene_no_collide, surf );
218
219 scene_copy_slice( &world->scene_no_collide, &surf->sm_no_collide );
220 }
221
222 for( u32 i=0; i<mdl_arrcount( &world->ent_traffic ); i++ ){
223 ent_traffic *vehc = mdl_arritm( &world->ent_traffic, i );
224
225 for( u32 j=0; j<vehc->submesh_count; j++ ){
226 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs,
227 vehc->submesh_start+j );
228
229 if( sm->flags & k_submesh_flag_consumed ){
230 continue;
231 }
232
233 m4x3f identity;
234 m4x3_identity( identity );
235 scene_add_mdl_submesh( &world->scene_no_collide,
236 &world->meta, sm, identity );
237
238 scene_copy_slice( &world->scene_no_collide, sm );
239 sm->flags |= k_submesh_flag_consumed;
240 }
241 }
242
243 vg_async_dispatch( call, async_scene_upload );
244 }
245
246 float fsd_cone_infinite( v3f p, v2f c )
247 {
248 v2f q = { v2_length( (v2f){ p[0], p[2] } ), -p[1] };
249 float s = vg_maxf( 0.0f, v2_dot( q, c ) );
250
251 v2f v0;
252 v2_muls( c, s, v0 );
253 v2_sub( q, v0, v0 );
254
255 float d = v2_length( v0 );
256 return d * ((q[0]*c[1]-q[1]*c[0]<0.0f)?-1.0f:1.0f);
257 }
258
259 struct light_indices_upload_info{
260 world_instance *world;
261 v3i count;
262
263 void *data;
264 };
265
266 VG_STATIC void async_upload_light_indices( void *payload, u32 size )
267 {
268 struct light_indices_upload_info *info = payload;
269
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 );
277 }
278
279 VG_STATIC void world_compute_light_indices( world_instance *world )
280 {
281 /* light cubes */
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 );
285
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 );
288
289 v3_floor( cubes_min, cubes_min );
290 v3_floor( cubes_max, cubes_max );
291
292 v3i icubes_min, icubes_max;
293
294 for( int i=0; i<3; i++ ){
295 icubes_min[i] = cubes_min[i];
296 icubes_max[i] = cubes_max[i];
297 }
298
299 v3f cube_size;
300
301 v3i icubes_count;
302 v3i_sub( icubes_max, icubes_min, icubes_count );
303
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;
308
309 icubes_count[i] = clamped_count;
310 cube_size[i] = (max / clamped_max) * k_light_cube_size;
311 cubes_max[i] = clamped_max;
312 }
313
314 v3_mul( cubes_min, cube_size, cubes_min );
315 v3_mul( cubes_max, cube_size, cubes_max );
316
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];
321
322 vg_info( "cubes[%d]: %d\n", i, icubes_count[i] );
323 }
324
325 int total_cubes = icubes_count[0]*icubes_count[1]*icubes_count[2];
326
327 u32 data_size = vg_align8(total_cubes*sizeof(u32)*2),
328 hdr_size = vg_align8(sizeof(struct light_indices_upload_info));
329
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;
333 info->world = world;
334 u32 *cubes_index = info->data;
335
336 for( int i=0; i<3; i++ )
337 info->count[i] = icubes_count[i];
338
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 );
343
344 float bound_radius = v3_length( cube_size );
345
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++ ){
349 boxf bbx;
350 v3_div( (v3f){ ix, iy, iz }, world->ub_lighting.g_cube_inv_range,
351 bbx[0] );
352 v3_div( (v3f){ ix+1, iy+1, iz+1 },
353 world->ub_lighting.g_cube_inv_range,
354 bbx[1] );
355
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] );
358
359 v3f center;
360 v3_add( bbx[0], bbx[1], center );
361 v3_muls( center, 0.5f, center );
362
363 u32 indices[6] = { 0, 0, 0, 0, 0, 0 };
364 u32 count = 0;
365
366 float influences[6] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
367 const int N = vg_list_size( influences );
368
369 for( u32 j=0; j<mdl_arrcount(&world->ent_light); j ++ ){
370 ent_light *light = mdl_arritm( &world->ent_light, j );
371 v3f closest;
372 closest_point_aabb( light->transform.co, bbx, closest );
373
374 float dist = v3_dist( closest, light->transform.co ),
375 influence = 1.0f/(dist+1.0f);
376
377 if( dist > light->range )
378 continue;
379
380 if( light->type == k_light_type_spot){
381 v3f local;
382 m4x3_mulv( light->inverse_world, center, local );
383
384 float r = fsd_cone_infinite( local, light->angle_sin_cos );
385
386 if( r > bound_radius )
387 continue;
388 }
389
390 int best_pos = N;
391 for( int k=best_pos-1; k>=0; k -- )
392 if( influence > influences[k] )
393 best_pos = k;
394
395 if( best_pos < N ){
396 for( int k=N-1; k>best_pos; k -- ){
397 influences[k] = influences[k-1];
398 indices[k] = indices[k-1];
399 }
400
401 influences[best_pos] = influence;
402 indices[best_pos] = j;
403 }
404 }
405
406 for( int j=0; j<N; j++ )
407 if( influences[j] > 0.0f )
408 count ++;
409
410 int base_index = iz * (icubes_count[0]*icubes_count[1]) +
411 iy * (icubes_count[0]) +
412 ix;
413
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;
419
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;
425
426 cubes_index[ base_index*2 + 0 ] = packed_index_lower;
427 cubes_index[ base_index*2 + 1 ] = packed_index_upper;
428 }
429 }
430 }
431
432 vg_async_dispatch( call, async_upload_light_indices );
433 }
434
435 VG_STATIC void async_world_postprocess_render( void *payload, u32 _size )
436 {
437 /* create scene lighting buffer */
438 world_instance *world = payload;
439
440 u32 size = VG_MAX(mdl_arrcount(&world->ent_light),1) * sizeof(float)*12;
441 vg_info( "Upload %ubytes (lighting)\n", size );
442
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 );
446
447 /* buffer layout
448 *
449 * colour position direction (spots)
450 * | . . . . | . . . . | . . . . |
451 * | Re Ge Be Night | Xco Yco Zco Range | Dx Dy Dz Da |
452 *
453 */
454
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 );
458
459 /* colour + night */
460 v3_muls( light->colour, light->colour[3] * 2.0f, light_dst[i*3+0] );
461 light_dst[i*3+0][3] = 2.0f;
462
463 if( !light->daytime ){
464 u32 hash = (i * 29986577u) & 0xffu;
465 float switch_on = hash;
466 switch_on *= (1.0f/255.0f);
467
468 light_dst[i*3+0][3] = 0.44f + switch_on * 0.015f;
469 }
470
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);
474
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 );
478 }
479
480 glUnmapBuffer( GL_TEXTURE_BUFFER );
481
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 );
485
486 /* Upload lighting uniform buffer */
487 if( world->water.enabled )
488 v4_copy( world->water.plane, world->ub_lighting.g_water_plane );
489
490 v4f info_vec;
491 v3f *bounds = world->scene_geo.bbx;
492
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 );
498
499 /*
500 * Rendering the depth map
501 */
502 camera ortho;
503
504 v3f extent;
505 v3_sub( world->scene_geo.bbx[1], world->scene_geo.bbx[0], extent );
506
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],
511 rl = 1.0f / (fr-fl),
512 tb = 1.0f / (ft-fb);
513
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 );
523
524 glDisable(GL_DEPTH_TEST);
525 glDisable(GL_BLEND);
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} );
530 render_fsquad();
531
532 glEnable(GL_BLEND);
533 glBlendFunc(GL_ONE, GL_ONE);
534 glBlendEquation(GL_MAX);
535
536 render_world_position( world, &ortho );
537 glDisable(GL_BLEND);
538 glEnable(GL_DEPTH_TEST);
539 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
540
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 );
545 }
546
547 VG_STATIC int reset_player( int argc, char const *argv[] );
548 VG_STATIC void world_post_process( world_instance *world )
549 {
550 world_compute_light_indices( world );
551 vg_async_call( async_world_postprocess_render, world, 0 );
552 }
553
554 VG_STATIC void world_process_resources( world_instance *world )
555 {
556 vg_info( "Loading textures\n" );
557
558 world->texture_count = 0;
559
560 world->texture_count = world->meta.textures.count+1;
561 world->textures = vg_linear_alloc( world->heap,
562 vg_align8(sizeof(GLuint)*world->texture_count) );
563
564 vg_tex2d_replace_with_error( &world->textures[0] );
565
566 for( u32 i=0; i<mdl_arrcount(&world->meta.textures); i++ ){
567 mdl_texture *tex = mdl_arritm( &world->meta.textures, i );
568
569 if( !tex->file.pack_size ){
570 vg_fatal_error( "World models must have packed textures!" );
571 }
572
573 vg_linear_clear( vg_mem.scratch );
574 void *src_data = vg_linear_alloc( vg_mem.scratch,
575 tex->file.pack_size );
576 mdl_fread_pack_file( &world->meta, &tex->file, src_data );
577
578 vg_tex2d_load_qoi_async( src_data, tex->file.pack_size,
579 VG_TEX2D_NEAREST|VG_TEX2D_REPEAT,
580 &world->textures[i+1] );
581 }
582
583 vg_info( "Loading materials\n" );
584
585 world->surface_count = world->meta.materials.count+1;
586 world->surfaces = vg_linear_alloc( world->heap,
587 vg_align8(sizeof(struct world_surface)*world->surface_count) );
588
589 /* error material */
590 struct world_surface *errmat = &world->surfaces[0];
591 memset( errmat, 0, sizeof(struct world_surface) );
592
593 for( u32 i=0; i<mdl_arrcount(&world->meta.materials); i++ ){
594 world->surfaces[i+1].info =
595 *(mdl_material *)mdl_arritm( &world->meta.materials, i );
596 }
597 }
598
599 VG_STATIC void world_free( world_instance *world )
600 {
601 vg_info( "Free world @%p\n", world );
602
603 /* free meshes */
604 mesh_free( &world->mesh_route_lines );
605 mesh_free( &world->mesh_geo );
606 mesh_free( &world->mesh_no_collide );
607 mesh_free( &world->mesh_water );
608
609 /* glDeleteBuffers silently ignores 0's and names that do not correspond to
610 * existing buffer objects.
611 * */
612 glDeleteBuffers( 1, &world->tbo_light_entities );
613 glDeleteTextures( 1, &world->tex_light_entities );
614 glDeleteTextures( 1, &world->tex_light_cubes );
615
616 /* delete textures and meshes */
617 glDeleteTextures( world->texture_count, world->textures );
618
619 u32 world_index = world - world_global.worlds;
620 if( world_index ){
621 vg_linear_del( world_global.worlds[world_index-1].heap,
622 vg_linear_header(world->heap) );
623 }
624
625 world->status = k_world_status_unloaded;
626 }
627
628 /*
629 * checks:
630 * 1. to see if all audios owned by the world have been stopped
631 * 2. that this is the least significant world
632 */
633 VG_STATIC int world_freeable( world_instance *world )
634 {
635 if( world->status != k_world_status_unloading ) return 0;
636 u8 world_id = (world - world_global.worlds) + 1;
637
638 for( u32 i=world_id; i<vg_list_size(world_global.worlds); i++ ){
639 if( world_global.worlds[i].status != k_world_status_unloaded ){
640 return 0;
641 }
642 }
643
644 int freeable = 1;
645 audio_lock();
646 for( u32 i=0; i<AUDIO_CHANNELS; i++ ){
647 audio_channel *ch = &vg_audio.channels[i];
648
649 if( ch->allocated && (ch->world_id == world_id)){
650 if( !audio_channel_finished( ch ) ){
651 freeable = 0;
652 break;
653 }
654 }
655 }
656 audio_unlock();
657 return freeable;
658 }
659
660 VG_STATIC void world_init_blank( world_instance *world )
661 {
662 memset( &world->meta, 0, sizeof(mdl_context) );
663
664 world->textures = NULL;
665 world->texture_count = 0;
666 world->surfaces = NULL;
667 world->surface_count = 0;
668
669 world->geo_bh = NULL;
670 world->volume_bh = NULL;
671 world->audio_bh = NULL;
672 world->rendering_gate = NULL;
673
674 world->water.enabled = 0;
675 world->time = 0.0;
676
677 /* default lighting conditions
678 * -------------------------------------------------------------*/
679 struct ub_world_lighting *state = &world->ub_lighting;
680
681 state->g_light_preview = 0;
682 state->g_shadow_samples = 8;
683 state->g_water_fog = 0.04f;
684
685 v4_zero( state->g_water_plane );
686 v4_zero( state->g_depth_bounds );
687
688 state->g_shadow_length = 9.50f;
689 state->g_shadow_spread = 0.65f;
690
691 v3_copy( (v3f){0.37f, 0.54f, 0.97f}, state->g_daysky_colour );
692 v3_copy( (v3f){0.03f, 0.05f, 0.20f}, state->g_nightsky_colour );
693 v3_copy( (v3f){1.00f, 0.32f, 0.01f}, state->g_sunset_colour );
694 v3_copy( (v3f){0.13f, 0.17f, 0.35f}, state->g_ambient_colour );
695 v3_copy( (v3f){0.25f, 0.17f, 0.51f}, state->g_sunset_ambient );
696 v3_copy( (v3f){1.10f, 0.89f, 0.35f}, state->g_sun_colour );
697 }
698
699 /* detatches any nonlocal gates */
700 VG_STATIC void world_unlink_nonlocal( world_instance *world )
701 {
702 for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ ){
703 ent_gate *gate = mdl_arritm( &world->ent_gate, j );
704
705 if( gate->type == k_gate_type_nonlocel ){
706 gate->type = k_gate_type_nonlocal_unlinked;
707 }
708 }
709 }
710
711 /* attatches nonlocal gates, to be called from main thread ONLY! */
712 VG_STATIC void world_link_nonlocal_async( void *payload, u32 size )
713 {
714 world_instance *world = payload;
715 u32 world_id = world - world_global.worlds;
716
717 for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ ){
718 ent_gate *gate = mdl_arritm( &world->ent_gate, j );
719
720 if( gate->type == k_gate_type_nonlocal_unlinked ){
721 const char *key = mdl_pstr( &world->meta, gate->key );
722 vg_info( "key: %s\n", key );
723
724 for( u32 i=0; i<vg_list_size(world_global.worlds); i++ ){
725 world_instance *other = &world_global.worlds[i];
726 if( other == world ) continue;
727 if( other->status != k_world_status_loaded ) continue;
728 vg_info( "Checking world %u for key matches\n", i );
729
730 for( u32 j=0; j<mdl_arrcount( &other->ent_gate ); j++ ){
731 ent_gate *gate2 = mdl_arritm( &other->ent_gate, j );
732 if( gate2->type != k_gate_type_nonlocal_unlinked ) continue;
733
734 const char *key2 = mdl_pstr( &other->meta, gate2->key );
735 vg_info( " key2: %s\n", key2 );
736
737 if( strcmp( key, key2 ) ) continue;
738
739 vg_success( "Non-local matching pair '%s' found. (%u:%u)\n",
740 key, world_id, i );
741
742 gate->type = k_gate_type_nonlocel;
743 gate2->type = k_gate_type_nonlocel;
744 gate->target = i;
745 gate2->target = world_id;
746
747 v3_copy( gate->co[0], gate2->co[1] );
748 v3_copy( gate2->co[0], gate->co[1] );
749 v4_copy( gate->q[0], gate2->q[1] );
750 v4_copy( gate2->q[0], gate->q[1] );
751
752 v4f qflip;
753 q_axis_angle( qflip, (v3f){0.0f,1.0f,0.0f}, VG_PIf );
754 q_mul( gate->q[0], qflip, gate->q[0] );
755 q_mul( gate->q[1], qflip, gate->q[1] );
756
757 gate_transform_update( gate );
758 gate_transform_update( gate2 );
759
760 goto matched;
761 }
762 }
763 matched:;
764 }
765 }
766 }
767
768 VG_STATIC void world_entities_init( u32 world_id )
769 {
770 world_instance *world = &world_global.worlds[world_id];
771
772 /* lights */
773 for( u32 j=0; j<mdl_arrcount(&world->ent_light); j ++ ){
774 ent_light *light = mdl_arritm( &world->ent_light, j );
775
776 m4x3f to_world;
777 q_m3x3( light->transform.q, to_world );
778 v3_copy( light->transform.co, to_world[3] );
779 m4x3_invert_affine( to_world, light->inverse_world );
780
781 light->angle_sin_cos[0] = sinf( light->angle * 0.5f );
782 light->angle_sin_cos[1] = cosf( light->angle * 0.5f );
783 }
784
785 /* gates */
786 for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ ){
787 ent_gate *gate = mdl_arritm( &world->ent_gate, j );
788
789 if( gate->type == k_gate_type_teleport ){
790 gate_transform_update( gate );
791 }
792 }
793 vg_async_call( world_link_nonlocal_async, world, 0 );
794
795 /* water */
796 for( u32 j=0; j<mdl_arrcount(&world->ent_water); j++ ){
797 ent_water *water = mdl_arritm( &world->ent_water, j );
798 if( world->water.enabled ){
799 vg_warn( "Multiple water surfaces in level!\n" );
800 break;
801 }
802
803 world->water.enabled = 1;
804 water_set_surface( world, water->transform.co[1] );
805 }
806
807 /* volumes */
808 for( u32 j=0; j<mdl_arrcount(&world->ent_volume); j++ ){
809 ent_volume *volume = mdl_arritm( &world->ent_volume, j );
810 mdl_transform_m4x3( &volume->transform, volume->to_world );
811 m4x3_invert_full( volume->to_world, volume->to_local );
812 }
813
814 /* audio packs */
815 for( u32 j=0; j<mdl_arrcount(&world->ent_audio); j++ ){
816 ent_audio *audio = mdl_arritm( &world->ent_audio, j );
817
818 for( u32 k=0; k<audio->clip_count; k++ ){
819 ent_audio_clip *clip = mdl_arritm( &world->ent_audio_clip,
820 audio->clip_start+k );
821
822 if( clip->file.pack_size ){
823 u32 size = clip->file.pack_size,
824 offset = clip->file.pack_offset;
825
826 /* embedded files are fine to clear the scratch buffer, only
827 * external audio uses it */
828
829 vg_linear_clear( vg_mem.scratch );
830 void *data = vg_linear_alloc( vg_mem.scratch,
831 clip->file.pack_size );
832
833 mdl_fread_pack_file( &world->meta, &clip->file, data );
834
835 clip->clip.path = NULL;
836 clip->clip.flags = audio->flags;
837 clip->clip.data = data;
838 clip->clip.size = size;
839 }
840 else{
841 clip->clip.path = mdl_pstr( &world->meta, clip->file.pstr_path );
842 clip->clip.flags = audio->flags;
843 clip->clip.data = NULL;
844 clip->clip.size = 0;
845 }
846
847 audio_clip_load( &clip->clip, world->heap );
848 }
849 }
850 }
851
852 VG_STATIC void world_load( u32 index, const char *path )
853 {
854 vg_rand_seed( 9001 );
855
856 world_instance *world = &world_global.worlds[index];
857 world_init_blank( world );
858 world->status = k_world_status_loading;
859
860 vg_info( "Loading world: %s\n", path );
861
862 void *allocator = NULL;
863 if( index == 0 ) allocator = world_global.heap;
864 else allocator = world_global.worlds[index-1].heap;
865
866 u32 heap_availible = vg_linear_remaining( allocator );
867 u32 min_overhead = sizeof(vg_linear_allocator);
868
869 if( heap_availible < (min_overhead+1024) ){
870 vg_fatal_error( "out of memory" );
871 }
872
873 u32 size = heap_availible - min_overhead;
874 void *heap = vg_create_linear_allocator( allocator, size, VG_MEMORY_SYSTEM );
875
876 world->heap = heap;
877 mdl_context *meta = &world->meta;
878
879 mdl_open( meta, path, world->heap );
880 mdl_load_metadata_block( meta, world->heap );
881 mdl_load_animation_block( meta, world->heap );
882 mdl_load_mesh_block( meta, world->heap );
883
884 mdl_load_array( meta, &world->ent_gate, "ent_gate", heap );
885 mdl_load_array( meta, &world->ent_camera, "ent_camera", heap );
886 mdl_load_array( meta, &world->ent_spawn, "ent_spawn", heap );
887 mdl_load_array( meta, &world->ent_light, "ent_light", heap );
888 mdl_load_array( meta, &world->ent_route_node,"ent_route_node", heap );
889 mdl_load_array( meta, &world->ent_path_index,"ent_path_index", heap );
890 mdl_load_array( meta, &world->ent_checkpoint,"ent_checkpoint", heap );
891 mdl_load_array( meta, &world->ent_route, "ent_route", heap );
892 mdl_load_array( meta, &world->ent_water, "ent_water", heap );
893 mdl_load_array( meta, &world->ent_audio_clip,"ent_audio_clip", heap );
894 mdl_load_array( meta, &world->ent_audio, "ent_audio", heap );
895 mdl_load_array( meta, &world->ent_volume, "ent_volume", heap );
896 mdl_load_array( meta, &world->ent_traffic, "ent_traffic", heap );
897 mdl_load_array( meta, &world->ent_marker, "ent_marker", heap );
898 mdl_load_array( meta, &world->ent_skateshop, "ent_skateshop", heap );
899 mdl_load_array( meta, &world->ent_swspreview,"ent_swspreview", heap );
900
901 mdl_array_ptr infos;
902 mdl_load_array( meta, &infos, "ent_worldinfo", vg_mem.scratch );
903
904 if( mdl_arrcount(&infos) ){
905 world->info = *((ent_worldinfo *)mdl_arritm(&infos,0));
906 }
907 else{
908 world->info.pstr_author = 0;
909 world->info.pstr_desc = 0;
910 world->info.pstr_name = 0;
911 world->info.timezone = 0.0f;
912 }
913
914 time_t t;
915 struct tm *tm;
916 time( &t );
917 tm = gmtime( &t );
918 world->time = (float)tm->tm_min/20.0f + (world->info.timezone/24.0f);
919
920 /* process resources from pack */
921 world_process_resources( world );
922
923 world_routes_ent_init( world );
924 world_entities_init( index );
925 world->volume_bh = bh_create( heap, &bh_system_volumes, world,
926 mdl_arrcount( &world->ent_volume ), 1 );
927
928 /* main bulk */
929 world_generate( world );
930 world_routes_generate( world );
931 world_post_process( world );
932
933 mdl_close( meta );
934 world->status = k_world_status_loaded;
935 }
936
937 #endif /* WORLD_GEN_H */