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