shader vacuuming
[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 /* TODO: We could get away with this being R16u, and just have it be
52 * a normed value between min and max of the bounding box Y */
53
54 fb->attachments[0].display_name = NULL;
55 fb->attachments[0].purpose = k_framebuffer_attachment_type_colour;
56 fb->attachments[0].internalformat = GL_RG16F;
57 fb->attachments[0].format = GL_RG;
58 fb->attachments[0].type = GL_FLOAT;
59 fb->attachments[0].attachment = GL_COLOR_ATTACHMENT0;
60
61 fb->attachments[1].purpose = k_framebuffer_attachment_type_none;
62 fb->attachments[2].purpose = k_framebuffer_attachment_type_none;
63 fb->attachments[3].purpose = k_framebuffer_attachment_type_none;
64 fb->attachments[4].purpose = k_framebuffer_attachment_type_none;
65
66 render_fb_allocate( fb );
67 }
68 }
69 vg_release_thread_sync();
70 }
71
72 VG_STATIC void world_link_lighting_ub( world_instance *world,
73 GLuint shader, int texture_id )
74 {
75 GLuint idx = glGetUniformBlockIndex( shader, "ub_world_lighting" );
76 glUniformBlockBinding( shader, idx, world->ubo_bind_point );
77
78 render_fb_bind_texture( &world->heightmap, 0, texture_id );
79 glUniform1i( glGetUniformLocation( shader, "g_world_depth" ), texture_id );
80 }
81
82 VG_STATIC void render_world_depth( world_instance *world, camera *cam );
83
84 /*
85 * Rendering
86 */
87
88 VG_STATIC void bind_terrain_noise(void)
89 {
90 vg_tex2d_bind( &tex_terrain_noise, 0 );
91 }
92
93 typedef void (*func_bind_point)( world_instance *world,
94 struct world_material *mat );
95
96 VG_STATIC void world_render_if( world_instance *world,
97 enum mdl_shader shader,
98 enum geo_type geo_type,
99 func_bind_point bind_point )
100 {
101
102 for( int i=0; i<world->material_count; i++ )
103 {
104 struct world_material *mat = &world->materials[i];
105
106 if( mat->info.shader == shader )
107 {
108 mdl_submesh *sm;
109
110 if( geo_type == k_geo_type_solid )
111 sm = &mat->sm_geo;
112 else
113 sm = &mat->sm_no_collide;
114
115 if( !sm->indice_count )
116 continue;
117
118 bind_point( world, mat );
119 mdl_draw_submesh( sm );
120 }
121 }
122 }
123
124 VG_STATIC
125 void world_render_both_stages( world_instance *world,
126 enum mdl_shader shader,
127 func_bind_point bind_point )
128 {
129 mesh_bind( &world->mesh_geo );
130 world_render_if( world, shader, k_geo_type_solid, bind_point );
131
132 glDisable( GL_CULL_FACE );
133 mesh_bind( &world->mesh_no_collide );
134 world_render_if( world, shader, k_geo_type_nonsolid, bind_point );
135 glEnable( GL_CULL_FACE );
136 }
137
138 VG_STATIC void bindpoint_diffuse_texture1( world_instance *world,
139 struct world_material *mat )
140 {
141 glActiveTexture( GL_TEXTURE1 );
142 glBindTexture( GL_TEXTURE_2D, world->textures[ mat->info.tex_diffuse ] );
143 }
144
145 VG_STATIC void render_world_vb( world_instance *world, camera *cam )
146 {
147 m4x3f identity_matrix;
148 m4x3_identity( identity_matrix );
149
150 shader_scene_vertex_blend_use();
151 shader_scene_vertex_blend_uTexGarbage(0);
152 shader_scene_vertex_blend_uTexGradients(1);
153 world_link_lighting_ub( world, _shader_scene_vertex_blend.id, 2 );
154 vg_tex2d_bind( &tex_terrain_noise, 0 );
155
156 shader_scene_vertex_blend_uPv( cam->mtx.pv );
157 shader_scene_vertex_blend_uPvmPrev( cam->mtx_prev.pv );
158 shader_scene_vertex_blend_uMdl( identity_matrix );
159 shader_scene_vertex_blend_uCamera( cam->transform[3] );
160 shader_scene_vertex_blend_uBoard0( TEMP_BOARD_0 );
161 shader_scene_vertex_blend_uBoard1( TEMP_BOARD_1 );
162
163 world_render_both_stages( world, k_shader_standard_vertex_blend,
164 bindpoint_diffuse_texture1 );
165 }
166
167 VG_STATIC void render_world_standard( world_instance *world, camera *cam )
168 {
169 m4x3f identity_matrix;
170 m4x3_identity( identity_matrix );
171
172 shader_scene_standard_use();
173 shader_scene_standard_uTexGarbage(0);
174 shader_scene_standard_uTexMain(1);
175 shader_scene_standard_uPv( cam->mtx.pv );
176 shader_scene_standard_uPvmPrev( cam->mtx_prev.pv );
177 world_link_lighting_ub( world, _shader_scene_standard.id, 2 );
178 bind_terrain_noise();
179
180 shader_scene_standard_uMdl( identity_matrix );
181 shader_scene_standard_uCamera( cam->transform[3] );
182 shader_scene_standard_uBoard0( TEMP_BOARD_0 );
183 shader_scene_standard_uBoard1( TEMP_BOARD_1 );
184
185 world_render_both_stages( world, k_shader_standard,
186 bindpoint_diffuse_texture1 );
187 }
188
189 VG_STATIC void render_world_alphatest( world_instance *world, camera *cam )
190 {
191 m4x3f identity_matrix;
192 m4x3_identity( identity_matrix );
193
194 shader_scene_standard_alphatest_use();
195 shader_scene_standard_alphatest_uTexGarbage(0);
196 shader_scene_standard_alphatest_uTexMain(1);
197 shader_scene_standard_alphatest_uPv( cam->mtx.pv );
198 shader_scene_standard_alphatest_uPvmPrev( cam->mtx_prev.pv );
199 world_link_lighting_ub( world, _shader_scene_standard_alphatest.id, 2 );
200 bind_terrain_noise();
201
202 shader_scene_standard_alphatest_uMdl( identity_matrix );
203 shader_scene_standard_alphatest_uCamera( cam->transform[3] );
204 shader_scene_standard_alphatest_uBoard0( TEMP_BOARD_0 );
205 shader_scene_standard_alphatest_uBoard1( TEMP_BOARD_1 );
206
207 glDisable(GL_CULL_FACE);
208
209 world_render_both_stages( world, k_shader_standard_cutout,
210 bindpoint_diffuse_texture1 );
211
212 glEnable(GL_CULL_FACE);
213 }
214
215 VG_STATIC void bindpoint_terrain( world_instance *world,
216 struct world_material *mat )
217 {
218 glActiveTexture( GL_TEXTURE1 );
219 glBindTexture( GL_TEXTURE_2D, world->textures[ mat->info.tex_diffuse ] );
220
221 shader_scene_terrain_uSandColour( mat->info.colour );
222 shader_scene_terrain_uBlendOffset( mat->info.colour1 );
223 }
224
225 VG_STATIC void render_terrain( world_instance *world, camera *cam )
226 {
227 m4x3f identity_matrix;
228 m4x3_identity( identity_matrix );
229
230 shader_scene_terrain_use();
231 shader_scene_terrain_uTexGarbage(0);
232 shader_scene_terrain_uTexGradients(1);
233 world_link_lighting_ub( world, _shader_scene_terrain.id, 2 );
234
235 vg_tex2d_bind( &tex_terrain_noise, 0 );
236
237 shader_scene_terrain_uPv( cam->mtx.pv );
238 shader_scene_terrain_uPvmPrev( cam->mtx_prev.pv );
239
240 shader_scene_terrain_uMdl( identity_matrix );
241 shader_scene_terrain_uCamera( cam->transform[3] );
242 shader_scene_terrain_uBoard0( TEMP_BOARD_0 );
243 shader_scene_terrain_uBoard1( TEMP_BOARD_1 );
244
245 world_render_both_stages( world, k_shader_terrain_blend, bindpoint_terrain );
246 }
247
248 VG_STATIC void render_sky( camera *cam )
249 {
250 /*
251 * Modify matrix to remove clipping and view translation
252 */
253 m4x4f v,
254 v_prev,
255 pv,
256 pv_prev;
257
258 m4x4_copy( cam->mtx.v, v );
259 m4x4_copy( cam->mtx_prev.v, v_prev );
260 v3_zero( v[3] );
261 v3_zero( v_prev[3] );
262
263 m4x4_copy( cam->mtx.p, pv );
264 m4x4_copy( cam->mtx_prev.p, pv_prev );
265 m4x4_reset_clipping( pv, cam->farz, cam->nearz );
266 m4x4_reset_clipping( pv_prev, cam->farz, cam->nearz );
267
268 m4x4_mul( pv, v, pv );
269 m4x4_mul( pv_prev, v_prev, pv_prev );
270
271 m4x3f identity_matrix;
272 m4x3_identity( identity_matrix );
273
274 /*
275 * Draw
276 */
277 shader_model_sky_use();
278 shader_model_sky_uMdl( identity_matrix );
279 shader_model_sky_uPv( pv );
280 shader_model_sky_uPvmPrev( pv_prev );
281 shader_model_sky_uTexGarbage(0);
282 shader_model_sky_uTime( world_global.sky_time );
283
284 vg_tex2d_bind( &tex_terrain_noise, 0 );
285
286 glDepthMask( GL_FALSE );
287 glDisable( GL_DEPTH_TEST );
288
289 mesh_bind( &world_global.skydome );
290 mdl_draw_submesh( &world_global.dome_upper );
291
292 glEnable( GL_DEPTH_TEST );
293 glDepthMask( GL_TRUE );
294 }
295
296 VG_STATIC void render_world_gates( world_instance *world, camera *cam )
297 {
298 float closest = INFINITY;
299
300 struct teleport_gate *gate = NULL;
301 world_instance *dest_world = world;
302
303 for( int i=0; i<world->gate_count; i++ )
304 {
305 struct route_gate *rg = &world->gates[i];
306 float dist = v3_dist2( rg->gate.co[0], cam->transform[3] );
307
308 vg_line_pt3( rg->gate.co[0], 0.25f, VG__BLUE );
309
310 if( dist < closest )
311 {
312 closest = dist;
313 gate = &rg->gate;
314 dest_world = world;
315 }
316 }
317
318 for( int i=0; i<world->nonlocalgate_count; i++ )
319 {
320 struct nonlocal_gate *nlg = &world->nonlocal_gates[i];
321
322 if( !nlg->working )
323 {
324 vg_line_pt3( nlg->gate.co[0], 0.25f, VG__RED );
325 continue;
326 }
327 else
328 vg_line_pt3( nlg->gate.co[0], 0.25f, VG__GREEN );
329
330 float dist = v3_dist2( nlg->gate.co[0], cam->transform[3] );
331
332 if( dist < closest )
333 {
334 closest = dist;
335 gate = &nlg->gate;
336 dest_world = &world_global.worlds[ nlg->target_map_index ];
337 }
338 }
339
340 if( gate )
341 render_gate( dest_world, gate, cam );
342 }
343
344 VG_STATIC void render_world( world_instance *world, camera *cam )
345 {
346 render_sky( cam );
347
348 render_world_routes( world, cam );
349 render_world_standard( world, cam );
350 render_world_vb( world, cam );
351 render_world_alphatest( world, cam );
352 render_terrain( world, cam );
353
354 /* Render SFD's */
355 int closest = 0;
356 float min_dist = INFINITY;
357
358 if( !world->route_count )
359 return;
360
361 for( int i=0; i<world->route_count; i++ )
362 {
363 float dist = v3_dist2(world->routes[i].scoreboard_transform[3], cam->pos);
364
365 if( dist < min_dist )
366 {
367 min_dist = dist;
368 closest = i;
369 }
370 }
371
372 sfd_render( cam, world->routes[closest].scoreboard_transform );
373 }
374
375 VG_STATIC void render_world_depth( world_instance *world, camera *cam )
376 {
377 m4x3f identity_matrix;
378 m4x3_identity( identity_matrix );
379
380 shader_scene_depth_use();
381 shader_scene_depth_uCamera( cam->transform[3] );
382 shader_scene_depth_uPv( cam->mtx.pv );
383 shader_scene_depth_uPvmPrev( cam->mtx_prev.pv );
384 shader_scene_depth_uMdl( identity_matrix );
385 world_link_lighting_ub( world, _shader_scene_depth.id, 2 );
386
387 mesh_bind( &world->mesh_geo );
388 mesh_draw( &world->mesh_geo );
389 }
390
391 VG_STATIC void render_world_position( world_instance *world, camera *cam )
392 {
393 m4x3f identity_matrix;
394 m4x3_identity( identity_matrix );
395
396 shader_scene_position_use();
397 shader_scene_position_uCamera( cam->transform[3] );
398 shader_scene_position_uPv( cam->mtx.pv );
399 shader_scene_position_uPvmPrev( cam->mtx_prev.pv );
400 shader_scene_position_uMdl( identity_matrix );
401 world_link_lighting_ub( world, _shader_scene_position.id, 2 );
402
403 mesh_bind( &world->mesh_geo );
404 mesh_draw( &world->mesh_geo );
405 }
406
407 #endif /* WORLD_RENDER_H */