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