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)
147 glActiveTexture( GL_TEXTURE0
);
148 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
153 enum mdl_shader shader
;
154 enum world_geo_type geo_type
;
156 void (*fn_bind_textures
)( world_instance
*world
,
157 struct world_surface
*mat
);
158 void (*fn_set_mdl
)( m4x3f mdl
);
159 void (*fn_set_uPvmPrev
)( m4x4f pvm
);
163 void world_render_traffic( world_instance
*world
, u32 material_id
,
164 struct world_pass
*pass
){
165 if( !mdl_arrcount( &world
->ent_traffic
) ) return;
167 /* HACK: use the first material for every traffic entity */
168 ent_traffic
*first
= mdl_arritm( &world
->ent_traffic
, 0 );
169 if( !first
->submesh_count
) return;
171 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
, first
->submesh_start
);
172 if( sm
->material_id
!= material_id
) return;
174 struct world_surface
*mat
= &world
->surfaces
[ material_id
];
175 pass
->fn_bind_textures( world
, mat
);
177 for( u32 j
=0; j
<mdl_arrcount( &world
->ent_traffic
); j
++ ){
178 ent_traffic
*traffic
= mdl_arritm( &world
->ent_traffic
, j
);
180 for( u32 k
=0; k
<traffic
->submesh_count
; k
++ ){
181 sm
= mdl_arritm( &world
->meta
.submeshs
,
182 traffic
->submesh_start
+k
);
185 q_m3x3( traffic
->transform
.q
, mmdl
);
186 v3_copy( traffic
->transform
.co
, mmdl
[3] );
189 m4x3_expand( mmdl
, m4mdl
);
190 m4x4_mul( pass
->cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
192 pass
->fn_set_mdl( mmdl
);
193 pass
->fn_set_uPvmPrev( m4mdl
);
195 mdl_draw_submesh( sm
);
201 void world_render_pass( world_instance
*world
, struct world_pass
*pass
){
202 for( int i
=0; i
<world
->surface_count
; i
++ ){
203 struct world_surface
*mat
= &world
->surfaces
[i
];
205 if( mat
->info
.shader
== pass
->shader
){
208 if( pass
->geo_type
== k_world_geo_type_solid
){
212 world_render_traffic( world
, i
, pass
);
213 sm
= &mat
->sm_no_collide
;
216 if( !sm
->indice_count
)
220 m4x3_identity( mmdl
);
221 pass
->fn_set_mdl( mmdl
);
222 pass
->fn_set_uPvmPrev( pass
->cam
->mtx_prev
.pv
);
224 pass
->fn_bind_textures( world
, mat
);
225 mdl_draw_submesh( sm
);
231 void world_render_both_stages( world_instance
*world
, struct world_pass
*pass
)
233 mesh_bind( &world
->mesh_geo
);
234 pass
->geo_type
= k_world_geo_type_solid
;
235 world_render_pass( world
, pass
);
237 glDisable( GL_CULL_FACE
);
238 mesh_bind( &world
->mesh_no_collide
);
239 pass
->geo_type
= k_world_geo_type_nonsolid
;
240 world_render_pass( world
, pass
);
241 glEnable( GL_CULL_FACE
);
244 static void bindpoint_diffuse_texture1( world_instance
*world
,
245 struct world_surface
*mat
)
248 glActiveTexture( GL_TEXTURE1
);
249 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
252 static void bindpoint_diffuse1_and_cubemap10( world_instance
*world
,
253 struct world_surface
*mat
){
254 glActiveTexture( GL_TEXTURE1
);
255 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
257 u32 cubemap_id
= mat
->info
.tex_none0
,
260 if( mdl_entity_id_type( cubemap_id
) == k_ent_cubemap
){
261 cubemap_index
= mdl_entity_id_id( cubemap_id
);
264 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, cubemap_index
);
265 glActiveTexture( GL_TEXTURE10
);
266 glBindTexture( GL_TEXTURE_CUBE_MAP
, cm
->texture_id
);
268 shader_scene_cubemapped_uColour( mat
->info
.colour
);
271 static void render_world_vb( world_instance
*world
, camera
*cam
)
273 shader_scene_vertex_blend_use();
274 shader_scene_vertex_blend_uTexGarbage(0);
275 shader_scene_vertex_blend_uTexGradients(1);
276 world_link_lighting_ub( world
, _shader_scene_vertex_blend
.id
);
277 world_bind_position_texture( world
, _shader_scene_vertex_blend
.id
,
278 _uniform_scene_vertex_blend_g_world_depth
, 2 );
279 world_bind_light_array( world
, _shader_scene_vertex_blend
.id
,
280 _uniform_scene_vertex_blend_uLightsArray
, 3 );
281 world_bind_light_index( world
, _shader_scene_vertex_blend
.id
,
282 _uniform_scene_vertex_blend_uLightsIndex
, 4 );
284 glActiveTexture( GL_TEXTURE0
);
285 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
287 shader_scene_vertex_blend_uPv( cam
->mtx
.pv
);
288 shader_scene_vertex_blend_uCamera( cam
->transform
[3] );
290 struct world_pass pass
= {
291 .shader
= k_shader_standard_vertex_blend
,
293 .fn_bind_textures
= bindpoint_diffuse_texture1
,
294 .fn_set_mdl
= shader_scene_vertex_blend_uMdl
,
295 .fn_set_uPvmPrev
= shader_scene_vertex_blend_uPvmPrev
,
298 world_render_both_stages( world
, &pass
);
301 static void world_shader_standard_bind( world_instance
*world
, camera
*cam
){
302 shader_scene_standard_use();
303 shader_scene_standard_uTexGarbage(0);
304 shader_scene_standard_uTexMain(1);
305 shader_scene_standard_uPv( cam
->mtx
.pv
);
307 world_link_lighting_ub( world
, _shader_scene_standard
.id
);
308 world_bind_position_texture( world
, _shader_scene_standard
.id
,
309 _uniform_scene_standard_g_world_depth
, 2 );
310 world_bind_light_array( world
, _shader_scene_standard
.id
,
311 _uniform_scene_standard_uLightsArray
, 3 );
312 world_bind_light_index( world
, _shader_scene_standard
.id
,
313 _uniform_scene_standard_uLightsIndex
, 4 );
315 bind_terrain_noise();
316 shader_scene_standard_uCamera( cam
->transform
[3] );
319 static void render_world_standard( world_instance
*world
, camera
*cam
){
320 world_shader_standard_bind( world
, cam
);
321 struct world_pass pass
= {
322 .shader
= k_shader_standard
,
324 .fn_bind_textures
= bindpoint_diffuse_texture1
,
325 .fn_set_mdl
= shader_scene_standard_uMdl
,
326 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
329 world_render_both_stages( world
, &pass
);
332 static void render_world_cubemapped( world_instance
*world
, camera
*cam
,
334 if( !mdl_arrcount( &world
->ent_cubemap
) )
337 if( layer_depth
== -1 ){
338 world_shader_standard_bind( world
, cam
);
340 struct world_pass pass
= {
341 .shader
= k_shader_cubemap
,
343 .fn_bind_textures
= bindpoint_diffuse_texture1
,
344 .fn_set_mdl
= shader_scene_standard_uMdl
,
345 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
348 world_render_both_stages( world
, &pass
);
351 shader_scene_cubemapped_use();
352 shader_scene_cubemapped_uTexGarbage(0);
353 shader_scene_cubemapped_uTexMain(1);
354 shader_scene_cubemapped_uTexCubemap(10);
355 shader_scene_cubemapped_uPv( cam
->mtx
.pv
);
357 world_link_lighting_ub( world
, _shader_scene_cubemapped
.id
);
358 world_bind_position_texture( world
, _shader_scene_cubemapped
.id
,
359 _uniform_scene_cubemapped_g_world_depth
, 2 );
360 world_bind_light_array( world
, _shader_scene_cubemapped
.id
,
361 _uniform_scene_cubemapped_uLightsArray
, 3 );
362 world_bind_light_index( world
, _shader_scene_cubemapped
.id
,
363 _uniform_scene_cubemapped_uLightsIndex
, 4 );
365 bind_terrain_noise();
366 shader_scene_cubemapped_uCamera( cam
->transform
[3] );
368 struct world_pass pass
= {
369 .shader
= k_shader_cubemap
,
371 .fn_bind_textures
= bindpoint_diffuse1_and_cubemap10
,
372 .fn_set_mdl
= shader_scene_cubemapped_uMdl
,
373 .fn_set_uPvmPrev
= shader_scene_cubemapped_uPvmPrev
,
376 world_render_both_stages( world
, &pass
);
380 static void render_world_alphatest( world_instance
*world
, camera
*cam
){
381 shader_scene_standard_alphatest_use();
382 shader_scene_standard_alphatest_uTexGarbage(0);
383 shader_scene_standard_alphatest_uTexMain(1);
384 shader_scene_standard_alphatest_uPv( cam
->mtx
.pv
);
386 world_link_lighting_ub( world
, _shader_scene_standard_alphatest
.id
);
387 world_bind_position_texture( world
, _shader_scene_standard_alphatest
.id
,
388 _uniform_scene_standard_alphatest_g_world_depth
, 2 );
389 world_bind_light_array( world
, _shader_scene_standard_alphatest
.id
,
390 _uniform_scene_standard_alphatest_uLightsArray
, 3 );
391 world_bind_light_index( world
, _shader_scene_standard_alphatest
.id
,
392 _uniform_scene_standard_alphatest_uLightsIndex
, 4 );
395 bind_terrain_noise();
398 shader_scene_standard_alphatest_uCamera( cam
->transform
[3] );
400 glDisable(GL_CULL_FACE
);
402 struct world_pass pass
= {
403 .shader
= k_shader_standard_cutout
,
405 .fn_bind_textures
= bindpoint_diffuse_texture1
,
406 .fn_set_mdl
= shader_scene_standard_alphatest_uMdl
,
407 .fn_set_uPvmPrev
= shader_scene_standard_alphatest_uPvmPrev
,
410 world_render_both_stages( world
, &pass
);
412 glEnable(GL_CULL_FACE
);
416 void world_render_challenges( world_instance
*world
, struct world_pass
*pass
,
417 v3f pos
, int layer_depth
){
419 if( skaterift
.activity
== k_skaterift_replay
) return;
420 if( world
!= world_current_instance() ) return;
425 u32 objective_list
[ 32 ],
426 challenge_list
[ 16 ];
428 v2f objective_uv_offsets
[ 32 ];
430 u32 objective_count
= 0,
433 ent_challenge
*active_challenge
= NULL
;
435 if( mdl_entity_id_type( world_static
.focused_entity
) == k_ent_challenge
){
436 if( (skaterift
.activity
== k_skaterift_default
) &&
437 world_static
.challenge_target
){
441 if( !((skaterift
.activity
!= k_skaterift_ent_focus
) &&
442 !world_static
.challenge_target
) ){
443 world_instance
*challenge_world
= world_current_instance();
444 u32 index
= mdl_entity_id_id( world_static
.focused_entity
);
445 active_challenge
= mdl_arritm(&challenge_world
->ent_challenge
, index
);
449 if( active_challenge
){
450 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
451 challenge_list
[ challenge_count
++ ] = world_static
.focused_entity
;
453 u32 next
= active_challenge
->first
;
454 while( mdl_entity_id_type(next
) == k_ent_objective
){
455 u32 index
= mdl_entity_id_id( next
);
456 objective_list
[ objective_count
++ ] = index
;
458 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
459 next
= objective
->id_next
;
465 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
467 bh_iter_init_range( 0, &it
, pos
, radius
+10.0f
);
469 while( bh_next( world
->entity_bh
, &it
, &idx
) ){
470 u32 id
= world
->entity_list
[ idx
],
471 type
= mdl_entity_id_type( id
),
472 index
= mdl_entity_id_id( id
);
474 if( type
== k_ent_objective
) {
475 if( objective_count
< vg_list_size(objective_list
) )
476 objective_list
[ objective_count
++ ] = index
;
478 else if( type
== k_ent_challenge
){
479 if( challenge_count
< vg_list_size(challenge_list
) )
480 challenge_list
[ challenge_count
++ ] = index
;
485 /* render objectives */
486 glDisable( GL_CULL_FACE
);
487 mesh_bind( &world
->mesh_no_collide
);
488 u32 last_material
= 0;
489 for( u32 i
=0; i
<objective_count
; i
++ ){
490 u32 index
= objective_list
[ i
];
491 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
492 if( (objective
->flags
& k_ent_objective_hidden
) &&
493 !active_challenge
) continue;
498 u32 passed
= objective
->flags
& k_ent_objective_passed
;
499 f32 target
= passed
? 0.0f
: 1.0f
;
500 vg_slewf(&objective
->transform
.s
[0], target
, vg
.time_frame_delta
*4.0f
);
501 scale
= vg_smoothstepf( objective
->transform
.s
[0] );
503 if( (objective
== world_static
.challenge_target
) || passed
)
504 shader_scene_fxglow_uUvOffset( (v2f
){ 16.0f
/256.0f
, 0.0f
} );
506 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
509 f32 dist
= v3_dist( objective
->transform
.co
, pos
) * (1.0f
/radius
);
510 scale
= vg_smoothstepf( vg_clampf( 5.0f
-dist
*5.0f
, 0.0f
,1.0f
) );
514 q_m3x3( objective
->transform
.q
, mmdl
);
515 m3x3_scalef( mmdl
, scale
);
516 v3_copy( objective
->transform
.co
, mmdl
[3] );
517 shader_scene_fxglow_uMdl( mmdl
);
519 for( u32 j
=0; j
<objective
->submesh_count
; j
++ ){
520 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
521 objective
->submesh_start
+ j
);
523 if( sm
->material_id
!= last_material
){
524 last_material
= sm
->material_id
;
525 pass
->fn_bind_textures( world
, &world
->surfaces
[sm
->material_id
] );
527 mdl_draw_submesh( sm
);
532 shader_scene_font_use();
533 shader_scene_font_uTexGarbage(0);
534 shader_scene_font_uTexMain(1);
536 shader_scene_font_uPv( skaterift
.cam
.mtx
.pv
);
537 shader_scene_font_uTime( vg
.time
);
539 /* TODO: Code dupe... */
540 world_link_lighting_ub( world
, _shader_scene_font
.id
);
541 world_bind_position_texture( world
, _shader_scene_font
.id
,
542 _uniform_scene_font_g_world_depth
, 2 );
543 world_bind_light_array( world
, _shader_scene_font
.id
,
544 _uniform_scene_font_uLightsArray
, 3 );
545 world_bind_light_index( world
, _shader_scene_font
.id
,
546 _uniform_scene_font_uLightsIndex
, 4 );
548 bind_terrain_noise();
549 shader_scene_font_uCamera( skaterift
.cam
.transform
[3] );
551 //shader_scene_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
552 glActiveTexture( GL_TEXTURE1
);
553 glBindTexture( GL_TEXTURE_2D
, gui
.font
.texture
);
555 mesh_bind( &gui
.font
.mesh
);
560 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_challenge
); i
++ ){
561 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, i
);
562 if( challenge
->status
) count
++;
566 c
+=highscore_intl( buf
+c
, count
, 3 );
568 c
+=highscore_intl( buf
+c
, mdl_arrcount(&world
->ent_challenge
), 3 );
571 f32 w
= font3d_string_width( &gui
.font
, 1, buf
);
573 m3x3_identity( mlocal
);
574 mlocal
[3][0] = -w
*0.5f
;
578 for( u32 i
=0; i
<challenge_count
; i
++ ){
579 u32 index
= challenge_list
[ i
];
580 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, index
);
582 mdl_transform_m4x3( &challenge
->transform
, mmdl
);
583 m4x3_mul( mmdl
, mlocal
, mmdl
);
585 vg_line_point( challenge
->transform
.co
, 0.25f
, VG__RED
);
587 f32 dist
= v3_dist( challenge
->transform
.co
, pos
) * (1.0f
/radius
),
588 scale
= vg_smoothstepf( vg_clampf( 10.0f
-dist
*10.0f
, 0.0f
,1.0f
) ),
591 if( challenge
->status
)
594 shader_scene_font_uOpacity( scale
);
595 shader_scene_font_uColourize( colour
);
597 struct font3d_render render
= {
600 .shader
= k_font_shader_world
603 font3d_begin( buf
, &skaterift
.cam
, mmdl
, &render
);
604 font3d_draw( &render
);
608 static void render_world_fxglow( world_instance
*world
, camera
*cam
,
610 shader_scene_fxglow_use();
611 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
612 shader_scene_fxglow_uTexMain(1);
613 shader_scene_fxglow_uPv( cam
->mtx
.pv
);
615 world_link_lighting_ub( world
, _shader_scene_fxglow
.id
);
616 world_bind_position_texture( world
, _shader_scene_fxglow
.id
,
617 _uniform_scene_fxglow_g_world_depth
, 2 );
618 world_bind_light_array( world
, _shader_scene_fxglow
.id
,
619 _uniform_scene_fxglow_uLightsArray
, 3 );
620 world_bind_light_index( world
, _shader_scene_fxglow
.id
,
621 _uniform_scene_fxglow_uLightsIndex
, 4 );
623 shader_scene_fxglow_uCamera( cam
->transform
[3] );
624 glDisable(GL_CULL_FACE
);
626 struct world_pass pass
= {
627 .shader
= k_shader_fxglow
,
629 .fn_bind_textures
= bindpoint_diffuse_texture1
,
630 .fn_set_mdl
= shader_scene_fxglow_uMdl
,
631 .fn_set_uPvmPrev
= shader_scene_fxglow_uPvmPrev
,
634 world_render_both_stages( world
, &pass
);
635 world_render_challenges( world
, &pass
, cam
->pos
, layer_depth
);
637 glEnable(GL_CULL_FACE
);
640 static void bindpoint_terrain( world_instance
*world
,
641 struct world_surface
*mat
)
643 glActiveTexture( GL_TEXTURE1
);
644 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
646 shader_scene_terrain_uSandColour( mat
->info
.colour
);
647 shader_scene_terrain_uBlendOffset( mat
->info
.colour1
);
650 static void bindpoint_override( world_instance
*world
,
651 struct world_surface
*mat
){
652 if( mat
->info
.flags
& k_material_flag_collision
){
653 shader_scene_override_uAlphatest(0);
656 glActiveTexture( GL_TEXTURE1
);
657 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
658 shader_scene_override_uAlphatest(1);
662 static void render_terrain( world_instance
*world
, camera
*cam
)
664 shader_scene_terrain_use();
665 shader_scene_terrain_uTexGarbage(0);
666 shader_scene_terrain_uTexGradients(1);
668 world_link_lighting_ub( world
, _shader_scene_terrain
.id
);
669 world_bind_position_texture( world
, _shader_scene_terrain
.id
,
670 _uniform_scene_terrain_g_world_depth
, 2 );
671 world_bind_light_array( world
, _shader_scene_terrain
.id
,
672 _uniform_scene_terrain_uLightsArray
, 3 );
673 world_bind_light_index( world
, _shader_scene_terrain
.id
,
674 _uniform_scene_terrain_uLightsIndex
, 4 );
676 glActiveTexture( GL_TEXTURE0
);
677 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
679 shader_scene_terrain_uPv( cam
->mtx
.pv
);
680 shader_scene_terrain_uCamera( cam
->transform
[3] );
682 struct world_pass pass
= {
683 .shader
= k_shader_terrain_blend
,
685 .fn_bind_textures
= bindpoint_terrain
,
686 .fn_set_mdl
= shader_scene_terrain_uMdl
,
687 .fn_set_uPvmPrev
= shader_scene_terrain_uPvmPrev
,
690 world_render_both_stages( world
, &pass
);
693 static void render_sky( world_instance
*world
, camera
*cam
)
696 * Modify matrix to remove clipping and view translation
703 m4x4_copy( cam
->mtx
.v
, v
);
704 m4x4_copy( cam
->mtx_prev
.v
, v_prev
);
706 v3_zero( v_prev
[3] );
708 m4x4_copy( cam
->mtx
.p
, pv
);
709 m4x4_copy( cam
->mtx_prev
.p
, pv_prev
);
710 m4x4_reset_clipping( pv
, cam
->farz
, cam
->nearz
);
711 m4x4_reset_clipping( pv_prev
, cam
->farz
, cam
->nearz
);
713 m4x4_mul( pv
, v
, pv
);
714 m4x4_mul( pv_prev
, v_prev
, pv_prev
);
716 m4x3f identity_matrix
;
717 m4x3_identity( identity_matrix
);
722 shader_model_sky_use();
723 shader_model_sky_uMdl( identity_matrix
);
724 shader_model_sky_uPv( pv
);
725 shader_model_sky_uPvmPrev( pv_prev
);
726 shader_model_sky_uTexGarbage(0);
727 world_link_lighting_ub( world
, _shader_model_sky
.id
);
729 glActiveTexture( GL_TEXTURE0
);
730 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
732 glDepthMask( GL_FALSE
);
733 glDisable( GL_DEPTH_TEST
);
735 mesh_bind( &world_render
.skydome
);
736 mesh_draw( &world_render
.skydome
);
738 glEnable( GL_DEPTH_TEST
);
739 glDepthMask( GL_TRUE
);
742 static void render_world_gates( world_instance
*world
, camera
*cam
,
745 float closest
= INFINITY
;
746 struct ent_gate
*gate
= NULL
;
748 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
749 ent_gate
*gi
= mdl_arritm( &world
->ent_gate
, i
);
751 if( !(gi
->flags
& k_ent_gate_linked
) )
754 float dist
= v3_dist2( gi
->co
[0], cam
->transform
[3] );
756 vg_line_point( gi
->co
[0], 0.25f
, VG__BLUE
);
758 if( dist
< closest
){
764 world
->rendering_gate
= gate
;
766 if( gate
->flags
& k_ent_gate_locked
) return;
768 if( gate
->flags
& k_ent_gate_nonlocal
){
769 if( world_static
.load_state
!= k_world_loader_none
){
770 world
->rendering_gate
= NULL
;
774 world_instance
*dest_world
= &world_static
.instances
[ gate
->target
];
775 render_gate( world
, dest_world
, gate
, cam
, layer_depth
);
778 render_gate( world
, world
, gate
, cam
, layer_depth
);
783 static void world_prerender( world_instance
*world
)
786 if( mdl_arrcount( &world
->ent_light
) ){
787 f32 rate
= vg_maxf(0.1f
, fabsf(k_day_length
)) * vg_signf(k_day_length
);
788 world
->time
+= vg
.time_delta
* (1.0/(rate
*60.0));
794 struct ub_world_lighting
*state
= &world
->ub_lighting
;
796 state
->g_time
= world
->time
;
797 state
->g_realtime
= vg
.time_real
;
798 state
->g_debug_indices
= k_debug_light_indices
;
799 state
->g_light_preview
= k_light_preview
;
800 state
->g_debug_complexity
= k_debug_light_complexity
;
802 if( skaterift
.activity
== k_skaterift_respawning
)
803 state
->g_time_of_day
= 0.1f
;
805 state
->g_time_of_day
= vg_fractf( world
->time
);
807 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
808 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
810 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
811 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
813 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
814 state
->g_sun_dir
[0] = sinf( a
);
815 state
->g_sun_dir
[1] = cosf( a
);
816 state
->g_sun_dir
[2] = 0.2f
;
817 v3_normalize( state
->g_sun_dir
);
819 world
->probabilities
[ k_probability_curve_constant
] = 1.0f
;
820 float dp
= state
->g_day_phase
;
822 world
->probabilities
[ k_probability_curve_wildlife_day
] =
823 (dp
*dp
*0.8f
+state
->g_sunset_phase
)*0.8f
;
824 world
->probabilities
[ k_probability_curve_wildlife_night
] =
825 1.0f
-powf(fabsf((state
->g_time_of_day
-0.5f
)*5.0f
),5.0f
);
827 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
828 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
829 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
832 static void render_world( world_instance
*world
, camera
*cam
,
835 render_sky( world
, cam
);
837 render_world_routes( world
, cam
, layer_depth
);
838 render_world_standard( world
, cam
);
839 render_world_cubemapped( world
, cam
, layer_depth
);
841 render_world_vb( world
, cam
);
842 render_world_alphatest( world
, cam
);
843 render_terrain( world
, cam
);
845 if( layer_depth
== -1 ) return;
846 if( layer_depth
== 0 ){
847 world_entity_focus_render();
851 float min_dist
= INFINITY
;
853 if( mdl_arrcount( &world
->ent_route
) ){
854 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
855 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
856 float dist
= v3_dist2( route
->board_transform
[3], cam
->pos
);
858 if( dist
< min_dist
){
864 ent_route
*route
= mdl_arritm( &world
->ent_route
, closest
);
865 sfd_render( world
, cam
, route
->board_transform
);
870 if( mdl_entity_id_type(world_static
.focused_entity
) == k_ent_challenge
)
871 greyout
= world_static
.focus_strength
;
873 if( greyout
> 0.0f
){
874 glDrawBuffers( 1, (GLenum
[]){ GL_COLOR_ATTACHMENT0
} );
876 glDisable(GL_DEPTH_TEST
);
877 glDepthMask(GL_FALSE
);
878 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
879 glBlendEquation(GL_FUNC_ADD
);
881 shader_blitcolour_use();
882 shader_blitcolour_uColour( (v4f
){ 0.5f
, 0.5f
, 0.5f
, greyout
*0.56f
} );
886 glEnable(GL_DEPTH_TEST
);
887 glDepthMask(GL_TRUE
);
888 glDrawBuffers( 2, (GLenum
[]){ GL_COLOR_ATTACHMENT0
,
889 GL_COLOR_ATTACHMENT1
} );
892 render_world_fxglow( world
, cam
, layer_depth
);
897 void render_world_override_pass( world_instance
*world
,
898 struct world_pass
*pass
){
899 for( int i
=0; i
<world
->surface_count
; i
++ ){
900 struct world_surface
*mat
= &world
->surfaces
[i
];
902 if( mat
->info
.flags
& k_material_flag_ghosts
) continue;
905 if( pass
->geo_type
== k_world_geo_type_solid
)
908 sm
= &mat
->sm_no_collide
;
910 if( !sm
->indice_count
)
914 m4x3_identity( mmdl
);
915 pass
->fn_set_mdl( mmdl
);
916 pass
->fn_set_uPvmPrev( pass
->cam
->mtx_prev
.pv
);
917 pass
->fn_bind_textures( world
, mat
);
918 mdl_draw_submesh( sm
);
922 static void render_world_override( world_instance
*world
){
923 struct world_pass pass
= {
924 .cam
= &skaterift
.cam
,
925 .fn_bind_textures
= bindpoint_override
,
926 .fn_set_mdl
= shader_scene_override_uMdl
,
927 .fn_set_uPvmPrev
= shader_scene_override_uPvmPrev
,
928 .shader
= k_shader_override
931 shader_scene_override_use();
932 respawn_chooser_shader_uniforms();
933 shader_scene_override_uTexGarbage(0);
934 shader_scene_override_uTexMain(1);
935 shader_scene_override_uPv( pass
.cam
->mtx
.pv
);
937 world_link_lighting_ub( world
, _shader_scene_override
.id
);
938 world_bind_position_texture( world
, _shader_scene_override
.id
,
939 _uniform_scene_override_g_world_depth
, 2 );
940 world_bind_light_array( world
, _shader_scene_override
.id
,
941 _uniform_scene_override_uLightsArray
, 3 );
942 world_bind_light_index( world
, _shader_scene_override
.id
,
943 _uniform_scene_override_uLightsIndex
, 4 );
945 bind_terrain_noise();
946 shader_scene_override_uCamera( pass
.cam
->transform
[3] );
948 glDisable( GL_CULL_FACE
);
949 mesh_bind( &world
->mesh_geo
);
950 pass
.geo_type
= k_world_geo_type_solid
;
951 render_world_override_pass( world
, &pass
);
952 mesh_bind( &world
->mesh_no_collide
);
953 pass
.geo_type
= k_world_geo_type_nonsolid
;
954 render_world_override_pass( world
, &pass
);
955 glEnable( GL_CULL_FACE
);
958 static void render_cubemap_side( world_instance
*world
, ent_cubemap
*cm
,
961 glFramebufferTexture2D( GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
962 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ side
, cm
->texture_id
, 0 );
963 glClear( GL_DEPTH_BUFFER_BIT
);
966 { -1.0f
, 0.0f
, 0.0f
},
967 { 1.0f
, 0.0f
, 0.0f
},
968 { 0.0f
, -1.0f
, 0.0f
},
969 { 0.0f
, 1.0f
, 0.0f
},
970 { 0.0f
, 0.0f
, -1.0f
},
974 { 0.0f
, -1.0f
, 0.0f
},
975 { 0.0f
, -1.0f
, 0.0f
},
976 { 0.0f
, 0.0f
, 1.0f
},
977 { 0.0f
, 0.0f
, -1.0f
},
978 { 0.0f
, -1.0f
, 0.0f
},
979 { 0.0f
, -1.0f
, 0.0f
}
982 v3_zero( cam
.angles
);
983 v3_copy( cm
->co
, cam
.pos
);
985 v3_copy( forward
[side
], cam
.transform
[2] );
986 v3_copy( up
[side
], cam
.transform
[1] );
987 v3_cross( up
[side
], forward
[side
], cam
.transform
[0] );
988 v3_copy( cm
->co
, cam
.transform
[3] );
989 m4x3_invert_affine( cam
.transform
, cam
.transform_inverse
);
991 camera_update_view( &cam
);
996 m4x4_copy( cam
.mtx
.p
, cam
.mtx_prev
.p
);
997 m4x4_projection( cam
.mtx
.p
, cam
.fov
, 1.0f
, cam
.nearz
, cam
.farz
);
998 camera_finalize( &cam
);
999 camera_finalize( &cam
);
1001 render_world( world
, &cam
, -1 );
1004 static void render_world_cubemaps( world_instance
*world
){
1005 if( world
->cubemap_cooldown
)
1006 world
->cubemap_cooldown
--;
1008 world
->cubemap_cooldown
= 60;
1010 glViewport( 0, 0, WORLD_CUBEMAP_RES
, WORLD_CUBEMAP_RES
);
1011 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_cubemap
); i
++ ){
1012 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, i
);
1013 glBindFramebuffer( GL_FRAMEBUFFER
, cm
->framebuffer_id
);
1015 world
->cubemap_side
++;
1016 if( world
->cubemap_side
>= 6 )
1017 world
->cubemap_side
= 0;
1019 render_cubemap_side( world
, cm
, world
->cubemap_side
);
1024 static void render_world_depth( world_instance
*world
, camera
*cam
)
1026 m4x3f identity_matrix
;
1027 m4x3_identity( identity_matrix
);
1029 shader_scene_depth_use();
1030 shader_scene_depth_uCamera( cam
->transform
[3] );
1031 shader_scene_depth_uPv( cam
->mtx
.pv
);
1032 shader_scene_depth_uPvmPrev( cam
->mtx_prev
.pv
);
1033 shader_scene_depth_uMdl( identity_matrix
);
1034 world_link_lighting_ub( world
, _shader_scene_depth
.id
);
1036 mesh_bind( &world
->mesh_geo
);
1037 mesh_draw( &world
->mesh_geo
);
1040 static void render_world_position( world_instance
*world
, camera
*cam
)
1042 m4x3f identity_matrix
;
1043 m4x3_identity( identity_matrix
);
1045 shader_scene_position_use();
1046 shader_scene_position_uCamera( cam
->transform
[3] );
1047 shader_scene_position_uPv( cam
->mtx
.pv
);
1048 shader_scene_position_uPvmPrev( cam
->mtx_prev
.pv
);
1049 shader_scene_position_uMdl( identity_matrix
);
1050 world_link_lighting_ub( world
, _shader_scene_position
.id
);
1052 mesh_bind( &world
->mesh_geo
);
1053 mesh_draw( &world
->mesh_geo
);