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