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