2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
9 #include "world_render.h"
11 static int ccmd_set_time( int argc
, const char *argv
[] ){
13 world_instance
*world
= world_current_instance();
14 world
->time
= atof( argv
[0] );
17 vg_error( "Usage set_time <0-1.0>\n" );
22 VG_STATIC
void async_world_render_init( void *payload
, u32 size
)
24 vg_info( "Allocate uniform buffers\n" );
25 for( int i
=0; i
<4; i
++ ){
26 world_instance
*world
= &world_static
.instances
[i
];
27 world
->ubo_bind_point
= i
;
29 glGenBuffers( 1, &world
->ubo_lighting
);
30 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
31 glBufferData( GL_UNIFORM_BUFFER
, sizeof(struct ub_world_lighting
),
32 NULL
, GL_DYNAMIC_DRAW
);
34 glBindBufferBase( GL_UNIFORM_BUFFER
, i
, world
->ubo_lighting
);
38 vg_info( "Allocate frame buffers\n" );
39 for( int i
=0; i
<4; i
++ ){
40 world_instance
*world
= &world_static
.instances
[i
];
41 struct framebuffer
*fb
= &world
->heightmap
;
43 fb
->display_name
= NULL
;
47 fb
->resolution_div
= 0;
49 fb
->attachments
[0].display_name
= NULL
;
50 fb
->attachments
[0].purpose
= k_framebuffer_attachment_type_texture
;
51 fb
->attachments
[0].internalformat
= GL_RG16F
;
52 fb
->attachments
[0].format
= GL_RG
;
53 fb
->attachments
[0].type
= GL_FLOAT
;
54 fb
->attachments
[0].attachment
= GL_COLOR_ATTACHMENT0
;
56 fb
->attachments
[1].purpose
= k_framebuffer_attachment_type_none
;
57 fb
->attachments
[2].purpose
= k_framebuffer_attachment_type_none
;
58 fb
->attachments
[3].purpose
= k_framebuffer_attachment_type_none
;
59 fb
->attachments
[4].purpose
= k_framebuffer_attachment_type_none
;
61 render_fb_allocate( fb
);
65 VG_STATIC
void world_render_init(void)
67 VG_VAR_F32( k_day_length
);
68 VG_VAR_I32( k_debug_light_indices
);
69 VG_VAR_I32( k_debug_light_complexity
);
70 VG_VAR_I32( k_light_preview
);
71 vg_console_reg_cmd( "set_time", ccmd_set_time
, NULL
);
73 world_render
.sky_rate
= 1.0;
74 world_render
.sky_target_rate
= 1.0;
76 shader_scene_standard_register();
77 shader_scene_standard_alphatest_register();
78 shader_scene_cubemapped_register();
79 shader_scene_fxglow_register();
80 shader_scene_vertex_blend_register();
81 shader_scene_terrain_register();
82 shader_scene_depth_register();
83 shader_scene_position_register();
84 shader_model_sky_register();
86 vg_info( "Loading world resources\n" );
87 vg_linear_clear( vg_mem
.scratch
);
90 mdl_open( &msky
, "models/rs_skydome.mdl", vg_mem
.scratch
);
91 mdl_load_metadata_block( &msky
, vg_mem
.scratch
);
92 mdl_async_load_glmesh( &msky
, &world_render
.skydome
);
95 vg_info( "Loading default world textures\n" );
96 vg_tex2d_load_qoi_async_file( "textures/garbage.qoi",
97 VG_TEX2D_NEAREST
|VG_TEX2D_REPEAT
,
98 &world_render
.tex_terrain_noise
);
100 vg_async_call( async_world_render_init
, NULL
, 0 );
103 VG_STATIC
void world_link_lighting_ub( world_instance
*world
, GLuint shader
)
105 GLuint idx
= glGetUniformBlockIndex( shader
, "ub_world_lighting" );
106 glUniformBlockBinding( shader
, idx
, world
->ubo_bind_point
);
109 VG_STATIC
void world_bind_position_texture( world_instance
*world
,
110 GLuint shader
, GLuint location
,
113 render_fb_bind_texture( &world
->heightmap
, 0, slot
);
114 glUniform1i( location
, slot
);
117 VG_STATIC
void world_bind_light_array( world_instance
*world
,
118 GLuint shader
, GLuint location
,
121 glActiveTexture( GL_TEXTURE0
+ slot
);
122 glBindTexture( GL_TEXTURE_BUFFER
, world
->tex_light_entities
);
123 glUniform1i( location
, slot
);
126 VG_STATIC
void world_bind_light_index( world_instance
*world
,
127 GLuint shader
, GLuint location
,
130 glActiveTexture( GL_TEXTURE0
+ slot
);
131 glBindTexture( GL_TEXTURE_3D
, world
->tex_light_cubes
);
132 glUniform1i( location
, slot
);
135 VG_STATIC
void render_world_depth( world_instance
*world
, camera
*cam
);
141 VG_STATIC
void bind_terrain_noise(void)
143 glActiveTexture( GL_TEXTURE0
);
144 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
149 enum mdl_shader shader
;
150 enum world_geo_type geo_type
;
152 void (*fn_bind_textures
)( world_instance
*world
,
153 struct world_surface
*mat
);
154 void (*fn_set_mdl
)( m4x3f mdl
);
155 void (*fn_set_uPvmPrev
)( m4x4f pvm
);
159 void world_render_traffic( world_instance
*world
, u32 material_id
,
160 struct world_pass
*pass
){
161 if( !mdl_arrcount( &world
->ent_traffic
) ) return;
163 /* HACK: use the first material for every traffic entity */
164 ent_traffic
*first
= mdl_arritm( &world
->ent_traffic
, 0 );
165 if( !first
->submesh_count
) return;
167 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
, first
->submesh_start
);
168 if( sm
->material_id
!= material_id
) return;
170 struct world_surface
*mat
= &world
->surfaces
[ material_id
];
171 pass
->fn_bind_textures( world
, mat
);
173 for( u32 j
=0; j
<mdl_arrcount( &world
->ent_traffic
); j
++ ){
174 ent_traffic
*traffic
= mdl_arritm( &world
->ent_traffic
, j
);
176 for( u32 k
=0; k
<traffic
->submesh_count
; k
++ ){
177 sm
= mdl_arritm( &world
->meta
.submeshs
,
178 traffic
->submesh_start
+k
);
181 q_m3x3( traffic
->transform
.q
, mmdl
);
182 v3_copy( traffic
->transform
.co
, mmdl
[3] );
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_pass( world_instance
*world
, struct world_pass
*pass
){
198 for( int i
=0; i
<world
->surface_count
; i
++ ){
199 struct world_surface
*mat
= &world
->surfaces
[i
];
201 if( mat
->info
.shader
== pass
->shader
){
204 if( pass
->geo_type
== k_world_geo_type_solid
){
208 world_render_traffic( world
, i
, pass
);
209 sm
= &mat
->sm_no_collide
;
212 if( !sm
->indice_count
)
216 m4x3_identity( mmdl
);
217 pass
->fn_set_mdl( mmdl
);
218 pass
->fn_set_uPvmPrev( pass
->cam
->mtx_prev
.pv
);
220 pass
->fn_bind_textures( world
, mat
);
221 mdl_draw_submesh( sm
);
227 void world_render_both_stages( world_instance
*world
, struct world_pass
*pass
)
229 mesh_bind( &world
->mesh_geo
);
230 pass
->geo_type
= k_world_geo_type_solid
;
231 world_render_pass( world
, pass
);
233 glDisable( GL_CULL_FACE
);
234 mesh_bind( &world
->mesh_no_collide
);
235 pass
->geo_type
= k_world_geo_type_nonsolid
;
236 world_render_pass( world
, pass
);
237 glEnable( GL_CULL_FACE
);
240 VG_STATIC
void bindpoint_diffuse_texture1( world_instance
*world
,
241 struct world_surface
*mat
)
244 glActiveTexture( GL_TEXTURE1
);
245 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
248 VG_STATIC
void bindpoint_diffuse1_and_cubemap10( world_instance
*world
,
249 struct world_surface
*mat
){
250 glActiveTexture( GL_TEXTURE1
);
251 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
253 u32 cubemap_id
= mat
->info
.tex_none0
,
256 if( mdl_entity_id_type( cubemap_id
) == k_ent_cubemap
){
257 cubemap_index
= mdl_entity_id_id( cubemap_id
);
260 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, cubemap_index
);
261 glActiveTexture( GL_TEXTURE10
);
262 glBindTexture( GL_TEXTURE_CUBE_MAP
, cm
->texture_id
);
264 shader_scene_cubemapped_uColour( mat
->info
.colour
);
267 VG_STATIC
void render_world_vb( world_instance
*world
, camera
*cam
)
269 shader_scene_vertex_blend_use();
270 shader_scene_vertex_blend_uTexGarbage(0);
271 shader_scene_vertex_blend_uTexGradients(1);
272 world_link_lighting_ub( world
, _shader_scene_vertex_blend
.id
);
273 world_bind_position_texture( world
, _shader_scene_vertex_blend
.id
,
274 _uniform_scene_vertex_blend_g_world_depth
, 2 );
275 world_bind_light_array( world
, _shader_scene_vertex_blend
.id
,
276 _uniform_scene_vertex_blend_uLightsArray
, 3 );
277 world_bind_light_index( world
, _shader_scene_vertex_blend
.id
,
278 _uniform_scene_vertex_blend_uLightsIndex
, 4 );
280 glActiveTexture( GL_TEXTURE0
);
281 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
283 shader_scene_vertex_blend_uPv( cam
->mtx
.pv
);
284 shader_scene_vertex_blend_uCamera( cam
->transform
[3] );
286 struct world_pass pass
= {
287 .shader
= k_shader_standard_vertex_blend
,
289 .fn_bind_textures
= bindpoint_diffuse_texture1
,
290 .fn_set_mdl
= shader_scene_vertex_blend_uMdl
,
291 .fn_set_uPvmPrev
= shader_scene_vertex_blend_uPvmPrev
,
294 world_render_both_stages( world
, &pass
);
297 VG_STATIC
void world_shader_standard_bind( world_instance
*world
, camera
*cam
){
298 shader_scene_standard_use();
299 shader_scene_standard_uTexGarbage(0);
300 shader_scene_standard_uTexMain(1);
301 shader_scene_standard_uPv( cam
->mtx
.pv
);
303 world_link_lighting_ub( world
, _shader_scene_standard
.id
);
304 world_bind_position_texture( world
, _shader_scene_standard
.id
,
305 _uniform_scene_standard_g_world_depth
, 2 );
306 world_bind_light_array( world
, _shader_scene_standard
.id
,
307 _uniform_scene_standard_uLightsArray
, 3 );
308 world_bind_light_index( world
, _shader_scene_standard
.id
,
309 _uniform_scene_standard_uLightsIndex
, 4 );
311 bind_terrain_noise();
312 shader_scene_standard_uCamera( cam
->transform
[3] );
315 VG_STATIC
void render_world_standard( world_instance
*world
, camera
*cam
){
316 world_shader_standard_bind( world
, cam
);
317 struct world_pass pass
= {
318 .shader
= k_shader_standard
,
320 .fn_bind_textures
= bindpoint_diffuse_texture1
,
321 .fn_set_mdl
= shader_scene_standard_uMdl
,
322 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
325 world_render_both_stages( world
, &pass
);
328 VG_STATIC
void render_world_cubemapped( world_instance
*world
, camera
*cam
,
330 if( !mdl_arrcount( &world
->ent_cubemap
) )
333 if( layer_depth
== -1 ){
334 world_shader_standard_bind( world
, cam
);
336 struct world_pass pass
= {
337 .shader
= k_shader_cubemap
,
339 .fn_bind_textures
= bindpoint_diffuse_texture1
,
340 .fn_set_mdl
= shader_scene_standard_uMdl
,
341 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
344 world_render_both_stages( world
, &pass
);
347 shader_scene_cubemapped_use();
348 shader_scene_cubemapped_uTexGarbage(0);
349 shader_scene_cubemapped_uTexMain(1);
350 shader_scene_cubemapped_uTexCubemap(10);
351 shader_scene_cubemapped_uPv( cam
->mtx
.pv
);
353 world_link_lighting_ub( world
, _shader_scene_cubemapped
.id
);
354 world_bind_position_texture( world
, _shader_scene_cubemapped
.id
,
355 _uniform_scene_cubemapped_g_world_depth
, 2 );
356 world_bind_light_array( world
, _shader_scene_cubemapped
.id
,
357 _uniform_scene_cubemapped_uLightsArray
, 3 );
358 world_bind_light_index( world
, _shader_scene_cubemapped
.id
,
359 _uniform_scene_cubemapped_uLightsIndex
, 4 );
361 bind_terrain_noise();
362 shader_scene_cubemapped_uCamera( cam
->transform
[3] );
364 struct world_pass pass
= {
365 .shader
= k_shader_cubemap
,
367 .fn_bind_textures
= bindpoint_diffuse1_and_cubemap10
,
368 .fn_set_mdl
= shader_scene_cubemapped_uMdl
,
369 .fn_set_uPvmPrev
= shader_scene_cubemapped_uPvmPrev
,
372 world_render_both_stages( world
, &pass
);
376 VG_STATIC
void render_world_alphatest( world_instance
*world
, camera
*cam
){
377 shader_scene_standard_alphatest_use();
378 shader_scene_standard_alphatest_uTexGarbage(0);
379 shader_scene_standard_alphatest_uTexMain(1);
380 shader_scene_standard_alphatest_uPv( cam
->mtx
.pv
);
382 world_link_lighting_ub( world
, _shader_scene_standard_alphatest
.id
);
383 world_bind_position_texture( world
, _shader_scene_standard_alphatest
.id
,
384 _uniform_scene_standard_alphatest_g_world_depth
, 2 );
385 world_bind_light_array( world
, _shader_scene_standard_alphatest
.id
,
386 _uniform_scene_standard_alphatest_uLightsArray
, 3 );
387 world_bind_light_index( world
, _shader_scene_standard_alphatest
.id
,
388 _uniform_scene_standard_alphatest_uLightsIndex
, 4 );
391 bind_terrain_noise();
394 shader_scene_standard_alphatest_uCamera( cam
->transform
[3] );
396 glDisable(GL_CULL_FACE
);
398 struct world_pass pass
= {
399 .shader
= k_shader_standard_cutout
,
401 .fn_bind_textures
= bindpoint_diffuse_texture1
,
402 .fn_set_mdl
= shader_scene_standard_alphatest_uMdl
,
403 .fn_set_uPvmPrev
= shader_scene_standard_alphatest_uPvmPrev
,
406 world_render_both_stages( world
, &pass
);
408 glEnable(GL_CULL_FACE
);
412 void world_render_challenges( world_instance
*world
, struct world_pass
*pass
,
416 glDisable( GL_CULL_FACE
);
417 mesh_bind( &world
->mesh_no_collide
);
419 u32 last_material
= 0;
421 const f32 radius
= 40.0f
;
424 bh_iter_init_range( 0, &it
, pos
, radius
);
427 while( bh_next( world
->entity_bh
, &it
, &idx
) ){
428 u32 id
= world
->entity_list
[ idx
],
429 type
= mdl_entity_id_type( id
),
430 index
= mdl_entity_id_id( id
);
432 if( type
!= k_ent_challenge
) continue;
434 ent_challenge
*challenge
= mdl_arritm(&world
->ent_challenge
,index
);
435 if( challenge
->flags
& k_ent_challenge_hidden
) continue;
437 f32 dist
= v3_dist( challenge
->transform
.co
, pos
) * (1.0f
/radius
),
438 scale
= vg_smoothstepf( vg_clampf( 10.0f
-dist
*10.0f
, 0.0f
,1.0f
) );
440 v3_fill( challenge
->transform
.s
, scale
);
443 mdl_transform_m4x3( &challenge
->transform
, mmdl
);
444 shader_scene_fxglow_uMdl( mmdl
);
446 for( u32 j
=0; j
<challenge
->submesh_count
; j
++ ){
447 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
448 challenge
->submesh_start
+ j
);
450 if( sm
->material_id
!= last_material
){
451 last_material
= sm
->material_id
;
453 pass
->fn_bind_textures( world
, &world
->surfaces
[sm
->material_id
] );
456 mdl_draw_submesh( sm
);
461 VG_STATIC
void render_world_fxglow( world_instance
*world
, camera
*cam
){
462 //glDrawBuffers( 1, (GLenum[]){ GL_COLOR_ATTACHMENT0 } );
464 shader_scene_fxglow_use();
465 shader_scene_fxglow_uTexMain(1);
466 shader_scene_fxglow_uPv( cam
->mtx
.pv
);
468 world_link_lighting_ub( world
, _shader_scene_fxglow
.id
);
469 world_bind_position_texture( world
, _shader_scene_fxglow
.id
,
470 _uniform_scene_fxglow_g_world_depth
, 2 );
471 world_bind_light_array( world
, _shader_scene_fxglow
.id
,
472 _uniform_scene_fxglow_uLightsArray
, 3 );
473 world_bind_light_index( world
, _shader_scene_fxglow
.id
,
474 _uniform_scene_fxglow_uLightsIndex
, 4 );
476 shader_scene_fxglow_uCamera( cam
->transform
[3] );
477 glDisable(GL_CULL_FACE
);
479 struct world_pass pass
= {
480 .shader
= k_shader_fxglow
,
482 .fn_bind_textures
= bindpoint_diffuse_texture1
,
483 .fn_set_mdl
= shader_scene_fxglow_uMdl
,
484 .fn_set_uPvmPrev
= shader_scene_fxglow_uPvmPrev
,
487 world_render_both_stages( world
, &pass
);
488 world_render_challenges( world
, &pass
, cam
->pos
);
490 glEnable(GL_CULL_FACE
);
491 //glDrawBuffers( 2, (GLenum[]){ GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 } );
494 VG_STATIC
void bindpoint_terrain( world_instance
*world
,
495 struct world_surface
*mat
)
497 glActiveTexture( GL_TEXTURE1
);
498 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
500 shader_scene_terrain_uSandColour( mat
->info
.colour
);
501 shader_scene_terrain_uBlendOffset( mat
->info
.colour1
);
504 VG_STATIC
void render_terrain( world_instance
*world
, camera
*cam
)
506 shader_scene_terrain_use();
507 shader_scene_terrain_uTexGarbage(0);
508 shader_scene_terrain_uTexGradients(1);
510 world_link_lighting_ub( world
, _shader_scene_terrain
.id
);
511 world_bind_position_texture( world
, _shader_scene_terrain
.id
,
512 _uniform_scene_terrain_g_world_depth
, 2 );
513 world_bind_light_array( world
, _shader_scene_terrain
.id
,
514 _uniform_scene_terrain_uLightsArray
, 3 );
515 world_bind_light_index( world
, _shader_scene_terrain
.id
,
516 _uniform_scene_terrain_uLightsIndex
, 4 );
518 glActiveTexture( GL_TEXTURE0
);
519 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
521 shader_scene_terrain_uPv( cam
->mtx
.pv
);
522 shader_scene_terrain_uCamera( cam
->transform
[3] );
524 struct world_pass pass
= {
525 .shader
= k_shader_terrain_blend
,
527 .fn_bind_textures
= bindpoint_terrain
,
528 .fn_set_mdl
= shader_scene_terrain_uMdl
,
529 .fn_set_uPvmPrev
= shader_scene_terrain_uPvmPrev
,
532 world_render_both_stages( world
, &pass
);
535 VG_STATIC
void render_sky( world_instance
*world
, camera
*cam
)
538 * Modify matrix to remove clipping and view translation
545 m4x4_copy( cam
->mtx
.v
, v
);
546 m4x4_copy( cam
->mtx_prev
.v
, v_prev
);
548 v3_zero( v_prev
[3] );
550 m4x4_copy( cam
->mtx
.p
, pv
);
551 m4x4_copy( cam
->mtx_prev
.p
, pv_prev
);
552 m4x4_reset_clipping( pv
, cam
->farz
, cam
->nearz
);
553 m4x4_reset_clipping( pv_prev
, cam
->farz
, cam
->nearz
);
555 m4x4_mul( pv
, v
, pv
);
556 m4x4_mul( pv_prev
, v_prev
, pv_prev
);
558 m4x3f identity_matrix
;
559 m4x3_identity( identity_matrix
);
564 shader_model_sky_use();
565 shader_model_sky_uMdl( identity_matrix
);
566 shader_model_sky_uPv( pv
);
567 shader_model_sky_uPvmPrev( pv_prev
);
568 shader_model_sky_uTexGarbage(0);
569 world_link_lighting_ub( world
, _shader_model_sky
.id
);
571 glActiveTexture( GL_TEXTURE0
);
572 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
574 glDepthMask( GL_FALSE
);
575 glDisable( GL_DEPTH_TEST
);
577 mesh_bind( &world_render
.skydome
);
578 mesh_draw( &world_render
.skydome
);
580 glEnable( GL_DEPTH_TEST
);
581 glDepthMask( GL_TRUE
);
584 VG_STATIC
void render_world_gates( world_instance
*world
, camera
*cam
,
587 float closest
= INFINITY
;
588 struct ent_gate
*gate
= NULL
;
590 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
591 ent_gate
*gi
= mdl_arritm( &world
->ent_gate
, i
);
593 if( !(gi
->flags
& k_ent_gate_linked
) )
596 float dist
= v3_dist2( gi
->co
[0], cam
->transform
[3] );
598 vg_line_point( gi
->co
[0], 0.25f
, VG__BLUE
);
600 if( dist
< closest
){
606 world
->rendering_gate
= gate
;
608 if( gate
->flags
& k_ent_gate_locked
) return;
610 if( gate
->flags
& k_ent_gate_nonlocal
){
611 if( world_static
.load_state
!= k_world_loader_none
){
612 world
->rendering_gate
= NULL
;
616 world_instance
*dest_world
= &world_static
.instances
[ gate
->target
];
617 render_gate( world
, dest_world
, gate
, cam
, layer_depth
);
620 render_gate( world
, world
, gate
, cam
, layer_depth
);
625 VG_STATIC
void world_prerender( world_instance
*world
)
628 if( mdl_arrcount( &world
->ent_light
) ){
629 f32 rate
= vg_maxf(0.1f
, fabsf(k_day_length
)) * vg_signf(k_day_length
);
630 world
->time
+= vg
.time_delta
* (1.0/(rate
*60.0));
636 struct ub_world_lighting
*state
= &world
->ub_lighting
;
638 state
->g_time
= world
->time
;
639 state
->g_realtime
= vg
.time
;
640 state
->g_debug_indices
= k_debug_light_indices
;
641 state
->g_light_preview
= k_light_preview
;
642 state
->g_debug_complexity
= k_debug_light_complexity
;
643 state
->g_time_of_day
= vg_fractf( world
->time
);
644 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
645 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
647 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
648 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
650 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
651 state
->g_sun_dir
[0] = sinf( a
);
652 state
->g_sun_dir
[1] = cosf( a
);
653 state
->g_sun_dir
[2] = 0.2f
;
654 v3_normalize( state
->g_sun_dir
);
656 world
->probabilities
[ k_probability_curve_constant
] = 1.0f
;
657 float dp
= state
->g_day_phase
;
659 world
->probabilities
[ k_probability_curve_wildlife_day
] =
660 (dp
*dp
*0.8f
+state
->g_sunset_phase
)*0.8f
;
661 world
->probabilities
[ k_probability_curve_wildlife_night
] =
662 1.0f
-powf(fabsf((state
->g_time_of_day
-0.5f
)*5.0f
),5.0f
);
664 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
665 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
666 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
669 VG_STATIC
void skateshop_render(void);
670 VG_STATIC
void render_world( world_instance
*world
, camera
*cam
,
673 render_sky( world
, cam
);
675 render_world_routes( world
, cam
, layer_depth
);
676 render_world_standard( world
, cam
);
677 render_world_cubemapped( world
, cam
, layer_depth
);
679 render_world_vb( world
, cam
);
680 render_world_alphatest( world
, cam
);
681 render_world_fxglow( world
, cam
);
682 render_terrain( world
, cam
);
684 if( layer_depth
== 0 ){
689 float min_dist
= INFINITY
;
691 if( !mdl_arrcount( &world
->ent_route
) )
694 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
695 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
696 float dist
= v3_dist2( route
->board_transform
[3], cam
->pos
);
698 if( dist
< min_dist
){
704 ent_route
*route
= mdl_arritm( &world
->ent_route
, closest
);
705 sfd_render( world
, cam
, route
->board_transform
);
709 VG_STATIC
void render_cubemap_side( world_instance
*world
, ent_cubemap
*cm
,
712 glFramebufferTexture2D( GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
713 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ side
, cm
->texture_id
, 0 );
714 glClear( GL_DEPTH_BUFFER_BIT
);
717 { -1.0f
, 0.0f
, 0.0f
},
718 { 1.0f
, 0.0f
, 0.0f
},
719 { 0.0f
, -1.0f
, 0.0f
},
720 { 0.0f
, 1.0f
, 0.0f
},
721 { 0.0f
, 0.0f
, -1.0f
},
725 { 0.0f
, -1.0f
, 0.0f
},
726 { 0.0f
, -1.0f
, 0.0f
},
727 { 0.0f
, 0.0f
, 1.0f
},
728 { 0.0f
, 0.0f
, -1.0f
},
729 { 0.0f
, -1.0f
, 0.0f
},
730 { 0.0f
, -1.0f
, 0.0f
}
733 v3_zero( cam
.angles
);
734 v3_copy( cm
->co
, cam
.pos
);
736 v3_copy( forward
[side
], cam
.transform
[2] );
737 v3_copy( up
[side
], cam
.transform
[1] );
738 v3_cross( up
[side
], forward
[side
], cam
.transform
[0] );
739 v3_copy( cm
->co
, cam
.transform
[3] );
740 m4x3_invert_affine( cam
.transform
, cam
.transform_inverse
);
742 camera_update_view( &cam
);
747 m4x4_copy( cam
.mtx
.p
, cam
.mtx_prev
.p
);
748 m4x4_projection( cam
.mtx
.p
, cam
.fov
, 1.0f
, cam
.nearz
, cam
.farz
);
749 camera_finalize( &cam
);
750 camera_finalize( &cam
);
752 render_world( world
, &cam
, -1 );
755 VG_STATIC
void render_world_cubemaps( world_instance
*world
){
756 if( world
->cubemap_cooldown
)
757 world
->cubemap_cooldown
--;
759 world
->cubemap_cooldown
= 60;
761 glViewport( 0, 0, WORLD_CUBEMAP_RES
, WORLD_CUBEMAP_RES
);
762 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_cubemap
); i
++ ){
763 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, i
);
764 glBindFramebuffer( GL_FRAMEBUFFER
, cm
->framebuffer_id
);
766 world
->cubemap_side
++;
767 if( world
->cubemap_side
>= 6 )
768 world
->cubemap_side
= 0;
770 render_cubemap_side( world
, cm
, world
->cubemap_side
);
775 VG_STATIC
void render_world_depth( world_instance
*world
, camera
*cam
)
777 m4x3f identity_matrix
;
778 m4x3_identity( identity_matrix
);
780 shader_scene_depth_use();
781 shader_scene_depth_uCamera( cam
->transform
[3] );
782 shader_scene_depth_uPv( cam
->mtx
.pv
);
783 shader_scene_depth_uPvmPrev( cam
->mtx_prev
.pv
);
784 shader_scene_depth_uMdl( identity_matrix
);
785 world_link_lighting_ub( world
, _shader_scene_depth
.id
);
787 mesh_bind( &world
->mesh_geo
);
788 mesh_draw( &world
->mesh_geo
);
791 VG_STATIC
void render_world_position( world_instance
*world
, camera
*cam
)
793 m4x3f identity_matrix
;
794 m4x3_identity( identity_matrix
);
796 shader_scene_position_use();
797 shader_scene_position_uCamera( cam
->transform
[3] );
798 shader_scene_position_uPv( cam
->mtx
.pv
);
799 shader_scene_position_uPvmPrev( cam
->mtx_prev
.pv
);
800 shader_scene_position_uMdl( identity_matrix
);
801 world_link_lighting_ub( world
, _shader_scene_position
.id
);
803 mesh_bind( &world
->mesh_geo
);
804 mesh_draw( &world
->mesh_geo
);