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