/*
--------------------------------------------------------------------------------
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.
--------------------------------------------------------------------------------
*/

#ifdef GL_ES
precision highp int;
precision highp float;
#endif

// ------------------------- SkyX clouds -----------------------------
// ------------------------ GLSL fragment ----------------------------
#define USE_OGRE_FROM_FUTURE
#include <OgreUnifiedShader.h>

SAMPLER2D(uClouds, 0);
SAMPLER2D(uCloudsNormal, 1);
SAMPLER2D(uCloudsTile, 2);

OGRE_UNIFORMS(
uniform float uExposure;
uniform vec3 uSunColor;
uniform float uHeight;
uniform float uTime;
uniform float uScale;
uniform vec2 uWindDirection;
uniform float uCloudLayerHeightVolume;
uniform float uCloudLayerVolumetricDisplacement;
uniform vec3 uAmbientLuminosity;
uniform float uDetailAttenuation;
uniform float uDistanceAttenuation;
)

MAIN_PARAMETERS
IN(vec3 vPosition, TEXCOORD0)
MAIN_DECLARATION
{
	// 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 * vec3_splat(vh);
  
  // Get texture coords
  vec2 TexCoord = CloudPosition.xz * vec2_splat(uScale);
  vec2 CoordsAnim = TexCoord + vec2_splat(uTime) * uWindDirection * vec2_splat(0.25);
  float Density = texture2D(uClouds, CoordsAnim).r;
  vec3 Normal = -(vec3_splat(2.0) * texture2D(uCloudsNormal, CoordsAnim).xyz - vec3_splat(1.0));
  Normal.zy = Normal.yz;

  ///------------ Volumetric effect:
  float CloudLayerHeightVolume = uCloudLayerHeightVolume*vPosition.y;
  float CloudLayerVolumetricDisplacement = uCloudLayerVolumetricDisplacement * vPosition.y;
  vec3 iNewPosition = normalize(vPosition + vec3_splat(CloudLayerVolumetricDisplacement) * vec3(Normal.x, 0.0, Normal.z));
  vh = (uHeight + uHeight * (1.0 - Density) * CloudLayerHeightVolume) / iNewPosition.y;
  CloudPosition = iNewPosition * vec3_splat(vh);
  TexCoord = CloudPosition.xz * vec2_splat(uScale); // Little offset
  
  CoordsAnim = TexCoord + vec2_splat(uTime) * uWindDirection * vec2_splat(0.25);
  Density = texture2D(uClouds, CoordsAnim + vec2(0.2, 0.6)).r;
  ///------------
  
  CoordsAnim = TexCoord - vec2_splat(uTime) * uWindDirection * vec2_splat(0.25);
  float CloudTile = texture2D(uCloudsTile, CoordsAnim).r;

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