2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
6 #include "world_render.h"
10 #include "ent_miniworld.h"
11 #include "player_remote.h"
12 #include "ent_skateshop.h"
13 #include "shaders/model_entity.h"
15 struct world_render world_render
;
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 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_VAR_I32( k_light_editor
);
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 vg_info( "Loading world resources\n" );
81 vg_linear_clear( vg_mem
.scratch
);
84 mdl_open( &msky
, "models/rs_skydome.mdl", vg_mem
.scratch
);
85 mdl_load_metadata_block( &msky
, vg_mem
.scratch
);
86 mdl_async_load_glmesh( &msky
, &world_render
.skydome
, NULL
);
89 vg_info( "Loading default world textures\n" );
90 vg_tex2d_load_qoi_async_file( "textures/garbage.qoi",
91 VG_TEX2D_NEAREST
|VG_TEX2D_REPEAT
,
92 &world_render
.tex_terrain_noise
);
94 vg_async_call( async_world_render_init
, NULL
, 0 );
98 * standard uniform bindings
99 * ----------------------------------------------------------------------------
101 void world_link_lighting_ub( world_instance
*world
, GLuint shader
)
103 GLuint idx
= glGetUniformBlockIndex( shader
, "ub_world_lighting" );
104 glUniformBlockBinding( shader
, idx
, world
->ubo_bind_point
);
107 void world_bind_position_texture( world_instance
*world
,
108 GLuint shader
, GLuint location
,
111 render_fb_bind_texture( &world
->heightmap
, 0, slot
);
112 glUniform1i( location
, slot
);
115 void world_bind_light_array( world_instance
*world
,
116 GLuint shader
, GLuint location
,
119 glActiveTexture( GL_TEXTURE0
+ slot
);
120 glBindTexture( GL_TEXTURE_BUFFER
, world
->tex_light_entities
);
121 glUniform1i( location
, slot
);
124 void world_bind_light_index( world_instance
*world
,
125 GLuint shader
, GLuint location
,
128 glActiveTexture( GL_TEXTURE0
+ slot
);
129 glBindTexture( GL_TEXTURE_3D
, world
->tex_light_cubes
);
130 glUniform1i( location
, slot
);
133 void bind_terrain_noise(void)
135 glActiveTexture( GL_TEXTURE0
);
136 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
140 * Get OpenGL texture name from texture ID.
142 static GLuint
world_get_texture( world_instance
*world
, u32 id
){
143 if( id
& 0x80000000 ) return skaterift
.rt_textures
[id
& ~0x80000000];
144 else return world
->textures
[ id
];
147 static void bindpoint_diffuse_texture1( world_instance
*world
,
148 struct world_surface
*mat
){
149 glActiveTexture( GL_TEXTURE1
);
150 glBindTexture( GL_TEXTURE_2D
,
151 world_get_texture(world
,mat
->info
.tex_diffuse
) );
156 * ----------------------------------------------------------------------------
161 enum mdl_shader shader
;
162 enum world_geo_type geo_type
;
164 void (*fn_bind_textures
)( world_instance
*world
,
165 struct world_surface
*mat
);
166 void (*fn_set_mdl
)( m4x3f mdl
);
167 void (*fn_set_uPvmPrev
)( m4x4f pvm
);
168 void (*fn_set_uNormalMtx
)( m3x3f mnorm
);
171 void render_world_depth( world_instance
*world
, vg_camera
*cam
);
174 * Render a run of submeshes, only of those which match material_id
176 static void world_render_submeshes( world_instance
*world
,
177 struct world_pass
*pass
,
178 mdl_transform
*transform
,
179 u32 start
, u32 count
, u32 material_id
){
180 for( u32 k
=0; k
<count
; k
++ ){
181 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
, start
+k
);
182 if( sm
->material_id
!= material_id
) continue;
185 mdl_transform_m4x3( transform
, mmdl
);
188 m4x3_expand( mmdl
, m4mdl
);
189 m4x4_mul( pass
->cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
191 pass
->fn_set_mdl( mmdl
);
192 pass
->fn_set_uPvmPrev( m4mdl
);
194 mdl_draw_submesh( sm
);
199 * Render props attached to this material
201 static void world_render_props( world_instance
*world
, u32 material_id
,
202 struct world_pass
*pass
){
203 struct world_surface
*mat
= &world
->surfaces
[ material_id
];
204 if( !(mat
->flags
& WORLD_SURFACE_HAS_PROPS
) ) return;
206 pass
->fn_bind_textures( world
, mat
);
208 for( u32 j
=0; j
<mdl_arrcount( &world
->ent_prop
); j
++ ){
209 ent_prop
*prop
= mdl_arritm( &world
->ent_prop
, j
);
210 if( prop
->flags
& 0x1 ) continue;
212 world_render_submeshes( world
, pass
, &prop
->transform
,
213 prop
->submesh_start
, prop
->submesh_count
, material_id
);
218 * Render traffic models attactched to this material
220 static void world_render_traffic( world_instance
*world
, u32 material_id
,
221 struct world_pass
*pass
){
222 struct world_surface
*mat
= &world
->surfaces
[ material_id
];
223 if( !(mat
->flags
& WORLD_SURFACE_HAS_TRAFFIC
) ) return;
225 pass
->fn_bind_textures( world
, mat
);
227 for( u32 j
=0; j
<mdl_arrcount( &world
->ent_traffic
); j
++ ){
228 ent_traffic
*traffic
= mdl_arritm( &world
->ent_traffic
, j
);
230 world_render_submeshes( world
, pass
, &traffic
->transform
,
231 traffic
->submesh_start
, traffic
->submesh_count
,
237 * Iterate and render all materials which match the passes shader and geometry
238 * type. Includes props/traffic.
240 static void world_render_pass( world_instance
*world
, struct world_pass
*pass
){
241 for( int i
=0; i
<world
->surface_count
; i
++ ){
242 struct world_surface
*mat
= &world
->surfaces
[i
];
244 if( mat
->info
.shader
== pass
->shader
){
247 if( pass
->geo_type
== k_world_geo_type_solid
){
251 world_render_traffic( world
, i
, pass
);
252 world_render_props( world
, i
, pass
);
253 sm
= &mat
->sm_no_collide
;
256 if( !sm
->indice_count
)
260 m4x3_identity( mmdl
);
261 pass
->fn_set_mdl( mmdl
);
262 pass
->fn_set_uPvmPrev( pass
->cam
->mtx_prev
.pv
);
264 pass
->fn_bind_textures( world
, mat
);
265 mdl_draw_submesh( sm
);
271 * Specific shader instructions
272 * ----------------------------------------------------------------------------
275 static void world_render_both_stages( world_instance
*world
,
276 struct world_pass
*pass
){
277 mesh_bind( &world
->mesh_geo
);
278 pass
->geo_type
= k_world_geo_type_solid
;
279 world_render_pass( world
, pass
);
281 glDisable( GL_CULL_FACE
);
282 mesh_bind( &world
->mesh_no_collide
);
283 pass
->geo_type
= k_world_geo_type_nonsolid
;
284 world_render_pass( world
, pass
);
285 glEnable( GL_CULL_FACE
);
288 static void render_world_vb( world_instance
*world
, vg_camera
*cam
){
289 shader_scene_vertex_blend_use();
290 shader_scene_vertex_blend_uTexGarbage(0);
291 shader_scene_vertex_blend_uTexGradients(1);
292 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_vertex_blend
);
294 glActiveTexture( GL_TEXTURE0
);
295 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
297 shader_scene_vertex_blend_uPv( cam
->mtx
.pv
);
298 shader_scene_vertex_blend_uCamera( cam
->transform
[3] );
300 struct world_pass pass
= {
301 .shader
= k_shader_standard_vertex_blend
,
303 .fn_bind_textures
= bindpoint_diffuse_texture1
,
304 .fn_set_mdl
= shader_scene_vertex_blend_uMdl
,
305 .fn_set_uPvmPrev
= shader_scene_vertex_blend_uPvmPrev
,
308 world_render_both_stages( world
, &pass
);
311 static void world_shader_standard_bind( world_instance
*world
, vg_camera
*cam
){
312 shader_scene_standard_use();
313 shader_scene_standard_uTexGarbage(0);
314 shader_scene_standard_uTexMain(1);
315 shader_scene_standard_uPv( cam
->mtx
.pv
);
316 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_standard
);
318 bind_terrain_noise();
319 shader_scene_standard_uCamera( cam
->transform
[3] );
322 static void render_world_standard( world_instance
*world
, vg_camera
*cam
){
323 world_shader_standard_bind( world
, cam
);
324 struct world_pass pass
= {
325 .shader
= k_shader_standard
,
327 .fn_bind_textures
= bindpoint_diffuse_texture1
,
328 .fn_set_mdl
= shader_scene_standard_uMdl
,
329 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
332 world_render_both_stages( world
, &pass
);
335 static void bindpoint_diffuse1_and_cubemap10( world_instance
*world
,
336 struct world_surface
*mat
){
337 glActiveTexture( GL_TEXTURE1
);
338 glBindTexture( GL_TEXTURE_2D
,
339 world_get_texture(world
,mat
->info
.tex_diffuse
) );
341 u32 cubemap_id
= mat
->info
.tex_none0
,
344 if( mdl_entity_id_type( cubemap_id
) == k_ent_cubemap
){
345 cubemap_index
= mdl_entity_id_id( cubemap_id
);
348 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, cubemap_index
);
349 glActiveTexture( GL_TEXTURE10
);
350 glBindTexture( GL_TEXTURE_CUBE_MAP
, cm
->texture_id
);
352 shader_scene_cubemapped_uColour( mat
->info
.colour
);
355 static void render_world_cubemapped( world_instance
*world
, vg_camera
*cam
,
357 if( !mdl_arrcount( &world
->ent_cubemap
) )
361 world_shader_standard_bind( world
, cam
);
363 struct world_pass pass
= {
364 .shader
= k_shader_cubemap
,
366 .fn_bind_textures
= bindpoint_diffuse_texture1
,
367 .fn_set_mdl
= shader_scene_standard_uMdl
,
368 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
371 world_render_both_stages( world
, &pass
);
374 shader_scene_cubemapped_use();
375 shader_scene_cubemapped_uTexGarbage(0);
376 shader_scene_cubemapped_uTexMain(1);
377 shader_scene_cubemapped_uTexCubemap(10);
378 shader_scene_cubemapped_uPv( cam
->mtx
.pv
);
380 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_cubemapped
);
382 bind_terrain_noise();
383 shader_scene_cubemapped_uCamera( cam
->transform
[3] );
385 struct world_pass pass
= {
386 .shader
= k_shader_cubemap
,
388 .fn_bind_textures
= bindpoint_diffuse1_and_cubemap10
,
389 .fn_set_mdl
= shader_scene_cubemapped_uMdl
,
390 .fn_set_uPvmPrev
= shader_scene_cubemapped_uPvmPrev
,
393 world_render_both_stages( world
, &pass
);
397 static void render_world_alphatest( world_instance
*world
, vg_camera
*cam
){
398 shader_scene_standard_alphatest_use();
399 shader_scene_standard_alphatest_uTexGarbage(0);
400 shader_scene_standard_alphatest_uTexMain(1);
401 shader_scene_standard_alphatest_uPv( cam
->mtx
.pv
);
403 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_standard_alphatest
);
405 bind_terrain_noise();
406 shader_scene_standard_alphatest_uCamera( cam
->transform
[3] );
407 glDisable(GL_CULL_FACE
);
409 struct world_pass pass
= {
410 .shader
= k_shader_standard_cutout
,
412 .fn_bind_textures
= bindpoint_diffuse_texture1
,
413 .fn_set_mdl
= shader_scene_standard_alphatest_uMdl
,
414 .fn_set_uPvmPrev
= shader_scene_standard_alphatest_uPvmPrev
,
417 world_render_both_stages( world
, &pass
);
418 glEnable(GL_CULL_FACE
);
421 static void render_world_foliage( world_instance
*world
, vg_camera
*cam
){
422 shader_scene_foliage_use();
423 shader_scene_foliage_uTexGarbage(0);
424 shader_scene_foliage_uTexMain(1);
425 shader_scene_foliage_uPv( cam
->mtx
.pv
);
426 shader_scene_foliage_uTime( vg
.time
);
428 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_foliage
);
429 bind_terrain_noise();
431 shader_scene_foliage_uCamera( cam
->transform
[3] );
432 glDisable(GL_CULL_FACE
);
433 struct world_pass pass
= {
434 .shader
= k_shader_foliage
,
436 .fn_bind_textures
= bindpoint_diffuse_texture1
,
437 .fn_set_mdl
= shader_scene_foliage_uMdl
,
438 .fn_set_uPvmPrev
= shader_scene_foliage_uPvmPrev
,
440 world_render_both_stages( world
, &pass
);
441 glEnable(GL_CULL_FACE
);
444 static void world_render_challenges( world_instance
*world
,
445 struct world_pass
*pass
, v3f pos
){
447 if( skaterift
.activity
== k_skaterift_replay
) return;
448 if( world
!= world_current_instance() ) return;
453 u32 objective_list
[ 32 ],
454 challenge_list
[ 16 ];
456 v2f objective_uv_offsets
[ 32 ];
458 u32 objective_count
= 0,
461 ent_challenge
*active_challenge
= NULL
;
463 if( mdl_entity_id_type( world_static
.focused_entity
) == k_ent_challenge
){
464 if( (skaterift
.activity
== k_skaterift_default
) &&
465 world_static
.challenge_target
){
469 if( !((skaterift
.activity
!= k_skaterift_ent_focus
) &&
470 !world_static
.challenge_target
) ){
471 world_instance
*challenge_world
= world_current_instance();
472 u32 index
= mdl_entity_id_id( world_static
.focused_entity
);
473 active_challenge
= mdl_arritm(&challenge_world
->ent_challenge
, index
);
477 if( active_challenge
){
478 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
479 challenge_list
[ challenge_count
++ ] = world_static
.focused_entity
;
481 u32 next
= active_challenge
->first
;
482 while( mdl_entity_id_type(next
) == k_ent_objective
){
483 u32 index
= mdl_entity_id_id( next
);
484 objective_list
[ objective_count
++ ] = index
;
486 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
487 next
= objective
->id_next
;
493 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
495 bh_iter_init_range( 0, &it
, pos
, radius
+10.0f
);
497 while( bh_next( world
->entity_bh
, &it
, &idx
) ){
498 u32 id
= world
->entity_list
[ idx
],
499 type
= mdl_entity_id_type( id
),
500 index
= mdl_entity_id_id( id
);
502 if( type
== k_ent_objective
) {
503 if( objective_count
< vg_list_size(objective_list
) )
504 objective_list
[ objective_count
++ ] = index
;
506 else if( type
== k_ent_challenge
){
507 if( challenge_count
< vg_list_size(challenge_list
) )
508 challenge_list
[ challenge_count
++ ] = index
;
513 /* render objectives */
514 glDisable( GL_CULL_FACE
);
515 mesh_bind( &world
->mesh_no_collide
);
516 u32 last_material
= 0;
517 for( u32 i
=0; i
<objective_count
; i
++ ){
518 u32 index
= objective_list
[ i
];
519 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
520 if( (objective
->flags
& k_ent_objective_hidden
) &&
521 !active_challenge
) continue;
526 u32 passed
= objective
->flags
& k_ent_objective_passed
;
527 f32 target
= passed
? 0.0f
: 1.0f
;
528 vg_slewf(&objective
->transform
.s
[0], target
, vg
.time_frame_delta
*4.0f
);
529 scale
= vg_smoothstepf( objective
->transform
.s
[0] );
531 if( (objective
== world_static
.challenge_target
) || passed
)
532 shader_scene_fxglow_uUvOffset( (v2f
){ 16.0f
/256.0f
, 0.0f
} );
534 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
537 f32 dist
= v3_dist( objective
->transform
.co
, pos
) * (1.0f
/radius
);
538 scale
= vg_smoothstepf( vg_clampf( 5.0f
-dist
*5.0f
, 0.0f
,1.0f
) );
542 q_m3x3( objective
->transform
.q
, mmdl
);
543 m3x3_scalef( mmdl
, scale
);
544 v3_copy( objective
->transform
.co
, mmdl
[3] );
545 shader_scene_fxglow_uMdl( mmdl
);
547 for( u32 j
=0; j
<objective
->submesh_count
; j
++ ){
548 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
549 objective
->submesh_start
+ j
);
551 if( sm
->material_id
!= last_material
){
552 last_material
= sm
->material_id
;
553 pass
->fn_bind_textures( world
, &world
->surfaces
[sm
->material_id
] );
555 mdl_draw_submesh( sm
);
560 font3d_bind( &gui
.font
, k_font_shader_world
, 0, world
, &skaterift
.cam
);
564 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_challenge
); i
++ )
566 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, i
);
567 if( challenge
->status
) count
++;
572 vg_strnull( &str
, buf
, sizeof(buf
) );
573 vg_strcati32( &str
, count
);
574 vg_strcatch( &str
, '/' );
575 vg_strcati32( &str
, mdl_arrcount(&world
->ent_challenge
) );
577 f32 w
= font3d_string_width( 1, buf
);
579 m3x3_identity( mlocal
);
580 mlocal
[3][0] = -w
*0.5f
;
584 for( u32 i
=0; i
<challenge_count
; i
++ )
586 u32 index
= challenge_list
[ i
];
587 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, index
);
589 mdl_transform_m4x3( &challenge
->transform
, mmdl
);
590 m4x3_mul( mmdl
, mlocal
, mmdl
);
592 vg_line_point( challenge
->transform
.co
, 0.25f
, VG__RED
);
594 f32 dist
= v3_dist( challenge
->transform
.co
, pos
) * (1.0f
/radius
),
595 scale
= vg_smoothstepf( vg_clampf( 10.0f
-dist
*10.0f
, 0.0f
,1.0f
) ),
598 if( challenge
->status
)
601 shader_scene_font_uOpacity( scale
);
602 shader_scene_font_uColourize( colour
);
603 font3d_simple_draw( 1, buf
, &skaterift
.cam
, mmdl
);
607 static void render_world_fxglow( world_instance
*host_world
,
608 world_instance
*world
, vg_camera
*cam
,
610 int generic
, int challenges
, int regions
){
611 shader_scene_fxglow_use();
612 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
613 shader_scene_fxglow_uTexMain(1);
614 shader_scene_fxglow_uPv( cam
->mtx
.pv
);
615 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_fxglow
);
617 shader_scene_fxglow_uCamera( cam
->transform
[3] );
618 glDisable(GL_CULL_FACE
);
620 struct world_pass pass
= {
621 .shader
= k_shader_fxglow
,
623 .fn_bind_textures
= bindpoint_diffuse_texture1
,
624 .fn_set_mdl
= shader_scene_fxglow_uMdl
,
625 .fn_set_uPvmPrev
= shader_scene_fxglow_uPvmPrev
,
629 world_render_both_stages( world
, &pass
);
632 mesh_bind( &world
->mesh_no_collide
);
634 u32 last_material
= 0;
635 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_region
); i
++ ){
636 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
637 ent_region
*region
= mdl_arritm( &world
->ent_region
, i
);
640 if( region
->flags
& k_ent_route_flag_achieve_gold
)
642 else if( region
->flags
& k_ent_route_flag_achieve_silver
)
645 shader_scene_fxglow_uUvOffset( (v2f
){ (8.0f
/256.0f
)*offset
, 0.0f
} );
648 mdl_transform_m4x3( ®ion
->transform
, mmdl
);
649 m4x3_mul( world_mmdl
, mmdl
, mmdl
);
650 shader_scene_fxglow_uMdl( mmdl
);
652 for( u32 j
=0; j
<region
->submesh_count
; j
++ ){
653 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
654 region
->submesh_start
+ j
);
656 if( sm
->material_id
!= last_material
){
657 last_material
= sm
->material_id
;
658 pass
.fn_bind_textures(world
,&world
->surfaces
[sm
->material_id
]);
660 mdl_draw_submesh( sm
);
666 world_render_challenges( world
, &pass
, cam
->pos
);
668 glEnable(GL_CULL_FACE
);
671 static void bindpoint_terrain( world_instance
*world
,
672 struct world_surface
*mat
)
674 glActiveTexture( GL_TEXTURE1
);
675 glBindTexture( GL_TEXTURE_2D
,
676 world_get_texture(world
,mat
->info
.tex_diffuse
) );
678 shader_scene_terrain_uSandColour( mat
->info
.colour
);
679 shader_scene_terrain_uBlendOffset( mat
->info
.colour1
);
682 static void bindpoint_override( world_instance
*world
,
683 struct world_surface
*mat
){
684 if( mat
->info
.flags
& k_material_flag_collision
){
685 shader_scene_override_uAlphatest(0);
688 glActiveTexture( GL_TEXTURE1
);
689 glBindTexture( GL_TEXTURE_2D
,
690 world_get_texture(world
,mat
->info
.tex_diffuse
) );
691 shader_scene_override_uAlphatest(1);
695 static void render_terrain( world_instance
*world
, vg_camera
*cam
){
696 shader_scene_terrain_use();
697 shader_scene_terrain_uTexGarbage(0);
698 shader_scene_terrain_uTexGradients(1);
700 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_terrain
);
701 glActiveTexture( GL_TEXTURE0
);
702 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
704 shader_scene_terrain_uPv( cam
->mtx
.pv
);
705 shader_scene_terrain_uCamera( cam
->transform
[3] );
707 struct world_pass pass
= {
708 .shader
= k_shader_terrain_blend
,
710 .fn_bind_textures
= bindpoint_terrain
,
711 .fn_set_mdl
= shader_scene_terrain_uMdl
,
712 .fn_set_uPvmPrev
= shader_scene_terrain_uPvmPrev
,
715 world_render_both_stages( world
, &pass
);
718 static void render_sky( world_instance
*world
, vg_camera
*cam
){
720 * Modify matrix to remove clipping and view translation
727 m4x4_copy( cam
->mtx
.v
, v
);
728 m4x4_copy( cam
->mtx_prev
.v
, v_prev
);
730 for( int i
=0; i
<3; i
++ ){
732 v3_normalize(v_prev
[i
]);
735 v3_zero( v_prev
[3] );
737 m4x4_copy( cam
->mtx
.p
, pv
);
738 m4x4_copy( cam
->mtx_prev
.p
, pv_prev
);
739 m4x4_reset_clipping( pv
, 100.0f
, 0.1f
);
740 m4x4_reset_clipping( pv_prev
, 100.0f
, 0.1f
);
742 m4x4_mul( pv
, v
, pv
);
743 m4x4_mul( pv_prev
, v_prev
, pv_prev
);
745 m4x3f identity_matrix
;
746 m4x3_identity( identity_matrix
);
751 if( world
->skybox
== k_skybox_default
){
752 shader_model_sky_use();
753 shader_model_sky_uMdl( identity_matrix
);
754 shader_model_sky_uPv( pv
);
755 shader_model_sky_uPvmPrev( pv_prev
);
756 shader_model_sky_uTexGarbage(0);
757 world_link_lighting_ub( world
, _shader_model_sky
.id
);
759 glActiveTexture( GL_TEXTURE0
);
760 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
762 else if( world
->skybox
== k_skybox_space
){
763 shader_model_sky_space_use();
765 shader_model_sky_space_uMdl( identity_matrix
);
766 shader_model_sky_space_uPv( pv
);
767 shader_model_sky_space_uPvmPrev( pv_prev
);
768 shader_model_sky_space_uTexGarbage(0);
769 world_link_lighting_ub( world
, _shader_model_sky_space
.id
);
771 glActiveTexture( GL_TEXTURE0
);
772 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
775 vg_fatal_error( "Programming error\n" );
778 glDepthMask( GL_FALSE
);
779 glDisable( GL_DEPTH_TEST
);
781 mesh_bind( &world_render
.skydome
);
782 mesh_draw( &world_render
.skydome
);
784 glEnable( GL_DEPTH_TEST
);
785 glDepthMask( GL_TRUE
);
788 void render_world_gates( world_instance
*world
, vg_camera
*cam
)
790 float closest
= INFINITY
;
791 struct ent_gate
*gate
= NULL
;
793 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
794 ent_gate
*gi
= mdl_arritm( &world
->ent_gate
, i
);
796 if( !(gi
->flags
& k_ent_gate_nonlocal
) )
797 if( !(gi
->flags
& k_ent_gate_linked
) )
800 float dist
= v3_dist2( gi
->co
[0], cam
->transform
[3] );
802 vg_line_point( gi
->co
[0], 0.25f
, VG__BLUE
);
804 if( dist
< closest
){
810 world
->rendering_gate
= gate
;
813 if( gate
->flags
& k_ent_gate_locked
){
814 world
->rendering_gate
= NULL
;
818 if( gate
->flags
& k_ent_gate_nonlocal
){
819 if( !(gate
->flags
& k_ent_gate_linked
) ||
820 (world_static
.load_state
!= k_world_loader_none
) ){
821 world
->rendering_gate
= NULL
;
822 render_gate_unlinked( world
, gate
, cam
);
826 world_instance
*dest_world
= &world_static
.instances
[ gate
->target
];
827 render_gate( world
, dest_world
, gate
, cam
);
830 render_gate( world
, world
, gate
, cam
);
834 void world_prerender( world_instance
*world
)
836 if( mdl_arrcount( &world
->ent_light
) ){
837 f32 rate
= vg_maxf(0.1f
, fabsf(k_day_length
)) * vg_signf(k_day_length
);
838 world
->time
+= vg
.time_frame_delta
* (1.0/(rate
*60.0));
844 if( world
->info
.flags
& 0x1 ){
845 world
->time
= world
->info
.timezone
;
848 struct ub_world_lighting
*state
= &world
->ub_lighting
;
850 state
->g_time
= world
->time
;
851 state
->g_realtime
= vg
.time_real
;
852 state
->g_debug_indices
= k_debug_light_indices
;
853 state
->g_light_preview
= k_light_preview
;
854 state
->g_debug_complexity
= k_debug_light_complexity
;
855 state
->g_time_of_day
= vg_fractf( world
->time
);
857 if( vg
.quality_profile
== k_quality_profile_high
)
858 state
->g_shadow_samples
= 8;
859 else if( vg
.quality_profile
== k_quality_profile_low
)
860 state
->g_shadow_samples
= 2;
862 state
->g_shadow_samples
= 0;
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_other_entities( world_instance
*world
, vg_camera
*cam
){
892 bh_iter_init_range( 0, &it
, cam
->pos
, radius
+10.0f
);
898 while( bh_next( world
->entity_bh
, &it
, &idx
) ){
899 u32 id
= world
->entity_list
[ idx
],
900 type
= mdl_entity_id_type( id
),
901 index
= mdl_entity_id_id( id
);
903 if( type
== k_ent_glider
) {
904 if( glider_count
< vg_list_size(glider_list
) )
905 glider_list
[ glider_count
++ ] = index
;
909 shader_model_entity_use();
910 shader_model_entity_uTexMain( 0 );
911 shader_model_entity_uCamera( cam
->transform
[3] );
912 shader_model_entity_uPv( cam
->mtx
.pv
);
914 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, model_entity
);
916 for( u32 j
=0; j
<glider_count
; j
++ ){
917 ent_glider
*glider
= mdl_arritm( &world
->ent_glider
, glider_list
[j
] );
919 if( !(glider
->flags
& 0x1) )
923 mdl_transform_m4x3( &glider
->transform
, mdl
);
925 f32 dist
= v3_dist( glider
->transform
.co
, cam
->pos
) * (1.0f
/radius
),
926 scale
= vg_smoothstepf( vg_clampf( 5.0f
-dist
*5.0f
, 0.0f
,1.0f
) );
927 m3x3_scalef( mdl
, scale
);
929 render_glider_model( cam
, world
, mdl
, k_board_shader_entity
);
933 void render_world( world_instance
*world
, vg_camera
*cam
,
934 int stenciled
, int viewing_from_gate
,
935 int with_water
, int with_cubemaps
)
938 glClear( GL_DEPTH_BUFFER_BIT
);
939 glStencilFunc( GL_EQUAL
, 1, 0xFF );
940 glStencilMask( 0x00 );
941 glEnable( GL_CULL_FACE
);
942 glEnable( GL_STENCIL_TEST
);
945 glStencilMask( 0xFF );
946 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
947 glDisable( GL_STENCIL_TEST
);
950 render_sky( world
, cam
);
953 m4x3_identity(identity
);
954 render_world_routes( world
, world
, identity
, cam
, viewing_from_gate
, 0 );
955 render_world_standard( world
, cam
);
956 render_world_cubemapped( world
, cam
, with_cubemaps
);
958 render_world_vb( world
, cam
);
959 render_world_alphatest( world
, cam
);
960 render_world_foliage( world
, cam
);
961 render_terrain( world
, cam
);
963 if( !viewing_from_gate
){
964 world_entity_focus_render();
968 float min_dist
= INFINITY
;
970 if( mdl_arrcount( &world
->ent_route
) ){
971 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
972 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
973 float dist
= v3_dist2( route
->board_transform
[3], cam
->pos
);
975 if( dist
< min_dist
){
981 ent_route
*route
= mdl_arritm( &world
->ent_route
, closest
);
982 sfd_render( world
, cam
, route
->board_transform
);
986 if( !viewing_from_gate
){
988 if( mdl_entity_id_type(world_static
.focused_entity
) == k_ent_challenge
)
989 greyout
= world_static
.focus_strength
;
991 if( greyout
> 0.0f
){
992 glDrawBuffers( 1, (GLenum
[]){ GL_COLOR_ATTACHMENT0
} );
994 glDisable(GL_DEPTH_TEST
);
995 glDepthMask(GL_FALSE
);
996 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
997 glBlendEquation(GL_FUNC_ADD
);
999 shader_blitcolour_use();
1000 shader_blitcolour_uColour( (v4f
){ 0.5f
, 0.5f
, 0.5f
, greyout
*0.56f
} );
1003 glDisable(GL_BLEND
);
1004 glEnable(GL_DEPTH_TEST
);
1005 glDepthMask(GL_TRUE
);
1006 glDrawBuffers( 2, (GLenum
[]){ GL_COLOR_ATTACHMENT0
,
1007 GL_COLOR_ATTACHMENT1
} );
1010 render_world_fxglow( world
, world
, cam
, NULL
, 1, 1, 0 );
1014 render_water_texture( world
, cam
);
1015 render_fb_bind( gpipeline
.fb_main
, 1 );
1019 glStencilFunc( GL_EQUAL
, 1, 0xFF );
1020 glStencilMask( 0x00 );
1021 glEnable( GL_CULL_FACE
);
1022 glEnable( GL_STENCIL_TEST
);
1026 render_water_surface( world
, cam
);
1029 render_remote_players( world
, cam
);
1030 render_other_entities( world
, cam
);
1031 ent_miniworld_render( world
, cam
);
1034 glStencilMask( 0xFF );
1035 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
1036 glDisable( GL_STENCIL_TEST
);
1041 static void render_world_override_pass( world_instance
*world
,
1042 struct world_pass
*pass
,
1043 m4x3f mmdl
, m3x3f mnormal
,
1045 for( int i
=0; i
<world
->surface_count
; i
++ ){
1046 struct world_surface
*mat
= &world
->surfaces
[i
];
1047 if( mat
->info
.flags
& k_material_flag_ghosts
) continue;
1050 if( pass
->geo_type
== k_world_geo_type_solid
)
1053 sm
= &mat
->sm_no_collide
;
1055 if( !sm
->indice_count
)
1058 pass
->fn_set_mdl( mmdl
);
1059 pass
->fn_set_uNormalMtx( mnormal
);
1060 pass
->fn_set_uPvmPrev( mpvm_prev
);
1061 pass
->fn_bind_textures( world
, mat
);
1062 mdl_draw_submesh( sm
);
1066 void render_world_override( world_instance
*world
,
1067 world_instance
*lighting_source
,
1070 ent_spawn
*dest_spawn
, v4f map_info
)
1072 struct world_pass pass
= {
1074 .fn_bind_textures
= bindpoint_override
,
1075 .fn_set_mdl
= shader_scene_override_uMdl
,
1076 .fn_set_uPvmPrev
= shader_scene_override_uPvmPrev
,
1077 .fn_set_uNormalMtx
= shader_scene_override_uNormalMtx
,
1078 .shader
= k_shader_override
1081 shader_scene_override_use();
1082 shader_scene_override_uTexGarbage(0);
1083 shader_scene_override_uTexMain(1);
1084 shader_scene_override_uPv( pass
.cam
->mtx
.pv
);
1085 shader_scene_override_uMapInfo( map_info
);
1087 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( lighting_source
, scene_override
);
1088 bind_terrain_noise();
1090 shader_scene_override_uCamera( pass
.cam
->transform
[3] );
1093 m4x3_expand( mmdl
, mpvm_prev
);
1094 m4x4_mul( cam
->mtx_prev
.pv
, mpvm_prev
, mpvm_prev
);
1097 m3x3_inv( mmdl
, mnormal
);
1098 m3x3_transpose( mnormal
, mnormal
);
1099 v3_normalize( mnormal
[0] );
1100 v3_normalize( mnormal
[1] );
1101 v3_normalize( mnormal
[2] );
1103 v4f uPlayerPos
, uSpawnPos
;
1104 v4_zero( uPlayerPos
);
1105 v4_zero( uSpawnPos
);
1107 v3_copy( world
->player_co
, uPlayerPos
);
1109 if( dest_spawn
&& (v3_dist2(dest_spawn
->transform
.co
,uPlayerPos
) > 0.1f
) )
1110 v3_copy( dest_spawn
->transform
.co
, uSpawnPos
);
1112 v3_add( uPlayerPos
, (v3f
){0,-1,0}, uSpawnPos
);
1114 uPlayerPos
[3] = v3_dist(uPlayerPos
,uSpawnPos
);
1115 uSpawnPos
[3] = 1.0f
/uPlayerPos
[3];
1117 shader_scene_override_uPlayerPos( uPlayerPos
);
1118 shader_scene_override_uSpawnPos( uSpawnPos
);
1121 glDisable( GL_CULL_FACE
);
1122 mesh_bind( &world
->mesh_geo
);
1123 pass
.geo_type
= k_world_geo_type_solid
;
1124 render_world_override_pass( world
, &pass
, mmdl
, mnormal
, mpvm_prev
);
1125 mesh_bind( &world
->mesh_no_collide
);
1126 pass
.geo_type
= k_world_geo_type_nonsolid
;
1127 render_world_override_pass( world
, &pass
, mmdl
, mnormal
, mpvm_prev
);
1128 glEnable( GL_CULL_FACE
);
1130 render_world_fxglow( world
, world
, cam
, mmdl
, 0, 0, 1 );
1133 static void render_cubemap_side( world_instance
*world
, ent_cubemap
*cm
,
1136 glFramebufferTexture2D( GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
1137 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ side
, cm
->texture_id
, 0 );
1138 glClear( GL_DEPTH_BUFFER_BIT
);
1141 { -1.0f
, 0.0f
, 0.0f
},
1142 { 1.0f
, 0.0f
, 0.0f
},
1143 { 0.0f
, -1.0f
, 0.0f
},
1144 { 0.0f
, 1.0f
, 0.0f
},
1145 { 0.0f
, 0.0f
, -1.0f
},
1146 { 0.0f
, 0.0f
, 1.0f
}
1149 { 0.0f
, -1.0f
, 0.0f
},
1150 { 0.0f
, -1.0f
, 0.0f
},
1151 { 0.0f
, 0.0f
, 1.0f
},
1152 { 0.0f
, 0.0f
, -1.0f
},
1153 { 0.0f
, -1.0f
, 0.0f
},
1154 { 0.0f
, -1.0f
, 0.0f
}
1157 v3_zero( cam
.angles
);
1158 v3_copy( cm
->co
, cam
.pos
);
1160 v3_copy( forward
[side
], cam
.transform
[2] );
1161 v3_copy( up
[side
], cam
.transform
[1] );
1162 v3_cross( up
[side
], forward
[side
], cam
.transform
[0] );
1163 v3_copy( cm
->co
, cam
.transform
[3] );
1164 m4x3_invert_affine( cam
.transform
, cam
.transform_inverse
);
1166 vg_camera_update_view( &cam
);
1171 m4x4_copy( cam
.mtx
.p
, cam
.mtx_prev
.p
);
1172 m4x4_projection( cam
.mtx
.p
, cam
.fov
, 1.0f
, cam
.nearz
, cam
.farz
);
1173 vg_camera_finalize( &cam
);
1174 vg_camera_finalize( &cam
);
1176 render_world( world
, &cam
, 0, 1, 1, 0 );
1179 void render_world_cubemaps( world_instance
*world
)
1181 if( world
->cubemap_cooldown
)
1182 world
->cubemap_cooldown
--;
1184 world
->cubemap_cooldown
= 60;
1186 glViewport( 0, 0, WORLD_CUBEMAP_RES
, WORLD_CUBEMAP_RES
);
1187 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_cubemap
); i
++ ){
1188 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, i
);
1189 glBindFramebuffer( GL_FRAMEBUFFER
, cm
->framebuffer_id
);
1191 world
->cubemap_side
++;
1192 if( world
->cubemap_side
>= 6 )
1193 world
->cubemap_side
= 0;
1195 render_cubemap_side( world
, cm
, world
->cubemap_side
);
1202 * ---------------------------------------------
1205 void render_world_depth( world_instance
*world
, vg_camera
*cam
)
1207 m4x3f identity_matrix
;
1208 m4x3_identity( identity_matrix
);
1210 shader_scene_depth_use();
1211 shader_scene_depth_uCamera( cam
->transform
[3] );
1212 shader_scene_depth_uPv( cam
->mtx
.pv
);
1213 shader_scene_depth_uPvmPrev( cam
->mtx_prev
.pv
);
1214 shader_scene_depth_uMdl( identity_matrix
);
1215 world_link_lighting_ub( world
, _shader_scene_depth
.id
);
1217 mesh_bind( &world
->mesh_geo
);
1218 mesh_draw( &world
->mesh_geo
);
1221 void render_world_position( world_instance
*world
, vg_camera
*cam
)
1223 m4x3f identity_matrix
;
1224 m4x3_identity( identity_matrix
);
1226 shader_scene_position_use();
1227 shader_scene_position_uCamera( cam
->transform
[3] );
1228 shader_scene_position_uPv( cam
->mtx
.pv
);
1229 shader_scene_position_uPvmPrev( cam
->mtx_prev
.pv
);
1230 shader_scene_position_uMdl( identity_matrix
);
1231 world_link_lighting_ub( world
, _shader_scene_position
.id
);
1233 mesh_bind( &world
->mesh_geo
);
1234 mesh_draw( &world
->mesh_geo
);
1237 struct ui_enum_opt skybox_setting_options
[] = {
1238 { 0, "g_daysky_colour" },
1239 { 1, "g_nightsky_colour" },
1240 { 2, "g_sunset_colour" },
1241 { 3, "g_ambient_colour" },
1242 { 4, "g_sun_colour" },
1245 static f32
*skybox_prop_location( world_instance
*world
, i32 index
){
1247 case 0: return world
->ub_lighting
.g_daysky_colour
; break;
1248 case 1: return world
->ub_lighting
.g_nightsky_colour
; break;
1249 case 2: return world
->ub_lighting
.g_sunset_colour
; break;
1250 case 3: return world
->ub_lighting
.g_ambient_colour
; break;
1251 case 4: return world
->ub_lighting
.g_sun_colour
; break;
1252 default: return NULL
;
1256 void imgui_world_light_edit( world_instance
*world
)
1258 ui_rect panel
= { vg
.window_x
-400, 0, 400, vg
.window_y
};
1259 ui_fill( panel
, ui_colour( k_ui_bg
+1 ) );
1260 ui_outline( panel
, 1, ui_colour( k_ui_bg
+7 ), 0 );
1261 ui_rect_pad( panel
, (ui_px
[2]){ 8, 8 } );
1262 vg_ui
.wants_mouse
= 1;
1264 static i32 option_to_edit
= 0;
1265 ui_enum( panel
, "option", skybox_setting_options
, 5, &option_to_edit
);
1266 ui_colourpicker( panel
, "colour",
1267 skybox_prop_location( world
, option_to_edit
) );
1269 if( ui_button( panel
, "save tweaker file ('/tmp/tweaker.txt')\n" ) == 1 ){
1270 FILE *fp
= fopen( "/tmp/tweaker.txt", "w" );
1272 for( i32 i
=0; i
<5; i
++ ){
1273 struct ui_enum_opt
*opt
= &skybox_setting_options
[i
];
1274 f32
*val
= skybox_prop_location( world
, i
);
1275 fprintf( fp
, "%s = {%.3ff, %.3ff, %.3ff, %.3ff},\n",
1276 opt
->alias
, val
[0], val
[1], val
[2], val
[3] );