2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
9 #include "world_render.h"
13 #include "ent_miniworld.h"
14 #include "player_remote.h"
15 #include "ent_skateshop.h"
17 static int ccmd_set_time( int argc
, const char *argv
[] ){
18 world_instance
*world
= world_current_instance();
20 world
->time
= atof( argv
[0] );
22 vg_error( "Usage set_time <0-1.0> (current time: %f)\n", world
->time
);
26 static void async_world_render_init( void *payload
, u32 size
)
28 vg_info( "Allocate uniform buffers\n" );
29 for( int i
=0; i
<k_world_max
; i
++ ){
30 world_instance
*world
= &world_static
.instances
[i
];
31 world
->ubo_bind_point
= i
;
33 glGenBuffers( 1, &world
->ubo_lighting
);
34 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
35 glBufferData( GL_UNIFORM_BUFFER
, sizeof(struct ub_world_lighting
),
36 NULL
, GL_DYNAMIC_DRAW
);
38 glBindBufferBase( GL_UNIFORM_BUFFER
, i
, world
->ubo_lighting
);
42 vg_info( "Allocate frame buffers\n" );
43 for( int i
=0; i
<k_world_max
; i
++ ){
44 world_instance
*world
= &world_static
.instances
[i
];
45 struct framebuffer
*fb
= &world
->heightmap
;
47 fb
->display_name
= NULL
;
51 fb
->resolution_div
= 0;
53 fb
->attachments
[0].display_name
= NULL
;
54 fb
->attachments
[0].purpose
= k_framebuffer_attachment_type_texture
;
55 fb
->attachments
[0].internalformat
= GL_RG16F
;
56 fb
->attachments
[0].format
= GL_RG
;
57 fb
->attachments
[0].type
= GL_FLOAT
;
58 fb
->attachments
[0].attachment
= GL_COLOR_ATTACHMENT0
;
60 fb
->attachments
[1].purpose
= k_framebuffer_attachment_type_none
;
61 fb
->attachments
[2].purpose
= k_framebuffer_attachment_type_none
;
62 fb
->attachments
[3].purpose
= k_framebuffer_attachment_type_none
;
63 fb
->attachments
[4].purpose
= k_framebuffer_attachment_type_none
;
65 render_fb_allocate( fb
);
69 static void world_render_init(void)
71 VG_VAR_F32( k_day_length
);
72 VG_VAR_I32( k_debug_light_indices
);
73 VG_VAR_I32( k_debug_light_complexity
);
74 VG_VAR_I32( k_light_preview
);
75 vg_console_reg_cmd( "set_time", ccmd_set_time
, NULL
);
77 world_render
.sky_rate
= 1.0;
78 world_render
.sky_target_rate
= 1.0;
80 shader_scene_standard_register();
81 shader_scene_standard_alphatest_register();
82 shader_scene_foliage_register();
83 shader_scene_override_register();
84 shader_scene_cubemapped_register();
85 shader_scene_fxglow_register();
86 shader_scene_vertex_blend_register();
87 shader_scene_terrain_register();
88 shader_scene_depth_register();
89 shader_scene_position_register();
90 shader_model_sky_register();
91 shader_model_sky_space_register();
93 vg_info( "Loading world resources\n" );
94 vg_linear_clear( vg_mem
.scratch
);
97 mdl_open( &msky
, "models/rs_skydome.mdl", vg_mem
.scratch
);
98 mdl_load_metadata_block( &msky
, vg_mem
.scratch
);
99 mdl_async_load_glmesh( &msky
, &world_render
.skydome
, NULL
);
102 vg_info( "Loading default world textures\n" );
103 vg_tex2d_load_qoi_async_file( "textures/garbage.qoi",
104 VG_TEX2D_NEAREST
|VG_TEX2D_REPEAT
,
105 &world_render
.tex_terrain_noise
);
107 vg_async_call( async_world_render_init
, NULL
, 0 );
110 static void world_link_lighting_ub( world_instance
*world
, GLuint shader
){
111 GLuint idx
= glGetUniformBlockIndex( shader
, "ub_world_lighting" );
112 glUniformBlockBinding( shader
, idx
, world
->ubo_bind_point
);
115 static void world_bind_position_texture( world_instance
*world
,
116 GLuint shader
, GLuint location
,
118 render_fb_bind_texture( &world
->heightmap
, 0, slot
);
119 glUniform1i( location
, slot
);
122 static void world_bind_light_array( world_instance
*world
,
123 GLuint shader
, GLuint location
,
125 glActiveTexture( GL_TEXTURE0
+ slot
);
126 glBindTexture( GL_TEXTURE_BUFFER
, world
->tex_light_entities
);
127 glUniform1i( location
, slot
);
130 static void world_bind_light_index( world_instance
*world
,
131 GLuint shader
, GLuint location
,
133 glActiveTexture( GL_TEXTURE0
+ slot
);
134 glBindTexture( GL_TEXTURE_3D
, world
->tex_light_cubes
);
135 glUniform1i( location
, slot
);
138 static void render_world_depth( world_instance
*world
, camera
*cam
);
144 static void bind_terrain_noise(void){
145 glActiveTexture( GL_TEXTURE0
);
146 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
151 enum mdl_shader shader
;
152 enum world_geo_type geo_type
;
154 void (*fn_bind_textures
)( world_instance
*world
,
155 struct world_surface
*mat
);
156 void (*fn_set_mdl
)( m4x3f mdl
);
157 void (*fn_set_uPvmPrev
)( m4x4f pvm
);
158 void (*fn_set_uNormalMtx
)( m3x3f mnorm
);
161 /* FIXME: we gotta do something about this crap, maybe batch lists. something..
162 * anything but this. */
164 void world_render_props( world_instance
*world
, u32 material_id
,
165 struct world_pass
*pass
){
166 if( !mdl_arrcount( &world
->ent_prop
) ) return;
169 struct world_surface
*mat
= &world
->surfaces
[ material_id
];
170 pass
->fn_bind_textures( world
, mat
);
172 for( u32 j
=0; j
<mdl_arrcount( &world
->ent_prop
); j
++ ){
173 ent_prop
*prop
= mdl_arritm( &world
->ent_prop
, j
);
174 if( prop
->flags
& 0x1 ) continue;
176 for( u32 k
=0; k
<prop
->submesh_count
; k
++ ){
178 mdl_arritm( &world
->meta
.submeshs
, prop
->submesh_start
+k
);
180 if( sm
->material_id
!= material_id
) continue;
183 mdl_transform_m4x3( &prop
->transform
, mmdl
);
186 m4x3_expand( mmdl
, m4mdl
);
187 m4x4_mul( pass
->cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
189 pass
->fn_set_mdl( mmdl
);
190 pass
->fn_set_uPvmPrev( m4mdl
);
192 mdl_draw_submesh( sm
);
198 void world_render_traffic( world_instance
*world
, u32 material_id
,
199 struct world_pass
*pass
){
200 if( !mdl_arrcount( &world
->ent_traffic
) ) return;
202 /* HACK: use the first material for every traffic entity */
203 ent_traffic
*first
= mdl_arritm( &world
->ent_traffic
, 0 );
204 if( !first
->submesh_count
) return;
206 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
, first
->submesh_start
);
207 if( sm
->material_id
!= material_id
) return;
209 struct world_surface
*mat
= &world
->surfaces
[ material_id
];
210 pass
->fn_bind_textures( world
, mat
);
212 for( u32 j
=0; j
<mdl_arrcount( &world
->ent_traffic
); j
++ ){
213 ent_traffic
*traffic
= mdl_arritm( &world
->ent_traffic
, j
);
215 for( u32 k
=0; k
<traffic
->submesh_count
; k
++ ){
216 sm
= mdl_arritm( &world
->meta
.submeshs
,
217 traffic
->submesh_start
+k
);
220 q_m3x3( traffic
->transform
.q
, mmdl
);
221 v3_copy( traffic
->transform
.co
, mmdl
[3] );
224 m4x3_expand( mmdl
, m4mdl
);
225 m4x4_mul( pass
->cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
227 pass
->fn_set_mdl( mmdl
);
228 pass
->fn_set_uPvmPrev( m4mdl
);
230 mdl_draw_submesh( sm
);
236 void world_render_pass( world_instance
*world
, struct world_pass
*pass
){
237 for( int i
=0; i
<world
->surface_count
; i
++ ){
238 struct world_surface
*mat
= &world
->surfaces
[i
];
240 if( mat
->info
.shader
== pass
->shader
){
243 if( pass
->geo_type
== k_world_geo_type_solid
){
247 world_render_traffic( world
, i
, pass
);
248 world_render_props( world
, i
, pass
);
249 sm
= &mat
->sm_no_collide
;
252 if( !sm
->indice_count
)
256 m4x3_identity( mmdl
);
257 pass
->fn_set_mdl( mmdl
);
258 pass
->fn_set_uPvmPrev( pass
->cam
->mtx_prev
.pv
);
260 pass
->fn_bind_textures( world
, mat
);
261 mdl_draw_submesh( sm
);
267 void world_render_both_stages( world_instance
*world
, struct world_pass
*pass
)
269 mesh_bind( &world
->mesh_geo
);
270 pass
->geo_type
= k_world_geo_type_solid
;
271 world_render_pass( world
, pass
);
273 glDisable( GL_CULL_FACE
);
274 mesh_bind( &world
->mesh_no_collide
);
275 pass
->geo_type
= k_world_geo_type_nonsolid
;
276 world_render_pass( world
, pass
);
277 glEnable( GL_CULL_FACE
);
280 static GLuint
world_get_texture( world_instance
*world
, u32 id
){
281 if( id
& 0x80000000 )
282 return skaterift
.rt_textures
[id
& ~0x80000000];
284 return world
->textures
[ id
];
287 static void bindpoint_diffuse_texture1( world_instance
*world
,
288 struct world_surface
*mat
){
289 glActiveTexture( GL_TEXTURE1
);
290 glBindTexture( GL_TEXTURE_2D
,
291 world_get_texture(world
,mat
->info
.tex_diffuse
) );
294 static void bindpoint_diffuse1_and_cubemap10( world_instance
*world
,
295 struct world_surface
*mat
){
296 glActiveTexture( GL_TEXTURE1
);
297 glBindTexture( GL_TEXTURE_2D
,
298 world_get_texture(world
,mat
->info
.tex_diffuse
) );
300 u32 cubemap_id
= mat
->info
.tex_none0
,
303 if( mdl_entity_id_type( cubemap_id
) == k_ent_cubemap
){
304 cubemap_index
= mdl_entity_id_id( cubemap_id
);
307 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, cubemap_index
);
308 glActiveTexture( GL_TEXTURE10
);
309 glBindTexture( GL_TEXTURE_CUBE_MAP
, cm
->texture_id
);
311 shader_scene_cubemapped_uColour( mat
->info
.colour
);
314 static void render_world_vb( world_instance
*world
, camera
*cam
){
315 shader_scene_vertex_blend_use();
316 shader_scene_vertex_blend_uTexGarbage(0);
317 shader_scene_vertex_blend_uTexGradients(1);
318 world_link_lighting_ub( world
, _shader_scene_vertex_blend
.id
);
319 world_bind_position_texture( world
, _shader_scene_vertex_blend
.id
,
320 _uniform_scene_vertex_blend_g_world_depth
, 2 );
321 world_bind_light_array( world
, _shader_scene_vertex_blend
.id
,
322 _uniform_scene_vertex_blend_uLightsArray
, 3 );
323 world_bind_light_index( world
, _shader_scene_vertex_blend
.id
,
324 _uniform_scene_vertex_blend_uLightsIndex
, 4 );
326 glActiveTexture( GL_TEXTURE0
);
327 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
329 shader_scene_vertex_blend_uPv( cam
->mtx
.pv
);
330 shader_scene_vertex_blend_uCamera( cam
->transform
[3] );
332 struct world_pass pass
= {
333 .shader
= k_shader_standard_vertex_blend
,
335 .fn_bind_textures
= bindpoint_diffuse_texture1
,
336 .fn_set_mdl
= shader_scene_vertex_blend_uMdl
,
337 .fn_set_uPvmPrev
= shader_scene_vertex_blend_uPvmPrev
,
340 world_render_both_stages( world
, &pass
);
343 static void world_shader_standard_bind( world_instance
*world
, camera
*cam
){
344 shader_scene_standard_use();
345 shader_scene_standard_uTexGarbage(0);
346 shader_scene_standard_uTexMain(1);
347 shader_scene_standard_uPv( cam
->mtx
.pv
);
349 world_link_lighting_ub( world
, _shader_scene_standard
.id
);
350 world_bind_position_texture( world
, _shader_scene_standard
.id
,
351 _uniform_scene_standard_g_world_depth
, 2 );
352 world_bind_light_array( world
, _shader_scene_standard
.id
,
353 _uniform_scene_standard_uLightsArray
, 3 );
354 world_bind_light_index( world
, _shader_scene_standard
.id
,
355 _uniform_scene_standard_uLightsIndex
, 4 );
357 bind_terrain_noise();
358 shader_scene_standard_uCamera( cam
->transform
[3] );
361 static void render_world_standard( world_instance
*world
, camera
*cam
){
362 world_shader_standard_bind( world
, cam
);
363 struct world_pass pass
= {
364 .shader
= k_shader_standard
,
366 .fn_bind_textures
= bindpoint_diffuse_texture1
,
367 .fn_set_mdl
= shader_scene_standard_uMdl
,
368 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
371 world_render_both_stages( world
, &pass
);
374 static void render_world_cubemapped( world_instance
*world
, camera
*cam
,
376 if( !mdl_arrcount( &world
->ent_cubemap
) )
380 world_shader_standard_bind( world
, cam
);
382 struct world_pass pass
= {
383 .shader
= k_shader_cubemap
,
385 .fn_bind_textures
= bindpoint_diffuse_texture1
,
386 .fn_set_mdl
= shader_scene_standard_uMdl
,
387 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
390 world_render_both_stages( world
, &pass
);
393 shader_scene_cubemapped_use();
394 shader_scene_cubemapped_uTexGarbage(0);
395 shader_scene_cubemapped_uTexMain(1);
396 shader_scene_cubemapped_uTexCubemap(10);
397 shader_scene_cubemapped_uPv( cam
->mtx
.pv
);
399 world_link_lighting_ub( world
, _shader_scene_cubemapped
.id
);
400 world_bind_position_texture( world
, _shader_scene_cubemapped
.id
,
401 _uniform_scene_cubemapped_g_world_depth
, 2 );
402 world_bind_light_array( world
, _shader_scene_cubemapped
.id
,
403 _uniform_scene_cubemapped_uLightsArray
, 3 );
404 world_bind_light_index( world
, _shader_scene_cubemapped
.id
,
405 _uniform_scene_cubemapped_uLightsIndex
, 4 );
407 bind_terrain_noise();
408 shader_scene_cubemapped_uCamera( cam
->transform
[3] );
410 struct world_pass pass
= {
411 .shader
= k_shader_cubemap
,
413 .fn_bind_textures
= bindpoint_diffuse1_and_cubemap10
,
414 .fn_set_mdl
= shader_scene_cubemapped_uMdl
,
415 .fn_set_uPvmPrev
= shader_scene_cubemapped_uPvmPrev
,
418 world_render_both_stages( world
, &pass
);
422 static void render_world_alphatest( world_instance
*world
, camera
*cam
){
423 shader_scene_standard_alphatest_use();
424 shader_scene_standard_alphatest_uTexGarbage(0);
425 shader_scene_standard_alphatest_uTexMain(1);
426 shader_scene_standard_alphatest_uPv( cam
->mtx
.pv
);
428 world_link_lighting_ub( world
, _shader_scene_standard_alphatest
.id
);
429 world_bind_position_texture( world
, _shader_scene_standard_alphatest
.id
,
430 _uniform_scene_standard_alphatest_g_world_depth
, 2 );
431 world_bind_light_array( world
, _shader_scene_standard_alphatest
.id
,
432 _uniform_scene_standard_alphatest_uLightsArray
, 3 );
433 world_bind_light_index( world
, _shader_scene_standard_alphatest
.id
,
434 _uniform_scene_standard_alphatest_uLightsIndex
, 4 );
437 bind_terrain_noise();
440 shader_scene_standard_alphatest_uCamera( cam
->transform
[3] );
442 glDisable(GL_CULL_FACE
);
444 struct world_pass pass
= {
445 .shader
= k_shader_standard_cutout
,
447 .fn_bind_textures
= bindpoint_diffuse_texture1
,
448 .fn_set_mdl
= shader_scene_standard_alphatest_uMdl
,
449 .fn_set_uPvmPrev
= shader_scene_standard_alphatest_uPvmPrev
,
452 world_render_both_stages( world
, &pass
);
454 glEnable(GL_CULL_FACE
);
457 static void render_world_foliage( world_instance
*world
, camera
*cam
){
458 shader_scene_foliage_use();
459 shader_scene_foliage_uTexGarbage(0);
460 shader_scene_foliage_uTexMain(1);
461 shader_scene_foliage_uPv( cam
->mtx
.pv
);
462 shader_scene_foliage_uTime( vg
.time
);
464 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_foliage
);
465 bind_terrain_noise();
467 shader_scene_foliage_uCamera( cam
->transform
[3] );
468 glDisable(GL_CULL_FACE
);
469 struct world_pass pass
= {
470 .shader
= k_shader_foliage
,
472 .fn_bind_textures
= bindpoint_diffuse_texture1
,
473 .fn_set_mdl
= shader_scene_foliage_uMdl
,
474 .fn_set_uPvmPrev
= shader_scene_foliage_uPvmPrev
,
476 world_render_both_stages( world
, &pass
);
477 glEnable(GL_CULL_FACE
);
480 static void world_render_challenges( world_instance
*world
,
481 struct world_pass
*pass
, v3f pos
){
483 if( skaterift
.activity
== k_skaterift_replay
) return;
484 if( world
!= world_current_instance() ) return;
489 u32 objective_list
[ 32 ],
490 challenge_list
[ 16 ];
492 v2f objective_uv_offsets
[ 32 ];
494 u32 objective_count
= 0,
497 ent_challenge
*active_challenge
= NULL
;
499 if( mdl_entity_id_type( world_static
.focused_entity
) == k_ent_challenge
){
500 if( (skaterift
.activity
== k_skaterift_default
) &&
501 world_static
.challenge_target
){
505 if( !((skaterift
.activity
!= k_skaterift_ent_focus
) &&
506 !world_static
.challenge_target
) ){
507 world_instance
*challenge_world
= world_current_instance();
508 u32 index
= mdl_entity_id_id( world_static
.focused_entity
);
509 active_challenge
= mdl_arritm(&challenge_world
->ent_challenge
, index
);
513 if( active_challenge
){
514 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
515 challenge_list
[ challenge_count
++ ] = world_static
.focused_entity
;
517 u32 next
= active_challenge
->first
;
518 while( mdl_entity_id_type(next
) == k_ent_objective
){
519 u32 index
= mdl_entity_id_id( next
);
520 objective_list
[ objective_count
++ ] = index
;
522 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
523 next
= objective
->id_next
;
529 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
531 bh_iter_init_range( 0, &it
, pos
, radius
+10.0f
);
533 while( bh_next( world
->entity_bh
, &it
, &idx
) ){
534 u32 id
= world
->entity_list
[ idx
],
535 type
= mdl_entity_id_type( id
),
536 index
= mdl_entity_id_id( id
);
538 if( type
== k_ent_objective
) {
539 if( objective_count
< vg_list_size(objective_list
) )
540 objective_list
[ objective_count
++ ] = index
;
542 else if( type
== k_ent_challenge
){
543 if( challenge_count
< vg_list_size(challenge_list
) )
544 challenge_list
[ challenge_count
++ ] = index
;
549 /* render objectives */
550 glDisable( GL_CULL_FACE
);
551 mesh_bind( &world
->mesh_no_collide
);
552 u32 last_material
= 0;
553 for( u32 i
=0; i
<objective_count
; i
++ ){
554 u32 index
= objective_list
[ i
];
555 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
556 if( (objective
->flags
& k_ent_objective_hidden
) &&
557 !active_challenge
) continue;
562 u32 passed
= objective
->flags
& k_ent_objective_passed
;
563 f32 target
= passed
? 0.0f
: 1.0f
;
564 vg_slewf(&objective
->transform
.s
[0], target
, vg
.time_frame_delta
*4.0f
);
565 scale
= vg_smoothstepf( objective
->transform
.s
[0] );
567 if( (objective
== world_static
.challenge_target
) || passed
)
568 shader_scene_fxglow_uUvOffset( (v2f
){ 16.0f
/256.0f
, 0.0f
} );
570 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
573 f32 dist
= v3_dist( objective
->transform
.co
, pos
) * (1.0f
/radius
);
574 scale
= vg_smoothstepf( vg_clampf( 5.0f
-dist
*5.0f
, 0.0f
,1.0f
) );
578 q_m3x3( objective
->transform
.q
, mmdl
);
579 m3x3_scalef( mmdl
, scale
);
580 v3_copy( objective
->transform
.co
, mmdl
[3] );
581 shader_scene_fxglow_uMdl( mmdl
);
583 for( u32 j
=0; j
<objective
->submesh_count
; j
++ ){
584 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
585 objective
->submesh_start
+ j
);
587 if( sm
->material_id
!= last_material
){
588 last_material
= sm
->material_id
;
589 pass
->fn_bind_textures( world
, &world
->surfaces
[sm
->material_id
] );
591 mdl_draw_submesh( sm
);
596 font3d_bind( &gui
.font
, k_font_shader_world
, 0, world
, &skaterift
.cam
);
601 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_challenge
); i
++ ){
602 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, i
);
603 if( challenge
->status
) count
++;
607 c
+=highscore_intl( buf
+c
, count
, 3 );
609 c
+=highscore_intl( buf
+c
, mdl_arrcount(&world
->ent_challenge
), 3 );
612 f32 w
= font3d_string_width( 1, buf
);
614 m3x3_identity( mlocal
);
615 mlocal
[3][0] = -w
*0.5f
;
619 for( u32 i
=0; i
<challenge_count
; i
++ ){
620 u32 index
= challenge_list
[ i
];
621 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, index
);
623 mdl_transform_m4x3( &challenge
->transform
, mmdl
);
624 m4x3_mul( mmdl
, mlocal
, mmdl
);
626 vg_line_point( challenge
->transform
.co
, 0.25f
, VG__RED
);
628 f32 dist
= v3_dist( challenge
->transform
.co
, pos
) * (1.0f
/radius
),
629 scale
= vg_smoothstepf( vg_clampf( 10.0f
-dist
*10.0f
, 0.0f
,1.0f
) ),
632 if( challenge
->status
)
635 shader_scene_font_uOpacity( scale
);
636 shader_scene_font_uColourize( colour
);
637 font3d_simple_draw( 1, buf
, &skaterift
.cam
, mmdl
);
641 static void render_world_fxglow( world_instance
*host_world
,
642 world_instance
*world
, camera
*cam
,
644 int generic
, int challenges
, int regions
){
645 shader_scene_fxglow_use();
646 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
647 shader_scene_fxglow_uTexMain(1);
648 shader_scene_fxglow_uPv( cam
->mtx
.pv
);
650 world_link_lighting_ub( host_world
, _shader_scene_fxglow
.id
);
651 world_bind_position_texture( host_world
, _shader_scene_fxglow
.id
,
652 _uniform_scene_fxglow_g_world_depth
, 2 );
653 world_bind_light_array( host_world
, _shader_scene_fxglow
.id
,
654 _uniform_scene_fxglow_uLightsArray
, 3 );
655 world_bind_light_index( host_world
, _shader_scene_fxglow
.id
,
656 _uniform_scene_fxglow_uLightsIndex
, 4 );
658 shader_scene_fxglow_uCamera( cam
->transform
[3] );
659 glDisable(GL_CULL_FACE
);
661 struct world_pass pass
= {
662 .shader
= k_shader_fxglow
,
664 .fn_bind_textures
= bindpoint_diffuse_texture1
,
665 .fn_set_mdl
= shader_scene_fxglow_uMdl
,
666 .fn_set_uPvmPrev
= shader_scene_fxglow_uPvmPrev
,
670 world_render_both_stages( world
, &pass
);
673 mesh_bind( &world
->mesh_no_collide
);
675 u32 last_material
= 0;
676 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_region
); i
++ ){
677 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
678 ent_region
*region
= mdl_arritm( &world
->ent_region
, i
);
681 if( region
->flags
& k_ent_route_flag_achieve_gold
)
683 else if( region
->flags
& k_ent_route_flag_achieve_silver
)
686 shader_scene_fxglow_uUvOffset( (v2f
){ (8.0f
/256.0f
)*offset
, 0.0f
} );
689 mdl_transform_m4x3( ®ion
->transform
, mmdl
);
690 m4x3_mul( world_mmdl
, mmdl
, mmdl
);
691 shader_scene_fxglow_uMdl( mmdl
);
693 for( u32 j
=0; j
<region
->submesh_count
; j
++ ){
694 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
695 region
->submesh_start
+ j
);
697 if( sm
->material_id
!= last_material
){
698 last_material
= sm
->material_id
;
699 pass
.fn_bind_textures(world
,&world
->surfaces
[sm
->material_id
]);
701 mdl_draw_submesh( sm
);
707 world_render_challenges( world
, &pass
, cam
->pos
);
709 glEnable(GL_CULL_FACE
);
712 static void bindpoint_terrain( world_instance
*world
,
713 struct world_surface
*mat
)
715 glActiveTexture( GL_TEXTURE1
);
716 glBindTexture( GL_TEXTURE_2D
,
717 world_get_texture(world
,mat
->info
.tex_diffuse
) );
719 shader_scene_terrain_uSandColour( mat
->info
.colour
);
720 shader_scene_terrain_uBlendOffset( mat
->info
.colour1
);
723 static void bindpoint_override( world_instance
*world
,
724 struct world_surface
*mat
){
725 if( mat
->info
.flags
& k_material_flag_collision
){
726 shader_scene_override_uAlphatest(0);
729 glActiveTexture( GL_TEXTURE1
);
730 glBindTexture( GL_TEXTURE_2D
,
731 world_get_texture(world
,mat
->info
.tex_diffuse
) );
732 shader_scene_override_uAlphatest(1);
736 static void render_terrain( world_instance
*world
, camera
*cam
){
737 shader_scene_terrain_use();
738 shader_scene_terrain_uTexGarbage(0);
739 shader_scene_terrain_uTexGradients(1);
741 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_terrain
);
742 glActiveTexture( GL_TEXTURE0
);
743 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
745 shader_scene_terrain_uPv( cam
->mtx
.pv
);
746 shader_scene_terrain_uCamera( cam
->transform
[3] );
748 struct world_pass pass
= {
749 .shader
= k_shader_terrain_blend
,
751 .fn_bind_textures
= bindpoint_terrain
,
752 .fn_set_mdl
= shader_scene_terrain_uMdl
,
753 .fn_set_uPvmPrev
= shader_scene_terrain_uPvmPrev
,
756 world_render_both_stages( world
, &pass
);
759 static void render_sky( world_instance
*world
, camera
*cam
){
761 * Modify matrix to remove clipping and view translation
768 m4x4_copy( cam
->mtx
.v
, v
);
769 m4x4_copy( cam
->mtx_prev
.v
, v_prev
);
771 for( int i
=0; i
<3; i
++ ){
773 v3_normalize(v_prev
[i
]);
776 v3_zero( v_prev
[3] );
778 m4x4_copy( cam
->mtx
.p
, pv
);
779 m4x4_copy( cam
->mtx_prev
.p
, pv_prev
);
780 m4x4_reset_clipping( pv
, 100.0f
, 0.1f
);
781 m4x4_reset_clipping( pv_prev
, 100.0f
, 0.1f
);
783 m4x4_mul( pv
, v
, pv
);
784 m4x4_mul( pv_prev
, v_prev
, pv_prev
);
786 m4x3f identity_matrix
;
787 m4x3_identity( identity_matrix
);
792 if( world
->skybox
== k_skybox_default
){
793 shader_model_sky_use();
794 shader_model_sky_uMdl( identity_matrix
);
795 shader_model_sky_uPv( pv
);
796 shader_model_sky_uPvmPrev( pv_prev
);
797 shader_model_sky_uTexGarbage(0);
798 world_link_lighting_ub( world
, _shader_model_sky
.id
);
800 glActiveTexture( GL_TEXTURE0
);
801 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
803 else if( world
->skybox
== k_skybox_space
){
804 shader_model_sky_space_use();
806 shader_model_sky_space_uMdl( identity_matrix
);
807 shader_model_sky_space_uPv( pv
);
808 shader_model_sky_space_uPvmPrev( pv_prev
);
809 shader_model_sky_space_uTexGarbage(0);
810 world_link_lighting_ub( world
, _shader_model_sky_space
.id
);
812 glActiveTexture( GL_TEXTURE0
);
813 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
819 glDepthMask( GL_FALSE
);
820 glDisable( GL_DEPTH_TEST
);
822 mesh_bind( &world_render
.skydome
);
823 mesh_draw( &world_render
.skydome
);
825 glEnable( GL_DEPTH_TEST
);
826 glDepthMask( GL_TRUE
);
829 static void render_world_gates( world_instance
*world
, camera
*cam
){
830 float closest
= INFINITY
;
831 struct ent_gate
*gate
= NULL
;
833 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
834 ent_gate
*gi
= mdl_arritm( &world
->ent_gate
, i
);
836 if( !(gi
->flags
& k_ent_gate_nonlocal
) )
837 if( !(gi
->flags
& k_ent_gate_linked
) )
840 float dist
= v3_dist2( gi
->co
[0], cam
->transform
[3] );
842 vg_line_point( gi
->co
[0], 0.25f
, VG__BLUE
);
844 if( dist
< closest
){
850 world
->rendering_gate
= gate
;
853 if( gate
->flags
& k_ent_gate_locked
){
854 world
->rendering_gate
= NULL
;
858 if( gate
->flags
& k_ent_gate_nonlocal
){
859 if( !(gate
->flags
& k_ent_gate_linked
) ||
860 (world_static
.load_state
!= k_world_loader_none
) ){
861 world
->rendering_gate
= NULL
;
862 render_gate_unlinked( world
, gate
, cam
);
866 world_instance
*dest_world
= &world_static
.instances
[ gate
->target
];
867 render_gate( world
, dest_world
, gate
, cam
);
870 render_gate( world
, world
, gate
, cam
);
874 static void world_prerender( world_instance
*world
){
875 if( mdl_arrcount( &world
->ent_light
) ){
876 f32 rate
= vg_maxf(0.1f
, fabsf(k_day_length
)) * vg_signf(k_day_length
);
877 world
->time
+= vg
.time_frame_delta
* (1.0/(rate
*60.0));
883 if( world
->info
.flags
& 0x1 ){
884 world
->time
= world
->info
.timezone
;
887 struct ub_world_lighting
*state
= &world
->ub_lighting
;
889 state
->g_time
= world
->time
;
890 state
->g_realtime
= vg
.time_real
;
891 state
->g_debug_indices
= k_debug_light_indices
;
892 state
->g_light_preview
= k_light_preview
;
893 state
->g_debug_complexity
= k_debug_light_complexity
;
894 state
->g_time_of_day
= vg_fractf( world
->time
);
896 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
897 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
899 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
900 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
902 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
903 state
->g_sun_dir
[0] = sinf( a
);
904 state
->g_sun_dir
[1] = cosf( a
);
905 state
->g_sun_dir
[2] = 0.2f
;
906 v3_normalize( state
->g_sun_dir
);
908 world
->probabilities
[ k_probability_curve_constant
] = 1.0f
;
909 float dp
= state
->g_day_phase
;
911 world
->probabilities
[ k_probability_curve_wildlife_day
] =
912 (dp
*dp
*0.8f
+state
->g_sunset_phase
)*0.8f
;
913 world
->probabilities
[ k_probability_curve_wildlife_night
] =
914 1.0f
-powf(fabsf((state
->g_time_of_day
-0.5f
)*5.0f
),5.0f
);
916 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
917 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
918 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
921 static void render_world( world_instance
*world
, camera
*cam
,
922 int stenciled
, int viewing_from_gate
,
923 int with_water
, int with_cubemaps
){
925 glClear( GL_DEPTH_BUFFER_BIT
);
926 glStencilFunc( GL_EQUAL
, 1, 0xFF );
927 glStencilMask( 0x00 );
928 glEnable( GL_CULL_FACE
);
929 glEnable( GL_STENCIL_TEST
);
932 glStencilMask( 0xFF );
933 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
934 glDisable( GL_STENCIL_TEST
);
937 render_sky( world
, cam
);
940 m4x3_identity(identity
);
941 render_world_routes( world
, world
, identity
, cam
, viewing_from_gate
, 0 );
942 render_world_standard( world
, cam
);
943 render_world_cubemapped( world
, cam
, with_cubemaps
);
945 render_world_vb( world
, cam
);
946 render_world_alphatest( world
, cam
);
947 render_world_foliage( world
, cam
);
948 render_terrain( world
, cam
);
950 if( !viewing_from_gate
){
951 world_entity_focus_render();
955 float min_dist
= INFINITY
;
957 if( mdl_arrcount( &world
->ent_route
) ){
958 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
959 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
960 float dist
= v3_dist2( route
->board_transform
[3], cam
->pos
);
962 if( dist
< min_dist
){
968 ent_route
*route
= mdl_arritm( &world
->ent_route
, closest
);
969 sfd_render( world
, cam
, route
->board_transform
);
973 if( !viewing_from_gate
){
975 if( mdl_entity_id_type(world_static
.focused_entity
) == k_ent_challenge
)
976 greyout
= world_static
.focus_strength
;
978 if( greyout
> 0.0f
){
979 glDrawBuffers( 1, (GLenum
[]){ GL_COLOR_ATTACHMENT0
} );
981 glDisable(GL_DEPTH_TEST
);
982 glDepthMask(GL_FALSE
);
983 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
984 glBlendEquation(GL_FUNC_ADD
);
986 shader_blitcolour_use();
987 shader_blitcolour_uColour( (v4f
){ 0.5f
, 0.5f
, 0.5f
, greyout
*0.56f
} );
991 glEnable(GL_DEPTH_TEST
);
992 glDepthMask(GL_TRUE
);
993 glDrawBuffers( 2, (GLenum
[]){ GL_COLOR_ATTACHMENT0
,
994 GL_COLOR_ATTACHMENT1
} );
997 render_world_fxglow( world
, world
, cam
, NULL
, 1, 1, 0 );
1001 render_water_texture( world
, cam
);
1002 render_fb_bind( gpipeline
.fb_main
, 1 );
1006 glStencilFunc( GL_EQUAL
, 1, 0xFF );
1007 glStencilMask( 0x00 );
1008 glEnable( GL_CULL_FACE
);
1009 glEnable( GL_STENCIL_TEST
);
1013 render_water_surface( world
, cam
);
1016 render_remote_players( world
, cam
);
1017 ent_miniworld_render( world
, cam
);
1020 glStencilMask( 0xFF );
1021 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
1022 glDisable( GL_STENCIL_TEST
);
1027 static void render_world_override_pass( world_instance
*world
,
1028 struct world_pass
*pass
,
1029 m4x3f mmdl
, m3x3f mnormal
,
1031 for( int i
=0; i
<world
->surface_count
; i
++ ){
1032 struct world_surface
*mat
= &world
->surfaces
[i
];
1034 if( mat
->info
.flags
& k_material_flag_ghosts
) continue;
1037 if( pass
->geo_type
== k_world_geo_type_solid
)
1040 sm
= &mat
->sm_no_collide
;
1042 if( !sm
->indice_count
)
1045 pass
->fn_set_mdl( mmdl
);
1046 pass
->fn_set_uNormalMtx( mnormal
);
1047 pass
->fn_set_uPvmPrev( mpvm_prev
);
1048 pass
->fn_bind_textures( world
, mat
);
1049 mdl_draw_submesh( sm
);
1053 static void render_world_override( world_instance
*world
,
1054 world_instance
*lighting_source
,
1057 ent_spawn
*dest_spawn
, v4f map_info
){
1058 struct world_pass pass
= {
1060 .fn_bind_textures
= bindpoint_override
,
1061 .fn_set_mdl
= shader_scene_override_uMdl
,
1062 .fn_set_uPvmPrev
= shader_scene_override_uPvmPrev
,
1063 .fn_set_uNormalMtx
= shader_scene_override_uNormalMtx
,
1064 .shader
= k_shader_override
1067 shader_scene_override_use();
1068 shader_scene_override_uTexGarbage(0);
1069 shader_scene_override_uTexMain(1);
1070 shader_scene_override_uPv( pass
.cam
->mtx
.pv
);
1071 shader_scene_override_uMapInfo( map_info
);
1073 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( lighting_source
, scene_override
);
1074 bind_terrain_noise();
1076 shader_scene_override_uCamera( pass
.cam
->transform
[3] );
1079 m4x3_expand( mmdl
, mpvm_prev
);
1080 m4x4_mul( cam
->mtx_prev
.pv
, mpvm_prev
, mpvm_prev
);
1083 m3x3_inv( mmdl
, mnormal
);
1084 m3x3_transpose( mnormal
, mnormal
);
1085 v3_normalize( mnormal
[0] );
1086 v3_normalize( mnormal
[1] );
1087 v3_normalize( mnormal
[2] );
1089 v4f uPlayerPos
, uSpawnPos
;
1090 v4_zero( uPlayerPos
);
1091 v4_zero( uSpawnPos
);
1093 v3_copy( world
->player_co
, uPlayerPos
);
1095 if( dest_spawn
&& (v3_dist2(dest_spawn
->transform
.co
,uPlayerPos
) > 0.1f
) )
1096 v3_copy( dest_spawn
->transform
.co
, uSpawnPos
);
1098 v3_add( uPlayerPos
, (v3f
){0,-1,0}, uSpawnPos
);
1100 uPlayerPos
[3] = v3_dist(uPlayerPos
,uSpawnPos
);
1101 uSpawnPos
[3] = 1.0f
/uPlayerPos
[3];
1103 shader_scene_override_uPlayerPos( uPlayerPos
);
1104 shader_scene_override_uSpawnPos( uSpawnPos
);
1107 glDisable( GL_CULL_FACE
);
1108 mesh_bind( &world
->mesh_geo
);
1109 pass
.geo_type
= k_world_geo_type_solid
;
1110 render_world_override_pass( world
, &pass
, mmdl
, mnormal
, mpvm_prev
);
1111 mesh_bind( &world
->mesh_no_collide
);
1112 pass
.geo_type
= k_world_geo_type_nonsolid
;
1113 render_world_override_pass( world
, &pass
, mmdl
, mnormal
, mpvm_prev
);
1114 glEnable( GL_CULL_FACE
);
1116 render_world_fxglow( world
, world
, cam
, mmdl
, 0, 0, 1 );
1119 static void render_cubemap_side( world_instance
*world
, ent_cubemap
*cm
,
1122 glFramebufferTexture2D( GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
1123 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ side
, cm
->texture_id
, 0 );
1124 glClear( GL_DEPTH_BUFFER_BIT
);
1127 { -1.0f
, 0.0f
, 0.0f
},
1128 { 1.0f
, 0.0f
, 0.0f
},
1129 { 0.0f
, -1.0f
, 0.0f
},
1130 { 0.0f
, 1.0f
, 0.0f
},
1131 { 0.0f
, 0.0f
, -1.0f
},
1132 { 0.0f
, 0.0f
, 1.0f
}
1135 { 0.0f
, -1.0f
, 0.0f
},
1136 { 0.0f
, -1.0f
, 0.0f
},
1137 { 0.0f
, 0.0f
, 1.0f
},
1138 { 0.0f
, 0.0f
, -1.0f
},
1139 { 0.0f
, -1.0f
, 0.0f
},
1140 { 0.0f
, -1.0f
, 0.0f
}
1143 v3_zero( cam
.angles
);
1144 v3_copy( cm
->co
, cam
.pos
);
1146 v3_copy( forward
[side
], cam
.transform
[2] );
1147 v3_copy( up
[side
], cam
.transform
[1] );
1148 v3_cross( up
[side
], forward
[side
], cam
.transform
[0] );
1149 v3_copy( cm
->co
, cam
.transform
[3] );
1150 m4x3_invert_affine( cam
.transform
, cam
.transform_inverse
);
1152 camera_update_view( &cam
);
1157 m4x4_copy( cam
.mtx
.p
, cam
.mtx_prev
.p
);
1158 m4x4_projection( cam
.mtx
.p
, cam
.fov
, 1.0f
, cam
.nearz
, cam
.farz
);
1159 camera_finalize( &cam
);
1160 camera_finalize( &cam
);
1162 render_world( world
, &cam
, 0, 1, 1, 0 );
1165 static void render_world_cubemaps( world_instance
*world
){
1166 if( world
->cubemap_cooldown
)
1167 world
->cubemap_cooldown
--;
1169 world
->cubemap_cooldown
= 60;
1171 glViewport( 0, 0, WORLD_CUBEMAP_RES
, WORLD_CUBEMAP_RES
);
1172 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_cubemap
); i
++ ){
1173 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, i
);
1174 glBindFramebuffer( GL_FRAMEBUFFER
, cm
->framebuffer_id
);
1176 world
->cubemap_side
++;
1177 if( world
->cubemap_side
>= 6 )
1178 world
->cubemap_side
= 0;
1180 render_cubemap_side( world
, cm
, world
->cubemap_side
);
1185 static void render_world_depth( world_instance
*world
, camera
*cam
){
1186 m4x3f identity_matrix
;
1187 m4x3_identity( identity_matrix
);
1189 shader_scene_depth_use();
1190 shader_scene_depth_uCamera( cam
->transform
[3] );
1191 shader_scene_depth_uPv( cam
->mtx
.pv
);
1192 shader_scene_depth_uPvmPrev( cam
->mtx_prev
.pv
);
1193 shader_scene_depth_uMdl( identity_matrix
);
1194 world_link_lighting_ub( world
, _shader_scene_depth
.id
);
1196 mesh_bind( &world
->mesh_geo
);
1197 mesh_draw( &world
->mesh_geo
);
1200 static void render_world_position( world_instance
*world
, camera
*cam
){
1201 m4x3f identity_matrix
;
1202 m4x3_identity( identity_matrix
);
1204 shader_scene_position_use();
1205 shader_scene_position_uCamera( cam
->transform
[3] );
1206 shader_scene_position_uPv( cam
->mtx
.pv
);
1207 shader_scene_position_uPvmPrev( cam
->mtx_prev
.pv
);
1208 shader_scene_position_uMdl( identity_matrix
);
1209 world_link_lighting_ub( world
, _shader_scene_position
.id
);
1211 mesh_bind( &world
->mesh_geo
);
1212 mesh_draw( &world
->mesh_geo
);