OpenGL Tutorial (Shadow) mapping (2)

Source: Internet
Author: User

Original address: http://ogldev.atspace.co.uk/www/tutorial24/tutorial24.html

Background

In the previous tutorial we learned the basic principle behind the shadow mapping technique and saw how to render the Dept h into a texture and later display it in the screen by sampling from the depth buffer. In this tutorial we'll see how to use this capability and display the shadow itself.

We know that shadow mapping are a two-pass technique and that in the first pass the scene are rendered from the point of Vie W of the Light. Let's review what happens to the Z component of the position vectors during that first pass:

  1. The position of the vertices that is fed into the vertex shader is generally specified in local space.
  2. The vertex shader transforms the position from local space to clip space and forwards it down the pipeline (see Tutorial 1 2 If you need a refresher about clip space).
  3. The rasterizer performs perspective divide (a division of the position vector by its W component). This takes the position vector from clip space to NDC space. In NDC space everything which ends up on the screen have a X, Y and Z in the range [ -1,1]. Things outside these ranges is clipped away.
  4. The rasterizer maps the X and Y of the position vector to the dimensions of the framebuffer (e.g. 800x600, 1024x768, etc). The results is the screen space coordinates of the position vector.
  5. The rasterizer takes the screen space coordinates of the three triangle vertices and interpolates them to create the Uniqu e coordinates for each pixel that the triangle covers. The Z value (still in the [ -1,1] range) are also interpolated so every pixel have its own depth.
  6. Since We disabled color writes in the first pass of the fragment shader is disabled. The depth test, however, still executes. To compare the Z value of the "the" pixel with the one in the "the" screen space coordinates of the pixel is used To fetch the depth from the buffer. If the depth of the new pixel is smaller than the stored one, the buffer is updated (and if color writes were enabled the C Olor Buffer would has also been updated).

In the process above we saw how the depth value from the light point of view are calculated and stored. The second pass we render from the camera point of view so naturally we get a different depth. But we need both depth values-one to get the triangles ordered correctly on the screens and the other to check what's in Side the shadow and what are not. The trick in shadow mapping are to maintain, position vectors and both WVP matrices while traveling through the 3D Pipeli Ne. One WVP matrix is calculated from the light point in view and the other from the camera point of view. The vertex shader gets one position vector in local space as usual, but it outputs the vectors:

    1. The builtin gl_position which is the result of the transforming, the Position by the camera WVP matrix.
    2. A "plain" vector which is the result of the transforming, the position by the light WVP matrix.

The first vector would go through above process (and NDC space...etc) and these'll be used for the regular Rasteriz ation. The second vector would simply be interpolated by the rasterizer across the triangle face and each fragment shader Invocati On'll be provided with its own value. So-for-each physical pixel we also has a clip space coordinate of the same point in the original triangle when lookin G at it from the light point of view. It is very likely, the physical pixels from the the the the the the the the-the GLE is the same. All this remains is to somehow use, the clip space coordinate in order to fetch the depth value from the shadow map. After this we can compare the depth to the one in the clip space coordinate and if the stored depth are smaller then it mea NS The Pixel is in shadow (because another pixel had the same clip space coordinate but with a smaller depth).

So what can we fetch the depth in the fragment shader using the clip space coordinate that is calculated by trasforming th E position by the light WVP matrix? When we start out we is basically in step 2 above.

  1. Since The fragment shader receives the clip space coordinate as a standard vertex attribute the rasterizer does not perfor M perspective divide on it (only what goes through Gl_position). It's something that's very easy-to-do manually in the shader. We divide the coordinate by it W component and get a coordinate in NDC space.
  2. We know this in NDC, the X and Y range from-1 to 1. In step 4 above the rasterizer maps, the NDC coordinates to screens space and uses them to store the depth. We is going to sample the depth, and for so we need a texture coordinate in the range [0,1]. If we linearly map the range [ -1,1] to [0,1] we'll get a texture coordinate that'll map to the same location in the SH Adow map. Example:the X in NDC are zero and the width of the texture is 800. The Zero in NDC needs to being mapped to 0.5 in the texture coordinate space (because it is half, between-1 and 1). The texture coordinate 0.5 is mapped to + in the texture which are the same location that's calculated by the rasterizer When it performs the screen space transform.
  3. Transforming X and Y from NDC space to texture space are done as follows:
    • u = 0.5 * X + 0.5
    • v = 0.5 * Y + 0.5
Source Walkthru

(lighting_technique.h:80)

class LightingTechnique : public Technique {
    public:
    ...
        void SetLightWVP(const Matrix4f& LightWVP);
        void SetShadowMapTextureUnit(unsigned int TextureUnit);
    ...
    private:
        GLuint m_LightWVPLocation;
        GLuint m_shadowMapLocation;
...

The lighting technique needs a couple of new attributes. A WVP matrix is calculated from the light point of view and a texture unit for the shadow map. We'll continue using texture unit 0 for the regular texture that's mapped on the object and would dedicate texture unit 1 for the shadow map.

(Lighting.vs)

#version the VEC3
Layout (location = 0) in Position;
Layout (location = 1) in vec2 Texcoord;
Layout (location = 2) in VEC3 Normal;
Uniform mat4 GWVP;
Uniform mat4 GLIGHTWVP;
Uniform mat4 Gworld;
out VEC4 lightspacepos;
out VEC2 TexCoord0;
out VEC3 Normal0;
out VEC3 WorldPos0;
Void Main ()
{
    gl_position = GWVP * VEC4 (Position, 1.0);
Lightspacepos = GLIGHTWVP * VEC4 (Position, 1.0);
    TexCoord0 = Texcoord;
    Normal0 = (Gworld * VEC4 (Normal, 0.0)). xyz;
    WorldPos0 = (Gworld * VEC4 (Position, 1.0)). xyz;
}

This was the updated vertex shader of the Lightingtechnique class with the additions marked in bold text. We have an additional WVP matrix uniform variable and a 4-vector as output which contains the clip space coordinates CALCU Lated by transforming, the position by the light WVP matrix. As can see, in the vertex shader of the first pass the variable GWVP contained the same matrix as GLIGHTWVP here and G L_position there got the same value as Lightspacepos here. But since Lightspacepos are just a standard vector it does does get an automatic perspective division as gl_position. We'll do this manually in the fragment shader below.

(lighting.fs:58)

float Calcshadowfactor (vec4 lightspacepos)
{
    vec3 projcoords = lightspacepos.xyz/ LIGHTSPACEPOS.W;
    vec2 uvcoords;
    uvcoords.x = 0.5 * projcoords.x + 0.5;
    uvcoords.y = 0.5 * projcoords.y + 0.5;
    Float z = 0.5 * projcoords.z + 0.5;
    Float Depth = Texture (Gshadowmap, uvcoords). x;
    if (Depth < (z + 0.00001))
        return 0.5;
    Else
        return 1.0;
}

This function was used in the fragment shader to calculate the shadow factor of a pixel. The shadow factor is a new factor with the light equation. We simply multiply the result of our current light equation by which factor and this causes some attenuation of the light I n pixels that is determined to being shadowed. The function takes the interpolated Lightspacepos vector that is passed from the vertex shader. The first step is to perform perspective division-we divide, the XYZ components by the W component. This transfers, the vector to NDC space. Next we prepare a 2D coordinate vector to being used as the texture coordinate and initialize it by transforming the Lightspa Cepos vector from NDC to texture space according to the equation of the background section. The texture coordinates is used to fetch the depth from the shadow map. This was the depth of the closest location from all the points in the scene that was projected to this pixel. We Compare that depth to the depth of the current PixeL and if it is smaller return a shadow factor of 0.5, else the Shadow Factor is 1.0 (no shadow). The Z from the NDC space also goes through transformation from the ( -1,1) range to (0,1) range because we had to is in th E same space when we compare. Notice that we add a small epsilon value to the current pixel ' s depth. This was to the avoid precision errors that was inherent when dealing with floating point values.

OpenGL Tutorial (Shadow) mapping (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.