save location & map, lighting qol
[carveJwlIkooP6JGAAIwe30JlM.git] / world_render.c
1 /*
2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef WORLD_RENDER_C
6 #define WORLD_RENDER_C
7
8 #include "world.h"
9 #include "world_render.h"
10
11 static int ccmd_set_time( int argc, const char *argv[] ){
12 if( argc == 1 ){
13 world_instance *world = world_current_instance();
14 world->time = atof( argv[0] );
15 }
16 else {
17 vg_error( "Usage set_time <0-1.0>\n" );
18 }
19 return 0;
20 }
21
22 VG_STATIC void async_world_render_init( void *payload, u32 size )
23 {
24 vg_info( "Allocate uniform buffers\n" );
25 for( int i=0; i<4; i++ ){
26 world_instance *world = &world_static.worlds[i];
27 world->ubo_bind_point = i;
28
29 glGenBuffers( 1, &world->ubo_lighting );
30 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
31 glBufferData( GL_UNIFORM_BUFFER, sizeof(struct ub_world_lighting),
32 NULL, GL_DYNAMIC_DRAW );
33
34 glBindBufferBase( GL_UNIFORM_BUFFER, i, world->ubo_lighting );
35 VG_CHECK_GL_ERR();
36 }
37
38 vg_info( "Allocate frame buffers\n" );
39 for( int i=0; i<4; i++ ){
40 world_instance *world = &world_static.worlds[i];
41 struct framebuffer *fb = &world->heightmap;
42
43 fb->display_name = NULL;
44 fb->link = NULL;
45 fb->fixed_w = 1024;
46 fb->fixed_h = 1024;
47 fb->resolution_div = 0;
48
49 fb->attachments[0].display_name = NULL;
50 fb->attachments[0].purpose = k_framebuffer_attachment_type_texture;
51 fb->attachments[0].internalformat = GL_RG16F;
52 fb->attachments[0].format = GL_RG;
53 fb->attachments[0].type = GL_FLOAT;
54 fb->attachments[0].attachment = GL_COLOR_ATTACHMENT0;
55
56 fb->attachments[1].purpose = k_framebuffer_attachment_type_none;
57 fb->attachments[2].purpose = k_framebuffer_attachment_type_none;
58 fb->attachments[3].purpose = k_framebuffer_attachment_type_none;
59 fb->attachments[4].purpose = k_framebuffer_attachment_type_none;
60
61 render_fb_allocate( fb );
62 }
63 }
64
65 VG_STATIC void world_render_init(void)
66 {
67 VG_VAR_F32( k_day_length );
68 VG_VAR_I32( k_debug_light_indices );
69 VG_VAR_I32( k_debug_light_complexity );
70 VG_VAR_I32( k_light_preview );
71 vg_console_reg_cmd( "set_time", ccmd_set_time, NULL );
72
73 world_render.sky_rate = 1.0;
74 world_render.sky_target_rate = 1.0;
75
76 shader_scene_standard_register();
77 shader_scene_standard_alphatest_register();
78 shader_scene_vertex_blend_register();
79 shader_scene_terrain_register();
80 shader_scene_depth_register();
81 shader_scene_position_register();
82 shader_model_sky_register();
83
84 vg_info( "Loading world resources\n" );
85 vg_linear_clear( vg_mem.scratch );
86
87 mdl_context msky;
88 mdl_open( &msky, "models/rs_skydome.mdl", vg_mem.scratch );
89 mdl_load_metadata_block( &msky, vg_mem.scratch );
90 mdl_async_load_glmesh( &msky, &world_render.skydome );
91 mdl_close( &msky );
92
93 vg_info( "Loading default world textures\n" );
94 vg_tex2d_load_qoi_async_file( "textures/garbage.qoi",
95 VG_TEX2D_NEAREST|VG_TEX2D_REPEAT,
96 &world_render.tex_terrain_noise );
97
98 vg_async_call( async_world_render_init, NULL, 0 );
99 }
100
101 VG_STATIC 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 VG_STATIC 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 VG_STATIC 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 VG_STATIC 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 VG_STATIC void render_world_depth( world_instance *world, camera *cam );
134
135 /*
136 * Rendering
137 */
138
139 VG_STATIC void bind_terrain_noise(void)
140 {
141 glActiveTexture( GL_TEXTURE0 );
142 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
143 }
144
145 struct world_pass{
146 camera *cam;
147 enum mdl_shader shader;
148 enum world_geo_type geo_type;
149
150 void (*fn_bind_textures)( world_instance *world,
151 struct world_surface *mat );
152 void (*fn_set_mdl)( m4x3f mdl );
153 void (*fn_set_uPvmPrev)( m4x4f pvm );
154 };
155
156 VG_STATIC void world_render_if( world_instance *world, struct world_pass *pass )
157 {
158 for( int i=0; i<world->surface_count; i++ ){
159 struct world_surface *mat = &world->surfaces[i];
160
161 if( mat->info.shader == pass->shader ){
162 mdl_submesh *sm;
163
164 if( pass->geo_type == k_world_geo_type_solid )
165 sm = &mat->sm_geo;
166 else
167 sm = &mat->sm_no_collide;
168
169 if( !sm->indice_count )
170 continue;
171
172 m4x3f mmdl;
173 m4x3_identity( mmdl );
174 pass->fn_set_mdl( mmdl );
175 pass->fn_set_uPvmPrev( pass->cam->mtx_prev.pv );
176
177 pass->fn_bind_textures( world, mat );
178 mdl_draw_submesh( sm );
179
180 for( u32 j=0; j<mdl_arrcount( &world->ent_traffic ); j++ ){
181 ent_traffic *traffic = mdl_arritm( &world->ent_traffic, j );
182
183 for( u32 k=0; k<traffic->submesh_count; k++ ){
184 sm = mdl_arritm( &world->meta.submeshs,
185 traffic->submesh_start+k );
186
187 q_m3x3( traffic->transform.q, mmdl );
188 v3_copy( traffic->transform.co, mmdl[3] );
189
190 m4x4f m4mdl;
191 m4x3_expand( mmdl, m4mdl );
192 m4x4_mul( pass->cam->mtx_prev.pv, m4mdl, m4mdl );
193
194 pass->fn_set_mdl( mmdl );
195 pass->fn_set_uPvmPrev( m4mdl );
196
197 mdl_draw_submesh( sm );
198 }
199 }
200 }
201 }
202 }
203
204 VG_STATIC
205 void world_render_both_stages( world_instance *world, struct world_pass *pass )
206 {
207 mesh_bind( &world->mesh_geo );
208 pass->geo_type = k_world_geo_type_solid;
209 world_render_if( world, pass );
210
211 glDisable( GL_CULL_FACE );
212 mesh_bind( &world->mesh_no_collide );
213 pass->geo_type = k_world_geo_type_nonsolid;
214 world_render_if( world, pass );
215 glEnable( GL_CULL_FACE );
216 }
217
218 VG_STATIC void bindpoint_diffuse_texture1( world_instance *world,
219 struct world_surface *mat )
220
221 {
222 glActiveTexture( GL_TEXTURE1 );
223 glBindTexture( GL_TEXTURE_2D, world->textures[ mat->info.tex_diffuse ] );
224 }
225
226 VG_STATIC void render_world_vb( world_instance *world, camera *cam )
227 {
228 shader_scene_vertex_blend_use();
229 shader_scene_vertex_blend_uTexGarbage(0);
230 shader_scene_vertex_blend_uTexGradients(1);
231 world_link_lighting_ub( world, _shader_scene_vertex_blend.id );
232 world_bind_position_texture( world, _shader_scene_vertex_blend.id,
233 _uniform_scene_vertex_blend_g_world_depth, 2 );
234 world_bind_light_array( world, _shader_scene_vertex_blend.id,
235 _uniform_scene_vertex_blend_uLightsArray, 3 );
236 world_bind_light_index( world, _shader_scene_vertex_blend.id,
237 _uniform_scene_vertex_blend_uLightsIndex, 4 );
238
239 glActiveTexture( GL_TEXTURE0 );
240 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
241
242 shader_scene_vertex_blend_uPv( cam->mtx.pv );
243 shader_scene_vertex_blend_uCamera( cam->transform[3] );
244
245 struct world_pass pass = {
246 .shader = k_shader_standard_vertex_blend,
247 .cam = cam,
248 .fn_bind_textures = bindpoint_diffuse_texture1,
249 .fn_set_mdl = shader_scene_vertex_blend_uMdl,
250 .fn_set_uPvmPrev = shader_scene_vertex_blend_uPvmPrev,
251 };
252
253 world_render_both_stages( world, &pass );
254 }
255
256 VG_STATIC void render_world_standard( world_instance *world, camera *cam )
257 {
258 shader_scene_standard_use();
259 shader_scene_standard_uTexGarbage(0);
260 shader_scene_standard_uTexMain(1);
261 shader_scene_standard_uPv( cam->mtx.pv );
262
263 world_link_lighting_ub( world, _shader_scene_standard.id );
264 world_bind_position_texture( world, _shader_scene_standard.id,
265 _uniform_scene_standard_g_world_depth, 2 );
266 world_bind_light_array( world, _shader_scene_standard.id,
267 _uniform_scene_standard_uLightsArray, 3 );
268 world_bind_light_index( world, _shader_scene_standard.id,
269 _uniform_scene_standard_uLightsIndex, 4 );
270
271 bind_terrain_noise();
272 shader_scene_standard_uCamera( cam->transform[3] );
273
274 struct world_pass pass = {
275 .shader = k_shader_standard,
276 .cam = cam,
277 .fn_bind_textures = bindpoint_diffuse_texture1,
278 .fn_set_mdl = shader_scene_standard_uMdl,
279 .fn_set_uPvmPrev = shader_scene_standard_uPvmPrev,
280 };
281
282 world_render_both_stages( world, &pass );
283 }
284
285 VG_STATIC void render_world_alphatest( world_instance *world, camera *cam )
286 {
287 shader_scene_standard_alphatest_use();
288 shader_scene_standard_alphatest_uTexGarbage(0);
289 shader_scene_standard_alphatest_uTexMain(1);
290 shader_scene_standard_alphatest_uPv( cam->mtx.pv );
291
292 world_link_lighting_ub( world, _shader_scene_standard_alphatest.id );
293 world_bind_position_texture( world, _shader_scene_standard_alphatest.id,
294 _uniform_scene_standard_alphatest_g_world_depth, 2 );
295 world_bind_light_array( world, _shader_scene_standard_alphatest.id,
296 _uniform_scene_standard_alphatest_uLightsArray, 3 );
297 world_bind_light_index( world, _shader_scene_standard_alphatest.id,
298 _uniform_scene_standard_alphatest_uLightsIndex, 4 );
299
300
301 bind_terrain_noise();
302
303
304 shader_scene_standard_alphatest_uCamera( cam->transform[3] );
305
306 glDisable(GL_CULL_FACE);
307
308 struct world_pass pass = {
309 .shader = k_shader_standard_cutout,
310 .cam = cam,
311 .fn_bind_textures = bindpoint_diffuse_texture1,
312 .fn_set_mdl = shader_scene_standard_alphatest_uMdl,
313 .fn_set_uPvmPrev = shader_scene_standard_alphatest_uPvmPrev,
314 };
315
316 world_render_both_stages( world, &pass );
317
318 glEnable(GL_CULL_FACE);
319 }
320
321 VG_STATIC void bindpoint_terrain( world_instance *world,
322 struct world_surface *mat )
323 {
324 glActiveTexture( GL_TEXTURE1 );
325 glBindTexture( GL_TEXTURE_2D, world->textures[ mat->info.tex_diffuse ] );
326
327 shader_scene_terrain_uSandColour( mat->info.colour );
328 shader_scene_terrain_uBlendOffset( mat->info.colour1 );
329 }
330
331 VG_STATIC void render_terrain( world_instance *world, camera *cam )
332 {
333 shader_scene_terrain_use();
334 shader_scene_terrain_uTexGarbage(0);
335 shader_scene_terrain_uTexGradients(1);
336
337 world_link_lighting_ub( world, _shader_scene_terrain.id );
338 world_bind_position_texture( world, _shader_scene_terrain.id,
339 _uniform_scene_terrain_g_world_depth, 2 );
340 world_bind_light_array( world, _shader_scene_terrain.id,
341 _uniform_scene_terrain_uLightsArray, 3 );
342 world_bind_light_index( world, _shader_scene_terrain.id,
343 _uniform_scene_terrain_uLightsIndex, 4 );
344
345 glActiveTexture( GL_TEXTURE0 );
346 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
347
348 shader_scene_terrain_uPv( cam->mtx.pv );
349 shader_scene_terrain_uCamera( cam->transform[3] );
350
351 struct world_pass pass = {
352 .shader = k_shader_terrain_blend,
353 .cam = cam,
354 .fn_bind_textures = bindpoint_terrain,
355 .fn_set_mdl = shader_scene_terrain_uMdl,
356 .fn_set_uPvmPrev = shader_scene_terrain_uPvmPrev,
357 };
358
359 world_render_both_stages( world, &pass );
360 }
361
362 VG_STATIC void render_sky( world_instance *world, camera *cam )
363 {
364 /*
365 * Modify matrix to remove clipping and view translation
366 */
367 m4x4f v,
368 v_prev,
369 pv,
370 pv_prev;
371
372 m4x4_copy( cam->mtx.v, v );
373 m4x4_copy( cam->mtx_prev.v, v_prev );
374 v3_zero( v[3] );
375 v3_zero( v_prev[3] );
376
377 m4x4_copy( cam->mtx.p, pv );
378 m4x4_copy( cam->mtx_prev.p, pv_prev );
379 m4x4_reset_clipping( pv, cam->farz, cam->nearz );
380 m4x4_reset_clipping( pv_prev, cam->farz, cam->nearz );
381
382 m4x4_mul( pv, v, pv );
383 m4x4_mul( pv_prev, v_prev, pv_prev );
384
385 m4x3f identity_matrix;
386 m4x3_identity( identity_matrix );
387
388 /*
389 * Draw
390 */
391 shader_model_sky_use();
392 shader_model_sky_uMdl( identity_matrix );
393 shader_model_sky_uPv( pv );
394 shader_model_sky_uPvmPrev( pv_prev );
395 shader_model_sky_uTexGarbage(0);
396 world_link_lighting_ub( world, _shader_model_sky.id );
397
398 glActiveTexture( GL_TEXTURE0 );
399 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
400
401 glDepthMask( GL_FALSE );
402 glDisable( GL_DEPTH_TEST );
403
404 mesh_bind( &world_render.skydome );
405 mesh_draw( &world_render.skydome );
406
407 glEnable( GL_DEPTH_TEST );
408 glDepthMask( GL_TRUE );
409 }
410
411 VG_STATIC void render_world_gates( world_instance *world, camera *cam,
412 int layer_depth )
413 {
414 float closest = INFINITY;
415 struct ent_gate *gate = NULL;
416
417 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
418 ent_gate *gi = mdl_arritm( &world->ent_gate, i );
419
420 if( gi->type == k_gate_type_unlinked )
421 continue;
422
423 float dist = v3_dist2( gi->co[0], cam->transform[3] );
424
425 vg_line_pt3( gi->co[0], 0.25f, VG__BLUE );
426
427 if( dist < closest ){
428 closest = dist;
429 gate = gi;
430 }
431 }
432
433 if( gate ){
434 /* should really be set in fixed update since its used in the physics
435 * of most systems. too bad! */
436 world->rendering_gate = gate;
437
438 if( gate->type == k_gate_type_teleport ){
439 render_gate( world, gate, cam, layer_depth );
440 }
441 else if( gate->type == k_gate_type_nonlocel ){
442 if( world_loader.state != k_world_loader_none ){
443 world->rendering_gate = NULL;
444 return;
445 }
446
447 world_instance *dest_world = &world_static.worlds[ gate->target ];
448 render_gate( dest_world, gate, cam, layer_depth );
449 }
450 else
451 world->rendering_gate = NULL;
452 }
453 }
454
455 VG_STATIC void world_prerender( world_instance *world )
456 {
457
458 if( mdl_arrcount( &world->ent_light ) ){
459 f32 rate = vg_maxf(0.1f, fabsf(k_day_length)) * vg_signf(k_day_length);
460 world->time += vg.time_delta * (1.0/(rate*60.0));
461 }
462 else{
463 world->time = 0.834;
464 }
465
466 struct ub_world_lighting *state = &world->ub_lighting;
467
468 state->g_time = world->time;
469 state->g_realtime = vg.time;
470 state->g_debug_indices = k_debug_light_indices;
471 state->g_light_preview = k_light_preview;
472 state->g_debug_complexity = k_debug_light_complexity;
473 state->g_time_of_day = vg_fractf( world->time );
474 state->g_day_phase = cosf( state->g_time_of_day * VG_PIf * 2.0f );
475 state->g_sunset_phase= cosf( state->g_time_of_day * VG_PIf * 4.0f + VG_PIf );
476
477 state->g_day_phase = state->g_day_phase * 0.5f + 0.5f;
478 state->g_sunset_phase = powf( state->g_sunset_phase * 0.5f + 0.5f, 6.0f );
479
480 float a = state->g_time_of_day * VG_PIf * 2.0f;
481 state->g_sun_dir[0] = sinf( a );
482 state->g_sun_dir[1] = cosf( a );
483 state->g_sun_dir[2] = 0.2f;
484 v3_normalize( state->g_sun_dir );
485
486 world->probabilities[ k_probability_curve_constant ] = 1.0f;
487 float dp = state->g_day_phase;
488
489 world->probabilities[ k_probability_curve_wildlife_day ] =
490 (dp*dp*0.8f+state->g_sunset_phase)*0.8f;
491 world->probabilities[ k_probability_curve_wildlife_night ] =
492 1.0f-powf(fabsf((state->g_time_of_day-0.5f)*5.0f),5.0f);
493
494 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
495 glBufferSubData( GL_UNIFORM_BUFFER, 0,
496 sizeof(struct ub_world_lighting), &world->ub_lighting );
497 }
498
499 VG_STATIC void skateshop_render(void);
500 VG_STATIC void render_world( world_instance *world, camera *cam,
501 int layer_depth )
502 {
503 render_sky( world, cam );
504
505 render_world_routes( world, cam, layer_depth );
506 render_world_standard( world, cam );
507 render_world_vb( world, cam );
508 render_world_alphatest( world, cam );
509 render_terrain( world, cam );
510
511 if( layer_depth == 0 ){
512 skateshop_render();
513
514 /* Render SFD's */
515 u32 closest = 0;
516 float min_dist = INFINITY;
517
518 if( !mdl_arrcount( &world->ent_route ) )
519 return;
520
521 for( u32 i=0; i<mdl_arrcount( &world->ent_route ); i++ ){
522 ent_route *route = mdl_arritm( &world->ent_route, i );
523 float dist = v3_dist2( route->board_transform[3], cam->pos );
524
525 if( dist < min_dist ){
526 min_dist = dist;
527 closest = i;
528 }
529 }
530
531 ent_route *route = mdl_arritm( &world->ent_route, closest );
532 sfd_render( world, cam, route->board_transform );
533 }
534 }
535
536 VG_STATIC void render_world_depth( world_instance *world, camera *cam )
537 {
538 m4x3f identity_matrix;
539 m4x3_identity( identity_matrix );
540
541 shader_scene_depth_use();
542 shader_scene_depth_uCamera( cam->transform[3] );
543 shader_scene_depth_uPv( cam->mtx.pv );
544 shader_scene_depth_uPvmPrev( cam->mtx_prev.pv );
545 shader_scene_depth_uMdl( identity_matrix );
546 world_link_lighting_ub( world, _shader_scene_depth.id );
547
548 mesh_bind( &world->mesh_geo );
549 mesh_draw( &world->mesh_geo );
550 }
551
552 VG_STATIC void render_world_position( world_instance *world, camera *cam )
553 {
554 m4x3f identity_matrix;
555 m4x3_identity( identity_matrix );
556
557 shader_scene_position_use();
558 shader_scene_position_uCamera( cam->transform[3] );
559 shader_scene_position_uPv( cam->mtx.pv );
560 shader_scene_position_uPvmPrev( cam->mtx_prev.pv );
561 shader_scene_position_uMdl( identity_matrix );
562 world_link_lighting_ub( world, _shader_scene_position.id );
563
564 mesh_bind( &world->mesh_geo );
565 mesh_draw( &world->mesh_geo );
566 }
567
568 #endif