medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 7da76bdccc469437ad702ab20b3fdfc8a64ca8e9
parent c4b7f2676446e8c0434e031c9c6a8ab9557866e1
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Mon Oct 31 22:28:21 +0200

Render the depth buffer

Diffstat:
shadow_map.cc | 87++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 77 insertions(+), 10 deletions(-)
diff --git a/shadow_map.cc b/shadow_map.cc @@ -11,11 +11,17 @@ static ImmediateTriangle triangles[ 512000 ]; static ImmediateContext imm; +static GLuint screen_vao, screen_vbo; + static GLuint prog; static GLint at_position; static GLint at_colour; static GLint un_vp; +static GLuint depth_prog; +static GLint depth_at_position; +static GLint depth_un_tex; + static GLuint fbo; static GLuint tex_depth; @@ -43,6 +49,32 @@ static const GLchar * frag_src = GLSL( } ); +static const GLchar * const depth_vert_src = GLSL( + in vec2 position; + + out vec2 smooth_position; + + void main() { + gl_Position = vec4( position, 0.0, 1.0 ); + smooth_position = position; + } +); + +static const GLchar * depth_frag_src = GLSL( + in vec2 smooth_position; + + out vec4 screen_colour; + + uniform sampler2D tex; + + void main() { + vec2 texcoord = ( smooth_position + vec2( 1.0, 1.0 ) ) / 2.0; + float Depth = texture( tex, texcoord ).x; + Depth = 1.0 - (1.0 - Depth) * 25.0; + screen_colour = vec4( Depth ); + } +); + static void draw_scene( GLint at_position, GLint at_colour ) { immediate_init( &imm, triangles, array_count( triangles ) ); @@ -94,7 +126,7 @@ static void update_camera( } static void camera_to_vp( glm::vec3 position, glm::vec3 angles, glm::mat4 * out_vp ) { - const glm::mat4 P( glm::perspective( glm::radians( 120.0f ), ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, 100.0f ) ); + const glm::mat4 P( glm::perspective( glm::radians( 120.0f ), ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, 25.0f ) ); *out_vp = glm::translate( glm::rotate( @@ -110,28 +142,53 @@ static void camera_to_vp( glm::vec3 position, glm::vec3 angles, glm::mat4 * out_ ); } +const u32 SHADOW_WIDTH = 1024; +const u32 SHADOW_HEIGHT = 1024; + extern "C" GAME_INIT( game_init ) { prog = compile_shader( vert_src, frag_src, "screen_colour" ); at_position = glGetAttribLocation( prog, "position" ); at_colour = glGetAttribLocation( prog, "colour" ); un_vp = glGetUniformLocation( prog, "VP" ); - glGenFramebuffers( 1, &fbo ); - glBindFramebuffer( GL_DRAW_FRAMEBUFFER, fbo ); + depth_prog = compile_shader( depth_vert_src, depth_frag_src, "screen_colour" ); + depth_at_position = glGetAttribLocation( depth_prog, "position" ); + depth_un_tex = glGetUniformLocation( depth_prog, "tex" ); + glGenTextures( 1, &tex_depth ); glBindTexture( GL_TEXTURE_2D, tex_depth ); + glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL ); + glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); + glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, WIDTH, HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL ); - glFramebufferTexture2D( GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex_depth, 0 ); + glGenFramebuffers( 1, &fbo ); + glBindFramebuffer( GL_FRAMEBUFFER, fbo ); + glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex_depth, 0 ); + glDrawBuffer( GL_NONE ); + glReadBuffer( GL_NONE ); - glBindFramebuffer( GL_DRAW_FRAMEBUFFER, 0 ); + assert( glCheckFramebufferStatus( GL_FRAMEBUFFER ) == GL_FRAMEBUFFER_COMPLETE ); + + glBindFramebuffer( GL_FRAMEBUFFER, 0 ); game->pos = glm::vec3( -10, -10, 5 ); game->angles = glm::radians( ( glm::vec3( -90, 45, 0 ) ) ); + + float verts[] = { -1, -1, -1, 1, 1, -1, 1, 1 }; + glGenVertexArrays( 1, &screen_vao ); + glBindVertexArray( screen_vao ); + glGenBuffers( 1, &screen_vbo ); + glBindBuffer( GL_ARRAY_BUFFER, screen_vbo ); + glBufferData( GL_ARRAY_BUFFER, sizeof( verts ), verts, GL_STATIC_DRAW ); + glEnableVertexAttribArray( depth_at_position ); + glVertexAttribPointer( depth_at_position, 2, GL_FLOAT, GL_FALSE, sizeof( float ) * 2, 0 ); + glBindVertexArray( 0 ); + glBindBuffer( GL_ARRAY_BUFFER, 0 ); } extern "C" GAME_FRAME( game_frame ) { - glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); update_camera( input, dt, game->pos, game->angles, &game->pos, &game->angles ); glm::mat4 vp; @@ -140,13 +197,23 @@ extern "C" GAME_FRAME( game_frame ) { glUseProgram( prog ); glUniformMatrix4fv( un_vp, 1, GL_FALSE, glm::value_ptr( vp ) ); + glViewport( 0, 0, SHADOW_WIDTH, SHADOW_HEIGHT ); glBindFramebuffer( GL_DRAW_FRAMEBUFFER, fbo ); + glClear( GL_DEPTH_BUFFER_BIT ); draw_scene( at_position, at_colour ); glBindFramebuffer( GL_DRAW_FRAMEBUFFER, 0 ); + glViewport( 0, 0, WIDTH, HEIGHT ); + glUseProgram( 0 ); + + glUseProgram( depth_prog ); + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + glActiveTexture( GL_TEXTURE0 ); + glBindTexture( GL_TEXTURE_2D, tex_depth ); + glUniform1i( depth_un_tex, 0 ); - glBindFramebuffer( GL_READ_FRAMEBUFFER, fbo ); - glBlitFramebuffer( 0, 0, WIDTH, HEIGHT, 0, 0, WIDTH, HEIGHT, GL_COLOR_BUFFER_BIT, GL_LINEAR ); - glBindFramebuffer( GL_READ_FRAMEBUFFER, 0 ); + glBindVertexArray( screen_vao ); + glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 ); + glBindVertexArray( 0 ); glUseProgram( 0 ); }