!!ARBfp1.0

#-----------------------------------------
#Texture0 = Normal Map
#Texture1 = Alpha map

#TexCoord0 = Diffuse/Normal/Specular tex coord
#TexCoord1 = Animated model space pixel position
#TexCoord3 = Interpolated tangent space light vector (IPTSLV)
#TexCoord4 = Interpolated tangent space eye vector (IPTSEV)

#-----------------------------------------

OUTPUT oCol = result.color;

PARAM const_diffuse = { 0.5, 0.5, 0.5, 1.0 };			# Constant diffuse color,
PARAM const_light = { 1.0, 1.0, 1.0, 0.0 };				# Constant light color (for the moment)
PARAM const_val = { 0.25, 0.5, 1.0, 2.0 };				# Just some constants
PARAM const_val2 = { 4.0, 6.0, 0.0, 0.0 };				

ATTRIB NormalMapTexCoord = fragment.texcoord[0];
ATTRIB PixelPosition = fragment.texcoord[1];
ATTRIB IPTSLV = fragment.texcoord[3];
ATTRIB IPTSEV = fragment.texcoord[4];

TEMP NormalMapTexel;
TEMP AlphaMapTexel;
TEMP TSLV;
TEMP TSEV;
TEMP r0;
TEMP r1;
TEMP Reflection;

TEX NormalMapTexel, NormalMapTexCoord, texture[0], 2D;	# Sample normalmap
TEX AlphaMapTexel, NormalMapTexCoord, texture[1], 2D;	# Sample alphamap

#-----------------------------------------
# Reconstruct normal from g,b components
MAD NormalMapTexel.rgba, NormalMapTexel, const_val.a, -const_val.b;# Bias and scale the normalmap texel (only rgb) (from 0->1, -1->1)
SWZ NormalMapTexel.rgb, NormalMapTexel, 0, a, g, 0;
DP3 NormalMapTexel.r, NormalMapTexel, NormalMapTexel;	# r = g^2 + b^2
SUB NormalMapTexel.r, const_val.z, NormalMapTexel.r;	# r = 1 - r;
RSQ NormalMapTexel.r, NormalMapTexel.r;			# r = 1/sqrt(r)
RCP NormalMapTexel.r, NormalMapTexel.r;			# r = 1/r = sqrt(1 - g^2 - b^2)

#-----------------------------------------
# Normalize TSLV
DP3 TSLV.a, IPTSLV, IPTSLV;
RSQ TSLV.a, TSLV.a;
MUL TSLV.xyz, IPTSLV, TSLV.a;

#-----------------------------------------
# Normalize TSEV
DP3 TSEV.a, IPTSEV, IPTSEV;
RSQ TSEV.a, TSEV.a;
MUL TSEV.xyz, IPTSEV, TSEV.a;

#-----------------------------------------
# Calc reflection vector
DP3 r0.a, NormalMapTexel, TSEV;
ADD r0.a, r0.a, r0.a;
MAD Reflection.xyz, NormalMapTexel, r0.a, -TSEV;

#-----------------------------------------
# Self shadowing
SUB r0.a, const_val.r, -TSLV.x;
MUL_SAT r0.a, r0.a, const_val2.x;
MUL r1.w, r1.w, r0.a;

#-----------------------------------------
# Diffuse
#MUL r1.rgb, const_diffuse, const_val.z;		# Multiply diffuse color with light color
MOV r1.rgb, const_diffuse;		# Multiply diffuse color with light color
MUL r1.rgb, r1, const_val.a;				# Scale by 2 for correct brightness
DP3_SAT r0.rgb, NormalMapTexel, TSLV;
MUL r0.rgb, r0, r1;							# Diffuse color * Diffuse dotprod


#-----------------------------------------
# Adjust final color depending on our alpha map used as a "masking" image
MAD r1, r0, const_val.w, -const_val.z;	# Bias and scale diffuse color (from 0->1, -1->1)
MUL r0, r1, AlphaMapTexel.r;			# Multiply bias with alpha texel
ADD r1, r0, const_val.z;				# Scale back to 0->1 (from -1->1)
MUL r0, r1, const_val.y;				#

#-----------------------------------------
# Set alpha value from constant
MOV r0.a, const_val.z;

#-----------------------------------------
# Output
MOV oCol, r0;

END