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

//Fragment shader for gbuffer pass.

#version 150
#extension GL_EXT_gpu_shader4:enable

#define SKINNED					1
#define HAS_NORMALS				2
#define HAS_GLOW				4
#define HAS_SPECULAR			8

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

uniform	float			uFarPlane; //used to clamp depth between 0 and 1
uniform float			uGlowBrightness; //glow brightness of this model (if it has a glow texture)
uniform vec3			uTint; //tint to apply to diffuse and glow
uniform vec3			uGlowTint; //tint to apply to diffuse and glow

uniform int				uFlag; //flag indicating various states of the object

//textures
uniform sampler2D		uNormMap; //normal map
uniform sampler2D		uDiffuse; //diffuse map
uniform sampler2D		uGlow; //glow map 
uniform sampler2D		uSpecular; //specular map

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

in vec3					vNormal;
in float				vDepth;
in mat3					vTBNMatrix; //matrix to transform from tangent to view space
in vec2					vTexCoord; 

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

out vec4				out_Color0; //to rendertarget 0
out vec4				out_Color1; //to rendertarget 1
out vec4				out_Color2; //to rendertarget 2

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

void main()
{
	vec4 color = texture( uDiffuse, vTexCoord );
	if ( color.a < 0.7 )
		discard;

	vec3 normal = (uFlag & HAS_NORMALS) > 0 ? vTBNMatrix * ( texture( uNormMap, vTexCoord ) * 2.0 - 1.0 ).xyz : vNormal;
	normal = normalize( normal );

	//1st texture: normals + depth
	out_Color0 = vec4( normal.x, normal.y, normal.z, -vDepth/uFarPlane );

	//2nd texture: diffuse, rgb and specular intensity
	out_Color1.rgb = color.rgb * uTint;
	out_Color1.a = (uFlag & HAS_SPECULAR) > 0 ? texture( uSpecular, vTexCoord ).r : 0.0;

	//3rd texture: glow
	out_Color2 = (uFlag & HAS_GLOW) > 0  ? vec4( texture( uGlow, vTexCoord ).rgb * uGlowTint * uTint, uGlowBrightness ) : vec4( 0.0, 0.0, 0.0, 0.0 );
}





