ent_challenge extra features
[carveJwlIkooP6JGAAIwe30JlM.git] / world_render.c
1 /*
2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef WORLD_RENDER_C
6 #define WORLD_RENDER_C
7
8 #include "world.h"
9 #include "world_render.h"
10
11 static int ccmd_set_time( int argc, const char *argv[] ){
12 if( argc == 1 ){
13 world_instance *world = world_current_instance();
14 world->time = atof( argv[0] );
15 }
16 else {
17 vg_error( "Usage set_time <0-1.0>\n" );
18 }
19 return 0;
20 }
21
22 VG_STATIC void async_world_render_init( void *payload, u32 size )
23 {
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;
28
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 );
33
34 glBindBufferBase( GL_UNIFORM_BUFFER, i, world->ubo_lighting );
35 VG_CHECK_GL_ERR();
36 }
37
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;
42
43 fb->display_name = NULL;
44 fb->link = NULL;
45 fb->fixed_w = 1024;
46 fb->fixed_h = 1024;
47 fb->resolution_div = 0;
48
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;
55
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;
60
61 render_fb_allocate( fb );
62 }
63 }
64
65 VG_STATIC void world_render_init(void)
66 {
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 );
72
73 world_render.sky_rate = 1.0;
74 world_render.sky_target_rate = 1.0;
75
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();
85
86 vg_info( "Loading world resources\n" );
87 vg_linear_clear( vg_mem.scratch );
88
89 mdl_context msky;
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 );
93 mdl_close( &msky );
94
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 );
99
100 vg_async_call( async_world_render_init, NULL, 0 );
101 }
102
103 VG_STATIC void world_link_lighting_ub( world_instance *world, GLuint shader )
104 {
105 GLuint idx = glGetUniformBlockIndex( shader, "ub_world_lighting" );
106 glUniformBlockBinding( shader, idx, world->ubo_bind_point );
107 }
108
109 VG_STATIC void world_bind_position_texture( world_instance *world,
110 GLuint shader, GLuint location,
111 int slot )
112 {
113 render_fb_bind_texture( &world->heightmap, 0, slot );
114 glUniform1i( location, slot );
115 }
116
117 VG_STATIC void world_bind_light_array( world_instance *world,
118 GLuint shader, GLuint location,
119 int slot )
120 {
121 glActiveTexture( GL_TEXTURE0 + slot );
122 glBindTexture( GL_TEXTURE_BUFFER, world->tex_light_entities );
123 glUniform1i( location, slot );
124 }
125
126 VG_STATIC void world_bind_light_index( world_instance *world,
127 GLuint shader, GLuint location,
128 int slot )
129 {
130 glActiveTexture( GL_TEXTURE0 + slot );
131 glBindTexture( GL_TEXTURE_3D, world->tex_light_cubes );
132 glUniform1i( location, slot );
133 }
134
135 VG_STATIC void render_world_depth( world_instance *world, camera *cam );
136
137 /*
138 * Rendering
139 */
140
141 VG_STATIC void bind_terrain_noise(void)
142 {
143 glActiveTexture( GL_TEXTURE0 );
144 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
145 }
146
147 struct world_pass{
148 camera *cam;
149 enum mdl_shader shader;
150 enum world_geo_type geo_type;
151
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 );
156 };
157
158 VG_STATIC
159 void world_render_traffic( world_instance *world, u32 material_id,
160 struct world_pass *pass ){
161 if( !mdl_arrcount( &world->ent_traffic ) ) return;
162
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;
166
167 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs, first->submesh_start );
168 if( sm->material_id != material_id ) return;
169
170 struct world_surface *mat = &world->surfaces[ material_id ];
171 pass->fn_bind_textures( world, mat );
172
173 for( u32 j=0; j<mdl_arrcount( &world->ent_traffic ); j++ ){
174 ent_traffic *traffic = mdl_arritm( &world->ent_traffic, j );
175
176 for( u32 k=0; k<traffic->submesh_count; k++ ){
177 sm = mdl_arritm( &world->meta.submeshs,
178 traffic->submesh_start+k );
179
180 m4x3f mmdl;
181 q_m3x3( traffic->transform.q, mmdl );
182 v3_copy( traffic->transform.co, mmdl[3] );
183
184 m4x4f m4mdl;
185 m4x3_expand( mmdl, m4mdl );
186 m4x4_mul( pass->cam->mtx_prev.pv, m4mdl, m4mdl );
187
188 pass->fn_set_mdl( mmdl );
189 pass->fn_set_uPvmPrev( m4mdl );
190
191 mdl_draw_submesh( sm );
192 }
193 }
194 }
195
196 VG_STATIC
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];
200
201 if( mat->info.shader == pass->shader ){
202 mdl_submesh *sm;
203
204 if( pass->geo_type == k_world_geo_type_solid ){
205 sm = &mat->sm_geo;
206 }
207 else{
208 world_render_traffic( world, i, pass );
209 sm = &mat->sm_no_collide;
210 }
211
212 if( !sm->indice_count )
213 continue;
214
215 m4x3f mmdl;
216 m4x3_identity( mmdl );
217 pass->fn_set_mdl( mmdl );
218 pass->fn_set_uPvmPrev( pass->cam->mtx_prev.pv );
219
220 pass->fn_bind_textures( world, mat );
221 mdl_draw_submesh( sm );
222 }
223 }
224 }
225
226 VG_STATIC
227 void world_render_both_stages( world_instance *world, struct world_pass *pass )
228 {
229 mesh_bind( &world->mesh_geo );
230 pass->geo_type = k_world_geo_type_solid;
231 world_render_pass( world, pass );
232
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 );
238 }
239
240 VG_STATIC void bindpoint_diffuse_texture1( world_instance *world,
241 struct world_surface *mat )
242
243 {
244 glActiveTexture( GL_TEXTURE1 );
245 glBindTexture( GL_TEXTURE_2D, world->textures[ mat->info.tex_diffuse ] );
246 }
247
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 ] );
252
253 u32 cubemap_id = mat->info.tex_none0,
254 cubemap_index = 0;
255
256 if( mdl_entity_id_type( cubemap_id ) == k_ent_cubemap ){
257 cubemap_index = mdl_entity_id_id( cubemap_id );
258 }
259
260 ent_cubemap *cm = mdl_arritm( &world->ent_cubemap, cubemap_index );
261 glActiveTexture( GL_TEXTURE10 );
262 glBindTexture( GL_TEXTURE_CUBE_MAP, cm->texture_id );
263
264 shader_scene_cubemapped_uColour( mat->info.colour );
265 }
266
267 VG_STATIC void render_world_vb( world_instance *world, camera *cam )
268 {
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 );
279
280 glActiveTexture( GL_TEXTURE0 );
281 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
282
283 shader_scene_vertex_blend_uPv( cam->mtx.pv );
284 shader_scene_vertex_blend_uCamera( cam->transform[3] );
285
286 struct world_pass pass = {
287 .shader = k_shader_standard_vertex_blend,
288 .cam = cam,
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,
292 };
293
294 world_render_both_stages( world, &pass );
295 }
296
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 );
302
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 );
310
311 bind_terrain_noise();
312 shader_scene_standard_uCamera( cam->transform[3] );
313 }
314
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,
319 .cam = cam,
320 .fn_bind_textures = bindpoint_diffuse_texture1,
321 .fn_set_mdl = shader_scene_standard_uMdl,
322 .fn_set_uPvmPrev = shader_scene_standard_uPvmPrev,
323 };
324
325 world_render_both_stages( world, &pass );
326 }
327
328 VG_STATIC void render_world_cubemapped( world_instance *world, camera *cam,
329 int layer_depth ){
330 if( !mdl_arrcount( &world->ent_cubemap ) )
331 return;
332
333 if( layer_depth == -1 ){
334 world_shader_standard_bind( world, cam );
335
336 struct world_pass pass = {
337 .shader = k_shader_cubemap,
338 .cam = cam,
339 .fn_bind_textures = bindpoint_diffuse_texture1,
340 .fn_set_mdl = shader_scene_standard_uMdl,
341 .fn_set_uPvmPrev = shader_scene_standard_uPvmPrev,
342 };
343
344 world_render_both_stages( world, &pass );
345 }
346 else {
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 );
352
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 );
360
361 bind_terrain_noise();
362 shader_scene_cubemapped_uCamera( cam->transform[3] );
363
364 struct world_pass pass = {
365 .shader = k_shader_cubemap,
366 .cam = cam,
367 .fn_bind_textures = bindpoint_diffuse1_and_cubemap10,
368 .fn_set_mdl = shader_scene_cubemapped_uMdl,
369 .fn_set_uPvmPrev = shader_scene_cubemapped_uPvmPrev,
370 };
371
372 world_render_both_stages( world, &pass );
373 }
374 }
375
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 );
381
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 );
389
390
391 bind_terrain_noise();
392
393
394 shader_scene_standard_alphatest_uCamera( cam->transform[3] );
395
396 glDisable(GL_CULL_FACE);
397
398 struct world_pass pass = {
399 .shader = k_shader_standard_cutout,
400 .cam = cam,
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,
404 };
405
406 world_render_both_stages( world, &pass );
407
408 glEnable(GL_CULL_FACE);
409 }
410
411 VG_STATIC
412 void world_render_challenges( world_instance *world, struct world_pass *pass,
413 v3f pos ){
414 if( !world ) return;
415
416 glDisable( GL_CULL_FACE );
417 mesh_bind( &world->mesh_no_collide );
418
419 u32 last_material = 0;
420
421 const f32 radius = 40.0f;
422
423 bh_iter it;
424 bh_iter_init_range( 0, &it, pos, radius );
425 i32 idx;
426
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 );
431
432 if( type != k_ent_challenge ) continue;
433
434 ent_challenge *challenge = mdl_arritm(&world->ent_challenge,index);
435 if( challenge->flags & k_ent_challenge_hidden ) continue;
436
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 ) );
439
440 v3_fill( challenge->transform.s, scale );
441
442 m4x3f mmdl;
443 mdl_transform_m4x3( &challenge->transform, mmdl );
444 shader_scene_fxglow_uMdl( mmdl );
445
446 for( u32 j=0; j<challenge->submesh_count; j++ ){
447 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs,
448 challenge->submesh_start + j );
449
450 if( sm->material_id != last_material ){
451 last_material = sm->material_id;
452
453 pass->fn_bind_textures( world, &world->surfaces[sm->material_id] );
454 }
455
456 mdl_draw_submesh( sm );
457 }
458 }
459 }
460
461 VG_STATIC void render_world_fxglow( world_instance *world, camera *cam ){
462 //glDrawBuffers( 1, (GLenum[]){ GL_COLOR_ATTACHMENT0 } );
463
464 shader_scene_fxglow_use();
465 shader_scene_fxglow_uTexMain(1);
466 shader_scene_fxglow_uPv( cam->mtx.pv );
467
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 );
475
476 shader_scene_fxglow_uCamera( cam->transform[3] );
477 glDisable(GL_CULL_FACE);
478
479 struct world_pass pass = {
480 .shader = k_shader_fxglow,
481 .cam = cam,
482 .fn_bind_textures = bindpoint_diffuse_texture1,
483 .fn_set_mdl = shader_scene_fxglow_uMdl,
484 .fn_set_uPvmPrev = shader_scene_fxglow_uPvmPrev,
485 };
486
487 world_render_both_stages( world, &pass );
488 world_render_challenges( world, &pass, cam->pos );
489
490 glEnable(GL_CULL_FACE);
491 //glDrawBuffers( 2, (GLenum[]){ GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 } );
492 }
493
494 VG_STATIC void bindpoint_terrain( world_instance *world,
495 struct world_surface *mat )
496 {
497 glActiveTexture( GL_TEXTURE1 );
498 glBindTexture( GL_TEXTURE_2D, world->textures[ mat->info.tex_diffuse ] );
499
500 shader_scene_terrain_uSandColour( mat->info.colour );
501 shader_scene_terrain_uBlendOffset( mat->info.colour1 );
502 }
503
504 VG_STATIC void render_terrain( world_instance *world, camera *cam )
505 {
506 shader_scene_terrain_use();
507 shader_scene_terrain_uTexGarbage(0);
508 shader_scene_terrain_uTexGradients(1);
509
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 );
517
518 glActiveTexture( GL_TEXTURE0 );
519 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
520
521 shader_scene_terrain_uPv( cam->mtx.pv );
522 shader_scene_terrain_uCamera( cam->transform[3] );
523
524 struct world_pass pass = {
525 .shader = k_shader_terrain_blend,
526 .cam = cam,
527 .fn_bind_textures = bindpoint_terrain,
528 .fn_set_mdl = shader_scene_terrain_uMdl,
529 .fn_set_uPvmPrev = shader_scene_terrain_uPvmPrev,
530 };
531
532 world_render_both_stages( world, &pass );
533 }
534
535 VG_STATIC void render_sky( world_instance *world, camera *cam )
536 {
537 /*
538 * Modify matrix to remove clipping and view translation
539 */
540 m4x4f v,
541 v_prev,
542 pv,
543 pv_prev;
544
545 m4x4_copy( cam->mtx.v, v );
546 m4x4_copy( cam->mtx_prev.v, v_prev );
547 v3_zero( v[3] );
548 v3_zero( v_prev[3] );
549
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 );
554
555 m4x4_mul( pv, v, pv );
556 m4x4_mul( pv_prev, v_prev, pv_prev );
557
558 m4x3f identity_matrix;
559 m4x3_identity( identity_matrix );
560
561 /*
562 * Draw
563 */
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 );
570
571 glActiveTexture( GL_TEXTURE0 );
572 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
573
574 glDepthMask( GL_FALSE );
575 glDisable( GL_DEPTH_TEST );
576
577 mesh_bind( &world_render.skydome );
578 mesh_draw( &world_render.skydome );
579
580 glEnable( GL_DEPTH_TEST );
581 glDepthMask( GL_TRUE );
582 }
583
584 VG_STATIC void render_world_gates( world_instance *world, camera *cam,
585 int layer_depth )
586 {
587 float closest = INFINITY;
588 struct ent_gate *gate = NULL;
589
590 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
591 ent_gate *gi = mdl_arritm( &world->ent_gate, i );
592
593 if( !(gi->flags & k_ent_gate_linked) )
594 continue;
595
596 float dist = v3_dist2( gi->co[0], cam->transform[3] );
597
598 vg_line_point( gi->co[0], 0.25f, VG__BLUE );
599
600 if( dist < closest ){
601 closest = dist;
602 gate = gi;
603 }
604 }
605
606 world->rendering_gate = gate;
607 if( gate ){
608 if( gate->flags & k_ent_gate_locked ) return;
609
610 if( gate->flags & k_ent_gate_nonlocal ){
611 if( world_static.load_state != k_world_loader_none ){
612 world->rendering_gate = NULL;
613 return;
614 }
615
616 world_instance *dest_world = &world_static.instances[ gate->target ];
617 render_gate( world, dest_world, gate, cam, layer_depth );
618 }
619 else{
620 render_gate( world, world, gate, cam, layer_depth );
621 }
622 }
623 }
624
625 VG_STATIC void world_prerender( world_instance *world )
626 {
627
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));
631 }
632 else{
633 world->time = 0.834;
634 }
635
636 struct ub_world_lighting *state = &world->ub_lighting;
637
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 );
646
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 );
649
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 );
655
656 world->probabilities[ k_probability_curve_constant ] = 1.0f;
657 float dp = state->g_day_phase;
658
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);
663
664 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
665 glBufferSubData( GL_UNIFORM_BUFFER, 0,
666 sizeof(struct ub_world_lighting), &world->ub_lighting );
667 }
668
669 VG_STATIC void skateshop_render(void);
670 VG_STATIC void render_world( world_instance *world, camera *cam,
671 int layer_depth )
672 {
673 render_sky( world, cam );
674
675 render_world_routes( world, cam, layer_depth );
676 render_world_standard( world, cam );
677 render_world_cubemapped( world, cam, layer_depth );
678
679 render_world_vb( world, cam );
680 render_world_alphatest( world, cam );
681 render_world_fxglow( world, cam );
682 render_terrain( world, cam );
683
684 if( layer_depth == 0 ){
685 skateshop_render();
686
687 /* Render SFD's */
688 u32 closest = 0;
689 float min_dist = INFINITY;
690
691 if( !mdl_arrcount( &world->ent_route ) )
692 return;
693
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 );
697
698 if( dist < min_dist ){
699 min_dist = dist;
700 closest = i;
701 }
702 }
703
704 ent_route *route = mdl_arritm( &world->ent_route, closest );
705 sfd_render( world, cam, route->board_transform );
706 }
707 }
708
709 VG_STATIC void render_cubemap_side( world_instance *world, ent_cubemap *cm,
710 u32 side ){
711 camera cam;
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 );
715
716 v3f forward[6] = {
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 },
722 { 0.0f, 0.0f, 1.0f }
723 };
724 v3f up[6] = {
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 }
731 };
732
733 v3_zero( cam.angles );
734 v3_copy( cm->co, cam.pos );
735
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 );
741
742 camera_update_view( &cam );
743
744 cam.nearz = 0.1f;
745 cam.farz = 1000.0f;
746 cam.fov = 90.0f;
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 );
751
752 render_world( world, &cam, -1 );
753 }
754
755 VG_STATIC void render_world_cubemaps( world_instance *world ){
756 if( world->cubemap_cooldown )
757 world->cubemap_cooldown --;
758 else{
759 world->cubemap_cooldown = 60;
760
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 );
765
766 world->cubemap_side ++;
767 if( world->cubemap_side >= 6 )
768 world->cubemap_side = 0;
769
770 render_cubemap_side( world, cm, world->cubemap_side );
771 }
772 }
773 }
774
775 VG_STATIC void render_world_depth( world_instance *world, camera *cam )
776 {
777 m4x3f identity_matrix;
778 m4x3_identity( identity_matrix );
779
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 );
786
787 mesh_bind( &world->mesh_geo );
788 mesh_draw( &world->mesh_geo );
789 }
790
791 VG_STATIC void render_world_position( world_instance *world, camera *cam )
792 {
793 m4x3f identity_matrix;
794 m4x3_identity( identity_matrix );
795
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 );
802
803 mesh_bind( &world->mesh_geo );
804 mesh_draw( &world->mesh_geo );
805 }
806
807 #endif