2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
9 #include "world_render.h"
12 #include "world_map.h"
13 #include "ent_miniworld.h"
14 #include "player_remote.h"
15 #include "ent_skateshop.h"
17 static int ccmd_set_time( int argc
, const char *argv
[] ){
18 world_instance
*world
= world_current_instance();
20 world
->time
= atof( argv
[0] );
22 vg_error( "Usage set_time <0-1.0> (current time: %f)\n", world
->time
);
26 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){
69 VG_VAR_F32( k_day_length
);
70 VG_VAR_I32( k_debug_light_indices
);
71 VG_VAR_I32( k_debug_light_complexity
);
72 VG_VAR_I32( k_light_preview
);
73 vg_console_reg_cmd( "set_time", ccmd_set_time
, NULL
);
75 world_render
.sky_rate
= 1.0;
76 world_render
.sky_target_rate
= 1.0;
78 shader_scene_standard_register();
79 shader_scene_standard_alphatest_register();
80 shader_scene_foliage_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
, NULL
);
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 );
109 * standard uniform bindings
110 * ----------------------------------------------------------------------------
112 static void world_link_lighting_ub( world_instance
*world
, GLuint shader
){
113 GLuint idx
= glGetUniformBlockIndex( shader
, "ub_world_lighting" );
114 glUniformBlockBinding( shader
, idx
, world
->ubo_bind_point
);
117 static void world_bind_position_texture( world_instance
*world
,
118 GLuint shader
, GLuint location
,
120 render_fb_bind_texture( &world
->heightmap
, 0, slot
);
121 glUniform1i( location
, slot
);
124 static void world_bind_light_array( world_instance
*world
,
125 GLuint shader
, GLuint location
,
127 glActiveTexture( GL_TEXTURE0
+ slot
);
128 glBindTexture( GL_TEXTURE_BUFFER
, world
->tex_light_entities
);
129 glUniform1i( location
, slot
);
132 static void world_bind_light_index( world_instance
*world
,
133 GLuint shader
, GLuint location
,
135 glActiveTexture( GL_TEXTURE0
+ slot
);
136 glBindTexture( GL_TEXTURE_3D
, world
->tex_light_cubes
);
137 glUniform1i( location
, slot
);
140 static void bind_terrain_noise(void){
141 glActiveTexture( GL_TEXTURE0
);
142 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
146 * Get OpenGL texture name from texture ID.
148 static GLuint
world_get_texture( world_instance
*world
, u32 id
){
149 if( id
& 0x80000000 ) return skaterift
.rt_textures
[id
& ~0x80000000];
150 else return world
->textures
[ id
];
153 static void bindpoint_diffuse_texture1( world_instance
*world
,
154 struct world_surface
*mat
){
155 glActiveTexture( GL_TEXTURE1
);
156 glBindTexture( GL_TEXTURE_2D
,
157 world_get_texture(world
,mat
->info
.tex_diffuse
) );
162 * ----------------------------------------------------------------------------
167 enum mdl_shader shader
;
168 enum world_geo_type geo_type
;
170 void (*fn_bind_textures
)( world_instance
*world
,
171 struct world_surface
*mat
);
172 void (*fn_set_mdl
)( m4x3f mdl
);
173 void (*fn_set_uPvmPrev
)( m4x4f pvm
);
174 void (*fn_set_uNormalMtx
)( m3x3f mnorm
);
177 static void render_world_depth( world_instance
*world
, camera
*cam
);
180 * Render a run of submeshes, only of those which match material_id
182 static void world_render_submeshes( world_instance
*world
,
183 struct world_pass
*pass
,
184 mdl_transform
*transform
,
185 u32 start
, u32 count
, u32 material_id
){
186 for( u32 k
=0; k
<count
; k
++ ){
187 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
, start
+k
);
188 if( sm
->material_id
!= material_id
) continue;
191 mdl_transform_m4x3( transform
, mmdl
);
194 m4x3_expand( mmdl
, m4mdl
);
195 m4x4_mul( pass
->cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
197 pass
->fn_set_mdl( mmdl
);
198 pass
->fn_set_uPvmPrev( m4mdl
);
200 mdl_draw_submesh( sm
);
205 * Render props attached to this material
207 static void world_render_props( world_instance
*world
, u32 material_id
,
208 struct world_pass
*pass
){
209 struct world_surface
*mat
= &world
->surfaces
[ material_id
];
210 if( !(mat
->flags
& WORLD_SURFACE_HAS_PROPS
) ) return;
212 pass
->fn_bind_textures( world
, mat
);
214 for( u32 j
=0; j
<mdl_arrcount( &world
->ent_prop
); j
++ ){
215 ent_prop
*prop
= mdl_arritm( &world
->ent_prop
, j
);
216 if( prop
->flags
& 0x1 ) continue;
218 world_render_submeshes( world
, pass
, &prop
->transform
,
219 prop
->submesh_start
, prop
->submesh_count
, material_id
);
224 * Render traffic models attactched to this material
226 static void world_render_traffic( world_instance
*world
, u32 material_id
,
227 struct world_pass
*pass
){
228 struct world_surface
*mat
= &world
->surfaces
[ material_id
];
229 if( !(mat
->flags
& WORLD_SURFACE_HAS_TRAFFIC
) ) return;
231 pass
->fn_bind_textures( world
, mat
);
233 for( u32 j
=0; j
<mdl_arrcount( &world
->ent_traffic
); j
++ ){
234 ent_traffic
*traffic
= mdl_arritm( &world
->ent_traffic
, j
);
236 world_render_submeshes( world
, pass
, &traffic
->transform
,
237 traffic
->submesh_start
, traffic
->submesh_count
,
243 * Iterate and render all materials which match the passes shader and geometry
244 * type. Includes props/traffic.
246 static void world_render_pass( world_instance
*world
, struct world_pass
*pass
){
247 for( int i
=0; i
<world
->surface_count
; i
++ ){
248 struct world_surface
*mat
= &world
->surfaces
[i
];
250 if( mat
->info
.shader
== pass
->shader
){
253 if( pass
->geo_type
== k_world_geo_type_solid
){
257 world_render_traffic( world
, i
, pass
);
258 world_render_props( world
, i
, pass
);
259 sm
= &mat
->sm_no_collide
;
262 if( !sm
->indice_count
)
266 m4x3_identity( mmdl
);
267 pass
->fn_set_mdl( mmdl
);
268 pass
->fn_set_uPvmPrev( pass
->cam
->mtx_prev
.pv
);
270 pass
->fn_bind_textures( world
, mat
);
271 mdl_draw_submesh( sm
);
277 * Specific shader instructions
278 * ----------------------------------------------------------------------------
281 static void world_render_both_stages( world_instance
*world
,
282 struct world_pass
*pass
){
283 mesh_bind( &world
->mesh_geo
);
284 pass
->geo_type
= k_world_geo_type_solid
;
285 world_render_pass( world
, pass
);
287 glDisable( GL_CULL_FACE
);
288 mesh_bind( &world
->mesh_no_collide
);
289 pass
->geo_type
= k_world_geo_type_nonsolid
;
290 world_render_pass( world
, pass
);
291 glEnable( GL_CULL_FACE
);
294 static void render_world_vb( world_instance
*world
, camera
*cam
){
295 shader_scene_vertex_blend_use();
296 shader_scene_vertex_blend_uTexGarbage(0);
297 shader_scene_vertex_blend_uTexGradients(1);
298 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_vertex_blend
);
300 glActiveTexture( GL_TEXTURE0
);
301 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
303 shader_scene_vertex_blend_uPv( cam
->mtx
.pv
);
304 shader_scene_vertex_blend_uCamera( cam
->transform
[3] );
306 struct world_pass pass
= {
307 .shader
= k_shader_standard_vertex_blend
,
309 .fn_bind_textures
= bindpoint_diffuse_texture1
,
310 .fn_set_mdl
= shader_scene_vertex_blend_uMdl
,
311 .fn_set_uPvmPrev
= shader_scene_vertex_blend_uPvmPrev
,
314 world_render_both_stages( world
, &pass
);
317 static void world_shader_standard_bind( world_instance
*world
, camera
*cam
){
318 shader_scene_standard_use();
319 shader_scene_standard_uTexGarbage(0);
320 shader_scene_standard_uTexMain(1);
321 shader_scene_standard_uPv( cam
->mtx
.pv
);
322 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_standard
);
324 bind_terrain_noise();
325 shader_scene_standard_uCamera( cam
->transform
[3] );
328 static void render_world_standard( world_instance
*world
, camera
*cam
){
329 world_shader_standard_bind( world
, cam
);
330 struct world_pass pass
= {
331 .shader
= k_shader_standard
,
333 .fn_bind_textures
= bindpoint_diffuse_texture1
,
334 .fn_set_mdl
= shader_scene_standard_uMdl
,
335 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
338 world_render_both_stages( world
, &pass
);
341 static void bindpoint_diffuse1_and_cubemap10( world_instance
*world
,
342 struct world_surface
*mat
){
343 glActiveTexture( GL_TEXTURE1
);
344 glBindTexture( GL_TEXTURE_2D
,
345 world_get_texture(world
,mat
->info
.tex_diffuse
) );
347 u32 cubemap_id
= mat
->info
.tex_none0
,
350 if( mdl_entity_id_type( cubemap_id
) == k_ent_cubemap
){
351 cubemap_index
= mdl_entity_id_id( cubemap_id
);
354 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, cubemap_index
);
355 glActiveTexture( GL_TEXTURE10
);
356 glBindTexture( GL_TEXTURE_CUBE_MAP
, cm
->texture_id
);
358 shader_scene_cubemapped_uColour( mat
->info
.colour
);
361 static void render_world_cubemapped( world_instance
*world
, camera
*cam
,
363 if( !mdl_arrcount( &world
->ent_cubemap
) )
367 world_shader_standard_bind( world
, cam
);
369 struct world_pass pass
= {
370 .shader
= k_shader_cubemap
,
372 .fn_bind_textures
= bindpoint_diffuse_texture1
,
373 .fn_set_mdl
= shader_scene_standard_uMdl
,
374 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
377 world_render_both_stages( world
, &pass
);
380 shader_scene_cubemapped_use();
381 shader_scene_cubemapped_uTexGarbage(0);
382 shader_scene_cubemapped_uTexMain(1);
383 shader_scene_cubemapped_uTexCubemap(10);
384 shader_scene_cubemapped_uPv( cam
->mtx
.pv
);
386 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_cubemapped
);
388 bind_terrain_noise();
389 shader_scene_cubemapped_uCamera( cam
->transform
[3] );
391 struct world_pass pass
= {
392 .shader
= k_shader_cubemap
,
394 .fn_bind_textures
= bindpoint_diffuse1_and_cubemap10
,
395 .fn_set_mdl
= shader_scene_cubemapped_uMdl
,
396 .fn_set_uPvmPrev
= shader_scene_cubemapped_uPvmPrev
,
399 world_render_both_stages( world
, &pass
);
403 static void render_world_alphatest( world_instance
*world
, camera
*cam
){
404 shader_scene_standard_alphatest_use();
405 shader_scene_standard_alphatest_uTexGarbage(0);
406 shader_scene_standard_alphatest_uTexMain(1);
407 shader_scene_standard_alphatest_uPv( cam
->mtx
.pv
);
409 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_standard_alphatest
);
411 bind_terrain_noise();
412 shader_scene_standard_alphatest_uCamera( cam
->transform
[3] );
413 glDisable(GL_CULL_FACE
);
415 struct world_pass pass
= {
416 .shader
= k_shader_standard_cutout
,
418 .fn_bind_textures
= bindpoint_diffuse_texture1
,
419 .fn_set_mdl
= shader_scene_standard_alphatest_uMdl
,
420 .fn_set_uPvmPrev
= shader_scene_standard_alphatest_uPvmPrev
,
423 world_render_both_stages( world
, &pass
);
424 glEnable(GL_CULL_FACE
);
427 static void render_world_foliage( world_instance
*world
, camera
*cam
){
428 shader_scene_foliage_use();
429 shader_scene_foliage_uTexGarbage(0);
430 shader_scene_foliage_uTexMain(1);
431 shader_scene_foliage_uPv( cam
->mtx
.pv
);
432 shader_scene_foliage_uTime( vg
.time
);
434 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_foliage
);
435 bind_terrain_noise();
437 shader_scene_foliage_uCamera( cam
->transform
[3] );
438 glDisable(GL_CULL_FACE
);
439 struct world_pass pass
= {
440 .shader
= k_shader_foliage
,
442 .fn_bind_textures
= bindpoint_diffuse_texture1
,
443 .fn_set_mdl
= shader_scene_foliage_uMdl
,
444 .fn_set_uPvmPrev
= shader_scene_foliage_uPvmPrev
,
446 world_render_both_stages( world
, &pass
);
447 glEnable(GL_CULL_FACE
);
450 static void world_render_challenges( world_instance
*world
,
451 struct world_pass
*pass
, v3f pos
){
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
);
570 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
++;
578 vg_strnull( &str
, buf
, sizeof(buf
) );
579 vg_strcati32( &str
, count
);
580 vg_strcatch( &str
, '/' );
581 vg_strcati32( &str
, mdl_arrcount(&world
->ent_challenge
) );
583 f32 w
= font3d_string_width( 1, buf
);
585 m3x3_identity( mlocal
);
586 mlocal
[3][0] = -w
*0.5f
;
590 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
*host_world
,
614 world_instance
*world
, camera
*cam
,
616 int generic
, int challenges
, int regions
){
617 shader_scene_fxglow_use();
618 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
619 shader_scene_fxglow_uTexMain(1);
620 shader_scene_fxglow_uPv( cam
->mtx
.pv
);
621 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_fxglow
);
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
,
635 world_render_both_stages( world
, &pass
);
638 mesh_bind( &world
->mesh_no_collide
);
640 u32 last_material
= 0;
641 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_region
); i
++ ){
642 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
643 ent_region
*region
= mdl_arritm( &world
->ent_region
, i
);
646 if( region
->flags
& k_ent_route_flag_achieve_gold
)
648 else if( region
->flags
& k_ent_route_flag_achieve_silver
)
651 shader_scene_fxglow_uUvOffset( (v2f
){ (8.0f
/256.0f
)*offset
, 0.0f
} );
654 mdl_transform_m4x3( ®ion
->transform
, mmdl
);
655 m4x3_mul( world_mmdl
, mmdl
, mmdl
);
656 shader_scene_fxglow_uMdl( mmdl
);
658 for( u32 j
=0; j
<region
->submesh_count
; j
++ ){
659 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
660 region
->submesh_start
+ j
);
662 if( sm
->material_id
!= last_material
){
663 last_material
= sm
->material_id
;
664 pass
.fn_bind_textures(world
,&world
->surfaces
[sm
->material_id
]);
666 mdl_draw_submesh( sm
);
672 world_render_challenges( world
, &pass
, cam
->pos
);
674 glEnable(GL_CULL_FACE
);
677 static void bindpoint_terrain( world_instance
*world
,
678 struct world_surface
*mat
)
680 glActiveTexture( GL_TEXTURE1
);
681 glBindTexture( GL_TEXTURE_2D
,
682 world_get_texture(world
,mat
->info
.tex_diffuse
) );
684 shader_scene_terrain_uSandColour( mat
->info
.colour
);
685 shader_scene_terrain_uBlendOffset( mat
->info
.colour1
);
688 static void bindpoint_override( world_instance
*world
,
689 struct world_surface
*mat
){
690 if( mat
->info
.flags
& k_material_flag_collision
){
691 shader_scene_override_uAlphatest(0);
694 glActiveTexture( GL_TEXTURE1
);
695 glBindTexture( GL_TEXTURE_2D
,
696 world_get_texture(world
,mat
->info
.tex_diffuse
) );
697 shader_scene_override_uAlphatest(1);
701 static void render_terrain( world_instance
*world
, camera
*cam
){
702 shader_scene_terrain_use();
703 shader_scene_terrain_uTexGarbage(0);
704 shader_scene_terrain_uTexGradients(1);
706 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_terrain
);
707 glActiveTexture( GL_TEXTURE0
);
708 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
710 shader_scene_terrain_uPv( cam
->mtx
.pv
);
711 shader_scene_terrain_uCamera( cam
->transform
[3] );
713 struct world_pass pass
= {
714 .shader
= k_shader_terrain_blend
,
716 .fn_bind_textures
= bindpoint_terrain
,
717 .fn_set_mdl
= shader_scene_terrain_uMdl
,
718 .fn_set_uPvmPrev
= shader_scene_terrain_uPvmPrev
,
721 world_render_both_stages( world
, &pass
);
724 static void render_sky( world_instance
*world
, camera
*cam
){
726 * Modify matrix to remove clipping and view translation
733 m4x4_copy( cam
->mtx
.v
, v
);
734 m4x4_copy( cam
->mtx_prev
.v
, v_prev
);
736 for( int i
=0; i
<3; i
++ ){
738 v3_normalize(v_prev
[i
]);
741 v3_zero( v_prev
[3] );
743 m4x4_copy( cam
->mtx
.p
, pv
);
744 m4x4_copy( cam
->mtx_prev
.p
, pv_prev
);
745 m4x4_reset_clipping( pv
, 100.0f
, 0.1f
);
746 m4x4_reset_clipping( pv_prev
, 100.0f
, 0.1f
);
748 m4x4_mul( pv
, v
, pv
);
749 m4x4_mul( pv_prev
, v_prev
, pv_prev
);
751 m4x3f identity_matrix
;
752 m4x3_identity( identity_matrix
);
757 if( world
->skybox
== k_skybox_default
){
758 shader_model_sky_use();
759 shader_model_sky_uMdl( identity_matrix
);
760 shader_model_sky_uPv( pv
);
761 shader_model_sky_uPvmPrev( pv_prev
);
762 shader_model_sky_uTexGarbage(0);
763 world_link_lighting_ub( world
, _shader_model_sky
.id
);
765 glActiveTexture( GL_TEXTURE0
);
766 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
768 else if( world
->skybox
== k_skybox_space
){
769 shader_model_sky_space_use();
771 shader_model_sky_space_uMdl( identity_matrix
);
772 shader_model_sky_space_uPv( pv
);
773 shader_model_sky_space_uPvmPrev( pv_prev
);
774 shader_model_sky_space_uTexGarbage(0);
775 world_link_lighting_ub( world
, _shader_model_sky_space
.id
);
777 glActiveTexture( GL_TEXTURE0
);
778 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
784 glDepthMask( GL_FALSE
);
785 glDisable( GL_DEPTH_TEST
);
787 mesh_bind( &world_render
.skydome
);
788 mesh_draw( &world_render
.skydome
);
790 glEnable( GL_DEPTH_TEST
);
791 glDepthMask( GL_TRUE
);
794 static void render_world_gates( world_instance
*world
, camera
*cam
){
795 float closest
= INFINITY
;
796 struct ent_gate
*gate
= NULL
;
798 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
799 ent_gate
*gi
= mdl_arritm( &world
->ent_gate
, i
);
801 if( !(gi
->flags
& k_ent_gate_nonlocal
) )
802 if( !(gi
->flags
& k_ent_gate_linked
) )
805 float dist
= v3_dist2( gi
->co
[0], cam
->transform
[3] );
807 vg_line_point( gi
->co
[0], 0.25f
, VG__BLUE
);
809 if( dist
< closest
){
815 world
->rendering_gate
= gate
;
818 if( gate
->flags
& k_ent_gate_locked
){
819 world
->rendering_gate
= NULL
;
823 if( gate
->flags
& k_ent_gate_nonlocal
){
824 if( !(gate
->flags
& k_ent_gate_linked
) ||
825 (world_static
.load_state
!= k_world_loader_none
) ){
826 world
->rendering_gate
= NULL
;
827 render_gate_unlinked( world
, gate
, cam
);
831 world_instance
*dest_world
= &world_static
.instances
[ gate
->target
];
832 render_gate( world
, dest_world
, gate
, cam
);
835 render_gate( world
, world
, gate
, cam
);
839 static void world_prerender( world_instance
*world
){
840 if( mdl_arrcount( &world
->ent_light
) ){
841 f32 rate
= vg_maxf(0.1f
, fabsf(k_day_length
)) * vg_signf(k_day_length
);
842 world
->time
+= vg
.time_frame_delta
* (1.0/(rate
*60.0));
848 if( world
->info
.flags
& 0x1 ){
849 world
->time
= world
->info
.timezone
;
852 struct ub_world_lighting
*state
= &world
->ub_lighting
;
854 state
->g_time
= world
->time
;
855 state
->g_realtime
= vg
.time_real
;
856 state
->g_debug_indices
= k_debug_light_indices
;
857 state
->g_light_preview
= k_light_preview
;
858 state
->g_debug_complexity
= k_debug_light_complexity
;
859 state
->g_time_of_day
= vg_fractf( world
->time
);
861 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
862 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
864 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
865 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
867 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
868 state
->g_sun_dir
[0] = sinf( a
);
869 state
->g_sun_dir
[1] = cosf( a
);
870 state
->g_sun_dir
[2] = 0.2f
;
871 v3_normalize( state
->g_sun_dir
);
873 world
->probabilities
[ k_probability_curve_constant
] = 1.0f
;
874 float dp
= state
->g_day_phase
;
876 world
->probabilities
[ k_probability_curve_wildlife_day
] =
877 (dp
*dp
*0.8f
+state
->g_sunset_phase
)*0.8f
;
878 world
->probabilities
[ k_probability_curve_wildlife_night
] =
879 1.0f
-powf(fabsf((state
->g_time_of_day
-0.5f
)*5.0f
),5.0f
);
881 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
882 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
883 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
886 static void render_world( world_instance
*world
, camera
*cam
,
887 int stenciled
, int viewing_from_gate
,
888 int with_water
, int with_cubemaps
){
890 glClear( GL_DEPTH_BUFFER_BIT
);
891 glStencilFunc( GL_EQUAL
, 1, 0xFF );
892 glStencilMask( 0x00 );
893 glEnable( GL_CULL_FACE
);
894 glEnable( GL_STENCIL_TEST
);
897 glStencilMask( 0xFF );
898 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
899 glDisable( GL_STENCIL_TEST
);
902 render_sky( world
, cam
);
905 m4x3_identity(identity
);
906 render_world_routes( world
, world
, identity
, cam
, viewing_from_gate
, 0 );
907 render_world_standard( world
, cam
);
908 render_world_cubemapped( world
, cam
, with_cubemaps
);
910 render_world_vb( world
, cam
);
911 render_world_alphatest( world
, cam
);
912 render_world_foliage( world
, cam
);
913 render_terrain( world
, cam
);
915 if( !viewing_from_gate
){
916 world_entity_focus_render();
920 float min_dist
= INFINITY
;
922 if( mdl_arrcount( &world
->ent_route
) ){
923 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
924 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
925 float dist
= v3_dist2( route
->board_transform
[3], cam
->pos
);
927 if( dist
< min_dist
){
933 ent_route
*route
= mdl_arritm( &world
->ent_route
, closest
);
934 sfd_render( world
, cam
, route
->board_transform
);
938 if( !viewing_from_gate
){
940 if( mdl_entity_id_type(world_static
.focused_entity
) == k_ent_challenge
)
941 greyout
= world_static
.focus_strength
;
943 if( greyout
> 0.0f
){
944 glDrawBuffers( 1, (GLenum
[]){ GL_COLOR_ATTACHMENT0
} );
946 glDisable(GL_DEPTH_TEST
);
947 glDepthMask(GL_FALSE
);
948 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
949 glBlendEquation(GL_FUNC_ADD
);
951 shader_blitcolour_use();
952 shader_blitcolour_uColour( (v4f
){ 0.5f
, 0.5f
, 0.5f
, greyout
*0.56f
} );
956 glEnable(GL_DEPTH_TEST
);
957 glDepthMask(GL_TRUE
);
958 glDrawBuffers( 2, (GLenum
[]){ GL_COLOR_ATTACHMENT0
,
959 GL_COLOR_ATTACHMENT1
} );
962 render_world_fxglow( world
, world
, cam
, NULL
, 1, 1, 0 );
966 render_water_texture( world
, cam
);
967 render_fb_bind( gpipeline
.fb_main
, 1 );
971 glStencilFunc( GL_EQUAL
, 1, 0xFF );
972 glStencilMask( 0x00 );
973 glEnable( GL_CULL_FACE
);
974 glEnable( GL_STENCIL_TEST
);
978 render_water_surface( world
, cam
);
981 render_remote_players( world
, cam
);
982 ent_miniworld_render( world
, cam
);
985 glStencilMask( 0xFF );
986 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
987 glDisable( GL_STENCIL_TEST
);
992 static void render_world_override_pass( world_instance
*world
,
993 struct world_pass
*pass
,
994 m4x3f mmdl
, m3x3f mnormal
,
996 for( int i
=0; i
<world
->surface_count
; i
++ ){
997 struct world_surface
*mat
= &world
->surfaces
[i
];
998 if( mat
->info
.flags
& k_material_flag_ghosts
) continue;
1001 if( pass
->geo_type
== k_world_geo_type_solid
)
1004 sm
= &mat
->sm_no_collide
;
1006 if( !sm
->indice_count
)
1009 pass
->fn_set_mdl( mmdl
);
1010 pass
->fn_set_uNormalMtx( mnormal
);
1011 pass
->fn_set_uPvmPrev( mpvm_prev
);
1012 pass
->fn_bind_textures( world
, mat
);
1013 mdl_draw_submesh( sm
);
1017 static void render_world_override( world_instance
*world
,
1018 world_instance
*lighting_source
,
1021 ent_spawn
*dest_spawn
, v4f map_info
){
1022 struct world_pass pass
= {
1024 .fn_bind_textures
= bindpoint_override
,
1025 .fn_set_mdl
= shader_scene_override_uMdl
,
1026 .fn_set_uPvmPrev
= shader_scene_override_uPvmPrev
,
1027 .fn_set_uNormalMtx
= shader_scene_override_uNormalMtx
,
1028 .shader
= k_shader_override
1031 shader_scene_override_use();
1032 shader_scene_override_uTexGarbage(0);
1033 shader_scene_override_uTexMain(1);
1034 shader_scene_override_uPv( pass
.cam
->mtx
.pv
);
1035 shader_scene_override_uMapInfo( map_info
);
1037 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( lighting_source
, scene_override
);
1038 bind_terrain_noise();
1040 shader_scene_override_uCamera( pass
.cam
->transform
[3] );
1043 m4x3_expand( mmdl
, mpvm_prev
);
1044 m4x4_mul( cam
->mtx_prev
.pv
, mpvm_prev
, mpvm_prev
);
1047 m3x3_inv( mmdl
, mnormal
);
1048 m3x3_transpose( mnormal
, mnormal
);
1049 v3_normalize( mnormal
[0] );
1050 v3_normalize( mnormal
[1] );
1051 v3_normalize( mnormal
[2] );
1053 v4f uPlayerPos
, uSpawnPos
;
1054 v4_zero( uPlayerPos
);
1055 v4_zero( uSpawnPos
);
1057 v3_copy( world
->player_co
, uPlayerPos
);
1059 if( dest_spawn
&& (v3_dist2(dest_spawn
->transform
.co
,uPlayerPos
) > 0.1f
) )
1060 v3_copy( dest_spawn
->transform
.co
, uSpawnPos
);
1062 v3_add( uPlayerPos
, (v3f
){0,-1,0}, uSpawnPos
);
1064 uPlayerPos
[3] = v3_dist(uPlayerPos
,uSpawnPos
);
1065 uSpawnPos
[3] = 1.0f
/uPlayerPos
[3];
1067 shader_scene_override_uPlayerPos( uPlayerPos
);
1068 shader_scene_override_uSpawnPos( uSpawnPos
);
1071 glDisable( GL_CULL_FACE
);
1072 mesh_bind( &world
->mesh_geo
);
1073 pass
.geo_type
= k_world_geo_type_solid
;
1074 render_world_override_pass( world
, &pass
, mmdl
, mnormal
, mpvm_prev
);
1075 mesh_bind( &world
->mesh_no_collide
);
1076 pass
.geo_type
= k_world_geo_type_nonsolid
;
1077 render_world_override_pass( world
, &pass
, mmdl
, mnormal
, mpvm_prev
);
1078 glEnable( GL_CULL_FACE
);
1080 render_world_fxglow( world
, world
, cam
, mmdl
, 0, 0, 1 );
1083 static void render_cubemap_side( world_instance
*world
, ent_cubemap
*cm
,
1086 glFramebufferTexture2D( GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
1087 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ side
, cm
->texture_id
, 0 );
1088 glClear( GL_DEPTH_BUFFER_BIT
);
1091 { -1.0f
, 0.0f
, 0.0f
},
1092 { 1.0f
, 0.0f
, 0.0f
},
1093 { 0.0f
, -1.0f
, 0.0f
},
1094 { 0.0f
, 1.0f
, 0.0f
},
1095 { 0.0f
, 0.0f
, -1.0f
},
1096 { 0.0f
, 0.0f
, 1.0f
}
1099 { 0.0f
, -1.0f
, 0.0f
},
1100 { 0.0f
, -1.0f
, 0.0f
},
1101 { 0.0f
, 0.0f
, 1.0f
},
1102 { 0.0f
, 0.0f
, -1.0f
},
1103 { 0.0f
, -1.0f
, 0.0f
},
1104 { 0.0f
, -1.0f
, 0.0f
}
1107 v3_zero( cam
.angles
);
1108 v3_copy( cm
->co
, cam
.pos
);
1110 v3_copy( forward
[side
], cam
.transform
[2] );
1111 v3_copy( up
[side
], cam
.transform
[1] );
1112 v3_cross( up
[side
], forward
[side
], cam
.transform
[0] );
1113 v3_copy( cm
->co
, cam
.transform
[3] );
1114 m4x3_invert_affine( cam
.transform
, cam
.transform_inverse
);
1116 camera_update_view( &cam
);
1121 m4x4_copy( cam
.mtx
.p
, cam
.mtx_prev
.p
);
1122 m4x4_projection( cam
.mtx
.p
, cam
.fov
, 1.0f
, cam
.nearz
, cam
.farz
);
1123 camera_finalize( &cam
);
1124 camera_finalize( &cam
);
1126 render_world( world
, &cam
, 0, 1, 1, 0 );
1129 static void render_world_cubemaps( world_instance
*world
){
1130 if( world
->cubemap_cooldown
)
1131 world
->cubemap_cooldown
--;
1133 world
->cubemap_cooldown
= 60;
1135 glViewport( 0, 0, WORLD_CUBEMAP_RES
, WORLD_CUBEMAP_RES
);
1136 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_cubemap
); i
++ ){
1137 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, i
);
1138 glBindFramebuffer( GL_FRAMEBUFFER
, cm
->framebuffer_id
);
1140 world
->cubemap_side
++;
1141 if( world
->cubemap_side
>= 6 )
1142 world
->cubemap_side
= 0;
1144 render_cubemap_side( world
, cm
, world
->cubemap_side
);
1151 * ---------------------------------------------
1154 static void render_world_depth( world_instance
*world
, camera
*cam
){
1155 m4x3f identity_matrix
;
1156 m4x3_identity( identity_matrix
);
1158 shader_scene_depth_use();
1159 shader_scene_depth_uCamera( cam
->transform
[3] );
1160 shader_scene_depth_uPv( cam
->mtx
.pv
);
1161 shader_scene_depth_uPvmPrev( cam
->mtx_prev
.pv
);
1162 shader_scene_depth_uMdl( identity_matrix
);
1163 world_link_lighting_ub( world
, _shader_scene_depth
.id
);
1165 mesh_bind( &world
->mesh_geo
);
1166 mesh_draw( &world
->mesh_geo
);
1169 static void render_world_position( world_instance
*world
, camera
*cam
){
1170 m4x3f identity_matrix
;
1171 m4x3_identity( identity_matrix
);
1173 shader_scene_position_use();
1174 shader_scene_position_uCamera( cam
->transform
[3] );
1175 shader_scene_position_uPv( cam
->mtx
.pv
);
1176 shader_scene_position_uPvmPrev( cam
->mtx_prev
.pv
);
1177 shader_scene_position_uMdl( identity_matrix
);
1178 world_link_lighting_ub( world
, _shader_scene_position
.id
);
1180 mesh_bind( &world
->mesh_geo
);
1181 mesh_draw( &world
->mesh_geo
);