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