add player guide
[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_fxglow_register();
79 shader_scene_vertex_blend_register();
80 shader_scene_terrain_register();
81 shader_scene_depth_register();
82 shader_scene_position_register();
83 shader_model_sky_register();
84
85 vg_info( "Loading world resources\n" );
86 vg_linear_clear( vg_mem.scratch );
87
88 mdl_context msky;
89 mdl_open( &msky, "models/rs_skydome.mdl", vg_mem.scratch );
90 mdl_load_metadata_block( &msky, vg_mem.scratch );
91 mdl_async_load_glmesh( &msky, &world_render.skydome );
92 mdl_close( &msky );
93
94 vg_info( "Loading default world textures\n" );
95 vg_tex2d_load_qoi_async_file( "textures/garbage.qoi",
96 VG_TEX2D_NEAREST|VG_TEX2D_REPEAT,
97 &world_render.tex_terrain_noise );
98
99 vg_async_call( async_world_render_init, NULL, 0 );
100 }
101
102 VG_STATIC 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 VG_STATIC 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 VG_STATIC 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 VG_STATIC 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 VG_STATIC void render_world_depth( world_instance *world, camera *cam );
135
136 /*
137 * Rendering
138 */
139
140 VG_STATIC void bind_terrain_noise(void)
141 {
142 glActiveTexture( GL_TEXTURE0 );
143 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
144 }
145
146 struct world_pass{
147 camera *cam;
148 enum mdl_shader shader;
149 enum world_geo_type geo_type;
150
151 void (*fn_bind_textures)( world_instance *world,
152 struct world_surface *mat );
153 void (*fn_set_mdl)( m4x3f mdl );
154 void (*fn_set_uPvmPrev)( m4x4f pvm );
155 };
156
157 /* TODO: high level control pass renders. */
158
159 VG_STATIC
160 void world_render_if( world_instance *world, struct world_pass *pass )
161 {
162 for( int i=0; i<world->surface_count; i++ ){
163 struct world_surface *mat = &world->surfaces[i];
164
165 if( mat->info.shader == pass->shader ){
166 mdl_submesh *sm;
167
168 if( pass->geo_type == k_world_geo_type_solid )
169 sm = &mat->sm_geo;
170 else
171 sm = &mat->sm_no_collide;
172
173 if( !sm->indice_count )
174 continue;
175
176 m4x3f mmdl;
177 m4x3_identity( mmdl );
178 pass->fn_set_mdl( mmdl );
179 pass->fn_set_uPvmPrev( pass->cam->mtx_prev.pv );
180
181 pass->fn_bind_textures( world, mat );
182 mdl_draw_submesh( sm );
183
184 for( u32 j=0; j<mdl_arrcount( &world->ent_traffic ); j++ ){
185 ent_traffic *traffic = mdl_arritm( &world->ent_traffic, j );
186
187 for( u32 k=0; k<traffic->submesh_count; k++ ){
188 sm = mdl_arritm( &world->meta.submeshs,
189 traffic->submesh_start+k );
190
191 q_m3x3( traffic->transform.q, mmdl );
192 v3_copy( traffic->transform.co, mmdl[3] );
193
194 m4x4f m4mdl;
195 m4x3_expand( mmdl, m4mdl );
196 m4x4_mul( pass->cam->mtx_prev.pv, m4mdl, m4mdl );
197
198 pass->fn_set_mdl( mmdl );
199 pass->fn_set_uPvmPrev( m4mdl );
200
201 mdl_draw_submesh( sm );
202 }
203 }
204 }
205 }
206 }
207
208 VG_STATIC
209 void world_render_challenges( world_instance *world ){
210 if( !world ) return;
211
212 shader_scene_fxglow_use();
213 for( u32 i=0; i<mdl_arrcount(&world->ent_challenge); i++ ){
214 ent_challenge *challenge = mdl_arritm(&world->ent_challenge,i);
215
216 m4x3f mmdl;
217 mdl_transform_m4x3( &challenge->transform, mmdl );
218 shader_scene_fxglow_uMdl( mmdl );
219 }
220 }
221
222 VG_STATIC
223 void world_render_both_stages( world_instance *world, struct world_pass *pass )
224 {
225 mesh_bind( &world->mesh_geo );
226 pass->geo_type = k_world_geo_type_solid;
227 world_render_if( world, pass );
228
229 glDisable( GL_CULL_FACE );
230 mesh_bind( &world->mesh_no_collide );
231 pass->geo_type = k_world_geo_type_nonsolid;
232 world_render_if( world, pass );
233 glEnable( GL_CULL_FACE );
234 }
235
236 VG_STATIC void bindpoint_diffuse_texture1( world_instance *world,
237 struct world_surface *mat )
238
239 {
240 glActiveTexture( GL_TEXTURE1 );
241 glBindTexture( GL_TEXTURE_2D, world->textures[ mat->info.tex_diffuse ] );
242 }
243
244 VG_STATIC void render_world_vb( world_instance *world, camera *cam )
245 {
246 shader_scene_vertex_blend_use();
247 shader_scene_vertex_blend_uTexGarbage(0);
248 shader_scene_vertex_blend_uTexGradients(1);
249 world_link_lighting_ub( world, _shader_scene_vertex_blend.id );
250 world_bind_position_texture( world, _shader_scene_vertex_blend.id,
251 _uniform_scene_vertex_blend_g_world_depth, 2 );
252 world_bind_light_array( world, _shader_scene_vertex_blend.id,
253 _uniform_scene_vertex_blend_uLightsArray, 3 );
254 world_bind_light_index( world, _shader_scene_vertex_blend.id,
255 _uniform_scene_vertex_blend_uLightsIndex, 4 );
256
257 glActiveTexture( GL_TEXTURE0 );
258 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
259
260 shader_scene_vertex_blend_uPv( cam->mtx.pv );
261 shader_scene_vertex_blend_uCamera( cam->transform[3] );
262
263 struct world_pass pass = {
264 .shader = k_shader_standard_vertex_blend,
265 .cam = cam,
266 .fn_bind_textures = bindpoint_diffuse_texture1,
267 .fn_set_mdl = shader_scene_vertex_blend_uMdl,
268 .fn_set_uPvmPrev = shader_scene_vertex_blend_uPvmPrev,
269 };
270
271 world_render_both_stages( world, &pass );
272 }
273
274 VG_STATIC void render_world_standard( world_instance *world, camera *cam )
275 {
276 shader_scene_standard_use();
277 shader_scene_standard_uTexGarbage(0);
278 shader_scene_standard_uTexMain(1);
279 shader_scene_standard_uPv( cam->mtx.pv );
280
281 world_link_lighting_ub( world, _shader_scene_standard.id );
282 world_bind_position_texture( world, _shader_scene_standard.id,
283 _uniform_scene_standard_g_world_depth, 2 );
284 world_bind_light_array( world, _shader_scene_standard.id,
285 _uniform_scene_standard_uLightsArray, 3 );
286 world_bind_light_index( world, _shader_scene_standard.id,
287 _uniform_scene_standard_uLightsIndex, 4 );
288
289 bind_terrain_noise();
290 shader_scene_standard_uCamera( cam->transform[3] );
291
292 struct world_pass pass = {
293 .shader = k_shader_standard,
294 .cam = cam,
295 .fn_bind_textures = bindpoint_diffuse_texture1,
296 .fn_set_mdl = shader_scene_standard_uMdl,
297 .fn_set_uPvmPrev = shader_scene_standard_uPvmPrev,
298 };
299
300 world_render_both_stages( world, &pass );
301 }
302
303 VG_STATIC void render_world_alphatest( world_instance *world, camera *cam )
304 {
305 shader_scene_standard_alphatest_use();
306 shader_scene_standard_alphatest_uTexGarbage(0);
307 shader_scene_standard_alphatest_uTexMain(1);
308 shader_scene_standard_alphatest_uPv( cam->mtx.pv );
309
310 world_link_lighting_ub( world, _shader_scene_standard_alphatest.id );
311 world_bind_position_texture( world, _shader_scene_standard_alphatest.id,
312 _uniform_scene_standard_alphatest_g_world_depth, 2 );
313 world_bind_light_array( world, _shader_scene_standard_alphatest.id,
314 _uniform_scene_standard_alphatest_uLightsArray, 3 );
315 world_bind_light_index( world, _shader_scene_standard_alphatest.id,
316 _uniform_scene_standard_alphatest_uLightsIndex, 4 );
317
318
319 bind_terrain_noise();
320
321
322 shader_scene_standard_alphatest_uCamera( cam->transform[3] );
323
324 glDisable(GL_CULL_FACE);
325
326 struct world_pass pass = {
327 .shader = k_shader_standard_cutout,
328 .cam = cam,
329 .fn_bind_textures = bindpoint_diffuse_texture1,
330 .fn_set_mdl = shader_scene_standard_alphatest_uMdl,
331 .fn_set_uPvmPrev = shader_scene_standard_alphatest_uPvmPrev,
332 };
333
334 world_render_both_stages( world, &pass );
335
336 glEnable(GL_CULL_FACE);
337 }
338
339 VG_STATIC void render_world_fxglow( world_instance *world, camera *cam ){
340 //glDrawBuffers( 1, (GLenum[]){ GL_COLOR_ATTACHMENT0 } );
341
342 shader_scene_fxglow_use();
343 shader_scene_fxglow_uTexMain(1);
344 shader_scene_fxglow_uPv( cam->mtx.pv );
345
346 world_link_lighting_ub( world, _shader_scene_fxglow.id );
347 world_bind_position_texture( world, _shader_scene_fxglow.id,
348 _uniform_scene_fxglow_g_world_depth, 2 );
349 world_bind_light_array( world, _shader_scene_fxglow.id,
350 _uniform_scene_fxglow_uLightsArray, 3 );
351 world_bind_light_index( world, _shader_scene_fxglow.id,
352 _uniform_scene_fxglow_uLightsIndex, 4 );
353
354 shader_scene_fxglow_uCamera( cam->transform[3] );
355 glDisable(GL_CULL_FACE);
356
357 struct world_pass pass = {
358 .shader = k_shader_fxglow,
359 .cam = cam,
360 .fn_bind_textures = bindpoint_diffuse_texture1,
361 .fn_set_mdl = shader_scene_fxglow_uMdl,
362 .fn_set_uPvmPrev = shader_scene_fxglow_uPvmPrev,
363 };
364
365 world_render_both_stages( world, &pass );
366
367 glEnable(GL_CULL_FACE);
368 //glDrawBuffers( 2, (GLenum[]){ GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 } );
369 }
370
371 VG_STATIC void bindpoint_terrain( world_instance *world,
372 struct world_surface *mat )
373 {
374 glActiveTexture( GL_TEXTURE1 );
375 glBindTexture( GL_TEXTURE_2D, world->textures[ mat->info.tex_diffuse ] );
376
377 shader_scene_terrain_uSandColour( mat->info.colour );
378 shader_scene_terrain_uBlendOffset( mat->info.colour1 );
379 }
380
381 VG_STATIC void render_terrain( world_instance *world, camera *cam )
382 {
383 shader_scene_terrain_use();
384 shader_scene_terrain_uTexGarbage(0);
385 shader_scene_terrain_uTexGradients(1);
386
387 world_link_lighting_ub( world, _shader_scene_terrain.id );
388 world_bind_position_texture( world, _shader_scene_terrain.id,
389 _uniform_scene_terrain_g_world_depth, 2 );
390 world_bind_light_array( world, _shader_scene_terrain.id,
391 _uniform_scene_terrain_uLightsArray, 3 );
392 world_bind_light_index( world, _shader_scene_terrain.id,
393 _uniform_scene_terrain_uLightsIndex, 4 );
394
395 glActiveTexture( GL_TEXTURE0 );
396 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
397
398 shader_scene_terrain_uPv( cam->mtx.pv );
399 shader_scene_terrain_uCamera( cam->transform[3] );
400
401 struct world_pass pass = {
402 .shader = k_shader_terrain_blend,
403 .cam = cam,
404 .fn_bind_textures = bindpoint_terrain,
405 .fn_set_mdl = shader_scene_terrain_uMdl,
406 .fn_set_uPvmPrev = shader_scene_terrain_uPvmPrev,
407 };
408
409 world_render_both_stages( world, &pass );
410 }
411
412 VG_STATIC void render_sky( world_instance *world, camera *cam )
413 {
414 /*
415 * Modify matrix to remove clipping and view translation
416 */
417 m4x4f v,
418 v_prev,
419 pv,
420 pv_prev;
421
422 m4x4_copy( cam->mtx.v, v );
423 m4x4_copy( cam->mtx_prev.v, v_prev );
424 v3_zero( v[3] );
425 v3_zero( v_prev[3] );
426
427 m4x4_copy( cam->mtx.p, pv );
428 m4x4_copy( cam->mtx_prev.p, pv_prev );
429 m4x4_reset_clipping( pv, cam->farz, cam->nearz );
430 m4x4_reset_clipping( pv_prev, cam->farz, cam->nearz );
431
432 m4x4_mul( pv, v, pv );
433 m4x4_mul( pv_prev, v_prev, pv_prev );
434
435 m4x3f identity_matrix;
436 m4x3_identity( identity_matrix );
437
438 /*
439 * Draw
440 */
441 shader_model_sky_use();
442 shader_model_sky_uMdl( identity_matrix );
443 shader_model_sky_uPv( pv );
444 shader_model_sky_uPvmPrev( pv_prev );
445 shader_model_sky_uTexGarbage(0);
446 world_link_lighting_ub( world, _shader_model_sky.id );
447
448 glActiveTexture( GL_TEXTURE0 );
449 glBindTexture( GL_TEXTURE_2D, world_render.tex_terrain_noise );
450
451 glDepthMask( GL_FALSE );
452 glDisable( GL_DEPTH_TEST );
453
454 mesh_bind( &world_render.skydome );
455 mesh_draw( &world_render.skydome );
456
457 glEnable( GL_DEPTH_TEST );
458 glDepthMask( GL_TRUE );
459 }
460
461 VG_STATIC void render_world_gates( world_instance *world, camera *cam,
462 int layer_depth )
463 {
464 float closest = INFINITY;
465 struct ent_gate *gate = NULL;
466
467 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
468 ent_gate *gi = mdl_arritm( &world->ent_gate, i );
469
470 if( gi->type == k_gate_type_unlinked )
471 continue;
472
473 float dist = v3_dist2( gi->co[0], cam->transform[3] );
474
475 vg_line_point( gi->co[0], 0.25f, VG__BLUE );
476
477 if( dist < closest ){
478 closest = dist;
479 gate = gi;
480 }
481 }
482
483 if( gate ){
484 /* should really be set in fixed update since its used in the physics
485 * of most systems. too bad! */
486 world->rendering_gate = gate;
487
488 if( gate->type == k_gate_type_teleport ){
489 render_gate( world, gate, cam, layer_depth );
490 }
491 else if( gate->type == k_gate_type_nonlocel ){
492 if( world_loader.state != k_world_loader_none ){
493 world->rendering_gate = NULL;
494 return;
495 }
496
497 world_instance *dest_world = &world_static.worlds[ gate->target ];
498 render_gate( dest_world, gate, cam, layer_depth );
499 }
500 else
501 world->rendering_gate = NULL;
502 }
503 }
504
505 VG_STATIC void world_prerender( world_instance *world )
506 {
507
508 if( mdl_arrcount( &world->ent_light ) ){
509 f32 rate = vg_maxf(0.1f, fabsf(k_day_length)) * vg_signf(k_day_length);
510 world->time += vg.time_delta * (1.0/(rate*60.0));
511 }
512 else{
513 world->time = 0.834;
514 }
515
516 struct ub_world_lighting *state = &world->ub_lighting;
517
518 state->g_time = world->time;
519 state->g_realtime = vg.time;
520 state->g_debug_indices = k_debug_light_indices;
521 state->g_light_preview = k_light_preview;
522 state->g_debug_complexity = k_debug_light_complexity;
523 state->g_time_of_day = vg_fractf( world->time );
524 state->g_day_phase = cosf( state->g_time_of_day * VG_PIf * 2.0f );
525 state->g_sunset_phase= cosf( state->g_time_of_day * VG_PIf * 4.0f + VG_PIf );
526
527 state->g_day_phase = state->g_day_phase * 0.5f + 0.5f;
528 state->g_sunset_phase = powf( state->g_sunset_phase * 0.5f + 0.5f, 6.0f );
529
530 float a = state->g_time_of_day * VG_PIf * 2.0f;
531 state->g_sun_dir[0] = sinf( a );
532 state->g_sun_dir[1] = cosf( a );
533 state->g_sun_dir[2] = 0.2f;
534 v3_normalize( state->g_sun_dir );
535
536 world->probabilities[ k_probability_curve_constant ] = 1.0f;
537 float dp = state->g_day_phase;
538
539 world->probabilities[ k_probability_curve_wildlife_day ] =
540 (dp*dp*0.8f+state->g_sunset_phase)*0.8f;
541 world->probabilities[ k_probability_curve_wildlife_night ] =
542 1.0f-powf(fabsf((state->g_time_of_day-0.5f)*5.0f),5.0f);
543
544 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
545 glBufferSubData( GL_UNIFORM_BUFFER, 0,
546 sizeof(struct ub_world_lighting), &world->ub_lighting );
547 }
548
549 VG_STATIC void skateshop_render(void);
550 VG_STATIC void render_world( world_instance *world, camera *cam,
551 int layer_depth )
552 {
553 render_sky( world, cam );
554
555 render_world_routes( world, cam, layer_depth );
556 render_world_standard( world, cam );
557 render_world_vb( world, cam );
558 render_world_alphatest( world, cam );
559 render_world_fxglow( world, cam );
560 render_terrain( world, cam );
561
562 if( layer_depth == 0 ){
563 skateshop_render();
564
565 /* Render SFD's */
566 u32 closest = 0;
567 float min_dist = INFINITY;
568
569 if( !mdl_arrcount( &world->ent_route ) )
570 return;
571
572 for( u32 i=0; i<mdl_arrcount( &world->ent_route ); i++ ){
573 ent_route *route = mdl_arritm( &world->ent_route, i );
574 float dist = v3_dist2( route->board_transform[3], cam->pos );
575
576 if( dist < min_dist ){
577 min_dist = dist;
578 closest = i;
579 }
580 }
581
582 ent_route *route = mdl_arritm( &world->ent_route, closest );
583 sfd_render( world, cam, route->board_transform );
584 }
585 }
586
587 VG_STATIC void render_world_depth( world_instance *world, camera *cam )
588 {
589 m4x3f identity_matrix;
590 m4x3_identity( identity_matrix );
591
592 shader_scene_depth_use();
593 shader_scene_depth_uCamera( cam->transform[3] );
594 shader_scene_depth_uPv( cam->mtx.pv );
595 shader_scene_depth_uPvmPrev( cam->mtx_prev.pv );
596 shader_scene_depth_uMdl( identity_matrix );
597 world_link_lighting_ub( world, _shader_scene_depth.id );
598
599 mesh_bind( &world->mesh_geo );
600 mesh_draw( &world->mesh_geo );
601 }
602
603 VG_STATIC void render_world_position( world_instance *world, camera *cam )
604 {
605 m4x3f identity_matrix;
606 m4x3_identity( identity_matrix );
607
608 shader_scene_position_use();
609 shader_scene_position_uCamera( cam->transform[3] );
610 shader_scene_position_uPv( cam->mtx.pv );
611 shader_scene_position_uPvmPrev( cam->mtx_prev.pv );
612 shader_scene_position_uMdl( identity_matrix );
613 world_link_lighting_ub( world, _shader_scene_position.id );
614
615 mesh_bind( &world->mesh_geo );
616 mesh_draw( &world->mesh_geo );
617 }
618
619 #endif