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