/*
--------------------------------------------------------------------------------
This source file is part of SkyX.
Visit http://www.paradise-studios.net/products/skyx/

Copyright (C) 2009-2012 Xavier Verguín González <xavyiy@gmail.com>

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt.
--------------------------------------------------------------------------------
*/

// ------------------------- SkyX clouds -----------------------------
// ------------------------ GLSL fragment ----------------------------

// IN
varying vec3 vPosition;
// OUT
// UNIFORM
uniform float uExposure;
// Sun information
uniform vec3 uSunColor;
// Main cloud layer parameters
uniform float uHeight;
uniform float uTime;
uniform float uScale;
uniform vec2 uWindDirection;
// Advanced cloud layer parameters
uniform float uCloudLayerHeightVolume; // 0.25
uniform float uCloudLayerVolumetricDisplacement; // 0.01
uniform vec3 uAmbientLuminosity; // 0.55 0.55 0.55
uniform float uDetailAttenuation; // 0.45
uniform float uDistanceAttenuation; // 0.05
uniform sampler2D uClouds;
uniform sampler2D uCloudsNormal;
uniform sampler2D uCloudsTile;

void main(void)
{
	// Get the cloud pixel lenght on the projected plane
    float vh = uHeight / vPosition.y;
    // Get the 3D position of the cloud pixel
    vec3 CloudPosition = vPosition * vh;
    
    // Get texture coords
    vec2 TexCoord = CloudPosition.xz*uScale;
    float Density = texture2D(uClouds, TexCoord+uTime*uWindDirection*0.25).r;
    vec3 Normal   = -(2.0*texture2D(uCloudsNormal, TexCoord+uTime*uWindDirection*0.25).xyz-1.0);
    Normal.zy = Normal.yz;
 
    ///------------ Volumetric effect:
    float CloudLayerHeightVolume = uCloudLayerHeightVolume*vPosition.y;
    float CloudLayerVolumetricDisplacement = uCloudLayerVolumetricDisplacement*vPosition.y;
    vec3 iNewPosition = normalize(vPosition + CloudLayerVolumetricDisplacement*vec3(Normal.x,0,Normal.z));
    vh = (uHeight+uHeight*(1.0-Density)*CloudLayerHeightVolume) / iNewPosition.y;
    CloudPosition = iNewPosition * vh;
    TexCoord = CloudPosition.xz*uScale;                              // Little offset
    Density = texture2D(uClouds, TexCoord+uTime*uWindDirection*0.25 + vec2(0.2,0.6)).r;
    ///------------
    
    float CloudTile = texture2D(uCloudsTile, TexCoord-uTime*uWindDirection*0.25).r;

	// Ambient + Sun*Density
    vec3 PixelColor = uAmbientLuminosity + uSunColor*Density;
    
    // AMBIENT addition
    //PixelColor += uAmbientLuminosity;
    
    // SUN addition 
    // PixelColor  += uSunColor*clamp(dot(-normalize(Normal), normalize(uSunPosition)), 0.0, 1.0);
    
    // FINAL colour
    float Alpha = Density * clamp(10.0*clamp(-uDistanceAttenuation+vPosition.y, 0.0, 1.0), 0.0, 1.0);
    
    gl_FragColor = vec4(PixelColor*(1.0-Density*0.35), Alpha*clamp(1.0-CloudTile*uDetailAttenuation, 0.0, 1.0));
    
  #ifdef LDR
    gl_FragColor.xyz = vec3(1.0 - exp(-uExposure * gl_FragColor.xyz));
  #else // HDR
    gl_FragColor.xyz *= pow(uExposure, 0.5);
  #endif
}
