/*
--------------------------------------------------------------------------------
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 volumetric clouds + lightning -----------------------
// ----------------------------- GLSL Fragment ----------------------------------

// IN
varying vec3 v3DCoord;
varying vec2 vNoiseUV;
varying float vOpacity;
varying vec3 vEyePixel;
varying float vDistance;
varying vec4 vPositionAtt;
// OUT
// UNIFORM
uniform float uInterpolation;
uniform vec3 uSunDirection;
uniform vec3 uAmbientColor;
uniform vec3 uSunColor;
uniform vec4 uLightResponse;
uniform vec4 uAmbientFactors;
uniform vec4 uLightning0;
uniform vec4 uLightning1;
uniform vec4 uLightning2;
uniform vec3 uLightningColor;
uniform sampler3D uDensity0;
uniform sampler3D uDensity1;
uniform sampler2D uNoise;

void main(void)
{    
    // x - Sun light power
    // y - Sun beta multiplier
    // z - Ambient color multiplier
    // w - Distance attenuation
	// uLightResponse = float4(0.25,0.2,1,0.1);
	
	// Ambient light factors
	// x - constant, y - linear, z - cuadratic, w - cubic
	// float4 uAmbientFactors = float4(0.4,1,1,1);

	vec3 Noise = texture2D(uNoise, vNoiseUV*5.0).xyz;
	vec3 Final3DCoord = v3DCoord+0.002575*(Noise-0.5)*2.0;
	Final3DCoord.z = clamp(Final3DCoord.z, 0.0, 1.0);

	vec3 Density0 = texture3D(uDensity0, Final3DCoord).xyz;
	vec3 Density1 = texture3D(uDensity1, Final3DCoord).xyz;
	vec3 Density  = Density0*(1.0-uInterpolation) + Density1*uInterpolation;
	
	vec3 finalcolor = vec3(0,0,0);
	float Opacity = 0.0;
	
	if (Density.x > 0.0)
	{
	    float cos0 = clamp(dot(uSunDirection,vEyePixel), 0.0, 1.0);
	    float c2=cos0*cos0;
		
		float Beta = c2*uLightResponse.y*(0.5+2.5*clamp(1.0-2.0*uSunDirection.y, 0.0, 1.0)*Density.y);

		float sunaccumulation = max(0.2, clamp(Beta+Density.y*uLightResponse.x+pow(vDistance,1.5)*uLightResponse.w, 0.0, 1.0));
		float ambientaccumulation = 
			  clamp(uAmbientFactors.x + uAmbientFactors.y*v3DCoord.z + uAmbientFactors.z*pow(v3DCoord.z,2.0) + uAmbientFactors.w*pow(v3DCoord.z,3.0), 0.0, 1.0)*uLightResponse.z;
	    
		finalcolor = uAmbientColor*ambientaccumulation + uSunColor*sunaccumulation;
		Opacity = (1.0 - exp(-Density.x*(7.5-6.5*v3DCoord.z)))*vOpacity;
		
		vec3 lightningColor = uLightningColor*2.0*(1.0-clamp(finalcolor,0.0,1.0));
		
		// Lightning 0
		finalcolor += lightningColor * uLightning0.w / (0.85 + vPositionAtt.w*length(uLightning0.xyz - vPositionAtt.xyz));
		// Lightning 1
		finalcolor += lightningColor * uLightning1.w / (0.85 + vPositionAtt.w*length(uLightning1.xyz - vPositionAtt.xyz));
		// Lightning 2
		finalcolor += lightningColor * uLightning2.w / (0.85 + vPositionAtt.w*length(uLightning2.xyz - vPositionAtt.xyz));
	}
	
    gl_FragColor = vec4(finalcolor, Opacity);

 //oColor.xyz*=0.0001;
 // oColor.a = saturate(oColor.a+1)*iOpacity;
 // oColor.xyz+=Noise;
//oColor.rgb*=0.0001;oColor.r = dot(uLightDirection,iEyePixel);
}