   Shadow mapping question
  Hello,I was trying to get into shadow mapping. Theory behind seemed to me easy - that is, render scene from light POV and save depth, then render scene from camera POV and using depths compare of camera and light check if pixel is in the shadow.I found some demo/tutorials that works, but I still didn't get details. First I used code like this:set light modelview and projection matrix, render scene, and save depth and texture matrices like this: bias * projection * modelview.then set camera modelview and projection matrix and render scene again, but this time use shader program that looks like this:VS:   ...    ShadowCoord = gl_TextureMatrix * gl_Vertex;  ...FS:   uniform sampler2DShadow ShadowMap;   ...    float shadow = shadow2DProj(ShadowMap, shadowCoord).w; //see if we are in shadow.  ..this worked, but I still didn't fully understand what is behind that shadow2DProj instruction. So I made some investigation and according to specifications I replaced code with this:VS is the same FS: uniform sampler2D ShadowMap;   ...    vec4 swCoords = shadowCoord / shadowCoord.q;    float depth1 = texture2D(ShadowMap, swCoords.st).r;    float depth2 = swCoords.p;    float shadow;    if (depth1 >= depth2) shadow = 1.0;    else shadow = 0.0;  ..  It still worked, BUT in fact I still dont understand it:( It should work like comparing of depth from light and camera, but I still dont see it in it!Because as I understand the code it means:  * ShadowCoords in vertex shader is vertex transformed by light matrices into screen coordinates (without w division) - think, that this is called homogeneous coordinates  * next, in fragment shader we divide it by W component and we get coordinates as the vertex appears in the screen (in 0 - 1 range) IS IT CORRECT?  * by using those coordinates we go to the ShadowMap (texture that represents scene depth from point of wiew) to get depth1 - this is probably light depth  * depth2 is just swCoords.p  * then comparison is made.But, I dont understand those two depths they both seems to me related only to the light, but not the scene, because that swCoords are realated only to the light!Please, where did I go wrong? Thanks.   
  As you said, the shadowmap holds the distance for a point in lightspace. Now when you render your scene you transform every point into lightspace again and compare it to the depth in the shadowmap. That way you know whether that point is occluded from the lights point of view. If this is the case the point will receive no light/is in shadow. So you always compare the depth related to the light with a another depth related to the light. Is that what made you wonder or did you have a completely different question? :)   ;  Thanks for reply, luckily I found yet this post, that helps me lot.   