my fucking fingers
[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_randf()*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_randf(), 1000.0f, vg_randf() }, 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 #if 0
600 VG_STATIC void world_free( world_instance *world )
601 {
602 vg_acquire_thread_sync();
603
604 /* free meshes */
605 mesh_free( &world->mesh_route_lines );
606 mesh_free( &world->mesh_geo );
607 mesh_free( &world->mesh_no_collide );
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 vg_release_thread_sync();
620
621 world->status = k_world_status_unloaded;
622 }
623 #endif
624
625 VG_STATIC void world_init_blank( world_instance *world )
626 {
627 memset( &world->meta, 0, sizeof(mdl_context) );
628
629 world->textures = NULL;
630 world->texture_count = 0;
631 world->surfaces = NULL;
632 world->surface_count = 0;
633
634 world->geo_bh = NULL;
635 world->volume_bh = NULL;
636 world->audio_bh = NULL;
637 world->rendering_gate = NULL;
638
639 world->water.enabled = 0;
640
641 /* default lighting conditions
642 * -------------------------------------------------------------*/
643 struct ub_world_lighting *state = &world->ub_lighting;
644
645 state->g_light_preview = 0;
646 state->g_shadow_samples = 8;
647 state->g_water_fog = 0.04f;
648
649 v4_zero( state->g_water_plane );
650 v4_zero( state->g_depth_bounds );
651
652 state->g_shadow_length = 9.50f;
653 state->g_shadow_spread = 0.65f;
654
655 v3_copy( (v3f){0.37f, 0.54f, 0.97f}, state->g_daysky_colour );
656 v3_copy( (v3f){0.03f, 0.05f, 0.20f}, state->g_nightsky_colour );
657 v3_copy( (v3f){1.00f, 0.32f, 0.01f}, state->g_sunset_colour );
658 v3_copy( (v3f){0.13f, 0.17f, 0.35f}, state->g_ambient_colour );
659 v3_copy( (v3f){0.25f, 0.17f, 0.51f}, state->g_sunset_ambient );
660 v3_copy( (v3f){1.10f, 0.89f, 0.35f}, state->g_sun_colour );
661 }
662
663 VG_STATIC void world_entities_init( u32 world_id )
664 {
665 world_instance *world = &world_global.worlds[world_id];
666
667 /* lights */
668 for( u32 j=0; j<mdl_arrcount(&world->ent_light); j ++ ){
669 ent_light *light = mdl_arritm( &world->ent_light, j );
670
671 m4x3f to_world;
672 q_m3x3( light->transform.q, to_world );
673 v3_copy( light->transform.co, to_world[3] );
674 m4x3_invert_affine( to_world, light->inverse_world );
675
676 light->angle_sin_cos[0] = sinf( light->angle * 0.5f );
677 light->angle_sin_cos[1] = cosf( light->angle * 0.5f );
678 }
679
680 /* gates */
681 for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ ){
682 ent_gate *gate = mdl_arritm( &world->ent_gate, j );
683
684 if( gate->type == k_gate_type_teleport ){
685 gate_transform_update( gate );
686 }
687 else if( gate->type == k_gate_type_nonlocal_unlinked ){
688 const char *key = mdl_pstr( &world->meta, gate->target );
689 vg_info( "key: %s\n", key );
690
691 for( u32 i=0; i<vg_list_size(world_global.worlds); i++ ){
692 world_instance *other = &world_global.worlds[i];
693 if( other == world ) continue;
694 if( other->status != k_world_status_loaded ) continue;
695 vg_info( "Checking world %u for key matches\n", i );
696
697 for( u32 j=0; j<mdl_arrcount( &other->ent_gate ); j++ ){
698 ent_gate *gate2 = mdl_arritm( &other->ent_gate, j );
699 if( gate2->type != k_gate_type_nonlocal_unlinked ) continue;
700
701 const char *key2 = mdl_pstr( &other->meta, gate2->target );
702 vg_info( " key2: %s\n", key2 );
703
704 if( strcmp( key, key2 ) ) continue;
705
706 vg_success( "Non-local matching pair '%s' found. (%u:%u)\n",
707 key, world_id, i );
708
709 gate->type = k_gate_type_nonlocel;
710 gate2->type = k_gate_type_nonlocel;
711 gate->target = i;
712 gate2->target = world_id;
713
714 v3_copy( gate->co[0], gate2->co[1] );
715 v3_copy( gate2->co[0], gate->co[1] );
716 v4_copy( gate->q[0], gate2->q[1] );
717 v4_copy( gate2->q[0], gate->q[1] );
718
719 v4f qflip;
720 q_axis_angle( qflip, (v3f){0.0f,1.0f,0.0f}, VG_PIf );
721 q_mul( gate->q[0], qflip, gate->q[0] );
722 q_mul( gate->q[1], qflip, gate->q[1] );
723
724 gate_transform_update( gate );
725 gate_transform_update( gate2 );
726
727 goto matched;
728 }
729 }
730 matched:;
731 }
732 }
733
734 /* water */
735 for( u32 j=0; j<mdl_arrcount(&world->ent_water); j++ ){
736 ent_water *water = mdl_arritm( &world->ent_water, j );
737 if( world->water.enabled ){
738 vg_warn( "Multiple water surfaces in level!\n" );
739 break;
740 }
741
742 world->water.enabled = 1;
743 water_set_surface( world, water->transform.co[1] );
744 }
745
746 /* volumes */
747 for( u32 j=0; j<mdl_arrcount(&world->ent_volume); j++ ){
748 ent_volume *volume = mdl_arritm( &world->ent_volume, j );
749 mdl_transform_m4x3( &volume->transform, volume->to_world );
750 m4x3_invert_full( volume->to_world, volume->to_local );
751 }
752
753 /* audio packs */
754 for( u32 j=0; j<mdl_arrcount(&world->ent_audio); j++ ){
755 ent_audio *audio = mdl_arritm( &world->ent_audio, j );
756
757 for( u32 k=0; k<audio->clip_count; k++ ){
758 ent_audio_clip *clip = mdl_arritm( &world->ent_audio_clip,
759 audio->clip_start+k );
760
761 if( clip->file.pack_size ){
762 u32 size = clip->file.pack_size,
763 offset = clip->file.pack_offset;
764
765 /* embedded files are fine to clear the scratch buffer, only
766 * external audio uses it */
767
768 vg_linear_clear( vg_mem.scratch );
769 void *data = vg_linear_alloc( vg_mem.scratch,
770 clip->file.pack_size );
771
772 mdl_fread_pack_file( &world->meta, &clip->file, data );
773
774 clip->clip.path = NULL;
775 clip->clip.flags = audio->flags;
776 clip->clip.data = data;
777 clip->clip.size = size;
778 }
779 else{
780 clip->clip.path = mdl_pstr( &world->meta, clip->file.pstr_path );
781 clip->clip.flags = audio->flags;
782 clip->clip.data = NULL;
783 clip->clip.size = 0;
784 }
785
786 audio_clip_load( &clip->clip, world->heap );
787 }
788 }
789 }
790
791 VG_STATIC void world_load( u32 index, const char *path )
792 {
793 world_instance *world = &world_global.worlds[index];
794 world_init_blank( world );
795 world->status = k_world_status_loading;
796
797 vg_info( "Loading world: %s\n", path );
798
799 void *allocator = NULL;
800 if( index == 0 ) allocator = world_global.heap;
801 else allocator = world_global.worlds[index-1].heap;
802
803 u32 heap_availible = vg_linear_remaining( allocator );
804 u32 min_overhead = sizeof(vg_linear_allocator);
805
806 if( heap_availible < (min_overhead+1024) ){
807 vg_fatal_error( "out of memory" );
808 }
809
810 u32 size = heap_availible - min_overhead;
811 void *heap = vg_create_linear_allocator( allocator, size,
812 VG_MEMORY_SYSTEM );
813
814 world->heap = heap;
815 mdl_context *meta = &world->meta;
816
817 mdl_open( meta, path, world->heap );
818 mdl_load_metadata_block( meta, world->heap );
819 mdl_load_animation_block( meta, world->heap );
820 mdl_load_mesh_block( meta, world->heap );
821
822 mdl_load_array( meta, &world->ent_gate, "ent_gate", heap );
823 mdl_load_array( meta, &world->ent_camera, "ent_camera", heap );
824 mdl_load_array( meta, &world->ent_spawn, "ent_spawn", heap );
825 mdl_load_array( meta, &world->ent_light, "ent_light", heap );
826 mdl_load_array( meta, &world->ent_route_node,"ent_route_node", heap );
827 mdl_load_array( meta, &world->ent_path_index,"ent_path_index", heap );
828 mdl_load_array( meta, &world->ent_checkpoint,"ent_checkpoint", heap );
829 mdl_load_array( meta, &world->ent_route, "ent_route", heap );
830 mdl_load_array( meta, &world->ent_water, "ent_water", heap );
831 mdl_load_array( meta, &world->ent_audio_clip,"ent_audio_clip", heap );
832 mdl_load_array( meta, &world->ent_audio, "ent_audio", heap );
833 mdl_load_array( meta, &world->ent_volume, "ent_volume", heap );
834 mdl_load_array( meta, &world->ent_traffic, "ent_traffic", heap );
835 mdl_load_array( meta, &world->ent_marker, "ent_marker", heap );
836 mdl_load_array( meta, &world->ent_skateshop, "ent_skateshop", heap );
837 mdl_load_array( meta, &world->ent_swspreview,"ent_swspreview", heap );
838
839 /* process resources from pack */
840 world_process_resources( world );
841
842 world_routes_ent_init( world );
843 world_entities_init( index );
844 world->volume_bh = bh_create( heap, &bh_system_volumes, world,
845 mdl_arrcount( &world->ent_volume ), 1 );
846
847 /* main bulk */
848 world_generate( world );
849 world_routes_generate( world );
850 world_post_process( world );
851
852 mdl_close( meta );
853
854 world->status = k_world_status_loaded;
855 }
856
857 #endif /* WORLD_GEN_H */