2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
9 #include "world_render.h"
14 static int ccmd_set_time( int argc
, const char *argv
[] ){
15 world_instance
*world
= world_current_instance();
17 world
->time
= atof( argv
[0] );
19 vg_error( "Usage set_time <0-1.0> (current time: %f)\n", world
->time
);
23 static void async_world_render_init( void *payload
, u32 size
)
25 vg_info( "Allocate uniform buffers\n" );
26 for( int i
=0; i
<k_world_max
; i
++ ){
27 world_instance
*world
= &world_static
.instances
[i
];
28 world
->ubo_bind_point
= i
;
30 glGenBuffers( 1, &world
->ubo_lighting
);
31 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
32 glBufferData( GL_UNIFORM_BUFFER
, sizeof(struct ub_world_lighting
),
33 NULL
, GL_DYNAMIC_DRAW
);
35 glBindBufferBase( GL_UNIFORM_BUFFER
, i
, world
->ubo_lighting
);
39 vg_info( "Allocate frame buffers\n" );
40 for( int i
=0; i
<k_world_max
; i
++ ){
41 world_instance
*world
= &world_static
.instances
[i
];
42 struct framebuffer
*fb
= &world
->heightmap
;
44 fb
->display_name
= NULL
;
48 fb
->resolution_div
= 0;
50 fb
->attachments
[0].display_name
= NULL
;
51 fb
->attachments
[0].purpose
= k_framebuffer_attachment_type_texture
;
52 fb
->attachments
[0].internalformat
= GL_RG16F
;
53 fb
->attachments
[0].format
= GL_RG
;
54 fb
->attachments
[0].type
= GL_FLOAT
;
55 fb
->attachments
[0].attachment
= GL_COLOR_ATTACHMENT0
;
57 fb
->attachments
[1].purpose
= k_framebuffer_attachment_type_none
;
58 fb
->attachments
[2].purpose
= k_framebuffer_attachment_type_none
;
59 fb
->attachments
[3].purpose
= k_framebuffer_attachment_type_none
;
60 fb
->attachments
[4].purpose
= k_framebuffer_attachment_type_none
;
62 render_fb_allocate( fb
);
66 static void world_render_init(void)
68 VG_VAR_F32( k_day_length
);
69 VG_VAR_I32( k_debug_light_indices
);
70 VG_VAR_I32( k_debug_light_complexity
);
71 VG_VAR_I32( k_light_preview
);
72 vg_console_reg_cmd( "set_time", ccmd_set_time
, NULL
);
74 world_render
.sky_rate
= 1.0;
75 world_render
.sky_target_rate
= 1.0;
77 shader_scene_standard_register();
78 shader_scene_standard_alphatest_register();
79 shader_scene_override_register();
80 shader_scene_cubemapped_register();
81 shader_scene_fxglow_register();
82 shader_scene_vertex_blend_register();
83 shader_scene_terrain_register();
84 shader_scene_depth_register();
85 shader_scene_position_register();
86 shader_model_sky_register();
88 vg_info( "Loading world resources\n" );
89 vg_linear_clear( vg_mem
.scratch
);
92 mdl_open( &msky
, "models/rs_skydome.mdl", vg_mem
.scratch
);
93 mdl_load_metadata_block( &msky
, vg_mem
.scratch
);
94 mdl_async_load_glmesh( &msky
, &world_render
.skydome
);
97 vg_info( "Loading default world textures\n" );
98 vg_tex2d_load_qoi_async_file( "textures/garbage.qoi",
99 VG_TEX2D_NEAREST
|VG_TEX2D_REPEAT
,
100 &world_render
.tex_terrain_noise
);
102 vg_async_call( async_world_render_init
, NULL
, 0 );
105 static void world_link_lighting_ub( world_instance
*world
, GLuint shader
){
106 GLuint idx
= glGetUniformBlockIndex( shader
, "ub_world_lighting" );
107 glUniformBlockBinding( shader
, idx
, world
->ubo_bind_point
);
110 static void world_bind_position_texture( world_instance
*world
,
111 GLuint shader
, GLuint location
,
113 render_fb_bind_texture( &world
->heightmap
, 0, slot
);
114 glUniform1i( location
, slot
);
117 static void world_bind_light_array( world_instance
*world
,
118 GLuint shader
, GLuint location
,
120 glActiveTexture( GL_TEXTURE0
+ slot
);
121 glBindTexture( GL_TEXTURE_BUFFER
, world
->tex_light_entities
);
122 glUniform1i( location
, slot
);
125 static void world_bind_light_index( world_instance
*world
,
126 GLuint shader
, GLuint location
,
128 glActiveTexture( GL_TEXTURE0
+ slot
);
129 glBindTexture( GL_TEXTURE_3D
, world
->tex_light_cubes
);
130 glUniform1i( location
, slot
);
133 static void render_world_depth( world_instance
*world
, camera
*cam
);
139 static void bind_terrain_noise(void){
140 glActiveTexture( GL_TEXTURE0
);
141 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
146 enum mdl_shader shader
;
147 enum world_geo_type geo_type
;
149 void (*fn_bind_textures
)( world_instance
*world
,
150 struct world_surface
*mat
);
151 void (*fn_set_mdl
)( m4x3f mdl
);
152 void (*fn_set_uPvmPrev
)( m4x4f pvm
);
156 void world_render_traffic( world_instance
*world
, u32 material_id
,
157 struct world_pass
*pass
){
158 if( !mdl_arrcount( &world
->ent_traffic
) ) return;
160 /* HACK: use the first material for every traffic entity */
161 ent_traffic
*first
= mdl_arritm( &world
->ent_traffic
, 0 );
162 if( !first
->submesh_count
) return;
164 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
, first
->submesh_start
);
165 if( sm
->material_id
!= material_id
) return;
167 struct world_surface
*mat
= &world
->surfaces
[ material_id
];
168 pass
->fn_bind_textures( world
, mat
);
170 for( u32 j
=0; j
<mdl_arrcount( &world
->ent_traffic
); j
++ ){
171 ent_traffic
*traffic
= mdl_arritm( &world
->ent_traffic
, j
);
173 for( u32 k
=0; k
<traffic
->submesh_count
; k
++ ){
174 sm
= mdl_arritm( &world
->meta
.submeshs
,
175 traffic
->submesh_start
+k
);
178 q_m3x3( traffic
->transform
.q
, mmdl
);
179 v3_copy( traffic
->transform
.co
, mmdl
[3] );
182 m4x3_expand( mmdl
, m4mdl
);
183 m4x4_mul( pass
->cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
185 pass
->fn_set_mdl( mmdl
);
186 pass
->fn_set_uPvmPrev( m4mdl
);
188 mdl_draw_submesh( sm
);
194 void world_render_pass( world_instance
*world
, struct world_pass
*pass
){
195 for( int i
=0; i
<world
->surface_count
; i
++ ){
196 struct world_surface
*mat
= &world
->surfaces
[i
];
198 if( mat
->info
.shader
== pass
->shader
){
201 if( pass
->geo_type
== k_world_geo_type_solid
){
205 world_render_traffic( world
, i
, pass
);
206 sm
= &mat
->sm_no_collide
;
209 if( !sm
->indice_count
)
213 m4x3_identity( mmdl
);
214 pass
->fn_set_mdl( mmdl
);
215 pass
->fn_set_uPvmPrev( pass
->cam
->mtx_prev
.pv
);
217 pass
->fn_bind_textures( world
, mat
);
218 mdl_draw_submesh( sm
);
224 void world_render_both_stages( world_instance
*world
, struct world_pass
*pass
)
226 mesh_bind( &world
->mesh_geo
);
227 pass
->geo_type
= k_world_geo_type_solid
;
228 world_render_pass( world
, pass
);
230 glDisable( GL_CULL_FACE
);
231 mesh_bind( &world
->mesh_no_collide
);
232 pass
->geo_type
= k_world_geo_type_nonsolid
;
233 world_render_pass( world
, pass
);
234 glEnable( GL_CULL_FACE
);
237 static void bindpoint_diffuse_texture1( world_instance
*world
,
238 struct world_surface
*mat
)
241 glActiveTexture( GL_TEXTURE1
);
242 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
245 static void bindpoint_diffuse1_and_cubemap10( world_instance
*world
,
246 struct world_surface
*mat
){
247 glActiveTexture( GL_TEXTURE1
);
248 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
250 u32 cubemap_id
= mat
->info
.tex_none0
,
253 if( mdl_entity_id_type( cubemap_id
) == k_ent_cubemap
){
254 cubemap_index
= mdl_entity_id_id( cubemap_id
);
257 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, cubemap_index
);
258 glActiveTexture( GL_TEXTURE10
);
259 glBindTexture( GL_TEXTURE_CUBE_MAP
, cm
->texture_id
);
261 shader_scene_cubemapped_uColour( mat
->info
.colour
);
264 static void render_world_vb( world_instance
*world
, camera
*cam
){
265 shader_scene_vertex_blend_use();
266 shader_scene_vertex_blend_uTexGarbage(0);
267 shader_scene_vertex_blend_uTexGradients(1);
268 world_link_lighting_ub( world
, _shader_scene_vertex_blend
.id
);
269 world_bind_position_texture( world
, _shader_scene_vertex_blend
.id
,
270 _uniform_scene_vertex_blend_g_world_depth
, 2 );
271 world_bind_light_array( world
, _shader_scene_vertex_blend
.id
,
272 _uniform_scene_vertex_blend_uLightsArray
, 3 );
273 world_bind_light_index( world
, _shader_scene_vertex_blend
.id
,
274 _uniform_scene_vertex_blend_uLightsIndex
, 4 );
276 glActiveTexture( GL_TEXTURE0
);
277 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
279 shader_scene_vertex_blend_uPv( cam
->mtx
.pv
);
280 shader_scene_vertex_blend_uCamera( cam
->transform
[3] );
282 struct world_pass pass
= {
283 .shader
= k_shader_standard_vertex_blend
,
285 .fn_bind_textures
= bindpoint_diffuse_texture1
,
286 .fn_set_mdl
= shader_scene_vertex_blend_uMdl
,
287 .fn_set_uPvmPrev
= shader_scene_vertex_blend_uPvmPrev
,
290 world_render_both_stages( world
, &pass
);
293 static void world_shader_standard_bind( world_instance
*world
, camera
*cam
){
294 shader_scene_standard_use();
295 shader_scene_standard_uTexGarbage(0);
296 shader_scene_standard_uTexMain(1);
297 shader_scene_standard_uPv( cam
->mtx
.pv
);
299 world_link_lighting_ub( world
, _shader_scene_standard
.id
);
300 world_bind_position_texture( world
, _shader_scene_standard
.id
,
301 _uniform_scene_standard_g_world_depth
, 2 );
302 world_bind_light_array( world
, _shader_scene_standard
.id
,
303 _uniform_scene_standard_uLightsArray
, 3 );
304 world_bind_light_index( world
, _shader_scene_standard
.id
,
305 _uniform_scene_standard_uLightsIndex
, 4 );
307 bind_terrain_noise();
308 shader_scene_standard_uCamera( cam
->transform
[3] );
311 static void render_world_standard( world_instance
*world
, camera
*cam
){
312 world_shader_standard_bind( world
, cam
);
313 struct world_pass pass
= {
314 .shader
= k_shader_standard
,
316 .fn_bind_textures
= bindpoint_diffuse_texture1
,
317 .fn_set_mdl
= shader_scene_standard_uMdl
,
318 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
321 world_render_both_stages( world
, &pass
);
324 static void render_world_cubemapped( world_instance
*world
, camera
*cam
,
326 if( !mdl_arrcount( &world
->ent_cubemap
) )
329 if( layer_depth
== -1 ){
330 world_shader_standard_bind( world
, cam
);
332 struct world_pass pass
= {
333 .shader
= k_shader_cubemap
,
335 .fn_bind_textures
= bindpoint_diffuse_texture1
,
336 .fn_set_mdl
= shader_scene_standard_uMdl
,
337 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
340 world_render_both_stages( world
, &pass
);
343 shader_scene_cubemapped_use();
344 shader_scene_cubemapped_uTexGarbage(0);
345 shader_scene_cubemapped_uTexMain(1);
346 shader_scene_cubemapped_uTexCubemap(10);
347 shader_scene_cubemapped_uPv( cam
->mtx
.pv
);
349 world_link_lighting_ub( world
, _shader_scene_cubemapped
.id
);
350 world_bind_position_texture( world
, _shader_scene_cubemapped
.id
,
351 _uniform_scene_cubemapped_g_world_depth
, 2 );
352 world_bind_light_array( world
, _shader_scene_cubemapped
.id
,
353 _uniform_scene_cubemapped_uLightsArray
, 3 );
354 world_bind_light_index( world
, _shader_scene_cubemapped
.id
,
355 _uniform_scene_cubemapped_uLightsIndex
, 4 );
357 bind_terrain_noise();
358 shader_scene_cubemapped_uCamera( cam
->transform
[3] );
360 struct world_pass pass
= {
361 .shader
= k_shader_cubemap
,
363 .fn_bind_textures
= bindpoint_diffuse1_and_cubemap10
,
364 .fn_set_mdl
= shader_scene_cubemapped_uMdl
,
365 .fn_set_uPvmPrev
= shader_scene_cubemapped_uPvmPrev
,
368 world_render_both_stages( world
, &pass
);
372 static void render_world_alphatest( world_instance
*world
, camera
*cam
){
373 shader_scene_standard_alphatest_use();
374 shader_scene_standard_alphatest_uTexGarbage(0);
375 shader_scene_standard_alphatest_uTexMain(1);
376 shader_scene_standard_alphatest_uPv( cam
->mtx
.pv
);
378 world_link_lighting_ub( world
, _shader_scene_standard_alphatest
.id
);
379 world_bind_position_texture( world
, _shader_scene_standard_alphatest
.id
,
380 _uniform_scene_standard_alphatest_g_world_depth
, 2 );
381 world_bind_light_array( world
, _shader_scene_standard_alphatest
.id
,
382 _uniform_scene_standard_alphatest_uLightsArray
, 3 );
383 world_bind_light_index( world
, _shader_scene_standard_alphatest
.id
,
384 _uniform_scene_standard_alphatest_uLightsIndex
, 4 );
387 bind_terrain_noise();
390 shader_scene_standard_alphatest_uCamera( cam
->transform
[3] );
392 glDisable(GL_CULL_FACE
);
394 struct world_pass pass
= {
395 .shader
= k_shader_standard_cutout
,
397 .fn_bind_textures
= bindpoint_diffuse_texture1
,
398 .fn_set_mdl
= shader_scene_standard_alphatest_uMdl
,
399 .fn_set_uPvmPrev
= shader_scene_standard_alphatest_uPvmPrev
,
402 world_render_both_stages( world
, &pass
);
404 glEnable(GL_CULL_FACE
);
408 void world_render_challenges( world_instance
*world
, struct world_pass
*pass
,
409 v3f pos
, int layer_depth
){
411 if( skaterift
.activity
== k_skaterift_replay
) return;
412 if( world
!= world_current_instance() ) return;
417 u32 objective_list
[ 32 ],
418 challenge_list
[ 16 ];
420 v2f objective_uv_offsets
[ 32 ];
422 u32 objective_count
= 0,
425 ent_challenge
*active_challenge
= NULL
;
427 if( mdl_entity_id_type( world_static
.focused_entity
) == k_ent_challenge
){
428 if( (skaterift
.activity
== k_skaterift_default
) &&
429 world_static
.challenge_target
){
433 if( !((skaterift
.activity
!= k_skaterift_ent_focus
) &&
434 !world_static
.challenge_target
) ){
435 world_instance
*challenge_world
= world_current_instance();
436 u32 index
= mdl_entity_id_id( world_static
.focused_entity
);
437 active_challenge
= mdl_arritm(&challenge_world
->ent_challenge
, index
);
441 if( active_challenge
){
442 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
443 challenge_list
[ challenge_count
++ ] = world_static
.focused_entity
;
445 u32 next
= active_challenge
->first
;
446 while( mdl_entity_id_type(next
) == k_ent_objective
){
447 u32 index
= mdl_entity_id_id( next
);
448 objective_list
[ objective_count
++ ] = index
;
450 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
451 next
= objective
->id_next
;
457 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
459 bh_iter_init_range( 0, &it
, pos
, radius
+10.0f
);
461 while( bh_next( world
->entity_bh
, &it
, &idx
) ){
462 u32 id
= world
->entity_list
[ idx
],
463 type
= mdl_entity_id_type( id
),
464 index
= mdl_entity_id_id( id
);
466 if( type
== k_ent_objective
) {
467 if( objective_count
< vg_list_size(objective_list
) )
468 objective_list
[ objective_count
++ ] = index
;
470 else if( type
== k_ent_challenge
){
471 if( challenge_count
< vg_list_size(challenge_list
) )
472 challenge_list
[ challenge_count
++ ] = index
;
477 /* render objectives */
478 glDisable( GL_CULL_FACE
);
479 mesh_bind( &world
->mesh_no_collide
);
480 u32 last_material
= 0;
481 for( u32 i
=0; i
<objective_count
; i
++ ){
482 u32 index
= objective_list
[ i
];
483 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
484 if( (objective
->flags
& k_ent_objective_hidden
) &&
485 !active_challenge
) continue;
490 u32 passed
= objective
->flags
& k_ent_objective_passed
;
491 f32 target
= passed
? 0.0f
: 1.0f
;
492 vg_slewf(&objective
->transform
.s
[0], target
, vg
.time_frame_delta
*4.0f
);
493 scale
= vg_smoothstepf( objective
->transform
.s
[0] );
495 if( (objective
== world_static
.challenge_target
) || passed
)
496 shader_scene_fxglow_uUvOffset( (v2f
){ 16.0f
/256.0f
, 0.0f
} );
498 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
501 f32 dist
= v3_dist( objective
->transform
.co
, pos
) * (1.0f
/radius
);
502 scale
= vg_smoothstepf( vg_clampf( 5.0f
-dist
*5.0f
, 0.0f
,1.0f
) );
506 q_m3x3( objective
->transform
.q
, mmdl
);
507 m3x3_scalef( mmdl
, scale
);
508 v3_copy( objective
->transform
.co
, mmdl
[3] );
509 shader_scene_fxglow_uMdl( mmdl
);
511 for( u32 j
=0; j
<objective
->submesh_count
; j
++ ){
512 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
513 objective
->submesh_start
+ j
);
515 if( sm
->material_id
!= last_material
){
516 last_material
= sm
->material_id
;
517 pass
->fn_bind_textures( world
, &world
->surfaces
[sm
->material_id
] );
519 mdl_draw_submesh( sm
);
524 font3d_bind( &gui
.font
, k_font_shader_world
, 0, world
, &skaterift
.cam
);
529 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_challenge
); i
++ ){
530 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, i
);
531 if( challenge
->status
) count
++;
535 c
+=highscore_intl( buf
+c
, count
, 3 );
537 c
+=highscore_intl( buf
+c
, mdl_arrcount(&world
->ent_challenge
), 3 );
540 f32 w
= font3d_string_width( 1, buf
);
542 m3x3_identity( mlocal
);
543 mlocal
[3][0] = -w
*0.5f
;
547 for( u32 i
=0; i
<challenge_count
; i
++ ){
548 u32 index
= challenge_list
[ i
];
549 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, index
);
551 mdl_transform_m4x3( &challenge
->transform
, mmdl
);
552 m4x3_mul( mmdl
, mlocal
, mmdl
);
554 vg_line_point( challenge
->transform
.co
, 0.25f
, VG__RED
);
556 f32 dist
= v3_dist( challenge
->transform
.co
, pos
) * (1.0f
/radius
),
557 scale
= vg_smoothstepf( vg_clampf( 10.0f
-dist
*10.0f
, 0.0f
,1.0f
) ),
560 if( challenge
->status
)
563 shader_scene_font_uOpacity( scale
);
564 shader_scene_font_uColourize( colour
);
565 font3d_simple_draw( 1, buf
, &skaterift
.cam
, mmdl
);
569 static void render_world_fxglow( world_instance
*world
, camera
*cam
,
571 shader_scene_fxglow_use();
572 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
573 shader_scene_fxglow_uTexMain(1);
574 shader_scene_fxglow_uPv( cam
->mtx
.pv
);
576 world_link_lighting_ub( world
, _shader_scene_fxglow
.id
);
577 world_bind_position_texture( world
, _shader_scene_fxglow
.id
,
578 _uniform_scene_fxglow_g_world_depth
, 2 );
579 world_bind_light_array( world
, _shader_scene_fxglow
.id
,
580 _uniform_scene_fxglow_uLightsArray
, 3 );
581 world_bind_light_index( world
, _shader_scene_fxglow
.id
,
582 _uniform_scene_fxglow_uLightsIndex
, 4 );
584 shader_scene_fxglow_uCamera( cam
->transform
[3] );
585 glDisable(GL_CULL_FACE
);
587 struct world_pass pass
= {
588 .shader
= k_shader_fxglow
,
590 .fn_bind_textures
= bindpoint_diffuse_texture1
,
591 .fn_set_mdl
= shader_scene_fxglow_uMdl
,
592 .fn_set_uPvmPrev
= shader_scene_fxglow_uPvmPrev
,
595 world_render_both_stages( world
, &pass
);
596 world_render_challenges( world
, &pass
, cam
->pos
, layer_depth
);
598 glEnable(GL_CULL_FACE
);
601 static void bindpoint_terrain( world_instance
*world
,
602 struct world_surface
*mat
)
604 glActiveTexture( GL_TEXTURE1
);
605 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
607 shader_scene_terrain_uSandColour( mat
->info
.colour
);
608 shader_scene_terrain_uBlendOffset( mat
->info
.colour1
);
611 static void bindpoint_override( world_instance
*world
,
612 struct world_surface
*mat
){
613 if( mat
->info
.flags
& k_material_flag_collision
){
614 shader_scene_override_uAlphatest(0);
617 glActiveTexture( GL_TEXTURE1
);
618 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
619 shader_scene_override_uAlphatest(1);
623 static void render_terrain( world_instance
*world
, camera
*cam
){
624 shader_scene_terrain_use();
625 shader_scene_terrain_uTexGarbage(0);
626 shader_scene_terrain_uTexGradients(1);
628 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_terrain
);
629 glActiveTexture( GL_TEXTURE0
);
630 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
632 shader_scene_terrain_uPv( cam
->mtx
.pv
);
633 shader_scene_terrain_uCamera( cam
->transform
[3] );
635 struct world_pass pass
= {
636 .shader
= k_shader_terrain_blend
,
638 .fn_bind_textures
= bindpoint_terrain
,
639 .fn_set_mdl
= shader_scene_terrain_uMdl
,
640 .fn_set_uPvmPrev
= shader_scene_terrain_uPvmPrev
,
643 world_render_both_stages( world
, &pass
);
646 static void render_sky( world_instance
*world
, camera
*cam
)
649 * Modify matrix to remove clipping and view translation
656 m4x4_copy( cam
->mtx
.v
, v
);
657 m4x4_copy( cam
->mtx_prev
.v
, v_prev
);
659 v3_zero( v_prev
[3] );
661 m4x4_copy( cam
->mtx
.p
, pv
);
662 m4x4_copy( cam
->mtx_prev
.p
, pv_prev
);
663 m4x4_reset_clipping( pv
, cam
->farz
, cam
->nearz
);
664 m4x4_reset_clipping( pv_prev
, cam
->farz
, cam
->nearz
);
666 m4x4_mul( pv
, v
, pv
);
667 m4x4_mul( pv_prev
, v_prev
, pv_prev
);
669 m4x3f identity_matrix
;
670 m4x3_identity( identity_matrix
);
675 shader_model_sky_use();
676 shader_model_sky_uMdl( identity_matrix
);
677 shader_model_sky_uPv( pv
);
678 shader_model_sky_uPvmPrev( pv_prev
);
679 shader_model_sky_uTexGarbage(0);
680 world_link_lighting_ub( world
, _shader_model_sky
.id
);
682 glActiveTexture( GL_TEXTURE0
);
683 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
685 glDepthMask( GL_FALSE
);
686 glDisable( GL_DEPTH_TEST
);
688 mesh_bind( &world_render
.skydome
);
689 mesh_draw( &world_render
.skydome
);
691 glEnable( GL_DEPTH_TEST
);
692 glDepthMask( GL_TRUE
);
695 static void render_world_gates( world_instance
*world
, camera
*cam
,
698 float closest
= INFINITY
;
699 struct ent_gate
*gate
= NULL
;
701 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
702 ent_gate
*gi
= mdl_arritm( &world
->ent_gate
, i
);
704 if( !(gi
->flags
& k_ent_gate_linked
) )
707 float dist
= v3_dist2( gi
->co
[0], cam
->transform
[3] );
709 vg_line_point( gi
->co
[0], 0.25f
, VG__BLUE
);
711 if( dist
< closest
){
717 world
->rendering_gate
= gate
;
719 if( gate
->flags
& k_ent_gate_locked
) return;
721 if( gate
->flags
& k_ent_gate_nonlocal
){
722 if( world_static
.load_state
!= k_world_loader_none
){
723 world
->rendering_gate
= NULL
;
727 world_instance
*dest_world
= &world_static
.instances
[ gate
->target
];
728 render_gate( world
, dest_world
, gate
, cam
, layer_depth
);
731 render_gate( world
, world
, gate
, cam
, layer_depth
);
736 static void world_prerender( world_instance
*world
){
737 if( mdl_arrcount( &world
->ent_light
) ){
738 f32 rate
= vg_maxf(0.1f
, fabsf(k_day_length
)) * vg_signf(k_day_length
);
739 world
->time
+= vg
.time_frame_delta
* (1.0/(rate
*60.0));
745 struct ub_world_lighting
*state
= &world
->ub_lighting
;
747 state
->g_time
= world
->time
;
748 state
->g_realtime
= vg
.time_real
;
749 state
->g_debug_indices
= k_debug_light_indices
;
750 state
->g_light_preview
= k_light_preview
;
751 state
->g_debug_complexity
= k_debug_light_complexity
;
753 if( skaterift
.activity
== k_skaterift_respawning
)
754 state
->g_time_of_day
= 0.1f
;
756 state
->g_time_of_day
= vg_fractf( world
->time
);
758 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
759 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
761 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
762 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
764 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
765 state
->g_sun_dir
[0] = sinf( a
);
766 state
->g_sun_dir
[1] = cosf( a
);
767 state
->g_sun_dir
[2] = 0.2f
;
768 v3_normalize( state
->g_sun_dir
);
770 world
->probabilities
[ k_probability_curve_constant
] = 1.0f
;
771 float dp
= state
->g_day_phase
;
773 world
->probabilities
[ k_probability_curve_wildlife_day
] =
774 (dp
*dp
*0.8f
+state
->g_sunset_phase
)*0.8f
;
775 world
->probabilities
[ k_probability_curve_wildlife_night
] =
776 1.0f
-powf(fabsf((state
->g_time_of_day
-0.5f
)*5.0f
),5.0f
);
778 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
779 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
780 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
783 static void render_world( world_instance
*world
, camera
*cam
,
786 render_sky( world
, cam
);
788 render_world_routes( world
, cam
, layer_depth
);
789 render_world_standard( world
, cam
);
790 render_world_cubemapped( world
, cam
, layer_depth
);
792 render_world_vb( world
, cam
);
793 render_world_alphatest( world
, cam
);
794 render_terrain( world
, cam
);
796 if( layer_depth
== -1 ) return;
797 if( layer_depth
== 0 ){
798 world_entity_focus_render();
802 float min_dist
= INFINITY
;
804 if( mdl_arrcount( &world
->ent_route
) ){
805 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
806 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
807 float dist
= v3_dist2( route
->board_transform
[3], cam
->pos
);
809 if( dist
< min_dist
){
815 ent_route
*route
= mdl_arritm( &world
->ent_route
, closest
);
816 sfd_render( world
, cam
, route
->board_transform
);
821 if( mdl_entity_id_type(world_static
.focused_entity
) == k_ent_challenge
)
822 greyout
= world_static
.focus_strength
;
824 if( greyout
> 0.0f
){
825 glDrawBuffers( 1, (GLenum
[]){ GL_COLOR_ATTACHMENT0
} );
827 glDisable(GL_DEPTH_TEST
);
828 glDepthMask(GL_FALSE
);
829 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
830 glBlendEquation(GL_FUNC_ADD
);
832 shader_blitcolour_use();
833 shader_blitcolour_uColour( (v4f
){ 0.5f
, 0.5f
, 0.5f
, greyout
*0.56f
} );
837 glEnable(GL_DEPTH_TEST
);
838 glDepthMask(GL_TRUE
);
839 glDrawBuffers( 2, (GLenum
[]){ GL_COLOR_ATTACHMENT0
,
840 GL_COLOR_ATTACHMENT1
} );
843 render_world_fxglow( world
, cam
, layer_depth
);
848 void render_world_override_pass( world_instance
*world
,
849 struct world_pass
*pass
){
850 for( int i
=0; i
<world
->surface_count
; i
++ ){
851 struct world_surface
*mat
= &world
->surfaces
[i
];
853 if( mat
->info
.flags
& k_material_flag_ghosts
) continue;
856 if( pass
->geo_type
== k_world_geo_type_solid
)
859 sm
= &mat
->sm_no_collide
;
861 if( !sm
->indice_count
)
865 m4x3_identity( mmdl
);
866 pass
->fn_set_mdl( mmdl
);
867 pass
->fn_set_uPvmPrev( pass
->cam
->mtx_prev
.pv
);
868 pass
->fn_bind_textures( world
, mat
);
869 mdl_draw_submesh( sm
);
873 static void render_world_override( world_instance
*world
){
874 struct world_pass pass
= {
875 .cam
= &skaterift
.cam
,
876 .fn_bind_textures
= bindpoint_override
,
877 .fn_set_mdl
= shader_scene_override_uMdl
,
878 .fn_set_uPvmPrev
= shader_scene_override_uPvmPrev
,
879 .shader
= k_shader_override
882 shader_scene_override_use();
883 respawn_chooser_shader_uniforms();
884 shader_scene_override_uTexGarbage(0);
885 shader_scene_override_uTexMain(1);
886 shader_scene_override_uPv( pass
.cam
->mtx
.pv
);
888 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_override
);
889 bind_terrain_noise();
890 shader_scene_override_uCamera( pass
.cam
->transform
[3] );
892 glDisable( GL_CULL_FACE
);
893 mesh_bind( &world
->mesh_geo
);
894 pass
.geo_type
= k_world_geo_type_solid
;
895 render_world_override_pass( world
, &pass
);
896 mesh_bind( &world
->mesh_no_collide
);
897 pass
.geo_type
= k_world_geo_type_nonsolid
;
898 render_world_override_pass( world
, &pass
);
899 glEnable( GL_CULL_FACE
);
902 static void render_cubemap_side( world_instance
*world
, ent_cubemap
*cm
,
905 glFramebufferTexture2D( GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
906 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ side
, cm
->texture_id
, 0 );
907 glClear( GL_DEPTH_BUFFER_BIT
);
910 { -1.0f
, 0.0f
, 0.0f
},
911 { 1.0f
, 0.0f
, 0.0f
},
912 { 0.0f
, -1.0f
, 0.0f
},
913 { 0.0f
, 1.0f
, 0.0f
},
914 { 0.0f
, 0.0f
, -1.0f
},
918 { 0.0f
, -1.0f
, 0.0f
},
919 { 0.0f
, -1.0f
, 0.0f
},
920 { 0.0f
, 0.0f
, 1.0f
},
921 { 0.0f
, 0.0f
, -1.0f
},
922 { 0.0f
, -1.0f
, 0.0f
},
923 { 0.0f
, -1.0f
, 0.0f
}
926 v3_zero( cam
.angles
);
927 v3_copy( cm
->co
, cam
.pos
);
929 v3_copy( forward
[side
], cam
.transform
[2] );
930 v3_copy( up
[side
], cam
.transform
[1] );
931 v3_cross( up
[side
], forward
[side
], cam
.transform
[0] );
932 v3_copy( cm
->co
, cam
.transform
[3] );
933 m4x3_invert_affine( cam
.transform
, cam
.transform_inverse
);
935 camera_update_view( &cam
);
940 m4x4_copy( cam
.mtx
.p
, cam
.mtx_prev
.p
);
941 m4x4_projection( cam
.mtx
.p
, cam
.fov
, 1.0f
, cam
.nearz
, cam
.farz
);
942 camera_finalize( &cam
);
943 camera_finalize( &cam
);
945 render_world( world
, &cam
, -1 );
948 static void render_world_cubemaps( world_instance
*world
){
949 if( world
->cubemap_cooldown
)
950 world
->cubemap_cooldown
--;
952 world
->cubemap_cooldown
= 60;
954 glViewport( 0, 0, WORLD_CUBEMAP_RES
, WORLD_CUBEMAP_RES
);
955 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_cubemap
); i
++ ){
956 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, i
);
957 glBindFramebuffer( GL_FRAMEBUFFER
, cm
->framebuffer_id
);
959 world
->cubemap_side
++;
960 if( world
->cubemap_side
>= 6 )
961 world
->cubemap_side
= 0;
963 render_cubemap_side( world
, cm
, world
->cubemap_side
);
968 static void render_world_depth( world_instance
*world
, camera
*cam
){
969 m4x3f identity_matrix
;
970 m4x3_identity( identity_matrix
);
972 shader_scene_depth_use();
973 shader_scene_depth_uCamera( cam
->transform
[3] );
974 shader_scene_depth_uPv( cam
->mtx
.pv
);
975 shader_scene_depth_uPvmPrev( cam
->mtx_prev
.pv
);
976 shader_scene_depth_uMdl( identity_matrix
);
977 world_link_lighting_ub( world
, _shader_scene_depth
.id
);
979 mesh_bind( &world
->mesh_geo
);
980 mesh_draw( &world
->mesh_geo
);
983 static void render_world_position( world_instance
*world
, camera
*cam
){
984 m4x3f identity_matrix
;
985 m4x3_identity( identity_matrix
);
987 shader_scene_position_use();
988 shader_scene_position_uCamera( cam
->transform
[3] );
989 shader_scene_position_uPv( cam
->mtx
.pv
);
990 shader_scene_position_uPvmPrev( cam
->mtx_prev
.pv
);
991 shader_scene_position_uMdl( identity_matrix
);
992 world_link_lighting_ub( world
, _shader_scene_position
.id
);
994 mesh_bind( &world
->mesh_geo
);
995 mesh_draw( &world
->mesh_geo
);