Location and resolution of the shadow map

I am currently learning C ++ and OpenGL and I was wondering if anyone could get through what is happening with the code below. It is currently calculating the location and resolution of the shadow map in a 3D environment.

Currently the code is working, just looking to understand everything.

//Vertex Shader Essentials.
Position = ProjectionMatrix * ViewMatrix * WorldMatrix * vec4 (VertexPosition, 1);
Normal = (ViewMatrix * WorldMatrix * vec4 (VertexNormal, 0)).xyz;
EyeSpaceLightPosition = ViewMatrix * LightPosition;
EyeSpacePosition = ViewMatrix * WorldMatrix * vec4 (VertexPosition, 1);
STCoords = VertexST;

//What is this block of code currently doing?
ShadowCoord = ProjectionMatrix * ShadowMatrix * WorldMatrix * vec4 (VertexPosition, 1);
ShadowCoord = ShadowCoord / ShadowCoord.w;  
ShadowCoord = (ShadowCoord + vec4 (1.0, 1.0, 1.0, 1.0)) * vec4 (1.0/2.0, 1.0/2.0, 1.0/2.0, 1.0);

//Alters the Shadow Map Resolution. 
//    Please Note - c is a slider that I control in the program execution.
float rounding = (c + 2.1) * 100.0;
ShadowCoord.x = (floor (ShadowCoord.x * rounding)) / rounding;
ShadowCoord.y = (floor (ShadowCoord.y * rounding)) / rounding;
ShadowCoord.z = (floor (ShadowCoord.z * rounding)) / rounding;


gl_Position = Position;

      

+3


source to share


1 answer


ShadowCoord = ProjectionMatrix * ShadowMatrix * WorldMatrix * vec4 (VertexPosition, 1);

      

This calculates the position of this vertex in light's eye space. What you recompile is that the string Position = ProjectionMatrix * ViewMatrix * WorldMatrix * vec4 (VertexPosition, 1);

should have been returned when rendering in the shadow buffer.

ShadowCoord = ShadowCoord / ShadowCoord.w;  

      

This refers to perspective projection, which defines where your shadow coordinate should fall onto the plane of the light images.



Think of it this way: From an easy point of view, the coordinate at point (1, 1, 1) should appear in the same place as at (2, 2, 2). For both of these, you must choose the same 2d location in the depth buffer. Splitting on w

achieves this.

ShadowCoord = (ShadowCoord + vec4 (1.0, 1.0, 1.0, 1.0)) * vec4 (1.0/2.0, 1.0/2.0, 1.0/2.0, 1.0);

      

This also goes for sampling in the right place. The above projection has an object in the center of the light scene - a thing, for example. (0, 0, 1) - ends with (0, 0). But (0, 0) is the bottom left light of the lightmap, not the center. This line ensures that the lightmap is taken to cover an area from (-1, -1) to (1, 1) in light projection space.

... so, in general, the code refers to the mapping from 3D vectors, which describe a vector from light to a point in light space, to 2d vectors, which describe where the point falls on the plane of view - which was mapped to create a depth map.

+1


source







All Articles