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