allow armature fixups on model load
[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, NULL );
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 void world_render_challenges( world_instance *world,
457 struct world_pass *pass, v3f pos ){
458 if( !world ) return;
459 if( skaterift.activity == k_skaterift_replay ) return;
460 if( world != world_current_instance() ) return;
461
462 /* sort lists */
463 f32 radius = 40.0f;
464
465 u32 objective_list[ 32 ],
466 challenge_list[ 16 ];
467
468 v2f objective_uv_offsets[ 32 ];
469
470 u32 objective_count = 0,
471 challenge_count = 0;
472
473 ent_challenge *active_challenge = NULL;
474 int running = 0;
475 if( mdl_entity_id_type( world_static.focused_entity ) == k_ent_challenge ){
476 if( (skaterift.activity == k_skaterift_default) &&
477 world_static.challenge_target ){
478 running = 1;
479 }
480
481 if( !((skaterift.activity != k_skaterift_ent_focus) &&
482 !world_static.challenge_target) ){
483 world_instance *challenge_world = world_current_instance();
484 u32 index = mdl_entity_id_id( world_static.focused_entity );
485 active_challenge = mdl_arritm(&challenge_world->ent_challenge, index);
486 }
487 }
488
489 if( active_challenge ){
490 shader_scene_fxglow_uUvOffset( (v2f){ 8.0f/256.0f, 0.0f } );
491 challenge_list[ challenge_count ++ ] = world_static.focused_entity;
492
493 u32 next = active_challenge->first;
494 while( mdl_entity_id_type(next) == k_ent_objective ){
495 u32 index = mdl_entity_id_id( next );
496 objective_list[ objective_count ++ ] = index;
497
498 ent_objective *objective = mdl_arritm( &world->ent_objective, index );
499 next = objective->id_next;
500 }
501
502 radius = 10000.0f;
503 }
504 else {
505 shader_scene_fxglow_uUvOffset( (v2f){ 0.0f, 0.0f } );
506 bh_iter it;
507 bh_iter_init_range( 0, &it, pos, radius+10.0f );
508 i32 idx;
509 while( bh_next( world->entity_bh, &it, &idx ) ){
510 u32 id = world->entity_list[ idx ],
511 type = mdl_entity_id_type( id ),
512 index = mdl_entity_id_id( id );
513
514 if( type == k_ent_objective ) {
515 if( objective_count < vg_list_size(objective_list) )
516 objective_list[ objective_count ++ ] = index;
517 }
518 else if( type == k_ent_challenge ){
519 if( challenge_count < vg_list_size(challenge_list) )
520 challenge_list[ challenge_count ++ ] = index;
521 }
522 }
523 }
524
525 /* render objectives */
526 glDisable( GL_CULL_FACE );
527 mesh_bind( &world->mesh_no_collide );
528 u32 last_material = 0;
529 for( u32 i=0; i<objective_count; i++ ){
530 u32 index = objective_list[ i ];
531 ent_objective *objective = mdl_arritm( &world->ent_objective, index );
532 if( (objective->flags & k_ent_objective_hidden) &&
533 !active_challenge ) continue;
534
535 f32 scale = 1.0f;
536
537 if( running ){
538 u32 passed = objective->flags & k_ent_objective_passed;
539 f32 target = passed? 0.0f: 1.0f;
540 vg_slewf(&objective->transform.s[0], target, vg.time_frame_delta*4.0f);
541 scale = vg_smoothstepf( objective->transform.s[0] );
542
543 if( (objective == world_static.challenge_target) || passed )
544 shader_scene_fxglow_uUvOffset( (v2f){ 16.0f/256.0f, 0.0f } );
545 else
546 shader_scene_fxglow_uUvOffset( (v2f){ 8.0f/256.0f, 0.0f } );
547 }
548 else {
549 f32 dist = v3_dist( objective->transform.co, pos ) * (1.0f/radius);
550 scale = vg_smoothstepf( vg_clampf( 5.0f-dist*5.0f, 0.0f,1.0f ) );
551 }
552
553 m4x3f mmdl;
554 q_m3x3( objective->transform.q, mmdl );
555 m3x3_scalef( mmdl, scale );
556 v3_copy( objective->transform.co, mmdl[3] );
557 shader_scene_fxglow_uMdl( mmdl );
558
559 for( u32 j=0; j<objective->submesh_count; j++ ){
560 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs,
561 objective->submesh_start + j );
562
563 if( sm->material_id != last_material ){
564 last_material = sm->material_id;
565 pass->fn_bind_textures( world, &world->surfaces[sm->material_id] );
566 }
567 mdl_draw_submesh( sm );
568 }
569 }
570
571 /* render texts */
572 font3d_bind( &gui.font, k_font_shader_world, 0, world, &skaterift.cam );
573
574 char buf[32];
575 u32 count = 0;
576
577 for( u32 i=0; i<mdl_arrcount(&world->ent_challenge); i++ ){
578 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, i );
579 if( challenge->status ) count ++;
580 }
581
582 int c=0;
583 c+=highscore_intl( buf+c, count, 3 );
584 buf[c++] = '/';
585 c+=highscore_intl( buf+c, mdl_arrcount(&world->ent_challenge), 3 );
586 buf[c++] = '\0';
587
588 f32 w = font3d_string_width( 1, buf );
589 m4x3f mlocal;
590 m3x3_identity( mlocal );
591 mlocal[3][0] = -w*0.5f;
592 mlocal[3][1] = 0.0f;
593 mlocal[3][2] = 0.0f;
594
595 for( u32 i=0; i<challenge_count; i++ ){
596 u32 index = challenge_list[ i ];
597 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, index );
598 m4x3f mmdl;
599 mdl_transform_m4x3( &challenge->transform, mmdl );
600 m4x3_mul( mmdl, mlocal, mmdl );
601
602 vg_line_point( challenge->transform.co, 0.25f, VG__RED );
603
604 f32 dist = v3_dist( challenge->transform.co, pos ) * (1.0f/radius),
605 scale = vg_smoothstepf( vg_clampf( 10.0f-dist*10.0f, 0.0f,1.0f ) ),
606 colour = 0.0f;
607
608 if( challenge->status )
609 colour = 1.0f;
610
611 shader_scene_font_uOpacity( scale );
612 shader_scene_font_uColourize( colour );
613 font3d_simple_draw( 1, buf, &skaterift.cam, mmdl );
614 }
615 }
616
617 static void render_world_fxglow( world_instance *host_world,
618 world_instance *world, camera *cam,
619 m4x3f world_mmdl,
620 int generic, int challenges, int regions ){
621 shader_scene_fxglow_use();
622 shader_scene_fxglow_uUvOffset( (v2f){ 0.0f, 0.0f } );
623 shader_scene_fxglow_uTexMain(1);
624 shader_scene_fxglow_uPv( cam->mtx.pv );
625
626 world_link_lighting_ub( host_world, _shader_scene_fxglow.id );
627 world_bind_position_texture( host_world, _shader_scene_fxglow.id,
628 _uniform_scene_fxglow_g_world_depth, 2 );
629 world_bind_light_array( host_world, _shader_scene_fxglow.id,
630 _uniform_scene_fxglow_uLightsArray, 3 );
631 world_bind_light_index( host_world, _shader_scene_fxglow.id,
632 _uniform_scene_fxglow_uLightsIndex, 4 );
633
634 shader_scene_fxglow_uCamera( cam->transform[3] );
635 glDisable(GL_CULL_FACE);
636
637 struct world_pass pass = {
638 .shader = k_shader_fxglow,
639 .cam = cam,
640 .fn_bind_textures = bindpoint_diffuse_texture1,
641 .fn_set_mdl = shader_scene_fxglow_uMdl,
642 .fn_set_uPvmPrev = shader_scene_fxglow_uPvmPrev,
643 };
644
645 if( generic )
646 world_render_both_stages( world, &pass );
647
648 if( regions ){
649 mesh_bind( &world->mesh_no_collide );
650
651 u32 last_material = 0;
652 for( u32 i=0; i<mdl_arrcount(&world->ent_region); i ++ ){
653 shader_scene_fxglow_uUvOffset( (v2f){ 0.0f, 0.0f } );
654 ent_region *region = mdl_arritm( &world->ent_region, i );
655
656 f32 offset = 0.0f;
657 if( region->flags & k_ent_route_flag_achieve_gold )
658 offset = 2.0f;
659 else if( region->flags & k_ent_route_flag_achieve_silver )
660 offset = 1.0f;
661
662 shader_scene_fxglow_uUvOffset( (v2f){ (8.0f/256.0f)*offset, 0.0f } );
663
664 m4x3f mmdl;
665 mdl_transform_m4x3( &region->transform, mmdl );
666 m4x3_mul( world_mmdl, mmdl, mmdl );
667 shader_scene_fxglow_uMdl( mmdl );
668
669 for( u32 j=0; j<region->submesh_count; j++ ){
670 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs,
671 region->submesh_start + j );
672
673 if( sm->material_id != last_material ){
674 last_material = sm->material_id;
675 pass.fn_bind_textures(world,&world->surfaces[sm->material_id]);
676 }
677 mdl_draw_submesh( sm );
678 }
679 }
680 }
681
682 if( challenges )
683 world_render_challenges( world, &pass, cam->pos );
684
685 glEnable(GL_CULL_FACE);
686 }
687
688 static void bindpoint_terrain( world_instance *world,
689 struct world_surface *mat )
690 {
691 glActiveTexture( GL_TEXTURE1 );
692 glBindTexture( GL_TEXTURE_2D,
693 world_get_texture(world,mat->info.tex_diffuse) );
694
695 shader_scene_terrain_uSandColour( mat->info.colour );
696 shader_scene_terrain_uBlendOffset( mat->info.colour1 );
697 }
698
699 static void bindpoint_override( world_instance *world,
700 struct world_surface *mat ){
701 if( mat->info.flags & k_material_flag_collision ){
702 shader_scene_override_uAlphatest(0);
703 }
704 else{
705 glActiveTexture( GL_TEXTURE1 );
706 glBindTexture( GL_TEXTURE_2D,
707 world_get_texture(world,mat->info.tex_diffuse) );
708 shader_scene_override_uAlphatest(1);
709 }
710 }
711
712 static void render_terrain( world_instance *world, camera *cam ){
713 shader_scene_terrain_use();
714 shader_scene_terrain_uTexGarbage(0);
715 shader_scene_terrain_uTexGradients(1);
716
717 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_terrain );
718 glActiveTexture( GL_TEXTURE0 );
719 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
720
721 shader_scene_terrain_uPv( cam->mtx.pv );
722 shader_scene_terrain_uCamera( cam->transform[3] );
723
724 struct world_pass pass = {
725 .shader = k_shader_terrain_blend,
726 .cam = cam,
727 .fn_bind_textures = bindpoint_terrain,
728 .fn_set_mdl = shader_scene_terrain_uMdl,
729 .fn_set_uPvmPrev = shader_scene_terrain_uPvmPrev,
730 };
731
732 world_render_both_stages( world, &pass );
733 }
734
735 static void render_sky( world_instance *world, camera *cam ){
736 /*
737 * Modify matrix to remove clipping and view translation
738 */
739 m4x4f v,
740 v_prev,
741 pv,
742 pv_prev;
743
744 m4x4_copy( cam->mtx.v, v );
745 m4x4_copy( cam->mtx_prev.v, v_prev );
746
747 for( int i=0; i<3; i++ ){
748 v3_normalize(v[i]);
749 v3_normalize(v_prev[i]);
750 }
751 v3_zero( v[3] );
752 v3_zero( v_prev[3] );
753
754 m4x4_copy( cam->mtx.p, pv );
755 m4x4_copy( cam->mtx_prev.p, pv_prev );
756 m4x4_reset_clipping( pv, 100.0f, 0.1f );
757 m4x4_reset_clipping( pv_prev, 100.0f, 0.1f );
758
759 m4x4_mul( pv, v, pv );
760 m4x4_mul( pv_prev, v_prev, pv_prev );
761
762 m4x3f identity_matrix;
763 m4x3_identity( identity_matrix );
764
765 /*
766 * Draw
767 */
768 if( world->skybox == k_skybox_default ){
769 shader_model_sky_use();
770 shader_model_sky_uMdl( identity_matrix );
771 shader_model_sky_uPv( pv );
772 shader_model_sky_uPvmPrev( pv_prev );
773 shader_model_sky_uTexGarbage(0);
774 world_link_lighting_ub( world, _shader_model_sky.id );
775
776 glActiveTexture( GL_TEXTURE0 );
777 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
778 }
779 else if( world->skybox == k_skybox_space ){
780 shader_model_sky_space_use();
781
782 shader_model_sky_space_uMdl( identity_matrix );
783 shader_model_sky_space_uPv( pv );
784 shader_model_sky_space_uPvmPrev( pv_prev );
785 shader_model_sky_space_uTexGarbage(0);
786 world_link_lighting_ub( world, _shader_model_sky_space.id );
787
788 glActiveTexture( GL_TEXTURE0 );
789 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
790 }
791 else {
792 assert(0);
793 }
794
795 glDepthMask( GL_FALSE );
796 glDisable( GL_DEPTH_TEST );
797
798 mesh_bind( &world_render.skydome );
799 mesh_draw( &world_render.skydome );
800
801 glEnable( GL_DEPTH_TEST );
802 glDepthMask( GL_TRUE );
803 }
804
805 static void render_world_gates( world_instance *world, camera *cam ){
806 float closest = INFINITY;
807 struct ent_gate *gate = NULL;
808
809 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
810 ent_gate *gi = mdl_arritm( &world->ent_gate, i );
811 if( !(gi->flags & k_ent_gate_linked) )
812 continue;
813
814 float dist = v3_dist2( gi->co[0], cam->transform[3] );
815
816 vg_line_point( gi->co[0], 0.25f, VG__BLUE );
817
818 if( dist < closest ){
819 closest = dist;
820 gate = gi;
821 }
822 }
823
824 world->rendering_gate = gate;
825
826 if( gate ){
827 if( gate->flags & k_ent_gate_locked ){
828 world->rendering_gate = NULL;
829 return;
830 }
831
832 if( gate->flags & k_ent_gate_nonlocal ){
833 if( world_static.load_state != k_world_loader_none ){
834 world->rendering_gate = NULL;
835 return;
836 }
837
838 world_instance *dest_world = &world_static.instances[ gate->target ];
839 render_gate( world, dest_world, gate, cam );
840 }
841 else
842 render_gate( world, world, gate, cam );
843 }
844 }
845
846 static void world_prerender( world_instance *world ){
847 if( mdl_arrcount( &world->ent_light ) ){
848 f32 rate = vg_maxf(0.1f, fabsf(k_day_length)) * vg_signf(k_day_length);
849 world->time += vg.time_frame_delta * (1.0/(rate*60.0));
850 }
851 else{
852 world->time = 0.834;
853 }
854
855 struct ub_world_lighting *state = &world->ub_lighting;
856
857 state->g_time = world->time;
858 state->g_realtime = vg.time_real;
859 state->g_debug_indices = k_debug_light_indices;
860 state->g_light_preview = k_light_preview;
861 state->g_debug_complexity = k_debug_light_complexity;
862 state->g_time_of_day = vg_fractf( world->time );
863
864 state->g_day_phase = cosf( state->g_time_of_day * VG_PIf * 2.0f );
865 state->g_sunset_phase= cosf( state->g_time_of_day * VG_PIf * 4.0f + VG_PIf );
866
867 state->g_day_phase = state->g_day_phase * 0.5f + 0.5f;
868 state->g_sunset_phase = powf( state->g_sunset_phase * 0.5f + 0.5f, 6.0f );
869
870 float a = state->g_time_of_day * VG_PIf * 2.0f;
871 state->g_sun_dir[0] = sinf( a );
872 state->g_sun_dir[1] = cosf( a );
873 state->g_sun_dir[2] = 0.2f;
874 v3_normalize( state->g_sun_dir );
875
876 world->probabilities[ k_probability_curve_constant ] = 1.0f;
877 float dp = state->g_day_phase;
878
879 world->probabilities[ k_probability_curve_wildlife_day ] =
880 (dp*dp*0.8f+state->g_sunset_phase)*0.8f;
881 world->probabilities[ k_probability_curve_wildlife_night ] =
882 1.0f-powf(fabsf((state->g_time_of_day-0.5f)*5.0f),5.0f);
883
884 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
885 glBufferSubData( GL_UNIFORM_BUFFER, 0,
886 sizeof(struct ub_world_lighting), &world->ub_lighting );
887 }
888
889 static void render_world( world_instance *world, camera *cam,
890 int stenciled, int viewing_from_gate,
891 int with_water, int with_cubemaps ){
892 if( stenciled ){
893 glClear( GL_DEPTH_BUFFER_BIT );
894 glStencilFunc( GL_EQUAL, 1, 0xFF );
895 glStencilMask( 0x00 );
896 glEnable( GL_CULL_FACE );
897 glEnable( GL_STENCIL_TEST );
898 }
899 else {
900 glStencilMask( 0xFF );
901 glStencilFunc( GL_ALWAYS, 1, 0xFF );
902 glDisable( GL_STENCIL_TEST );
903 }
904
905 render_sky( world, cam );
906
907 m4x3f identity;
908 m4x3_identity(identity);
909 render_world_routes( world, world, identity, cam, viewing_from_gate, 0 );
910 render_world_standard( world, cam );
911 render_world_cubemapped( world, cam, with_cubemaps );
912
913 render_world_vb( world, cam );
914 render_world_alphatest( world, cam );
915 render_terrain( world, cam );
916
917 if( !viewing_from_gate ){
918 world_entity_focus_render();
919
920 /* Render SFD's */
921 u32 closest = 0;
922 float min_dist = INFINITY;
923
924 if( mdl_arrcount( &world->ent_route ) ){
925 for( u32 i=0; i<mdl_arrcount( &world->ent_route ); i++ ){
926 ent_route *route = mdl_arritm( &world->ent_route, i );
927 float dist = v3_dist2( route->board_transform[3], cam->pos );
928
929 if( dist < min_dist ){
930 min_dist = dist;
931 closest = i;
932 }
933 }
934
935 ent_route *route = mdl_arritm( &world->ent_route, closest );
936 sfd_render( world, cam, route->board_transform );
937 }
938 }
939
940 if( !viewing_from_gate ){
941 f32 greyout = 0.0f;
942 if( mdl_entity_id_type(world_static.focused_entity) == k_ent_challenge )
943 greyout = world_static.focus_strength;
944
945 if( greyout > 0.0f ){
946 glDrawBuffers( 1, (GLenum[]){ GL_COLOR_ATTACHMENT0 } );
947 glEnable(GL_BLEND);
948 glDisable(GL_DEPTH_TEST);
949 glDepthMask(GL_FALSE);
950 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
951 glBlendEquation(GL_FUNC_ADD);
952
953 shader_blitcolour_use();
954 shader_blitcolour_uColour( (v4f){ 0.5f, 0.5f, 0.5f, greyout*0.56f } );
955 render_fsquad();
956
957 glDisable(GL_BLEND);
958 glEnable(GL_DEPTH_TEST);
959 glDepthMask(GL_TRUE);
960 glDrawBuffers( 2, (GLenum[]){ GL_COLOR_ATTACHMENT0,
961 GL_COLOR_ATTACHMENT1 } );
962 }
963
964 render_world_fxglow( world, world, cam, NULL, 1, 1, 0 );
965 }
966
967 if( with_water ){
968 render_water_texture( world, cam );
969 render_fb_bind( gpipeline.fb_main, 1 );
970 }
971
972 if( stenciled ){
973 glStencilFunc( GL_EQUAL, 1, 0xFF );
974 glStencilMask( 0x00 );
975 glEnable( GL_CULL_FACE );
976 glEnable( GL_STENCIL_TEST );
977 }
978
979 if( with_water ){
980 render_water_surface( world, cam );
981 }
982
983 render_remote_players( world, cam );
984 ent_miniworld_render( world, cam );
985
986 if( stenciled ){
987 glStencilMask( 0xFF );
988 glStencilFunc( GL_ALWAYS, 1, 0xFF );
989 glDisable( GL_STENCIL_TEST );
990 }
991 }
992
993
994 static void render_world_override_pass( world_instance *world,
995 struct world_pass *pass,
996 m4x3f mmdl, m3x3f mnormal,
997 m4x4f mpvm_prev ){
998 for( int i=0; i<world->surface_count; i++ ){
999 struct world_surface *mat = &world->surfaces[i];
1000
1001 if( mat->info.flags & k_material_flag_ghosts ) continue;
1002
1003 mdl_submesh *sm;
1004 if( pass->geo_type == k_world_geo_type_solid )
1005 sm = &mat->sm_geo;
1006 else
1007 sm = &mat->sm_no_collide;
1008
1009 if( !sm->indice_count )
1010 continue;
1011
1012 pass->fn_set_mdl( mmdl );
1013 pass->fn_set_uNormalMtx( mnormal );
1014 pass->fn_set_uPvmPrev( mpvm_prev );
1015 pass->fn_bind_textures( world, mat );
1016 mdl_draw_submesh( sm );
1017 }
1018 }
1019
1020 static void render_world_override( world_instance *world,
1021 world_instance *lighting_source,
1022 m4x3f mmdl,
1023 camera *cam,
1024 ent_spawn *dest_spawn, v4f map_info ){
1025 struct world_pass pass = {
1026 .cam = cam,
1027 .fn_bind_textures = bindpoint_override,
1028 .fn_set_mdl = shader_scene_override_uMdl,
1029 .fn_set_uPvmPrev = shader_scene_override_uPvmPrev,
1030 .fn_set_uNormalMtx = shader_scene_override_uNormalMtx,
1031 .shader = k_shader_override
1032 };
1033
1034 shader_scene_override_use();
1035 shader_scene_override_uTexGarbage(0);
1036 shader_scene_override_uTexMain(1);
1037 shader_scene_override_uPv( pass.cam->mtx.pv );
1038 shader_scene_override_uMapInfo( map_info );
1039
1040 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( lighting_source, scene_override );
1041 bind_terrain_noise();
1042
1043 shader_scene_override_uCamera( pass.cam->transform[3] );
1044
1045 m4x4f mpvm_prev;
1046 m4x3_expand( mmdl, mpvm_prev );
1047 m4x4_mul( cam->mtx_prev.pv, mpvm_prev, mpvm_prev );
1048
1049 m3x3f mnormal;
1050 m3x3_inv( mmdl, mnormal );
1051 m3x3_transpose( mnormal, mnormal );
1052 v3_normalize( mnormal[0] );
1053 v3_normalize( mnormal[1] );
1054 v3_normalize( mnormal[2] );
1055
1056 v4f uPlayerPos, uSpawnPos;
1057 v4_zero( uPlayerPos );
1058 v4_zero( uSpawnPos );
1059
1060 v3_copy( world->player_co, uPlayerPos );
1061
1062 if( dest_spawn && (v3_dist2(dest_spawn->transform.co,uPlayerPos) > 0.1f) )
1063 v3_copy( dest_spawn->transform.co, uSpawnPos );
1064 else
1065 v3_add( uPlayerPos, (v3f){0,-1,0}, uSpawnPos );
1066
1067 uPlayerPos[3] = v3_dist(uPlayerPos,uSpawnPos);
1068 uSpawnPos[3] = 1.0f/uPlayerPos[3];
1069
1070 shader_scene_override_uPlayerPos( uPlayerPos );
1071 shader_scene_override_uSpawnPos( uSpawnPos );
1072
1073
1074 glDisable( GL_CULL_FACE );
1075 mesh_bind( &world->mesh_geo );
1076 pass.geo_type = k_world_geo_type_solid;
1077 render_world_override_pass( world, &pass, mmdl, mnormal, mpvm_prev );
1078 mesh_bind( &world->mesh_no_collide );
1079 pass.geo_type = k_world_geo_type_nonsolid;
1080 render_world_override_pass( world, &pass, mmdl, mnormal, mpvm_prev );
1081 glEnable( GL_CULL_FACE );
1082
1083 render_world_fxglow( world, world, cam, mmdl, 0, 0, 1 );
1084 }
1085
1086 static void render_cubemap_side( world_instance *world, ent_cubemap *cm,
1087 u32 side ){
1088 camera cam;
1089 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
1090 GL_TEXTURE_CUBE_MAP_POSITIVE_X + side, cm->texture_id, 0 );
1091 glClear( GL_DEPTH_BUFFER_BIT );
1092
1093 v3f forward[6] = {
1094 { -1.0f, 0.0f, 0.0f },
1095 { 1.0f, 0.0f, 0.0f },
1096 { 0.0f, -1.0f, 0.0f },
1097 { 0.0f, 1.0f, 0.0f },
1098 { 0.0f, 0.0f, -1.0f },
1099 { 0.0f, 0.0f, 1.0f }
1100 };
1101 v3f up[6] = {
1102 { 0.0f, -1.0f, 0.0f },
1103 { 0.0f, -1.0f, 0.0f },
1104 { 0.0f, 0.0f, 1.0f },
1105 { 0.0f, 0.0f, -1.0f },
1106 { 0.0f, -1.0f, 0.0f },
1107 { 0.0f, -1.0f, 0.0f }
1108 };
1109
1110 v3_zero( cam.angles );
1111 v3_copy( cm->co, cam.pos );
1112
1113 v3_copy( forward[side], cam.transform[2] );
1114 v3_copy( up[side], cam.transform[1] );
1115 v3_cross( up[side], forward[side], cam.transform[0] );
1116 v3_copy( cm->co, cam.transform[3] );
1117 m4x3_invert_affine( cam.transform, cam.transform_inverse );
1118
1119 camera_update_view( &cam );
1120
1121 cam.nearz = 0.1f;
1122 cam.farz = 1000.0f;
1123 cam.fov = 90.0f;
1124 m4x4_copy( cam.mtx.p, cam.mtx_prev.p );
1125 m4x4_projection( cam.mtx.p, cam.fov, 1.0f, cam.nearz, cam.farz );
1126 camera_finalize( &cam );
1127 camera_finalize( &cam );
1128
1129 render_world( world, &cam, 0, 1, 1, 0 );
1130 }
1131
1132 static void render_world_cubemaps( world_instance *world ){
1133 if( world->cubemap_cooldown )
1134 world->cubemap_cooldown --;
1135 else{
1136 world->cubemap_cooldown = 60;
1137
1138 glViewport( 0, 0, WORLD_CUBEMAP_RES, WORLD_CUBEMAP_RES );
1139 for( u32 i=0; i<mdl_arrcount( &world->ent_cubemap ); i++ ){
1140 ent_cubemap *cm = mdl_arritm( &world->ent_cubemap, i );
1141 glBindFramebuffer( GL_FRAMEBUFFER, cm->framebuffer_id );
1142
1143 world->cubemap_side ++;
1144 if( world->cubemap_side >= 6 )
1145 world->cubemap_side = 0;
1146
1147 render_cubemap_side( world, cm, world->cubemap_side );
1148 }
1149 }
1150 }
1151
1152 static void render_world_depth( world_instance *world, camera *cam ){
1153 m4x3f identity_matrix;
1154 m4x3_identity( identity_matrix );
1155
1156 shader_scene_depth_use();
1157 shader_scene_depth_uCamera( cam->transform[3] );
1158 shader_scene_depth_uPv( cam->mtx.pv );
1159 shader_scene_depth_uPvmPrev( cam->mtx_prev.pv );
1160 shader_scene_depth_uMdl( identity_matrix );
1161 world_link_lighting_ub( world, _shader_scene_depth.id );
1162
1163 mesh_bind( &world->mesh_geo );
1164 mesh_draw( &world->mesh_geo );
1165 }
1166
1167 static void render_world_position( world_instance *world, camera *cam ){
1168 m4x3f identity_matrix;
1169 m4x3_identity( identity_matrix );
1170
1171 shader_scene_position_use();
1172 shader_scene_position_uCamera( cam->transform[3] );
1173 shader_scene_position_uPv( cam->mtx.pv );
1174 shader_scene_position_uPvmPrev( cam->mtx_prev.pv );
1175 shader_scene_position_uMdl( identity_matrix );
1176 world_link_lighting_ub( world, _shader_scene_position.id );
1177
1178 mesh_bind( &world->mesh_geo );
1179 mesh_draw( &world->mesh_geo );
1180 }
1181
1182 #endif