sync time between clients to utc
[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 #include "font.h"
11 #include "gui.h"
12 #include "respawn.h"
13
14 static int ccmd_set_time( int argc, const char *argv[] ){
15 world_instance *world = world_current_instance();
16 if( argc == 1 )
17 world->time = atof( argv[0] );
18 else
19 vg_error( "Usage set_time <0-1.0> (current time: %f)\n", world->time );
20 return 0;
21 }
22
23 static void async_world_render_init( void *payload, u32 size )
24 {
25 vg_info( "Allocate uniform buffers\n" );
26 for( int i=0; i<k_world_max; i++ ){
27 world_instance *world = &world_static.instances[i];
28 world->ubo_bind_point = i;
29
30 glGenBuffers( 1, &world->ubo_lighting );
31 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
32 glBufferData( GL_UNIFORM_BUFFER, sizeof(struct ub_world_lighting),
33 NULL, GL_DYNAMIC_DRAW );
34
35 glBindBufferBase( GL_UNIFORM_BUFFER, i, world->ubo_lighting );
36 VG_CHECK_GL_ERR();
37 }
38
39 vg_info( "Allocate frame buffers\n" );
40 for( int i=0; i<k_world_max; i++ ){
41 world_instance *world = &world_static.instances[i];
42 struct framebuffer *fb = &world->heightmap;
43
44 fb->display_name = NULL;
45 fb->link = NULL;
46 fb->fixed_w = 1024;
47 fb->fixed_h = 1024;
48 fb->resolution_div = 0;
49
50 fb->attachments[0].display_name = NULL;
51 fb->attachments[0].purpose = k_framebuffer_attachment_type_texture;
52 fb->attachments[0].internalformat = GL_RG16F;
53 fb->attachments[0].format = GL_RG;
54 fb->attachments[0].type = GL_FLOAT;
55 fb->attachments[0].attachment = GL_COLOR_ATTACHMENT0;
56
57 fb->attachments[1].purpose = k_framebuffer_attachment_type_none;
58 fb->attachments[2].purpose = k_framebuffer_attachment_type_none;
59 fb->attachments[3].purpose = k_framebuffer_attachment_type_none;
60 fb->attachments[4].purpose = k_framebuffer_attachment_type_none;
61
62 render_fb_allocate( fb );
63 }
64 }
65
66 static void world_render_init(void)
67 {
68 VG_VAR_F32( k_day_length );
69 VG_VAR_I32( k_debug_light_indices );
70 VG_VAR_I32( k_debug_light_complexity );
71 VG_VAR_I32( k_light_preview );
72 vg_console_reg_cmd( "set_time", ccmd_set_time, NULL );
73
74 world_render.sky_rate = 1.0;
75 world_render.sky_target_rate = 1.0;
76
77 shader_scene_standard_register();
78 shader_scene_standard_alphatest_register();
79 shader_scene_override_register();
80 shader_scene_cubemapped_register();
81 shader_scene_fxglow_register();
82 shader_scene_vertex_blend_register();
83 shader_scene_terrain_register();
84 shader_scene_depth_register();
85 shader_scene_position_register();
86 shader_model_sky_register();
87
88 vg_info( "Loading world resources\n" );
89 vg_linear_clear( vg_mem.scratch );
90
91 mdl_context msky;
92 mdl_open( &msky, "models/rs_skydome.mdl", vg_mem.scratch );
93 mdl_load_metadata_block( &msky, vg_mem.scratch );
94 mdl_async_load_glmesh( &msky, &world_render.skydome );
95 mdl_close( &msky );
96
97 vg_info( "Loading default world textures\n" );
98 vg_tex2d_load_qoi_async_file( "textures/garbage.qoi",
99 VG_TEX2D_NEAREST|VG_TEX2D_REPEAT,
100 &world_render.tex_terrain_noise );
101
102 vg_async_call( async_world_render_init, NULL, 0 );
103 }
104
105 static void world_link_lighting_ub( world_instance *world, GLuint shader ){
106 GLuint idx = glGetUniformBlockIndex( shader, "ub_world_lighting" );
107 glUniformBlockBinding( shader, idx, world->ubo_bind_point );
108 }
109
110 static void world_bind_position_texture( world_instance *world,
111 GLuint shader, GLuint location,
112 int slot ){
113 render_fb_bind_texture( &world->heightmap, 0, slot );
114 glUniform1i( location, slot );
115 }
116
117 static void world_bind_light_array( world_instance *world,
118 GLuint shader, GLuint location,
119 int slot ){
120 glActiveTexture( GL_TEXTURE0 + slot );
121 glBindTexture( GL_TEXTURE_BUFFER, world->tex_light_entities );
122 glUniform1i( location, slot );
123 }
124
125 static void world_bind_light_index( world_instance *world,
126 GLuint shader, GLuint location,
127 int slot ){
128 glActiveTexture( GL_TEXTURE0 + slot );
129 glBindTexture( GL_TEXTURE_3D, world->tex_light_cubes );
130 glUniform1i( location, slot );
131 }
132
133 static void render_world_depth( world_instance *world, camera *cam );
134
135 /*
136 * Rendering
137 */
138
139 static void bind_terrain_noise(void){
140 glActiveTexture( GL_TEXTURE0 );
141 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
142 }
143
144 struct world_pass{
145 camera *cam;
146 enum mdl_shader shader;
147 enum world_geo_type geo_type;
148
149 void (*fn_bind_textures)( world_instance *world,
150 struct world_surface *mat );
151 void (*fn_set_mdl)( m4x3f mdl );
152 void (*fn_set_uPvmPrev)( m4x4f pvm );
153 };
154
155 static
156 void world_render_traffic( world_instance *world, u32 material_id,
157 struct world_pass *pass ){
158 if( !mdl_arrcount( &world->ent_traffic ) ) return;
159
160 /* HACK: use the first material for every traffic entity */
161 ent_traffic *first = mdl_arritm( &world->ent_traffic, 0 );
162 if( !first->submesh_count ) return;
163
164 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs, first->submesh_start );
165 if( sm->material_id != material_id ) return;
166
167 struct world_surface *mat = &world->surfaces[ material_id ];
168 pass->fn_bind_textures( world, mat );
169
170 for( u32 j=0; j<mdl_arrcount( &world->ent_traffic ); j++ ){
171 ent_traffic *traffic = mdl_arritm( &world->ent_traffic, j );
172
173 for( u32 k=0; k<traffic->submesh_count; k++ ){
174 sm = mdl_arritm( &world->meta.submeshs,
175 traffic->submesh_start+k );
176
177 m4x3f mmdl;
178 q_m3x3( traffic->transform.q, mmdl );
179 v3_copy( traffic->transform.co, mmdl[3] );
180
181 m4x4f m4mdl;
182 m4x3_expand( mmdl, m4mdl );
183 m4x4_mul( pass->cam->mtx_prev.pv, m4mdl, m4mdl );
184
185 pass->fn_set_mdl( mmdl );
186 pass->fn_set_uPvmPrev( m4mdl );
187
188 mdl_draw_submesh( sm );
189 }
190 }
191 }
192
193 static
194 void world_render_pass( world_instance *world, struct world_pass *pass ){
195 for( int i=0; i<world->surface_count; i++ ){
196 struct world_surface *mat = &world->surfaces[i];
197
198 if( mat->info.shader == pass->shader ){
199 mdl_submesh *sm;
200
201 if( pass->geo_type == k_world_geo_type_solid ){
202 sm = &mat->sm_geo;
203 }
204 else{
205 world_render_traffic( world, i, pass );
206 sm = &mat->sm_no_collide;
207 }
208
209 if( !sm->indice_count )
210 continue;
211
212 m4x3f mmdl;
213 m4x3_identity( mmdl );
214 pass->fn_set_mdl( mmdl );
215 pass->fn_set_uPvmPrev( pass->cam->mtx_prev.pv );
216
217 pass->fn_bind_textures( world, mat );
218 mdl_draw_submesh( sm );
219 }
220 }
221 }
222
223 static
224 void world_render_both_stages( world_instance *world, struct world_pass *pass )
225 {
226 mesh_bind( &world->mesh_geo );
227 pass->geo_type = k_world_geo_type_solid;
228 world_render_pass( world, pass );
229
230 glDisable( GL_CULL_FACE );
231 mesh_bind( &world->mesh_no_collide );
232 pass->geo_type = k_world_geo_type_nonsolid;
233 world_render_pass( world, pass );
234 glEnable( GL_CULL_FACE );
235 }
236
237 static void bindpoint_diffuse_texture1( world_instance *world,
238 struct world_surface *mat )
239
240 {
241 glActiveTexture( GL_TEXTURE1 );
242 glBindTexture( GL_TEXTURE_2D, world->textures[ mat->info.tex_diffuse ] );
243 }
244
245 static void bindpoint_diffuse1_and_cubemap10( world_instance *world,
246 struct world_surface *mat ){
247 glActiveTexture( GL_TEXTURE1 );
248 glBindTexture( GL_TEXTURE_2D, world->textures[ mat->info.tex_diffuse ] );
249
250 u32 cubemap_id = mat->info.tex_none0,
251 cubemap_index = 0;
252
253 if( mdl_entity_id_type( cubemap_id ) == k_ent_cubemap ){
254 cubemap_index = mdl_entity_id_id( cubemap_id );
255 }
256
257 ent_cubemap *cm = mdl_arritm( &world->ent_cubemap, cubemap_index );
258 glActiveTexture( GL_TEXTURE10 );
259 glBindTexture( GL_TEXTURE_CUBE_MAP, cm->texture_id );
260
261 shader_scene_cubemapped_uColour( mat->info.colour );
262 }
263
264 static void render_world_vb( world_instance *world, camera *cam ){
265 shader_scene_vertex_blend_use();
266 shader_scene_vertex_blend_uTexGarbage(0);
267 shader_scene_vertex_blend_uTexGradients(1);
268 world_link_lighting_ub( world, _shader_scene_vertex_blend.id );
269 world_bind_position_texture( world, _shader_scene_vertex_blend.id,
270 _uniform_scene_vertex_blend_g_world_depth, 2 );
271 world_bind_light_array( world, _shader_scene_vertex_blend.id,
272 _uniform_scene_vertex_blend_uLightsArray, 3 );
273 world_bind_light_index( world, _shader_scene_vertex_blend.id,
274 _uniform_scene_vertex_blend_uLightsIndex, 4 );
275
276 glActiveTexture( GL_TEXTURE0 );
277 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
278
279 shader_scene_vertex_blend_uPv( cam->mtx.pv );
280 shader_scene_vertex_blend_uCamera( cam->transform[3] );
281
282 struct world_pass pass = {
283 .shader = k_shader_standard_vertex_blend,
284 .cam = cam,
285 .fn_bind_textures = bindpoint_diffuse_texture1,
286 .fn_set_mdl = shader_scene_vertex_blend_uMdl,
287 .fn_set_uPvmPrev = shader_scene_vertex_blend_uPvmPrev,
288 };
289
290 world_render_both_stages( world, &pass );
291 }
292
293 static void world_shader_standard_bind( world_instance *world, camera *cam ){
294 shader_scene_standard_use();
295 shader_scene_standard_uTexGarbage(0);
296 shader_scene_standard_uTexMain(1);
297 shader_scene_standard_uPv( cam->mtx.pv );
298
299 world_link_lighting_ub( world, _shader_scene_standard.id );
300 world_bind_position_texture( world, _shader_scene_standard.id,
301 _uniform_scene_standard_g_world_depth, 2 );
302 world_bind_light_array( world, _shader_scene_standard.id,
303 _uniform_scene_standard_uLightsArray, 3 );
304 world_bind_light_index( world, _shader_scene_standard.id,
305 _uniform_scene_standard_uLightsIndex, 4 );
306
307 bind_terrain_noise();
308 shader_scene_standard_uCamera( cam->transform[3] );
309 }
310
311 static void render_world_standard( world_instance *world, camera *cam ){
312 world_shader_standard_bind( world, cam );
313 struct world_pass pass = {
314 .shader = k_shader_standard,
315 .cam = cam,
316 .fn_bind_textures = bindpoint_diffuse_texture1,
317 .fn_set_mdl = shader_scene_standard_uMdl,
318 .fn_set_uPvmPrev = shader_scene_standard_uPvmPrev,
319 };
320
321 world_render_both_stages( world, &pass );
322 }
323
324 static void render_world_cubemapped( world_instance *world, camera *cam,
325 int layer_depth ){
326 if( !mdl_arrcount( &world->ent_cubemap ) )
327 return;
328
329 if( layer_depth == -1 ){
330 world_shader_standard_bind( world, cam );
331
332 struct world_pass pass = {
333 .shader = k_shader_cubemap,
334 .cam = cam,
335 .fn_bind_textures = bindpoint_diffuse_texture1,
336 .fn_set_mdl = shader_scene_standard_uMdl,
337 .fn_set_uPvmPrev = shader_scene_standard_uPvmPrev,
338 };
339
340 world_render_both_stages( world, &pass );
341 }
342 else {
343 shader_scene_cubemapped_use();
344 shader_scene_cubemapped_uTexGarbage(0);
345 shader_scene_cubemapped_uTexMain(1);
346 shader_scene_cubemapped_uTexCubemap(10);
347 shader_scene_cubemapped_uPv( cam->mtx.pv );
348
349 world_link_lighting_ub( world, _shader_scene_cubemapped.id );
350 world_bind_position_texture( world, _shader_scene_cubemapped.id,
351 _uniform_scene_cubemapped_g_world_depth, 2 );
352 world_bind_light_array( world, _shader_scene_cubemapped.id,
353 _uniform_scene_cubemapped_uLightsArray, 3 );
354 world_bind_light_index( world, _shader_scene_cubemapped.id,
355 _uniform_scene_cubemapped_uLightsIndex, 4 );
356
357 bind_terrain_noise();
358 shader_scene_cubemapped_uCamera( cam->transform[3] );
359
360 struct world_pass pass = {
361 .shader = k_shader_cubemap,
362 .cam = cam,
363 .fn_bind_textures = bindpoint_diffuse1_and_cubemap10,
364 .fn_set_mdl = shader_scene_cubemapped_uMdl,
365 .fn_set_uPvmPrev = shader_scene_cubemapped_uPvmPrev,
366 };
367
368 world_render_both_stages( world, &pass );
369 }
370 }
371
372 static void render_world_alphatest( world_instance *world, camera *cam ){
373 shader_scene_standard_alphatest_use();
374 shader_scene_standard_alphatest_uTexGarbage(0);
375 shader_scene_standard_alphatest_uTexMain(1);
376 shader_scene_standard_alphatest_uPv( cam->mtx.pv );
377
378 world_link_lighting_ub( world, _shader_scene_standard_alphatest.id );
379 world_bind_position_texture( world, _shader_scene_standard_alphatest.id,
380 _uniform_scene_standard_alphatest_g_world_depth, 2 );
381 world_bind_light_array( world, _shader_scene_standard_alphatest.id,
382 _uniform_scene_standard_alphatest_uLightsArray, 3 );
383 world_bind_light_index( world, _shader_scene_standard_alphatest.id,
384 _uniform_scene_standard_alphatest_uLightsIndex, 4 );
385
386
387 bind_terrain_noise();
388
389
390 shader_scene_standard_alphatest_uCamera( cam->transform[3] );
391
392 glDisable(GL_CULL_FACE);
393
394 struct world_pass pass = {
395 .shader = k_shader_standard_cutout,
396 .cam = cam,
397 .fn_bind_textures = bindpoint_diffuse_texture1,
398 .fn_set_mdl = shader_scene_standard_alphatest_uMdl,
399 .fn_set_uPvmPrev = shader_scene_standard_alphatest_uPvmPrev,
400 };
401
402 world_render_both_stages( world, &pass );
403
404 glEnable(GL_CULL_FACE);
405 }
406
407 static
408 void world_render_challenges( world_instance *world, struct world_pass *pass,
409 v3f pos, int layer_depth ){
410 if( !world ) return;
411 if( skaterift.activity == k_skaterift_replay ) return;
412 if( world != world_current_instance() ) return;
413
414 /* sort lists */
415 f32 radius = 40.0f;
416
417 u32 objective_list[ 32 ],
418 challenge_list[ 16 ];
419
420 v2f objective_uv_offsets[ 32 ];
421
422 u32 objective_count = 0,
423 challenge_count = 0;
424
425 ent_challenge *active_challenge = NULL;
426 int running = 0;
427 if( mdl_entity_id_type( world_static.focused_entity ) == k_ent_challenge ){
428 if( (skaterift.activity == k_skaterift_default) &&
429 world_static.challenge_target ){
430 running = 1;
431 }
432
433 if( !((skaterift.activity != k_skaterift_ent_focus) &&
434 !world_static.challenge_target) ){
435 world_instance *challenge_world = world_current_instance();
436 u32 index = mdl_entity_id_id( world_static.focused_entity );
437 active_challenge = mdl_arritm(&challenge_world->ent_challenge, index);
438 }
439 }
440
441 if( active_challenge ){
442 shader_scene_fxglow_uUvOffset( (v2f){ 8.0f/256.0f, 0.0f } );
443 challenge_list[ challenge_count ++ ] = world_static.focused_entity;
444
445 u32 next = active_challenge->first;
446 while( mdl_entity_id_type(next) == k_ent_objective ){
447 u32 index = mdl_entity_id_id( next );
448 objective_list[ objective_count ++ ] = index;
449
450 ent_objective *objective = mdl_arritm( &world->ent_objective, index );
451 next = objective->id_next;
452 }
453
454 radius = 10000.0f;
455 }
456 else {
457 shader_scene_fxglow_uUvOffset( (v2f){ 0.0f, 0.0f } );
458 bh_iter it;
459 bh_iter_init_range( 0, &it, pos, radius+10.0f );
460 i32 idx;
461 while( bh_next( world->entity_bh, &it, &idx ) ){
462 u32 id = world->entity_list[ idx ],
463 type = mdl_entity_id_type( id ),
464 index = mdl_entity_id_id( id );
465
466 if( type == k_ent_objective ) {
467 if( objective_count < vg_list_size(objective_list) )
468 objective_list[ objective_count ++ ] = index;
469 }
470 else if( type == k_ent_challenge ){
471 if( challenge_count < vg_list_size(challenge_list) )
472 challenge_list[ challenge_count ++ ] = index;
473 }
474 }
475 }
476
477 /* render objectives */
478 glDisable( GL_CULL_FACE );
479 mesh_bind( &world->mesh_no_collide );
480 u32 last_material = 0;
481 for( u32 i=0; i<objective_count; i++ ){
482 u32 index = objective_list[ i ];
483 ent_objective *objective = mdl_arritm( &world->ent_objective, index );
484 if( (objective->flags & k_ent_objective_hidden) &&
485 !active_challenge ) continue;
486
487 f32 scale = 1.0f;
488
489 if( running ){
490 u32 passed = objective->flags & k_ent_objective_passed;
491 f32 target = passed? 0.0f: 1.0f;
492 vg_slewf(&objective->transform.s[0], target, vg.time_frame_delta*4.0f);
493 scale = vg_smoothstepf( objective->transform.s[0] );
494
495 if( (objective == world_static.challenge_target) || passed )
496 shader_scene_fxglow_uUvOffset( (v2f){ 16.0f/256.0f, 0.0f } );
497 else
498 shader_scene_fxglow_uUvOffset( (v2f){ 8.0f/256.0f, 0.0f } );
499 }
500 else {
501 f32 dist = v3_dist( objective->transform.co, pos ) * (1.0f/radius);
502 scale = vg_smoothstepf( vg_clampf( 5.0f-dist*5.0f, 0.0f,1.0f ) );
503 }
504
505 m4x3f mmdl;
506 q_m3x3( objective->transform.q, mmdl );
507 m3x3_scalef( mmdl, scale );
508 v3_copy( objective->transform.co, mmdl[3] );
509 shader_scene_fxglow_uMdl( mmdl );
510
511 for( u32 j=0; j<objective->submesh_count; j++ ){
512 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs,
513 objective->submesh_start + j );
514
515 if( sm->material_id != last_material ){
516 last_material = sm->material_id;
517 pass->fn_bind_textures( world, &world->surfaces[sm->material_id] );
518 }
519 mdl_draw_submesh( sm );
520 }
521 }
522
523 /* render texts */
524 font3d_bind( &gui.font, k_font_shader_world, 0, world, &skaterift.cam );
525
526 char buf[32];
527 u32 count = 0;
528
529 for( u32 i=0; i<mdl_arrcount(&world->ent_challenge); i++ ){
530 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, i );
531 if( challenge->status ) count ++;
532 }
533
534 int c=0;
535 c+=highscore_intl( buf+c, count, 3 );
536 buf[c++] = '/';
537 c+=highscore_intl( buf+c, mdl_arrcount(&world->ent_challenge), 3 );
538 buf[c++] = '\0';
539
540 f32 w = font3d_string_width( 1, buf );
541 m4x3f mlocal;
542 m3x3_identity( mlocal );
543 mlocal[3][0] = -w*0.5f;
544 mlocal[3][1] = 0.0f;
545 mlocal[3][2] = 0.0f;
546
547 for( u32 i=0; i<challenge_count; i++ ){
548 u32 index = challenge_list[ i ];
549 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, index );
550 m4x3f mmdl;
551 mdl_transform_m4x3( &challenge->transform, mmdl );
552 m4x3_mul( mmdl, mlocal, mmdl );
553
554 vg_line_point( challenge->transform.co, 0.25f, VG__RED );
555
556 f32 dist = v3_dist( challenge->transform.co, pos ) * (1.0f/radius),
557 scale = vg_smoothstepf( vg_clampf( 10.0f-dist*10.0f, 0.0f,1.0f ) ),
558 colour = 0.0f;
559
560 if( challenge->status )
561 colour = 1.0f;
562
563 shader_scene_font_uOpacity( scale );
564 shader_scene_font_uColourize( colour );
565 font3d_simple_draw( 1, buf, &skaterift.cam, mmdl );
566 }
567 }
568
569 static void render_world_fxglow( world_instance *world, camera *cam,
570 int layer_depth ){
571 shader_scene_fxglow_use();
572 shader_scene_fxglow_uUvOffset( (v2f){ 0.0f, 0.0f } );
573 shader_scene_fxglow_uTexMain(1);
574 shader_scene_fxglow_uPv( cam->mtx.pv );
575
576 world_link_lighting_ub( world, _shader_scene_fxglow.id );
577 world_bind_position_texture( world, _shader_scene_fxglow.id,
578 _uniform_scene_fxglow_g_world_depth, 2 );
579 world_bind_light_array( world, _shader_scene_fxglow.id,
580 _uniform_scene_fxglow_uLightsArray, 3 );
581 world_bind_light_index( world, _shader_scene_fxglow.id,
582 _uniform_scene_fxglow_uLightsIndex, 4 );
583
584 shader_scene_fxglow_uCamera( cam->transform[3] );
585 glDisable(GL_CULL_FACE);
586
587 struct world_pass pass = {
588 .shader = k_shader_fxglow,
589 .cam = cam,
590 .fn_bind_textures = bindpoint_diffuse_texture1,
591 .fn_set_mdl = shader_scene_fxglow_uMdl,
592 .fn_set_uPvmPrev = shader_scene_fxglow_uPvmPrev,
593 };
594
595 world_render_both_stages( world, &pass );
596 world_render_challenges( world, &pass, cam->pos, layer_depth );
597
598 glEnable(GL_CULL_FACE);
599 }
600
601 static void bindpoint_terrain( world_instance *world,
602 struct world_surface *mat )
603 {
604 glActiveTexture( GL_TEXTURE1 );
605 glBindTexture( GL_TEXTURE_2D, world->textures[ mat->info.tex_diffuse ] );
606
607 shader_scene_terrain_uSandColour( mat->info.colour );
608 shader_scene_terrain_uBlendOffset( mat->info.colour1 );
609 }
610
611 static void bindpoint_override( world_instance *world,
612 struct world_surface *mat ){
613 if( mat->info.flags & k_material_flag_collision ){
614 shader_scene_override_uAlphatest(0);
615 }
616 else{
617 glActiveTexture( GL_TEXTURE1 );
618 glBindTexture( GL_TEXTURE_2D, world->textures[ mat->info.tex_diffuse ] );
619 shader_scene_override_uAlphatest(1);
620 }
621 }
622
623 static void render_terrain( world_instance *world, camera *cam ){
624 shader_scene_terrain_use();
625 shader_scene_terrain_uTexGarbage(0);
626 shader_scene_terrain_uTexGradients(1);
627
628 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_terrain );
629 glActiveTexture( GL_TEXTURE0 );
630 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
631
632 shader_scene_terrain_uPv( cam->mtx.pv );
633 shader_scene_terrain_uCamera( cam->transform[3] );
634
635 struct world_pass pass = {
636 .shader = k_shader_terrain_blend,
637 .cam = cam,
638 .fn_bind_textures = bindpoint_terrain,
639 .fn_set_mdl = shader_scene_terrain_uMdl,
640 .fn_set_uPvmPrev = shader_scene_terrain_uPvmPrev,
641 };
642
643 world_render_both_stages( world, &pass );
644 }
645
646 static void render_sky( world_instance *world, camera *cam )
647 {
648 /*
649 * Modify matrix to remove clipping and view translation
650 */
651 m4x4f v,
652 v_prev,
653 pv,
654 pv_prev;
655
656 m4x4_copy( cam->mtx.v, v );
657 m4x4_copy( cam->mtx_prev.v, v_prev );
658 v3_zero( v[3] );
659 v3_zero( v_prev[3] );
660
661 m4x4_copy( cam->mtx.p, pv );
662 m4x4_copy( cam->mtx_prev.p, pv_prev );
663 m4x4_reset_clipping( pv, cam->farz, cam->nearz );
664 m4x4_reset_clipping( pv_prev, cam->farz, cam->nearz );
665
666 m4x4_mul( pv, v, pv );
667 m4x4_mul( pv_prev, v_prev, pv_prev );
668
669 m4x3f identity_matrix;
670 m4x3_identity( identity_matrix );
671
672 /*
673 * Draw
674 */
675 shader_model_sky_use();
676 shader_model_sky_uMdl( identity_matrix );
677 shader_model_sky_uPv( pv );
678 shader_model_sky_uPvmPrev( pv_prev );
679 shader_model_sky_uTexGarbage(0);
680 world_link_lighting_ub( world, _shader_model_sky.id );
681
682 glActiveTexture( GL_TEXTURE0 );
683 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
684
685 glDepthMask( GL_FALSE );
686 glDisable( GL_DEPTH_TEST );
687
688 mesh_bind( &world_render.skydome );
689 mesh_draw( &world_render.skydome );
690
691 glEnable( GL_DEPTH_TEST );
692 glDepthMask( GL_TRUE );
693 }
694
695 static void render_world_gates( world_instance *world, camera *cam,
696 int layer_depth )
697 {
698 float closest = INFINITY;
699 struct ent_gate *gate = NULL;
700
701 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
702 ent_gate *gi = mdl_arritm( &world->ent_gate, i );
703
704 if( !(gi->flags & k_ent_gate_linked) )
705 continue;
706
707 float dist = v3_dist2( gi->co[0], cam->transform[3] );
708
709 vg_line_point( gi->co[0], 0.25f, VG__BLUE );
710
711 if( dist < closest ){
712 closest = dist;
713 gate = gi;
714 }
715 }
716
717 world->rendering_gate = gate;
718 if( gate ){
719 if( gate->flags & k_ent_gate_locked ) return;
720
721 if( gate->flags & k_ent_gate_nonlocal ){
722 if( world_static.load_state != k_world_loader_none ){
723 world->rendering_gate = NULL;
724 return;
725 }
726
727 world_instance *dest_world = &world_static.instances[ gate->target ];
728 render_gate( world, dest_world, gate, cam, layer_depth );
729 }
730 else{
731 render_gate( world, world, gate, cam, layer_depth );
732 }
733 }
734 }
735
736 static void world_prerender( world_instance *world ){
737 if( mdl_arrcount( &world->ent_light ) ){
738 f32 rate = vg_maxf(0.1f, fabsf(k_day_length)) * vg_signf(k_day_length);
739 world->time += vg.time_frame_delta * (1.0/(rate*60.0));
740 }
741 else{
742 world->time = 0.834;
743 }
744
745 struct ub_world_lighting *state = &world->ub_lighting;
746
747 state->g_time = world->time;
748 state->g_realtime = vg.time_real;
749 state->g_debug_indices = k_debug_light_indices;
750 state->g_light_preview = k_light_preview;
751 state->g_debug_complexity = k_debug_light_complexity;
752
753 if( skaterift.activity == k_skaterift_respawning )
754 state->g_time_of_day = 0.1f;
755 else
756 state->g_time_of_day = vg_fractf( world->time );
757
758 state->g_day_phase = cosf( state->g_time_of_day * VG_PIf * 2.0f );
759 state->g_sunset_phase= cosf( state->g_time_of_day * VG_PIf * 4.0f + VG_PIf );
760
761 state->g_day_phase = state->g_day_phase * 0.5f + 0.5f;
762 state->g_sunset_phase = powf( state->g_sunset_phase * 0.5f + 0.5f, 6.0f );
763
764 float a = state->g_time_of_day * VG_PIf * 2.0f;
765 state->g_sun_dir[0] = sinf( a );
766 state->g_sun_dir[1] = cosf( a );
767 state->g_sun_dir[2] = 0.2f;
768 v3_normalize( state->g_sun_dir );
769
770 world->probabilities[ k_probability_curve_constant ] = 1.0f;
771 float dp = state->g_day_phase;
772
773 world->probabilities[ k_probability_curve_wildlife_day ] =
774 (dp*dp*0.8f+state->g_sunset_phase)*0.8f;
775 world->probabilities[ k_probability_curve_wildlife_night ] =
776 1.0f-powf(fabsf((state->g_time_of_day-0.5f)*5.0f),5.0f);
777
778 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
779 glBufferSubData( GL_UNIFORM_BUFFER, 0,
780 sizeof(struct ub_world_lighting), &world->ub_lighting );
781 }
782
783 static void render_world( world_instance *world, camera *cam,
784 int layer_depth )
785 {
786 render_sky( world, cam );
787
788 render_world_routes( world, cam, layer_depth );
789 render_world_standard( world, cam );
790 render_world_cubemapped( world, cam, layer_depth );
791
792 render_world_vb( world, cam );
793 render_world_alphatest( world, cam );
794 render_terrain( world, cam );
795
796 if( layer_depth == -1 ) return;
797 if( layer_depth == 0 ){
798 world_entity_focus_render();
799
800 /* Render SFD's */
801 u32 closest = 0;
802 float min_dist = INFINITY;
803
804 if( mdl_arrcount( &world->ent_route ) ){
805 for( u32 i=0; i<mdl_arrcount( &world->ent_route ); i++ ){
806 ent_route *route = mdl_arritm( &world->ent_route, i );
807 float dist = v3_dist2( route->board_transform[3], cam->pos );
808
809 if( dist < min_dist ){
810 min_dist = dist;
811 closest = i;
812 }
813 }
814
815 ent_route *route = mdl_arritm( &world->ent_route, closest );
816 sfd_render( world, cam, route->board_transform );
817 }
818 }
819
820 f32 greyout = 0.0f;
821 if( mdl_entity_id_type(world_static.focused_entity) == k_ent_challenge )
822 greyout = world_static.focus_strength;
823
824 if( greyout > 0.0f ){
825 glDrawBuffers( 1, (GLenum[]){ GL_COLOR_ATTACHMENT0 } );
826 glEnable(GL_BLEND);
827 glDisable(GL_DEPTH_TEST);
828 glDepthMask(GL_FALSE);
829 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
830 glBlendEquation(GL_FUNC_ADD);
831
832 shader_blitcolour_use();
833 shader_blitcolour_uColour( (v4f){ 0.5f, 0.5f, 0.5f, greyout*0.56f } );
834 render_fsquad();
835
836 glDisable(GL_BLEND);
837 glEnable(GL_DEPTH_TEST);
838 glDepthMask(GL_TRUE);
839 glDrawBuffers( 2, (GLenum[]){ GL_COLOR_ATTACHMENT0,
840 GL_COLOR_ATTACHMENT1 } );
841 }
842
843 render_world_fxglow( world, cam, layer_depth );
844 }
845
846
847 static
848 void render_world_override_pass( world_instance *world,
849 struct world_pass *pass ){
850 for( int i=0; i<world->surface_count; i++ ){
851 struct world_surface *mat = &world->surfaces[i];
852
853 if( mat->info.flags & k_material_flag_ghosts ) continue;
854
855 mdl_submesh *sm;
856 if( pass->geo_type == k_world_geo_type_solid )
857 sm = &mat->sm_geo;
858 else
859 sm = &mat->sm_no_collide;
860
861 if( !sm->indice_count )
862 continue;
863
864 m4x3f mmdl;
865 m4x3_identity( mmdl );
866 pass->fn_set_mdl( mmdl );
867 pass->fn_set_uPvmPrev( pass->cam->mtx_prev.pv );
868 pass->fn_bind_textures( world, mat );
869 mdl_draw_submesh( sm );
870 }
871 }
872
873 static void render_world_override( world_instance *world ){
874 struct world_pass pass = {
875 .cam = &skaterift.cam,
876 .fn_bind_textures = bindpoint_override,
877 .fn_set_mdl = shader_scene_override_uMdl,
878 .fn_set_uPvmPrev = shader_scene_override_uPvmPrev,
879 .shader = k_shader_override
880 };
881
882 shader_scene_override_use();
883 respawn_chooser_shader_uniforms();
884 shader_scene_override_uTexGarbage(0);
885 shader_scene_override_uTexMain(1);
886 shader_scene_override_uPv( pass.cam->mtx.pv );
887
888 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_override );
889 bind_terrain_noise();
890 shader_scene_override_uCamera( pass.cam->transform[3] );
891
892 glDisable( GL_CULL_FACE );
893 mesh_bind( &world->mesh_geo );
894 pass.geo_type = k_world_geo_type_solid;
895 render_world_override_pass( world, &pass );
896 mesh_bind( &world->mesh_no_collide );
897 pass.geo_type = k_world_geo_type_nonsolid;
898 render_world_override_pass( world, &pass );
899 glEnable( GL_CULL_FACE );
900 }
901
902 static void render_cubemap_side( world_instance *world, ent_cubemap *cm,
903 u32 side ){
904 camera cam;
905 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
906 GL_TEXTURE_CUBE_MAP_POSITIVE_X + side, cm->texture_id, 0 );
907 glClear( GL_DEPTH_BUFFER_BIT );
908
909 v3f forward[6] = {
910 { -1.0f, 0.0f, 0.0f },
911 { 1.0f, 0.0f, 0.0f },
912 { 0.0f, -1.0f, 0.0f },
913 { 0.0f, 1.0f, 0.0f },
914 { 0.0f, 0.0f, -1.0f },
915 { 0.0f, 0.0f, 1.0f }
916 };
917 v3f up[6] = {
918 { 0.0f, -1.0f, 0.0f },
919 { 0.0f, -1.0f, 0.0f },
920 { 0.0f, 0.0f, 1.0f },
921 { 0.0f, 0.0f, -1.0f },
922 { 0.0f, -1.0f, 0.0f },
923 { 0.0f, -1.0f, 0.0f }
924 };
925
926 v3_zero( cam.angles );
927 v3_copy( cm->co, cam.pos );
928
929 v3_copy( forward[side], cam.transform[2] );
930 v3_copy( up[side], cam.transform[1] );
931 v3_cross( up[side], forward[side], cam.transform[0] );
932 v3_copy( cm->co, cam.transform[3] );
933 m4x3_invert_affine( cam.transform, cam.transform_inverse );
934
935 camera_update_view( &cam );
936
937 cam.nearz = 0.1f;
938 cam.farz = 1000.0f;
939 cam.fov = 90.0f;
940 m4x4_copy( cam.mtx.p, cam.mtx_prev.p );
941 m4x4_projection( cam.mtx.p, cam.fov, 1.0f, cam.nearz, cam.farz );
942 camera_finalize( &cam );
943 camera_finalize( &cam );
944
945 render_world( world, &cam, -1 );
946 }
947
948 static void render_world_cubemaps( world_instance *world ){
949 if( world->cubemap_cooldown )
950 world->cubemap_cooldown --;
951 else{
952 world->cubemap_cooldown = 60;
953
954 glViewport( 0, 0, WORLD_CUBEMAP_RES, WORLD_CUBEMAP_RES );
955 for( u32 i=0; i<mdl_arrcount( &world->ent_cubemap ); i++ ){
956 ent_cubemap *cm = mdl_arritm( &world->ent_cubemap, i );
957 glBindFramebuffer( GL_FRAMEBUFFER, cm->framebuffer_id );
958
959 world->cubemap_side ++;
960 if( world->cubemap_side >= 6 )
961 world->cubemap_side = 0;
962
963 render_cubemap_side( world, cm, world->cubemap_side );
964 }
965 }
966 }
967
968 static void render_world_depth( world_instance *world, camera *cam ){
969 m4x3f identity_matrix;
970 m4x3_identity( identity_matrix );
971
972 shader_scene_depth_use();
973 shader_scene_depth_uCamera( cam->transform[3] );
974 shader_scene_depth_uPv( cam->mtx.pv );
975 shader_scene_depth_uPvmPrev( cam->mtx_prev.pv );
976 shader_scene_depth_uMdl( identity_matrix );
977 world_link_lighting_ub( world, _shader_scene_depth.id );
978
979 mesh_bind( &world->mesh_geo );
980 mesh_draw( &world->mesh_geo );
981 }
982
983 static void render_world_position( world_instance *world, camera *cam ){
984 m4x3f identity_matrix;
985 m4x3_identity( identity_matrix );
986
987 shader_scene_position_use();
988 shader_scene_position_uCamera( cam->transform[3] );
989 shader_scene_position_uPv( cam->mtx.pv );
990 shader_scene_position_uPvmPrev( cam->mtx_prev.pv );
991 shader_scene_position_uMdl( identity_matrix );
992 world_link_lighting_ub( world, _shader_scene_position.id );
993
994 mesh_bind( &world->mesh_geo );
995 mesh_draw( &world->mesh_geo );
996 }
997
998 #endif