Shadow Map (unidirectional)

 

 

  Have long wanted to see shadow mapping, has been dragged now, today finally saw a shadow mapping in one direction, then take the scene looked at the effect (take each scene feels a bit cumbersome).

  Shadow mapping process in general:

// 1. The preferred depth map rendering 
the glViewport ( 0 , 0 , SHADOW_WIDTH, SHADOW_HEIGHT); 
glBindFramebuffer (GL_FRAMEBUFFER, depthMapFBO); 
    the glClear (GL_DEPTH_BUFFER_BIT); 
    ConfigureShaderAndMatrices (); 
    RenderScene (); 
glBindFramebuffer (GL_FRAMEBUFFER, 0 );
 // 2. as usual render the scene, but this time using depth maps 
glViewport ( 0 , 0 , SCR_WIDTH, SCR_HEIGHT); 
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
ConfigureShaderAndMatrices (); 
glBindTexture (GL_TEXTURE_2D, depthMap); 
RenderScene ();

  Ideas:

  1, rendering depth map

  The position of the light source is again rendered camera position, of course, use amazing GBuffer (deferred shading) off-screen rendering, the depth of such a texture can be stored. After the appearance of depth can be displayed by a plane map texture, convenient test the correctness of the depth map.

        

 

  2, rendering shadows

  This is a normal step and rendering the same, but added depth to judge. For the current fragment to convert its location to the coordinate in the view of the light source, so that the coordinates of the depth map corresponding to thereof can be sampled from the depth map, the depth map indicating the nearest fragments in corresponding fragment ray depth value , the fragment of the current depth value with the depth value from the depth texture sampling, the sample value is greater than the depth of said fragments in a shadow.

  

  Looks a bit wrong, depth texture using a sampling cycle setting, some of the shaded areas should not have appeared in the shadows. Change the property of the depth of the texture.

GLuint WKS::Texture::GenDepthTexture(GLuint width, GLuint height) {
    glGenTextures(1, &this->textureID);
    glBindTexture(GL_TEXTURE_2D, this->textureID);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
    GLfloat borderColor[] = { 1.0, 1.0, 1.0, 1.0 };
    glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
    glBindTexture(GL_TEXTURE_2D, 0);
    return this->textureID;
}

  

  好多了。

  当然了,这样的阴影范围有限,要获取完整的阴影需要使用点阴影的方式,对六个方向获取深度纹理。(见下节)

  

Guess you like

Origin www.cnblogs.com/chen9510/p/11706219.html