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