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