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