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"
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
)
28 vg_info( "Allocate uniform buffers\n" );
29 for( int i
=0; i
<k_world_max
; i
++ ){
30 world_instance
*world
= &world_static
.instances
[i
];
31 world
->ubo_bind_point
= i
;
33 glGenBuffers( 1, &world
->ubo_lighting
);
34 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
35 glBufferData( GL_UNIFORM_BUFFER
, sizeof(struct ub_world_lighting
),
36 NULL
, GL_DYNAMIC_DRAW
);
38 glBindBufferBase( GL_UNIFORM_BUFFER
, i
, world
->ubo_lighting
);
42 vg_info( "Allocate frame buffers\n" );
43 for( int i
=0; i
<k_world_max
; i
++ ){
44 world_instance
*world
= &world_static
.instances
[i
];
45 struct framebuffer
*fb
= &world
->heightmap
;
47 fb
->display_name
= NULL
;
51 fb
->resolution_div
= 0;
53 fb
->attachments
[0].display_name
= NULL
;
54 fb
->attachments
[0].purpose
= k_framebuffer_attachment_type_texture
;
55 fb
->attachments
[0].internalformat
= GL_RG16F
;
56 fb
->attachments
[0].format
= GL_RG
;
57 fb
->attachments
[0].type
= GL_FLOAT
;
58 fb
->attachments
[0].attachment
= GL_COLOR_ATTACHMENT0
;
60 fb
->attachments
[1].purpose
= k_framebuffer_attachment_type_none
;
61 fb
->attachments
[2].purpose
= k_framebuffer_attachment_type_none
;
62 fb
->attachments
[3].purpose
= k_framebuffer_attachment_type_none
;
63 fb
->attachments
[4].purpose
= k_framebuffer_attachment_type_none
;
65 render_fb_allocate( fb
);
69 static void world_render_init(void)
71 VG_VAR_F32( k_day_length
);
72 VG_VAR_I32( k_debug_light_indices
);
73 VG_VAR_I32( k_debug_light_complexity
);
74 VG_VAR_I32( k_light_preview
);
75 vg_console_reg_cmd( "set_time", ccmd_set_time
, NULL
);
77 world_render
.sky_rate
= 1.0;
78 world_render
.sky_target_rate
= 1.0;
80 shader_scene_standard_register();
81 shader_scene_standard_alphatest_register();
82 shader_scene_override_register();
83 shader_scene_cubemapped_register();
84 shader_scene_fxglow_register();
85 shader_scene_vertex_blend_register();
86 shader_scene_terrain_register();
87 shader_scene_depth_register();
88 shader_scene_position_register();
89 shader_model_sky_register();
90 shader_model_sky_space_register();
92 vg_info( "Loading world resources\n" );
93 vg_linear_clear( vg_mem
.scratch
);
96 mdl_open( &msky
, "models/rs_skydome.mdl", vg_mem
.scratch
);
97 mdl_load_metadata_block( &msky
, vg_mem
.scratch
);
98 mdl_async_load_glmesh( &msky
, &world_render
.skydome
, NULL
);
101 vg_info( "Loading default world textures\n" );
102 vg_tex2d_load_qoi_async_file( "textures/garbage.qoi",
103 VG_TEX2D_NEAREST
|VG_TEX2D_REPEAT
,
104 &world_render
.tex_terrain_noise
);
106 vg_async_call( async_world_render_init
, NULL
, 0 );
109 static void world_link_lighting_ub( world_instance
*world
, GLuint shader
){
110 GLuint idx
= glGetUniformBlockIndex( shader
, "ub_world_lighting" );
111 glUniformBlockBinding( shader
, idx
, world
->ubo_bind_point
);
114 static void world_bind_position_texture( world_instance
*world
,
115 GLuint shader
, GLuint location
,
117 render_fb_bind_texture( &world
->heightmap
, 0, slot
);
118 glUniform1i( location
, slot
);
121 static void world_bind_light_array( world_instance
*world
,
122 GLuint shader
, GLuint location
,
124 glActiveTexture( GL_TEXTURE0
+ slot
);
125 glBindTexture( GL_TEXTURE_BUFFER
, world
->tex_light_entities
);
126 glUniform1i( location
, slot
);
129 static void world_bind_light_index( world_instance
*world
,
130 GLuint shader
, GLuint location
,
132 glActiveTexture( GL_TEXTURE0
+ slot
);
133 glBindTexture( GL_TEXTURE_3D
, world
->tex_light_cubes
);
134 glUniform1i( location
, slot
);
137 static void render_world_depth( world_instance
*world
, camera
*cam
);
143 static void bind_terrain_noise(void){
144 glActiveTexture( GL_TEXTURE0
);
145 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
150 enum mdl_shader shader
;
151 enum world_geo_type geo_type
;
153 void (*fn_bind_textures
)( world_instance
*world
,
154 struct world_surface
*mat
);
155 void (*fn_set_mdl
)( m4x3f mdl
);
156 void (*fn_set_uPvmPrev
)( m4x4f pvm
);
157 void (*fn_set_uNormalMtx
)( m3x3f mnorm
);
160 /* FIXME: we gotta do something about this crap, maybe batch lists. something..
161 * anything but this. */
163 void world_render_props( world_instance
*world
, u32 material_id
,
164 struct world_pass
*pass
){
165 if( !mdl_arrcount( &world
->ent_prop
) ) return;
168 struct world_surface
*mat
= &world
->surfaces
[ material_id
];
169 pass
->fn_bind_textures( world
, mat
);
171 for( u32 j
=0; j
<mdl_arrcount( &world
->ent_prop
); j
++ ){
172 ent_prop
*prop
= mdl_arritm( &world
->ent_prop
, j
);
173 if( prop
->flags
& 0x1 ) continue;
175 for( u32 k
=0; k
<prop
->submesh_count
; k
++ ){
177 mdl_arritm( &world
->meta
.submeshs
, prop
->submesh_start
+k
);
179 if( sm
->material_id
!= material_id
) continue;
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 GLuint
world_get_texture( world_instance
*world
, u32 id
){
280 if( id
& 0x80000000 )
281 return skaterift
.rt_textures
[id
& ~0x80000000];
283 return world
->textures
[ id
];
286 static void bindpoint_diffuse_texture1( world_instance
*world
,
287 struct world_surface
*mat
){
288 glActiveTexture( GL_TEXTURE1
);
289 glBindTexture( GL_TEXTURE_2D
,
290 world_get_texture(world
,mat
->info
.tex_diffuse
) );
293 static void bindpoint_diffuse1_and_cubemap10( world_instance
*world
,
294 struct world_surface
*mat
){
295 glActiveTexture( GL_TEXTURE1
);
296 glBindTexture( GL_TEXTURE_2D
,
297 world_get_texture(world
,mat
->info
.tex_diffuse
) );
299 u32 cubemap_id
= mat
->info
.tex_none0
,
302 if( mdl_entity_id_type( cubemap_id
) == k_ent_cubemap
){
303 cubemap_index
= mdl_entity_id_id( cubemap_id
);
306 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, cubemap_index
);
307 glActiveTexture( GL_TEXTURE10
);
308 glBindTexture( GL_TEXTURE_CUBE_MAP
, cm
->texture_id
);
310 shader_scene_cubemapped_uColour( mat
->info
.colour
);
313 static void render_world_vb( world_instance
*world
, camera
*cam
){
314 shader_scene_vertex_blend_use();
315 shader_scene_vertex_blend_uTexGarbage(0);
316 shader_scene_vertex_blend_uTexGradients(1);
317 world_link_lighting_ub( world
, _shader_scene_vertex_blend
.id
);
318 world_bind_position_texture( world
, _shader_scene_vertex_blend
.id
,
319 _uniform_scene_vertex_blend_g_world_depth
, 2 );
320 world_bind_light_array( world
, _shader_scene_vertex_blend
.id
,
321 _uniform_scene_vertex_blend_uLightsArray
, 3 );
322 world_bind_light_index( world
, _shader_scene_vertex_blend
.id
,
323 _uniform_scene_vertex_blend_uLightsIndex
, 4 );
325 glActiveTexture( GL_TEXTURE0
);
326 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
328 shader_scene_vertex_blend_uPv( cam
->mtx
.pv
);
329 shader_scene_vertex_blend_uCamera( cam
->transform
[3] );
331 struct world_pass pass
= {
332 .shader
= k_shader_standard_vertex_blend
,
334 .fn_bind_textures
= bindpoint_diffuse_texture1
,
335 .fn_set_mdl
= shader_scene_vertex_blend_uMdl
,
336 .fn_set_uPvmPrev
= shader_scene_vertex_blend_uPvmPrev
,
339 world_render_both_stages( world
, &pass
);
342 static void world_shader_standard_bind( world_instance
*world
, camera
*cam
){
343 shader_scene_standard_use();
344 shader_scene_standard_uTexGarbage(0);
345 shader_scene_standard_uTexMain(1);
346 shader_scene_standard_uPv( cam
->mtx
.pv
);
348 world_link_lighting_ub( world
, _shader_scene_standard
.id
);
349 world_bind_position_texture( world
, _shader_scene_standard
.id
,
350 _uniform_scene_standard_g_world_depth
, 2 );
351 world_bind_light_array( world
, _shader_scene_standard
.id
,
352 _uniform_scene_standard_uLightsArray
, 3 );
353 world_bind_light_index( world
, _shader_scene_standard
.id
,
354 _uniform_scene_standard_uLightsIndex
, 4 );
356 bind_terrain_noise();
357 shader_scene_standard_uCamera( cam
->transform
[3] );
360 static void render_world_standard( world_instance
*world
, camera
*cam
){
361 world_shader_standard_bind( world
, cam
);
362 struct world_pass pass
= {
363 .shader
= k_shader_standard
,
365 .fn_bind_textures
= bindpoint_diffuse_texture1
,
366 .fn_set_mdl
= shader_scene_standard_uMdl
,
367 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
370 world_render_both_stages( world
, &pass
);
373 static void render_world_cubemapped( world_instance
*world
, camera
*cam
,
375 if( !mdl_arrcount( &world
->ent_cubemap
) )
379 world_shader_standard_bind( world
, cam
);
381 struct world_pass pass
= {
382 .shader
= k_shader_cubemap
,
384 .fn_bind_textures
= bindpoint_diffuse_texture1
,
385 .fn_set_mdl
= shader_scene_standard_uMdl
,
386 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
389 world_render_both_stages( world
, &pass
);
392 shader_scene_cubemapped_use();
393 shader_scene_cubemapped_uTexGarbage(0);
394 shader_scene_cubemapped_uTexMain(1);
395 shader_scene_cubemapped_uTexCubemap(10);
396 shader_scene_cubemapped_uPv( cam
->mtx
.pv
);
398 world_link_lighting_ub( world
, _shader_scene_cubemapped
.id
);
399 world_bind_position_texture( world
, _shader_scene_cubemapped
.id
,
400 _uniform_scene_cubemapped_g_world_depth
, 2 );
401 world_bind_light_array( world
, _shader_scene_cubemapped
.id
,
402 _uniform_scene_cubemapped_uLightsArray
, 3 );
403 world_bind_light_index( world
, _shader_scene_cubemapped
.id
,
404 _uniform_scene_cubemapped_uLightsIndex
, 4 );
406 bind_terrain_noise();
407 shader_scene_cubemapped_uCamera( cam
->transform
[3] );
409 struct world_pass pass
= {
410 .shader
= k_shader_cubemap
,
412 .fn_bind_textures
= bindpoint_diffuse1_and_cubemap10
,
413 .fn_set_mdl
= shader_scene_cubemapped_uMdl
,
414 .fn_set_uPvmPrev
= shader_scene_cubemapped_uPvmPrev
,
417 world_render_both_stages( world
, &pass
);
421 static void render_world_alphatest( world_instance
*world
, camera
*cam
){
422 shader_scene_standard_alphatest_use();
423 shader_scene_standard_alphatest_uTexGarbage(0);
424 shader_scene_standard_alphatest_uTexMain(1);
425 shader_scene_standard_alphatest_uPv( cam
->mtx
.pv
);
427 world_link_lighting_ub( world
, _shader_scene_standard_alphatest
.id
);
428 world_bind_position_texture( world
, _shader_scene_standard_alphatest
.id
,
429 _uniform_scene_standard_alphatest_g_world_depth
, 2 );
430 world_bind_light_array( world
, _shader_scene_standard_alphatest
.id
,
431 _uniform_scene_standard_alphatest_uLightsArray
, 3 );
432 world_bind_light_index( world
, _shader_scene_standard_alphatest
.id
,
433 _uniform_scene_standard_alphatest_uLightsIndex
, 4 );
436 bind_terrain_noise();
439 shader_scene_standard_alphatest_uCamera( cam
->transform
[3] );
441 glDisable(GL_CULL_FACE
);
443 struct world_pass pass
= {
444 .shader
= k_shader_standard_cutout
,
446 .fn_bind_textures
= bindpoint_diffuse_texture1
,
447 .fn_set_mdl
= shader_scene_standard_alphatest_uMdl
,
448 .fn_set_uPvmPrev
= shader_scene_standard_alphatest_uPvmPrev
,
451 world_render_both_stages( world
, &pass
);
453 glEnable(GL_CULL_FACE
);
456 static void world_render_challenges( world_instance
*world
,
457 struct world_pass
*pass
, v3f pos
){
459 if( skaterift
.activity
== k_skaterift_replay
) return;
460 if( world
!= world_current_instance() ) return;
465 u32 objective_list
[ 32 ],
466 challenge_list
[ 16 ];
468 v2f objective_uv_offsets
[ 32 ];
470 u32 objective_count
= 0,
473 ent_challenge
*active_challenge
= NULL
;
475 if( mdl_entity_id_type( world_static
.focused_entity
) == k_ent_challenge
){
476 if( (skaterift
.activity
== k_skaterift_default
) &&
477 world_static
.challenge_target
){
481 if( !((skaterift
.activity
!= k_skaterift_ent_focus
) &&
482 !world_static
.challenge_target
) ){
483 world_instance
*challenge_world
= world_current_instance();
484 u32 index
= mdl_entity_id_id( world_static
.focused_entity
);
485 active_challenge
= mdl_arritm(&challenge_world
->ent_challenge
, index
);
489 if( active_challenge
){
490 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
491 challenge_list
[ challenge_count
++ ] = world_static
.focused_entity
;
493 u32 next
= active_challenge
->first
;
494 while( mdl_entity_id_type(next
) == k_ent_objective
){
495 u32 index
= mdl_entity_id_id( next
);
496 objective_list
[ objective_count
++ ] = index
;
498 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
499 next
= objective
->id_next
;
505 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
507 bh_iter_init_range( 0, &it
, pos
, radius
+10.0f
);
509 while( bh_next( world
->entity_bh
, &it
, &idx
) ){
510 u32 id
= world
->entity_list
[ idx
],
511 type
= mdl_entity_id_type( id
),
512 index
= mdl_entity_id_id( id
);
514 if( type
== k_ent_objective
) {
515 if( objective_count
< vg_list_size(objective_list
) )
516 objective_list
[ objective_count
++ ] = index
;
518 else if( type
== k_ent_challenge
){
519 if( challenge_count
< vg_list_size(challenge_list
) )
520 challenge_list
[ challenge_count
++ ] = index
;
525 /* render objectives */
526 glDisable( GL_CULL_FACE
);
527 mesh_bind( &world
->mesh_no_collide
);
528 u32 last_material
= 0;
529 for( u32 i
=0; i
<objective_count
; i
++ ){
530 u32 index
= objective_list
[ i
];
531 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
532 if( (objective
->flags
& k_ent_objective_hidden
) &&
533 !active_challenge
) continue;
538 u32 passed
= objective
->flags
& k_ent_objective_passed
;
539 f32 target
= passed
? 0.0f
: 1.0f
;
540 vg_slewf(&objective
->transform
.s
[0], target
, vg
.time_frame_delta
*4.0f
);
541 scale
= vg_smoothstepf( objective
->transform
.s
[0] );
543 if( (objective
== world_static
.challenge_target
) || passed
)
544 shader_scene_fxglow_uUvOffset( (v2f
){ 16.0f
/256.0f
, 0.0f
} );
546 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
549 f32 dist
= v3_dist( objective
->transform
.co
, pos
) * (1.0f
/radius
);
550 scale
= vg_smoothstepf( vg_clampf( 5.0f
-dist
*5.0f
, 0.0f
,1.0f
) );
554 q_m3x3( objective
->transform
.q
, mmdl
);
555 m3x3_scalef( mmdl
, scale
);
556 v3_copy( objective
->transform
.co
, mmdl
[3] );
557 shader_scene_fxglow_uMdl( mmdl
);
559 for( u32 j
=0; j
<objective
->submesh_count
; j
++ ){
560 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
561 objective
->submesh_start
+ j
);
563 if( sm
->material_id
!= last_material
){
564 last_material
= sm
->material_id
;
565 pass
->fn_bind_textures( world
, &world
->surfaces
[sm
->material_id
] );
567 mdl_draw_submesh( sm
);
572 font3d_bind( &gui
.font
, k_font_shader_world
, 0, world
, &skaterift
.cam
);
577 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_challenge
); i
++ ){
578 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, i
);
579 if( challenge
->status
) count
++;
583 c
+=highscore_intl( buf
+c
, count
, 3 );
585 c
+=highscore_intl( buf
+c
, mdl_arrcount(&world
->ent_challenge
), 3 );
588 f32 w
= font3d_string_width( 1, buf
);
590 m3x3_identity( mlocal
);
591 mlocal
[3][0] = -w
*0.5f
;
595 for( u32 i
=0; i
<challenge_count
; i
++ ){
596 u32 index
= challenge_list
[ i
];
597 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, index
);
599 mdl_transform_m4x3( &challenge
->transform
, mmdl
);
600 m4x3_mul( mmdl
, mlocal
, mmdl
);
602 vg_line_point( challenge
->transform
.co
, 0.25f
, VG__RED
);
604 f32 dist
= v3_dist( challenge
->transform
.co
, pos
) * (1.0f
/radius
),
605 scale
= vg_smoothstepf( vg_clampf( 10.0f
-dist
*10.0f
, 0.0f
,1.0f
) ),
608 if( challenge
->status
)
611 shader_scene_font_uOpacity( scale
);
612 shader_scene_font_uColourize( colour
);
613 font3d_simple_draw( 1, buf
, &skaterift
.cam
, mmdl
);
617 static void render_world_fxglow( world_instance
*host_world
,
618 world_instance
*world
, camera
*cam
,
620 int generic
, int challenges
, int regions
){
621 shader_scene_fxglow_use();
622 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
623 shader_scene_fxglow_uTexMain(1);
624 shader_scene_fxglow_uPv( cam
->mtx
.pv
);
626 world_link_lighting_ub( host_world
, _shader_scene_fxglow
.id
);
627 world_bind_position_texture( host_world
, _shader_scene_fxglow
.id
,
628 _uniform_scene_fxglow_g_world_depth
, 2 );
629 world_bind_light_array( host_world
, _shader_scene_fxglow
.id
,
630 _uniform_scene_fxglow_uLightsArray
, 3 );
631 world_bind_light_index( host_world
, _shader_scene_fxglow
.id
,
632 _uniform_scene_fxglow_uLightsIndex
, 4 );
634 shader_scene_fxglow_uCamera( cam
->transform
[3] );
635 glDisable(GL_CULL_FACE
);
637 struct world_pass pass
= {
638 .shader
= k_shader_fxglow
,
640 .fn_bind_textures
= bindpoint_diffuse_texture1
,
641 .fn_set_mdl
= shader_scene_fxglow_uMdl
,
642 .fn_set_uPvmPrev
= shader_scene_fxglow_uPvmPrev
,
646 world_render_both_stages( world
, &pass
);
649 mesh_bind( &world
->mesh_no_collide
);
651 u32 last_material
= 0;
652 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_region
); i
++ ){
653 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
654 ent_region
*region
= mdl_arritm( &world
->ent_region
, i
);
657 if( region
->flags
& k_ent_route_flag_achieve_gold
)
659 else if( region
->flags
& k_ent_route_flag_achieve_silver
)
662 shader_scene_fxglow_uUvOffset( (v2f
){ (8.0f
/256.0f
)*offset
, 0.0f
} );
665 mdl_transform_m4x3( ®ion
->transform
, mmdl
);
666 m4x3_mul( world_mmdl
, mmdl
, mmdl
);
667 shader_scene_fxglow_uMdl( mmdl
);
669 for( u32 j
=0; j
<region
->submesh_count
; j
++ ){
670 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
671 region
->submesh_start
+ j
);
673 if( sm
->material_id
!= last_material
){
674 last_material
= sm
->material_id
;
675 pass
.fn_bind_textures(world
,&world
->surfaces
[sm
->material_id
]);
677 mdl_draw_submesh( sm
);
683 world_render_challenges( world
, &pass
, cam
->pos
);
685 glEnable(GL_CULL_FACE
);
688 static void bindpoint_terrain( world_instance
*world
,
689 struct world_surface
*mat
)
691 glActiveTexture( GL_TEXTURE1
);
692 glBindTexture( GL_TEXTURE_2D
,
693 world_get_texture(world
,mat
->info
.tex_diffuse
) );
695 shader_scene_terrain_uSandColour( mat
->info
.colour
);
696 shader_scene_terrain_uBlendOffset( mat
->info
.colour1
);
699 static void bindpoint_override( world_instance
*world
,
700 struct world_surface
*mat
){
701 if( mat
->info
.flags
& k_material_flag_collision
){
702 shader_scene_override_uAlphatest(0);
705 glActiveTexture( GL_TEXTURE1
);
706 glBindTexture( GL_TEXTURE_2D
,
707 world_get_texture(world
,mat
->info
.tex_diffuse
) );
708 shader_scene_override_uAlphatest(1);
712 static void render_terrain( world_instance
*world
, camera
*cam
){
713 shader_scene_terrain_use();
714 shader_scene_terrain_uTexGarbage(0);
715 shader_scene_terrain_uTexGradients(1);
717 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_terrain
);
718 glActiveTexture( GL_TEXTURE0
);
719 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
721 shader_scene_terrain_uPv( cam
->mtx
.pv
);
722 shader_scene_terrain_uCamera( cam
->transform
[3] );
724 struct world_pass pass
= {
725 .shader
= k_shader_terrain_blend
,
727 .fn_bind_textures
= bindpoint_terrain
,
728 .fn_set_mdl
= shader_scene_terrain_uMdl
,
729 .fn_set_uPvmPrev
= shader_scene_terrain_uPvmPrev
,
732 world_render_both_stages( world
, &pass
);
735 static void render_sky( world_instance
*world
, camera
*cam
){
737 * Modify matrix to remove clipping and view translation
744 m4x4_copy( cam
->mtx
.v
, v
);
745 m4x4_copy( cam
->mtx_prev
.v
, v_prev
);
747 for( int i
=0; i
<3; i
++ ){
749 v3_normalize(v_prev
[i
]);
752 v3_zero( v_prev
[3] );
754 m4x4_copy( cam
->mtx
.p
, pv
);
755 m4x4_copy( cam
->mtx_prev
.p
, pv_prev
);
756 m4x4_reset_clipping( pv
, 100.0f
, 0.1f
);
757 m4x4_reset_clipping( pv_prev
, 100.0f
, 0.1f
);
759 m4x4_mul( pv
, v
, pv
);
760 m4x4_mul( pv_prev
, v_prev
, pv_prev
);
762 m4x3f identity_matrix
;
763 m4x3_identity( identity_matrix
);
768 if( world
->skybox
== k_skybox_default
){
769 shader_model_sky_use();
770 shader_model_sky_uMdl( identity_matrix
);
771 shader_model_sky_uPv( pv
);
772 shader_model_sky_uPvmPrev( pv_prev
);
773 shader_model_sky_uTexGarbage(0);
774 world_link_lighting_ub( world
, _shader_model_sky
.id
);
776 glActiveTexture( GL_TEXTURE0
);
777 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
779 else if( world
->skybox
== k_skybox_space
){
780 shader_model_sky_space_use();
782 shader_model_sky_space_uMdl( identity_matrix
);
783 shader_model_sky_space_uPv( pv
);
784 shader_model_sky_space_uPvmPrev( pv_prev
);
785 shader_model_sky_space_uTexGarbage(0);
786 world_link_lighting_ub( world
, _shader_model_sky_space
.id
);
788 glActiveTexture( GL_TEXTURE0
);
789 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
795 glDepthMask( GL_FALSE
);
796 glDisable( GL_DEPTH_TEST
);
798 mesh_bind( &world_render
.skydome
);
799 mesh_draw( &world_render
.skydome
);
801 glEnable( GL_DEPTH_TEST
);
802 glDepthMask( GL_TRUE
);
805 static void render_world_gates( world_instance
*world
, camera
*cam
){
806 float closest
= INFINITY
;
807 struct ent_gate
*gate
= NULL
;
809 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
810 ent_gate
*gi
= mdl_arritm( &world
->ent_gate
, i
);
811 if( !(gi
->flags
& k_ent_gate_linked
) )
814 float dist
= v3_dist2( gi
->co
[0], cam
->transform
[3] );
816 vg_line_point( gi
->co
[0], 0.25f
, VG__BLUE
);
818 if( dist
< closest
){
824 world
->rendering_gate
= gate
;
827 if( gate
->flags
& k_ent_gate_locked
){
828 world
->rendering_gate
= NULL
;
832 if( gate
->flags
& k_ent_gate_nonlocal
){
833 if( world_static
.load_state
!= k_world_loader_none
){
834 world
->rendering_gate
= NULL
;
838 world_instance
*dest_world
= &world_static
.instances
[ gate
->target
];
839 render_gate( world
, dest_world
, gate
, cam
);
842 render_gate( world
, world
, gate
, cam
);
846 static void world_prerender( world_instance
*world
){
847 if( mdl_arrcount( &world
->ent_light
) ){
848 f32 rate
= vg_maxf(0.1f
, fabsf(k_day_length
)) * vg_signf(k_day_length
);
849 world
->time
+= vg
.time_frame_delta
* (1.0/(rate
*60.0));
855 struct ub_world_lighting
*state
= &world
->ub_lighting
;
857 state
->g_time
= world
->time
;
858 state
->g_realtime
= vg
.time_real
;
859 state
->g_debug_indices
= k_debug_light_indices
;
860 state
->g_light_preview
= k_light_preview
;
861 state
->g_debug_complexity
= k_debug_light_complexity
;
862 state
->g_time_of_day
= vg_fractf( world
->time
);
864 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
865 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
867 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
868 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
870 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
871 state
->g_sun_dir
[0] = sinf( a
);
872 state
->g_sun_dir
[1] = cosf( a
);
873 state
->g_sun_dir
[2] = 0.2f
;
874 v3_normalize( state
->g_sun_dir
);
876 world
->probabilities
[ k_probability_curve_constant
] = 1.0f
;
877 float dp
= state
->g_day_phase
;
879 world
->probabilities
[ k_probability_curve_wildlife_day
] =
880 (dp
*dp
*0.8f
+state
->g_sunset_phase
)*0.8f
;
881 world
->probabilities
[ k_probability_curve_wildlife_night
] =
882 1.0f
-powf(fabsf((state
->g_time_of_day
-0.5f
)*5.0f
),5.0f
);
884 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
885 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
886 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
889 static void render_world( world_instance
*world
, camera
*cam
,
890 int stenciled
, int viewing_from_gate
,
891 int with_water
, int with_cubemaps
){
893 glClear( GL_DEPTH_BUFFER_BIT
);
894 glStencilFunc( GL_EQUAL
, 1, 0xFF );
895 glStencilMask( 0x00 );
896 glEnable( GL_CULL_FACE
);
897 glEnable( GL_STENCIL_TEST
);
900 glStencilMask( 0xFF );
901 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
902 glDisable( GL_STENCIL_TEST
);
905 render_sky( world
, cam
);
908 m4x3_identity(identity
);
909 render_world_routes( world
, world
, identity
, cam
, viewing_from_gate
, 0 );
910 render_world_standard( world
, cam
);
911 render_world_cubemapped( world
, cam
, with_cubemaps
);
913 render_world_vb( world
, cam
);
914 render_world_alphatest( world
, cam
);
915 render_terrain( world
, cam
);
917 if( !viewing_from_gate
){
918 world_entity_focus_render();
922 float min_dist
= INFINITY
;
924 if( mdl_arrcount( &world
->ent_route
) ){
925 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
926 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
927 float dist
= v3_dist2( route
->board_transform
[3], cam
->pos
);
929 if( dist
< min_dist
){
935 ent_route
*route
= mdl_arritm( &world
->ent_route
, closest
);
936 sfd_render( world
, cam
, route
->board_transform
);
940 if( !viewing_from_gate
){
942 if( mdl_entity_id_type(world_static
.focused_entity
) == k_ent_challenge
)
943 greyout
= world_static
.focus_strength
;
945 if( greyout
> 0.0f
){
946 glDrawBuffers( 1, (GLenum
[]){ GL_COLOR_ATTACHMENT0
} );
948 glDisable(GL_DEPTH_TEST
);
949 glDepthMask(GL_FALSE
);
950 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
951 glBlendEquation(GL_FUNC_ADD
);
953 shader_blitcolour_use();
954 shader_blitcolour_uColour( (v4f
){ 0.5f
, 0.5f
, 0.5f
, greyout
*0.56f
} );
958 glEnable(GL_DEPTH_TEST
);
959 glDepthMask(GL_TRUE
);
960 glDrawBuffers( 2, (GLenum
[]){ GL_COLOR_ATTACHMENT0
,
961 GL_COLOR_ATTACHMENT1
} );
964 render_world_fxglow( world
, world
, cam
, NULL
, 1, 1, 0 );
968 render_water_texture( world
, cam
);
969 render_fb_bind( gpipeline
.fb_main
, 1 );
973 glStencilFunc( GL_EQUAL
, 1, 0xFF );
974 glStencilMask( 0x00 );
975 glEnable( GL_CULL_FACE
);
976 glEnable( GL_STENCIL_TEST
);
980 render_water_surface( world
, cam
);
983 render_remote_players( world
, cam
);
984 ent_miniworld_render( world
, cam
);
987 glStencilMask( 0xFF );
988 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
989 glDisable( GL_STENCIL_TEST
);
994 static void render_world_override_pass( world_instance
*world
,
995 struct world_pass
*pass
,
996 m4x3f mmdl
, m3x3f mnormal
,
998 for( int i
=0; i
<world
->surface_count
; i
++ ){
999 struct world_surface
*mat
= &world
->surfaces
[i
];
1001 if( mat
->info
.flags
& k_material_flag_ghosts
) continue;
1004 if( pass
->geo_type
== k_world_geo_type_solid
)
1007 sm
= &mat
->sm_no_collide
;
1009 if( !sm
->indice_count
)
1012 pass
->fn_set_mdl( mmdl
);
1013 pass
->fn_set_uNormalMtx( mnormal
);
1014 pass
->fn_set_uPvmPrev( mpvm_prev
);
1015 pass
->fn_bind_textures( world
, mat
);
1016 mdl_draw_submesh( sm
);
1020 static void render_world_override( world_instance
*world
,
1021 world_instance
*lighting_source
,
1024 ent_spawn
*dest_spawn
, v4f map_info
){
1025 struct world_pass pass
= {
1027 .fn_bind_textures
= bindpoint_override
,
1028 .fn_set_mdl
= shader_scene_override_uMdl
,
1029 .fn_set_uPvmPrev
= shader_scene_override_uPvmPrev
,
1030 .fn_set_uNormalMtx
= shader_scene_override_uNormalMtx
,
1031 .shader
= k_shader_override
1034 shader_scene_override_use();
1035 shader_scene_override_uTexGarbage(0);
1036 shader_scene_override_uTexMain(1);
1037 shader_scene_override_uPv( pass
.cam
->mtx
.pv
);
1038 shader_scene_override_uMapInfo( map_info
);
1040 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( lighting_source
, scene_override
);
1041 bind_terrain_noise();
1043 shader_scene_override_uCamera( pass
.cam
->transform
[3] );
1046 m4x3_expand( mmdl
, mpvm_prev
);
1047 m4x4_mul( cam
->mtx_prev
.pv
, mpvm_prev
, mpvm_prev
);
1050 m3x3_inv( mmdl
, mnormal
);
1051 m3x3_transpose( mnormal
, mnormal
);
1052 v3_normalize( mnormal
[0] );
1053 v3_normalize( mnormal
[1] );
1054 v3_normalize( mnormal
[2] );
1056 v4f uPlayerPos
, uSpawnPos
;
1057 v4_zero( uPlayerPos
);
1058 v4_zero( uSpawnPos
);
1060 v3_copy( world
->player_co
, uPlayerPos
);
1062 if( dest_spawn
&& (v3_dist2(dest_spawn
->transform
.co
,uPlayerPos
) > 0.1f
) )
1063 v3_copy( dest_spawn
->transform
.co
, uSpawnPos
);
1065 v3_add( uPlayerPos
, (v3f
){0,-1,0}, uSpawnPos
);
1067 uPlayerPos
[3] = v3_dist(uPlayerPos
,uSpawnPos
);
1068 uSpawnPos
[3] = 1.0f
/uPlayerPos
[3];
1070 shader_scene_override_uPlayerPos( uPlayerPos
);
1071 shader_scene_override_uSpawnPos( uSpawnPos
);
1074 glDisable( GL_CULL_FACE
);
1075 mesh_bind( &world
->mesh_geo
);
1076 pass
.geo_type
= k_world_geo_type_solid
;
1077 render_world_override_pass( world
, &pass
, mmdl
, mnormal
, mpvm_prev
);
1078 mesh_bind( &world
->mesh_no_collide
);
1079 pass
.geo_type
= k_world_geo_type_nonsolid
;
1080 render_world_override_pass( world
, &pass
, mmdl
, mnormal
, mpvm_prev
);
1081 glEnable( GL_CULL_FACE
);
1083 render_world_fxglow( world
, world
, cam
, mmdl
, 0, 0, 1 );
1086 static void render_cubemap_side( world_instance
*world
, ent_cubemap
*cm
,
1089 glFramebufferTexture2D( GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
1090 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ side
, cm
->texture_id
, 0 );
1091 glClear( GL_DEPTH_BUFFER_BIT
);
1094 { -1.0f
, 0.0f
, 0.0f
},
1095 { 1.0f
, 0.0f
, 0.0f
},
1096 { 0.0f
, -1.0f
, 0.0f
},
1097 { 0.0f
, 1.0f
, 0.0f
},
1098 { 0.0f
, 0.0f
, -1.0f
},
1099 { 0.0f
, 0.0f
, 1.0f
}
1102 { 0.0f
, -1.0f
, 0.0f
},
1103 { 0.0f
, -1.0f
, 0.0f
},
1104 { 0.0f
, 0.0f
, 1.0f
},
1105 { 0.0f
, 0.0f
, -1.0f
},
1106 { 0.0f
, -1.0f
, 0.0f
},
1107 { 0.0f
, -1.0f
, 0.0f
}
1110 v3_zero( cam
.angles
);
1111 v3_copy( cm
->co
, cam
.pos
);
1113 v3_copy( forward
[side
], cam
.transform
[2] );
1114 v3_copy( up
[side
], cam
.transform
[1] );
1115 v3_cross( up
[side
], forward
[side
], cam
.transform
[0] );
1116 v3_copy( cm
->co
, cam
.transform
[3] );
1117 m4x3_invert_affine( cam
.transform
, cam
.transform_inverse
);
1119 camera_update_view( &cam
);
1124 m4x4_copy( cam
.mtx
.p
, cam
.mtx_prev
.p
);
1125 m4x4_projection( cam
.mtx
.p
, cam
.fov
, 1.0f
, cam
.nearz
, cam
.farz
);
1126 camera_finalize( &cam
);
1127 camera_finalize( &cam
);
1129 render_world( world
, &cam
, 0, 1, 1, 0 );
1132 static void render_world_cubemaps( world_instance
*world
){
1133 if( world
->cubemap_cooldown
)
1134 world
->cubemap_cooldown
--;
1136 world
->cubemap_cooldown
= 60;
1138 glViewport( 0, 0, WORLD_CUBEMAP_RES
, WORLD_CUBEMAP_RES
);
1139 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_cubemap
); i
++ ){
1140 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, i
);
1141 glBindFramebuffer( GL_FRAMEBUFFER
, cm
->framebuffer_id
);
1143 world
->cubemap_side
++;
1144 if( world
->cubemap_side
>= 6 )
1145 world
->cubemap_side
= 0;
1147 render_cubemap_side( world
, cm
, world
->cubemap_side
);
1152 static void render_world_depth( world_instance
*world
, camera
*cam
){
1153 m4x3f identity_matrix
;
1154 m4x3_identity( identity_matrix
);
1156 shader_scene_depth_use();
1157 shader_scene_depth_uCamera( cam
->transform
[3] );
1158 shader_scene_depth_uPv( cam
->mtx
.pv
);
1159 shader_scene_depth_uPvmPrev( cam
->mtx_prev
.pv
);
1160 shader_scene_depth_uMdl( identity_matrix
);
1161 world_link_lighting_ub( world
, _shader_scene_depth
.id
);
1163 mesh_bind( &world
->mesh_geo
);
1164 mesh_draw( &world
->mesh_geo
);
1167 static void render_world_position( world_instance
*world
, camera
*cam
){
1168 m4x3f identity_matrix
;
1169 m4x3_identity( identity_matrix
);
1171 shader_scene_position_use();
1172 shader_scene_position_uCamera( cam
->transform
[3] );
1173 shader_scene_position_uPv( cam
->mtx
.pv
);
1174 shader_scene_position_uPvmPrev( cam
->mtx_prev
.pv
);
1175 shader_scene_position_uMdl( identity_matrix
);
1176 world_link_lighting_ub( world
, _shader_scene_position
.id
);
1178 mesh_bind( &world
->mesh_geo
);
1179 mesh_draw( &world
->mesh_geo
);