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 render_world_standard( 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] );
314 struct world_pass pass
= {
315 .shader
= k_shader_standard
,
317 .fn_bind_textures
= bindpoint_diffuse_texture1
,
318 .fn_set_mdl
= shader_scene_standard_uMdl
,
319 .fn_set_uPvmPrev
= shader_scene_standard_uPvmPrev
,
322 world_render_both_stages( world
, &pass
);
325 VG_STATIC
void render_world_cubemapped( world_instance
*world
, camera
*cam
){
326 if( !mdl_arrcount( &world
->ent_cubemap
) )
329 shader_scene_cubemapped_use();
330 shader_scene_cubemapped_uTexGarbage(0);
331 shader_scene_cubemapped_uTexMain(1);
332 shader_scene_cubemapped_uTexCubemap(10);
333 shader_scene_cubemapped_uPv( cam
->mtx
.pv
);
335 world_link_lighting_ub( world
, _shader_scene_cubemapped
.id
);
336 world_bind_position_texture( world
, _shader_scene_cubemapped
.id
,
337 _uniform_scene_cubemapped_g_world_depth
, 2 );
338 world_bind_light_array( world
, _shader_scene_cubemapped
.id
,
339 _uniform_scene_cubemapped_uLightsArray
, 3 );
340 world_bind_light_index( world
, _shader_scene_cubemapped
.id
,
341 _uniform_scene_cubemapped_uLightsIndex
, 4 );
343 bind_terrain_noise();
344 shader_scene_cubemapped_uCamera( cam
->transform
[3] );
346 struct world_pass pass
= {
347 .shader
= k_shader_cubemap
,
349 .fn_bind_textures
= bindpoint_diffuse1_and_cubemap10
,
350 .fn_set_mdl
= shader_scene_cubemapped_uMdl
,
351 .fn_set_uPvmPrev
= shader_scene_cubemapped_uPvmPrev
,
354 world_render_both_stages( world
, &pass
);
357 VG_STATIC
void render_world_alphatest( world_instance
*world
, camera
*cam
){
358 shader_scene_standard_alphatest_use();
359 shader_scene_standard_alphatest_uTexGarbage(0);
360 shader_scene_standard_alphatest_uTexMain(1);
361 shader_scene_standard_alphatest_uPv( cam
->mtx
.pv
);
363 world_link_lighting_ub( world
, _shader_scene_standard_alphatest
.id
);
364 world_bind_position_texture( world
, _shader_scene_standard_alphatest
.id
,
365 _uniform_scene_standard_alphatest_g_world_depth
, 2 );
366 world_bind_light_array( world
, _shader_scene_standard_alphatest
.id
,
367 _uniform_scene_standard_alphatest_uLightsArray
, 3 );
368 world_bind_light_index( world
, _shader_scene_standard_alphatest
.id
,
369 _uniform_scene_standard_alphatest_uLightsIndex
, 4 );
372 bind_terrain_noise();
375 shader_scene_standard_alphatest_uCamera( cam
->transform
[3] );
377 glDisable(GL_CULL_FACE
);
379 struct world_pass pass
= {
380 .shader
= k_shader_standard_cutout
,
382 .fn_bind_textures
= bindpoint_diffuse_texture1
,
383 .fn_set_mdl
= shader_scene_standard_alphatest_uMdl
,
384 .fn_set_uPvmPrev
= shader_scene_standard_alphatest_uPvmPrev
,
387 world_render_both_stages( world
, &pass
);
389 glEnable(GL_CULL_FACE
);
393 void world_render_challenges( world_instance
*world
, struct world_pass
*pass
,
397 glDisable( GL_CULL_FACE
);
398 mesh_bind( &world
->mesh_no_collide
);
400 u32 last_material
= 0;
402 const f32 radius
= 40.0f
;
405 bh_iter_init_range( 0, &it
, pos
, radius
);
408 while( bh_next( world
->entity_bh
, &it
, &idx
) ){
409 u32 id
= world
->entity_list
[ idx
],
410 type
= mdl_entity_id_type( id
),
411 index
= mdl_entity_id_id( id
);
413 if( type
!= k_ent_challenge
) continue;
415 ent_challenge
*challenge
= mdl_arritm(&world
->ent_challenge
,index
);
417 f32 dist
= v3_dist( challenge
->transform
.co
, pos
) * (1.0f
/radius
),
418 scale
= vg_smoothstepf( vg_clampf( 10.0f
-dist
*10.0f
, 0.0f
,1.0f
) );
420 v3_fill( challenge
->transform
.s
, scale
);
423 mdl_transform_m4x3( &challenge
->transform
, mmdl
);
424 shader_scene_fxglow_uMdl( mmdl
);
426 for( u32 j
=0; j
<challenge
->submesh_count
; j
++ ){
427 mdl_submesh
*sm
= mdl_arritm( &world
->meta
.submeshs
,
428 challenge
->submesh_start
+ j
);
430 if( sm
->material_id
!= last_material
){
431 last_material
= sm
->material_id
;
433 pass
->fn_bind_textures( world
, &world
->surfaces
[sm
->material_id
] );
436 mdl_draw_submesh( sm
);
441 VG_STATIC
void render_world_fxglow( world_instance
*world
, camera
*cam
){
442 //glDrawBuffers( 1, (GLenum[]){ GL_COLOR_ATTACHMENT0 } );
444 shader_scene_fxglow_use();
445 shader_scene_fxglow_uTexMain(1);
446 shader_scene_fxglow_uPv( cam
->mtx
.pv
);
448 world_link_lighting_ub( world
, _shader_scene_fxglow
.id
);
449 world_bind_position_texture( world
, _shader_scene_fxglow
.id
,
450 _uniform_scene_fxglow_g_world_depth
, 2 );
451 world_bind_light_array( world
, _shader_scene_fxglow
.id
,
452 _uniform_scene_fxglow_uLightsArray
, 3 );
453 world_bind_light_index( world
, _shader_scene_fxglow
.id
,
454 _uniform_scene_fxglow_uLightsIndex
, 4 );
456 shader_scene_fxglow_uCamera( cam
->transform
[3] );
457 glDisable(GL_CULL_FACE
);
459 struct world_pass pass
= {
460 .shader
= k_shader_fxglow
,
462 .fn_bind_textures
= bindpoint_diffuse_texture1
,
463 .fn_set_mdl
= shader_scene_fxglow_uMdl
,
464 .fn_set_uPvmPrev
= shader_scene_fxglow_uPvmPrev
,
467 world_render_both_stages( world
, &pass
);
468 world_render_challenges( world
, &pass
, cam
->pos
);
470 glEnable(GL_CULL_FACE
);
471 //glDrawBuffers( 2, (GLenum[]){ GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 } );
474 VG_STATIC
void bindpoint_terrain( world_instance
*world
,
475 struct world_surface
*mat
)
477 glActiveTexture( GL_TEXTURE1
);
478 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
480 shader_scene_terrain_uSandColour( mat
->info
.colour
);
481 shader_scene_terrain_uBlendOffset( mat
->info
.colour1
);
484 VG_STATIC
void render_terrain( world_instance
*world
, camera
*cam
)
486 shader_scene_terrain_use();
487 shader_scene_terrain_uTexGarbage(0);
488 shader_scene_terrain_uTexGradients(1);
490 world_link_lighting_ub( world
, _shader_scene_terrain
.id
);
491 world_bind_position_texture( world
, _shader_scene_terrain
.id
,
492 _uniform_scene_terrain_g_world_depth
, 2 );
493 world_bind_light_array( world
, _shader_scene_terrain
.id
,
494 _uniform_scene_terrain_uLightsArray
, 3 );
495 world_bind_light_index( world
, _shader_scene_terrain
.id
,
496 _uniform_scene_terrain_uLightsIndex
, 4 );
498 glActiveTexture( GL_TEXTURE0
);
499 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
501 shader_scene_terrain_uPv( cam
->mtx
.pv
);
502 shader_scene_terrain_uCamera( cam
->transform
[3] );
504 struct world_pass pass
= {
505 .shader
= k_shader_terrain_blend
,
507 .fn_bind_textures
= bindpoint_terrain
,
508 .fn_set_mdl
= shader_scene_terrain_uMdl
,
509 .fn_set_uPvmPrev
= shader_scene_terrain_uPvmPrev
,
512 world_render_both_stages( world
, &pass
);
515 VG_STATIC
void render_sky( world_instance
*world
, camera
*cam
)
518 * Modify matrix to remove clipping and view translation
525 m4x4_copy( cam
->mtx
.v
, v
);
526 m4x4_copy( cam
->mtx_prev
.v
, v_prev
);
528 v3_zero( v_prev
[3] );
530 m4x4_copy( cam
->mtx
.p
, pv
);
531 m4x4_copy( cam
->mtx_prev
.p
, pv_prev
);
532 m4x4_reset_clipping( pv
, cam
->farz
, cam
->nearz
);
533 m4x4_reset_clipping( pv_prev
, cam
->farz
, cam
->nearz
);
535 m4x4_mul( pv
, v
, pv
);
536 m4x4_mul( pv_prev
, v_prev
, pv_prev
);
538 m4x3f identity_matrix
;
539 m4x3_identity( identity_matrix
);
544 shader_model_sky_use();
545 shader_model_sky_uMdl( identity_matrix
);
546 shader_model_sky_uPv( pv
);
547 shader_model_sky_uPvmPrev( pv_prev
);
548 shader_model_sky_uTexGarbage(0);
549 world_link_lighting_ub( world
, _shader_model_sky
.id
);
551 glActiveTexture( GL_TEXTURE0
);
552 glBindTexture( GL_TEXTURE_2D
, world_render
.tex_terrain_noise
);
554 glDepthMask( GL_FALSE
);
555 glDisable( GL_DEPTH_TEST
);
557 mesh_bind( &world_render
.skydome
);
558 mesh_draw( &world_render
.skydome
);
560 glEnable( GL_DEPTH_TEST
);
561 glDepthMask( GL_TRUE
);
564 VG_STATIC
void render_world_gates( world_instance
*world
, camera
*cam
,
567 float closest
= INFINITY
;
568 struct ent_gate
*gate
= NULL
;
570 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_gate
); i
++ ){
571 ent_gate
*gi
= mdl_arritm( &world
->ent_gate
, i
);
573 if( !(gi
->flags
& k_ent_gate_linked
) )
576 float dist
= v3_dist2( gi
->co
[0], cam
->transform
[3] );
578 vg_line_point( gi
->co
[0], 0.25f
, VG__BLUE
);
580 if( dist
< closest
){
586 world
->rendering_gate
= gate
;
588 if( gate
->flags
& k_ent_gate_locked
) return;
590 if( gate
->flags
& k_ent_gate_nonlocal
){
591 if( world_static
.load_state
!= k_world_loader_none
){
592 world
->rendering_gate
= NULL
;
596 world_instance
*dest_world
= &world_static
.instances
[ gate
->target
];
597 render_gate( world
, dest_world
, gate
, cam
, layer_depth
);
600 render_gate( world
, world
, gate
, cam
, layer_depth
);
605 VG_STATIC
void world_prerender( world_instance
*world
)
608 if( mdl_arrcount( &world
->ent_light
) ){
609 f32 rate
= vg_maxf(0.1f
, fabsf(k_day_length
)) * vg_signf(k_day_length
);
610 world
->time
+= vg
.time_delta
* (1.0/(rate
*60.0));
616 struct ub_world_lighting
*state
= &world
->ub_lighting
;
618 state
->g_time
= world
->time
;
619 state
->g_realtime
= vg
.time
;
620 state
->g_debug_indices
= k_debug_light_indices
;
621 state
->g_light_preview
= k_light_preview
;
622 state
->g_debug_complexity
= k_debug_light_complexity
;
623 state
->g_time_of_day
= vg_fractf( world
->time
);
624 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
625 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
627 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
628 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
630 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
631 state
->g_sun_dir
[0] = sinf( a
);
632 state
->g_sun_dir
[1] = cosf( a
);
633 state
->g_sun_dir
[2] = 0.2f
;
634 v3_normalize( state
->g_sun_dir
);
636 world
->probabilities
[ k_probability_curve_constant
] = 1.0f
;
637 float dp
= state
->g_day_phase
;
639 world
->probabilities
[ k_probability_curve_wildlife_day
] =
640 (dp
*dp
*0.8f
+state
->g_sunset_phase
)*0.8f
;
641 world
->probabilities
[ k_probability_curve_wildlife_night
] =
642 1.0f
-powf(fabsf((state
->g_time_of_day
-0.5f
)*5.0f
),5.0f
);
644 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
645 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
646 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
649 VG_STATIC
void skateshop_render(void);
650 VG_STATIC
void render_world( world_instance
*world
, camera
*cam
,
653 render_sky( world
, cam
);
655 render_world_routes( world
, cam
, layer_depth
);
656 render_world_standard( world
, cam
);
658 if( layer_depth
!= -1 ){
659 /* TODO: RENDER ONLY BASETEXTURE */
660 render_world_cubemapped( world
, cam
);
663 render_world_vb( world
, cam
);
664 render_world_alphatest( world
, cam
);
665 render_world_fxglow( world
, cam
);
666 render_terrain( world
, cam
);
668 if( layer_depth
== 0 ){
673 float min_dist
= INFINITY
;
675 if( !mdl_arrcount( &world
->ent_route
) )
678 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
679 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
680 float dist
= v3_dist2( route
->board_transform
[3], cam
->pos
);
682 if( dist
< min_dist
){
688 ent_route
*route
= mdl_arritm( &world
->ent_route
, closest
);
689 sfd_render( world
, cam
, route
->board_transform
);
693 VG_STATIC
void render_world_cubemaps( world_instance
*world
){
694 glViewport( 0, 0, WORLD_CUBEMAP_RES
, WORLD_CUBEMAP_RES
);
696 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_cubemap
); i
++ ){
697 ent_cubemap
*cm
= mdl_arritm( &world
->ent_cubemap
, i
);
699 glBindFramebuffer( GL_FRAMEBUFFER
, cm
->framebuffer_id
);
701 for( u32 j
=0; j
<6; j
++ ){
702 glFramebufferTexture2D( GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
,
703 GL_TEXTURE_CUBE_MAP_POSITIVE_X
+ j
, cm
->texture_id
, 0 );
704 glClear( GL_DEPTH_BUFFER_BIT
);
707 { -1.0f
, 0.0f
, 0.0f
},
708 { 1.0f
, 0.0f
, 0.0f
},
709 { 0.0f
, -1.0f
, 0.0f
},
710 { 0.0f
, 1.0f
, 0.0f
},
711 { 0.0f
, 0.0f
, -1.0f
},
715 { 0.0f
, -1.0f
, 0.0f
},
716 { 0.0f
, -1.0f
, 0.0f
},
717 { 0.0f
, 0.0f
, 1.0f
},
718 { 0.0f
, 0.0f
, -1.0f
},
719 { 0.0f
, -1.0f
, 0.0f
},
720 { 0.0f
, -1.0f
, 0.0f
}
723 v3_zero( cam
.angles
);
724 v3_copy( cm
->co
, cam
.pos
);
726 v3_copy( forward
[j
], cam
.transform
[2] );
727 v3_copy( up
[j
], cam
.transform
[1] );
728 v3_cross( up
[j
], forward
[j
], cam
.transform
[0] );
729 v3_copy( cm
->co
, cam
.transform
[3] );
730 m4x3_invert_affine( cam
.transform
, cam
.transform_inverse
);
732 camera_update_view( &cam
);
737 m4x4_copy( cam
.mtx
.p
, cam
.mtx_prev
.p
);
738 m4x4_projection( cam
.mtx
.p
, cam
.fov
, 1.0f
, cam
.nearz
, cam
.farz
);
739 camera_finalize( &cam
);
740 camera_finalize( &cam
);
742 render_world( world
, &cam
, -1 );
747 VG_STATIC
void render_world_depth( world_instance
*world
, camera
*cam
)
749 m4x3f identity_matrix
;
750 m4x3_identity( identity_matrix
);
752 shader_scene_depth_use();
753 shader_scene_depth_uCamera( cam
->transform
[3] );
754 shader_scene_depth_uPv( cam
->mtx
.pv
);
755 shader_scene_depth_uPvmPrev( cam
->mtx_prev
.pv
);
756 shader_scene_depth_uMdl( identity_matrix
);
757 world_link_lighting_ub( world
, _shader_scene_depth
.id
);
759 mesh_bind( &world
->mesh_geo
);
760 mesh_draw( &world
->mesh_geo
);
763 VG_STATIC
void render_world_position( world_instance
*world
, camera
*cam
)
765 m4x3f identity_matrix
;
766 m4x3_identity( identity_matrix
);
768 shader_scene_position_use();
769 shader_scene_position_uCamera( cam
->transform
[3] );
770 shader_scene_position_uPv( cam
->mtx
.pv
);
771 shader_scene_position_uPvmPrev( cam
->mtx_prev
.pv
);
772 shader_scene_position_uMdl( identity_matrix
);
773 world_link_lighting_ub( world
, _shader_scene_position
.id
);
775 mesh_bind( &world
->mesh_geo
);
776 mesh_draw( &world
->mesh_geo
);