5cfc9d102af3a94996e032ac177193ffc7ff4836
[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( world_instance *world, const char *path );
11
12 VG_STATIC void world_add_all_if_material( m4x3f transform, scene *pscene,
13 mdl_context *mdl, u32 id )
14 {
15 for( int i=0; i<mdl->info.node_count; i++ )
16 {
17 mdl_node *pnode = mdl_node_from_id( mdl, i );
18
19 for( int j=0; j<pnode->submesh_count; j++ )
20 {
21 mdl_submesh *sm = mdl_node_submesh( mdl, pnode, j );
22 if( sm->material_id == id )
23 {
24 m4x3f transform2;
25 mdl_node_transform( pnode, transform2 );
26 m4x3_mul( transform, transform2, transform2 );
27
28 scene_add_mdl_submesh( pscene, mdl, sm, transform2 );
29 }
30 }
31 }
32 }
33
34 VG_STATIC void world_add_blob( world_instance *world,
35 scene *pscene, ray_hit *hit )
36 {
37 m4x3f transform;
38 v4f qsurface, qrandom;
39 v3f axis;
40
41 v3_cross( (v3f){0.0f,1.0f,0.0f}, hit->normal, axis );
42
43 float angle = v3_dot(hit->normal,(v3f){0.0f,1.0f,0.0f});
44 q_axis_angle( qsurface, axis, angle );
45 q_axis_angle( qrandom, (v3f){0.0f,1.0f,0.0f}, vg_randf()*VG_TAUf );
46 q_mul( qsurface, qrandom, qsurface );
47 q_m3x3( qsurface, transform );
48 v3_copy( hit->pos, transform[3] );
49
50 scene_vert verts[] =
51 {
52 { .co = { -1.00f, 0.0f, 0.0f } },
53 { .co = { 1.00f, 0.0f, 0.0f } },
54 { .co = { -1.00f, 1.2f, 0.0f } },
55 { .co = { 1.00f, 1.2f, 0.0f } },
56 { .co = { -0.25f, 2.0f, 0.0f } },
57 { .co = { 0.25f, 2.0f, 0.0f } }
58 };
59
60 const u32 indices[] = { 0,1,3, 0,3,2, 2,3,5, 2,5,4 };
61
62 if( pscene->vertex_count + vg_list_size(verts) > pscene->max_vertices )
63 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
64
65 if( pscene->indice_count + vg_list_size(indices) > pscene->max_indices )
66 vg_fatal_exit_loop( "Scene index buffer overflow" );
67
68 scene_vert *dst_verts = &pscene->arrvertices[ pscene->vertex_count ];
69 u32 *dst_indices = &pscene->arrindices [ pscene->indice_count ];
70
71 scene_vert *ref = &world->scene_geo->arrvertices[ hit->tri[0] ];
72
73 for( u32 i=0; i<vg_list_size(verts); i++ )
74 {
75 scene_vert *pvert = &dst_verts[ i ],
76 *src = &verts[ i ];
77
78 m4x3_mulv( transform, src->co, pvert->co );
79 scene_vert_pack_norm( pvert, transform[1] );
80
81 v2_copy( ref->uv, pvert->uv );
82 }
83
84 for( u32 i=0; i<vg_list_size(indices); i++ )
85 dst_indices[i] = indices[i] + pscene->vertex_count;
86
87 pscene->vertex_count += vg_list_size(verts);
88 pscene->indice_count += vg_list_size(indices);
89 }
90
91 /* Sprinkle foliage models over the map on terrain material */
92 VG_STATIC void world_apply_procedural_foliage( world_instance *world,
93 struct world_material *mat )
94 {
95 if( vg.quality_profile == k_quality_profile_low )
96 return;
97
98 vg_info( "Applying foliage (%u)\n", mat->info.pstr_name );
99
100 vg_linear_clear( vg_mem.scratch );
101
102 mdl_context *mfoliage =
103 mdl_load_full( vg_mem.scratch, "models/rs_foliage.mdl");
104
105 v3f volume;
106 v3_sub( world->scene_geo->bbx[1], world->scene_geo->bbx[0], volume );
107 volume[1] = 1.0f;
108
109 mdl_node *mblob = mdl_node_from_name( mfoliage, "blob" );
110 mdl_submesh *sm_blob = mdl_node_submesh( mfoliage, mblob, 0 );
111
112 int count = 0;
113
114 float area = volume[0]*volume[2];
115 u32 particles = 0.08f * area;
116
117 vg_info( "Map area: %f. Max particles: %u\n", area, particles );
118
119 for( int i=0;i<particles;i++ )
120 {
121 v3f pos;
122 v3_mul( volume, (v3f){ vg_randf(), 1000.0f, vg_randf() }, pos );
123 pos[1] = 1000.0f;
124 v3_add( pos, world->scene_geo->bbx[0], pos );
125
126 ray_hit hit;
127 hit.dist = INFINITY;
128
129 if( ray_world( world, pos, (v3f){0.0f,-1.0f,0.0f}, &hit ))
130 {
131 struct world_material *m1 = ray_hit_material( world, &hit );
132 if((hit.normal[1] > 0.8f) && (m1 == mat) && (hit.pos[1] > 0.0f+10.0f))
133 {
134 world_add_blob( world, world->scene_no_collide, &hit );
135 count ++;
136 }
137 }
138 }
139
140 vg_info( "%d foliage models added\n", count );
141 }
142
143 VG_STATIC void world_ents_allocate( world_instance *world )
144 {
145 vg_info( "Allocating entities\n" );
146
147 /* count entites to allocate buffers for them.
148 * maybe in the future we just store these numbers in the model file...
149 *
150 * TODO: use this in world_routes too */
151
152 struct countable
153 {
154 enum classtype ct;
155 void **to_allocate;
156 u32 item_size;
157 int count;
158 }
159 entity_counts[] =
160 {
161 {
162 k_classtype_spawn,
163 (void*)&world->spawns,
164 sizeof(struct respawn_point)
165 },
166 {
167 k_classtype_audio,
168 (void*)&world->audio_things,
169 sizeof(struct world_audio_thing)
170 },
171 {
172 k_classtype_trigger,
173 (void*)&world->triggers,
174 sizeof(struct trigger_zone)
175 },
176 {
177 k_classtype_logic_relay,
178 (void*)&world->logic_relays,
179 sizeof(struct logic_relay)
180 },
181 {
182 k_classtype_logic_achievement,
183 (void*)&world->logic_achievements,
184 sizeof(struct logic_achievement)
185 },
186 {
187 k_classtype_point_light,
188 (void*)&world->lights,
189 sizeof(struct world_light)
190 },
191 {
192 k_classtype_nonlocal_gate,
193 (void*)&world->nonlocal_gates,
194 sizeof(struct nonlocal_gate)
195 }
196 };
197
198 for( int i=0; i<vg_list_size(entity_counts); i++ )
199 entity_counts[i].count = 0;
200
201 for( int i=0; i<world->meta->info.node_count; i++ )
202 {
203 mdl_node *pnode = mdl_node_from_id( world->meta, i );
204
205 for( int j=0; j<vg_list_size(entity_counts); j ++ )
206 {
207 if( pnode->classtype == entity_counts[j].ct )
208 {
209 pnode->sub_uid = entity_counts[j].count;
210 entity_counts[j].count ++;
211 break;
212 }
213 }
214 }
215
216 for( int i=0; i<vg_list_size(entity_counts); i++ )
217 {
218 struct countable *counter = &entity_counts[i];
219
220 u32 bufsize = counter->item_size*counter->count;
221 *counter->to_allocate = vg_linear_alloc( world_global.generic_heap,
222 bufsize );
223 }
224 }
225
226 VG_STATIC void world_pct_spawn( world_instance *world, mdl_node *pnode )
227 {
228 struct respawn_point *rp = &world->spawns[ world->spawn_count ++ ];
229
230 v3_copy( pnode->co, rp->co );
231 v4_copy( pnode->q, rp->q );
232 rp->name = mdl_pstr( world->meta, pnode->pstr_name );
233 }
234
235 VG_STATIC void world_pct_water( world_instance *world, mdl_node *pnode )
236 {
237 if( world->water.enabled )
238 {
239 vg_warn( "Multiple water surfaces in level! ('%s')\n",
240 mdl_pstr( world->meta, pnode->pstr_name ));
241 return;
242 }
243
244 world->water.enabled = 1;
245 water_set_surface( world, pnode->co[1] );
246 }
247
248 VG_STATIC void world_pct_audio( world_instance *world, mdl_node *pnode )
249 {
250 struct world_audio_thing *thing = &world->audio_things[
251 world->audio_things_count ];
252
253 memset( thing, 0, sizeof(struct world_audio_thing) );
254 struct classtype_audio *aud = mdl_get_entdata( world->meta, pnode );
255
256 v3_copy( pnode->co, thing->pos );
257
258 if( aud->flags & AUDIO_FLAG_SPACIAL_3D )
259 thing->volume = aud->volume * pnode->s[0];
260 else
261 thing->volume = aud->volume;
262
263 thing->flags = aud->flags;
264 thing->temp_embedded_clip.path = mdl_pstr( world->meta, aud->pstr_file );
265 thing->temp_embedded_clip.source_mode = k_audio_source_mono;
266
267 audio_clip_load( &thing->temp_embedded_clip, world_global.audio_heap );
268 thing->player.name = mdl_pstr( world->meta, pnode->pstr_name );
269 thing->player.enqued = 0;
270
271 pnode->sub_uid = world->audio_things_count;
272 world->audio_things_count ++;
273 }
274
275 VG_STATIC void world_pct_trigger( world_instance *world, mdl_node *pnode )
276 {
277 struct trigger_zone *trigger = &world->triggers[ world->trigger_count ];
278 struct classtype_trigger *inf = mdl_get_entdata( world->meta, pnode );
279
280 if( inf->target )
281 {
282 mdl_node *target_node = mdl_node_from_id( world->meta, inf->target );
283
284 trigger->target.sub_id = target_node->sub_uid;
285 trigger->target.classtype = target_node->classtype;
286 }
287 else
288 {
289 vg_warn( "Trigger with no target...\n" );
290 return;
291 }
292
293 mdl_node_transform( pnode, trigger->transform );
294 m4x3_invert_full( trigger->transform, trigger->inv_transform );
295
296 world->trigger_count ++;
297 }
298
299
300 VG_STATIC void world_pct_relay( world_instance *world, mdl_node *pnode )
301 {
302 struct logic_relay *relay = &world->logic_relays[ world->relay_count ];
303 struct classtype_logic_relay *inf = mdl_get_entdata( world->meta, pnode );
304
305 relay->target_count = 0;
306
307 for( int i=0; i<vg_list_size(relay->targets); i++ )
308 {
309 if( inf->targets[i] )
310 {
311 struct relay_target *target = &relay->targets[relay->target_count ++];
312 mdl_node *other = mdl_node_from_id( world->meta, inf->targets[i] );
313
314 target->classtype = other->classtype;
315 target->sub_id = other->sub_uid;
316 }
317 }
318
319 v3_copy( pnode->co, relay->pos );
320 world->relay_count ++;
321 }
322
323
324 VG_STATIC void world_pct_achievement( world_instance *world, mdl_node *pnode )
325 {
326 struct logic_achievement *ach =
327 &world->logic_achievements[ world->achievement_count ];
328 struct classtype_logic_achievement *inf =
329 mdl_get_entdata( world->meta, pnode );
330
331 v3_copy( pnode->co, ach->pos );
332 ach->achievement_id = mdl_pstr( world->meta, inf->pstr_name );
333 ach->achieved = 0;
334
335 world->achievement_count ++;
336 }
337
338 VG_STATIC void world_pct_point_light( world_instance *world, mdl_node *pnode )
339 {
340 struct world_light *light = &world->lights[ world->light_count ];
341 v3_copy( pnode->co, light->co );
342
343 struct classtype_point_light *inf = mdl_get_entdata( world->meta, pnode );
344 v4_copy( inf->colour, light->colour );
345
346 world->light_count ++;
347 }
348
349 VG_STATIC void world_pct_nonlocal_gate( world_instance *world, mdl_node *pnode )
350 {
351 struct nonlocal_gate *gate = &world->nonlocal_gates[
352 world->nonlocalgate_count ++ ];
353 struct classtype_gate *inf = mdl_get_entdata( world->meta, pnode );
354
355 gate->working = 0;
356 gate->node = pnode;
357 gate->target_map_index = 0;
358 v2_copy( inf->dims, gate->gate.dims );
359 }
360
361 VG_STATIC void world_entities_process( world_instance *world )
362 {
363 struct entity_instruction
364 {
365 enum classtype ct;
366 void (*process)( world_instance *world, mdl_node *pnode );
367 }
368 entity_instructions[] =
369 {
370 { k_classtype_spawn, world_pct_spawn },
371 { k_classtype_water, world_pct_water },
372 { k_classtype_audio, world_pct_audio },
373 { k_classtype_trigger, world_pct_trigger },
374 { k_classtype_logic_relay, world_pct_relay },
375 { k_classtype_logic_achievement, world_pct_achievement },
376 { k_classtype_point_light, world_pct_point_light },
377 { k_classtype_nonlocal_gate, world_pct_nonlocal_gate }
378 };
379
380 for( int i=0; i<world->meta->info.node_count; i++ )
381 {
382 mdl_node *pnode = mdl_node_from_id( world->meta, i );
383
384 for( int j=0; j<vg_list_size(entity_instructions); j++ )
385 {
386 struct entity_instruction *instr = &entity_instructions[j];
387
388 if( pnode->classtype == instr->ct )
389 {
390 instr->process( world, pnode );
391 break;
392 }
393 }
394 }
395 }
396
397 VG_STATIC void world_link_nonlocal_gates( int index_a, int index_b )
398 {
399 vg_info( "Linking non-local gates\n" );
400 world_instance *a = &world_global.worlds[ index_a ],
401 *b = &world_global.worlds[ index_b ];
402
403 for( int i=0; i<a->nonlocalgate_count; i++ )
404 {
405 struct nonlocal_gate *ga = &a->nonlocal_gates[i];
406 struct classtype_gate *ga_inf = mdl_get_entdata( a->meta, ga->node );
407 const char *ga_name = mdl_pstr( a->meta, ga_inf->target );
408
409 for( int j=0; j<b->nonlocalgate_count; j++ )
410 {
411 struct nonlocal_gate *gb = &b->nonlocal_gates[j];
412 struct classtype_gate *gb_inf = mdl_get_entdata( b->meta, gb->node );
413 const char *gb_name = mdl_pstr( b->meta, gb_inf->target );
414
415 if( !strcmp( ga_name, gb_name ) )
416 {
417 vg_success( "Created longjump for ID '%s'\n", ga_name );
418
419 v4f qYflip;
420 q_axis_angle( qYflip, (v3f){0.0f,1.0f,0.0f}, VG_PIf );
421
422 /* TODO: Gates are created very wonkily. refactor. */
423 ga->target_map_index = index_b;
424 gb->target_map_index = index_a;
425 ga->working = 1;
426 gb->working = 1;
427
428 v4_copy( ga->node->q, ga->gate.q[0] );
429 v4_copy( gb->node->q, ga->gate.q[1] );
430 v3_copy( ga->node->co, ga->gate.co[0] );
431 v3_copy( gb->node->co, ga->gate.co[1] );
432
433 v4_copy( gb->node->q, gb->gate.q[0] );
434 v4_copy( ga->node->q, gb->gate.q[1] );
435 v3_copy( gb->node->co, gb->gate.co[0] );
436 v3_copy( ga->node->co, gb->gate.co[1] );
437
438 /* reverse B's direction */
439 q_mul( gb->gate.q[0], qYflip, gb->gate.q[0] );
440 q_mul( gb->gate.q[1], qYflip, gb->gate.q[1] );
441 q_normalize( gb->gate.q[0] );
442 q_normalize( gb->gate.q[1] );
443
444 gate_transform_update( &ga->gate );
445 gate_transform_update( &gb->gate );
446 }
447 }
448 }
449 }
450
451 VG_STATIC void world_scene_compute_light_clusters( world_instance *world,
452 scene *sc )
453 {
454 for( int i=0; i<sc->vertex_count; i++ )
455 {
456 scene_vert *vert = &sc->arrvertices[i];
457 vert->lights[0] = 255;
458 vert->lights[1] = 255;
459 vert->lights[2] = 255;
460 vert->lights[3] = 255;
461
462 float distances[4] = { INFINITY, INFINITY, INFINITY, INFINITY };
463
464 for( int j=0; j<world->light_count; j ++ )
465 {
466 float dist = v3_dist2( world->lights[j].co, vert->co );
467
468 int best_pos = 4;
469 for( int k=best_pos-1; k>=0; k -- )
470 if( dist < distances[k] )
471 best_pos = k;
472
473 if( best_pos < 4 )
474 {
475 for( int k=3; k>best_pos; k -- )
476 {
477 distances[k] = distances[k-1];
478 vert->lights[k] = vert->lights[k-1];
479 }
480
481 distances[best_pos] = dist;
482 vert->lights[best_pos] = j;
483 }
484 }
485 }
486 }
487
488 VG_STATIC void world_generate( world_instance *world )
489 {
490 /*
491 * Compile meshes into the world scenes
492 */
493 world->scene_geo = scene_init( world_global.generic_heap, 320000, 1200000 );
494
495 m4x3f midentity;
496 m4x3_identity( midentity );
497
498 /*
499 * Generate scene: collidable geometry
500 * ----------------------------------------------------------------
501 */
502
503 vg_info( "Generating collidable geometry\n" );
504
505
506 for( int i=0; i<world->material_count; i++ )
507 {
508 struct world_material *mat = &world->materials[ i ];
509
510 if( mat->info.flags & k_material_flag_collision )
511 world_add_all_if_material( midentity, world->scene_geo, world->meta, i );
512
513 scene_copy_slice( world->scene_geo, &mat->sm_geo );
514 }
515 world_scene_compute_light_clusters( world, world->scene_geo );
516
517 /* compress that bad boy */
518 world->scene_geo = scene_fix( world_global.generic_heap, world->scene_geo );
519
520 vg_acquire_thread_sync();
521 {
522 scene_upload( world->scene_geo, &world->mesh_geo );
523 }
524 vg_release_thread_sync();
525
526 /* setup spacial mapping and rigidbody */
527 world->geo_bh = scene_bh_create( world_global.generic_heap,
528 world->scene_geo );
529
530 v3_zero( world->rb_geo.co );
531 q_identity( world->rb_geo.q );
532
533 world->rb_geo.type = k_rb_shape_scene;
534 world->rb_geo.inf.scene.bh_scene = world->geo_bh;
535 world->rb_geo.is_world = 1;
536 rb_init( &world->rb_geo );
537
538 /*
539 * Generate scene: non-collidable geometry
540 * ----------------------------------------------------------------
541 */
542 vg_info( "Generating non-collidable geometry\n" );
543
544 world->scene_no_collide = scene_init( world_global.generic_heap,
545 200000, 500000 );
546
547 for( int i=0; i<world->material_count; i++ )
548 {
549 struct world_material *mat = &world->materials[ i ];
550
551 if( !(mat->info.flags & k_material_flag_collision) )
552 {
553 world_add_all_if_material( midentity, world->scene_no_collide,
554 world->meta, i );
555 }
556
557 if( mat->info.flags & k_material_flag_grow_grass )
558 world_apply_procedural_foliage( world, mat );
559
560 scene_copy_slice( world->scene_no_collide, &mat->sm_no_collide );
561 }
562 world_scene_compute_light_clusters( world, world->scene_no_collide );
563
564 /* upload and free that */
565 vg_acquire_thread_sync();
566 {
567 scene_upload( world->scene_no_collide, &world->mesh_no_collide );
568 }
569 vg_release_thread_sync();
570
571 vg_linear_del( world_global.generic_heap, world->scene_no_collide );
572 world->scene_no_collide = NULL;
573 }
574
575 VG_STATIC int reset_player( int argc, char const *argv[] );
576 VG_STATIC void world_post_process( world_instance *world )
577 {
578 /* initialize audio if need be */
579 audio_lock();
580 for( int i=0; i<world->audio_things_count; i++ )
581 {
582 struct world_audio_thing *thingy = &world->audio_things[ i ];
583
584 audio_player_init( &thingy->player );
585 audio_player_set_flags( &thingy->player, thingy->flags );
586 audio_player_set_vol( &thingy->player, thingy->volume );
587 audio_player_set_pan( &thingy->player, 0.0f );
588
589 if( thingy->flags & AUDIO_FLAG_SPACIAL_3D )
590 audio_player_set_position( &thingy->player, thingy->pos );
591
592 if( thingy->flags & AUDIO_FLAG_AUTO_START )
593 audio_player_playclip( &thingy->player, &thingy->temp_embedded_clip );
594 }
595 audio_unlock();
596
597 vg_acquire_thread_sync();
598 {
599 /* Upload lighting uniform buffer */
600 if( world->water.enabled )
601 v4_copy( world->water.plane, world->ub_lighting.g_water_plane );
602
603 v4f info_vec;
604 v3f *bounds = world->scene_geo->bbx;
605
606 info_vec[0] = bounds[0][0];
607 info_vec[1] = bounds[0][2];
608 info_vec[2] = 1.0f/ (bounds[1][0]-bounds[0][0]);
609 info_vec[3] = 1.0f/ (bounds[1][2]-bounds[0][2]);
610 v4_copy( info_vec, world->ub_lighting.g_depth_bounds );
611
612 /* add scene lights */
613 for( int i=0; i<world->light_count; i++ )
614 {
615 struct world_light *light = &world->lights[i];
616
617 v3_muls( light->colour, light->colour[3] * 2.0f,
618 world->ub_lighting.g_point_light_colours[i] );
619 v3_copy( light->co,
620 world->ub_lighting.g_point_light_positions[i] );
621 }
622
623 /* upload full buffer */
624 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
625 glBufferSubData( GL_UNIFORM_BUFFER, 0,
626 sizeof(struct ub_world_lighting), &world->ub_lighting );
627
628
629 /*
630 * Rendering the depth map
631 */
632 camera ortho;
633
634 v3f extent;
635 v3_sub( world->scene_geo->bbx[1], world->scene_geo->bbx[0], extent );
636
637 float fl = world->scene_geo->bbx[0][0],
638 fr = world->scene_geo->bbx[1][0],
639 fb = world->scene_geo->bbx[0][2],
640 ft = world->scene_geo->bbx[1][2],
641 rl = 1.0f / (fr-fl),
642 tb = 1.0f / (ft-fb);
643
644 m4x4_zero( ortho.mtx.p );
645 ortho.mtx.p[0][0] = 2.0f * rl;
646 ortho.mtx.p[2][1] = 2.0f * tb;
647 ortho.mtx.p[3][0] = (fr + fl) * -rl;
648 ortho.mtx.p[3][1] = (ft + fb) * -tb;
649 ortho.mtx.p[3][3] = 1.0f;
650 m4x3_identity( ortho.transform );
651 camera_update_view( &ortho );
652 camera_finalize( &ortho );
653
654 glDisable(GL_DEPTH_TEST);
655 glDisable(GL_BLEND);
656 glDisable(GL_CULL_FACE);
657 render_fb_bind( &world->heightmap );
658 shader_blitcolour_use();
659 shader_blitcolour_uColour( (v4f){-9999.0f,-9999.0f,-9999.0f,-9999.0f} );
660 render_fsquad();
661
662 /* todo: hmm?? */
663 glEnable(GL_BLEND);
664 glBlendFunc(GL_ONE, GL_ONE);
665 glBlendEquation(GL_MAX);
666
667 render_world_position( world, &ortho );
668 glDisable(GL_BLEND);
669 glEnable(GL_DEPTH_TEST);
670 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
671
672 }
673
674 vg_release_thread_sync();
675
676 #if 0
677 /*
678 * Setup scene collider
679 */
680 reset_player( 1, (const char *[]){"start"} );
681 #endif
682 }
683
684 VG_STATIC void world_process_resources( world_instance *world )
685 {
686 vg_info( "Loading textures\n" );
687 world->texture_count = world->meta->info.texture_count;
688 world->textures = vg_linear_alloc( world_global.generic_heap,
689 sizeof(GLuint)*world->texture_count );
690
691 vg_acquire_thread_sync();
692 {
693 /* error texture */
694 world->textures[0] = vg_tex2d_new();
695 vg_tex2d_set_error();
696 vg_tex2d_nearest();
697 vg_tex2d_repeat();
698
699 for( int i=1; i<world->texture_count; i++ )
700 {
701 mdl_texture *tex = &world->meta->texture_buffer[i];
702
703 if( !tex->pack_offset )
704 {
705 vg_release_thread_sync();
706 vg_fatal_exit_loop( "World models must have packed textures!" );
707 }
708
709 vg_linear_clear( vg_mem.scratch );
710 world->textures[i] = vg_tex2d_new();
711 vg_tex2d_set_error();
712 vg_tex2d_qoi( world->meta->pack + tex->pack_offset, tex->pack_length,
713 mdl_pstr( world->meta, tex->pstr_name ));
714 vg_tex2d_nearest();
715 vg_tex2d_repeat();
716 }
717 }
718 vg_release_thread_sync();
719
720 vg_info( "Loading materials\n" );
721
722 u32 size = sizeof(struct world_material) * world->meta->info.material_count;
723 world->materials = vg_linear_alloc( world_global.generic_heap, size );
724
725 world->material_count = world->meta->info.material_count;
726 memset( world->materials, 0, size );
727
728 for( int i=1; i<world->material_count; i++ )
729 world->materials[i].info = world->meta->material_buffer[i];
730
731 /* error material */
732 struct world_material *errmat = &world->materials[0];
733 v4_copy( (v4f){ 1.0f,0.0f,0.0f,1.0f }, errmat->info.colour );
734 v4_copy( (v4f){ 1.0f,0.0f,0.0f,1.0f }, errmat->info.colour1 );
735 errmat->info.flags = 0x00;
736 errmat->info.pstr_name = 0; /* useless? */
737 errmat->info.shader = -1;
738 errmat->info.tex_decal = 0;
739 errmat->info.tex_diffuse = 0;
740 errmat->info.tex_normal = 0;
741 }
742
743 VG_STATIC void world_unload( world_instance *world )
744 {
745 vg_acquire_thread_sync();
746
747 /* free meshes */
748 mesh_free( &world->mesh_route_lines );
749 mesh_free( &world->mesh_geo );
750 mesh_free( &world->mesh_no_collide );
751
752 /* FIXME: CANT DO THIS HERE */
753 world_global.time = 0.0;
754 world_global.rewind_from = 0.0;
755 world_global.rewind_to = 0.0;
756 world_global.last_use = 0.0;
757 world_global.active_gate = 0;
758 world_global.current_run_version = 2;
759 world_global.active_route_board = 0;
760
761 for( int i=0; i<vg_list_size(world_global.ui_bars); i++ )
762 {
763 struct route_ui_bar *uib = &world_global.ui_bars[i];
764 uib->segment_start = 0;
765 uib->segment_count = 0;
766 uib->fade_start = 0;
767 uib->fade_count = 0;
768 uib->fade_timer_start = 0.0;
769 uib->xpos = 0.0f;
770 }
771
772 /* delete textures and meshes */
773 glDeleteTextures( world->texture_count, world->textures );
774
775 /* delete the entire block of memory */
776 /* FIXME: WE CANT DO THIS SHIT ANYMORE, NEED TO DEALLOC FROM ABOVE */
777 #if 0
778 vg_linear_clear( world->dynamic_vgl );
779 vg_linear_clear( world->audio_vgl );
780 #endif
781
782
783 vg_release_thread_sync();
784 }
785
786 VG_STATIC void world_add_global_light( world_instance *world,
787 v2f dir, v3f colour )
788 {
789 int id = world->ub_lighting.g_light_count;
790
791 v3_copy( colour, world->ub_lighting.g_light_colours[id] );
792 v3_copy( (v3f){ cosf(dir[1]) * cosf(dir[0]),
793 sinf(dir[0]),
794 sinf(dir[1]) * cosf(dir[0]) },
795 world->ub_lighting.g_light_directions[id] );
796
797 world->ub_lighting.g_light_count ++;
798 }
799
800 VG_STATIC void world_clean( world_instance *world )
801 {
802 /* clean dangling pointers */
803 world->meta = NULL;
804
805 world->textures = NULL;
806 world->texture_count = 0;
807 world->materials = NULL;
808 world->material_count = 0;
809
810 world->scene_geo = NULL;
811 world->scene_no_collide = NULL;
812 world->scene_lines = NULL;
813
814 world->geo_bh = NULL;
815 world->trigger_bh = NULL;
816 world->audio_bh = NULL;
817
818 world->spawns = NULL;
819 world->spawn_count = 0;
820
821 world->audio_things = NULL;
822 world->audio_things_count = 0;
823
824 world->triggers = NULL;
825 world->trigger_count = 0;
826
827 world->lights = NULL;
828 world->light_count = 0;
829
830 world->logic_relays = NULL;
831 world->relay_count = 0;
832
833 world->logic_achievements = NULL;
834 world->achievement_count = 0;
835
836 world->nodes = NULL;
837 world->node_count = 0;
838
839 world->routes = NULL;
840 world->route_count = 0;
841
842 world->gates = NULL;
843 world->gate_count = 0;
844
845 world->collectors = NULL;
846 world->collector_count = 0;
847
848 world->nonlocal_gates = NULL;
849 world->nonlocalgate_count = 0;
850
851 world->water.enabled = 0;
852
853
854 /* default lighting conditions
855 * -------------------------------------------------------------*/
856 world->ub_lighting.g_light_count = 0;
857 world->ub_lighting.g_light_preview = 0;
858 world->ub_lighting.g_shadow_samples = 8;
859 world->ub_lighting.g_water_fog = 0.04f;
860
861 v4_zero( world->ub_lighting.g_water_plane );
862 v4_zero( world->ub_lighting.g_depth_bounds );
863 v4_zero( world->ub_lighting.g_ambient_colour );
864
865 v3_copy( (v3f){ 0.09f, 0.03f, 0.07f }, world->ub_lighting.g_ambient_colour );
866
867 world_add_global_light( world, (v2f){ 0.63f, -0.08f },
868 (v3f){ 1.36f, 1.35f, 1.01f } );
869
870 world_add_global_light( world, (v2f){ -2.60f, -0.13f },
871 (v3f){ 0.33f, 0.56f, 0.64f } );
872
873 world_add_global_light( world, (v2f){ 2.60f, -0.84f },
874 (v3f){ 0.05f, 0.05f, 0.23f } );
875
876 world->ub_lighting.g_light_directions[0][3] = 9.50f;
877 world->ub_lighting.g_light_colours[0][3] = 0.65f;
878 }
879
880 VG_STATIC void world_load( world_instance *world, const char *path )
881 {
882 world_unload( world );
883 world_clean( world );
884
885 world->meta = mdl_load_full( world_global.generic_heap, path );
886 vg_info( "Loading world: %s\n", path );
887
888 /* process resources from pack */
889 world_process_resources( world );
890
891 /* dynamic allocations */
892 world_ents_allocate( world );
893 world_routes_allocate( world );
894
895 /* meta processing */
896 world_routes_process( world );
897 world_entities_process( world );
898
899 /* main bulk */
900 world_generate( world );
901 world_routes_generate( world );
902 world_post_process( world );
903 }
904
905 #endif /* WORLD_GEN_H */