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