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