chill out cubemaps
[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
436 f32 dist = v3_dist( challenge->transform.co, pos ) * (1.0f/radius),
437 scale = vg_smoothstepf( vg_clampf( 10.0f-dist*10.0f, 0.0f,1.0f ) );
438
439 v3_fill( challenge->transform.s, scale );
440
441 m4x3f mmdl;
442 mdl_transform_m4x3( &challenge->transform, mmdl );
443 shader_scene_fxglow_uMdl( mmdl );
444
445 for( u32 j=0; j<challenge->submesh_count; j++ ){
446 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs,
447 challenge->submesh_start + j );
448
449 if( sm->material_id != last_material ){
450 last_material = sm->material_id;
451
452 pass->fn_bind_textures( world, &world->surfaces[sm->material_id] );
453 }
454
455 mdl_draw_submesh( sm );
456 }
457 }
458 }
459
460 VG_STATIC void render_world_fxglow( world_instance *world, camera *cam ){
461 //glDrawBuffers( 1, (GLenum[]){ GL_COLOR_ATTACHMENT0 } );
462
463 shader_scene_fxglow_use();
464 shader_scene_fxglow_uTexMain(1);
465 shader_scene_fxglow_uPv( cam->mtx.pv );
466
467 world_link_lighting_ub( world, _shader_scene_fxglow.id );
468 world_bind_position_texture( world, _shader_scene_fxglow.id,
469 _uniform_scene_fxglow_g_world_depth, 2 );
470 world_bind_light_array( world, _shader_scene_fxglow.id,
471 _uniform_scene_fxglow_uLightsArray, 3 );
472 world_bind_light_index( world, _shader_scene_fxglow.id,
473 _uniform_scene_fxglow_uLightsIndex, 4 );
474
475 shader_scene_fxglow_uCamera( cam->transform[3] );
476 glDisable(GL_CULL_FACE);
477
478 struct world_pass pass = {
479 .shader = k_shader_fxglow,
480 .cam = cam,
481 .fn_bind_textures = bindpoint_diffuse_texture1,
482 .fn_set_mdl = shader_scene_fxglow_uMdl,
483 .fn_set_uPvmPrev = shader_scene_fxglow_uPvmPrev,
484 };
485
486 world_render_both_stages( world, &pass );
487 world_render_challenges( world, &pass, cam->pos );
488
489 glEnable(GL_CULL_FACE);
490 //glDrawBuffers( 2, (GLenum[]){ GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 } );
491 }
492
493 VG_STATIC void bindpoint_terrain( world_instance *world,
494 struct world_surface *mat )
495 {
496 glActiveTexture( GL_TEXTURE1 );
497 glBindTexture( GL_TEXTURE_2D, world->textures[ mat->info.tex_diffuse ] );
498
499 shader_scene_terrain_uSandColour( mat->info.colour );
500 shader_scene_terrain_uBlendOffset( mat->info.colour1 );
501 }
502
503 VG_STATIC void render_terrain( world_instance *world, camera *cam )
504 {
505 shader_scene_terrain_use();
506 shader_scene_terrain_uTexGarbage(0);
507 shader_scene_terrain_uTexGradients(1);
508
509 world_link_lighting_ub( world, _shader_scene_terrain.id );
510 world_bind_position_texture( world, _shader_scene_terrain.id,
511 _uniform_scene_terrain_g_world_depth, 2 );
512 world_bind_light_array( world, _shader_scene_terrain.id,
513 _uniform_scene_terrain_uLightsArray, 3 );
514 world_bind_light_index( world, _shader_scene_terrain.id,
515 _uniform_scene_terrain_uLightsIndex, 4 );
516
517 glActiveTexture( GL_TEXTURE0 );
518 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
519
520 shader_scene_terrain_uPv( cam->mtx.pv );
521 shader_scene_terrain_uCamera( cam->transform[3] );
522
523 struct world_pass pass = {
524 .shader = k_shader_terrain_blend,
525 .cam = cam,
526 .fn_bind_textures = bindpoint_terrain,
527 .fn_set_mdl = shader_scene_terrain_uMdl,
528 .fn_set_uPvmPrev = shader_scene_terrain_uPvmPrev,
529 };
530
531 world_render_both_stages( world, &pass );
532 }
533
534 VG_STATIC void render_sky( world_instance *world, camera *cam )
535 {
536 /*
537 * Modify matrix to remove clipping and view translation
538 */
539 m4x4f v,
540 v_prev,
541 pv,
542 pv_prev;
543
544 m4x4_copy( cam->mtx.v, v );
545 m4x4_copy( cam->mtx_prev.v, v_prev );
546 v3_zero( v[3] );
547 v3_zero( v_prev[3] );
548
549 m4x4_copy( cam->mtx.p, pv );
550 m4x4_copy( cam->mtx_prev.p, pv_prev );
551 m4x4_reset_clipping( pv, cam->farz, cam->nearz );
552 m4x4_reset_clipping( pv_prev, cam->farz, cam->nearz );
553
554 m4x4_mul( pv, v, pv );
555 m4x4_mul( pv_prev, v_prev, pv_prev );
556
557 m4x3f identity_matrix;
558 m4x3_identity( identity_matrix );
559
560 /*
561 * Draw
562 */
563 shader_model_sky_use();
564 shader_model_sky_uMdl( identity_matrix );
565 shader_model_sky_uPv( pv );
566 shader_model_sky_uPvmPrev( pv_prev );
567 shader_model_sky_uTexGarbage(0);
568 world_link_lighting_ub( world, _shader_model_sky.id );
569
570 glActiveTexture( GL_TEXTURE0 );
571 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
572
573 glDepthMask( GL_FALSE );
574 glDisable( GL_DEPTH_TEST );
575
576 mesh_bind( &world_render.skydome );
577 mesh_draw( &world_render.skydome );
578
579 glEnable( GL_DEPTH_TEST );
580 glDepthMask( GL_TRUE );
581 }
582
583 VG_STATIC void render_world_gates( world_instance *world, camera *cam,
584 int layer_depth )
585 {
586 float closest = INFINITY;
587 struct ent_gate *gate = NULL;
588
589 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
590 ent_gate *gi = mdl_arritm( &world->ent_gate, i );
591
592 if( !(gi->flags & k_ent_gate_linked) )
593 continue;
594
595 float dist = v3_dist2( gi->co[0], cam->transform[3] );
596
597 vg_line_point( gi->co[0], 0.25f, VG__BLUE );
598
599 if( dist < closest ){
600 closest = dist;
601 gate = gi;
602 }
603 }
604
605 world->rendering_gate = gate;
606 if( gate ){
607 if( gate->flags & k_ent_gate_locked ) return;
608
609 if( gate->flags & k_ent_gate_nonlocal ){
610 if( world_static.load_state != k_world_loader_none ){
611 world->rendering_gate = NULL;
612 return;
613 }
614
615 world_instance *dest_world = &world_static.instances[ gate->target ];
616 render_gate( world, dest_world, gate, cam, layer_depth );
617 }
618 else{
619 render_gate( world, world, gate, cam, layer_depth );
620 }
621 }
622 }
623
624 VG_STATIC void world_prerender( world_instance *world )
625 {
626
627 if( mdl_arrcount( &world->ent_light ) ){
628 f32 rate = vg_maxf(0.1f, fabsf(k_day_length)) * vg_signf(k_day_length);
629 world->time += vg.time_delta * (1.0/(rate*60.0));
630 }
631 else{
632 world->time = 0.834;
633 }
634
635 struct ub_world_lighting *state = &world->ub_lighting;
636
637 state->g_time = world->time;
638 state->g_realtime = vg.time;
639 state->g_debug_indices = k_debug_light_indices;
640 state->g_light_preview = k_light_preview;
641 state->g_debug_complexity = k_debug_light_complexity;
642 state->g_time_of_day = vg_fractf( world->time );
643 state->g_day_phase = cosf( state->g_time_of_day * VG_PIf * 2.0f );
644 state->g_sunset_phase= cosf( state->g_time_of_day * VG_PIf * 4.0f + VG_PIf );
645
646 state->g_day_phase = state->g_day_phase * 0.5f + 0.5f;
647 state->g_sunset_phase = powf( state->g_sunset_phase * 0.5f + 0.5f, 6.0f );
648
649 float a = state->g_time_of_day * VG_PIf * 2.0f;
650 state->g_sun_dir[0] = sinf( a );
651 state->g_sun_dir[1] = cosf( a );
652 state->g_sun_dir[2] = 0.2f;
653 v3_normalize( state->g_sun_dir );
654
655 world->probabilities[ k_probability_curve_constant ] = 1.0f;
656 float dp = state->g_day_phase;
657
658 world->probabilities[ k_probability_curve_wildlife_day ] =
659 (dp*dp*0.8f+state->g_sunset_phase)*0.8f;
660 world->probabilities[ k_probability_curve_wildlife_night ] =
661 1.0f-powf(fabsf((state->g_time_of_day-0.5f)*5.0f),5.0f);
662
663 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
664 glBufferSubData( GL_UNIFORM_BUFFER, 0,
665 sizeof(struct ub_world_lighting), &world->ub_lighting );
666 }
667
668 VG_STATIC void skateshop_render(void);
669 VG_STATIC void render_world( world_instance *world, camera *cam,
670 int layer_depth )
671 {
672 render_sky( world, cam );
673
674 render_world_routes( world, cam, layer_depth );
675 render_world_standard( world, cam );
676 render_world_cubemapped( world, cam, layer_depth );
677
678 render_world_vb( world, cam );
679 render_world_alphatest( world, cam );
680 render_world_fxglow( world, cam );
681 render_terrain( world, cam );
682
683 if( layer_depth == 0 ){
684 skateshop_render();
685
686 /* Render SFD's */
687 u32 closest = 0;
688 float min_dist = INFINITY;
689
690 if( !mdl_arrcount( &world->ent_route ) )
691 return;
692
693 for( u32 i=0; i<mdl_arrcount( &world->ent_route ); i++ ){
694 ent_route *route = mdl_arritm( &world->ent_route, i );
695 float dist = v3_dist2( route->board_transform[3], cam->pos );
696
697 if( dist < min_dist ){
698 min_dist = dist;
699 closest = i;
700 }
701 }
702
703 ent_route *route = mdl_arritm( &world->ent_route, closest );
704 sfd_render( world, cam, route->board_transform );
705 }
706 }
707
708 VG_STATIC void render_cubemap_side( world_instance *world, ent_cubemap *cm,
709 u32 side ){
710 camera cam;
711 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
712 GL_TEXTURE_CUBE_MAP_POSITIVE_X + side, cm->texture_id, 0 );
713 glClear( GL_DEPTH_BUFFER_BIT );
714
715 v3f forward[6] = {
716 { -1.0f, 0.0f, 0.0f },
717 { 1.0f, 0.0f, 0.0f },
718 { 0.0f, -1.0f, 0.0f },
719 { 0.0f, 1.0f, 0.0f },
720 { 0.0f, 0.0f, -1.0f },
721 { 0.0f, 0.0f, 1.0f }
722 };
723 v3f up[6] = {
724 { 0.0f, -1.0f, 0.0f },
725 { 0.0f, -1.0f, 0.0f },
726 { 0.0f, 0.0f, 1.0f },
727 { 0.0f, 0.0f, -1.0f },
728 { 0.0f, -1.0f, 0.0f },
729 { 0.0f, -1.0f, 0.0f }
730 };
731
732 v3_zero( cam.angles );
733 v3_copy( cm->co, cam.pos );
734
735 v3_copy( forward[side], cam.transform[2] );
736 v3_copy( up[side], cam.transform[1] );
737 v3_cross( up[side], forward[side], cam.transform[0] );
738 v3_copy( cm->co, cam.transform[3] );
739 m4x3_invert_affine( cam.transform, cam.transform_inverse );
740
741 camera_update_view( &cam );
742
743 cam.nearz = 0.1f;
744 cam.farz = 1000.0f;
745 cam.fov = 90.0f;
746 m4x4_copy( cam.mtx.p, cam.mtx_prev.p );
747 m4x4_projection( cam.mtx.p, cam.fov, 1.0f, cam.nearz, cam.farz );
748 camera_finalize( &cam );
749 camera_finalize( &cam );
750
751 render_world( world, &cam, -1 );
752 }
753
754 VG_STATIC void render_world_cubemaps( world_instance *world ){
755 if( world->cubemap_cooldown )
756 world->cubemap_cooldown --;
757 else{
758 world->cubemap_cooldown = 60;
759
760 glViewport( 0, 0, WORLD_CUBEMAP_RES, WORLD_CUBEMAP_RES );
761 for( u32 i=0; i<mdl_arrcount( &world->ent_cubemap ); i++ ){
762 ent_cubemap *cm = mdl_arritm( &world->ent_cubemap, i );
763 glBindFramebuffer( GL_FRAMEBUFFER, cm->framebuffer_id );
764
765 world->cubemap_side ++;
766 if( world->cubemap_side >= 6 )
767 world->cubemap_side = 0;
768
769 render_cubemap_side( world, cm, world->cubemap_side );
770 }
771 }
772 }
773
774 VG_STATIC void render_world_depth( world_instance *world, camera *cam )
775 {
776 m4x3f identity_matrix;
777 m4x3_identity( identity_matrix );
778
779 shader_scene_depth_use();
780 shader_scene_depth_uCamera( cam->transform[3] );
781 shader_scene_depth_uPv( cam->mtx.pv );
782 shader_scene_depth_uPvmPrev( cam->mtx_prev.pv );
783 shader_scene_depth_uMdl( identity_matrix );
784 world_link_lighting_ub( world, _shader_scene_depth.id );
785
786 mesh_bind( &world->mesh_geo );
787 mesh_draw( &world->mesh_geo );
788 }
789
790 VG_STATIC void render_world_position( world_instance *world, camera *cam )
791 {
792 m4x3f identity_matrix;
793 m4x3_identity( identity_matrix );
794
795 shader_scene_position_use();
796 shader_scene_position_uCamera( cam->transform[3] );
797 shader_scene_position_uPv( cam->mtx.pv );
798 shader_scene_position_uPvmPrev( cam->mtx_prev.pv );
799 shader_scene_position_uMdl( identity_matrix );
800 world_link_lighting_ub( world, _shader_scene_position.id );
801
802 mesh_bind( &world->mesh_geo );
803 mesh_draw( &world->mesh_geo );
804 }
805
806 #endif