2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
9 #include "world_render.h"
13 #include "ent_miniworld.h"
14 #include "player_remote.h"
16 static int ccmd_set_time( int argc
, const char *argv
[] ){
17 world_instance
*world
= world_current_instance();
19 world
->time
= atof( argv
[0] );
21 vg_error( "Usage set_time <0-1.0> (current time: %f)\n", world
->time
);
25 static void async_world_render_init( void *payload
, u32 size
)
27 vg_info( "Allocate uniform buffers\n" );
28 for( int i
=0; i
<k_world_max
; 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
<k_world_max
; 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();
89 shader_model_sky_space_register();
91 vg_info( "Loading world resources\n" );
92 vg_linear_clear( vg_mem
.scratch
);
95 mdl_open( &msky
, "models/rs_skydome.mdl", vg_mem
.scratch
);
96 mdl_load_metadata_block( &msky
, vg_mem
.scratch
);
97 mdl_async_load_glmesh( &msky
, &world_render
.skydome
);
100 vg_info( "Loading default world textures\n" );
101 vg_tex2d_load_qoi_async_file( "textures/garbage.qoi",
102 VG_TEX2D_NEAREST
|VG_TEX2D_REPEAT
,
103 &world_render
.tex_terrain_noise
);
105 vg_async_call( async_world_render_init
, NULL
, 0 );
108 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
,
116 render_fb_bind_texture( &world
->heightmap
, 0, slot
);
117 glUniform1i( location
, slot
);
120 static void world_bind_light_array( world_instance
*world
,
121 GLuint shader
, GLuint location
,
123 glActiveTexture( GL_TEXTURE0
+ slot
);
124 glBindTexture( GL_TEXTURE_BUFFER
, world
->tex_light_entities
);
125 glUniform1i( location
, slot
);
128 static void world_bind_light_index( world_instance
*world
,
129 GLuint shader
, GLuint location
,
131 glActiveTexture( GL_TEXTURE0
+ slot
);
132 glBindTexture( GL_TEXTURE_3D
, world
->tex_light_cubes
);
133 glUniform1i( location
, slot
);
136 static void render_world_depth( world_instance
*world
, camera
*cam
);
142 static void bind_terrain_noise(void){
143 glActiveTexture( GL_TEXTURE0
);
144 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
149 enum mdl_shader shader
;
150 enum world_geo_type geo_type
;
152 void (*fn_bind_textures
)( world_instance
*world
,
153 struct world_surface
*mat
);
154 void (*fn_set_mdl
)( m4x3f mdl
);
155 void (*fn_set_uPvmPrev
)( m4x4f pvm
);
156 void (*fn_set_uNormalMtx
)( m3x3f mnorm
);
159 /* FIXME: we gotta do something about this crap, maybe batch lists. something..
160 * anything but this. */
162 void world_render_props( world_instance
*world
, u32 material_id
,
163 struct world_pass
*pass
){
164 if( !mdl_arrcount( &world
->ent_prop
) ) return;
166 /* HACK: use the first material for every prop entity */
167 ent_prop
*first
= mdl_arritm( &world
->ent_prop
, 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_prop
); j
++ ){
177 ent_prop
*prop
= mdl_arritm( &world
->ent_prop
, j
);
178 if( prop
->flags
& 0x1 ) continue;
180 for( u32 k
=0; k
<prop
->submesh_count
; k
++ ){
181 sm
= mdl_arritm( &world
->meta
.submeshs
, prop
->submesh_start
+k
);
184 mdl_transform_m4x3( &prop
->transform
, mmdl
);
187 m4x3_expand( mmdl
, m4mdl
);
188 m4x4_mul( pass
->cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
190 pass
->fn_set_mdl( mmdl
);
191 pass
->fn_set_uPvmPrev( m4mdl
);
193 mdl_draw_submesh( sm
);
199 void world_render_traffic( world_instance
*world
, u32 material_id
,
200 struct world_pass
*pass
){
201 if( !mdl_arrcount( &world
->ent_traffic
) ) return;
203 /* HACK: use the first material for every traffic entity */
204 ent_traffic
*first
= mdl_arritm( &world
->ent_traffic
, 0 );
205 if( !first
->submesh_count
) return;
207 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
, first
->submesh_start
);
208 if( sm
->material_id
!= material_id
) return;
210 struct world_surface
*mat
= &world
->surfaces
[ material_id
];
211 pass
->fn_bind_textures( world
, mat
);
213 for( u32 j
=0; j
<mdl_arrcount( &world
->ent_traffic
); j
++ ){
214 ent_traffic
*traffic
= mdl_arritm( &world
->ent_traffic
, j
);
216 for( u32 k
=0; k
<traffic
->submesh_count
; k
++ ){
217 sm
= mdl_arritm( &world
->meta
.submeshs
,
218 traffic
->submesh_start
+k
);
221 q_m3x3( traffic
->transform
.q
, mmdl
);
222 v3_copy( traffic
->transform
.co
, mmdl
[3] );
225 m4x3_expand( mmdl
, m4mdl
);
226 m4x4_mul( pass
->cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
228 pass
->fn_set_mdl( mmdl
);
229 pass
->fn_set_uPvmPrev( m4mdl
);
231 mdl_draw_submesh( sm
);
237 void world_render_pass( world_instance
*world
, struct world_pass
*pass
){
238 for( int i
=0; i
<world
->surface_count
; i
++ ){
239 struct world_surface
*mat
= &world
->surfaces
[i
];
241 if( mat
->info
.shader
== pass
->shader
){
244 if( pass
->geo_type
== k_world_geo_type_solid
){
248 world_render_traffic( world
, i
, pass
);
249 world_render_props( world
, i
, pass
);
250 sm
= &mat
->sm_no_collide
;
253 if( !sm
->indice_count
)
257 m4x3_identity( mmdl
);
258 pass
->fn_set_mdl( mmdl
);
259 pass
->fn_set_uPvmPrev( pass
->cam
->mtx_prev
.pv
);
261 pass
->fn_bind_textures( world
, mat
);
262 mdl_draw_submesh( sm
);
268 void world_render_both_stages( world_instance
*world
, struct world_pass
*pass
)
270 mesh_bind( &world
->mesh_geo
);
271 pass
->geo_type
= k_world_geo_type_solid
;
272 world_render_pass( world
, pass
);
274 glDisable( GL_CULL_FACE
);
275 mesh_bind( &world
->mesh_no_collide
);
276 pass
->geo_type
= k_world_geo_type_nonsolid
;
277 world_render_pass( world
, pass
);
278 glEnable( GL_CULL_FACE
);
281 static void bindpoint_diffuse_texture1( world_instance
*world
,
282 struct world_surface
*mat
)
285 glActiveTexture( GL_TEXTURE1
);
286 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
289 static void bindpoint_diffuse1_and_cubemap10( world_instance
*world
,
290 struct world_surface
*mat
){
291 glActiveTexture( GL_TEXTURE1
);
292 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
294 u32 cubemap_id
= mat
->info
.tex_none0
,
297 if( mdl_entity_id_type( cubemap_id
) == k_ent_cubemap
){
298 cubemap_index
= mdl_entity_id_id( cubemap_id
);
301 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, cubemap_index
);
302 glActiveTexture( GL_TEXTURE10
);
303 glBindTexture( GL_TEXTURE_CUBE_MAP
, cm
->texture_id
);
305 shader_scene_cubemapped_uColour( mat
->info
.colour
);
308 static void render_world_vb( world_instance
*world
, camera
*cam
){
309 shader_scene_vertex_blend_use();
310 shader_scene_vertex_blend_uTexGarbage(0);
311 shader_scene_vertex_blend_uTexGradients(1);
312 world_link_lighting_ub( world
, _shader_scene_vertex_blend
.id
);
313 world_bind_position_texture( world
, _shader_scene_vertex_blend
.id
,
314 _uniform_scene_vertex_blend_g_world_depth
, 2 );
315 world_bind_light_array( world
, _shader_scene_vertex_blend
.id
,
316 _uniform_scene_vertex_blend_uLightsArray
, 3 );
317 world_bind_light_index( world
, _shader_scene_vertex_blend
.id
,
318 _uniform_scene_vertex_blend_uLightsIndex
, 4 );
320 glActiveTexture( GL_TEXTURE0
);
321 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
323 shader_scene_vertex_blend_uPv( cam
->mtx
.pv
);
324 shader_scene_vertex_blend_uCamera( cam
->transform
[3] );
326 struct world_pass pass
= {
327 .shader
= k_shader_standard_vertex_blend
,
329 .fn_bind_textures
= bindpoint_diffuse_texture1
,
330 .fn_set_mdl
= shader_scene_vertex_blend_uMdl
,
331 .fn_set_uPvmPrev
= shader_scene_vertex_blend_uPvmPrev
,
334 world_render_both_stages( world
, &pass
);
337 static void world_shader_standard_bind( world_instance
*world
, camera
*cam
){
338 shader_scene_standard_use();
339 shader_scene_standard_uTexGarbage(0);
340 shader_scene_standard_uTexMain(1);
341 shader_scene_standard_uPv( cam
->mtx
.pv
);
343 world_link_lighting_ub( world
, _shader_scene_standard
.id
);
344 world_bind_position_texture( world
, _shader_scene_standard
.id
,
345 _uniform_scene_standard_g_world_depth
, 2 );
346 world_bind_light_array( world
, _shader_scene_standard
.id
,
347 _uniform_scene_standard_uLightsArray
, 3 );
348 world_bind_light_index( world
, _shader_scene_standard
.id
,
349 _uniform_scene_standard_uLightsIndex
, 4 );
351 bind_terrain_noise();
352 shader_scene_standard_uCamera( cam
->transform
[3] );
355 static void render_world_standard( world_instance
*world
, camera
*cam
){
356 world_shader_standard_bind( world
, cam
);
357 struct world_pass pass
= {
358 .shader
= k_shader_standard
,
360 .fn_bind_textures
= bindpoint_diffuse_texture1
,
361 .fn_set_mdl
= shader_scene_standard_uMdl
,
362 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
365 world_render_both_stages( world
, &pass
);
368 static void render_world_cubemapped( world_instance
*world
, camera
*cam
,
370 if( !mdl_arrcount( &world
->ent_cubemap
) )
374 world_shader_standard_bind( world
, cam
);
376 struct world_pass pass
= {
377 .shader
= k_shader_cubemap
,
379 .fn_bind_textures
= bindpoint_diffuse_texture1
,
380 .fn_set_mdl
= shader_scene_standard_uMdl
,
381 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
384 world_render_both_stages( world
, &pass
);
387 shader_scene_cubemapped_use();
388 shader_scene_cubemapped_uTexGarbage(0);
389 shader_scene_cubemapped_uTexMain(1);
390 shader_scene_cubemapped_uTexCubemap(10);
391 shader_scene_cubemapped_uPv( cam
->mtx
.pv
);
393 world_link_lighting_ub( world
, _shader_scene_cubemapped
.id
);
394 world_bind_position_texture( world
, _shader_scene_cubemapped
.id
,
395 _uniform_scene_cubemapped_g_world_depth
, 2 );
396 world_bind_light_array( world
, _shader_scene_cubemapped
.id
,
397 _uniform_scene_cubemapped_uLightsArray
, 3 );
398 world_bind_light_index( world
, _shader_scene_cubemapped
.id
,
399 _uniform_scene_cubemapped_uLightsIndex
, 4 );
401 bind_terrain_noise();
402 shader_scene_cubemapped_uCamera( cam
->transform
[3] );
404 struct world_pass pass
= {
405 .shader
= k_shader_cubemap
,
407 .fn_bind_textures
= bindpoint_diffuse1_and_cubemap10
,
408 .fn_set_mdl
= shader_scene_cubemapped_uMdl
,
409 .fn_set_uPvmPrev
= shader_scene_cubemapped_uPvmPrev
,
412 world_render_both_stages( world
, &pass
);
416 static void render_world_alphatest( world_instance
*world
, camera
*cam
){
417 shader_scene_standard_alphatest_use();
418 shader_scene_standard_alphatest_uTexGarbage(0);
419 shader_scene_standard_alphatest_uTexMain(1);
420 shader_scene_standard_alphatest_uPv( cam
->mtx
.pv
);
422 world_link_lighting_ub( world
, _shader_scene_standard_alphatest
.id
);
423 world_bind_position_texture( world
, _shader_scene_standard_alphatest
.id
,
424 _uniform_scene_standard_alphatest_g_world_depth
, 2 );
425 world_bind_light_array( world
, _shader_scene_standard_alphatest
.id
,
426 _uniform_scene_standard_alphatest_uLightsArray
, 3 );
427 world_bind_light_index( world
, _shader_scene_standard_alphatest
.id
,
428 _uniform_scene_standard_alphatest_uLightsIndex
, 4 );
431 bind_terrain_noise();
434 shader_scene_standard_alphatest_uCamera( cam
->transform
[3] );
436 glDisable(GL_CULL_FACE
);
438 struct world_pass pass
= {
439 .shader
= k_shader_standard_cutout
,
441 .fn_bind_textures
= bindpoint_diffuse_texture1
,
442 .fn_set_mdl
= shader_scene_standard_alphatest_uMdl
,
443 .fn_set_uPvmPrev
= shader_scene_standard_alphatest_uPvmPrev
,
446 world_render_both_stages( world
, &pass
);
448 glEnable(GL_CULL_FACE
);
452 void world_render_challenges( world_instance
*world
, struct world_pass
*pass
,
455 if( skaterift
.activity
== k_skaterift_replay
) return;
456 if( world
!= world_current_instance() ) return;
461 u32 objective_list
[ 32 ],
462 challenge_list
[ 16 ];
464 v2f objective_uv_offsets
[ 32 ];
466 u32 objective_count
= 0,
469 ent_challenge
*active_challenge
= NULL
;
471 if( mdl_entity_id_type( world_static
.focused_entity
) == k_ent_challenge
){
472 if( (skaterift
.activity
== k_skaterift_default
) &&
473 world_static
.challenge_target
){
477 if( !((skaterift
.activity
!= k_skaterift_ent_focus
) &&
478 !world_static
.challenge_target
) ){
479 world_instance
*challenge_world
= world_current_instance();
480 u32 index
= mdl_entity_id_id( world_static
.focused_entity
);
481 active_challenge
= mdl_arritm(&challenge_world
->ent_challenge
, index
);
485 if( active_challenge
){
486 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
487 challenge_list
[ challenge_count
++ ] = world_static
.focused_entity
;
489 u32 next
= active_challenge
->first
;
490 while( mdl_entity_id_type(next
) == k_ent_objective
){
491 u32 index
= mdl_entity_id_id( next
);
492 objective_list
[ objective_count
++ ] = index
;
494 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
495 next
= objective
->id_next
;
501 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
503 bh_iter_init_range( 0, &it
, pos
, radius
+10.0f
);
505 while( bh_next( world
->entity_bh
, &it
, &idx
) ){
506 u32 id
= world
->entity_list
[ idx
],
507 type
= mdl_entity_id_type( id
),
508 index
= mdl_entity_id_id( id
);
510 if( type
== k_ent_objective
) {
511 if( objective_count
< vg_list_size(objective_list
) )
512 objective_list
[ objective_count
++ ] = index
;
514 else if( type
== k_ent_challenge
){
515 if( challenge_count
< vg_list_size(challenge_list
) )
516 challenge_list
[ challenge_count
++ ] = index
;
521 /* render objectives */
522 glDisable( GL_CULL_FACE
);
523 mesh_bind( &world
->mesh_no_collide
);
524 u32 last_material
= 0;
525 for( u32 i
=0; i
<objective_count
; i
++ ){
526 u32 index
= objective_list
[ i
];
527 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
528 if( (objective
->flags
& k_ent_objective_hidden
) &&
529 !active_challenge
) continue;
534 u32 passed
= objective
->flags
& k_ent_objective_passed
;
535 f32 target
= passed
? 0.0f
: 1.0f
;
536 vg_slewf(&objective
->transform
.s
[0], target
, vg
.time_frame_delta
*4.0f
);
537 scale
= vg_smoothstepf( objective
->transform
.s
[0] );
539 if( (objective
== world_static
.challenge_target
) || passed
)
540 shader_scene_fxglow_uUvOffset( (v2f
){ 16.0f
/256.0f
, 0.0f
} );
542 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
545 f32 dist
= v3_dist( objective
->transform
.co
, pos
) * (1.0f
/radius
);
546 scale
= vg_smoothstepf( vg_clampf( 5.0f
-dist
*5.0f
, 0.0f
,1.0f
) );
550 q_m3x3( objective
->transform
.q
, mmdl
);
551 m3x3_scalef( mmdl
, scale
);
552 v3_copy( objective
->transform
.co
, mmdl
[3] );
553 shader_scene_fxglow_uMdl( mmdl
);
555 for( u32 j
=0; j
<objective
->submesh_count
; j
++ ){
556 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
557 objective
->submesh_start
+ j
);
559 if( sm
->material_id
!= last_material
){
560 last_material
= sm
->material_id
;
561 pass
->fn_bind_textures( world
, &world
->surfaces
[sm
->material_id
] );
563 mdl_draw_submesh( sm
);
568 font3d_bind( &gui
.font
, k_font_shader_world
, 0, world
, &skaterift
.cam
);
573 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_challenge
); i
++ ){
574 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, i
);
575 if( challenge
->status
) count
++;
579 c
+=highscore_intl( buf
+c
, count
, 3 );
581 c
+=highscore_intl( buf
+c
, mdl_arrcount(&world
->ent_challenge
), 3 );
584 f32 w
= font3d_string_width( 1, buf
);
586 m3x3_identity( mlocal
);
587 mlocal
[3][0] = -w
*0.5f
;
591 for( u32 i
=0; i
<challenge_count
; i
++ ){
592 u32 index
= challenge_list
[ i
];
593 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, index
);
595 mdl_transform_m4x3( &challenge
->transform
, mmdl
);
596 m4x3_mul( mmdl
, mlocal
, mmdl
);
598 vg_line_point( challenge
->transform
.co
, 0.25f
, VG__RED
);
600 f32 dist
= v3_dist( challenge
->transform
.co
, pos
) * (1.0f
/radius
),
601 scale
= vg_smoothstepf( vg_clampf( 10.0f
-dist
*10.0f
, 0.0f
,1.0f
) ),
604 if( challenge
->status
)
607 shader_scene_font_uOpacity( scale
);
608 shader_scene_font_uColourize( colour
);
609 font3d_simple_draw( 1, buf
, &skaterift
.cam
, mmdl
);
613 static void render_world_fxglow( world_instance
*world
, camera
*cam
){
614 shader_scene_fxglow_use();
615 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
616 shader_scene_fxglow_uTexMain(1);
617 shader_scene_fxglow_uPv( cam
->mtx
.pv
);
619 world_link_lighting_ub( world
, _shader_scene_fxglow
.id
);
620 world_bind_position_texture( world
, _shader_scene_fxglow
.id
,
621 _uniform_scene_fxglow_g_world_depth
, 2 );
622 world_bind_light_array( world
, _shader_scene_fxglow
.id
,
623 _uniform_scene_fxglow_uLightsArray
, 3 );
624 world_bind_light_index( world
, _shader_scene_fxglow
.id
,
625 _uniform_scene_fxglow_uLightsIndex
, 4 );
627 shader_scene_fxglow_uCamera( cam
->transform
[3] );
628 glDisable(GL_CULL_FACE
);
630 struct world_pass pass
= {
631 .shader
= k_shader_fxglow
,
633 .fn_bind_textures
= bindpoint_diffuse_texture1
,
634 .fn_set_mdl
= shader_scene_fxglow_uMdl
,
635 .fn_set_uPvmPrev
= shader_scene_fxglow_uPvmPrev
,
638 world_render_both_stages( world
, &pass
);
639 world_render_challenges( world
, &pass
, cam
->pos
);
641 glEnable(GL_CULL_FACE
);
644 static void bindpoint_terrain( world_instance
*world
,
645 struct world_surface
*mat
)
647 glActiveTexture( GL_TEXTURE1
);
648 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
650 shader_scene_terrain_uSandColour( mat
->info
.colour
);
651 shader_scene_terrain_uBlendOffset( mat
->info
.colour1
);
654 static void bindpoint_override( world_instance
*world
,
655 struct world_surface
*mat
){
656 if( mat
->info
.flags
& k_material_flag_collision
){
657 shader_scene_override_uAlphatest(0);
660 glActiveTexture( GL_TEXTURE1
);
661 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
662 shader_scene_override_uAlphatest(1);
666 static void render_terrain( world_instance
*world
, camera
*cam
){
667 shader_scene_terrain_use();
668 shader_scene_terrain_uTexGarbage(0);
669 shader_scene_terrain_uTexGradients(1);
671 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_terrain
);
672 glActiveTexture( GL_TEXTURE0
);
673 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
675 shader_scene_terrain_uPv( cam
->mtx
.pv
);
676 shader_scene_terrain_uCamera( cam
->transform
[3] );
678 struct world_pass pass
= {
679 .shader
= k_shader_terrain_blend
,
681 .fn_bind_textures
= bindpoint_terrain
,
682 .fn_set_mdl
= shader_scene_terrain_uMdl
,
683 .fn_set_uPvmPrev
= shader_scene_terrain_uPvmPrev
,
686 world_render_both_stages( world
, &pass
);
689 static void render_sky( world_instance
*world
, camera
*cam
){
691 * Modify matrix to remove clipping and view translation
698 m4x4_copy( cam
->mtx
.v
, v
);
699 m4x4_copy( cam
->mtx_prev
.v
, v_prev
);
701 for( int i
=0; i
<3; i
++ ){
703 v3_normalize(v_prev
[i
]);
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
, 100.0f
, 0.1f
);
711 m4x4_reset_clipping( pv_prev
, 100.0f
, 0.1f
);
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 if( world
->skybox
== k_skybox_default
){
723 shader_model_sky_use();
724 shader_model_sky_uMdl( identity_matrix
);
725 shader_model_sky_uPv( pv
);
726 shader_model_sky_uPvmPrev( pv_prev
);
727 shader_model_sky_uTexGarbage(0);
728 world_link_lighting_ub( world
, _shader_model_sky
.id
);
730 glActiveTexture( GL_TEXTURE0
);
731 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
733 else if( world
->skybox
== k_skybox_space
){
734 shader_model_sky_space_use();
736 shader_model_sky_space_uMdl( identity_matrix
);
737 shader_model_sky_space_uPv( pv
);
738 shader_model_sky_space_uPvmPrev( pv_prev
);
739 shader_model_sky_space_uTexGarbage(0);
740 world_link_lighting_ub( world
, _shader_model_sky_space
.id
);
742 glActiveTexture( GL_TEXTURE0
);
743 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
749 glDepthMask( GL_FALSE
);
750 glDisable( GL_DEPTH_TEST
);
752 mesh_bind( &world_render
.skydome
);
753 mesh_draw( &world_render
.skydome
);
755 glEnable( GL_DEPTH_TEST
);
756 glDepthMask( GL_TRUE
);
759 static void render_world_gates( world_instance
*world
, camera
*cam
){
760 float closest
= INFINITY
;
761 struct ent_gate
*gate
= NULL
;
763 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
764 ent_gate
*gi
= mdl_arritm( &world
->ent_gate
, i
);
766 if( !(gi
->flags
& (k_ent_gate_linked
|k_ent_gate_nonlocal_DELETED
|
767 k_ent_gate_locked
)) )
770 float dist
= v3_dist2( gi
->co
[0], cam
->transform
[3] );
772 vg_line_point( gi
->co
[0], 0.25f
, VG__BLUE
);
774 if( dist
< closest
){
780 world
->rendering_gate
= gate
;
782 render_gate( world
, world
, gate
, cam
);
785 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_frame_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
;
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
,
829 int stenciled
, int viewing_from_gate
,
830 int with_water
, int with_cubemaps
){
832 glClear( GL_DEPTH_BUFFER_BIT
);
833 glStencilFunc( GL_EQUAL
, 1, 0xFF );
834 glStencilMask( 0x00 );
835 glEnable( GL_CULL_FACE
);
836 glEnable( GL_STENCIL_TEST
);
839 glStencilMask( 0xFF );
840 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
841 glDisable( GL_STENCIL_TEST
);
844 render_sky( world
, cam
);
847 m4x3_identity(identity
);
848 render_world_routes( world
, world
, identity
, cam
, viewing_from_gate
, 0 );
849 render_world_standard( world
, cam
);
850 render_world_cubemapped( world
, cam
, with_cubemaps
);
852 render_world_vb( world
, cam
);
853 render_world_alphatest( world
, cam
);
854 render_terrain( world
, cam
);
856 if( !viewing_from_gate
){
857 world_entity_focus_render();
861 float min_dist
= INFINITY
;
863 if( mdl_arrcount( &world
->ent_route
) ){
864 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
865 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
866 float dist
= v3_dist2( route
->board_transform
[3], cam
->pos
);
868 if( dist
< min_dist
){
874 ent_route
*route
= mdl_arritm( &world
->ent_route
, closest
);
875 sfd_render( world
, cam
, route
->board_transform
);
879 if( !viewing_from_gate
){
881 if( mdl_entity_id_type(world_static
.focused_entity
) == k_ent_challenge
)
882 greyout
= world_static
.focus_strength
;
884 if( greyout
> 0.0f
){
885 glDrawBuffers( 1, (GLenum
[]){ GL_COLOR_ATTACHMENT0
} );
887 glDisable(GL_DEPTH_TEST
);
888 glDepthMask(GL_FALSE
);
889 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
890 glBlendEquation(GL_FUNC_ADD
);
892 shader_blitcolour_use();
893 shader_blitcolour_uColour( (v4f
){ 0.5f
, 0.5f
, 0.5f
, greyout
*0.56f
} );
897 glEnable(GL_DEPTH_TEST
);
898 glDepthMask(GL_TRUE
);
899 glDrawBuffers( 2, (GLenum
[]){ GL_COLOR_ATTACHMENT0
,
900 GL_COLOR_ATTACHMENT1
} );
903 render_world_fxglow( world
, cam
);
907 render_water_texture( world
, cam
);
908 render_fb_bind( gpipeline
.fb_main
, 1 );
912 glStencilFunc( GL_EQUAL
, 1, 0xFF );
913 glStencilMask( 0x00 );
914 glEnable( GL_CULL_FACE
);
915 glEnable( GL_STENCIL_TEST
);
919 render_water_surface( world
, cam
);
922 render_remote_players( world
, cam
);
923 ent_miniworld_render( world
, cam
);
926 glStencilMask( 0xFF );
927 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
928 glDisable( GL_STENCIL_TEST
);
933 static void render_world_override_pass( world_instance
*world
,
934 struct world_pass
*pass
,
935 m4x3f mmdl
, m3x3f mnormal
,
937 for( int i
=0; i
<world
->surface_count
; i
++ ){
938 struct world_surface
*mat
= &world
->surfaces
[i
];
940 if( mat
->info
.flags
& k_material_flag_ghosts
) continue;
943 if( pass
->geo_type
== k_world_geo_type_solid
)
946 sm
= &mat
->sm_no_collide
;
948 if( !sm
->indice_count
)
951 pass
->fn_set_mdl( mmdl
);
952 pass
->fn_set_uNormalMtx( mnormal
);
953 pass
->fn_set_uPvmPrev( mpvm_prev
);
954 pass
->fn_bind_textures( world
, mat
);
955 mdl_draw_submesh( sm
);
959 static void render_world_override( world_instance
*world
,
960 world_instance
*lighting_source
,
963 ent_spawn
*dest_spawn
){
964 struct world_pass pass
= {
966 .fn_bind_textures
= bindpoint_override
,
967 .fn_set_mdl
= shader_scene_override_uMdl
,
968 .fn_set_uPvmPrev
= shader_scene_override_uPvmPrev
,
969 .fn_set_uNormalMtx
= shader_scene_override_uNormalMtx
,
970 .shader
= k_shader_override
973 shader_scene_override_use();
974 shader_scene_override_uTexGarbage(0);
975 shader_scene_override_uTexMain(1);
976 shader_scene_override_uPv( pass
.cam
->mtx
.pv
);
978 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( lighting_source
, scene_override
);
979 bind_terrain_noise();
981 shader_scene_override_uCamera( pass
.cam
->transform
[3] );
984 m4x3_expand( mmdl
, mpvm_prev
);
985 m4x4_mul( cam
->mtx_prev
.pv
, mpvm_prev
, mpvm_prev
);
988 m3x3_inv( mmdl
, mnormal
);
989 m3x3_transpose( mnormal
, mnormal
);
990 v3_normalize( mnormal
[0] );
991 v3_normalize( mnormal
[1] );
992 v3_normalize( mnormal
[2] );
994 v4f uPlayerPos
, uSpawnPos
;
995 v4_zero( uPlayerPos
);
996 v4_zero( uSpawnPos
);
998 v3_copy( world
->player_co
, uPlayerPos
);
1000 if( dest_spawn
&& (v3_dist2(dest_spawn
->transform
.co
,uPlayerPos
) > 0.1f
) )
1001 v3_copy( dest_spawn
->transform
.co
, uSpawnPos
);
1003 v3_add( uPlayerPos
, (v3f
){0,-1,0}, uSpawnPos
);
1005 uPlayerPos
[3] = v3_dist(uPlayerPos
,uSpawnPos
);
1006 uSpawnPos
[3] = 1.0f
/uPlayerPos
[3];
1008 shader_scene_override_uPlayerPos( uPlayerPos
);
1009 shader_scene_override_uSpawnPos( uSpawnPos
);
1012 glDisable( GL_CULL_FACE
);
1013 mesh_bind( &world
->mesh_geo
);
1014 pass
.geo_type
= k_world_geo_type_solid
;
1015 render_world_override_pass( world
, &pass
, mmdl
, mnormal
, mpvm_prev
);
1016 mesh_bind( &world
->mesh_no_collide
);
1017 pass
.geo_type
= k_world_geo_type_nonsolid
;
1018 render_world_override_pass( world
, &pass
, mmdl
, mnormal
, mpvm_prev
);
1019 glEnable( GL_CULL_FACE
);
1022 static void render_cubemap_side( world_instance
*world
, ent_cubemap
*cm
,
1025 glFramebufferTexture2D( GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
1026 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ side
, cm
->texture_id
, 0 );
1027 glClear( GL_DEPTH_BUFFER_BIT
);
1030 { -1.0f
, 0.0f
, 0.0f
},
1031 { 1.0f
, 0.0f
, 0.0f
},
1032 { 0.0f
, -1.0f
, 0.0f
},
1033 { 0.0f
, 1.0f
, 0.0f
},
1034 { 0.0f
, 0.0f
, -1.0f
},
1035 { 0.0f
, 0.0f
, 1.0f
}
1038 { 0.0f
, -1.0f
, 0.0f
},
1039 { 0.0f
, -1.0f
, 0.0f
},
1040 { 0.0f
, 0.0f
, 1.0f
},
1041 { 0.0f
, 0.0f
, -1.0f
},
1042 { 0.0f
, -1.0f
, 0.0f
},
1043 { 0.0f
, -1.0f
, 0.0f
}
1046 v3_zero( cam
.angles
);
1047 v3_copy( cm
->co
, cam
.pos
);
1049 v3_copy( forward
[side
], cam
.transform
[2] );
1050 v3_copy( up
[side
], cam
.transform
[1] );
1051 v3_cross( up
[side
], forward
[side
], cam
.transform
[0] );
1052 v3_copy( cm
->co
, cam
.transform
[3] );
1053 m4x3_invert_affine( cam
.transform
, cam
.transform_inverse
);
1055 camera_update_view( &cam
);
1060 m4x4_copy( cam
.mtx
.p
, cam
.mtx_prev
.p
);
1061 m4x4_projection( cam
.mtx
.p
, cam
.fov
, 1.0f
, cam
.nearz
, cam
.farz
);
1062 camera_finalize( &cam
);
1063 camera_finalize( &cam
);
1065 render_world( world
, &cam
, 0, 1, 1, 0 );
1068 static void render_world_cubemaps( world_instance
*world
){
1069 if( world
->cubemap_cooldown
)
1070 world
->cubemap_cooldown
--;
1072 world
->cubemap_cooldown
= 60;
1074 glViewport( 0, 0, WORLD_CUBEMAP_RES
, WORLD_CUBEMAP_RES
);
1075 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_cubemap
); i
++ ){
1076 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, i
);
1077 glBindFramebuffer( GL_FRAMEBUFFER
, cm
->framebuffer_id
);
1079 world
->cubemap_side
++;
1080 if( world
->cubemap_side
>= 6 )
1081 world
->cubemap_side
= 0;
1083 render_cubemap_side( world
, cm
, world
->cubemap_side
);
1088 static void render_world_depth( world_instance
*world
, camera
*cam
){
1089 m4x3f identity_matrix
;
1090 m4x3_identity( identity_matrix
);
1092 shader_scene_depth_use();
1093 shader_scene_depth_uCamera( cam
->transform
[3] );
1094 shader_scene_depth_uPv( cam
->mtx
.pv
);
1095 shader_scene_depth_uPvmPrev( cam
->mtx_prev
.pv
);
1096 shader_scene_depth_uMdl( identity_matrix
);
1097 world_link_lighting_ub( world
, _shader_scene_depth
.id
);
1099 mesh_bind( &world
->mesh_geo
);
1100 mesh_draw( &world
->mesh_geo
);
1103 static void render_world_position( world_instance
*world
, camera
*cam
){
1104 m4x3f identity_matrix
;
1105 m4x3_identity( identity_matrix
);
1107 shader_scene_position_use();
1108 shader_scene_position_uCamera( cam
->transform
[3] );
1109 shader_scene_position_uPv( cam
->mtx
.pv
);
1110 shader_scene_position_uPvmPrev( cam
->mtx_prev
.pv
);
1111 shader_scene_position_uMdl( identity_matrix
);
1112 world_link_lighting_ub( world
, _shader_scene_position
.id
);
1114 mesh_bind( &world
->mesh_geo
);
1115 mesh_draw( &world
->mesh_geo
);