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();
87 shader_model_sky_space_register();
89 vg_info( "Loading world resources\n" );
90 vg_linear_clear( vg_mem
.scratch
);
93 mdl_open( &msky
, "models/rs_skydome.mdl", vg_mem
.scratch
);
94 mdl_load_metadata_block( &msky
, vg_mem
.scratch
);
95 mdl_async_load_glmesh( &msky
, &world_render
.skydome
);
98 vg_info( "Loading default world textures\n" );
99 vg_tex2d_load_qoi_async_file( "textures/garbage.qoi",
100 VG_TEX2D_NEAREST
|VG_TEX2D_REPEAT
,
101 &world_render
.tex_terrain_noise
);
103 vg_async_call( async_world_render_init
, NULL
, 0 );
106 static void world_link_lighting_ub( world_instance
*world
, GLuint shader
){
107 GLuint idx
= glGetUniformBlockIndex( shader
, "ub_world_lighting" );
108 glUniformBlockBinding( shader
, idx
, world
->ubo_bind_point
);
111 static void world_bind_position_texture( world_instance
*world
,
112 GLuint shader
, GLuint location
,
114 render_fb_bind_texture( &world
->heightmap
, 0, slot
);
115 glUniform1i( location
, slot
);
118 static void world_bind_light_array( world_instance
*world
,
119 GLuint shader
, GLuint location
,
121 glActiveTexture( GL_TEXTURE0
+ slot
);
122 glBindTexture( GL_TEXTURE_BUFFER
, world
->tex_light_entities
);
123 glUniform1i( location
, slot
);
126 static void world_bind_light_index( world_instance
*world
,
127 GLuint shader
, GLuint location
,
129 glActiveTexture( GL_TEXTURE0
+ slot
);
130 glBindTexture( GL_TEXTURE_3D
, world
->tex_light_cubes
);
131 glUniform1i( location
, slot
);
134 static void render_world_depth( world_instance
*world
, camera
*cam
);
140 static void bind_terrain_noise(void){
141 glActiveTexture( GL_TEXTURE0
);
142 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
147 enum mdl_shader shader
;
148 enum world_geo_type geo_type
;
150 void (*fn_bind_textures
)( world_instance
*world
,
151 struct world_surface
*mat
);
152 void (*fn_set_mdl
)( m4x3f mdl
);
153 void (*fn_set_uPvmPrev
)( m4x4f pvm
);
154 void (*fn_set_uNormalMtx
)( m3x3f mnorm
);
157 /* FIXME: we gotta do something about this crap, maybe batch lists. something..
158 * anything but this. */
160 void world_render_props( world_instance
*world
, u32 material_id
,
161 struct world_pass
*pass
){
162 if( !mdl_arrcount( &world
->ent_prop
) ) return;
164 /* HACK: use the first material for every prop entity */
165 ent_prop
*first
= mdl_arritm( &world
->ent_prop
, 0 );
166 if( !first
->submesh_count
) return;
168 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
, first
->submesh_start
);
169 if( sm
->material_id
!= material_id
) return;
171 struct world_surface
*mat
= &world
->surfaces
[ material_id
];
172 pass
->fn_bind_textures( world
, mat
);
174 for( u32 j
=0; j
<mdl_arrcount( &world
->ent_prop
); j
++ ){
175 ent_prop
*prop
= mdl_arritm( &world
->ent_prop
, j
);
176 if( prop
->flags
& 0x1 ) continue;
178 for( u32 k
=0; k
<prop
->submesh_count
; k
++ ){
179 sm
= mdl_arritm( &world
->meta
.submeshs
, prop
->submesh_start
+k
);
182 mdl_transform_m4x3( &prop
->transform
, mmdl
);
185 m4x3_expand( mmdl
, m4mdl
);
186 m4x4_mul( pass
->cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
188 pass
->fn_set_mdl( mmdl
);
189 pass
->fn_set_uPvmPrev( m4mdl
);
191 mdl_draw_submesh( sm
);
197 void world_render_traffic( world_instance
*world
, u32 material_id
,
198 struct world_pass
*pass
){
199 if( !mdl_arrcount( &world
->ent_traffic
) ) return;
201 /* HACK: use the first material for every traffic entity */
202 ent_traffic
*first
= mdl_arritm( &world
->ent_traffic
, 0 );
203 if( !first
->submesh_count
) return;
205 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
, first
->submesh_start
);
206 if( sm
->material_id
!= material_id
) return;
208 struct world_surface
*mat
= &world
->surfaces
[ material_id
];
209 pass
->fn_bind_textures( world
, mat
);
211 for( u32 j
=0; j
<mdl_arrcount( &world
->ent_traffic
); j
++ ){
212 ent_traffic
*traffic
= mdl_arritm( &world
->ent_traffic
, j
);
214 for( u32 k
=0; k
<traffic
->submesh_count
; k
++ ){
215 sm
= mdl_arritm( &world
->meta
.submeshs
,
216 traffic
->submesh_start
+k
);
219 q_m3x3( traffic
->transform
.q
, mmdl
);
220 v3_copy( traffic
->transform
.co
, mmdl
[3] );
223 m4x3_expand( mmdl
, m4mdl
);
224 m4x4_mul( pass
->cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
226 pass
->fn_set_mdl( mmdl
);
227 pass
->fn_set_uPvmPrev( m4mdl
);
229 mdl_draw_submesh( sm
);
235 void world_render_pass( world_instance
*world
, struct world_pass
*pass
){
236 for( int i
=0; i
<world
->surface_count
; i
++ ){
237 struct world_surface
*mat
= &world
->surfaces
[i
];
239 if( mat
->info
.shader
== pass
->shader
){
242 if( pass
->geo_type
== k_world_geo_type_solid
){
246 world_render_traffic( world
, i
, pass
);
247 world_render_props( world
, i
, pass
);
248 sm
= &mat
->sm_no_collide
;
251 if( !sm
->indice_count
)
255 m4x3_identity( mmdl
);
256 pass
->fn_set_mdl( mmdl
);
257 pass
->fn_set_uPvmPrev( pass
->cam
->mtx_prev
.pv
);
259 pass
->fn_bind_textures( world
, mat
);
260 mdl_draw_submesh( sm
);
266 void world_render_both_stages( world_instance
*world
, struct world_pass
*pass
)
268 mesh_bind( &world
->mesh_geo
);
269 pass
->geo_type
= k_world_geo_type_solid
;
270 world_render_pass( world
, pass
);
272 glDisable( GL_CULL_FACE
);
273 mesh_bind( &world
->mesh_no_collide
);
274 pass
->geo_type
= k_world_geo_type_nonsolid
;
275 world_render_pass( world
, pass
);
276 glEnable( GL_CULL_FACE
);
279 static void bindpoint_diffuse_texture1( world_instance
*world
,
280 struct world_surface
*mat
)
283 glActiveTexture( GL_TEXTURE1
);
284 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
287 static void bindpoint_diffuse1_and_cubemap10( world_instance
*world
,
288 struct world_surface
*mat
){
289 glActiveTexture( GL_TEXTURE1
);
290 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
292 u32 cubemap_id
= mat
->info
.tex_none0
,
295 if( mdl_entity_id_type( cubemap_id
) == k_ent_cubemap
){
296 cubemap_index
= mdl_entity_id_id( cubemap_id
);
299 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, cubemap_index
);
300 glActiveTexture( GL_TEXTURE10
);
301 glBindTexture( GL_TEXTURE_CUBE_MAP
, cm
->texture_id
);
303 shader_scene_cubemapped_uColour( mat
->info
.colour
);
306 static void render_world_vb( world_instance
*world
, camera
*cam
){
307 shader_scene_vertex_blend_use();
308 shader_scene_vertex_blend_uTexGarbage(0);
309 shader_scene_vertex_blend_uTexGradients(1);
310 world_link_lighting_ub( world
, _shader_scene_vertex_blend
.id
);
311 world_bind_position_texture( world
, _shader_scene_vertex_blend
.id
,
312 _uniform_scene_vertex_blend_g_world_depth
, 2 );
313 world_bind_light_array( world
, _shader_scene_vertex_blend
.id
,
314 _uniform_scene_vertex_blend_uLightsArray
, 3 );
315 world_bind_light_index( world
, _shader_scene_vertex_blend
.id
,
316 _uniform_scene_vertex_blend_uLightsIndex
, 4 );
318 glActiveTexture( GL_TEXTURE0
);
319 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
321 shader_scene_vertex_blend_uPv( cam
->mtx
.pv
);
322 shader_scene_vertex_blend_uCamera( cam
->transform
[3] );
324 struct world_pass pass
= {
325 .shader
= k_shader_standard_vertex_blend
,
327 .fn_bind_textures
= bindpoint_diffuse_texture1
,
328 .fn_set_mdl
= shader_scene_vertex_blend_uMdl
,
329 .fn_set_uPvmPrev
= shader_scene_vertex_blend_uPvmPrev
,
332 world_render_both_stages( world
, &pass
);
335 static void world_shader_standard_bind( world_instance
*world
, camera
*cam
){
336 shader_scene_standard_use();
337 shader_scene_standard_uTexGarbage(0);
338 shader_scene_standard_uTexMain(1);
339 shader_scene_standard_uPv( cam
->mtx
.pv
);
341 world_link_lighting_ub( world
, _shader_scene_standard
.id
);
342 world_bind_position_texture( world
, _shader_scene_standard
.id
,
343 _uniform_scene_standard_g_world_depth
, 2 );
344 world_bind_light_array( world
, _shader_scene_standard
.id
,
345 _uniform_scene_standard_uLightsArray
, 3 );
346 world_bind_light_index( world
, _shader_scene_standard
.id
,
347 _uniform_scene_standard_uLightsIndex
, 4 );
349 bind_terrain_noise();
350 shader_scene_standard_uCamera( cam
->transform
[3] );
353 static void render_world_standard( world_instance
*world
, camera
*cam
){
354 world_shader_standard_bind( world
, cam
);
355 struct world_pass pass
= {
356 .shader
= k_shader_standard
,
358 .fn_bind_textures
= bindpoint_diffuse_texture1
,
359 .fn_set_mdl
= shader_scene_standard_uMdl
,
360 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
363 world_render_both_stages( world
, &pass
);
366 static void render_world_cubemapped( world_instance
*world
, camera
*cam
,
368 if( !mdl_arrcount( &world
->ent_cubemap
) )
371 if( layer_depth
== -1 ){
372 world_shader_standard_bind( world
, cam
);
374 struct world_pass pass
= {
375 .shader
= k_shader_cubemap
,
377 .fn_bind_textures
= bindpoint_diffuse_texture1
,
378 .fn_set_mdl
= shader_scene_standard_uMdl
,
379 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
382 world_render_both_stages( world
, &pass
);
385 shader_scene_cubemapped_use();
386 shader_scene_cubemapped_uTexGarbage(0);
387 shader_scene_cubemapped_uTexMain(1);
388 shader_scene_cubemapped_uTexCubemap(10);
389 shader_scene_cubemapped_uPv( cam
->mtx
.pv
);
391 world_link_lighting_ub( world
, _shader_scene_cubemapped
.id
);
392 world_bind_position_texture( world
, _shader_scene_cubemapped
.id
,
393 _uniform_scene_cubemapped_g_world_depth
, 2 );
394 world_bind_light_array( world
, _shader_scene_cubemapped
.id
,
395 _uniform_scene_cubemapped_uLightsArray
, 3 );
396 world_bind_light_index( world
, _shader_scene_cubemapped
.id
,
397 _uniform_scene_cubemapped_uLightsIndex
, 4 );
399 bind_terrain_noise();
400 shader_scene_cubemapped_uCamera( cam
->transform
[3] );
402 struct world_pass pass
= {
403 .shader
= k_shader_cubemap
,
405 .fn_bind_textures
= bindpoint_diffuse1_and_cubemap10
,
406 .fn_set_mdl
= shader_scene_cubemapped_uMdl
,
407 .fn_set_uPvmPrev
= shader_scene_cubemapped_uPvmPrev
,
410 world_render_both_stages( world
, &pass
);
414 static void render_world_alphatest( world_instance
*world
, camera
*cam
){
415 shader_scene_standard_alphatest_use();
416 shader_scene_standard_alphatest_uTexGarbage(0);
417 shader_scene_standard_alphatest_uTexMain(1);
418 shader_scene_standard_alphatest_uPv( cam
->mtx
.pv
);
420 world_link_lighting_ub( world
, _shader_scene_standard_alphatest
.id
);
421 world_bind_position_texture( world
, _shader_scene_standard_alphatest
.id
,
422 _uniform_scene_standard_alphatest_g_world_depth
, 2 );
423 world_bind_light_array( world
, _shader_scene_standard_alphatest
.id
,
424 _uniform_scene_standard_alphatest_uLightsArray
, 3 );
425 world_bind_light_index( world
, _shader_scene_standard_alphatest
.id
,
426 _uniform_scene_standard_alphatest_uLightsIndex
, 4 );
429 bind_terrain_noise();
432 shader_scene_standard_alphatest_uCamera( cam
->transform
[3] );
434 glDisable(GL_CULL_FACE
);
436 struct world_pass pass
= {
437 .shader
= k_shader_standard_cutout
,
439 .fn_bind_textures
= bindpoint_diffuse_texture1
,
440 .fn_set_mdl
= shader_scene_standard_alphatest_uMdl
,
441 .fn_set_uPvmPrev
= shader_scene_standard_alphatest_uPvmPrev
,
444 world_render_both_stages( world
, &pass
);
446 glEnable(GL_CULL_FACE
);
450 void world_render_challenges( world_instance
*world
, struct world_pass
*pass
,
451 v3f pos
, int layer_depth
){
453 if( skaterift
.activity
== k_skaterift_replay
) return;
454 if( world
!= world_current_instance() ) return;
459 u32 objective_list
[ 32 ],
460 challenge_list
[ 16 ];
462 v2f objective_uv_offsets
[ 32 ];
464 u32 objective_count
= 0,
467 ent_challenge
*active_challenge
= NULL
;
469 if( mdl_entity_id_type( world_static
.focused_entity
) == k_ent_challenge
){
470 if( (skaterift
.activity
== k_skaterift_default
) &&
471 world_static
.challenge_target
){
475 if( !((skaterift
.activity
!= k_skaterift_ent_focus
) &&
476 !world_static
.challenge_target
) ){
477 world_instance
*challenge_world
= world_current_instance();
478 u32 index
= mdl_entity_id_id( world_static
.focused_entity
);
479 active_challenge
= mdl_arritm(&challenge_world
->ent_challenge
, index
);
483 if( active_challenge
){
484 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
485 challenge_list
[ challenge_count
++ ] = world_static
.focused_entity
;
487 u32 next
= active_challenge
->first
;
488 while( mdl_entity_id_type(next
) == k_ent_objective
){
489 u32 index
= mdl_entity_id_id( next
);
490 objective_list
[ objective_count
++ ] = index
;
492 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
493 next
= objective
->id_next
;
499 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
501 bh_iter_init_range( 0, &it
, pos
, radius
+10.0f
);
503 while( bh_next( world
->entity_bh
, &it
, &idx
) ){
504 u32 id
= world
->entity_list
[ idx
],
505 type
= mdl_entity_id_type( id
),
506 index
= mdl_entity_id_id( id
);
508 if( type
== k_ent_objective
) {
509 if( objective_count
< vg_list_size(objective_list
) )
510 objective_list
[ objective_count
++ ] = index
;
512 else if( type
== k_ent_challenge
){
513 if( challenge_count
< vg_list_size(challenge_list
) )
514 challenge_list
[ challenge_count
++ ] = index
;
519 /* render objectives */
520 glDisable( GL_CULL_FACE
);
521 mesh_bind( &world
->mesh_no_collide
);
522 u32 last_material
= 0;
523 for( u32 i
=0; i
<objective_count
; i
++ ){
524 u32 index
= objective_list
[ i
];
525 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
526 if( (objective
->flags
& k_ent_objective_hidden
) &&
527 !active_challenge
) continue;
532 u32 passed
= objective
->flags
& k_ent_objective_passed
;
533 f32 target
= passed
? 0.0f
: 1.0f
;
534 vg_slewf(&objective
->transform
.s
[0], target
, vg
.time_frame_delta
*4.0f
);
535 scale
= vg_smoothstepf( objective
->transform
.s
[0] );
537 if( (objective
== world_static
.challenge_target
) || passed
)
538 shader_scene_fxglow_uUvOffset( (v2f
){ 16.0f
/256.0f
, 0.0f
} );
540 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
543 f32 dist
= v3_dist( objective
->transform
.co
, pos
) * (1.0f
/radius
);
544 scale
= vg_smoothstepf( vg_clampf( 5.0f
-dist
*5.0f
, 0.0f
,1.0f
) );
548 q_m3x3( objective
->transform
.q
, mmdl
);
549 m3x3_scalef( mmdl
, scale
);
550 v3_copy( objective
->transform
.co
, mmdl
[3] );
551 shader_scene_fxglow_uMdl( mmdl
);
553 for( u32 j
=0; j
<objective
->submesh_count
; j
++ ){
554 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
555 objective
->submesh_start
+ j
);
557 if( sm
->material_id
!= last_material
){
558 last_material
= sm
->material_id
;
559 pass
->fn_bind_textures( world
, &world
->surfaces
[sm
->material_id
] );
561 mdl_draw_submesh( sm
);
566 font3d_bind( &gui
.font
, k_font_shader_world
, 0, world
, &skaterift
.cam
);
571 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_challenge
); i
++ ){
572 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, i
);
573 if( challenge
->status
) count
++;
577 c
+=highscore_intl( buf
+c
, count
, 3 );
579 c
+=highscore_intl( buf
+c
, mdl_arrcount(&world
->ent_challenge
), 3 );
582 f32 w
= font3d_string_width( 1, buf
);
584 m3x3_identity( mlocal
);
585 mlocal
[3][0] = -w
*0.5f
;
589 for( u32 i
=0; i
<challenge_count
; i
++ ){
590 u32 index
= challenge_list
[ i
];
591 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, index
);
593 mdl_transform_m4x3( &challenge
->transform
, mmdl
);
594 m4x3_mul( mmdl
, mlocal
, mmdl
);
596 vg_line_point( challenge
->transform
.co
, 0.25f
, VG__RED
);
598 f32 dist
= v3_dist( challenge
->transform
.co
, pos
) * (1.0f
/radius
),
599 scale
= vg_smoothstepf( vg_clampf( 10.0f
-dist
*10.0f
, 0.0f
,1.0f
) ),
602 if( challenge
->status
)
605 shader_scene_font_uOpacity( scale
);
606 shader_scene_font_uColourize( colour
);
607 font3d_simple_draw( 1, buf
, &skaterift
.cam
, mmdl
);
611 static void render_world_fxglow( world_instance
*world
, camera
*cam
,
613 shader_scene_fxglow_use();
614 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
615 shader_scene_fxglow_uTexMain(1);
616 shader_scene_fxglow_uPv( cam
->mtx
.pv
);
618 world_link_lighting_ub( world
, _shader_scene_fxglow
.id
);
619 world_bind_position_texture( world
, _shader_scene_fxglow
.id
,
620 _uniform_scene_fxglow_g_world_depth
, 2 );
621 world_bind_light_array( world
, _shader_scene_fxglow
.id
,
622 _uniform_scene_fxglow_uLightsArray
, 3 );
623 world_bind_light_index( world
, _shader_scene_fxglow
.id
,
624 _uniform_scene_fxglow_uLightsIndex
, 4 );
626 shader_scene_fxglow_uCamera( cam
->transform
[3] );
627 glDisable(GL_CULL_FACE
);
629 struct world_pass pass
= {
630 .shader
= k_shader_fxglow
,
632 .fn_bind_textures
= bindpoint_diffuse_texture1
,
633 .fn_set_mdl
= shader_scene_fxglow_uMdl
,
634 .fn_set_uPvmPrev
= shader_scene_fxglow_uPvmPrev
,
637 world_render_both_stages( world
, &pass
);
638 world_render_challenges( world
, &pass
, cam
->pos
, layer_depth
);
640 glEnable(GL_CULL_FACE
);
643 static void bindpoint_terrain( world_instance
*world
,
644 struct world_surface
*mat
)
646 glActiveTexture( GL_TEXTURE1
);
647 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
649 shader_scene_terrain_uSandColour( mat
->info
.colour
);
650 shader_scene_terrain_uBlendOffset( mat
->info
.colour1
);
653 static void bindpoint_override( world_instance
*world
,
654 struct world_surface
*mat
){
655 if( mat
->info
.flags
& k_material_flag_collision
){
656 shader_scene_override_uAlphatest(0);
659 glActiveTexture( GL_TEXTURE1
);
660 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
661 shader_scene_override_uAlphatest(1);
665 static void render_terrain( world_instance
*world
, camera
*cam
){
666 shader_scene_terrain_use();
667 shader_scene_terrain_uTexGarbage(0);
668 shader_scene_terrain_uTexGradients(1);
670 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_terrain
);
671 glActiveTexture( GL_TEXTURE0
);
672 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
674 shader_scene_terrain_uPv( cam
->mtx
.pv
);
675 shader_scene_terrain_uCamera( cam
->transform
[3] );
677 struct world_pass pass
= {
678 .shader
= k_shader_terrain_blend
,
680 .fn_bind_textures
= bindpoint_terrain
,
681 .fn_set_mdl
= shader_scene_terrain_uMdl
,
682 .fn_set_uPvmPrev
= shader_scene_terrain_uPvmPrev
,
685 world_render_both_stages( world
, &pass
);
688 static void render_sky( world_instance
*world
, camera
*cam
){
690 * Modify matrix to remove clipping and view translation
697 m4x4_copy( cam
->mtx
.v
, v
);
698 m4x4_copy( cam
->mtx_prev
.v
, v_prev
);
700 v3_zero( v_prev
[3] );
702 m4x4_copy( cam
->mtx
.p
, pv
);
703 m4x4_copy( cam
->mtx_prev
.p
, pv_prev
);
704 m4x4_reset_clipping( pv
, cam
->farz
, cam
->nearz
);
705 m4x4_reset_clipping( pv_prev
, cam
->farz
, cam
->nearz
);
707 m4x4_mul( pv
, v
, pv
);
708 m4x4_mul( pv_prev
, v_prev
, pv_prev
);
710 m4x3f identity_matrix
;
711 m4x3_identity( identity_matrix
);
716 if( world
->skybox
== k_skybox_default
){
717 shader_model_sky_use();
718 shader_model_sky_uMdl( identity_matrix
);
719 shader_model_sky_uPv( pv
);
720 shader_model_sky_uPvmPrev( pv_prev
);
721 shader_model_sky_uTexGarbage(0);
722 world_link_lighting_ub( world
, _shader_model_sky
.id
);
724 glActiveTexture( GL_TEXTURE0
);
725 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
727 else if( world
->skybox
== k_skybox_space
){
728 shader_model_sky_space_use();
730 shader_model_sky_space_uMdl( identity_matrix
);
731 shader_model_sky_space_uPv( pv
);
732 shader_model_sky_space_uPvmPrev( pv_prev
);
733 shader_model_sky_space_uTexGarbage(0);
734 world_link_lighting_ub( world
, _shader_model_sky_space
.id
);
736 glActiveTexture( GL_TEXTURE0
);
737 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
743 glDepthMask( GL_FALSE
);
744 glDisable( GL_DEPTH_TEST
);
746 mesh_bind( &world_render
.skydome
);
747 mesh_draw( &world_render
.skydome
);
749 glEnable( GL_DEPTH_TEST
);
750 glDepthMask( GL_TRUE
);
753 static void render_world_gates( world_instance
*world
, camera
*cam
,
756 float closest
= INFINITY
;
757 struct ent_gate
*gate
= NULL
;
759 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
760 ent_gate
*gi
= mdl_arritm( &world
->ent_gate
, i
);
762 if( !(gi
->flags
& (k_ent_gate_linked
|k_ent_gate_nonlocal_DELETED
|
763 k_ent_gate_locked
)) )
766 float dist
= v3_dist2( gi
->co
[0], cam
->transform
[3] );
768 vg_line_point( gi
->co
[0], 0.25f
, VG__BLUE
);
770 if( dist
< closest
){
776 world
->rendering_gate
= gate
;
778 render_gate( world
, world
, gate
, cam
, layer_depth
);
781 static void world_prerender( world_instance
*world
){
782 if( mdl_arrcount( &world
->ent_light
) ){
783 f32 rate
= vg_maxf(0.1f
, fabsf(k_day_length
)) * vg_signf(k_day_length
);
784 world
->time
+= vg
.time_frame_delta
* (1.0/(rate
*60.0));
790 struct ub_world_lighting
*state
= &world
->ub_lighting
;
792 state
->g_time
= world
->time
;
793 state
->g_realtime
= vg
.time_real
;
794 state
->g_debug_indices
= k_debug_light_indices
;
795 state
->g_light_preview
= k_light_preview
;
796 state
->g_debug_complexity
= k_debug_light_complexity
;
798 if( skaterift
.activity
== k_skaterift_respawning
)
799 state
->g_time_of_day
= 0.1f
;
801 state
->g_time_of_day
= vg_fractf( world
->time
);
803 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
804 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
806 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
807 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
809 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
810 state
->g_sun_dir
[0] = sinf( a
);
811 state
->g_sun_dir
[1] = cosf( a
);
812 state
->g_sun_dir
[2] = 0.2f
;
813 v3_normalize( state
->g_sun_dir
);
815 world
->probabilities
[ k_probability_curve_constant
] = 1.0f
;
816 float dp
= state
->g_day_phase
;
818 world
->probabilities
[ k_probability_curve_wildlife_day
] =
819 (dp
*dp
*0.8f
+state
->g_sunset_phase
)*0.8f
;
820 world
->probabilities
[ k_probability_curve_wildlife_night
] =
821 1.0f
-powf(fabsf((state
->g_time_of_day
-0.5f
)*5.0f
),5.0f
);
823 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
824 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
825 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
828 static void render_world( world_instance
*world
, camera
*cam
,
831 render_sky( world
, cam
);
833 render_world_routes( world
, cam
, layer_depth
);
834 render_world_standard( world
, cam
);
835 render_world_cubemapped( world
, cam
, layer_depth
);
837 render_world_vb( world
, cam
);
838 render_world_alphatest( world
, cam
);
839 render_terrain( world
, cam
);
841 if( layer_depth
== -1 ) return;
842 if( layer_depth
== 0 ){
843 world_entity_focus_render();
847 float min_dist
= INFINITY
;
849 if( mdl_arrcount( &world
->ent_route
) ){
850 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
851 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
852 float dist
= v3_dist2( route
->board_transform
[3], cam
->pos
);
854 if( dist
< min_dist
){
860 ent_route
*route
= mdl_arritm( &world
->ent_route
, closest
);
861 sfd_render( world
, cam
, route
->board_transform
);
866 if( mdl_entity_id_type(world_static
.focused_entity
) == k_ent_challenge
)
867 greyout
= world_static
.focus_strength
;
869 if( greyout
> 0.0f
){
870 glDrawBuffers( 1, (GLenum
[]){ GL_COLOR_ATTACHMENT0
} );
872 glDisable(GL_DEPTH_TEST
);
873 glDepthMask(GL_FALSE
);
874 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
875 glBlendEquation(GL_FUNC_ADD
);
877 shader_blitcolour_use();
878 shader_blitcolour_uColour( (v4f
){ 0.5f
, 0.5f
, 0.5f
, greyout
*0.56f
} );
882 glEnable(GL_DEPTH_TEST
);
883 glDepthMask(GL_TRUE
);
884 glDrawBuffers( 2, (GLenum
[]){ GL_COLOR_ATTACHMENT0
,
885 GL_COLOR_ATTACHMENT1
} );
888 render_world_fxglow( world
, cam
, layer_depth
);
892 static void render_world_override_pass( world_instance
*world
,
893 struct world_pass
*pass
,
894 m4x3f mmdl
, m3x3f mnormal
,
896 for( int i
=0; i
<world
->surface_count
; i
++ ){
897 struct world_surface
*mat
= &world
->surfaces
[i
];
899 if( mat
->info
.flags
& k_material_flag_ghosts
) continue;
902 if( pass
->geo_type
== k_world_geo_type_solid
)
905 sm
= &mat
->sm_no_collide
;
907 if( !sm
->indice_count
)
910 pass
->fn_set_mdl( mmdl
);
911 pass
->fn_set_uNormalMtx( mnormal
);
912 pass
->fn_set_uPvmPrev( mpvm_prev
);
913 pass
->fn_bind_textures( world
, mat
);
914 mdl_draw_submesh( sm
);
918 static void render_world_override( world_instance
*world
,
919 world_instance
*lighting_source
,
921 struct world_pass pass
= {
922 .cam
= &skaterift
.cam
,
923 .fn_bind_textures
= bindpoint_override
,
924 .fn_set_mdl
= shader_scene_override_uMdl
,
925 .fn_set_uPvmPrev
= shader_scene_override_uPvmPrev
,
926 .fn_set_uNormalMtx
= shader_scene_override_uNormalMtx
,
927 .shader
= k_shader_override
930 shader_scene_override_use();
932 respawn_chooser_shader_uniforms();
934 shader_scene_override_uTexGarbage(0);
935 shader_scene_override_uTexMain(1);
936 shader_scene_override_uPv( pass
.cam
->mtx
.pv
);
938 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( lighting_source
, scene_override
);
939 bind_terrain_noise();
941 shader_scene_override_uCamera( pass
.cam
->transform
[3] );
944 m4x3_expand( mmdl
, mpvm_prev
);
945 m4x4_mul( skaterift
.cam
.mtx_prev
.pv
, mpvm_prev
, mpvm_prev
);
948 m3x3_inv( mmdl
, mnormal
);
949 m3x3_transpose( mnormal
, mnormal
);
950 v3_normalize( mnormal
[0] );
951 v3_normalize( mnormal
[1] );
952 v3_normalize( mnormal
[2] );
954 v4f uPlayerPos
, uSpawnPos
;
955 v4_zero( uPlayerPos
);
956 v4_zero( uSpawnPos
);
958 v3_copy( world
->player_co
, uPlayerPos
);
961 m4x3_invert_full( mmdl
, mmdl_inv
);
963 m4x3_mulv( mmdl_inv
, localplayer
.rb
.co
, localized
);
964 ent_spawn
*spawn
= world_find_closest_spawn( world
, localized
);
966 v3_copy( spawn
->transform
.co
, uSpawnPos
);
968 uPlayerPos
[3] = v3_dist(uPlayerPos
,uSpawnPos
);
969 uSpawnPos
[3] = 1.0f
/uPlayerPos
[3];
971 shader_scene_override_uPlayerPos( uPlayerPos
);
972 shader_scene_override_uSpawnPos( uSpawnPos
);
976 glDisable( GL_CULL_FACE
);
977 mesh_bind( &world
->mesh_geo
);
978 pass
.geo_type
= k_world_geo_type_solid
;
979 render_world_override_pass( world
, &pass
, mmdl
, mnormal
, mpvm_prev
);
980 mesh_bind( &world
->mesh_no_collide
);
981 pass
.geo_type
= k_world_geo_type_nonsolid
;
982 render_world_override_pass( world
, &pass
, mmdl
, mnormal
, mpvm_prev
);
983 glEnable( GL_CULL_FACE
);
986 static void render_cubemap_side( world_instance
*world
, ent_cubemap
*cm
,
989 glFramebufferTexture2D( GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
990 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ side
, cm
->texture_id
, 0 );
991 glClear( GL_DEPTH_BUFFER_BIT
);
994 { -1.0f
, 0.0f
, 0.0f
},
995 { 1.0f
, 0.0f
, 0.0f
},
996 { 0.0f
, -1.0f
, 0.0f
},
997 { 0.0f
, 1.0f
, 0.0f
},
998 { 0.0f
, 0.0f
, -1.0f
},
1002 { 0.0f
, -1.0f
, 0.0f
},
1003 { 0.0f
, -1.0f
, 0.0f
},
1004 { 0.0f
, 0.0f
, 1.0f
},
1005 { 0.0f
, 0.0f
, -1.0f
},
1006 { 0.0f
, -1.0f
, 0.0f
},
1007 { 0.0f
, -1.0f
, 0.0f
}
1010 v3_zero( cam
.angles
);
1011 v3_copy( cm
->co
, cam
.pos
);
1013 v3_copy( forward
[side
], cam
.transform
[2] );
1014 v3_copy( up
[side
], cam
.transform
[1] );
1015 v3_cross( up
[side
], forward
[side
], cam
.transform
[0] );
1016 v3_copy( cm
->co
, cam
.transform
[3] );
1017 m4x3_invert_affine( cam
.transform
, cam
.transform_inverse
);
1019 camera_update_view( &cam
);
1024 m4x4_copy( cam
.mtx
.p
, cam
.mtx_prev
.p
);
1025 m4x4_projection( cam
.mtx
.p
, cam
.fov
, 1.0f
, cam
.nearz
, cam
.farz
);
1026 camera_finalize( &cam
);
1027 camera_finalize( &cam
);
1029 render_world( world
, &cam
, -1 );
1032 static void render_world_cubemaps( world_instance
*world
){
1033 if( world
->cubemap_cooldown
)
1034 world
->cubemap_cooldown
--;
1036 world
->cubemap_cooldown
= 60;
1038 glViewport( 0, 0, WORLD_CUBEMAP_RES
, WORLD_CUBEMAP_RES
);
1039 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_cubemap
); i
++ ){
1040 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, i
);
1041 glBindFramebuffer( GL_FRAMEBUFFER
, cm
->framebuffer_id
);
1043 world
->cubemap_side
++;
1044 if( world
->cubemap_side
>= 6 )
1045 world
->cubemap_side
= 0;
1047 render_cubemap_side( world
, cm
, world
->cubemap_side
);
1052 static void render_world_depth( world_instance
*world
, camera
*cam
){
1053 m4x3f identity_matrix
;
1054 m4x3_identity( identity_matrix
);
1056 shader_scene_depth_use();
1057 shader_scene_depth_uCamera( cam
->transform
[3] );
1058 shader_scene_depth_uPv( cam
->mtx
.pv
);
1059 shader_scene_depth_uPvmPrev( cam
->mtx_prev
.pv
);
1060 shader_scene_depth_uMdl( identity_matrix
);
1061 world_link_lighting_ub( world
, _shader_scene_depth
.id
);
1063 mesh_bind( &world
->mesh_geo
);
1064 mesh_draw( &world
->mesh_geo
);
1067 static void render_world_position( world_instance
*world
, camera
*cam
){
1068 m4x3f identity_matrix
;
1069 m4x3_identity( identity_matrix
);
1071 shader_scene_position_use();
1072 shader_scene_position_uCamera( cam
->transform
[3] );
1073 shader_scene_position_uPv( cam
->mtx
.pv
);
1074 shader_scene_position_uPvmPrev( cam
->mtx_prev
.pv
);
1075 shader_scene_position_uMdl( identity_matrix
);
1076 world_link_lighting_ub( world
, _shader_scene_position
.id
);
1078 mesh_bind( &world
->mesh_geo
);
1079 mesh_draw( &world
->mesh_geo
);