medfall

A super great game engine
Log | Files | Refs

commit 95f11e8fe66a4ae6927efd394793067723f43abf
parent f0daff30dd4d58974265c9f1b89201fcba894107
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat, 11 Nov 2017 21:14:25 +0200

Draw skybox last to reduce overdraw

Diffstat:
clipmap.cc | 6++++--
linear_algebra.h | 27+++++++++++++++++++++++++++
shaders/skybox.glsl | 2+-
skybox.cc | 1-
4 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/clipmap.cc b/clipmap.cc @@ -436,6 +436,7 @@ GAME_FRAME( game_frame ) { v3 eye_pos = game->pos + v3( 0, 0, EYE_HEIGHT ); m4 P = m4_perspective( VERTICAL_FOV, get_aspect_ratio(), NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH ); + m4 Pinf = m4_perspective_inf( VERTICAL_FOV, get_aspect_ratio(), NEAR_PLANE_DEPTH ); m4 V = m4_view( forward, right, up, eye_pos ); m4 Vsky = m4_view( forward, right, up, v3( 0 ) ); v3 sun_dir = v3( -cosf( game->sun_angle ), 0, sinf( game->sun_angle ) ); @@ -443,9 +444,8 @@ GAME_FRAME( game_frame ) { renderer_begin_frame(); renderer_begin_pass( RENDERER_CLEAR_COLOUR_DO, RENDERER_CLEAR_DEPTH_DO ); - skybox_render( &game->skybox, Vsky, P, game->sun_angle, sun_dir ); - UniformBinding view_uniforms = renderer_uniforms( V, P, game->pos ); + UniformBinding inf_view_uniforms = renderer_uniforms( V, Pinf, game->pos ); UniformBinding sun_uniforms = renderer_uniforms( sun_dir, game->sun_angle ); // draw terrain @@ -585,6 +585,8 @@ GAME_FRAME( game_frame ) { immediate_render( explosion_render_state ); } + skybox_render( &game->skybox, Vsky, Pinf, game->sun_angle, sun_dir ); + // networking { char buf[ 1400 ]; diff --git a/linear_algebra.h b/linear_algebra.h @@ -783,6 +783,33 @@ forceinline m4 m4_perspective( float vertical_fov_degrees, float aspect_ratio, f ); } +forceinline m4 m4_perspective_inf( float vertical_fov_degrees, float aspect_ratio, float near_plane ) { + float tan_half_vertical_fov = tanf( deg_to_rad( vertical_fov_degrees ) / 2.0f ); + float epsilon = 2.4e-6; + + return m4( + 1.0f / ( tan_half_vertical_fov * aspect_ratio ), + 0.0f, + 0.0f, + 0.0f, + + 0.0f, + 1.0f / tan_half_vertical_fov, + 0.0f, + 0.0f, + + 0.0f, + 0.0f, + epsilon - 1.0f, + ( epsilon - 2.0f ) * near_plane, + + 0.0f, + 0.0f, + -1.0f, + 0.0f + ); +} + forceinline m4 operator*( const m4 & lhs, const m4 & rhs ) { return m4( dot( lhs.row0(), rhs.col0 ), diff --git a/shaders/skybox.glsl b/shaders/skybox.glsl @@ -9,7 +9,7 @@ layout( std140 ) uniform view { }; void main() { - gl_Position = P * V * vec4( position, 1.0 ); + gl_Position = P * V * vec4( position, 0.0 ); pos = position; } diff --git a/skybox.cc b/skybox.cc @@ -134,7 +134,6 @@ void skybox_render( const Skybox * skybox, const m4 & Vsky, const m4 & P, float RenderState render_state; render_state.shader = get_shader( SHADER_SKYBOX ); - render_state.disable_depth_writes = true; render_state.uniforms[ UNIFORMS_VIEW ] = renderer_uniforms( Vsky, P ); render_state.uniforms[ UNIFORMS_SKY ] = renderer_uniforms( hosek.A, hosek.B, hosek.C, hosek.D, hosek.E, hosek.F, hosek.G, hosek.H, hosek.I, hosek.Z, sun_dir ); render_state.textures[ 0 ] = renderer_blue_noise();