fix quality profile water/shadows
[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 "world_map.h"
13 #include "ent_miniworld.h"
14 #include "player_remote.h"
15 #include "ent_skateshop.h"
16
17 static int ccmd_set_time( int argc, const char *argv[] ){
18 world_instance *world = world_current_instance();
19 if( argc == 1 )
20 world->time = atof( argv[0] );
21 else
22 vg_error( "Usage set_time <0-1.0> (current time: %f)\n", world->time );
23 return 0;
24 }
25
26 static void async_world_render_init( void *payload, u32 size ){
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 VG_VAR_F32( k_day_length );
70 VG_VAR_I32( k_debug_light_indices );
71 VG_VAR_I32( k_debug_light_complexity );
72 VG_VAR_I32( k_light_preview );
73 vg_console_reg_cmd( "set_time", ccmd_set_time, NULL );
74
75 world_render.sky_rate = 1.0;
76 world_render.sky_target_rate = 1.0;
77
78 shader_scene_standard_register();
79 shader_scene_standard_alphatest_register();
80 shader_scene_foliage_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 shader_model_sky_space_register();
90
91 vg_info( "Loading world resources\n" );
92 vg_linear_clear( vg_mem.scratch );
93
94 mdl_context msky;
95 mdl_open( &msky, "models/rs_skydome.mdl", vg_mem.scratch );
96 mdl_load_metadata_block( &msky, vg_mem.scratch );
97 mdl_async_load_glmesh( &msky, &world_render.skydome, NULL );
98 mdl_close( &msky );
99
100 vg_info( "Loading default world textures\n" );
101 vg_tex2d_load_qoi_async_file( "textures/garbage.qoi",
102 VG_TEX2D_NEAREST|VG_TEX2D_REPEAT,
103 &world_render.tex_terrain_noise );
104
105 vg_async_call( async_world_render_init, NULL, 0 );
106 }
107
108 /*
109 * standard uniform bindings
110 * ----------------------------------------------------------------------------
111 */
112 static void world_link_lighting_ub( world_instance *world, GLuint shader ){
113 GLuint idx = glGetUniformBlockIndex( shader, "ub_world_lighting" );
114 glUniformBlockBinding( shader, idx, world->ubo_bind_point );
115 }
116
117 static void world_bind_position_texture( world_instance *world,
118 GLuint shader, GLuint location,
119 int slot ){
120 render_fb_bind_texture( &world->heightmap, 0, slot );
121 glUniform1i( location, slot );
122 }
123
124 static void world_bind_light_array( world_instance *world,
125 GLuint shader, GLuint location,
126 int slot ){
127 glActiveTexture( GL_TEXTURE0 + slot );
128 glBindTexture( GL_TEXTURE_BUFFER, world->tex_light_entities );
129 glUniform1i( location, slot );
130 }
131
132 static void world_bind_light_index( world_instance *world,
133 GLuint shader, GLuint location,
134 int slot ){
135 glActiveTexture( GL_TEXTURE0 + slot );
136 glBindTexture( GL_TEXTURE_3D, world->tex_light_cubes );
137 glUniform1i( location, slot );
138 }
139
140 static void bind_terrain_noise(void){
141 glActiveTexture( GL_TEXTURE0 );
142 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
143 }
144
145 /*
146 * Get OpenGL texture name from texture ID.
147 */
148 static GLuint world_get_texture( world_instance *world, u32 id ){
149 if( id & 0x80000000 ) return skaterift.rt_textures[id & ~0x80000000];
150 else return world->textures[ id ];
151 }
152
153 static void bindpoint_diffuse_texture1( world_instance *world,
154 struct world_surface *mat ){
155 glActiveTexture( GL_TEXTURE1 );
156 glBindTexture( GL_TEXTURE_2D,
157 world_get_texture(world,mat->info.tex_diffuse) );
158 }
159
160 /*
161 * Passes Rendering
162 * ----------------------------------------------------------------------------
163 */
164
165 struct world_pass{
166 camera *cam;
167 enum mdl_shader shader;
168 enum world_geo_type geo_type;
169
170 void (*fn_bind_textures)( world_instance *world,
171 struct world_surface *mat );
172 void (*fn_set_mdl)( m4x3f mdl );
173 void (*fn_set_uPvmPrev)( m4x4f pvm );
174 void (*fn_set_uNormalMtx)( m3x3f mnorm );
175 };
176
177 static void render_world_depth( world_instance *world, camera *cam );
178
179 /*
180 * Render a run of submeshes, only of those which match material_id
181 */
182 static void world_render_submeshes( world_instance *world,
183 struct world_pass *pass,
184 mdl_transform *transform,
185 u32 start, u32 count, u32 material_id ){
186 for( u32 k=0; k<count; k++ ){
187 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs, start+k );
188 if( sm->material_id != material_id ) continue;
189
190 m4x3f mmdl;
191 mdl_transform_m4x3( transform, mmdl );
192
193 m4x4f m4mdl;
194 m4x3_expand( mmdl, m4mdl );
195 m4x4_mul( pass->cam->mtx_prev.pv, m4mdl, m4mdl );
196
197 pass->fn_set_mdl( mmdl );
198 pass->fn_set_uPvmPrev( m4mdl );
199
200 mdl_draw_submesh( sm );
201 }
202 }
203
204 /*
205 * Render props attached to this material
206 */
207 static void world_render_props( world_instance *world, u32 material_id,
208 struct world_pass *pass ){
209 struct world_surface *mat = &world->surfaces[ material_id ];
210 if( !(mat->flags & WORLD_SURFACE_HAS_PROPS) ) return;
211
212 pass->fn_bind_textures( world, mat );
213
214 for( u32 j=0; j<mdl_arrcount( &world->ent_prop ); j++ ){
215 ent_prop *prop = mdl_arritm( &world->ent_prop, j );
216 if( prop->flags & 0x1 ) continue;
217
218 world_render_submeshes( world, pass, &prop->transform,
219 prop->submesh_start, prop->submesh_count, material_id );
220 }
221 }
222
223 /*
224 * Render traffic models attactched to this material
225 */
226 static void world_render_traffic( world_instance *world, u32 material_id,
227 struct world_pass *pass ){
228 struct world_surface *mat = &world->surfaces[ material_id ];
229 if( !(mat->flags & WORLD_SURFACE_HAS_TRAFFIC) ) return;
230
231 pass->fn_bind_textures( world, mat );
232
233 for( u32 j=0; j<mdl_arrcount( &world->ent_traffic ); j++ ){
234 ent_traffic *traffic = mdl_arritm( &world->ent_traffic, j );
235
236 world_render_submeshes( world, pass, &traffic->transform,
237 traffic->submesh_start, traffic->submesh_count,
238 material_id );
239 }
240 }
241
242 /*
243 * Iterate and render all materials which match the passes shader and geometry
244 * type. Includes props/traffic.
245 */
246 static void world_render_pass( world_instance *world, struct world_pass *pass ){
247 for( int i=0; i<world->surface_count; i++ ){
248 struct world_surface *mat = &world->surfaces[i];
249
250 if( mat->info.shader == pass->shader ){
251 mdl_submesh *sm;
252
253 if( pass->geo_type == k_world_geo_type_solid ){
254 sm = &mat->sm_geo;
255 }
256 else{
257 world_render_traffic( world, i, pass );
258 world_render_props( world, i, pass );
259 sm = &mat->sm_no_collide;
260 }
261
262 if( !sm->indice_count )
263 continue;
264
265 m4x3f mmdl;
266 m4x3_identity( mmdl );
267 pass->fn_set_mdl( mmdl );
268 pass->fn_set_uPvmPrev( pass->cam->mtx_prev.pv );
269
270 pass->fn_bind_textures( world, mat );
271 mdl_draw_submesh( sm );
272 }
273 }
274 }
275
276 /*
277 * Specific shader instructions
278 * ----------------------------------------------------------------------------
279 */
280
281 static void world_render_both_stages( world_instance *world,
282 struct world_pass *pass ){
283 mesh_bind( &world->mesh_geo );
284 pass->geo_type = k_world_geo_type_solid;
285 world_render_pass( world, pass );
286
287 glDisable( GL_CULL_FACE );
288 mesh_bind( &world->mesh_no_collide );
289 pass->geo_type = k_world_geo_type_nonsolid;
290 world_render_pass( world, pass );
291 glEnable( GL_CULL_FACE );
292 }
293
294 static void render_world_vb( world_instance *world, camera *cam ){
295 shader_scene_vertex_blend_use();
296 shader_scene_vertex_blend_uTexGarbage(0);
297 shader_scene_vertex_blend_uTexGradients(1);
298 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_vertex_blend );
299
300 glActiveTexture( GL_TEXTURE0 );
301 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
302
303 shader_scene_vertex_blend_uPv( cam->mtx.pv );
304 shader_scene_vertex_blend_uCamera( cam->transform[3] );
305
306 struct world_pass pass = {
307 .shader = k_shader_standard_vertex_blend,
308 .cam = cam,
309 .fn_bind_textures = bindpoint_diffuse_texture1,
310 .fn_set_mdl = shader_scene_vertex_blend_uMdl,
311 .fn_set_uPvmPrev = shader_scene_vertex_blend_uPvmPrev,
312 };
313
314 world_render_both_stages( world, &pass );
315 }
316
317 static void world_shader_standard_bind( world_instance *world, camera *cam ){
318 shader_scene_standard_use();
319 shader_scene_standard_uTexGarbage(0);
320 shader_scene_standard_uTexMain(1);
321 shader_scene_standard_uPv( cam->mtx.pv );
322 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_standard );
323
324 bind_terrain_noise();
325 shader_scene_standard_uCamera( cam->transform[3] );
326 }
327
328 static void render_world_standard( world_instance *world, camera *cam ){
329 world_shader_standard_bind( world, cam );
330 struct world_pass pass = {
331 .shader = k_shader_standard,
332 .cam = cam,
333 .fn_bind_textures = bindpoint_diffuse_texture1,
334 .fn_set_mdl = shader_scene_standard_uMdl,
335 .fn_set_uPvmPrev = shader_scene_standard_uPvmPrev,
336 };
337
338 world_render_both_stages( world, &pass );
339 }
340
341 static void bindpoint_diffuse1_and_cubemap10( world_instance *world,
342 struct world_surface *mat ){
343 glActiveTexture( GL_TEXTURE1 );
344 glBindTexture( GL_TEXTURE_2D,
345 world_get_texture(world,mat->info.tex_diffuse) );
346
347 u32 cubemap_id = mat->info.tex_none0,
348 cubemap_index = 0;
349
350 if( mdl_entity_id_type( cubemap_id ) == k_ent_cubemap ){
351 cubemap_index = mdl_entity_id_id( cubemap_id );
352 }
353
354 ent_cubemap *cm = mdl_arritm( &world->ent_cubemap, cubemap_index );
355 glActiveTexture( GL_TEXTURE10 );
356 glBindTexture( GL_TEXTURE_CUBE_MAP, cm->texture_id );
357
358 shader_scene_cubemapped_uColour( mat->info.colour );
359 }
360
361 static void render_world_cubemapped( world_instance *world, camera *cam,
362 int enabled ){
363 if( !mdl_arrcount( &world->ent_cubemap ) )
364 return;
365
366 if( !enabled ){
367 world_shader_standard_bind( world, cam );
368
369 struct world_pass pass = {
370 .shader = k_shader_cubemap,
371 .cam = cam,
372 .fn_bind_textures = bindpoint_diffuse_texture1,
373 .fn_set_mdl = shader_scene_standard_uMdl,
374 .fn_set_uPvmPrev = shader_scene_standard_uPvmPrev,
375 };
376
377 world_render_both_stages( world, &pass );
378 }
379 else {
380 shader_scene_cubemapped_use();
381 shader_scene_cubemapped_uTexGarbage(0);
382 shader_scene_cubemapped_uTexMain(1);
383 shader_scene_cubemapped_uTexCubemap(10);
384 shader_scene_cubemapped_uPv( cam->mtx.pv );
385
386 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_cubemapped );
387
388 bind_terrain_noise();
389 shader_scene_cubemapped_uCamera( cam->transform[3] );
390
391 struct world_pass pass = {
392 .shader = k_shader_cubemap,
393 .cam = cam,
394 .fn_bind_textures = bindpoint_diffuse1_and_cubemap10,
395 .fn_set_mdl = shader_scene_cubemapped_uMdl,
396 .fn_set_uPvmPrev = shader_scene_cubemapped_uPvmPrev,
397 };
398
399 world_render_both_stages( world, &pass );
400 }
401 }
402
403 static void render_world_alphatest( world_instance *world, camera *cam ){
404 shader_scene_standard_alphatest_use();
405 shader_scene_standard_alphatest_uTexGarbage(0);
406 shader_scene_standard_alphatest_uTexMain(1);
407 shader_scene_standard_alphatest_uPv( cam->mtx.pv );
408
409 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_standard_alphatest );
410
411 bind_terrain_noise();
412 shader_scene_standard_alphatest_uCamera( cam->transform[3] );
413 glDisable(GL_CULL_FACE);
414
415 struct world_pass pass = {
416 .shader = k_shader_standard_cutout,
417 .cam = cam,
418 .fn_bind_textures = bindpoint_diffuse_texture1,
419 .fn_set_mdl = shader_scene_standard_alphatest_uMdl,
420 .fn_set_uPvmPrev = shader_scene_standard_alphatest_uPvmPrev,
421 };
422
423 world_render_both_stages( world, &pass );
424 glEnable(GL_CULL_FACE);
425 }
426
427 static void render_world_foliage( world_instance *world, camera *cam ){
428 shader_scene_foliage_use();
429 shader_scene_foliage_uTexGarbage(0);
430 shader_scene_foliage_uTexMain(1);
431 shader_scene_foliage_uPv( cam->mtx.pv );
432 shader_scene_foliage_uTime( vg.time );
433
434 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_foliage );
435 bind_terrain_noise();
436
437 shader_scene_foliage_uCamera( cam->transform[3] );
438 glDisable(GL_CULL_FACE);
439 struct world_pass pass = {
440 .shader = k_shader_foliage,
441 .cam = cam,
442 .fn_bind_textures = bindpoint_diffuse_texture1,
443 .fn_set_mdl = shader_scene_foliage_uMdl,
444 .fn_set_uPvmPrev = shader_scene_foliage_uPvmPrev,
445 };
446 world_render_both_stages( world, &pass );
447 glEnable(GL_CULL_FACE);
448 }
449
450 static void world_render_challenges( world_instance *world,
451 struct world_pass *pass, v3f pos ){
452 if( !world ) return;
453 if( skaterift.activity == k_skaterift_replay ) return;
454 if( world != world_current_instance() ) return;
455
456 /* sort lists */
457 f32 radius = 40.0f;
458
459 u32 objective_list[ 32 ],
460 challenge_list[ 16 ];
461
462 v2f objective_uv_offsets[ 32 ];
463
464 u32 objective_count = 0,
465 challenge_count = 0;
466
467 ent_challenge *active_challenge = NULL;
468 int running = 0;
469 if( mdl_entity_id_type( world_static.focused_entity ) == k_ent_challenge ){
470 if( (skaterift.activity == k_skaterift_default) &&
471 world_static.challenge_target ){
472 running = 1;
473 }
474
475 if( !((skaterift.activity != k_skaterift_ent_focus) &&
476 !world_static.challenge_target) ){
477 world_instance *challenge_world = world_current_instance();
478 u32 index = mdl_entity_id_id( world_static.focused_entity );
479 active_challenge = mdl_arritm(&challenge_world->ent_challenge, index);
480 }
481 }
482
483 if( active_challenge ){
484 shader_scene_fxglow_uUvOffset( (v2f){ 8.0f/256.0f, 0.0f } );
485 challenge_list[ challenge_count ++ ] = world_static.focused_entity;
486
487 u32 next = active_challenge->first;
488 while( mdl_entity_id_type(next) == k_ent_objective ){
489 u32 index = mdl_entity_id_id( next );
490 objective_list[ objective_count ++ ] = index;
491
492 ent_objective *objective = mdl_arritm( &world->ent_objective, index );
493 next = objective->id_next;
494 }
495
496 radius = 10000.0f;
497 }
498 else {
499 shader_scene_fxglow_uUvOffset( (v2f){ 0.0f, 0.0f } );
500 bh_iter it;
501 bh_iter_init_range( 0, &it, pos, radius+10.0f );
502 i32 idx;
503 while( bh_next( world->entity_bh, &it, &idx ) ){
504 u32 id = world->entity_list[ idx ],
505 type = mdl_entity_id_type( id ),
506 index = mdl_entity_id_id( id );
507
508 if( type == k_ent_objective ) {
509 if( objective_count < vg_list_size(objective_list) )
510 objective_list[ objective_count ++ ] = index;
511 }
512 else if( type == k_ent_challenge ){
513 if( challenge_count < vg_list_size(challenge_list) )
514 challenge_list[ challenge_count ++ ] = index;
515 }
516 }
517 }
518
519 /* render objectives */
520 glDisable( GL_CULL_FACE );
521 mesh_bind( &world->mesh_no_collide );
522 u32 last_material = 0;
523 for( u32 i=0; i<objective_count; i++ ){
524 u32 index = objective_list[ i ];
525 ent_objective *objective = mdl_arritm( &world->ent_objective, index );
526 if( (objective->flags & k_ent_objective_hidden) &&
527 !active_challenge ) continue;
528
529 f32 scale = 1.0f;
530
531 if( running ){
532 u32 passed = objective->flags & k_ent_objective_passed;
533 f32 target = passed? 0.0f: 1.0f;
534 vg_slewf(&objective->transform.s[0], target, vg.time_frame_delta*4.0f);
535 scale = vg_smoothstepf( objective->transform.s[0] );
536
537 if( (objective == world_static.challenge_target) || passed )
538 shader_scene_fxglow_uUvOffset( (v2f){ 16.0f/256.0f, 0.0f } );
539 else
540 shader_scene_fxglow_uUvOffset( (v2f){ 8.0f/256.0f, 0.0f } );
541 }
542 else {
543 f32 dist = v3_dist( objective->transform.co, pos ) * (1.0f/radius);
544 scale = vg_smoothstepf( vg_clampf( 5.0f-dist*5.0f, 0.0f,1.0f ) );
545 }
546
547 m4x3f mmdl;
548 q_m3x3( objective->transform.q, mmdl );
549 m3x3_scalef( mmdl, scale );
550 v3_copy( objective->transform.co, mmdl[3] );
551 shader_scene_fxglow_uMdl( mmdl );
552
553 for( u32 j=0; j<objective->submesh_count; j++ ){
554 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs,
555 objective->submesh_start + j );
556
557 if( sm->material_id != last_material ){
558 last_material = sm->material_id;
559 pass->fn_bind_textures( world, &world->surfaces[sm->material_id] );
560 }
561 mdl_draw_submesh( sm );
562 }
563 }
564
565 /* render texts */
566 font3d_bind( &gui.font, k_font_shader_world, 0, world, &skaterift.cam );
567
568 u32 count = 0;
569
570 for( u32 i=0; i<mdl_arrcount(&world->ent_challenge); i++ )
571 {
572 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, i );
573 if( challenge->status ) count ++;
574 }
575
576 char buf[32];
577 vg_str str;
578 vg_strnull( &str, buf, sizeof(buf) );
579 vg_strcati32( &str, count );
580 vg_strcatch( &str, '/' );
581 vg_strcati32( &str, mdl_arrcount(&world->ent_challenge) );
582
583 f32 w = font3d_string_width( 1, buf );
584 m4x3f mlocal;
585 m3x3_identity( mlocal );
586 mlocal[3][0] = -w*0.5f;
587 mlocal[3][1] = 0.0f;
588 mlocal[3][2] = 0.0f;
589
590 for( u32 i=0; i<challenge_count; i++ )
591 {
592 u32 index = challenge_list[ i ];
593 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, index );
594 m4x3f mmdl;
595 mdl_transform_m4x3( &challenge->transform, mmdl );
596 m4x3_mul( mmdl, mlocal, mmdl );
597
598 vg_line_point( challenge->transform.co, 0.25f, VG__RED );
599
600 f32 dist = v3_dist( challenge->transform.co, pos ) * (1.0f/radius),
601 scale = vg_smoothstepf( vg_clampf( 10.0f-dist*10.0f, 0.0f,1.0f ) ),
602 colour = 0.0f;
603
604 if( challenge->status )
605 colour = 1.0f;
606
607 shader_scene_font_uOpacity( scale );
608 shader_scene_font_uColourize( colour );
609 font3d_simple_draw( 1, buf, &skaterift.cam, mmdl );
610 }
611 }
612
613 static void render_world_fxglow( world_instance *host_world,
614 world_instance *world, camera *cam,
615 m4x3f world_mmdl,
616 int generic, int challenges, int regions ){
617 shader_scene_fxglow_use();
618 shader_scene_fxglow_uUvOffset( (v2f){ 0.0f, 0.0f } );
619 shader_scene_fxglow_uTexMain(1);
620 shader_scene_fxglow_uPv( cam->mtx.pv );
621 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_fxglow );
622
623 shader_scene_fxglow_uCamera( cam->transform[3] );
624 glDisable(GL_CULL_FACE);
625
626 struct world_pass pass = {
627 .shader = k_shader_fxglow,
628 .cam = cam,
629 .fn_bind_textures = bindpoint_diffuse_texture1,
630 .fn_set_mdl = shader_scene_fxglow_uMdl,
631 .fn_set_uPvmPrev = shader_scene_fxglow_uPvmPrev,
632 };
633
634 if( generic )
635 world_render_both_stages( world, &pass );
636
637 if( regions ){
638 mesh_bind( &world->mesh_no_collide );
639
640 u32 last_material = 0;
641 for( u32 i=0; i<mdl_arrcount(&world->ent_region); i ++ ){
642 shader_scene_fxglow_uUvOffset( (v2f){ 0.0f, 0.0f } );
643 ent_region *region = mdl_arritm( &world->ent_region, i );
644
645 f32 offset = 0.0f;
646 if( region->flags & k_ent_route_flag_achieve_gold )
647 offset = 2.0f;
648 else if( region->flags & k_ent_route_flag_achieve_silver )
649 offset = 1.0f;
650
651 shader_scene_fxglow_uUvOffset( (v2f){ (8.0f/256.0f)*offset, 0.0f } );
652
653 m4x3f mmdl;
654 mdl_transform_m4x3( &region->transform, mmdl );
655 m4x3_mul( world_mmdl, mmdl, mmdl );
656 shader_scene_fxglow_uMdl( mmdl );
657
658 for( u32 j=0; j<region->submesh_count; j++ ){
659 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs,
660 region->submesh_start + j );
661
662 if( sm->material_id != last_material ){
663 last_material = sm->material_id;
664 pass.fn_bind_textures(world,&world->surfaces[sm->material_id]);
665 }
666 mdl_draw_submesh( sm );
667 }
668 }
669 }
670
671 if( challenges )
672 world_render_challenges( world, &pass, cam->pos );
673
674 glEnable(GL_CULL_FACE);
675 }
676
677 static void bindpoint_terrain( world_instance *world,
678 struct world_surface *mat )
679 {
680 glActiveTexture( GL_TEXTURE1 );
681 glBindTexture( GL_TEXTURE_2D,
682 world_get_texture(world,mat->info.tex_diffuse) );
683
684 shader_scene_terrain_uSandColour( mat->info.colour );
685 shader_scene_terrain_uBlendOffset( mat->info.colour1 );
686 }
687
688 static void bindpoint_override( world_instance *world,
689 struct world_surface *mat ){
690 if( mat->info.flags & k_material_flag_collision ){
691 shader_scene_override_uAlphatest(0);
692 }
693 else{
694 glActiveTexture( GL_TEXTURE1 );
695 glBindTexture( GL_TEXTURE_2D,
696 world_get_texture(world,mat->info.tex_diffuse) );
697 shader_scene_override_uAlphatest(1);
698 }
699 }
700
701 static void render_terrain( world_instance *world, camera *cam ){
702 shader_scene_terrain_use();
703 shader_scene_terrain_uTexGarbage(0);
704 shader_scene_terrain_uTexGradients(1);
705
706 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_terrain );
707 glActiveTexture( GL_TEXTURE0 );
708 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
709
710 shader_scene_terrain_uPv( cam->mtx.pv );
711 shader_scene_terrain_uCamera( cam->transform[3] );
712
713 struct world_pass pass = {
714 .shader = k_shader_terrain_blend,
715 .cam = cam,
716 .fn_bind_textures = bindpoint_terrain,
717 .fn_set_mdl = shader_scene_terrain_uMdl,
718 .fn_set_uPvmPrev = shader_scene_terrain_uPvmPrev,
719 };
720
721 world_render_both_stages( world, &pass );
722 }
723
724 static void render_sky( world_instance *world, camera *cam ){
725 /*
726 * Modify matrix to remove clipping and view translation
727 */
728 m4x4f v,
729 v_prev,
730 pv,
731 pv_prev;
732
733 m4x4_copy( cam->mtx.v, v );
734 m4x4_copy( cam->mtx_prev.v, v_prev );
735
736 for( int i=0; i<3; i++ ){
737 v3_normalize(v[i]);
738 v3_normalize(v_prev[i]);
739 }
740 v3_zero( v[3] );
741 v3_zero( v_prev[3] );
742
743 m4x4_copy( cam->mtx.p, pv );
744 m4x4_copy( cam->mtx_prev.p, pv_prev );
745 m4x4_reset_clipping( pv, 100.0f, 0.1f );
746 m4x4_reset_clipping( pv_prev, 100.0f, 0.1f );
747
748 m4x4_mul( pv, v, pv );
749 m4x4_mul( pv_prev, v_prev, pv_prev );
750
751 m4x3f identity_matrix;
752 m4x3_identity( identity_matrix );
753
754 /*
755 * Draw
756 */
757 if( world->skybox == k_skybox_default ){
758 shader_model_sky_use();
759 shader_model_sky_uMdl( identity_matrix );
760 shader_model_sky_uPv( pv );
761 shader_model_sky_uPvmPrev( pv_prev );
762 shader_model_sky_uTexGarbage(0);
763 world_link_lighting_ub( world, _shader_model_sky.id );
764
765 glActiveTexture( GL_TEXTURE0 );
766 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
767 }
768 else if( world->skybox == k_skybox_space ){
769 shader_model_sky_space_use();
770
771 shader_model_sky_space_uMdl( identity_matrix );
772 shader_model_sky_space_uPv( pv );
773 shader_model_sky_space_uPvmPrev( pv_prev );
774 shader_model_sky_space_uTexGarbage(0);
775 world_link_lighting_ub( world, _shader_model_sky_space.id );
776
777 glActiveTexture( GL_TEXTURE0 );
778 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
779 }
780 else {
781 assert(0);
782 }
783
784 glDepthMask( GL_FALSE );
785 glDisable( GL_DEPTH_TEST );
786
787 mesh_bind( &world_render.skydome );
788 mesh_draw( &world_render.skydome );
789
790 glEnable( GL_DEPTH_TEST );
791 glDepthMask( GL_TRUE );
792 }
793
794 static void render_world_gates( world_instance *world, camera *cam ){
795 float closest = INFINITY;
796 struct ent_gate *gate = NULL;
797
798 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
799 ent_gate *gi = mdl_arritm( &world->ent_gate, i );
800
801 if( !(gi->flags & k_ent_gate_nonlocal) )
802 if( !(gi->flags & k_ent_gate_linked) )
803 continue;
804
805 float dist = v3_dist2( gi->co[0], cam->transform[3] );
806
807 vg_line_point( gi->co[0], 0.25f, VG__BLUE );
808
809 if( dist < closest ){
810 closest = dist;
811 gate = gi;
812 }
813 }
814
815 world->rendering_gate = gate;
816
817 if( gate ){
818 if( gate->flags & k_ent_gate_locked ){
819 world->rendering_gate = NULL;
820 return;
821 }
822
823 if( gate->flags & k_ent_gate_nonlocal ){
824 if( !(gate->flags & k_ent_gate_linked) ||
825 (world_static.load_state != k_world_loader_none) ){
826 world->rendering_gate = NULL;
827 render_gate_unlinked( world, gate, cam );
828 return;
829 }
830
831 world_instance *dest_world = &world_static.instances[ gate->target ];
832 render_gate( world, dest_world, gate, cam );
833 }
834 else
835 render_gate( world, world, gate, cam );
836 }
837 }
838
839 static void world_prerender( world_instance *world ){
840 if( mdl_arrcount( &world->ent_light ) ){
841 f32 rate = vg_maxf(0.1f, fabsf(k_day_length)) * vg_signf(k_day_length);
842 world->time += vg.time_frame_delta * (1.0/(rate*60.0));
843 }
844 else{
845 world->time = 0.834;
846 }
847
848 if( world->info.flags & 0x1 ){
849 world->time = world->info.timezone;
850 }
851
852 struct ub_world_lighting *state = &world->ub_lighting;
853
854 state->g_time = world->time;
855 state->g_realtime = vg.time_real;
856 state->g_debug_indices = k_debug_light_indices;
857 state->g_light_preview = k_light_preview;
858 state->g_debug_complexity = k_debug_light_complexity;
859 state->g_time_of_day = vg_fractf( world->time );
860
861 if( vg.quality_profile == k_quality_profile_high )
862 state->g_shadow_samples = 8;
863 else if( vg.quality_profile == k_quality_profile_low )
864 state->g_shadow_samples = 2;
865 else
866 state->g_shadow_samples = 0;
867
868 state->g_day_phase = cosf( state->g_time_of_day * VG_PIf * 2.0f );
869 state->g_sunset_phase= cosf( state->g_time_of_day * VG_PIf * 4.0f + VG_PIf );
870
871 state->g_day_phase = state->g_day_phase * 0.5f + 0.5f;
872 state->g_sunset_phase = powf( state->g_sunset_phase * 0.5f + 0.5f, 6.0f );
873
874 float a = state->g_time_of_day * VG_PIf * 2.0f;
875 state->g_sun_dir[0] = sinf( a );
876 state->g_sun_dir[1] = cosf( a );
877 state->g_sun_dir[2] = 0.2f;
878 v3_normalize( state->g_sun_dir );
879
880 world->probabilities[ k_probability_curve_constant ] = 1.0f;
881 float dp = state->g_day_phase;
882
883 world->probabilities[ k_probability_curve_wildlife_day ] =
884 (dp*dp*0.8f+state->g_sunset_phase)*0.8f;
885 world->probabilities[ k_probability_curve_wildlife_night ] =
886 1.0f-powf(fabsf((state->g_time_of_day-0.5f)*5.0f),5.0f);
887
888 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
889 glBufferSubData( GL_UNIFORM_BUFFER, 0,
890 sizeof(struct ub_world_lighting), &world->ub_lighting );
891 }
892
893 static void render_world( world_instance *world, camera *cam,
894 int stenciled, int viewing_from_gate,
895 int with_water, int with_cubemaps ){
896 if( stenciled ){
897 glClear( GL_DEPTH_BUFFER_BIT );
898 glStencilFunc( GL_EQUAL, 1, 0xFF );
899 glStencilMask( 0x00 );
900 glEnable( GL_CULL_FACE );
901 glEnable( GL_STENCIL_TEST );
902 }
903 else {
904 glStencilMask( 0xFF );
905 glStencilFunc( GL_ALWAYS, 1, 0xFF );
906 glDisable( GL_STENCIL_TEST );
907 }
908
909 render_sky( world, cam );
910
911 m4x3f identity;
912 m4x3_identity(identity);
913 render_world_routes( world, world, identity, cam, viewing_from_gate, 0 );
914 render_world_standard( world, cam );
915 render_world_cubemapped( world, cam, with_cubemaps );
916
917 render_world_vb( world, cam );
918 render_world_alphatest( world, cam );
919 render_world_foliage( world, cam );
920 render_terrain( world, cam );
921
922 if( !viewing_from_gate ){
923 world_entity_focus_render();
924
925 /* Render SFD's */
926 u32 closest = 0;
927 float min_dist = INFINITY;
928
929 if( mdl_arrcount( &world->ent_route ) ){
930 for( u32 i=0; i<mdl_arrcount( &world->ent_route ); i++ ){
931 ent_route *route = mdl_arritm( &world->ent_route, i );
932 float dist = v3_dist2( route->board_transform[3], cam->pos );
933
934 if( dist < min_dist ){
935 min_dist = dist;
936 closest = i;
937 }
938 }
939
940 ent_route *route = mdl_arritm( &world->ent_route, closest );
941 sfd_render( world, cam, route->board_transform );
942 }
943 }
944
945 if( !viewing_from_gate ){
946 f32 greyout = 0.0f;
947 if( mdl_entity_id_type(world_static.focused_entity) == k_ent_challenge )
948 greyout = world_static.focus_strength;
949
950 if( greyout > 0.0f ){
951 glDrawBuffers( 1, (GLenum[]){ GL_COLOR_ATTACHMENT0 } );
952 glEnable(GL_BLEND);
953 glDisable(GL_DEPTH_TEST);
954 glDepthMask(GL_FALSE);
955 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
956 glBlendEquation(GL_FUNC_ADD);
957
958 shader_blitcolour_use();
959 shader_blitcolour_uColour( (v4f){ 0.5f, 0.5f, 0.5f, greyout*0.56f } );
960 render_fsquad();
961
962 glDisable(GL_BLEND);
963 glEnable(GL_DEPTH_TEST);
964 glDepthMask(GL_TRUE);
965 glDrawBuffers( 2, (GLenum[]){ GL_COLOR_ATTACHMENT0,
966 GL_COLOR_ATTACHMENT1 } );
967 }
968
969 render_world_fxglow( world, world, cam, NULL, 1, 1, 0 );
970 }
971
972 if( with_water ){
973 render_water_texture( world, cam );
974 render_fb_bind( gpipeline.fb_main, 1 );
975 }
976
977 if( stenciled ){
978 glStencilFunc( GL_EQUAL, 1, 0xFF );
979 glStencilMask( 0x00 );
980 glEnable( GL_CULL_FACE );
981 glEnable( GL_STENCIL_TEST );
982 }
983
984 if( with_water ){
985 render_water_surface( world, cam );
986 }
987
988 render_remote_players( world, cam );
989 ent_miniworld_render( world, cam );
990
991 if( stenciled ){
992 glStencilMask( 0xFF );
993 glStencilFunc( GL_ALWAYS, 1, 0xFF );
994 glDisable( GL_STENCIL_TEST );
995 }
996 }
997
998
999 static void render_world_override_pass( world_instance *world,
1000 struct world_pass *pass,
1001 m4x3f mmdl, m3x3f mnormal,
1002 m4x4f mpvm_prev ){
1003 for( int i=0; i<world->surface_count; i++ ){
1004 struct world_surface *mat = &world->surfaces[i];
1005 if( mat->info.flags & k_material_flag_ghosts ) continue;
1006
1007 mdl_submesh *sm;
1008 if( pass->geo_type == k_world_geo_type_solid )
1009 sm = &mat->sm_geo;
1010 else
1011 sm = &mat->sm_no_collide;
1012
1013 if( !sm->indice_count )
1014 continue;
1015
1016 pass->fn_set_mdl( mmdl );
1017 pass->fn_set_uNormalMtx( mnormal );
1018 pass->fn_set_uPvmPrev( mpvm_prev );
1019 pass->fn_bind_textures( world, mat );
1020 mdl_draw_submesh( sm );
1021 }
1022 }
1023
1024 static void render_world_override( world_instance *world,
1025 world_instance *lighting_source,
1026 m4x3f mmdl,
1027 camera *cam,
1028 ent_spawn *dest_spawn, v4f map_info ){
1029 struct world_pass pass = {
1030 .cam = cam,
1031 .fn_bind_textures = bindpoint_override,
1032 .fn_set_mdl = shader_scene_override_uMdl,
1033 .fn_set_uPvmPrev = shader_scene_override_uPvmPrev,
1034 .fn_set_uNormalMtx = shader_scene_override_uNormalMtx,
1035 .shader = k_shader_override
1036 };
1037
1038 shader_scene_override_use();
1039 shader_scene_override_uTexGarbage(0);
1040 shader_scene_override_uTexMain(1);
1041 shader_scene_override_uPv( pass.cam->mtx.pv );
1042 shader_scene_override_uMapInfo( map_info );
1043
1044 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( lighting_source, scene_override );
1045 bind_terrain_noise();
1046
1047 shader_scene_override_uCamera( pass.cam->transform[3] );
1048
1049 m4x4f mpvm_prev;
1050 m4x3_expand( mmdl, mpvm_prev );
1051 m4x4_mul( cam->mtx_prev.pv, mpvm_prev, mpvm_prev );
1052
1053 m3x3f mnormal;
1054 m3x3_inv( mmdl, mnormal );
1055 m3x3_transpose( mnormal, mnormal );
1056 v3_normalize( mnormal[0] );
1057 v3_normalize( mnormal[1] );
1058 v3_normalize( mnormal[2] );
1059
1060 v4f uPlayerPos, uSpawnPos;
1061 v4_zero( uPlayerPos );
1062 v4_zero( uSpawnPos );
1063
1064 v3_copy( world->player_co, uPlayerPos );
1065
1066 if( dest_spawn && (v3_dist2(dest_spawn->transform.co,uPlayerPos) > 0.1f) )
1067 v3_copy( dest_spawn->transform.co, uSpawnPos );
1068 else
1069 v3_add( uPlayerPos, (v3f){0,-1,0}, uSpawnPos );
1070
1071 uPlayerPos[3] = v3_dist(uPlayerPos,uSpawnPos);
1072 uSpawnPos[3] = 1.0f/uPlayerPos[3];
1073
1074 shader_scene_override_uPlayerPos( uPlayerPos );
1075 shader_scene_override_uSpawnPos( uSpawnPos );
1076
1077
1078 glDisable( GL_CULL_FACE );
1079 mesh_bind( &world->mesh_geo );
1080 pass.geo_type = k_world_geo_type_solid;
1081 render_world_override_pass( world, &pass, mmdl, mnormal, mpvm_prev );
1082 mesh_bind( &world->mesh_no_collide );
1083 pass.geo_type = k_world_geo_type_nonsolid;
1084 render_world_override_pass( world, &pass, mmdl, mnormal, mpvm_prev );
1085 glEnable( GL_CULL_FACE );
1086
1087 render_world_fxglow( world, world, cam, mmdl, 0, 0, 1 );
1088 }
1089
1090 static void render_cubemap_side( world_instance *world, ent_cubemap *cm,
1091 u32 side ){
1092 camera cam;
1093 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
1094 GL_TEXTURE_CUBE_MAP_POSITIVE_X + side, cm->texture_id, 0 );
1095 glClear( GL_DEPTH_BUFFER_BIT );
1096
1097 v3f forward[6] = {
1098 { -1.0f, 0.0f, 0.0f },
1099 { 1.0f, 0.0f, 0.0f },
1100 { 0.0f, -1.0f, 0.0f },
1101 { 0.0f, 1.0f, 0.0f },
1102 { 0.0f, 0.0f, -1.0f },
1103 { 0.0f, 0.0f, 1.0f }
1104 };
1105 v3f up[6] = {
1106 { 0.0f, -1.0f, 0.0f },
1107 { 0.0f, -1.0f, 0.0f },
1108 { 0.0f, 0.0f, 1.0f },
1109 { 0.0f, 0.0f, -1.0f },
1110 { 0.0f, -1.0f, 0.0f },
1111 { 0.0f, -1.0f, 0.0f }
1112 };
1113
1114 v3_zero( cam.angles );
1115 v3_copy( cm->co, cam.pos );
1116
1117 v3_copy( forward[side], cam.transform[2] );
1118 v3_copy( up[side], cam.transform[1] );
1119 v3_cross( up[side], forward[side], cam.transform[0] );
1120 v3_copy( cm->co, cam.transform[3] );
1121 m4x3_invert_affine( cam.transform, cam.transform_inverse );
1122
1123 camera_update_view( &cam );
1124
1125 cam.nearz = 0.1f;
1126 cam.farz = 1000.0f;
1127 cam.fov = 90.0f;
1128 m4x4_copy( cam.mtx.p, cam.mtx_prev.p );
1129 m4x4_projection( cam.mtx.p, cam.fov, 1.0f, cam.nearz, cam.farz );
1130 camera_finalize( &cam );
1131 camera_finalize( &cam );
1132
1133 render_world( world, &cam, 0, 1, 1, 0 );
1134 }
1135
1136 static void render_world_cubemaps( world_instance *world ){
1137 if( world->cubemap_cooldown )
1138 world->cubemap_cooldown --;
1139 else{
1140 world->cubemap_cooldown = 60;
1141
1142 glViewport( 0, 0, WORLD_CUBEMAP_RES, WORLD_CUBEMAP_RES );
1143 for( u32 i=0; i<mdl_arrcount( &world->ent_cubemap ); i++ ){
1144 ent_cubemap *cm = mdl_arritm( &world->ent_cubemap, i );
1145 glBindFramebuffer( GL_FRAMEBUFFER, cm->framebuffer_id );
1146
1147 world->cubemap_side ++;
1148 if( world->cubemap_side >= 6 )
1149 world->cubemap_side = 0;
1150
1151 render_cubemap_side( world, cm, world->cubemap_side );
1152 }
1153 }
1154 }
1155
1156 /*
1157 * Geo shaders
1158 * ---------------------------------------------
1159 */
1160
1161 static void render_world_depth( world_instance *world, camera *cam ){
1162 m4x3f identity_matrix;
1163 m4x3_identity( identity_matrix );
1164
1165 shader_scene_depth_use();
1166 shader_scene_depth_uCamera( cam->transform[3] );
1167 shader_scene_depth_uPv( cam->mtx.pv );
1168 shader_scene_depth_uPvmPrev( cam->mtx_prev.pv );
1169 shader_scene_depth_uMdl( identity_matrix );
1170 world_link_lighting_ub( world, _shader_scene_depth.id );
1171
1172 mesh_bind( &world->mesh_geo );
1173 mesh_draw( &world->mesh_geo );
1174 }
1175
1176 static void render_world_position( world_instance *world, camera *cam ){
1177 m4x3f identity_matrix;
1178 m4x3_identity( identity_matrix );
1179
1180 shader_scene_position_use();
1181 shader_scene_position_uCamera( cam->transform[3] );
1182 shader_scene_position_uPv( cam->mtx.pv );
1183 shader_scene_position_uPvmPrev( cam->mtx_prev.pv );
1184 shader_scene_position_uMdl( identity_matrix );
1185 world_link_lighting_ub( world, _shader_scene_position.id );
1186
1187 mesh_bind( &world->mesh_geo );
1188 mesh_draw( &world->mesh_geo );
1189 }
1190
1191 #endif