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