map binary data
[carveJwlIkooP6JGAAIwe30JlM.git] / world_render.c
1 /*
2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #include "world.h"
6 #include "world_render.h"
7 #include "font.h"
8 #include "gui.h"
9 #include "world_map.h"
10 #include "ent_miniworld.h"
11 #include "player_remote.h"
12 #include "ent_skateshop.h"
13 #include "ent_npc.h"
14 #include "shaders/model_entity.h"
15
16 struct world_render world_render;
17
18 static int ccmd_set_time( int argc, const char *argv[] ){
19 world_instance *world = world_current_instance();
20 if( argc == 1 )
21 world->time = atof( argv[0] );
22 else
23 vg_error( "Usage set_time <0-1.0> (current time: %f)\n", world->time );
24 return 0;
25 }
26
27 static void async_world_render_init( void *payload, u32 size ){
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 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_VAR_I32( k_light_editor );
76 vg_console_reg_cmd( "set_time", ccmd_set_time, NULL );
77
78 world_render.sky_rate = 1.0;
79 world_render.sky_target_rate = 1.0;
80
81 vg_info( "Loading world resources\n" );
82 vg_linear_clear( vg_mem.scratch );
83
84 mdl_context msky;
85 mdl_open( &msky, "models/rs_skydome.mdl", vg_mem.scratch );
86 mdl_load_metadata_block( &msky, vg_mem.scratch );
87 mdl_async_load_glmesh( &msky, &world_render.skydome, NULL );
88 mdl_close( &msky );
89
90 vg_info( "Loading default world textures\n" );
91 vg_tex2d_load_qoi_async_file( "textures/garbage.qoi",
92 VG_TEX2D_NEAREST|VG_TEX2D_REPEAT,
93 &world_render.tex_terrain_noise );
94
95 vg_async_call( async_world_render_init, NULL, 0 );
96 }
97
98 /*
99 * standard uniform bindings
100 * ----------------------------------------------------------------------------
101 */
102 void world_link_lighting_ub( world_instance *world, GLuint shader )
103 {
104 GLuint idx = glGetUniformBlockIndex( shader, "ub_world_lighting" );
105 glUniformBlockBinding( shader, idx, world->ubo_bind_point );
106 }
107
108 void world_bind_position_texture( world_instance *world,
109 GLuint shader, GLuint location,
110 int slot )
111 {
112 render_fb_bind_texture( &world->heightmap, 0, slot );
113 glUniform1i( location, slot );
114 }
115
116 void world_bind_light_array( world_instance *world,
117 GLuint shader, GLuint location,
118 int slot )
119 {
120 glActiveTexture( GL_TEXTURE0 + slot );
121 glBindTexture( GL_TEXTURE_BUFFER, world->tex_light_entities );
122 glUniform1i( location, slot );
123 }
124
125 void world_bind_light_index( world_instance *world,
126 GLuint shader, GLuint location,
127 int slot )
128 {
129 glActiveTexture( GL_TEXTURE0 + slot );
130 glBindTexture( GL_TEXTURE_3D, world->tex_light_cubes );
131 glUniform1i( location, slot );
132 }
133
134 void bind_terrain_noise(void)
135 {
136 glActiveTexture( GL_TEXTURE0 );
137 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
138 }
139
140 /*
141 * Get OpenGL texture name from texture ID.
142 */
143 static GLuint world_get_texture( world_instance *world, u32 id ){
144 if( id & 0x80000000 ) return skaterift.rt_textures[id & ~0x80000000];
145 else return world->textures[ id ];
146 }
147
148 /*
149 * Passes Rendering
150 * ----------------------------------------------------------------------------
151 */
152
153 struct world_pass
154 {
155 vg_camera *cam;
156 enum mdl_shader shader;
157 enum world_geo_type geo_type;
158
159 void (*fn_bind)( world_instance *world, struct world_surface *mat );
160 void (*fn_set_mdl)( m4x3f mdl );
161 void (*fn_set_uPvmPrev)( m4x4f pvm );
162 void (*fn_set_uNormalMtx)( m3x3f mnorm );
163 };
164
165 void render_world_depth( world_instance *world, vg_camera *cam );
166
167 /*
168 * Render a run of submeshes, only of those which match material_id
169 */
170 static void world_render_submeshes( world_instance *world,
171 struct world_pass *pass,
172 mdl_transform *transform,
173 u32 start, u32 count, u32 material_id )
174 {
175 for( u32 k=0; k<count; k++ )
176 {
177 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs, start+k );
178 if( sm->material_id != material_id )
179 continue;
180
181 m4x3f mmdl;
182 mdl_transform_m4x3( 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 * Render props attached to this material
197 */
198 static void world_render_props( world_instance *world, u32 material_id,
199 struct world_pass *pass )
200 {
201 struct world_surface *mat = &world->surfaces[ material_id ];
202 if( !(mat->flags & WORLD_SURFACE_HAS_PROPS) ) return;
203
204 pass->fn_bind( world, mat );
205
206 for( u32 j=0; j<mdl_arrcount( &world->ent_prop ); j++ ){
207 ent_prop *prop = mdl_arritm( &world->ent_prop, j );
208 if( prop->flags & 0x1 ) continue;
209
210 world_render_submeshes( world, pass, &prop->transform,
211 prop->submesh_start, prop->submesh_count, material_id );
212 }
213 }
214
215 /*
216 * Render traffic models attactched to this material
217 */
218 static void world_render_traffic( world_instance *world, u32 material_id,
219 struct world_pass *pass )
220 {
221 struct world_surface *mat = &world->surfaces[ material_id ];
222 if( !(mat->flags & WORLD_SURFACE_HAS_TRAFFIC) ) return;
223
224 pass->fn_bind( world, mat );
225
226 for( u32 j=0; j<mdl_arrcount( &world->ent_traffic ); j++ ){
227 ent_traffic *traffic = mdl_arritm( &world->ent_traffic, j );
228
229 world_render_submeshes( world, pass, &traffic->transform,
230 traffic->submesh_start, traffic->submesh_count,
231 material_id );
232 }
233 }
234
235 /*
236 * Iterate and render all materials which match the passes shader and geometry
237 * type. Includes props/traffic.
238 */
239 static void world_render_pass( world_instance *world, struct world_pass *pass )
240 {
241 for( int i=0; i<world->surface_count; i++ )
242 {
243 struct world_surface *mat = &world->surfaces[i];
244
245 if( mat->info.shader == pass->shader )
246 {
247 mdl_submesh *sm;
248
249 if( pass->geo_type == k_world_geo_type_solid )
250 {
251 sm = &mat->sm_geo;
252 }
253 else
254 {
255 world_render_traffic( world, i, pass );
256 world_render_props( world, i, pass );
257 sm = &mat->sm_no_collide;
258 }
259
260 if( !sm->indice_count )
261 continue;
262
263 m4x3f mmdl;
264 m4x3_identity( mmdl );
265 pass->fn_set_mdl( mmdl );
266 pass->fn_set_uPvmPrev( pass->cam->mtx_prev.pv );
267 pass->fn_bind( world, mat );
268 mdl_draw_submesh( sm );
269 }
270 }
271 }
272
273 /*
274 * Specific shader instructions
275 * ----------------------------------------------------------------------------
276 */
277
278 static void world_render_both_stages( world_instance *world,
279 struct world_pass *pass )
280 {
281 mesh_bind( &world->mesh_geo );
282 pass->geo_type = k_world_geo_type_solid;
283 world_render_pass( world, pass );
284
285 glDisable( GL_CULL_FACE );
286 mesh_bind( &world->mesh_no_collide );
287 pass->geo_type = k_world_geo_type_nonsolid;
288 world_render_pass( world, pass );
289 glEnable( GL_CULL_FACE );
290 }
291
292 static void bindpoint_world_vb( world_instance *world,
293 struct world_surface *mat )
294 {
295 struct shader_props_vertex_blend *props = mat->info.props.compiled;
296
297 glActiveTexture( GL_TEXTURE1 );
298 glBindTexture( GL_TEXTURE_2D, world_get_texture(world, props->tex_diffuse) );
299
300 #if 0
301 shader_scene_vertex_blend_uOffset( props->blend_offset );
302 #endif
303 }
304
305 static void render_world_vb( world_instance *world, vg_camera *cam )
306 {
307 shader_scene_vertex_blend_use();
308 shader_scene_vertex_blend_uTexGarbage(0);
309 shader_scene_vertex_blend_uTexGradients(1);
310 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_vertex_blend );
311
312 glActiveTexture( GL_TEXTURE0 );
313 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
314
315 shader_scene_vertex_blend_uPv( cam->mtx.pv );
316 shader_scene_vertex_blend_uCamera( cam->transform[3] );
317
318 struct world_pass pass =
319 {
320 .shader = k_shader_standard_vertex_blend,
321 .cam = cam,
322 .fn_bind = bindpoint_world_vb,
323 .fn_set_mdl = shader_scene_vertex_blend_uMdl,
324 .fn_set_uPvmPrev = shader_scene_vertex_blend_uPvmPrev,
325 };
326
327 world_render_both_stages( world, &pass );
328 }
329
330 static void world_shader_standard_bind( world_instance *world, vg_camera *cam )
331 {
332 shader_scene_standard_use();
333 shader_scene_standard_uTexGarbage(0);
334 shader_scene_standard_uTexMain(1);
335 shader_scene_standard_uPv( cam->mtx.pv );
336 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_standard );
337
338 bind_terrain_noise();
339 shader_scene_standard_uCamera( cam->transform[3] );
340 }
341
342 static void bindpoint_standard( world_instance *world,
343 struct world_surface *mat )
344 {
345 struct shader_props_standard *props = mat->info.props.compiled;
346
347 glActiveTexture( GL_TEXTURE1 );
348 glBindTexture( GL_TEXTURE_2D, world_get_texture(world, props->tex_diffuse) );
349 }
350
351 static void render_world_standard( world_instance *world, vg_camera *cam )
352 {
353 world_shader_standard_bind( world, cam );
354 struct world_pass pass =
355 {
356 .shader = k_shader_standard,
357 .cam = cam,
358 .fn_bind = bindpoint_standard,
359 .fn_set_mdl = shader_scene_standard_uMdl,
360 .fn_set_uPvmPrev = shader_scene_standard_uPvmPrev,
361 };
362
363 world_render_both_stages( world, &pass );
364 }
365
366 static void bindpoint_world_cubemapped( world_instance *world,
367 struct world_surface *mat )
368 {
369 struct shader_props_cubemapped *props = mat->info.props.compiled;
370
371 glActiveTexture( GL_TEXTURE1 );
372 glBindTexture( GL_TEXTURE_2D,
373 world_get_texture( world,props->tex_diffuse ) );
374
375 u32 cubemap_id = props->cubemap_entity,
376 cubemap_index = 0;
377
378 if( mdl_entity_id_type( cubemap_id ) == k_ent_cubemap )
379 {
380 cubemap_index = mdl_entity_id_id( cubemap_id );
381 }
382
383 ent_cubemap *cm = mdl_arritm( &world->ent_cubemap, cubemap_index );
384 glActiveTexture( GL_TEXTURE10 );
385 glBindTexture( GL_TEXTURE_CUBE_MAP, cm->texture_id );
386
387 shader_scene_cubemapped_uColour( props->tint );
388 }
389
390 static void bindpoint_world_cubemapped_disabled( world_instance *world,
391 struct world_surface *mat )
392 {
393 struct shader_props_cubemapped *props = mat->info.props.compiled;
394
395 glActiveTexture( GL_TEXTURE1 );
396 glBindTexture( GL_TEXTURE_2D,
397 world_get_texture( world, props->tex_diffuse ) );
398 }
399
400 static void render_world_cubemapped( world_instance *world, vg_camera *cam,
401 int enabled )
402 {
403 if( !mdl_arrcount( &world->ent_cubemap ) )
404 return;
405
406 if( !enabled )
407 {
408 world_shader_standard_bind( world, cam );
409
410 struct world_pass pass =
411 {
412 .shader = k_shader_cubemap,
413 .cam = cam,
414 .fn_bind = bindpoint_world_cubemapped_disabled,
415 .fn_set_mdl = shader_scene_standard_uMdl,
416 .fn_set_uPvmPrev = shader_scene_standard_uPvmPrev,
417 };
418
419 world_render_both_stages( world, &pass );
420 }
421 else
422 {
423 shader_scene_cubemapped_use();
424 shader_scene_cubemapped_uTexGarbage(0);
425 shader_scene_cubemapped_uTexMain(1);
426 shader_scene_cubemapped_uTexCubemap(10);
427 shader_scene_cubemapped_uPv( cam->mtx.pv );
428
429 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_cubemapped );
430
431 bind_terrain_noise();
432 shader_scene_cubemapped_uCamera( cam->transform[3] );
433
434 struct world_pass pass =
435 {
436 .shader = k_shader_cubemap,
437 .cam = cam,
438 .fn_bind = bindpoint_world_cubemapped,
439 .fn_set_mdl = shader_scene_cubemapped_uMdl,
440 .fn_set_uPvmPrev = shader_scene_cubemapped_uPvmPrev,
441 };
442
443 world_render_both_stages( world, &pass );
444 }
445 }
446
447 static void render_world_alphatest( world_instance *world, vg_camera *cam )
448 {
449 shader_scene_standard_alphatest_use();
450 shader_scene_standard_alphatest_uTexGarbage(0);
451 shader_scene_standard_alphatest_uTexMain(1);
452 shader_scene_standard_alphatest_uPv( cam->mtx.pv );
453
454 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_standard_alphatest );
455
456 bind_terrain_noise();
457 shader_scene_standard_alphatest_uCamera( cam->transform[3] );
458 glDisable(GL_CULL_FACE);
459
460 struct world_pass pass =
461 {
462 .shader = k_shader_standard_cutout,
463 .cam = cam,
464 .fn_bind = bindpoint_standard,
465 .fn_set_mdl = shader_scene_standard_alphatest_uMdl,
466 .fn_set_uPvmPrev = shader_scene_standard_alphatest_uPvmPrev,
467 };
468
469 world_render_both_stages( world, &pass );
470 glEnable(GL_CULL_FACE);
471 }
472
473 static void render_world_foliage( world_instance *world, vg_camera *cam )
474 {
475 shader_scene_foliage_use();
476 shader_scene_foliage_uTexGarbage(0);
477 shader_scene_foliage_uTexMain(1);
478 shader_scene_foliage_uPv( cam->mtx.pv );
479 shader_scene_foliage_uTime( vg.time );
480
481 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_foliage );
482 bind_terrain_noise();
483
484 shader_scene_foliage_uCamera( cam->transform[3] );
485 glDisable(GL_CULL_FACE);
486 struct world_pass pass =
487 {
488 .shader = k_shader_foliage,
489 .cam = cam,
490 .fn_bind = bindpoint_standard,
491 .fn_set_mdl = shader_scene_foliage_uMdl,
492 .fn_set_uPvmPrev = shader_scene_foliage_uPvmPrev,
493 };
494 world_render_both_stages( world, &pass );
495 glEnable(GL_CULL_FACE);
496 }
497
498 static void world_render_challenges( world_instance *world,
499 struct world_pass *pass, v3f pos )
500 {
501 if( !world ) return;
502 if( skaterift.activity == k_skaterift_replay ) return;
503 if( world != world_current_instance() ) return;
504
505 /* sort lists */
506 f32 radius = 40.0f;
507
508 u32 objective_list[ 32 ],
509 challenge_list[ 16 ];
510
511 v2f objective_uv_offsets[ 32 ];
512
513 u32 objective_count = 0,
514 challenge_count = 0;
515
516 ent_challenge *active_challenge = NULL;
517 int running = 0;
518 if( mdl_entity_id_type( world_static.focused_entity ) == k_ent_challenge ){
519 if( (skaterift.activity == k_skaterift_default) &&
520 world_static.challenge_target ){
521 running = 1;
522 }
523
524 if( !((skaterift.activity != k_skaterift_ent_focus) &&
525 !world_static.challenge_target) ){
526 world_instance *challenge_world = world_current_instance();
527 u32 index = mdl_entity_id_id( world_static.focused_entity );
528 active_challenge = mdl_arritm(&challenge_world->ent_challenge, index);
529 }
530 }
531
532 if( active_challenge ){
533 shader_scene_fxglow_uUvOffset( (v2f){ 8.0f/256.0f, 0.0f } );
534 challenge_list[ challenge_count ++ ] = world_static.focused_entity;
535
536 u32 next = active_challenge->first;
537 while( mdl_entity_id_type(next) == k_ent_objective ){
538 u32 index = mdl_entity_id_id( next );
539 objective_list[ objective_count ++ ] = index;
540
541 ent_objective *objective = mdl_arritm( &world->ent_objective, index );
542 next = objective->id_next;
543 }
544
545 radius = 10000.0f;
546 }
547 else {
548 shader_scene_fxglow_uUvOffset( (v2f){ 0.0f, 0.0f } );
549 bh_iter it;
550 bh_iter_init_range( 0, &it, pos, radius+10.0f );
551 i32 idx;
552 while( bh_next( world->entity_bh, &it, &idx ) ){
553 u32 id = world->entity_list[ idx ],
554 type = mdl_entity_id_type( id ),
555 index = mdl_entity_id_id( id );
556
557 if( type == k_ent_objective ) {
558 if( objective_count < vg_list_size(objective_list) )
559 objective_list[ objective_count ++ ] = index;
560 }
561 else if( type == k_ent_challenge ){
562 if( challenge_count < vg_list_size(challenge_list) )
563 challenge_list[ challenge_count ++ ] = index;
564 }
565 }
566 }
567
568 /* render objectives */
569 glDisable( GL_CULL_FACE );
570 mesh_bind( &world->mesh_no_collide );
571 u32 last_material = 0;
572 for( u32 i=0; i<objective_count; i++ )
573 {
574 u32 index = objective_list[ i ];
575 ent_objective *objective = mdl_arritm( &world->ent_objective, index );
576 if( (objective->flags & k_ent_objective_hidden) &&
577 !active_challenge ) continue;
578
579 f32 scale = 1.0f;
580
581 if( running )
582 {
583 u32 passed = objective->flags & k_ent_objective_passed;
584 f32 target = passed? 0.0f: 1.0f;
585 vg_slewf(&objective->transform.s[0], target, vg.time_frame_delta*4.0f);
586 scale = vg_smoothstepf( objective->transform.s[0] );
587
588 if( (objective == world_static.challenge_target) || passed )
589 shader_scene_fxglow_uUvOffset( (v2f){ 16.0f/256.0f, 0.0f } );
590 else
591 shader_scene_fxglow_uUvOffset( (v2f){ 8.0f/256.0f, 0.0f } );
592 }
593 else
594 {
595 f32 dist = v3_dist( objective->transform.co, pos ) * (1.0f/radius);
596 scale = vg_smoothstepf( vg_clampf( 5.0f-dist*5.0f, 0.0f,1.0f ) );
597 }
598
599 m4x3f mmdl;
600 q_m3x3( objective->transform.q, mmdl );
601 m3x3_scalef( mmdl, scale );
602 v3_copy( objective->transform.co, mmdl[3] );
603 shader_scene_fxglow_uMdl( mmdl );
604
605 for( u32 j=0; j<objective->submesh_count; j++ )
606 {
607 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs,
608 objective->submesh_start + j );
609
610 if( sm->material_id != last_material )
611 {
612 last_material = sm->material_id;
613 pass->fn_bind( world, &world->surfaces[sm->material_id] );
614 }
615 mdl_draw_submesh( sm );
616 }
617 }
618
619 /* render texts */
620 font3d_bind( &gui.font, k_font_shader_world, 0, world, &skaterift.cam );
621
622 u32 count = 0;
623
624 for( u32 i=0; i<mdl_arrcount(&world->ent_challenge); i++ )
625 {
626 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, i );
627 if( challenge->status ) count ++;
628 }
629
630 char buf[32];
631 vg_str str;
632 vg_strnull( &str, buf, sizeof(buf) );
633 vg_strcati32( &str, count );
634 vg_strcatch( &str, '/' );
635 vg_strcati32( &str, mdl_arrcount(&world->ent_challenge) );
636
637 f32 w = font3d_string_width( 1, buf );
638 m4x3f mlocal;
639 m3x3_identity( mlocal );
640 mlocal[3][0] = -w*0.5f;
641 mlocal[3][1] = 0.0f;
642 mlocal[3][2] = 0.0f;
643
644 for( u32 i=0; i<challenge_count; i++ )
645 {
646 u32 index = challenge_list[ i ];
647 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, index );
648 m4x3f mmdl;
649 mdl_transform_m4x3( &challenge->transform, mmdl );
650 m4x3_mul( mmdl, mlocal, mmdl );
651
652 vg_line_point( challenge->transform.co, 0.25f, VG__RED );
653
654 f32 dist = v3_dist( challenge->transform.co, pos ) * (1.0f/radius),
655 scale = vg_smoothstepf( vg_clampf( 10.0f-dist*10.0f, 0.0f,1.0f ) ),
656 colour = 0.0f;
657
658 if( challenge->status )
659 colour = 1.0f;
660
661 shader_scene_font_uOpacity( scale );
662 shader_scene_font_uColourize( colour );
663 font3d_simple_draw( 1, buf, &skaterift.cam, mmdl );
664 }
665 }
666
667 static void bindpoint_fxglow( world_instance *world,
668 struct world_surface *mat )
669 {
670 struct shader_props_standard *props = mat->info.props.compiled;
671
672 glActiveTexture( GL_TEXTURE1 );
673 glBindTexture( GL_TEXTURE_2D, world_get_texture(world, props->tex_diffuse) );
674 }
675
676 static void render_world_fxglow( world_instance *host_world,
677 world_instance *world, vg_camera *cam,
678 m4x3f world_mmdl,
679 int generic, int challenges, int regions )
680 {
681 shader_scene_fxglow_use();
682 shader_scene_fxglow_uUvOffset( (v2f){ 0.0f, 0.0f } );
683 shader_scene_fxglow_uTexMain(1);
684 shader_scene_fxglow_uPv( cam->mtx.pv );
685 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_fxglow );
686
687 shader_scene_fxglow_uCamera( cam->transform[3] );
688 glDisable(GL_CULL_FACE);
689
690 struct world_pass pass =
691 {
692 .shader = k_shader_fxglow,
693 .cam = cam,
694 .fn_bind = bindpoint_fxglow,
695 .fn_set_mdl = shader_scene_fxglow_uMdl,
696 .fn_set_uPvmPrev = shader_scene_fxglow_uPvmPrev,
697 };
698
699 if( generic )
700 world_render_both_stages( world, &pass );
701
702 if( regions ){
703 mesh_bind( &world->mesh_no_collide );
704
705 u32 last_material = 0;
706 for( u32 i=0; i<mdl_arrcount(&world->ent_region); i ++ ){
707 shader_scene_fxglow_uUvOffset( (v2f){ 0.0f, 0.0f } );
708 ent_region *region = mdl_arritm( &world->ent_region, i );
709
710 f32 offset = 0.0f;
711 if( region->flags & k_ent_route_flag_achieve_gold )
712 offset = 2.0f;
713 else if( region->flags & k_ent_route_flag_achieve_silver )
714 offset = 1.0f;
715
716 shader_scene_fxglow_uUvOffset( (v2f){ (8.0f/256.0f)*offset, 0.0f } );
717
718 m4x3f mmdl;
719 mdl_transform_m4x3( &region->transform, mmdl );
720 m4x3_mul( world_mmdl, mmdl, mmdl );
721 shader_scene_fxglow_uMdl( mmdl );
722
723 for( u32 j=0; j<region->submesh_count; j++ )
724 {
725 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs,
726 region->submesh_start + j );
727
728 if( sm->material_id != last_material )
729 {
730 last_material = sm->material_id;
731 pass.fn_bind( world, &world->surfaces[sm->material_id] );
732 }
733 mdl_draw_submesh( sm );
734 }
735 }
736 }
737
738 if( challenges )
739 world_render_challenges( world, &pass, cam->pos );
740
741 glEnable(GL_CULL_FACE);
742 }
743
744 static void bindpoint_terrain( world_instance *world,
745 struct world_surface *mat )
746 {
747 struct shader_props_terrain *props = mat->info.props.compiled;
748
749 glActiveTexture( GL_TEXTURE1 );
750 glBindTexture( GL_TEXTURE_2D, world_get_texture(world, props->tex_diffuse) );
751 shader_scene_terrain_uBlendOffset( props->blend_offset );
752 shader_scene_terrain_uSandColour( props->sand_colour );
753 }
754
755 static void bindpoint_override( world_instance *world,
756 struct world_surface *mat )
757 {
758 if( mat->info.flags & k_material_flag_collision )
759 {
760 shader_scene_override_uAlphatest(0);
761 }
762 else
763 {
764 glActiveTexture( GL_TEXTURE1 );
765 glBindTexture( GL_TEXTURE_2D, world_get_texture(world, mat->alpha_tex) );
766 shader_scene_override_uAlphatest(1);
767 }
768 }
769
770 static void render_terrain( world_instance *world, vg_camera *cam )
771 {
772 shader_scene_terrain_use();
773 shader_scene_terrain_uTexGarbage(0);
774 shader_scene_terrain_uTexGradients(1);
775
776 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, scene_terrain );
777 glActiveTexture( GL_TEXTURE0 );
778 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
779
780 shader_scene_terrain_uPv( cam->mtx.pv );
781 shader_scene_terrain_uCamera( cam->transform[3] );
782
783 struct world_pass pass =
784 {
785 .shader = k_shader_terrain_blend,
786 .cam = cam,
787 .fn_bind = bindpoint_terrain,
788 .fn_set_mdl = shader_scene_terrain_uMdl,
789 .fn_set_uPvmPrev = shader_scene_terrain_uPvmPrev,
790 };
791
792 world_render_both_stages( world, &pass );
793 }
794
795 static void render_sky( world_instance *world, vg_camera *cam )
796 {
797 /*
798 * Modify matrix to remove clipping and view translation
799 */
800 m4x4f v,
801 v_prev,
802 pv,
803 pv_prev;
804
805 m4x4_copy( cam->mtx.v, v );
806 m4x4_copy( cam->mtx_prev.v, v_prev );
807
808 for( int i=0; i<3; i++ ){
809 v3_normalize(v[i]);
810 v3_normalize(v_prev[i]);
811 }
812 v3_zero( v[3] );
813 v3_zero( v_prev[3] );
814
815 m4x4_copy( cam->mtx.p, pv );
816 m4x4_copy( cam->mtx_prev.p, pv_prev );
817 m4x4_reset_clipping( pv, 100.0f, 0.1f );
818 m4x4_reset_clipping( pv_prev, 100.0f, 0.1f );
819
820 m4x4_mul( pv, v, pv );
821 m4x4_mul( pv_prev, v_prev, pv_prev );
822
823 m4x3f identity_matrix;
824 m4x3_identity( identity_matrix );
825
826 /*
827 * Draw
828 */
829 if( world->skybox == k_skybox_default ){
830 shader_model_sky_use();
831 shader_model_sky_uMdl( identity_matrix );
832 shader_model_sky_uPv( pv );
833 shader_model_sky_uPvmPrev( pv_prev );
834 shader_model_sky_uTexGarbage(0);
835 world_link_lighting_ub( world, _shader_model_sky.id );
836
837 glActiveTexture( GL_TEXTURE0 );
838 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
839 }
840 else if( world->skybox == k_skybox_space ){
841 shader_model_sky_space_use();
842
843 shader_model_sky_space_uMdl( identity_matrix );
844 shader_model_sky_space_uPv( pv );
845 shader_model_sky_space_uPvmPrev( pv_prev );
846 shader_model_sky_space_uTexGarbage(0);
847 world_link_lighting_ub( world, _shader_model_sky_space.id );
848
849 glActiveTexture( GL_TEXTURE0 );
850 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
851 }
852 else {
853 vg_fatal_error( "Programming error\n" );
854 }
855
856 glDepthMask( GL_FALSE );
857 glDisable( GL_DEPTH_TEST );
858
859 mesh_bind( &world_render.skydome );
860 mesh_draw( &world_render.skydome );
861
862 glEnable( GL_DEPTH_TEST );
863 glDepthMask( GL_TRUE );
864 }
865
866 void render_world_gates( world_instance *world, vg_camera *cam )
867 {
868 float closest = INFINITY;
869 struct ent_gate *gate = NULL;
870
871 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
872 ent_gate *gi = mdl_arritm( &world->ent_gate, i );
873
874 if( !(gi->flags & k_ent_gate_nonlocal) )
875 if( !(gi->flags & k_ent_gate_linked) )
876 continue;
877
878 float dist = v3_dist2( gi->co[0], cam->transform[3] );
879
880 vg_line_point( gi->co[0], 0.25f, VG__BLUE );
881
882 if( dist < closest ){
883 closest = dist;
884 gate = gi;
885 }
886 }
887
888 world->rendering_gate = gate;
889
890 if( gate ){
891 if( gate->flags & k_ent_gate_locked ){
892 world->rendering_gate = NULL;
893 return;
894 }
895
896 if( gate->flags & k_ent_gate_nonlocal ){
897 if( !(gate->flags & k_ent_gate_linked) ||
898 (world_static.load_state != k_world_loader_none) ){
899 world->rendering_gate = NULL;
900 render_gate_unlinked( world, gate, cam );
901 return;
902 }
903
904 world_instance *dest_world = &world_static.instances[ gate->target ];
905 render_gate( world, dest_world, gate, cam );
906 }
907 else
908 render_gate( world, world, gate, cam );
909 }
910 }
911
912 void world_prerender( world_instance *world )
913 {
914 if( mdl_arrcount( &world->ent_light ) ){
915 f32 rate = vg_maxf(0.1f, fabsf(k_day_length)) * vg_signf(k_day_length);
916 world->time += vg.time_frame_delta * (1.0/(rate*60.0));
917 }
918 else{
919 world->time = 0.834;
920 }
921
922 if( world->info.flags & 0x1 ){
923 world->time = world->info.timezone;
924 }
925
926 struct ub_world_lighting *state = &world->ub_lighting;
927
928 state->g_time = world->time;
929 state->g_realtime = vg.time_real;
930 state->g_debug_indices = k_debug_light_indices;
931 state->g_light_preview = k_light_preview;
932 state->g_debug_complexity = k_debug_light_complexity;
933 state->g_time_of_day = vg_fractf( world->time );
934
935 if( vg.quality_profile == k_quality_profile_high )
936 state->g_shadow_samples = 8;
937 else if( vg.quality_profile == k_quality_profile_low )
938 state->g_shadow_samples = 2;
939 else
940 state->g_shadow_samples = 0;
941
942 state->g_day_phase = cosf( state->g_time_of_day * VG_PIf * 2.0f );
943 state->g_sunset_phase= cosf( state->g_time_of_day * VG_PIf * 4.0f + VG_PIf );
944
945 state->g_day_phase = state->g_day_phase * 0.5f + 0.5f;
946 state->g_sunset_phase = powf( state->g_sunset_phase * 0.5f + 0.5f, 6.0f );
947
948 float a = state->g_time_of_day * VG_PIf * 2.0f;
949 state->g_sun_dir[0] = sinf( a );
950 state->g_sun_dir[1] = cosf( a );
951 state->g_sun_dir[2] = 0.2f;
952 v3_normalize( state->g_sun_dir );
953
954 world->probabilities[ k_probability_curve_constant ] = 1.0f;
955 float dp = state->g_day_phase;
956
957 world->probabilities[ k_probability_curve_wildlife_day ] =
958 (dp*dp*0.8f+state->g_sunset_phase)*0.8f;
959 world->probabilities[ k_probability_curve_wildlife_night ] =
960 1.0f-powf(fabsf((state->g_time_of_day-0.5f)*5.0f),5.0f);
961
962 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
963 glBufferSubData( GL_UNIFORM_BUFFER, 0,
964 sizeof(struct ub_world_lighting), &world->ub_lighting );
965 }
966
967 static void render_other_entities( world_instance *world, vg_camera *cam )
968 {
969 f32 radius = 40.0f;
970 bh_iter it;
971 bh_iter_init_range( 0, &it, cam->pos, radius+10.0f );
972
973 u32 glider_list[4],
974 glider_count = 0,
975 npc_list[4],
976 npc_count = 0;
977
978 i32 idx;
979 while( bh_next( world->entity_bh, &it, &idx ) ){
980 u32 id = world->entity_list[ idx ],
981 type = mdl_entity_id_type( id ),
982 index = mdl_entity_id_id( id );
983
984 if( type == k_ent_glider )
985 {
986 if( glider_count < vg_list_size(glider_list) )
987 glider_list[ glider_count ++ ] = index;
988 }
989 else if( type == k_ent_npc )
990 {
991 if( npc_count < vg_list_size(npc_list) )
992 npc_list[ npc_count ++ ] = index;
993 }
994 }
995
996 shader_model_entity_use();
997 shader_model_entity_uTexMain( 0 );
998 shader_model_entity_uCamera( cam->transform[3] );
999 shader_model_entity_uPv( cam->mtx.pv );
1000 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, model_entity );
1001
1002 for( u32 j=0; j<glider_count; j ++ )
1003 {
1004 ent_glider *glider = mdl_arritm( &world->ent_glider, glider_list[j] );
1005
1006 if( !(glider->flags & 0x1) )
1007 continue;
1008
1009 m4x3f mdl;
1010 mdl_transform_m4x3( &glider->transform, mdl );
1011
1012 f32 dist = v3_dist( glider->transform.co, cam->pos ) * (1.0f/radius),
1013 scale = vg_smoothstepf( vg_clampf( 5.0f-dist*5.0f, 0.0f,1.0f ) );
1014 m3x3_scalef( mdl, scale );
1015
1016 render_glider_model( cam, world, mdl, k_board_shader_entity );
1017 }
1018
1019 for( u32 j=0; j<npc_count; j ++ )
1020 {
1021 u32 index = npc_list[j];
1022 ent_npc *npc = mdl_arritm( &world->ent_npc, npc_list[j] );
1023 npc_update( npc );
1024 npc_render( npc, world, cam );
1025 }
1026 }
1027
1028 void render_world( world_instance *world, vg_camera *cam,
1029 int stenciled, int viewing_from_gate,
1030 int with_water, int with_cubemaps )
1031 {
1032 if( stenciled ){
1033 glClear( GL_DEPTH_BUFFER_BIT );
1034 glStencilFunc( GL_EQUAL, 1, 0xFF );
1035 glStencilMask( 0x00 );
1036 glEnable( GL_CULL_FACE );
1037 glEnable( GL_STENCIL_TEST );
1038 }
1039 else {
1040 glStencilMask( 0xFF );
1041 glStencilFunc( GL_ALWAYS, 1, 0xFF );
1042 glDisable( GL_STENCIL_TEST );
1043 }
1044
1045 render_sky( world, cam );
1046
1047 m4x3f identity;
1048 m4x3_identity(identity);
1049 render_world_routes( world, world, identity, cam, viewing_from_gate, 0 );
1050 render_world_standard( world, cam );
1051 render_world_cubemapped( world, cam, with_cubemaps );
1052
1053 render_world_vb( world, cam );
1054 render_world_alphatest( world, cam );
1055 render_world_foliage( world, cam );
1056 render_terrain( world, cam );
1057
1058 if( !viewing_from_gate ){
1059 world_entity_focus_render();
1060
1061 /* Render SFD's */
1062 u32 closest = 0;
1063 float min_dist = INFINITY;
1064
1065 if( mdl_arrcount( &world->ent_route ) ){
1066 for( u32 i=0; i<mdl_arrcount( &world->ent_route ); i++ ){
1067 ent_route *route = mdl_arritm( &world->ent_route, i );
1068 float dist = v3_dist2( route->board_transform[3], cam->pos );
1069
1070 if( dist < min_dist ){
1071 min_dist = dist;
1072 closest = i;
1073 }
1074 }
1075
1076 ent_route *route = mdl_arritm( &world->ent_route, closest );
1077 sfd_render( world, cam, route->board_transform );
1078 }
1079 }
1080
1081 if( !viewing_from_gate ){
1082 f32 greyout = 0.0f;
1083 if( mdl_entity_id_type(world_static.focused_entity) == k_ent_challenge )
1084 greyout = world_static.focus_strength;
1085
1086 if( greyout > 0.0f ){
1087 glDrawBuffers( 1, (GLenum[]){ GL_COLOR_ATTACHMENT0 } );
1088 glEnable(GL_BLEND);
1089 glDisable(GL_DEPTH_TEST);
1090 glDepthMask(GL_FALSE);
1091 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1092 glBlendEquation(GL_FUNC_ADD);
1093
1094 shader_blitcolour_use();
1095 shader_blitcolour_uColour( (v4f){ 0.5f, 0.5f, 0.5f, greyout*0.56f } );
1096 render_fsquad();
1097
1098 glDisable(GL_BLEND);
1099 glEnable(GL_DEPTH_TEST);
1100 glDepthMask(GL_TRUE);
1101 glDrawBuffers( 2, (GLenum[]){ GL_COLOR_ATTACHMENT0,
1102 GL_COLOR_ATTACHMENT1 } );
1103 }
1104
1105 render_world_fxglow( world, world, cam, NULL, 1, 1, 0 );
1106 }
1107
1108 if( with_water ){
1109 render_water_texture( world, cam );
1110 render_fb_bind( gpipeline.fb_main, 1 );
1111 }
1112
1113 if( stenciled ){
1114 glStencilFunc( GL_EQUAL, 1, 0xFF );
1115 glStencilMask( 0x00 );
1116 glEnable( GL_CULL_FACE );
1117 glEnable( GL_STENCIL_TEST );
1118 }
1119
1120 if( with_water ){
1121 render_water_surface( world, cam );
1122 }
1123
1124 render_remote_players( world, cam );
1125 render_other_entities( world, cam );
1126 ent_miniworld_render( world, cam );
1127
1128 if( stenciled ){
1129 glStencilMask( 0xFF );
1130 glStencilFunc( GL_ALWAYS, 1, 0xFF );
1131 glDisable( GL_STENCIL_TEST );
1132 }
1133 }
1134
1135
1136 static void render_world_override_pass( world_instance *world,
1137 struct world_pass *pass,
1138 m4x3f mmdl, m3x3f mnormal,
1139 m4x4f mpvm_prev )
1140 {
1141 for( int i=0; i<world->surface_count; i++ )
1142 {
1143 struct world_surface *mat = &world->surfaces[i];
1144 if( mat->info.flags & k_material_flag_ghosts ) continue;
1145
1146 mdl_submesh *sm;
1147 if( pass->geo_type == k_world_geo_type_solid )
1148 sm = &mat->sm_geo;
1149 else
1150 sm = &mat->sm_no_collide;
1151
1152 if( !sm->indice_count )
1153 continue;
1154
1155 pass->fn_set_mdl( mmdl );
1156 pass->fn_set_uNormalMtx( mnormal );
1157 pass->fn_set_uPvmPrev( mpvm_prev );
1158 pass->fn_bind( world, mat );
1159 mdl_draw_submesh( sm );
1160 }
1161 }
1162
1163 void render_world_override( world_instance *world,
1164 world_instance *lighting_source,
1165 m4x3f mmdl,
1166 vg_camera *cam,
1167 ent_spawn *dest_spawn, v4f map_info )
1168 {
1169 struct world_pass pass =
1170 {
1171 .cam = cam,
1172 .fn_bind = bindpoint_override,
1173 .fn_set_mdl = shader_scene_override_uMdl,
1174 .fn_set_uPvmPrev = shader_scene_override_uPvmPrev,
1175 .fn_set_uNormalMtx = shader_scene_override_uNormalMtx,
1176 .shader = k_shader_override
1177 };
1178
1179 shader_scene_override_use();
1180 shader_scene_override_uTexGarbage(0);
1181 shader_scene_override_uTexMain(1);
1182 shader_scene_override_uPv( pass.cam->mtx.pv );
1183 shader_scene_override_uMapInfo( map_info );
1184
1185 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( lighting_source, scene_override );
1186 bind_terrain_noise();
1187
1188 shader_scene_override_uCamera( pass.cam->transform[3] );
1189
1190 m4x4f mpvm_prev;
1191 m4x3_expand( mmdl, mpvm_prev );
1192 m4x4_mul( cam->mtx_prev.pv, mpvm_prev, mpvm_prev );
1193
1194 m3x3f mnormal;
1195 m3x3_inv( mmdl, mnormal );
1196 m3x3_transpose( mnormal, mnormal );
1197 v3_normalize( mnormal[0] );
1198 v3_normalize( mnormal[1] );
1199 v3_normalize( mnormal[2] );
1200
1201 v4f uPlayerPos, uSpawnPos;
1202 v4_zero( uPlayerPos );
1203 v4_zero( uSpawnPos );
1204
1205 v3_copy( world->player_co, uPlayerPos );
1206
1207 if( dest_spawn && (v3_dist2(dest_spawn->transform.co,uPlayerPos) > 0.1f) )
1208 v3_copy( dest_spawn->transform.co, uSpawnPos );
1209 else
1210 v3_add( uPlayerPos, (v3f){0,-1,0}, uSpawnPos );
1211
1212 uPlayerPos[3] = v3_dist(uPlayerPos,uSpawnPos);
1213 uSpawnPos[3] = 1.0f/uPlayerPos[3];
1214
1215 shader_scene_override_uPlayerPos( uPlayerPos );
1216 shader_scene_override_uSpawnPos( uSpawnPos );
1217
1218
1219 glDisable( GL_CULL_FACE );
1220 mesh_bind( &world->mesh_geo );
1221 pass.geo_type = k_world_geo_type_solid;
1222 render_world_override_pass( world, &pass, mmdl, mnormal, mpvm_prev );
1223 mesh_bind( &world->mesh_no_collide );
1224 pass.geo_type = k_world_geo_type_nonsolid;
1225 render_world_override_pass( world, &pass, mmdl, mnormal, mpvm_prev );
1226 glEnable( GL_CULL_FACE );
1227
1228 render_world_fxglow( world, world, cam, mmdl, 0, 0, 1 );
1229 }
1230
1231 static void render_cubemap_side( world_instance *world, ent_cubemap *cm,
1232 u32 side ){
1233 vg_camera cam;
1234 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
1235 GL_TEXTURE_CUBE_MAP_POSITIVE_X + side, cm->texture_id, 0 );
1236 glClear( GL_DEPTH_BUFFER_BIT );
1237
1238 v3f forward[6] = {
1239 { -1.0f, 0.0f, 0.0f },
1240 { 1.0f, 0.0f, 0.0f },
1241 { 0.0f, -1.0f, 0.0f },
1242 { 0.0f, 1.0f, 0.0f },
1243 { 0.0f, 0.0f, -1.0f },
1244 { 0.0f, 0.0f, 1.0f }
1245 };
1246 v3f up[6] = {
1247 { 0.0f, -1.0f, 0.0f },
1248 { 0.0f, -1.0f, 0.0f },
1249 { 0.0f, 0.0f, 1.0f },
1250 { 0.0f, 0.0f, -1.0f },
1251 { 0.0f, -1.0f, 0.0f },
1252 { 0.0f, -1.0f, 0.0f }
1253 };
1254
1255 v3_zero( cam.angles );
1256 v3_copy( cm->co, cam.pos );
1257
1258 v3_copy( forward[side], cam.transform[2] );
1259 v3_copy( up[side], cam.transform[1] );
1260 v3_cross( up[side], forward[side], cam.transform[0] );
1261 v3_copy( cm->co, cam.transform[3] );
1262 m4x3_invert_affine( cam.transform, cam.transform_inverse );
1263
1264 vg_camera_update_view( &cam );
1265
1266 cam.nearz = 0.1f;
1267 cam.farz = 1000.0f;
1268 cam.fov = 90.0f;
1269 m4x4_copy( cam.mtx.p, cam.mtx_prev.p );
1270 m4x4_projection( cam.mtx.p, cam.fov, 1.0f, cam.nearz, cam.farz );
1271 vg_camera_finalize( &cam );
1272 vg_camera_finalize( &cam );
1273
1274 render_world( world, &cam, 0, 1, 1, 0 );
1275 }
1276
1277 void render_world_cubemaps( world_instance *world )
1278 {
1279 if( world->cubemap_cooldown )
1280 world->cubemap_cooldown --;
1281 else{
1282 world->cubemap_cooldown = 60;
1283
1284 glViewport( 0, 0, WORLD_CUBEMAP_RES, WORLD_CUBEMAP_RES );
1285 for( u32 i=0; i<mdl_arrcount( &world->ent_cubemap ); i++ ){
1286 ent_cubemap *cm = mdl_arritm( &world->ent_cubemap, i );
1287 glBindFramebuffer( GL_FRAMEBUFFER, cm->framebuffer_id );
1288
1289 world->cubemap_side ++;
1290 if( world->cubemap_side >= 6 )
1291 world->cubemap_side = 0;
1292
1293 render_cubemap_side( world, cm, world->cubemap_side );
1294 }
1295 }
1296 }
1297
1298 /*
1299 * Geo shaders
1300 * ---------------------------------------------
1301 */
1302
1303 void render_world_depth( world_instance *world, vg_camera *cam )
1304 {
1305 m4x3f identity_matrix;
1306 m4x3_identity( identity_matrix );
1307
1308 shader_scene_depth_use();
1309 shader_scene_depth_uCamera( cam->transform[3] );
1310 shader_scene_depth_uPv( cam->mtx.pv );
1311 shader_scene_depth_uPvmPrev( cam->mtx_prev.pv );
1312 shader_scene_depth_uMdl( identity_matrix );
1313 world_link_lighting_ub( world, _shader_scene_depth.id );
1314
1315 mesh_bind( &world->mesh_geo );
1316 mesh_draw( &world->mesh_geo );
1317 }
1318
1319 void render_world_position( world_instance *world, vg_camera *cam )
1320 {
1321 m4x3f identity_matrix;
1322 m4x3_identity( identity_matrix );
1323
1324 shader_scene_position_use();
1325 shader_scene_position_uCamera( cam->transform[3] );
1326 shader_scene_position_uPv( cam->mtx.pv );
1327 shader_scene_position_uPvmPrev( cam->mtx_prev.pv );
1328 shader_scene_position_uMdl( identity_matrix );
1329 world_link_lighting_ub( world, _shader_scene_position.id );
1330
1331 mesh_bind( &world->mesh_geo );
1332 mesh_draw( &world->mesh_geo );
1333 }
1334
1335 struct ui_enum_opt skybox_setting_options[] = {
1336 { 0, "g_daysky_colour" },
1337 { 1, "g_nightsky_colour" },
1338 { 2, "g_sunset_colour" },
1339 { 3, "g_ambient_colour" },
1340 { 4, "g_sun_colour" },
1341 };
1342
1343 static f32 *skybox_prop_location( world_instance *world, i32 index ){
1344 switch( index ){
1345 case 0: return world->ub_lighting.g_daysky_colour; break;
1346 case 1: return world->ub_lighting.g_nightsky_colour; break;
1347 case 2: return world->ub_lighting.g_sunset_colour; break;
1348 case 3: return world->ub_lighting.g_ambient_colour; break;
1349 case 4: return world->ub_lighting.g_sun_colour; break;
1350 default: return NULL;
1351 }
1352 }
1353
1354 void imgui_world_light_edit( world_instance *world )
1355 {
1356 ui_rect panel = { vg.window_x-400, 0, 400, vg.window_y };
1357 ui_fill( panel, ui_colour( k_ui_bg+1 ) );
1358 ui_outline( panel, 1, ui_colour( k_ui_bg+7 ), 0 );
1359 ui_rect_pad( panel, (ui_px[2]){ 8, 8 } );
1360 vg_ui.wants_mouse = 1;
1361
1362 static i32 option_to_edit = 0;
1363 ui_enum( panel, "option", skybox_setting_options, 5, &option_to_edit );
1364 ui_colourpicker( panel, "colour",
1365 skybox_prop_location( world, option_to_edit ) );
1366
1367 if( ui_button( panel, "save tweaker file ('/tmp/tweaker.txt')\n" ) == 1 ){
1368 FILE *fp = fopen( "/tmp/tweaker.txt", "w" );
1369
1370 for( i32 i=0; i<5; i ++ ){
1371 struct ui_enum_opt *opt = &skybox_setting_options[i];
1372 f32 *val = skybox_prop_location( world, i );
1373 fprintf( fp, "%s = {%.3ff, %.3ff, %.3ff, %.3ff},\n",
1374 opt->alias, val[0], val[1], val[2], val[3] );
1375 }
1376 fclose( fp );
1377 }
1378 }