2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
12 VG_STATIC vg_tex2d tex_terrain_noise
= { .path
= "textures/garbage.qoi",
13 .flags
= VG_TEXTURE_NEAREST
};
15 VG_STATIC
void world_render_init(void)
17 vg_info( "Loading default world textures\n" );
19 vg_acquire_thread_sync();
21 vg_tex2d_init( (vg_tex2d
*[]){ &tex_terrain_noise
}, 1 );
24 vg_info( "Allocate uniform buffers\n" );
25 for( int i
=0; i
<4; i
++ )
27 world_instance
*world
= &world_global
.worlds
[i
];
28 world
->ubo_bind_point
= i
;
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
);
35 glBindBufferBase( GL_UNIFORM_BUFFER
, i
, world
->ubo_lighting
);
39 vg_info( "Allocate frame buffers\n" );
40 for( int i
=0; i
<4; i
++ )
42 world_instance
*world
= &world_global
.worlds
[i
];
43 struct framebuffer
*fb
= &world
->heightmap
;
45 fb
->display_name
= NULL
;
49 fb
->resolution_div
= 0;
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 */
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
;
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
;
66 render_fb_allocate( fb
);
69 vg_release_thread_sync();
72 VG_STATIC
void world_link_lighting_ub( world_instance
*world
, GLuint shader
)
74 GLuint idx
= glGetUniformBlockIndex( shader
, "ub_world_lighting" );
75 glUniformBlockBinding( shader
, idx
, world
->ubo_bind_point
);
78 VG_STATIC
void world_bind_position_texture( world_instance
*world
,
79 GLuint shader
, GLuint location
,
82 render_fb_bind_texture( &world
->heightmap
, 0, slot
);
83 glUniform1i( location
, slot
);
86 VG_STATIC
void world_bind_light_array( world_instance
*world
,
87 GLuint shader
, GLuint location
,
90 glActiveTexture( GL_TEXTURE0
+ slot
);
91 glBindTexture( GL_TEXTURE_BUFFER
, world
->tex_light_entities
);
92 glUniform1i( location
, slot
);
95 VG_STATIC
void render_world_depth( world_instance
*world
, camera
*cam
);
101 VG_STATIC
void bind_terrain_noise(void)
103 vg_tex2d_bind( &tex_terrain_noise
, 0 );
106 typedef void (*func_bind_point
)( world_instance
*world
,
107 struct world_material
*mat
);
109 VG_STATIC
void world_render_if( world_instance
*world
,
110 enum mdl_shader shader
,
111 enum geo_type geo_type
,
112 func_bind_point bind_point
)
115 for( int i
=0; i
<world
->material_count
; i
++ )
117 struct world_material
*mat
= &world
->materials
[i
];
119 if( mat
->info
.shader
== shader
)
123 if( geo_type
== k_geo_type_solid
)
126 sm
= &mat
->sm_no_collide
;
128 if( !sm
->indice_count
)
131 bind_point( world
, mat
);
132 mdl_draw_submesh( sm
);
138 void world_render_both_stages( world_instance
*world
,
139 enum mdl_shader shader
,
140 func_bind_point bind_point
)
142 mesh_bind( &world
->mesh_geo
);
143 world_render_if( world
, shader
, k_geo_type_solid
, bind_point
);
145 glDisable( GL_CULL_FACE
);
146 mesh_bind( &world
->mesh_no_collide
);
147 world_render_if( world
, shader
, k_geo_type_nonsolid
, bind_point
);
148 glEnable( GL_CULL_FACE
);
151 VG_STATIC
void bindpoint_diffuse_texture1( world_instance
*world
,
152 struct world_material
*mat
)
154 glActiveTexture( GL_TEXTURE1
);
155 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
158 VG_STATIC
void render_world_vb( world_instance
*world
, camera
*cam
)
160 m4x3f identity_matrix
;
161 m4x3_identity( identity_matrix
);
163 shader_scene_vertex_blend_use();
164 shader_scene_vertex_blend_uTexGarbage(0);
165 shader_scene_vertex_blend_uTexGradients(1);
166 world_link_lighting_ub( world
, _shader_scene_vertex_blend
.id
);
167 world_bind_position_texture( world
, _shader_scene_vertex_blend
.id
,
168 _uniform_scene_vertex_blend_g_world_depth
, 2 );
169 world_bind_light_array( world
, _shader_scene_vertex_blend
.id
,
170 _uniform_scene_vertex_blend_uLightsArray
, 3 );
172 vg_tex2d_bind( &tex_terrain_noise
, 0 );
174 shader_scene_vertex_blend_uPv( cam
->mtx
.pv
);
175 shader_scene_vertex_blend_uPvmPrev( cam
->mtx_prev
.pv
);
176 shader_scene_vertex_blend_uMdl( identity_matrix
);
177 shader_scene_vertex_blend_uCamera( cam
->transform
[3] );
178 shader_scene_vertex_blend_uBoard0( TEMP_BOARD_0
);
179 shader_scene_vertex_blend_uBoard1( TEMP_BOARD_1
);
181 world_render_both_stages( world
, k_shader_standard_vertex_blend
,
182 bindpoint_diffuse_texture1
);
185 VG_STATIC
void render_world_standard( world_instance
*world
, camera
*cam
)
187 m4x3f identity_matrix
;
188 m4x3_identity( identity_matrix
);
190 shader_scene_standard_use();
191 shader_scene_standard_uTexGarbage(0);
192 shader_scene_standard_uTexMain(1);
193 shader_scene_standard_uPv( cam
->mtx
.pv
);
194 shader_scene_standard_uPvmPrev( cam
->mtx_prev
.pv
);
196 world_link_lighting_ub( world
, _shader_scene_standard
.id
);
197 world_bind_position_texture( world
, _shader_scene_standard
.id
,
198 _uniform_scene_standard_g_world_depth
, 2 );
199 world_bind_light_array( world
, _shader_scene_standard
.id
,
200 _uniform_scene_standard_uLightsArray
, 3 );
202 bind_terrain_noise();
204 shader_scene_standard_uMdl( identity_matrix
);
205 shader_scene_standard_uCamera( cam
->transform
[3] );
206 shader_scene_standard_uBoard0( TEMP_BOARD_0
);
207 shader_scene_standard_uBoard1( TEMP_BOARD_1
);
209 world_render_both_stages( world
, k_shader_standard
,
210 bindpoint_diffuse_texture1
);
213 VG_STATIC
void render_world_alphatest( world_instance
*world
, camera
*cam
)
215 m4x3f identity_matrix
;
216 m4x3_identity( identity_matrix
);
218 shader_scene_standard_alphatest_use();
219 shader_scene_standard_alphatest_uTexGarbage(0);
220 shader_scene_standard_alphatest_uTexMain(1);
221 shader_scene_standard_alphatest_uPv( cam
->mtx
.pv
);
222 shader_scene_standard_alphatest_uPvmPrev( cam
->mtx_prev
.pv
);
224 world_link_lighting_ub( world
, _shader_scene_standard_alphatest
.id
);
225 world_bind_position_texture( world
, _shader_scene_standard_alphatest
.id
,
226 _uniform_scene_standard_alphatest_g_world_depth
, 2 );
227 world_bind_light_array( world
, _shader_scene_standard_alphatest
.id
,
228 _uniform_scene_standard_alphatest_uLightsArray
, 3 );
231 bind_terrain_noise();
233 shader_scene_standard_alphatest_uMdl( identity_matrix
);
234 shader_scene_standard_alphatest_uCamera( cam
->transform
[3] );
235 shader_scene_standard_alphatest_uBoard0( TEMP_BOARD_0
);
236 shader_scene_standard_alphatest_uBoard1( TEMP_BOARD_1
);
238 glDisable(GL_CULL_FACE
);
240 world_render_both_stages( world
, k_shader_standard_cutout
,
241 bindpoint_diffuse_texture1
);
243 glEnable(GL_CULL_FACE
);
246 VG_STATIC
void bindpoint_terrain( world_instance
*world
,
247 struct world_material
*mat
)
249 glActiveTexture( GL_TEXTURE1
);
250 glBindTexture( GL_TEXTURE_2D
, world
->textures
[ mat
->info
.tex_diffuse
] );
252 shader_scene_terrain_uSandColour( mat
->info
.colour
);
253 shader_scene_terrain_uBlendOffset( mat
->info
.colour1
);
256 VG_STATIC
void render_terrain( world_instance
*world
, camera
*cam
)
258 m4x3f identity_matrix
;
259 m4x3_identity( identity_matrix
);
261 shader_scene_terrain_use();
262 shader_scene_terrain_uTexGarbage(0);
263 shader_scene_terrain_uTexGradients(1);
265 world_link_lighting_ub( world
, _shader_scene_terrain
.id
);
266 world_bind_position_texture( world
, _shader_scene_terrain
.id
,
267 _uniform_scene_terrain_g_world_depth
, 2 );
268 world_bind_light_array( world
, _shader_scene_terrain
.id
,
269 _uniform_scene_terrain_uLightsArray
, 3 );
271 vg_tex2d_bind( &tex_terrain_noise
, 0 );
273 shader_scene_terrain_uPv( cam
->mtx
.pv
);
274 shader_scene_terrain_uPvmPrev( cam
->mtx_prev
.pv
);
276 shader_scene_terrain_uMdl( identity_matrix
);
277 shader_scene_terrain_uCamera( cam
->transform
[3] );
278 shader_scene_terrain_uBoard0( TEMP_BOARD_0
);
279 shader_scene_terrain_uBoard1( TEMP_BOARD_1
);
281 world_render_both_stages( world
, k_shader_terrain_blend
, bindpoint_terrain
);
284 VG_STATIC
void render_sky( world_instance
*world
, camera
*cam
)
287 * Modify matrix to remove clipping and view translation
294 m4x4_copy( cam
->mtx
.v
, v
);
295 m4x4_copy( cam
->mtx_prev
.v
, v_prev
);
297 v3_zero( v_prev
[3] );
299 m4x4_copy( cam
->mtx
.p
, pv
);
300 m4x4_copy( cam
->mtx_prev
.p
, pv_prev
);
301 m4x4_reset_clipping( pv
, cam
->farz
, cam
->nearz
);
302 m4x4_reset_clipping( pv_prev
, cam
->farz
, cam
->nearz
);
304 m4x4_mul( pv
, v
, pv
);
305 m4x4_mul( pv_prev
, v_prev
, pv_prev
);
307 m4x3f identity_matrix
;
308 m4x3_identity( identity_matrix
);
313 shader_model_sky_use();
314 shader_model_sky_uMdl( identity_matrix
);
315 shader_model_sky_uPv( pv
);
316 shader_model_sky_uPvmPrev( pv_prev
);
317 shader_model_sky_uTexGarbage(0);
318 world_link_lighting_ub( world
, _shader_model_sky
.id
);
320 vg_tex2d_bind( &tex_terrain_noise
, 0 );
322 glDepthMask( GL_FALSE
);
323 glDisable( GL_DEPTH_TEST
);
325 mesh_bind( &world_global
.skydome
);
326 mdl_draw_submesh( &world_global
.dome_upper
);
328 glEnable( GL_DEPTH_TEST
);
329 glDepthMask( GL_TRUE
);
332 VG_STATIC
void render_world_gates( world_instance
*world
, camera
*cam
)
334 float closest
= INFINITY
;
336 struct teleport_gate
*gate
= NULL
;
337 world_instance
*dest_world
= world
;
339 for( int i
=0; i
<world
->gate_count
; i
++ )
341 struct route_gate
*rg
= &world
->gates
[i
];
342 float dist
= v3_dist2( rg
->gate
.co
[0], cam
->transform
[3] );
344 vg_line_pt3( rg
->gate
.co
[0], 0.25f
, VG__BLUE
);
354 for( int i
=0; i
<world
->nonlocalgate_count
; i
++ )
356 struct nonlocal_gate
*nlg
= &world
->nonlocal_gates
[i
];
360 vg_line_pt3( nlg
->gate
.co
[0], 0.25f
, VG__RED
);
364 vg_line_pt3( nlg
->gate
.co
[0], 0.25f
, VG__GREEN
);
366 float dist
= v3_dist2( nlg
->gate
.co
[0], cam
->transform
[3] );
372 dest_world
= &world_global
.worlds
[ nlg
->target_map_index
];
377 render_gate( dest_world
, gate
, cam
);
380 VG_STATIC
void render_world( world_instance
*world
, camera
*cam
)
382 render_sky( world
, cam
);
384 render_world_routes( world
, cam
);
385 render_world_standard( world
, cam
);
386 render_world_vb( world
, cam
);
387 render_world_alphatest( world
, cam
);
388 render_terrain( world
, cam
);
392 float min_dist
= INFINITY
;
394 if( !world
->route_count
)
397 for( int i
=0; i
<world
->route_count
; i
++ )
399 float dist
= v3_dist2(world
->routes
[i
].scoreboard_transform
[3], cam
->pos
);
401 if( dist
< min_dist
)
408 sfd_render( cam
, world
->routes
[closest
].scoreboard_transform
);
411 VG_STATIC
void render_world_depth( world_instance
*world
, camera
*cam
)
413 m4x3f identity_matrix
;
414 m4x3_identity( identity_matrix
);
416 shader_scene_depth_use();
417 shader_scene_depth_uCamera( cam
->transform
[3] );
418 shader_scene_depth_uPv( cam
->mtx
.pv
);
419 shader_scene_depth_uPvmPrev( cam
->mtx_prev
.pv
);
420 shader_scene_depth_uMdl( identity_matrix
);
421 world_link_lighting_ub( world
, _shader_scene_depth
.id
);
423 mesh_bind( &world
->mesh_geo
);
424 mesh_draw( &world
->mesh_geo
);
427 VG_STATIC
void render_world_position( world_instance
*world
, camera
*cam
)
429 m4x3f identity_matrix
;
430 m4x3_identity( identity_matrix
);
432 shader_scene_position_use();
433 shader_scene_position_uCamera( cam
->transform
[3] );
434 shader_scene_position_uPv( cam
->mtx
.pv
);
435 shader_scene_position_uPvmPrev( cam
->mtx_prev
.pv
);
436 shader_scene_position_uMdl( identity_matrix
);
437 world_link_lighting_ub( world
, _shader_scene_position
.id
);
439 mesh_bind( &world
->mesh_geo
);
440 mesh_draw( &world
->mesh_geo
);
443 #endif /* WORLD_RENDER_H */