2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 VG_STATIC
void world_load( world_instance
*world
, const char *path
);
12 VG_STATIC
void world_add_all_if_material( m4x3f transform
, scene
*pscene
,
13 mdl_context
*mdl
, u32 id
)
15 for( int i
=0; i
<mdl
->info
.node_count
; i
++ )
17 mdl_node
*pnode
= mdl_node_from_id( mdl
, i
);
19 for( int j
=0; j
<pnode
->submesh_count
; j
++ )
21 mdl_submesh
*sm
= mdl_node_submesh( mdl
, pnode
, j
);
22 if( sm
->material_id
== id
)
25 mdl_node_transform( pnode
, transform2
);
26 m4x3_mul( transform
, transform2
, transform2
);
28 scene_add_mdl_submesh( pscene
, mdl
, sm
, transform2
);
34 VG_STATIC
void world_add_blob( world_instance
*world
,
35 scene
*pscene
, ray_hit
*hit
)
38 v4f qsurface
, qrandom
;
41 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
->normal
, axis
);
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] );
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
} }
60 const u32 indices
[] = { 0,1,3, 0,3,2, 2,3,5, 2,5,4 };
62 if( pscene
->vertex_count
+ vg_list_size(verts
) > pscene
->max_vertices
)
63 vg_fatal_exit_loop( "Scene vertex buffer overflow" );
65 if( pscene
->indice_count
+ vg_list_size(indices
) > pscene
->max_indices
)
66 vg_fatal_exit_loop( "Scene index buffer overflow" );
68 scene_vert
*dst_verts
= &pscene
->arrvertices
[ pscene
->vertex_count
];
69 u32
*dst_indices
= &pscene
->arrindices
[ pscene
->indice_count
];
71 scene_vert
*ref
= &world
->scene_geo
->arrvertices
[ hit
->tri
[0] ];
73 for( u32 i
=0; i
<vg_list_size(verts
); i
++ )
75 scene_vert
*pvert
= &dst_verts
[ i
],
78 m4x3_mulv( transform
, src
->co
, pvert
->co
);
79 scene_vert_pack_norm( pvert
, transform
[1] );
81 v2_copy( ref
->uv
, pvert
->uv
);
84 for( u32 i
=0; i
<vg_list_size(indices
); i
++ )
85 dst_indices
[i
] = indices
[i
] + pscene
->vertex_count
;
87 pscene
->vertex_count
+= vg_list_size(verts
);
88 pscene
->indice_count
+= vg_list_size(indices
);
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
)
95 if( vg
.quality_profile
== k_quality_profile_low
)
98 vg_info( "Applying foliage (%u)\n", mat
->info
.pstr_name
);
100 vg_linear_clear( vg_mem
.scratch
);
102 mdl_context
*mfoliage
=
103 mdl_load_full( vg_mem
.scratch
, "models/rs_foliage.mdl");
106 v3_sub( world
->scene_geo
->bbx
[1], world
->scene_geo
->bbx
[0], volume
);
109 mdl_node
*mblob
= mdl_node_from_name( mfoliage
, "blob" );
110 mdl_submesh
*sm_blob
= mdl_node_submesh( mfoliage
, mblob
, 0 );
114 float area
= volume
[0]*volume
[2];
115 u32 particles
= 0.08f
* area
;
117 vg_info( "Map area: %f. Max particles: %u\n", area
, particles
);
119 for( int i
=0;i
<particles
;i
++ )
122 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
124 v3_add( pos
, world
->scene_geo
->bbx
[0], pos
);
129 if( ray_world( world
, pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
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
))
134 world_add_blob( world
, world
->scene_no_collide
, &hit
);
140 vg_info( "%d foliage models added\n", count
);
143 VG_STATIC
void world_ents_allocate( world_instance
*world
)
145 vg_info( "Allocating entities\n" );
147 /* count entites to allocate buffers for them.
148 * maybe in the future we just store these numbers in the model file...
150 * TODO: use this in world_routes too */
163 (void*)&world
->spawns
,
164 sizeof(struct respawn_point
)
168 (void*)&world
->audio_things
,
169 sizeof(struct world_audio_thing
)
173 (void*)&world
->triggers
,
174 sizeof(struct trigger_zone
)
177 k_classtype_logic_relay
,
178 (void*)&world
->logic_relays
,
179 sizeof(struct logic_relay
)
182 k_classtype_logic_achievement
,
183 (void*)&world
->logic_achievements
,
184 sizeof(struct logic_achievement
)
187 k_classtype_world_light
,
188 (void*)&world
->lights
,
189 sizeof(struct world_light
)
192 k_classtype_nonlocal_gate
,
193 (void*)&world
->nonlocal_gates
,
194 sizeof(struct nonlocal_gate
)
198 for( int i
=0; i
<vg_list_size(entity_counts
); i
++ )
199 entity_counts
[i
].count
= 0;
201 for( int i
=0; i
<world
->meta
->info
.node_count
; i
++ )
203 mdl_node
*pnode
= mdl_node_from_id( world
->meta
, i
);
205 for( int j
=0; j
<vg_list_size(entity_counts
); j
++ )
207 if( pnode
->classtype
== entity_counts
[j
].ct
)
209 pnode
->sub_uid
= entity_counts
[j
].count
;
210 entity_counts
[j
].count
++;
216 for( int i
=0; i
<vg_list_size(entity_counts
); i
++ )
218 struct countable
*counter
= &entity_counts
[i
];
220 u32 bufsize
= counter
->item_size
*counter
->count
;
221 *counter
->to_allocate
= vg_linear_alloc( world_global
.generic_heap
,
226 VG_STATIC
void world_pct_spawn( world_instance
*world
, mdl_node
*pnode
)
228 struct respawn_point
*rp
= &world
->spawns
[ world
->spawn_count
++ ];
230 v3_copy( pnode
->co
, rp
->co
);
231 v4_copy( pnode
->q
, rp
->q
);
232 rp
->name
= mdl_pstr( world
->meta
, pnode
->pstr_name
);
235 VG_STATIC
void world_pct_water( world_instance
*world
, mdl_node
*pnode
)
237 if( world
->water
.enabled
)
239 vg_warn( "Multiple water surfaces in level! ('%s')\n",
240 mdl_pstr( world
->meta
, pnode
->pstr_name
));
244 world
->water
.enabled
= 1;
245 water_set_surface( world
, pnode
->co
[1] );
248 VG_STATIC
void world_pct_audio( world_instance
*world
, mdl_node
*pnode
)
250 struct world_audio_thing
*thing
= &world
->audio_things
[
251 world
->audio_things_count
];
253 memset( thing
, 0, sizeof(struct world_audio_thing
) );
254 struct classtype_audio
*aud
= mdl_get_entdata( world
->meta
, pnode
);
256 v3_copy( pnode
->co
, thing
->pos
);
258 if( aud
->flags
& AUDIO_FLAG_SPACIAL_3D
)
259 thing
->volume
= aud
->volume
* pnode
->s
[0];
261 thing
->volume
= aud
->volume
;
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
;
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;
271 pnode
->sub_uid
= world
->audio_things_count
;
272 world
->audio_things_count
++;
275 VG_STATIC
void world_pct_trigger( world_instance
*world
, mdl_node
*pnode
)
277 struct trigger_zone
*trigger
= &world
->triggers
[ world
->trigger_count
];
278 struct classtype_trigger
*inf
= mdl_get_entdata( world
->meta
, pnode
);
282 mdl_node
*target_node
= mdl_node_from_id( world
->meta
, inf
->target
);
284 trigger
->target
.sub_id
= target_node
->sub_uid
;
285 trigger
->target
.classtype
= target_node
->classtype
;
289 vg_warn( "Trigger with no target...\n" );
293 mdl_node_transform( pnode
, trigger
->transform
);
294 m4x3_invert_full( trigger
->transform
, trigger
->inv_transform
);
296 world
->trigger_count
++;
300 VG_STATIC
void world_pct_relay( world_instance
*world
, mdl_node
*pnode
)
302 struct logic_relay
*relay
= &world
->logic_relays
[ world
->relay_count
];
303 struct classtype_logic_relay
*inf
= mdl_get_entdata( world
->meta
, pnode
);
305 relay
->target_count
= 0;
307 for( int i
=0; i
<vg_list_size(relay
->targets
); i
++ )
309 if( inf
->targets
[i
] )
311 struct relay_target
*target
= &relay
->targets
[relay
->target_count
++];
312 mdl_node
*other
= mdl_node_from_id( world
->meta
, inf
->targets
[i
] );
314 target
->classtype
= other
->classtype
;
315 target
->sub_id
= other
->sub_uid
;
319 v3_copy( pnode
->co
, relay
->pos
);
320 world
->relay_count
++;
324 VG_STATIC
void world_pct_achievement( world_instance
*world
, mdl_node
*pnode
)
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
);
331 v3_copy( pnode
->co
, ach
->pos
);
332 ach
->achievement_id
= mdl_pstr( world
->meta
, inf
->pstr_name
);
335 world
->achievement_count
++;
338 VG_STATIC
void world_pct_world_light( world_instance
*world
, mdl_node
*pnode
)
340 struct world_light
*light
= &world
->lights
[ world
->light_count
++ ];
342 light
->inf
= mdl_get_entdata( world
->meta
, pnode
);
345 VG_STATIC
void world_pct_nonlocal_gate( world_instance
*world
, mdl_node
*pnode
)
347 struct nonlocal_gate
*gate
= &world
->nonlocal_gates
[
348 world
->nonlocalgate_count
++ ];
349 struct classtype_gate
*inf
= mdl_get_entdata( world
->meta
, pnode
);
353 gate
->target_map_index
= 0;
354 v2_copy( inf
->dims
, gate
->gate
.dims
);
357 VG_STATIC
void world_entities_process( world_instance
*world
)
359 struct entity_instruction
362 void (*process
)( world_instance
*world
, mdl_node
*pnode
);
364 entity_instructions
[] =
366 { k_classtype_spawn
, world_pct_spawn
},
367 { k_classtype_water
, world_pct_water
},
368 { k_classtype_audio
, world_pct_audio
},
369 { k_classtype_trigger
, world_pct_trigger
},
370 { k_classtype_logic_relay
, world_pct_relay
},
371 { k_classtype_logic_achievement
, world_pct_achievement
},
372 { k_classtype_world_light
, world_pct_world_light
},
373 { k_classtype_nonlocal_gate
, world_pct_nonlocal_gate
}
376 for( int i
=0; i
<world
->meta
->info
.node_count
; i
++ )
378 mdl_node
*pnode
= mdl_node_from_id( world
->meta
, i
);
380 for( int j
=0; j
<vg_list_size(entity_instructions
); j
++ )
382 struct entity_instruction
*instr
= &entity_instructions
[j
];
384 if( pnode
->classtype
== instr
->ct
)
386 instr
->process( world
, pnode
);
393 VG_STATIC
void world_link_nonlocal_gates( int index_a
, int index_b
)
395 vg_info( "Linking non-local gates\n" );
396 world_instance
*a
= &world_global
.worlds
[ index_a
],
397 *b
= &world_global
.worlds
[ index_b
];
399 for( int i
=0; i
<a
->nonlocalgate_count
; i
++ )
401 struct nonlocal_gate
*ga
= &a
->nonlocal_gates
[i
];
402 struct classtype_gate
*ga_inf
= mdl_get_entdata( a
->meta
, ga
->node
);
403 const char *ga_name
= mdl_pstr( a
->meta
, ga_inf
->target
);
405 for( int j
=0; j
<b
->nonlocalgate_count
; j
++ )
407 struct nonlocal_gate
*gb
= &b
->nonlocal_gates
[j
];
408 struct classtype_gate
*gb_inf
= mdl_get_entdata( b
->meta
, gb
->node
);
409 const char *gb_name
= mdl_pstr( b
->meta
, gb_inf
->target
);
411 if( !strcmp( ga_name
, gb_name
) )
413 vg_success( "Created longjump for ID '%s'\n", ga_name
);
416 q_axis_angle( qYflip
, (v3f
){0.0f
,1.0f
,0.0f
}, VG_PIf
);
418 /* TODO: Gates are created very wonkily. refactor. */
419 ga
->target_map_index
= index_b
;
420 gb
->target_map_index
= index_a
;
424 v4_copy( ga
->node
->q
, ga
->gate
.q
[0] );
425 v4_copy( gb
->node
->q
, ga
->gate
.q
[1] );
426 v3_copy( ga
->node
->co
, ga
->gate
.co
[0] );
427 v3_copy( gb
->node
->co
, ga
->gate
.co
[1] );
429 v4_copy( gb
->node
->q
, gb
->gate
.q
[0] );
430 v4_copy( ga
->node
->q
, gb
->gate
.q
[1] );
431 v3_copy( gb
->node
->co
, gb
->gate
.co
[0] );
432 v3_copy( ga
->node
->co
, gb
->gate
.co
[1] );
434 /* reverse B's direction */
435 q_mul( gb
->gate
.q
[0], qYflip
, gb
->gate
.q
[0] );
436 q_mul( gb
->gate
.q
[1], qYflip
, gb
->gate
.q
[1] );
437 q_normalize( gb
->gate
.q
[0] );
438 q_normalize( gb
->gate
.q
[1] );
440 gate_transform_update( &ga
->gate
);
441 gate_transform_update( &gb
->gate
);
447 VG_STATIC
float colour_luminance( v3f v
)
449 return v3_dot( v
, (v3f
){0.2126f
, 0.7152f
, 0.0722f
} );
452 VG_STATIC
float calc_light_influence( world_instance
*world
, v3f position
,
455 struct world_light
*world_light
= &world
->lights
[ light
];
456 struct classtype_world_light
*inf
= world_light
->inf
;
459 v3_sub( world_light
->node
->co
, position
, light_delta
);
460 v3_muls( light_delta
, 10.0f
, light_delta
);
462 float quadratic
= v3_dot( light_delta
, light_delta
),
463 attenuation
= 1.0f
/( 1.0f
+ quadratic
);
465 float quadratic_light
= attenuation
* colour_luminance( inf
->colour
);
467 if( (inf
->type
== k_light_type_point
) ||
468 (inf
->type
== k_light_type_point_nighttime_only
) )
470 return quadratic_light
;
472 else if( (inf
->type
== k_light_type_spot
) ||
473 (inf
->type
== k_light_type_spot_nighttime_only
) )
476 q_mulv( world_light
->node
->q
, (v3f
){0.0f
,1.0f
,0.0f
}, dir
);
478 float spot_theta
= vg_maxf( 0.0f
, v3_dot( light_delta
, dir
) ),
479 falloff
= spot_theta
>= 0.0f
? 1.0f
: 0.0f
;
481 return quadratic_light
* falloff
;
487 VG_STATIC
void world_scene_compute_light_clusters( world_instance
*world
,
490 for( int i
=0; i
<sc
->vertex_count
; i
++ )
492 scene_vert
*vert
= &sc
->arrvertices
[i
];
498 float influences
[3] = { 0.0f
, 0.0f
, 0.0f
};
499 const int N
= vg_list_size( influences
);
501 for( int j
=0; j
<world
->light_count
; j
++ )
503 float influence
= calc_light_influence( world
, vert
->co
, j
);
506 for( int k
=best_pos
-1; k
>=0; k
-- )
507 if( influence
> influences
[k
] )
512 for( int k
=N
-1; k
>best_pos
; k
-- )
514 influences
[k
] = influences
[k
-1];
515 vert
->lights
[k
] = vert
->lights
[k
-1];
518 influences
[best_pos
] = influence
;
519 vert
->lights
[best_pos
] = j
;
523 for( int j
=0; j
<N
; j
++ )
525 if( influences
[j
] > 0.00000125f
)
531 VG_STATIC
void world_generate( world_instance
*world
)
534 * Compile meshes into the world scenes
536 world
->scene_geo
= scene_init( world_global
.generic_heap
, 320000, 1200000 );
539 m4x3_identity( midentity
);
542 * Generate scene: collidable geometry
543 * ----------------------------------------------------------------
546 vg_info( "Generating collidable geometry\n" );
549 for( int i
=0; i
<world
->material_count
; i
++ )
551 struct world_material
*mat
= &world
->materials
[ i
];
553 if( mat
->info
.flags
& k_material_flag_collision
)
554 world_add_all_if_material( midentity
, world
->scene_geo
, world
->meta
, i
);
556 scene_copy_slice( world
->scene_geo
, &mat
->sm_geo
);
558 world_scene_compute_light_clusters( world
, world
->scene_geo
);
560 /* compress that bad boy */
561 world
->scene_geo
= scene_fix( world_global
.generic_heap
, world
->scene_geo
);
563 vg_acquire_thread_sync();
565 scene_upload( world
->scene_geo
, &world
->mesh_geo
);
567 vg_release_thread_sync();
569 /* setup spacial mapping and rigidbody */
570 world
->geo_bh
= scene_bh_create( world_global
.generic_heap
,
573 v3_zero( world
->rb_geo
.co
);
574 q_identity( world
->rb_geo
.q
);
576 world
->rb_geo
.type
= k_rb_shape_scene
;
577 world
->rb_geo
.inf
.scene
.bh_scene
= world
->geo_bh
;
578 world
->rb_geo
.is_world
= 1;
579 rb_init( &world
->rb_geo
);
582 * Generate scene: non-collidable geometry
583 * ----------------------------------------------------------------
585 vg_info( "Generating non-collidable geometry\n" );
587 world
->scene_no_collide
= scene_init( world_global
.generic_heap
,
590 for( int i
=0; i
<world
->material_count
; i
++ )
592 struct world_material
*mat
= &world
->materials
[ i
];
594 if( !(mat
->info
.flags
& k_material_flag_collision
) )
596 world_add_all_if_material( midentity
, world
->scene_no_collide
,
600 if( mat
->info
.flags
& k_material_flag_grow_grass
)
601 world_apply_procedural_foliage( world
, mat
);
603 scene_copy_slice( world
->scene_no_collide
, &mat
->sm_no_collide
);
605 world_scene_compute_light_clusters( world
, world
->scene_no_collide
);
607 /* upload and free that */
608 vg_acquire_thread_sync();
610 scene_upload( world
->scene_no_collide
, &world
->mesh_no_collide
);
612 vg_release_thread_sync();
614 vg_linear_del( world_global
.generic_heap
, world
->scene_no_collide
);
615 world
->scene_no_collide
= NULL
;
618 VG_STATIC
int reset_player( int argc
, char const *argv
[] );
619 VG_STATIC
void world_post_process( world_instance
*world
)
621 /* initialize audio if need be */
623 for( int i
=0; i
<world
->audio_things_count
; i
++ )
625 struct world_audio_thing
*thingy
= &world
->audio_things
[ i
];
627 audio_player_init( &thingy
->player
);
628 audio_player_set_flags( &thingy
->player
, thingy
->flags
);
629 audio_player_set_vol( &thingy
->player
, thingy
->volume
);
630 audio_player_set_pan( &thingy
->player
, 0.0f
);
632 if( thingy
->flags
& AUDIO_FLAG_SPACIAL_3D
)
633 audio_player_set_position( &thingy
->player
, thingy
->pos
);
635 if( thingy
->flags
& AUDIO_FLAG_AUTO_START
)
636 audio_player_playclip( &thingy
->player
, &thingy
->temp_embedded_clip
);
640 vg_acquire_thread_sync();
642 /* create scene lighting buffer */
644 u32 size
= VG_MAX(world
->light_count
,1) * sizeof(float)*12;
646 vg_info( "Upload %ubytes (lighting)\n", size
);
648 glGenBuffers( 1, &world
->tbo_light_entities
);
649 glBindBuffer( GL_TEXTURE_BUFFER
, world
->tbo_light_entities
);
650 glBufferData( GL_TEXTURE_BUFFER
, size
, NULL
, GL_DYNAMIC_DRAW
);
654 * colour position direction (spots)
655 * | . . . . | . . . . | . . . . |
656 * | Re Ge Be Night | Xco Yco Zco | Dx Dy Dz Da |
660 v4f
*light_dst
= glMapBuffer( GL_TEXTURE_BUFFER
, GL_WRITE_ONLY
);
662 for( int i
=0; i
<world
->light_count
; i
++ )
664 struct world_light
*light
= &world
->lights
[i
];
665 struct classtype_world_light
*inf
= light
->inf
;
668 v3_muls( inf
->colour
, inf
->colour
[3] * 2.0f
, light_dst
[i
*3+0] );
669 light_dst
[i
*3+0][3] = 0.0f
;
671 if( (inf
->type
== k_light_type_spot_nighttime_only
) ||
672 (inf
->type
== k_light_type_point_nighttime_only
) )
674 u32 hash
= (i
* 29986577) & 0xff;
675 float switch_on
= hash
;
676 switch_on
*= (1.0f
/255.0f
);
678 light_dst
[i
*3+0][3] = 0.44f
+ switch_on
* 0.015f
;
681 /* position + nothing */
682 v3_copy( light
->node
->co
, light_dst
[i
*3+1] );
684 /* direction + angle */
685 q_mulv( light
->node
->q
, (v3f
){0.0f
,-1.0f
,0.0f
}, light_dst
[i
*3+2]);
686 light_dst
[i
*3+2][3] = inf
->angle
;
689 glUnmapBuffer( GL_TEXTURE_BUFFER
);
691 glGenTextures( 1, &world
->tex_light_entities
);
692 glBindTexture( GL_TEXTURE_BUFFER
, world
->tex_light_entities
);
693 glTexBuffer( GL_TEXTURE_BUFFER
, GL_RGBA32F
, world
->tbo_light_entities
);
696 /* Upload lighting uniform buffer */
697 if( world
->water
.enabled
)
698 v4_copy( world
->water
.plane
, world
->ub_lighting
.g_water_plane
);
701 v3f
*bounds
= world
->scene_geo
->bbx
;
703 info_vec
[0] = bounds
[0][0];
704 info_vec
[1] = bounds
[0][2];
705 info_vec
[2] = 1.0f
/ (bounds
[1][0]-bounds
[0][0]);
706 info_vec
[3] = 1.0f
/ (bounds
[1][2]-bounds
[0][2]);
707 v4_copy( info_vec
, world
->ub_lighting
.g_depth_bounds
);
709 /* upload full buffer */
710 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
711 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
712 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
716 * Rendering the depth map
721 v3_sub( world
->scene_geo
->bbx
[1], world
->scene_geo
->bbx
[0], extent
);
723 float fl
= world
->scene_geo
->bbx
[0][0],
724 fr
= world
->scene_geo
->bbx
[1][0],
725 fb
= world
->scene_geo
->bbx
[0][2],
726 ft
= world
->scene_geo
->bbx
[1][2],
730 m4x4_zero( ortho
.mtx
.p
);
731 ortho
.mtx
.p
[0][0] = 2.0f
* rl
;
732 ortho
.mtx
.p
[2][1] = 2.0f
* tb
;
733 ortho
.mtx
.p
[3][0] = (fr
+ fl
) * -rl
;
734 ortho
.mtx
.p
[3][1] = (ft
+ fb
) * -tb
;
735 ortho
.mtx
.p
[3][3] = 1.0f
;
736 m4x3_identity( ortho
.transform
);
737 camera_update_view( &ortho
);
738 camera_finalize( &ortho
);
740 glDisable(GL_DEPTH_TEST
);
742 glDisable(GL_CULL_FACE
);
743 render_fb_bind( &world
->heightmap
);
744 shader_blitcolour_use();
745 shader_blitcolour_uColour( (v4f
){-9999.0f
,-9999.0f
,-9999.0f
,-9999.0f
} );
750 glBlendFunc(GL_ONE
, GL_ONE
);
751 glBlendEquation(GL_MAX
);
753 render_world_position( world
, &ortho
);
755 glEnable(GL_DEPTH_TEST
);
756 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
760 vg_release_thread_sync();
764 * Setup scene collider
766 reset_player( 1, (const char *[]){"start"} );
770 VG_STATIC
void world_process_resources( world_instance
*world
)
772 vg_info( "Loading textures\n" );
773 world
->texture_count
= world
->meta
->info
.texture_count
;
774 world
->textures
= vg_linear_alloc( world_global
.generic_heap
,
775 sizeof(GLuint
)*world
->texture_count
);
777 vg_acquire_thread_sync();
780 world
->textures
[0] = vg_tex2d_new();
781 vg_tex2d_set_error();
785 for( int i
=1; i
<world
->texture_count
; i
++ )
787 mdl_texture
*tex
= &world
->meta
->texture_buffer
[i
];
789 if( !tex
->pack_offset
)
791 vg_release_thread_sync();
792 vg_fatal_exit_loop( "World models must have packed textures!" );
795 vg_linear_clear( vg_mem
.scratch
);
796 world
->textures
[i
] = vg_tex2d_new();
797 vg_tex2d_set_error();
798 vg_tex2d_qoi( world
->meta
->pack
+ tex
->pack_offset
, tex
->pack_length
,
799 mdl_pstr( world
->meta
, tex
->pstr_name
));
804 vg_release_thread_sync();
806 vg_info( "Loading materials\n" );
808 u32 size
= sizeof(struct world_material
) * world
->meta
->info
.material_count
;
809 world
->materials
= vg_linear_alloc( world_global
.generic_heap
, size
);
811 world
->material_count
= world
->meta
->info
.material_count
;
812 memset( world
->materials
, 0, size
);
814 for( int i
=1; i
<world
->material_count
; i
++ )
815 world
->materials
[i
].info
= world
->meta
->material_buffer
[i
];
818 struct world_material
*errmat
= &world
->materials
[0];
819 v4_copy( (v4f
){ 1.0f
,0.0f
,0.0f
,1.0f
}, errmat
->info
.colour
);
820 v4_copy( (v4f
){ 1.0f
,0.0f
,0.0f
,1.0f
}, errmat
->info
.colour1
);
821 errmat
->info
.flags
= 0x00;
822 errmat
->info
.pstr_name
= 0; /* useless? */
823 errmat
->info
.shader
= -1;
824 errmat
->info
.tex_decal
= 0;
825 errmat
->info
.tex_diffuse
= 0;
826 errmat
->info
.tex_normal
= 0;
829 VG_STATIC
void world_unload( world_instance
*world
)
831 vg_acquire_thread_sync();
834 mesh_free( &world
->mesh_route_lines
);
835 mesh_free( &world
->mesh_geo
);
836 mesh_free( &world
->mesh_no_collide
);
838 glDeleteBuffers( 1, &world
->tbo_light_entities
);
839 glDeleteTextures( 1, &world
->tex_light_entities
);
841 /* FIXME: CANT DO THIS HERE */
842 world_global
.time
= 0.0;
843 world_global
.rewind_from
= 0.0;
844 world_global
.rewind_to
= 0.0;
845 world_global
.last_use
= 0.0;
846 world_global
.active_gate
= 0;
847 world_global
.current_run_version
= 2;
848 world_global
.active_route_board
= 0;
850 for( int i
=0; i
<vg_list_size(world_global
.ui_bars
); i
++ )
852 struct route_ui_bar
*uib
= &world_global
.ui_bars
[i
];
853 uib
->segment_start
= 0;
854 uib
->segment_count
= 0;
857 uib
->fade_timer_start
= 0.0;
861 /* delete textures and meshes */
862 glDeleteTextures( world
->texture_count
, world
->textures
);
864 /* delete the entire block of memory */
865 /* FIXME: WE CANT DO THIS SHIT ANYMORE, NEED TO DEALLOC FROM ABOVE */
867 vg_linear_clear( world
->dynamic_vgl
);
868 vg_linear_clear( world
->audio_vgl
);
872 vg_release_thread_sync();
875 VG_STATIC
void world_add_global_light( world_instance
*world
,
876 v2f dir
, v3f colour
)
878 int id
= world
->ub_lighting
.g_light_count
;
880 v3_copy( colour
, world
->ub_lighting
.g_light_colours
[id
] );
881 v3_copy( (v3f
){ cosf(dir
[1]) * cosf(dir
[0]),
883 sinf(dir
[1]) * cosf(dir
[0]) },
884 world
->ub_lighting
.g_light_directions
[id
] );
886 world
->ub_lighting
.g_light_count
++;
889 VG_STATIC
void world_clean( world_instance
*world
)
891 /* clean dangling pointers */
894 world
->textures
= NULL
;
895 world
->texture_count
= 0;
896 world
->materials
= NULL
;
897 world
->material_count
= 0;
899 world
->scene_geo
= NULL
;
900 world
->scene_no_collide
= NULL
;
901 world
->scene_lines
= NULL
;
903 world
->geo_bh
= NULL
;
904 world
->trigger_bh
= NULL
;
905 world
->audio_bh
= NULL
;
907 world
->spawns
= NULL
;
908 world
->spawn_count
= 0;
910 world
->audio_things
= NULL
;
911 world
->audio_things_count
= 0;
913 world
->triggers
= NULL
;
914 world
->trigger_count
= 0;
916 world
->lights
= NULL
;
917 world
->light_count
= 0;
919 world
->logic_relays
= NULL
;
920 world
->relay_count
= 0;
922 world
->logic_achievements
= NULL
;
923 world
->achievement_count
= 0;
926 world
->node_count
= 0;
928 world
->routes
= NULL
;
929 world
->route_count
= 0;
932 world
->gate_count
= 0;
934 world
->collectors
= NULL
;
935 world
->collector_count
= 0;
937 world
->nonlocal_gates
= NULL
;
938 world
->nonlocalgate_count
= 0;
940 world
->water
.enabled
= 0;
943 /* default lighting conditions
944 * -------------------------------------------------------------*/
945 world
->ub_lighting
.g_light_count
= 0;
946 world
->ub_lighting
.g_light_preview
= 0;
947 world
->ub_lighting
.g_shadow_samples
= 8;
948 world
->ub_lighting
.g_water_fog
= 0.04f
;
950 v4_zero( world
->ub_lighting
.g_water_plane
);
951 v4_zero( world
->ub_lighting
.g_depth_bounds
);
952 v4_zero( world
->ub_lighting
.g_ambient_colour
);
954 v3_copy( (v3f
){ 0.09f
, 0.03f
, 0.07f
}, world
->ub_lighting
.g_ambient_colour
);
956 world_add_global_light( world
, (v2f
){ 0.63f
, -0.08f
},
957 (v3f
){ 1.36f
, 1.35f
, 1.01f
} );
959 world_add_global_light( world
, (v2f
){ -2.60f
, -0.13f
},
960 (v3f
){ 0.33f
, 0.56f
, 0.64f
} );
962 world_add_global_light( world
, (v2f
){ 2.60f
, -0.84f
},
963 (v3f
){ 0.05f
, 0.05f
, 0.23f
} );
965 world
->ub_lighting
.g_light_directions
[0][3] = 9.50f
;
966 world
->ub_lighting
.g_light_colours
[0][3] = 0.65f
;
969 VG_STATIC
void world_load( world_instance
*world
, const char *path
)
971 world_unload( world
);
972 world_clean( world
);
974 world
->meta
= mdl_load_full( world_global
.generic_heap
, path
);
975 vg_info( "Loading world: %s\n", path
);
977 /* process resources from pack */
978 world_process_resources( world
);
980 /* dynamic allocations */
981 world_ents_allocate( world
);
982 world_routes_allocate( world
);
984 /* meta processing */
985 world_routes_process( world
);
986 world_entities_process( world
);
989 world_generate( world
);
990 world_routes_generate( world
);
991 world_post_process( world
);
994 #endif /* WORLD_GEN_H */