// All content © 2010-2011 DigiPen (USA) Corporation, all rights reserved.

//Fragment shader for the lava effect

#version 150

// ---------------------------------------------------------------------------
//uniforms

//textures
uniform sampler2D		uDiffuse; 
uniform sampler2D		uDiffuse2;
uniform sampler2D		uGlowMap; 
uniform sampler2D		uNormMap;
uniform sampler2D		uSpecular;
uniform float			uFarPlane; //camera far plane
uniform float			uGlowBrightness; //for glow map
uniform float			uTimer; //for glow map

// ---------------------------------------------------------------------------
// from vertex shader

in vec2					vTexCoord0;
in vec2					vTexCoord1;

in float				vDepth; //depth
in mat3					vTBNMatrix; //matrix to transform from view space to tangent

in vec3					vLightDir;

// ---------------------------------------------------------------------------
// output

out vec4				out_Color0;
out vec4				out_Color1;
out vec4				out_Color2;

// ---------------------------------------------------------------------------

void main()
{	
	vec2 tc0 = vec2( vTexCoord0.s, vTexCoord0.t );

	float val = sin(uTimer + tc0.x * 25.0) * 0.015;
	vec2 textureOffset = vec2( val, 0.0 );

	//1st texture: normals and depth
	vec3 normal = vTBNMatrix * ( texture( uNormMap, tc0 ) * 2.0 - 1.0 ).xyz;
	normal = normalize( normal );

	out_Color0 = vec4( normal.x, normal.y, normal.z, -vDepth/uFarPlane );

	float lerpValue = clamp( texture( uGlowMap, vTexCoord0 ).r, 0.0, 0.8 );

	//2nd texture: diffuse and specular
	//texcoord0: top, texcoord1: bottom
	vec3 diffuse = mix( texture( uDiffuse, tc0 ).rgb * 0.6, texture( uDiffuse2, vTexCoord1 + textureOffset ).rgb, lerpValue );

	//add simple phong lighting to the lava diffuse
	out_Color1 = vec4( diffuse, 0.0 );

	//3rd texture: glow
	out_Color2 = vec4( diffuse, uGlowBrightness );

	//out_Color1 = vec4( texture(uDiffuse, vTexCoord1).rgb, 0.0 );
	//out_Color2 = vec4( texture( uGlowMap, vTexCoord1 ).rgb, uGlowBrightness ) ;
}





