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