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
[] ){
16 world_instance
*world
= world_current_instance();
17 world
->time
= atof( argv
[0] );
20 vg_error( "Usage set_time <0-1.0>\n" );
25 static void async_world_render_init( void *payload
, u32 size
)
27 vg_info( "Allocate uniform buffers\n" );
28 for( int i
=0; i
<4; i
++ ){
29 world_instance
*world
= &world_static
.instances
[i
];
30 world
->ubo_bind_point
= i
;
32 glGenBuffers( 1, &world
->ubo_lighting
);
33 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
34 glBufferData( GL_UNIFORM_BUFFER
, sizeof(struct ub_world_lighting
),
35 NULL
, GL_DYNAMIC_DRAW
);
37 glBindBufferBase( GL_UNIFORM_BUFFER
, i
, world
->ubo_lighting
);
41 vg_info( "Allocate frame buffers\n" );
42 for( int i
=0; i
<4; i
++ ){
43 world_instance
*world
= &world_static
.instances
[i
];
44 struct framebuffer
*fb
= &world
->heightmap
;
46 fb
->display_name
= NULL
;
50 fb
->resolution_div
= 0;
52 fb
->attachments
[0].display_name
= NULL
;
53 fb
->attachments
[0].purpose
= k_framebuffer_attachment_type_texture
;
54 fb
->attachments
[0].internalformat
= GL_RG16F
;
55 fb
->attachments
[0].format
= GL_RG
;
56 fb
->attachments
[0].type
= GL_FLOAT
;
57 fb
->attachments
[0].attachment
= GL_COLOR_ATTACHMENT0
;
59 fb
->attachments
[1].purpose
= k_framebuffer_attachment_type_none
;
60 fb
->attachments
[2].purpose
= k_framebuffer_attachment_type_none
;
61 fb
->attachments
[3].purpose
= k_framebuffer_attachment_type_none
;
62 fb
->attachments
[4].purpose
= k_framebuffer_attachment_type_none
;
64 render_fb_allocate( fb
);
68 static void world_render_init(void)
70 VG_VAR_F32( k_day_length
);
71 VG_VAR_I32( k_debug_light_indices
);
72 VG_VAR_I32( k_debug_light_complexity
);
73 VG_VAR_I32( k_light_preview
);
74 vg_console_reg_cmd( "set_time", ccmd_set_time
, NULL
);
76 world_render
.sky_rate
= 1.0;
77 world_render
.sky_target_rate
= 1.0;
79 shader_scene_standard_register();
80 shader_scene_standard_alphatest_register();
81 shader_scene_override_register();
82 shader_scene_cubemapped_register();
83 shader_scene_fxglow_register();
84 shader_scene_vertex_blend_register();
85 shader_scene_terrain_register();
86 shader_scene_depth_register();
87 shader_scene_position_register();
88 shader_model_sky_register();
90 vg_info( "Loading world resources\n" );
91 vg_linear_clear( vg_mem
.scratch
);
94 mdl_open( &msky
, "models/rs_skydome.mdl", vg_mem
.scratch
);
95 mdl_load_metadata_block( &msky
, vg_mem
.scratch
);
96 mdl_async_load_glmesh( &msky
, &world_render
.skydome
);
99 vg_info( "Loading default world textures\n" );
100 vg_tex2d_load_qoi_async_file( "textures/garbage.qoi",
101 VG_TEX2D_NEAREST
|VG_TEX2D_REPEAT
,
102 &world_render
.tex_terrain_noise
);
104 vg_async_call( async_world_render_init
, NULL
, 0 );
107 static void world_link_lighting_ub( world_instance
*world
, GLuint shader
)
109 GLuint idx
= glGetUniformBlockIndex( shader
, "ub_world_lighting" );
110 glUniformBlockBinding( shader
, idx
, world
->ubo_bind_point
);
113 static void world_bind_position_texture( world_instance
*world
,
114 GLuint shader
, GLuint location
,
117 render_fb_bind_texture( &world
->heightmap
, 0, slot
);
118 glUniform1i( location
, slot
);
121 static void world_bind_light_array( world_instance
*world
,
122 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
,
134 glActiveTexture( GL_TEXTURE0
+ slot
);
135 glBindTexture( GL_TEXTURE_3D
, world
->tex_light_cubes
);
136 glUniform1i( location
, slot
);
139 static void render_world_depth( world_instance
*world
, camera
*cam
);
145 static void bind_terrain_noise(void){
146 glActiveTexture( GL_TEXTURE0
);
147 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
152 enum mdl_shader shader
;
153 enum world_geo_type geo_type
;
155 void (*fn_bind_textures
)( world_instance
*world
,
156 struct world_surface
*mat
);
157 void (*fn_set_mdl
)( m4x3f mdl
);
158 void (*fn_set_uPvmPrev
)( m4x4f pvm
);
162 void world_render_traffic( world_instance
*world
, u32 material_id
,
163 struct world_pass
*pass
){
164 if( !mdl_arrcount( &world
->ent_traffic
) ) return;
166 /* HACK: use the first material for every traffic entity */
167 ent_traffic
*first
= mdl_arritm( &world
->ent_traffic
, 0 );
168 if( !first
->submesh_count
) return;
170 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
, first
->submesh_start
);
171 if( sm
->material_id
!= material_id
) return;
173 struct world_surface
*mat
= &world
->surfaces
[ material_id
];
174 pass
->fn_bind_textures( world
, mat
);
176 for( u32 j
=0; j
<mdl_arrcount( &world
->ent_traffic
); j
++ ){
177 ent_traffic
*traffic
= mdl_arritm( &world
->ent_traffic
, j
);
179 for( u32 k
=0; k
<traffic
->submesh_count
; k
++ ){
180 sm
= mdl_arritm( &world
->meta
.submeshs
,
181 traffic
->submesh_start
+k
);
184 q_m3x3( traffic
->transform
.q
, mmdl
);
185 v3_copy( traffic
->transform
.co
, mmdl
[3] );
188 m4x3_expand( mmdl
, m4mdl
);
189 m4x4_mul( pass
->cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
191 pass
->fn_set_mdl( mmdl
);
192 pass
->fn_set_uPvmPrev( m4mdl
);
194 mdl_draw_submesh( sm
);
200 void world_render_pass( world_instance
*world
, struct world_pass
*pass
){
201 for( int i
=0; i
<world
->surface_count
; i
++ ){
202 struct world_surface
*mat
= &world
->surfaces
[i
];
204 if( mat
->info
.shader
== pass
->shader
){
207 if( pass
->geo_type
== k_world_geo_type_solid
){
211 world_render_traffic( world
, i
, pass
);
212 sm
= &mat
->sm_no_collide
;
215 if( !sm
->indice_count
)
219 m4x3_identity( mmdl
);
220 pass
->fn_set_mdl( mmdl
);
221 pass
->fn_set_uPvmPrev( pass
->cam
->mtx_prev
.pv
);
223 pass
->fn_bind_textures( world
, mat
);
224 mdl_draw_submesh( sm
);
230 void world_render_both_stages( world_instance
*world
, struct world_pass
*pass
)
232 mesh_bind( &world
->mesh_geo
);
233 pass
->geo_type
= k_world_geo_type_solid
;
234 world_render_pass( world
, pass
);
236 glDisable( GL_CULL_FACE
);
237 mesh_bind( &world
->mesh_no_collide
);
238 pass
->geo_type
= k_world_geo_type_nonsolid
;
239 world_render_pass( world
, pass
);
240 glEnable( GL_CULL_FACE
);
243 static void bindpoint_diffuse_texture1( world_instance
*world
,
244 struct world_surface
*mat
)
247 glActiveTexture( GL_TEXTURE1
);
248 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
251 static void bindpoint_diffuse1_and_cubemap10( world_instance
*world
,
252 struct world_surface
*mat
){
253 glActiveTexture( GL_TEXTURE1
);
254 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
256 u32 cubemap_id
= mat
->info
.tex_none0
,
259 if( mdl_entity_id_type( cubemap_id
) == k_ent_cubemap
){
260 cubemap_index
= mdl_entity_id_id( cubemap_id
);
263 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, cubemap_index
);
264 glActiveTexture( GL_TEXTURE10
);
265 glBindTexture( GL_TEXTURE_CUBE_MAP
, cm
->texture_id
);
267 shader_scene_cubemapped_uColour( mat
->info
.colour
);
270 static void render_world_vb( world_instance
*world
, camera
*cam
)
272 shader_scene_vertex_blend_use();
273 shader_scene_vertex_blend_uTexGarbage(0);
274 shader_scene_vertex_blend_uTexGradients(1);
275 world_link_lighting_ub( world
, _shader_scene_vertex_blend
.id
);
276 world_bind_position_texture( world
, _shader_scene_vertex_blend
.id
,
277 _uniform_scene_vertex_blend_g_world_depth
, 2 );
278 world_bind_light_array( world
, _shader_scene_vertex_blend
.id
,
279 _uniform_scene_vertex_blend_uLightsArray
, 3 );
280 world_bind_light_index( world
, _shader_scene_vertex_blend
.id
,
281 _uniform_scene_vertex_blend_uLightsIndex
, 4 );
283 glActiveTexture( GL_TEXTURE0
);
284 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
286 shader_scene_vertex_blend_uPv( cam
->mtx
.pv
);
287 shader_scene_vertex_blend_uCamera( cam
->transform
[3] );
289 struct world_pass pass
= {
290 .shader
= k_shader_standard_vertex_blend
,
292 .fn_bind_textures
= bindpoint_diffuse_texture1
,
293 .fn_set_mdl
= shader_scene_vertex_blend_uMdl
,
294 .fn_set_uPvmPrev
= shader_scene_vertex_blend_uPvmPrev
,
297 world_render_both_stages( world
, &pass
);
300 static void world_shader_standard_bind( world_instance
*world
, camera
*cam
){
301 shader_scene_standard_use();
302 shader_scene_standard_uTexGarbage(0);
303 shader_scene_standard_uTexMain(1);
304 shader_scene_standard_uPv( cam
->mtx
.pv
);
306 world_link_lighting_ub( world
, _shader_scene_standard
.id
);
307 world_bind_position_texture( world
, _shader_scene_standard
.id
,
308 _uniform_scene_standard_g_world_depth
, 2 );
309 world_bind_light_array( world
, _shader_scene_standard
.id
,
310 _uniform_scene_standard_uLightsArray
, 3 );
311 world_bind_light_index( world
, _shader_scene_standard
.id
,
312 _uniform_scene_standard_uLightsIndex
, 4 );
314 bind_terrain_noise();
315 shader_scene_standard_uCamera( cam
->transform
[3] );
318 static void render_world_standard( world_instance
*world
, camera
*cam
){
319 world_shader_standard_bind( world
, cam
);
320 struct world_pass pass
= {
321 .shader
= k_shader_standard
,
323 .fn_bind_textures
= bindpoint_diffuse_texture1
,
324 .fn_set_mdl
= shader_scene_standard_uMdl
,
325 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
328 world_render_both_stages( world
, &pass
);
331 static void render_world_cubemapped( world_instance
*world
, camera
*cam
,
333 if( !mdl_arrcount( &world
->ent_cubemap
) )
336 if( layer_depth
== -1 ){
337 world_shader_standard_bind( world
, cam
);
339 struct world_pass pass
= {
340 .shader
= k_shader_cubemap
,
342 .fn_bind_textures
= bindpoint_diffuse_texture1
,
343 .fn_set_mdl
= shader_scene_standard_uMdl
,
344 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
347 world_render_both_stages( world
, &pass
);
350 shader_scene_cubemapped_use();
351 shader_scene_cubemapped_uTexGarbage(0);
352 shader_scene_cubemapped_uTexMain(1);
353 shader_scene_cubemapped_uTexCubemap(10);
354 shader_scene_cubemapped_uPv( cam
->mtx
.pv
);
356 world_link_lighting_ub( world
, _shader_scene_cubemapped
.id
);
357 world_bind_position_texture( world
, _shader_scene_cubemapped
.id
,
358 _uniform_scene_cubemapped_g_world_depth
, 2 );
359 world_bind_light_array( world
, _shader_scene_cubemapped
.id
,
360 _uniform_scene_cubemapped_uLightsArray
, 3 );
361 world_bind_light_index( world
, _shader_scene_cubemapped
.id
,
362 _uniform_scene_cubemapped_uLightsIndex
, 4 );
364 bind_terrain_noise();
365 shader_scene_cubemapped_uCamera( cam
->transform
[3] );
367 struct world_pass pass
= {
368 .shader
= k_shader_cubemap
,
370 .fn_bind_textures
= bindpoint_diffuse1_and_cubemap10
,
371 .fn_set_mdl
= shader_scene_cubemapped_uMdl
,
372 .fn_set_uPvmPrev
= shader_scene_cubemapped_uPvmPrev
,
375 world_render_both_stages( world
, &pass
);
379 static void render_world_alphatest( world_instance
*world
, camera
*cam
){
380 shader_scene_standard_alphatest_use();
381 shader_scene_standard_alphatest_uTexGarbage(0);
382 shader_scene_standard_alphatest_uTexMain(1);
383 shader_scene_standard_alphatest_uPv( cam
->mtx
.pv
);
385 world_link_lighting_ub( world
, _shader_scene_standard_alphatest
.id
);
386 world_bind_position_texture( world
, _shader_scene_standard_alphatest
.id
,
387 _uniform_scene_standard_alphatest_g_world_depth
, 2 );
388 world_bind_light_array( world
, _shader_scene_standard_alphatest
.id
,
389 _uniform_scene_standard_alphatest_uLightsArray
, 3 );
390 world_bind_light_index( world
, _shader_scene_standard_alphatest
.id
,
391 _uniform_scene_standard_alphatest_uLightsIndex
, 4 );
394 bind_terrain_noise();
397 shader_scene_standard_alphatest_uCamera( cam
->transform
[3] );
399 glDisable(GL_CULL_FACE
);
401 struct world_pass pass
= {
402 .shader
= k_shader_standard_cutout
,
404 .fn_bind_textures
= bindpoint_diffuse_texture1
,
405 .fn_set_mdl
= shader_scene_standard_alphatest_uMdl
,
406 .fn_set_uPvmPrev
= shader_scene_standard_alphatest_uPvmPrev
,
409 world_render_both_stages( world
, &pass
);
411 glEnable(GL_CULL_FACE
);
415 void world_render_challenges( world_instance
*world
, struct world_pass
*pass
,
416 v3f pos
, int layer_depth
){
418 if( skaterift
.activity
== k_skaterift_replay
) return;
419 if( world
!= world_current_instance() ) return;
424 u32 objective_list
[ 32 ],
425 challenge_list
[ 16 ];
427 v2f objective_uv_offsets
[ 32 ];
429 u32 objective_count
= 0,
432 ent_challenge
*active_challenge
= NULL
;
434 if( mdl_entity_id_type( world_static
.focused_entity
) == k_ent_challenge
){
435 if( (skaterift
.activity
== k_skaterift_default
) &&
436 world_static
.challenge_target
){
440 if( !((skaterift
.activity
!= k_skaterift_ent_focus
) &&
441 !world_static
.challenge_target
) ){
442 world_instance
*challenge_world
= world_current_instance();
443 u32 index
= mdl_entity_id_id( world_static
.focused_entity
);
444 active_challenge
= mdl_arritm(&challenge_world
->ent_challenge
, index
);
448 if( active_challenge
){
449 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
450 challenge_list
[ challenge_count
++ ] = world_static
.focused_entity
;
452 u32 next
= active_challenge
->first
;
453 while( mdl_entity_id_type(next
) == k_ent_objective
){
454 u32 index
= mdl_entity_id_id( next
);
455 objective_list
[ objective_count
++ ] = index
;
457 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
458 next
= objective
->id_next
;
464 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
466 bh_iter_init_range( 0, &it
, pos
, radius
+10.0f
);
468 while( bh_next( world
->entity_bh
, &it
, &idx
) ){
469 u32 id
= world
->entity_list
[ idx
],
470 type
= mdl_entity_id_type( id
),
471 index
= mdl_entity_id_id( id
);
473 if( type
== k_ent_objective
) {
474 if( objective_count
< vg_list_size(objective_list
) )
475 objective_list
[ objective_count
++ ] = index
;
477 else if( type
== k_ent_challenge
){
478 if( challenge_count
< vg_list_size(challenge_list
) )
479 challenge_list
[ challenge_count
++ ] = index
;
484 /* render objectives */
485 glDisable( GL_CULL_FACE
);
486 mesh_bind( &world
->mesh_no_collide
);
487 u32 last_material
= 0;
488 for( u32 i
=0; i
<objective_count
; i
++ ){
489 u32 index
= objective_list
[ i
];
490 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
491 if( (objective
->flags
& k_ent_objective_hidden
) &&
492 !active_challenge
) continue;
497 u32 passed
= objective
->flags
& k_ent_objective_passed
;
498 f32 target
= passed
? 0.0f
: 1.0f
;
499 vg_slewf(&objective
->transform
.s
[0], target
, vg
.time_frame_delta
*4.0f
);
500 scale
= vg_smoothstepf( objective
->transform
.s
[0] );
502 if( (objective
== world_static
.challenge_target
) || passed
)
503 shader_scene_fxglow_uUvOffset( (v2f
){ 16.0f
/256.0f
, 0.0f
} );
505 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
508 f32 dist
= v3_dist( objective
->transform
.co
, pos
) * (1.0f
/radius
);
509 scale
= vg_smoothstepf( vg_clampf( 5.0f
-dist
*5.0f
, 0.0f
,1.0f
) );
513 q_m3x3( objective
->transform
.q
, mmdl
);
514 m3x3_scalef( mmdl
, scale
);
515 v3_copy( objective
->transform
.co
, mmdl
[3] );
516 shader_scene_fxglow_uMdl( mmdl
);
518 for( u32 j
=0; j
<objective
->submesh_count
; j
++ ){
519 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
520 objective
->submesh_start
+ j
);
522 if( sm
->material_id
!= last_material
){
523 last_material
= sm
->material_id
;
524 pass
->fn_bind_textures( world
, &world
->surfaces
[sm
->material_id
] );
526 mdl_draw_submesh( sm
);
531 font3d_bind( &gui
.font
, k_font_shader_world
, 0, world
, &skaterift
.cam
);
536 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_challenge
); i
++ ){
537 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, i
);
538 if( challenge
->status
) count
++;
542 c
+=highscore_intl( buf
+c
, count
, 3 );
544 c
+=highscore_intl( buf
+c
, mdl_arrcount(&world
->ent_challenge
), 3 );
547 f32 w
= font3d_string_width( 1, buf
);
549 m3x3_identity( mlocal
);
550 mlocal
[3][0] = -w
*0.5f
;
554 for( u32 i
=0; i
<challenge_count
; i
++ ){
555 u32 index
= challenge_list
[ i
];
556 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, index
);
558 mdl_transform_m4x3( &challenge
->transform
, mmdl
);
559 m4x3_mul( mmdl
, mlocal
, mmdl
);
561 vg_line_point( challenge
->transform
.co
, 0.25f
, VG__RED
);
563 f32 dist
= v3_dist( challenge
->transform
.co
, pos
) * (1.0f
/radius
),
564 scale
= vg_smoothstepf( vg_clampf( 10.0f
-dist
*10.0f
, 0.0f
,1.0f
) ),
567 if( challenge
->status
)
570 shader_scene_font_uOpacity( scale
);
571 shader_scene_font_uColourize( colour
);
572 font3d_simple_draw( 1, buf
, &skaterift
.cam
, mmdl
);
576 static void render_world_fxglow( world_instance
*world
, camera
*cam
,
578 shader_scene_fxglow_use();
579 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
580 shader_scene_fxglow_uTexMain(1);
581 shader_scene_fxglow_uPv( cam
->mtx
.pv
);
583 world_link_lighting_ub( world
, _shader_scene_fxglow
.id
);
584 world_bind_position_texture( world
, _shader_scene_fxglow
.id
,
585 _uniform_scene_fxglow_g_world_depth
, 2 );
586 world_bind_light_array( world
, _shader_scene_fxglow
.id
,
587 _uniform_scene_fxglow_uLightsArray
, 3 );
588 world_bind_light_index( world
, _shader_scene_fxglow
.id
,
589 _uniform_scene_fxglow_uLightsIndex
, 4 );
591 shader_scene_fxglow_uCamera( cam
->transform
[3] );
592 glDisable(GL_CULL_FACE
);
594 struct world_pass pass
= {
595 .shader
= k_shader_fxglow
,
597 .fn_bind_textures
= bindpoint_diffuse_texture1
,
598 .fn_set_mdl
= shader_scene_fxglow_uMdl
,
599 .fn_set_uPvmPrev
= shader_scene_fxglow_uPvmPrev
,
602 world_render_both_stages( world
, &pass
);
603 world_render_challenges( world
, &pass
, cam
->pos
, layer_depth
);
605 glEnable(GL_CULL_FACE
);
608 static void bindpoint_terrain( world_instance
*world
,
609 struct world_surface
*mat
)
611 glActiveTexture( GL_TEXTURE1
);
612 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
614 shader_scene_terrain_uSandColour( mat
->info
.colour
);
615 shader_scene_terrain_uBlendOffset( mat
->info
.colour1
);
618 static void bindpoint_override( world_instance
*world
,
619 struct world_surface
*mat
){
620 if( mat
->info
.flags
& k_material_flag_collision
){
621 shader_scene_override_uAlphatest(0);
624 glActiveTexture( GL_TEXTURE1
);
625 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
626 shader_scene_override_uAlphatest(1);
630 static void render_terrain( world_instance
*world
, camera
*cam
)
632 shader_scene_terrain_use();
633 shader_scene_terrain_uTexGarbage(0);
634 shader_scene_terrain_uTexGradients(1);
636 world_link_lighting_ub( world
, _shader_scene_terrain
.id
);
637 world_bind_position_texture( world
, _shader_scene_terrain
.id
,
638 _uniform_scene_terrain_g_world_depth
, 2 );
639 world_bind_light_array( world
, _shader_scene_terrain
.id
,
640 _uniform_scene_terrain_uLightsArray
, 3 );
641 world_bind_light_index( world
, _shader_scene_terrain
.id
,
642 _uniform_scene_terrain_uLightsIndex
, 4 );
644 glActiveTexture( GL_TEXTURE0
);
645 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
647 shader_scene_terrain_uPv( cam
->mtx
.pv
);
648 shader_scene_terrain_uCamera( cam
->transform
[3] );
650 struct world_pass pass
= {
651 .shader
= k_shader_terrain_blend
,
653 .fn_bind_textures
= bindpoint_terrain
,
654 .fn_set_mdl
= shader_scene_terrain_uMdl
,
655 .fn_set_uPvmPrev
= shader_scene_terrain_uPvmPrev
,
658 world_render_both_stages( world
, &pass
);
661 static void render_sky( world_instance
*world
, camera
*cam
)
664 * Modify matrix to remove clipping and view translation
671 m4x4_copy( cam
->mtx
.v
, v
);
672 m4x4_copy( cam
->mtx_prev
.v
, v_prev
);
674 v3_zero( v_prev
[3] );
676 m4x4_copy( cam
->mtx
.p
, pv
);
677 m4x4_copy( cam
->mtx_prev
.p
, pv_prev
);
678 m4x4_reset_clipping( pv
, cam
->farz
, cam
->nearz
);
679 m4x4_reset_clipping( pv_prev
, cam
->farz
, cam
->nearz
);
681 m4x4_mul( pv
, v
, pv
);
682 m4x4_mul( pv_prev
, v_prev
, pv_prev
);
684 m4x3f identity_matrix
;
685 m4x3_identity( identity_matrix
);
690 shader_model_sky_use();
691 shader_model_sky_uMdl( identity_matrix
);
692 shader_model_sky_uPv( pv
);
693 shader_model_sky_uPvmPrev( pv_prev
);
694 shader_model_sky_uTexGarbage(0);
695 world_link_lighting_ub( world
, _shader_model_sky
.id
);
697 glActiveTexture( GL_TEXTURE0
);
698 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
700 glDepthMask( GL_FALSE
);
701 glDisable( GL_DEPTH_TEST
);
703 mesh_bind( &world_render
.skydome
);
704 mesh_draw( &world_render
.skydome
);
706 glEnable( GL_DEPTH_TEST
);
707 glDepthMask( GL_TRUE
);
710 static void render_world_gates( world_instance
*world
, camera
*cam
,
713 float closest
= INFINITY
;
714 struct ent_gate
*gate
= NULL
;
716 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
717 ent_gate
*gi
= mdl_arritm( &world
->ent_gate
, i
);
719 if( !(gi
->flags
& k_ent_gate_linked
) )
722 float dist
= v3_dist2( gi
->co
[0], cam
->transform
[3] );
724 vg_line_point( gi
->co
[0], 0.25f
, VG__BLUE
);
726 if( dist
< closest
){
732 world
->rendering_gate
= gate
;
734 if( gate
->flags
& k_ent_gate_locked
) return;
736 if( gate
->flags
& k_ent_gate_nonlocal
){
737 if( world_static
.load_state
!= k_world_loader_none
){
738 world
->rendering_gate
= NULL
;
742 world_instance
*dest_world
= &world_static
.instances
[ gate
->target
];
743 render_gate( world
, dest_world
, gate
, cam
, layer_depth
);
746 render_gate( world
, world
, gate
, cam
, layer_depth
);
751 static void world_prerender( world_instance
*world
)
754 if( mdl_arrcount( &world
->ent_light
) ){
755 f32 rate
= vg_maxf(0.1f
, fabsf(k_day_length
)) * vg_signf(k_day_length
);
756 world
->time
+= vg
.time_delta
* (1.0/(rate
*60.0));
762 struct ub_world_lighting
*state
= &world
->ub_lighting
;
764 state
->g_time
= world
->time
;
765 state
->g_realtime
= vg
.time_real
;
766 state
->g_debug_indices
= k_debug_light_indices
;
767 state
->g_light_preview
= k_light_preview
;
768 state
->g_debug_complexity
= k_debug_light_complexity
;
770 if( skaterift
.activity
== k_skaterift_respawning
)
771 state
->g_time_of_day
= 0.1f
;
773 state
->g_time_of_day
= vg_fractf( world
->time
);
775 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
776 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
778 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
779 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
781 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
782 state
->g_sun_dir
[0] = sinf( a
);
783 state
->g_sun_dir
[1] = cosf( a
);
784 state
->g_sun_dir
[2] = 0.2f
;
785 v3_normalize( state
->g_sun_dir
);
787 world
->probabilities
[ k_probability_curve_constant
] = 1.0f
;
788 float dp
= state
->g_day_phase
;
790 world
->probabilities
[ k_probability_curve_wildlife_day
] =
791 (dp
*dp
*0.8f
+state
->g_sunset_phase
)*0.8f
;
792 world
->probabilities
[ k_probability_curve_wildlife_night
] =
793 1.0f
-powf(fabsf((state
->g_time_of_day
-0.5f
)*5.0f
),5.0f
);
795 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
796 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
797 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
800 static void render_world( world_instance
*world
, camera
*cam
,
803 render_sky( world
, cam
);
805 render_world_routes( world
, cam
, layer_depth
);
806 render_world_standard( world
, cam
);
807 render_world_cubemapped( world
, cam
, layer_depth
);
809 render_world_vb( world
, cam
);
810 render_world_alphatest( world
, cam
);
811 render_terrain( world
, cam
);
813 if( layer_depth
== -1 ) return;
814 if( layer_depth
== 0 ){
815 world_entity_focus_render();
819 float min_dist
= INFINITY
;
821 if( mdl_arrcount( &world
->ent_route
) ){
822 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
823 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
824 float dist
= v3_dist2( route
->board_transform
[3], cam
->pos
);
826 if( dist
< min_dist
){
832 ent_route
*route
= mdl_arritm( &world
->ent_route
, closest
);
833 sfd_render( world
, cam
, route
->board_transform
);
838 if( mdl_entity_id_type(world_static
.focused_entity
) == k_ent_challenge
)
839 greyout
= world_static
.focus_strength
;
841 if( greyout
> 0.0f
){
842 glDrawBuffers( 1, (GLenum
[]){ GL_COLOR_ATTACHMENT0
} );
844 glDisable(GL_DEPTH_TEST
);
845 glDepthMask(GL_FALSE
);
846 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
847 glBlendEquation(GL_FUNC_ADD
);
849 shader_blitcolour_use();
850 shader_blitcolour_uColour( (v4f
){ 0.5f
, 0.5f
, 0.5f
, greyout
*0.56f
} );
854 glEnable(GL_DEPTH_TEST
);
855 glDepthMask(GL_TRUE
);
856 glDrawBuffers( 2, (GLenum
[]){ GL_COLOR_ATTACHMENT0
,
857 GL_COLOR_ATTACHMENT1
} );
860 render_world_fxglow( world
, cam
, layer_depth
);
865 void render_world_override_pass( world_instance
*world
,
866 struct world_pass
*pass
){
867 for( int i
=0; i
<world
->surface_count
; i
++ ){
868 struct world_surface
*mat
= &world
->surfaces
[i
];
870 if( mat
->info
.flags
& k_material_flag_ghosts
) continue;
873 if( pass
->geo_type
== k_world_geo_type_solid
)
876 sm
= &mat
->sm_no_collide
;
878 if( !sm
->indice_count
)
882 m4x3_identity( mmdl
);
883 pass
->fn_set_mdl( mmdl
);
884 pass
->fn_set_uPvmPrev( pass
->cam
->mtx_prev
.pv
);
885 pass
->fn_bind_textures( world
, mat
);
886 mdl_draw_submesh( sm
);
890 static void render_world_override( world_instance
*world
){
891 struct world_pass pass
= {
892 .cam
= &skaterift
.cam
,
893 .fn_bind_textures
= bindpoint_override
,
894 .fn_set_mdl
= shader_scene_override_uMdl
,
895 .fn_set_uPvmPrev
= shader_scene_override_uPvmPrev
,
896 .shader
= k_shader_override
899 shader_scene_override_use();
900 respawn_chooser_shader_uniforms();
901 shader_scene_override_uTexGarbage(0);
902 shader_scene_override_uTexMain(1);
903 shader_scene_override_uPv( pass
.cam
->mtx
.pv
);
905 world_link_lighting_ub( world
, _shader_scene_override
.id
);
906 world_bind_position_texture( world
, _shader_scene_override
.id
,
907 _uniform_scene_override_g_world_depth
, 2 );
908 world_bind_light_array( world
, _shader_scene_override
.id
,
909 _uniform_scene_override_uLightsArray
, 3 );
910 world_bind_light_index( world
, _shader_scene_override
.id
,
911 _uniform_scene_override_uLightsIndex
, 4 );
913 bind_terrain_noise();
914 shader_scene_override_uCamera( pass
.cam
->transform
[3] );
916 glDisable( GL_CULL_FACE
);
917 mesh_bind( &world
->mesh_geo
);
918 pass
.geo_type
= k_world_geo_type_solid
;
919 render_world_override_pass( world
, &pass
);
920 mesh_bind( &world
->mesh_no_collide
);
921 pass
.geo_type
= k_world_geo_type_nonsolid
;
922 render_world_override_pass( world
, &pass
);
923 glEnable( GL_CULL_FACE
);
926 static void render_cubemap_side( world_instance
*world
, ent_cubemap
*cm
,
929 glFramebufferTexture2D( GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
930 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ side
, cm
->texture_id
, 0 );
931 glClear( GL_DEPTH_BUFFER_BIT
);
934 { -1.0f
, 0.0f
, 0.0f
},
935 { 1.0f
, 0.0f
, 0.0f
},
936 { 0.0f
, -1.0f
, 0.0f
},
937 { 0.0f
, 1.0f
, 0.0f
},
938 { 0.0f
, 0.0f
, -1.0f
},
942 { 0.0f
, -1.0f
, 0.0f
},
943 { 0.0f
, -1.0f
, 0.0f
},
944 { 0.0f
, 0.0f
, 1.0f
},
945 { 0.0f
, 0.0f
, -1.0f
},
946 { 0.0f
, -1.0f
, 0.0f
},
947 { 0.0f
, -1.0f
, 0.0f
}
950 v3_zero( cam
.angles
);
951 v3_copy( cm
->co
, cam
.pos
);
953 v3_copy( forward
[side
], cam
.transform
[2] );
954 v3_copy( up
[side
], cam
.transform
[1] );
955 v3_cross( up
[side
], forward
[side
], cam
.transform
[0] );
956 v3_copy( cm
->co
, cam
.transform
[3] );
957 m4x3_invert_affine( cam
.transform
, cam
.transform_inverse
);
959 camera_update_view( &cam
);
964 m4x4_copy( cam
.mtx
.p
, cam
.mtx_prev
.p
);
965 m4x4_projection( cam
.mtx
.p
, cam
.fov
, 1.0f
, cam
.nearz
, cam
.farz
);
966 camera_finalize( &cam
);
967 camera_finalize( &cam
);
969 render_world( world
, &cam
, -1 );
972 static void render_world_cubemaps( world_instance
*world
){
973 if( world
->cubemap_cooldown
)
974 world
->cubemap_cooldown
--;
976 world
->cubemap_cooldown
= 60;
978 glViewport( 0, 0, WORLD_CUBEMAP_RES
, WORLD_CUBEMAP_RES
);
979 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_cubemap
); i
++ ){
980 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, i
);
981 glBindFramebuffer( GL_FRAMEBUFFER
, cm
->framebuffer_id
);
983 world
->cubemap_side
++;
984 if( world
->cubemap_side
>= 6 )
985 world
->cubemap_side
= 0;
987 render_cubemap_side( world
, cm
, world
->cubemap_side
);
992 static void render_world_depth( world_instance
*world
, camera
*cam
)
994 m4x3f identity_matrix
;
995 m4x3_identity( identity_matrix
);
997 shader_scene_depth_use();
998 shader_scene_depth_uCamera( cam
->transform
[3] );
999 shader_scene_depth_uPv( cam
->mtx
.pv
);
1000 shader_scene_depth_uPvmPrev( cam
->mtx_prev
.pv
);
1001 shader_scene_depth_uMdl( identity_matrix
);
1002 world_link_lighting_ub( world
, _shader_scene_depth
.id
);
1004 mesh_bind( &world
->mesh_geo
);
1005 mesh_draw( &world
->mesh_geo
);
1008 static void render_world_position( world_instance
*world
, camera
*cam
)
1010 m4x3f identity_matrix
;
1011 m4x3_identity( identity_matrix
);
1013 shader_scene_position_use();
1014 shader_scene_position_uCamera( cam
->transform
[3] );
1015 shader_scene_position_uPv( cam
->mtx
.pv
);
1016 shader_scene_position_uPvmPrev( cam
->mtx_prev
.pv
);
1017 shader_scene_position_uMdl( identity_matrix
);
1018 world_link_lighting_ub( world
, _shader_scene_position
.id
);
1020 mesh_bind( &world
->mesh_geo
);
1021 mesh_draw( &world
->mesh_geo
);