title: Parallax mapping and shadow map
body: <p>I'm currently working on a new OpenGL Engine. I had a version running with normal mapping and shadow map shadding. I recently added parallax mapping to enhance the visual effect of plane texture.
  My problem is that I don't understand how to include the shadow mapping with the parallax effect. At the moment the shadow only follow the plane surface thus breaking the depth effect of the parallax mapping.</p>
  <p>All the resources I found online talking about this problem (like <a href="https://stackoverflow.com/questions/55089830/adding-shadows-to-parallax-occlusion-map">this one</a>) talk about self-shadow and not shadow from other objets in the scene (computed from the shadowmap).</p>
  <p><a href="https://i.stack.imgur.com/VQN1c.jpg" rel="nofollow noreferrer">Here is an image displaying my problem</a></p>
  <p>I followed LearnOpenGL.com tutorials but if needed, here is my function calculating the shadow :</p>
  <pre><code>float ShadowCalculationDirectionalLight(vec4 fragPosLightSpace)
  {
      // perform perspective divide
      vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
      // transform to [0,1] range
      projCoords = projCoords * 0.5 + 0.5;
      // Gestion quand on est en dehors de la ShadowMap -&gt; pas d'ombre
      if(projCoords.x &lt; 0 || projCoords.x &gt; 1 || projCoords.y &lt; 0 || projCoords.y &gt; 1)
          return 0;
      // get closest depth value from light's perspective (using [0,1] range     fragPosLight as coords)
      float closestDepth = texture(dirLight.shadowMap, projCoords.xy).r; 
      // get depth of current fragment from light's perspective
      float currentDepth = projCoords.z;
      // calculate bias (based on depth map resolution and slope)
      vec3 lightDir = normalize(-dirLight.direction);
      float bias = max(0.05 * (1.0 - dot(fs_in.Normal, lightDir)), 0.005);
      // PCF
      float shadow = 0.0;
      vec2 texelSize = 1.0 / textureSize(dirLight.shadowMap, 0);
      for(int x = -1; x &lt;= 1; ++x)
      {
          for(int y = -1; y &lt;= 1; ++y)
          {
              float pcfDepth = texture(dirLight.shadowMap, projCoords.xy + vec2(x, y) * texelSize).r; 
              shadow += currentDepth - bias &gt; pcfDepth  ? 1.0 : 0.0;        
          }    
      }
      shadow /= 9.0;
      
      // keep the shadow at 0.0 when outside the far_plane region of the light's frustum.
      if(projCoords.z &gt; 1.0)
          shadow = 0.0;
          
      return shadow;
  }
  </code></pre>
  <p>And here is the function for the parallax mapping</p>
  <pre><code>vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir)
  { 
      float numLayers = mix(material.maxLayers, material.minLayers, abs(dot(vec3(0.0, 0.0, 1.0), viewDir)));  
      // calculate the size of each layer
      float layerDepth = 1.0 / numLayers;
      // depth of current layer
      float currentLayerDepth = 0.0;
      // the amount to shift the texture coordinates per layer (from vector P)
      vec2 P = viewDir.xy / viewDir.z * material.heightScale; 
      vec2 deltaTexCoords = P / numLayers;
    
      // get initial values
      vec2  currentTexCoords     = texCoords;
      float currentDepthMapValue =  1 - texture(material.heightMap, currentTexCoords).r;
        
      while(currentLayerDepth &lt; currentDepthMapValue)
      {
          // shift texture coordinates along direction of P
          currentTexCoords -= deltaTexCoords;
          // get depthmap value at current texture coordinates
          currentDepthMapValue = texture(material.heightMap, currentTexCoords).r;  
          // get depth of next layer
          currentLayerDepth += layerDepth;  
      }
      
      // get texture coordinates before collision (reverse operations)
      vec2 prevTexCoords = currentTexCoords + deltaTexCoords;
  
      // get depth after and before collision for linear interpolation
      float afterDepth  = currentDepthMapValue - currentLayerDepth;
      float beforeDepth = texture(material.heightMap, prevTexCoords).r - currentLayerDepth + layerDepth;
   
      // interpolation of texture coordinates
      float weight = afterDepth / (afterDepth - beforeDepth);
      vec2 finalTexCoords = prevTexCoords * weight + currentTexCoords * (1.0 - weight);
  
      return finalTexCoords;
  }
  </code></pre>
  <p>Am I just missing something obvious ? My intuition was to use the displacement (calculated by doing the difference between the original textcoord and the displaced textcoord from the parallax function) and applied it to the light space textcoord of the shadow calculation but then I also need to transform them to tangent space so I'm lost.</p>
  <p>Thanks in advance !</p>