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"
14 #include "shaders/model_entity.h"
16 struct world_render world_render
;
18 static int ccmd_set_time( int argc
, const char *argv
[] ){
19 world_instance
*world
= world_current_instance();
21 world
->time
= atof( argv
[0] );
23 vg_error( "Usage set_time <0-1.0> (current time: %f)\n", world
->time
);
27 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 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_VAR_I32( k_light_editor
);
76 vg_console_reg_cmd( "set_time", ccmd_set_time
, NULL
);
78 world_render
.sky_rate
= 1.0;
79 world_render
.sky_target_rate
= 1.0;
81 vg_info( "Loading world resources\n" );
82 vg_linear_clear( vg_mem
.scratch
);
85 mdl_open( &msky
, "models/rs_skydome.mdl", vg_mem
.scratch
);
86 mdl_load_metadata_block( &msky
, vg_mem
.scratch
);
87 mdl_async_load_glmesh( &msky
, &world_render
.skydome
, NULL
);
90 vg_info( "Loading default world textures\n" );
91 vg_tex2d_load_qoi_async_file( "textures/garbage.qoi",
92 VG_TEX2D_NEAREST
|VG_TEX2D_REPEAT
,
93 &world_render
.tex_terrain_noise
);
95 vg_async_call( async_world_render_init
, NULL
, 0 );
99 * standard uniform bindings
100 * ----------------------------------------------------------------------------
102 void world_link_lighting_ub( world_instance
*world
, GLuint shader
)
104 GLuint idx
= glGetUniformBlockIndex( shader
, "ub_world_lighting" );
105 glUniformBlockBinding( shader
, idx
, world
->ubo_bind_point
);
108 void world_bind_position_texture( world_instance
*world
,
109 GLuint shader
, GLuint location
,
112 render_fb_bind_texture( &world
->heightmap
, 0, slot
);
113 glUniform1i( location
, slot
);
116 void world_bind_light_array( world_instance
*world
,
117 GLuint shader
, GLuint location
,
120 glActiveTexture( GL_TEXTURE0
+ slot
);
121 glBindTexture( GL_TEXTURE_BUFFER
, world
->tex_light_entities
);
122 glUniform1i( location
, slot
);
125 void world_bind_light_index( world_instance
*world
,
126 GLuint shader
, GLuint location
,
129 glActiveTexture( GL_TEXTURE0
+ slot
);
130 glBindTexture( GL_TEXTURE_3D
, world
->tex_light_cubes
);
131 glUniform1i( location
, slot
);
134 void bind_terrain_noise(void)
136 glActiveTexture( GL_TEXTURE0
);
137 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
141 * Get OpenGL texture name from texture ID.
143 static GLuint
world_get_texture( world_instance
*world
, u32 id
){
144 if( id
& 0x80000000 ) return skaterift
.rt_textures
[id
& ~0x80000000];
145 else return world
->textures
[ id
];
148 static void bindpoint_diffuse_texture1( world_instance
*world
,
149 struct world_surface
*mat
){
150 glActiveTexture( GL_TEXTURE1
);
151 glBindTexture( GL_TEXTURE_2D
,
152 world_get_texture(world
,mat
->info
.tex_diffuse
) );
157 * ----------------------------------------------------------------------------
162 enum mdl_shader shader
;
163 enum world_geo_type geo_type
;
165 void (*fn_bind_textures
)( world_instance
*world
,
166 struct world_surface
*mat
);
167 void (*fn_set_mdl
)( m4x3f mdl
);
168 void (*fn_set_uPvmPrev
)( m4x4f pvm
);
169 void (*fn_set_uNormalMtx
)( m3x3f mnorm
);
172 void render_world_depth( world_instance
*world
, vg_camera
*cam
);
175 * Render a run of submeshes, only of those which match material_id
177 static void world_render_submeshes( world_instance
*world
,
178 struct world_pass
*pass
,
179 mdl_transform
*transform
,
180 u32 start
, u32 count
, u32 material_id
){
181 for( u32 k
=0; k
<count
; k
++ ){
182 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
, start
+k
);
183 if( sm
->material_id
!= material_id
) continue;
186 mdl_transform_m4x3( transform
, mmdl
);
189 m4x3_expand( mmdl
, m4mdl
);
190 m4x4_mul( pass
->cam
->mtx_prev
.pv
, m4mdl
, m4mdl
);
192 pass
->fn_set_mdl( mmdl
);
193 pass
->fn_set_uPvmPrev( m4mdl
);
195 mdl_draw_submesh( sm
);
200 * Render props attached to this material
202 static void world_render_props( world_instance
*world
, u32 material_id
,
203 struct world_pass
*pass
){
204 struct world_surface
*mat
= &world
->surfaces
[ material_id
];
205 if( !(mat
->flags
& WORLD_SURFACE_HAS_PROPS
) ) return;
207 pass
->fn_bind_textures( world
, mat
);
209 for( u32 j
=0; j
<mdl_arrcount( &world
->ent_prop
); j
++ ){
210 ent_prop
*prop
= mdl_arritm( &world
->ent_prop
, j
);
211 if( prop
->flags
& 0x1 ) continue;
213 world_render_submeshes( world
, pass
, &prop
->transform
,
214 prop
->submesh_start
, prop
->submesh_count
, material_id
);
219 * Render traffic models attactched to this material
221 static void world_render_traffic( world_instance
*world
, u32 material_id
,
222 struct world_pass
*pass
){
223 struct world_surface
*mat
= &world
->surfaces
[ material_id
];
224 if( !(mat
->flags
& WORLD_SURFACE_HAS_TRAFFIC
) ) return;
226 pass
->fn_bind_textures( world
, mat
);
228 for( u32 j
=0; j
<mdl_arrcount( &world
->ent_traffic
); j
++ ){
229 ent_traffic
*traffic
= mdl_arritm( &world
->ent_traffic
, j
);
231 world_render_submeshes( world
, pass
, &traffic
->transform
,
232 traffic
->submesh_start
, traffic
->submesh_count
,
238 * Iterate and render all materials which match the passes shader and geometry
239 * type. Includes props/traffic.
241 static void world_render_pass( world_instance
*world
, struct world_pass
*pass
){
242 for( int i
=0; i
<world
->surface_count
; i
++ ){
243 struct world_surface
*mat
= &world
->surfaces
[i
];
245 if( mat
->info
.shader
== pass
->shader
){
248 if( pass
->geo_type
== k_world_geo_type_solid
){
252 world_render_traffic( world
, i
, pass
);
253 world_render_props( world
, i
, pass
);
254 sm
= &mat
->sm_no_collide
;
257 if( !sm
->indice_count
)
261 m4x3_identity( mmdl
);
262 pass
->fn_set_mdl( mmdl
);
263 pass
->fn_set_uPvmPrev( pass
->cam
->mtx_prev
.pv
);
265 pass
->fn_bind_textures( world
, mat
);
266 mdl_draw_submesh( sm
);
272 * Specific shader instructions
273 * ----------------------------------------------------------------------------
276 static void world_render_both_stages( world_instance
*world
,
277 struct world_pass
*pass
){
278 mesh_bind( &world
->mesh_geo
);
279 pass
->geo_type
= k_world_geo_type_solid
;
280 world_render_pass( world
, pass
);
282 glDisable( GL_CULL_FACE
);
283 mesh_bind( &world
->mesh_no_collide
);
284 pass
->geo_type
= k_world_geo_type_nonsolid
;
285 world_render_pass( world
, pass
);
286 glEnable( GL_CULL_FACE
);
289 static void render_world_vb( world_instance
*world
, vg_camera
*cam
){
290 shader_scene_vertex_blend_use();
291 shader_scene_vertex_blend_uTexGarbage(0);
292 shader_scene_vertex_blend_uTexGradients(1);
293 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_vertex_blend
);
295 glActiveTexture( GL_TEXTURE0
);
296 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
298 shader_scene_vertex_blend_uPv( cam
->mtx
.pv
);
299 shader_scene_vertex_blend_uCamera( cam
->transform
[3] );
301 struct world_pass pass
= {
302 .shader
= k_shader_standard_vertex_blend
,
304 .fn_bind_textures
= bindpoint_diffuse_texture1
,
305 .fn_set_mdl
= shader_scene_vertex_blend_uMdl
,
306 .fn_set_uPvmPrev
= shader_scene_vertex_blend_uPvmPrev
,
309 world_render_both_stages( world
, &pass
);
312 static void world_shader_standard_bind( world_instance
*world
, vg_camera
*cam
){
313 shader_scene_standard_use();
314 shader_scene_standard_uTexGarbage(0);
315 shader_scene_standard_uTexMain(1);
316 shader_scene_standard_uPv( cam
->mtx
.pv
);
317 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_standard
);
319 bind_terrain_noise();
320 shader_scene_standard_uCamera( cam
->transform
[3] );
323 static void render_world_standard( world_instance
*world
, vg_camera
*cam
){
324 world_shader_standard_bind( world
, cam
);
325 struct world_pass pass
= {
326 .shader
= k_shader_standard
,
328 .fn_bind_textures
= bindpoint_diffuse_texture1
,
329 .fn_set_mdl
= shader_scene_standard_uMdl
,
330 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
333 world_render_both_stages( world
, &pass
);
336 static void bindpoint_diffuse1_and_cubemap10( world_instance
*world
,
337 struct world_surface
*mat
){
338 glActiveTexture( GL_TEXTURE1
);
339 glBindTexture( GL_TEXTURE_2D
,
340 world_get_texture(world
,mat
->info
.tex_diffuse
) );
342 u32 cubemap_id
= mat
->info
.tex_none0
,
345 if( mdl_entity_id_type( cubemap_id
) == k_ent_cubemap
){
346 cubemap_index
= mdl_entity_id_id( cubemap_id
);
349 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, cubemap_index
);
350 glActiveTexture( GL_TEXTURE10
);
351 glBindTexture( GL_TEXTURE_CUBE_MAP
, cm
->texture_id
);
353 shader_scene_cubemapped_uColour( mat
->info
.colour
);
356 static void render_world_cubemapped( world_instance
*world
, vg_camera
*cam
,
358 if( !mdl_arrcount( &world
->ent_cubemap
) )
362 world_shader_standard_bind( world
, cam
);
364 struct world_pass pass
= {
365 .shader
= k_shader_cubemap
,
367 .fn_bind_textures
= bindpoint_diffuse_texture1
,
368 .fn_set_mdl
= shader_scene_standard_uMdl
,
369 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
372 world_render_both_stages( world
, &pass
);
375 shader_scene_cubemapped_use();
376 shader_scene_cubemapped_uTexGarbage(0);
377 shader_scene_cubemapped_uTexMain(1);
378 shader_scene_cubemapped_uTexCubemap(10);
379 shader_scene_cubemapped_uPv( cam
->mtx
.pv
);
381 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_cubemapped
);
383 bind_terrain_noise();
384 shader_scene_cubemapped_uCamera( cam
->transform
[3] );
386 struct world_pass pass
= {
387 .shader
= k_shader_cubemap
,
389 .fn_bind_textures
= bindpoint_diffuse1_and_cubemap10
,
390 .fn_set_mdl
= shader_scene_cubemapped_uMdl
,
391 .fn_set_uPvmPrev
= shader_scene_cubemapped_uPvmPrev
,
394 world_render_both_stages( world
, &pass
);
398 static void render_world_alphatest( world_instance
*world
, vg_camera
*cam
){
399 shader_scene_standard_alphatest_use();
400 shader_scene_standard_alphatest_uTexGarbage(0);
401 shader_scene_standard_alphatest_uTexMain(1);
402 shader_scene_standard_alphatest_uPv( cam
->mtx
.pv
);
404 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_standard_alphatest
);
406 bind_terrain_noise();
407 shader_scene_standard_alphatest_uCamera( cam
->transform
[3] );
408 glDisable(GL_CULL_FACE
);
410 struct world_pass pass
= {
411 .shader
= k_shader_standard_cutout
,
413 .fn_bind_textures
= bindpoint_diffuse_texture1
,
414 .fn_set_mdl
= shader_scene_standard_alphatest_uMdl
,
415 .fn_set_uPvmPrev
= shader_scene_standard_alphatest_uPvmPrev
,
418 world_render_both_stages( world
, &pass
);
419 glEnable(GL_CULL_FACE
);
422 static void render_world_foliage( world_instance
*world
, vg_camera
*cam
){
423 shader_scene_foliage_use();
424 shader_scene_foliage_uTexGarbage(0);
425 shader_scene_foliage_uTexMain(1);
426 shader_scene_foliage_uPv( cam
->mtx
.pv
);
427 shader_scene_foliage_uTime( vg
.time
);
429 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_foliage
);
430 bind_terrain_noise();
432 shader_scene_foliage_uCamera( cam
->transform
[3] );
433 glDisable(GL_CULL_FACE
);
434 struct world_pass pass
= {
435 .shader
= k_shader_foliage
,
437 .fn_bind_textures
= bindpoint_diffuse_texture1
,
438 .fn_set_mdl
= shader_scene_foliage_uMdl
,
439 .fn_set_uPvmPrev
= shader_scene_foliage_uPvmPrev
,
441 world_render_both_stages( world
, &pass
);
442 glEnable(GL_CULL_FACE
);
445 static void world_render_challenges( world_instance
*world
,
446 struct world_pass
*pass
, v3f pos
){
448 if( skaterift
.activity
== k_skaterift_replay
) return;
449 if( world
!= world_current_instance() ) return;
454 u32 objective_list
[ 32 ],
455 challenge_list
[ 16 ];
457 v2f objective_uv_offsets
[ 32 ];
459 u32 objective_count
= 0,
462 ent_challenge
*active_challenge
= NULL
;
464 if( mdl_entity_id_type( world_static
.focused_entity
) == k_ent_challenge
){
465 if( (skaterift
.activity
== k_skaterift_default
) &&
466 world_static
.challenge_target
){
470 if( !((skaterift
.activity
!= k_skaterift_ent_focus
) &&
471 !world_static
.challenge_target
) ){
472 world_instance
*challenge_world
= world_current_instance();
473 u32 index
= mdl_entity_id_id( world_static
.focused_entity
);
474 active_challenge
= mdl_arritm(&challenge_world
->ent_challenge
, index
);
478 if( active_challenge
){
479 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
480 challenge_list
[ challenge_count
++ ] = world_static
.focused_entity
;
482 u32 next
= active_challenge
->first
;
483 while( mdl_entity_id_type(next
) == k_ent_objective
){
484 u32 index
= mdl_entity_id_id( next
);
485 objective_list
[ objective_count
++ ] = index
;
487 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
488 next
= objective
->id_next
;
494 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
496 bh_iter_init_range( 0, &it
, pos
, radius
+10.0f
);
498 while( bh_next( world
->entity_bh
, &it
, &idx
) ){
499 u32 id
= world
->entity_list
[ idx
],
500 type
= mdl_entity_id_type( id
),
501 index
= mdl_entity_id_id( id
);
503 if( type
== k_ent_objective
) {
504 if( objective_count
< vg_list_size(objective_list
) )
505 objective_list
[ objective_count
++ ] = index
;
507 else if( type
== k_ent_challenge
){
508 if( challenge_count
< vg_list_size(challenge_list
) )
509 challenge_list
[ challenge_count
++ ] = index
;
514 /* render objectives */
515 glDisable( GL_CULL_FACE
);
516 mesh_bind( &world
->mesh_no_collide
);
517 u32 last_material
= 0;
518 for( u32 i
=0; i
<objective_count
; i
++ ){
519 u32 index
= objective_list
[ i
];
520 ent_objective
*objective
= mdl_arritm( &world
->ent_objective
, index
);
521 if( (objective
->flags
& k_ent_objective_hidden
) &&
522 !active_challenge
) continue;
527 u32 passed
= objective
->flags
& k_ent_objective_passed
;
528 f32 target
= passed
? 0.0f
: 1.0f
;
529 vg_slewf(&objective
->transform
.s
[0], target
, vg
.time_frame_delta
*4.0f
);
530 scale
= vg_smoothstepf( objective
->transform
.s
[0] );
532 if( (objective
== world_static
.challenge_target
) || passed
)
533 shader_scene_fxglow_uUvOffset( (v2f
){ 16.0f
/256.0f
, 0.0f
} );
535 shader_scene_fxglow_uUvOffset( (v2f
){ 8.0f
/256.0f
, 0.0f
} );
538 f32 dist
= v3_dist( objective
->transform
.co
, pos
) * (1.0f
/radius
);
539 scale
= vg_smoothstepf( vg_clampf( 5.0f
-dist
*5.0f
, 0.0f
,1.0f
) );
543 q_m3x3( objective
->transform
.q
, mmdl
);
544 m3x3_scalef( mmdl
, scale
);
545 v3_copy( objective
->transform
.co
, mmdl
[3] );
546 shader_scene_fxglow_uMdl( mmdl
);
548 for( u32 j
=0; j
<objective
->submesh_count
; j
++ ){
549 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
550 objective
->submesh_start
+ j
);
552 if( sm
->material_id
!= last_material
){
553 last_material
= sm
->material_id
;
554 pass
->fn_bind_textures( world
, &world
->surfaces
[sm
->material_id
] );
556 mdl_draw_submesh( sm
);
561 font3d_bind( &gui
.font
, k_font_shader_world
, 0, world
, &skaterift
.cam
);
565 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_challenge
); i
++ )
567 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, i
);
568 if( challenge
->status
) count
++;
573 vg_strnull( &str
, buf
, sizeof(buf
) );
574 vg_strcati32( &str
, count
);
575 vg_strcatch( &str
, '/' );
576 vg_strcati32( &str
, mdl_arrcount(&world
->ent_challenge
) );
578 f32 w
= font3d_string_width( 1, buf
);
580 m3x3_identity( mlocal
);
581 mlocal
[3][0] = -w
*0.5f
;
585 for( u32 i
=0; i
<challenge_count
; i
++ )
587 u32 index
= challenge_list
[ i
];
588 ent_challenge
*challenge
= mdl_arritm( &world
->ent_challenge
, index
);
590 mdl_transform_m4x3( &challenge
->transform
, mmdl
);
591 m4x3_mul( mmdl
, mlocal
, mmdl
);
593 vg_line_point( challenge
->transform
.co
, 0.25f
, VG__RED
);
595 f32 dist
= v3_dist( challenge
->transform
.co
, pos
) * (1.0f
/radius
),
596 scale
= vg_smoothstepf( vg_clampf( 10.0f
-dist
*10.0f
, 0.0f
,1.0f
) ),
599 if( challenge
->status
)
602 shader_scene_font_uOpacity( scale
);
603 shader_scene_font_uColourize( colour
);
604 font3d_simple_draw( 1, buf
, &skaterift
.cam
, mmdl
);
608 static void render_world_fxglow( world_instance
*host_world
,
609 world_instance
*world
, vg_camera
*cam
,
611 int generic
, int challenges
, int regions
){
612 shader_scene_fxglow_use();
613 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
614 shader_scene_fxglow_uTexMain(1);
615 shader_scene_fxglow_uPv( cam
->mtx
.pv
);
616 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_fxglow
);
618 shader_scene_fxglow_uCamera( cam
->transform
[3] );
619 glDisable(GL_CULL_FACE
);
621 struct world_pass pass
= {
622 .shader
= k_shader_fxglow
,
624 .fn_bind_textures
= bindpoint_diffuse_texture1
,
625 .fn_set_mdl
= shader_scene_fxglow_uMdl
,
626 .fn_set_uPvmPrev
= shader_scene_fxglow_uPvmPrev
,
630 world_render_both_stages( world
, &pass
);
633 mesh_bind( &world
->mesh_no_collide
);
635 u32 last_material
= 0;
636 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_region
); i
++ ){
637 shader_scene_fxglow_uUvOffset( (v2f
){ 0.0f
, 0.0f
} );
638 ent_region
*region
= mdl_arritm( &world
->ent_region
, i
);
641 if( region
->flags
& k_ent_route_flag_achieve_gold
)
643 else if( region
->flags
& k_ent_route_flag_achieve_silver
)
646 shader_scene_fxglow_uUvOffset( (v2f
){ (8.0f
/256.0f
)*offset
, 0.0f
} );
649 mdl_transform_m4x3( ®ion
->transform
, mmdl
);
650 m4x3_mul( world_mmdl
, mmdl
, mmdl
);
651 shader_scene_fxglow_uMdl( mmdl
);
653 for( u32 j
=0; j
<region
->submesh_count
; j
++ ){
654 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
655 region
->submesh_start
+ j
);
657 if( sm
->material_id
!= last_material
){
658 last_material
= sm
->material_id
;
659 pass
.fn_bind_textures(world
,&world
->surfaces
[sm
->material_id
]);
661 mdl_draw_submesh( sm
);
667 world_render_challenges( world
, &pass
, cam
->pos
);
669 glEnable(GL_CULL_FACE
);
672 static void bindpoint_terrain( world_instance
*world
,
673 struct world_surface
*mat
)
675 glActiveTexture( GL_TEXTURE1
);
676 glBindTexture( GL_TEXTURE_2D
,
677 world_get_texture(world
,mat
->info
.tex_diffuse
) );
679 shader_scene_terrain_uSandColour( mat
->info
.colour
);
680 shader_scene_terrain_uBlendOffset( mat
->info
.colour1
);
683 static void bindpoint_override( world_instance
*world
,
684 struct world_surface
*mat
){
685 if( mat
->info
.flags
& k_material_flag_collision
){
686 shader_scene_override_uAlphatest(0);
689 glActiveTexture( GL_TEXTURE1
);
690 glBindTexture( GL_TEXTURE_2D
,
691 world_get_texture(world
,mat
->info
.tex_diffuse
) );
692 shader_scene_override_uAlphatest(1);
696 static void render_terrain( world_instance
*world
, vg_camera
*cam
){
697 shader_scene_terrain_use();
698 shader_scene_terrain_uTexGarbage(0);
699 shader_scene_terrain_uTexGradients(1);
701 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, scene_terrain
);
702 glActiveTexture( GL_TEXTURE0
);
703 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
705 shader_scene_terrain_uPv( cam
->mtx
.pv
);
706 shader_scene_terrain_uCamera( cam
->transform
[3] );
708 struct world_pass pass
= {
709 .shader
= k_shader_terrain_blend
,
711 .fn_bind_textures
= bindpoint_terrain
,
712 .fn_set_mdl
= shader_scene_terrain_uMdl
,
713 .fn_set_uPvmPrev
= shader_scene_terrain_uPvmPrev
,
716 world_render_both_stages( world
, &pass
);
719 static void render_sky( world_instance
*world
, vg_camera
*cam
){
721 * Modify matrix to remove clipping and view translation
728 m4x4_copy( cam
->mtx
.v
, v
);
729 m4x4_copy( cam
->mtx_prev
.v
, v_prev
);
731 for( int i
=0; i
<3; i
++ ){
733 v3_normalize(v_prev
[i
]);
736 v3_zero( v_prev
[3] );
738 m4x4_copy( cam
->mtx
.p
, pv
);
739 m4x4_copy( cam
->mtx_prev
.p
, pv_prev
);
740 m4x4_reset_clipping( pv
, 100.0f
, 0.1f
);
741 m4x4_reset_clipping( pv_prev
, 100.0f
, 0.1f
);
743 m4x4_mul( pv
, v
, pv
);
744 m4x4_mul( pv_prev
, v_prev
, pv_prev
);
746 m4x3f identity_matrix
;
747 m4x3_identity( identity_matrix
);
752 if( world
->skybox
== k_skybox_default
){
753 shader_model_sky_use();
754 shader_model_sky_uMdl( identity_matrix
);
755 shader_model_sky_uPv( pv
);
756 shader_model_sky_uPvmPrev( pv_prev
);
757 shader_model_sky_uTexGarbage(0);
758 world_link_lighting_ub( world
, _shader_model_sky
.id
);
760 glActiveTexture( GL_TEXTURE0
);
761 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
763 else if( world
->skybox
== k_skybox_space
){
764 shader_model_sky_space_use();
766 shader_model_sky_space_uMdl( identity_matrix
);
767 shader_model_sky_space_uPv( pv
);
768 shader_model_sky_space_uPvmPrev( pv_prev
);
769 shader_model_sky_space_uTexGarbage(0);
770 world_link_lighting_ub( world
, _shader_model_sky_space
.id
);
772 glActiveTexture( GL_TEXTURE0
);
773 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
776 vg_fatal_error( "Programming error\n" );
779 glDepthMask( GL_FALSE
);
780 glDisable( GL_DEPTH_TEST
);
782 mesh_bind( &world_render
.skydome
);
783 mesh_draw( &world_render
.skydome
);
785 glEnable( GL_DEPTH_TEST
);
786 glDepthMask( GL_TRUE
);
789 void render_world_gates( world_instance
*world
, vg_camera
*cam
)
791 float closest
= INFINITY
;
792 struct ent_gate
*gate
= NULL
;
794 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
795 ent_gate
*gi
= mdl_arritm( &world
->ent_gate
, i
);
797 if( !(gi
->flags
& k_ent_gate_nonlocal
) )
798 if( !(gi
->flags
& k_ent_gate_linked
) )
801 float dist
= v3_dist2( gi
->co
[0], cam
->transform
[3] );
803 vg_line_point( gi
->co
[0], 0.25f
, VG__BLUE
);
805 if( dist
< closest
){
811 world
->rendering_gate
= gate
;
814 if( gate
->flags
& k_ent_gate_locked
){
815 world
->rendering_gate
= NULL
;
819 if( gate
->flags
& k_ent_gate_nonlocal
){
820 if( !(gate
->flags
& k_ent_gate_linked
) ||
821 (world_static
.load_state
!= k_world_loader_none
) ){
822 world
->rendering_gate
= NULL
;
823 render_gate_unlinked( world
, gate
, cam
);
827 world_instance
*dest_world
= &world_static
.instances
[ gate
->target
];
828 render_gate( world
, dest_world
, gate
, cam
);
831 render_gate( world
, world
, gate
, cam
);
835 void world_prerender( world_instance
*world
)
837 if( mdl_arrcount( &world
->ent_light
) ){
838 f32 rate
= vg_maxf(0.1f
, fabsf(k_day_length
)) * vg_signf(k_day_length
);
839 world
->time
+= vg
.time_frame_delta
* (1.0/(rate
*60.0));
845 if( world
->info
.flags
& 0x1 ){
846 world
->time
= world
->info
.timezone
;
849 struct ub_world_lighting
*state
= &world
->ub_lighting
;
851 state
->g_time
= world
->time
;
852 state
->g_realtime
= vg
.time_real
;
853 state
->g_debug_indices
= k_debug_light_indices
;
854 state
->g_light_preview
= k_light_preview
;
855 state
->g_debug_complexity
= k_debug_light_complexity
;
856 state
->g_time_of_day
= vg_fractf( world
->time
);
858 if( vg
.quality_profile
== k_quality_profile_high
)
859 state
->g_shadow_samples
= 8;
860 else if( vg
.quality_profile
== k_quality_profile_low
)
861 state
->g_shadow_samples
= 2;
863 state
->g_shadow_samples
= 0;
865 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
866 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
868 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
869 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
871 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
872 state
->g_sun_dir
[0] = sinf( a
);
873 state
->g_sun_dir
[1] = cosf( a
);
874 state
->g_sun_dir
[2] = 0.2f
;
875 v3_normalize( state
->g_sun_dir
);
877 world
->probabilities
[ k_probability_curve_constant
] = 1.0f
;
878 float dp
= state
->g_day_phase
;
880 world
->probabilities
[ k_probability_curve_wildlife_day
] =
881 (dp
*dp
*0.8f
+state
->g_sunset_phase
)*0.8f
;
882 world
->probabilities
[ k_probability_curve_wildlife_night
] =
883 1.0f
-powf(fabsf((state
->g_time_of_day
-0.5f
)*5.0f
),5.0f
);
885 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
886 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
887 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
890 static void render_other_entities( world_instance
*world
, vg_camera
*cam
)
894 bh_iter_init_range( 0, &it
, cam
->pos
, radius
+10.0f
);
902 while( bh_next( world
->entity_bh
, &it
, &idx
) ){
903 u32 id
= world
->entity_list
[ idx
],
904 type
= mdl_entity_id_type( id
),
905 index
= mdl_entity_id_id( id
);
907 if( type
== k_ent_glider
)
909 if( glider_count
< vg_list_size(glider_list
) )
910 glider_list
[ glider_count
++ ] = index
;
912 else if( type
== k_ent_npc
)
914 if( npc_count
< vg_list_size(npc_list
) )
915 npc_list
[ npc_count
++ ] = index
;
919 shader_model_entity_use();
920 shader_model_entity_uTexMain( 0 );
921 shader_model_entity_uCamera( cam
->transform
[3] );
922 shader_model_entity_uPv( cam
->mtx
.pv
);
923 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, model_entity
);
925 for( u32 j
=0; j
<glider_count
; j
++ )
927 ent_glider
*glider
= mdl_arritm( &world
->ent_glider
, glider_list
[j
] );
929 if( !(glider
->flags
& 0x1) )
933 mdl_transform_m4x3( &glider
->transform
, mdl
);
935 f32 dist
= v3_dist( glider
->transform
.co
, cam
->pos
) * (1.0f
/radius
),
936 scale
= vg_smoothstepf( vg_clampf( 5.0f
-dist
*5.0f
, 0.0f
,1.0f
) );
937 m3x3_scalef( mdl
, scale
);
939 render_glider_model( cam
, world
, mdl
, k_board_shader_entity
);
942 for( u32 j
=0; j
<npc_count
; j
++ )
944 u32 index
= npc_list
[j
];
945 ent_npc
*npc
= mdl_arritm( &world
->ent_npc
, npc_list
[j
] );
947 npc_render( npc
, world
, cam
);
951 void render_world( world_instance
*world
, vg_camera
*cam
,
952 int stenciled
, int viewing_from_gate
,
953 int with_water
, int with_cubemaps
)
956 glClear( GL_DEPTH_BUFFER_BIT
);
957 glStencilFunc( GL_EQUAL
, 1, 0xFF );
958 glStencilMask( 0x00 );
959 glEnable( GL_CULL_FACE
);
960 glEnable( GL_STENCIL_TEST
);
963 glStencilMask( 0xFF );
964 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
965 glDisable( GL_STENCIL_TEST
);
968 render_sky( world
, cam
);
971 m4x3_identity(identity
);
972 render_world_routes( world
, world
, identity
, cam
, viewing_from_gate
, 0 );
973 render_world_standard( world
, cam
);
974 render_world_cubemapped( world
, cam
, with_cubemaps
);
976 render_world_vb( world
, cam
);
977 render_world_alphatest( world
, cam
);
978 render_world_foliage( world
, cam
);
979 render_terrain( world
, cam
);
981 if( !viewing_from_gate
){
982 world_entity_focus_render();
986 float min_dist
= INFINITY
;
988 if( mdl_arrcount( &world
->ent_route
) ){
989 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
990 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
991 float dist
= v3_dist2( route
->board_transform
[3], cam
->pos
);
993 if( dist
< min_dist
){
999 ent_route
*route
= mdl_arritm( &world
->ent_route
, closest
);
1000 sfd_render( world
, cam
, route
->board_transform
);
1004 if( !viewing_from_gate
){
1006 if( mdl_entity_id_type(world_static
.focused_entity
) == k_ent_challenge
)
1007 greyout
= world_static
.focus_strength
;
1009 if( greyout
> 0.0f
){
1010 glDrawBuffers( 1, (GLenum
[]){ GL_COLOR_ATTACHMENT0
} );
1012 glDisable(GL_DEPTH_TEST
);
1013 glDepthMask(GL_FALSE
);
1014 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
1015 glBlendEquation(GL_FUNC_ADD
);
1017 shader_blitcolour_use();
1018 shader_blitcolour_uColour( (v4f
){ 0.5f
, 0.5f
, 0.5f
, greyout
*0.56f
} );
1021 glDisable(GL_BLEND
);
1022 glEnable(GL_DEPTH_TEST
);
1023 glDepthMask(GL_TRUE
);
1024 glDrawBuffers( 2, (GLenum
[]){ GL_COLOR_ATTACHMENT0
,
1025 GL_COLOR_ATTACHMENT1
} );
1028 render_world_fxglow( world
, world
, cam
, NULL
, 1, 1, 0 );
1032 render_water_texture( world
, cam
);
1033 render_fb_bind( gpipeline
.fb_main
, 1 );
1037 glStencilFunc( GL_EQUAL
, 1, 0xFF );
1038 glStencilMask( 0x00 );
1039 glEnable( GL_CULL_FACE
);
1040 glEnable( GL_STENCIL_TEST
);
1044 render_water_surface( world
, cam
);
1047 render_remote_players( world
, cam
);
1048 render_other_entities( world
, cam
);
1049 ent_miniworld_render( world
, cam
);
1052 glStencilMask( 0xFF );
1053 glStencilFunc( GL_ALWAYS
, 1, 0xFF );
1054 glDisable( GL_STENCIL_TEST
);
1059 static void render_world_override_pass( world_instance
*world
,
1060 struct world_pass
*pass
,
1061 m4x3f mmdl
, m3x3f mnormal
,
1063 for( int i
=0; i
<world
->surface_count
; i
++ ){
1064 struct world_surface
*mat
= &world
->surfaces
[i
];
1065 if( mat
->info
.flags
& k_material_flag_ghosts
) continue;
1068 if( pass
->geo_type
== k_world_geo_type_solid
)
1071 sm
= &mat
->sm_no_collide
;
1073 if( !sm
->indice_count
)
1076 pass
->fn_set_mdl( mmdl
);
1077 pass
->fn_set_uNormalMtx( mnormal
);
1078 pass
->fn_set_uPvmPrev( mpvm_prev
);
1079 pass
->fn_bind_textures( world
, mat
);
1080 mdl_draw_submesh( sm
);
1084 void render_world_override( world_instance
*world
,
1085 world_instance
*lighting_source
,
1088 ent_spawn
*dest_spawn
, v4f map_info
)
1090 struct world_pass pass
= {
1092 .fn_bind_textures
= bindpoint_override
,
1093 .fn_set_mdl
= shader_scene_override_uMdl
,
1094 .fn_set_uPvmPrev
= shader_scene_override_uPvmPrev
,
1095 .fn_set_uNormalMtx
= shader_scene_override_uNormalMtx
,
1096 .shader
= k_shader_override
1099 shader_scene_override_use();
1100 shader_scene_override_uTexGarbage(0);
1101 shader_scene_override_uTexMain(1);
1102 shader_scene_override_uPv( pass
.cam
->mtx
.pv
);
1103 shader_scene_override_uMapInfo( map_info
);
1105 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( lighting_source
, scene_override
);
1106 bind_terrain_noise();
1108 shader_scene_override_uCamera( pass
.cam
->transform
[3] );
1111 m4x3_expand( mmdl
, mpvm_prev
);
1112 m4x4_mul( cam
->mtx_prev
.pv
, mpvm_prev
, mpvm_prev
);
1115 m3x3_inv( mmdl
, mnormal
);
1116 m3x3_transpose( mnormal
, mnormal
);
1117 v3_normalize( mnormal
[0] );
1118 v3_normalize( mnormal
[1] );
1119 v3_normalize( mnormal
[2] );
1121 v4f uPlayerPos
, uSpawnPos
;
1122 v4_zero( uPlayerPos
);
1123 v4_zero( uSpawnPos
);
1125 v3_copy( world
->player_co
, uPlayerPos
);
1127 if( dest_spawn
&& (v3_dist2(dest_spawn
->transform
.co
,uPlayerPos
) > 0.1f
) )
1128 v3_copy( dest_spawn
->transform
.co
, uSpawnPos
);
1130 v3_add( uPlayerPos
, (v3f
){0,-1,0}, uSpawnPos
);
1132 uPlayerPos
[3] = v3_dist(uPlayerPos
,uSpawnPos
);
1133 uSpawnPos
[3] = 1.0f
/uPlayerPos
[3];
1135 shader_scene_override_uPlayerPos( uPlayerPos
);
1136 shader_scene_override_uSpawnPos( uSpawnPos
);
1139 glDisable( GL_CULL_FACE
);
1140 mesh_bind( &world
->mesh_geo
);
1141 pass
.geo_type
= k_world_geo_type_solid
;
1142 render_world_override_pass( world
, &pass
, mmdl
, mnormal
, mpvm_prev
);
1143 mesh_bind( &world
->mesh_no_collide
);
1144 pass
.geo_type
= k_world_geo_type_nonsolid
;
1145 render_world_override_pass( world
, &pass
, mmdl
, mnormal
, mpvm_prev
);
1146 glEnable( GL_CULL_FACE
);
1148 render_world_fxglow( world
, world
, cam
, mmdl
, 0, 0, 1 );
1151 static void render_cubemap_side( world_instance
*world
, ent_cubemap
*cm
,
1154 glFramebufferTexture2D( GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
1155 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ side
, cm
->texture_id
, 0 );
1156 glClear( GL_DEPTH_BUFFER_BIT
);
1159 { -1.0f
, 0.0f
, 0.0f
},
1160 { 1.0f
, 0.0f
, 0.0f
},
1161 { 0.0f
, -1.0f
, 0.0f
},
1162 { 0.0f
, 1.0f
, 0.0f
},
1163 { 0.0f
, 0.0f
, -1.0f
},
1164 { 0.0f
, 0.0f
, 1.0f
}
1167 { 0.0f
, -1.0f
, 0.0f
},
1168 { 0.0f
, -1.0f
, 0.0f
},
1169 { 0.0f
, 0.0f
, 1.0f
},
1170 { 0.0f
, 0.0f
, -1.0f
},
1171 { 0.0f
, -1.0f
, 0.0f
},
1172 { 0.0f
, -1.0f
, 0.0f
}
1175 v3_zero( cam
.angles
);
1176 v3_copy( cm
->co
, cam
.pos
);
1178 v3_copy( forward
[side
], cam
.transform
[2] );
1179 v3_copy( up
[side
], cam
.transform
[1] );
1180 v3_cross( up
[side
], forward
[side
], cam
.transform
[0] );
1181 v3_copy( cm
->co
, cam
.transform
[3] );
1182 m4x3_invert_affine( cam
.transform
, cam
.transform_inverse
);
1184 vg_camera_update_view( &cam
);
1189 m4x4_copy( cam
.mtx
.p
, cam
.mtx_prev
.p
);
1190 m4x4_projection( cam
.mtx
.p
, cam
.fov
, 1.0f
, cam
.nearz
, cam
.farz
);
1191 vg_camera_finalize( &cam
);
1192 vg_camera_finalize( &cam
);
1194 render_world( world
, &cam
, 0, 1, 1, 0 );
1197 void render_world_cubemaps( world_instance
*world
)
1199 if( world
->cubemap_cooldown
)
1200 world
->cubemap_cooldown
--;
1202 world
->cubemap_cooldown
= 60;
1204 glViewport( 0, 0, WORLD_CUBEMAP_RES
, WORLD_CUBEMAP_RES
);
1205 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_cubemap
); i
++ ){
1206 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, i
);
1207 glBindFramebuffer( GL_FRAMEBUFFER
, cm
->framebuffer_id
);
1209 world
->cubemap_side
++;
1210 if( world
->cubemap_side
>= 6 )
1211 world
->cubemap_side
= 0;
1213 render_cubemap_side( world
, cm
, world
->cubemap_side
);
1220 * ---------------------------------------------
1223 void render_world_depth( world_instance
*world
, vg_camera
*cam
)
1225 m4x3f identity_matrix
;
1226 m4x3_identity( identity_matrix
);
1228 shader_scene_depth_use();
1229 shader_scene_depth_uCamera( cam
->transform
[3] );
1230 shader_scene_depth_uPv( cam
->mtx
.pv
);
1231 shader_scene_depth_uPvmPrev( cam
->mtx_prev
.pv
);
1232 shader_scene_depth_uMdl( identity_matrix
);
1233 world_link_lighting_ub( world
, _shader_scene_depth
.id
);
1235 mesh_bind( &world
->mesh_geo
);
1236 mesh_draw( &world
->mesh_geo
);
1239 void render_world_position( world_instance
*world
, vg_camera
*cam
)
1241 m4x3f identity_matrix
;
1242 m4x3_identity( identity_matrix
);
1244 shader_scene_position_use();
1245 shader_scene_position_uCamera( cam
->transform
[3] );
1246 shader_scene_position_uPv( cam
->mtx
.pv
);
1247 shader_scene_position_uPvmPrev( cam
->mtx_prev
.pv
);
1248 shader_scene_position_uMdl( identity_matrix
);
1249 world_link_lighting_ub( world
, _shader_scene_position
.id
);
1251 mesh_bind( &world
->mesh_geo
);
1252 mesh_draw( &world
->mesh_geo
);
1255 struct ui_enum_opt skybox_setting_options
[] = {
1256 { 0, "g_daysky_colour" },
1257 { 1, "g_nightsky_colour" },
1258 { 2, "g_sunset_colour" },
1259 { 3, "g_ambient_colour" },
1260 { 4, "g_sun_colour" },
1263 static f32
*skybox_prop_location( world_instance
*world
, i32 index
){
1265 case 0: return world
->ub_lighting
.g_daysky_colour
; break;
1266 case 1: return world
->ub_lighting
.g_nightsky_colour
; break;
1267 case 2: return world
->ub_lighting
.g_sunset_colour
; break;
1268 case 3: return world
->ub_lighting
.g_ambient_colour
; break;
1269 case 4: return world
->ub_lighting
.g_sun_colour
; break;
1270 default: return NULL
;
1274 void imgui_world_light_edit( world_instance
*world
)
1276 ui_rect panel
= { vg
.window_x
-400, 0, 400, vg
.window_y
};
1277 ui_fill( panel
, ui_colour( k_ui_bg
+1 ) );
1278 ui_outline( panel
, 1, ui_colour( k_ui_bg
+7 ), 0 );
1279 ui_rect_pad( panel
, (ui_px
[2]){ 8, 8 } );
1280 vg_ui
.wants_mouse
= 1;
1282 static i32 option_to_edit
= 0;
1283 ui_enum( panel
, "option", skybox_setting_options
, 5, &option_to_edit
);
1284 ui_colourpicker( panel
, "colour",
1285 skybox_prop_location( world
, option_to_edit
) );
1287 if( ui_button( panel
, "save tweaker file ('/tmp/tweaker.txt')\n" ) == 1 ){
1288 FILE *fp
= fopen( "/tmp/tweaker.txt", "w" );
1290 for( i32 i
=0; i
<5; i
++ ){
1291 struct ui_enum_opt
*opt
= &skybox_setting_options
[i
];
1292 f32
*val
= skybox_prop_location( world
, i
);
1293 fprintf( fp
, "%s = {%.3ff, %.3ff, %.3ff, %.3ff},\n",
1294 opt
->alias
, val
[0], val
[1], val
[2], val
[3] );