     // After removing the initial path from camera pos to top of atmosphere or the
     // current camera position if inside atmosphere, t > 0
     if (t > 0.0) {
+      // Here we must test if we are hitting the ground:
+      bool  insideATM    = false;
+      double offset      = 0.0f;
+      double maxLength   = 0.0f;
+      dRay ray;
+      ray.direction = vec4(v, 0.0);
+      ray.origin = vec4(x, 1.0);     
+      bool  hitGround = dAtmosphereIntersection(vec3(0.0), ray,  Rg,
+                                                insideATM, offset, maxLength);
+      if (hitGround) {
+        t = float(offset); 
+      }
       // Calculate the zenith angles for x0 and v, s:
       vec3  x0     = x + t * v;
       float r0     = length(x0);

    float muSun           = dot(n, s);
    // Is direct Sun light arriving at x0? If not, there is no direct light from Sun (shadowed)
    vec3  transmittanceL0 = muSun < -sqrt(1.0f - ((Rg * Rg) / (r0 * r0))) ? vec3(0.0f) : transmittanceLUT(r0, muSun);
    //return transmittanceL0;
    // E[L*] at x0
    vec3 irradianceReflected = irradiance(irradianceTexture, r0, muSun);
    return irradianceReflected;
    // Adding clouds texture
    //vec4 clouds = vec4(0.85)*texture(cloudsTexture, vs_st);


      //renderTarget = vec4(HDR(groundColor), 1.0); 
      //renderTarget = vec4(groundColor, 1.0); 
      //renderTarget = vec4(HDR(sunColor), 1.0); 
      //renderTarget = vec4(HDR(sunColor), 1.0); 
      renderTarget = vec4(HDR(inscatterColor + groundColor + inscatterColor), 1.0);       
      
    } else {
