!!ARBvp1.0 OPTION ARB_position_invariant ;

# VPROG_INTERACTION
#
# input:
#
# attrib[8] TEX0	texture coordinates
# attrib[9] TEX1	normal
# attrib[10] TEX2	tangent[0]
# attrib[11] TEX3	tangent[1]
# COL			vertex color
#
# c[4]	localLightOrigin
# c[5]	localViewOrigin
# c[6]	lightProjection S
# c[7]	lightProjection T
# c[8]	lightProjection Q
# c[9]	lightFalloff S
# c[10]	bumpMatrix S
# c[11]	bumpMatrix T
# c[12]	diffuseMatrix S
# c[13]	diffuseMatrix T
# c[14]	specularMatrix S
# c[15]	specularMatrix T
# c[16]	vertex color modulate
# c[17]	vertex color add
#
# output:
#
# texture 0 is the cube map
# texture 1 is the per-surface bump map
# texture 2 is the light falloff texture
# texture 3 is the light projection texture
# texture 4 is the per-surface diffuse map
# texture 5 is the per-surface specular map
# texture 6 is the specular lookup table

TEMP	R0, R1, R2;

PARAM	defaultTexCoord = { 0, 0.5, 0, 1 };


# calculate vector to light in R0
SUB		R0.xyz, program.env[4], vertex.position;

# put into texture space for TEX0
DP3		result.texcoord[0].x, vertex.attrib[9], R0;
DP3		result.texcoord[0].y, vertex.attrib[10], R0;
DP3		result.texcoord[0].z, vertex.attrib[11], R0;

# textures 1 takes the base coordinates by the texture matrix
MOV		result.texcoord[1].xy, defaultTexCoord;
MOV		result.texcoord[1], vertex.attrib[8];
#DP4		result.texcoord[1].x, vertex.attrib[8], program.env[10];
#DP4		result.texcoord[1].y, vertex.attrib[8], program.env[11];

# texture 2 has one texgen
MOV		result.texcoord[2].xy, defaultTexCoord;
DP4		result.texcoord[2].x, vertex.position, program.env[9];

# texture 3 has three texgens
DP4		result.texcoord[3].x, vertex.position, program.env[6];
DP4		result.texcoord[3].y, vertex.position, program.env[7];
DP4		result.texcoord[3].w, vertex.position, program.env[8];

# calculate vector to viewer in R0
SUB		R0.xyz, program.env[5], vertex.position;

# put into texture space for TEX6
DP3		result.texcoord[6].x, vertex.attrib[9], R0;
DP3		result.texcoord[6].y, vertex.attrib[10], R0;
DP3		result.texcoord[6].z, vertex.attrib[11], R0;

END

#==================================================================================

!!ARBfp1.0
OPTION ARB_precision_hint_nicest;

# texture 0 is the cube map
# texture 1 is the per-surface bump map
# texture 2 is the light falloff texture
# texture 3 is the light projection texture
# texture 4 is the per-surface diffuse map
# texture 5 is the per-surface specular map
# texture 6 is the specular lookup table

# env[0] is the diffuse modifier
# env[1] is the specular modifier

TEMP	toLight, light, toViewer, halfAngle, color, R1, R2, localNormal, newtexcoord;
TEMP	T1, T2, tangent;

#PARAM   subOne = { -2.5, -2.5, -2.5, -2.5 };
#PARAM   scaleTwo = { 5, 5, 5, 5 };
#PARAM   subOne = { -2.25, -2.25, -2.25, -2.25 };
#PARAM   scaleTwo = { 4.5, 4.5, 4.5, 4.5 };
#PARAM   subOne = { -2, -2, -2, -2 };
#PARAM   scaleTwo = { 4, 4, 4, 4 };
#PARAM   subOne = { -64, -64, -64, -64 };
#PARAM   scaleTwo = { 128, 128, 128, 128 };
#PARAM   subOne = { -1, -1, -1, -1 };
#PARAM   scaleTwo = { 2, 2, 2, 2 };
#PARAM   subOne = { -1.5, -1.5, -1.5, -1.5 };
#PARAM   scaleTwo = { 3, 3, 3, 3 };
#PARAM   subOne = { -1.25, -1.25, -1.25, -1.25 };
#PARAM   scaleTwo = { 2.5, 2.5, 2.5, 2.5 };
PARAM   subOne = { -1.0625, -1.0625, -1.0625, -1.0625 };
PARAM   scaleTwo = { 2.125, 2.125, 2.125, 2.125 };

PARAM	specExp = { 24, 0, 0, 0 };
#PARAM	specExp = { 16, 0, 0, 0 };

#PARAM	pointFive = { 0.5, 0.5, 0.5, 0.5 };

PARAM	GlobalLight = 0.9;	#1.0
PARAM	ZeroFive = 0.5;
#PARAM   scaleTwoX = { 4, 4, 4, 4 };
#PARAM   subOneX = { -3.5, -3.5, -3.5, -3.5 };
PARAM   scaleTwoX = { 8, 8, 8, 8 };
PARAM   subOneX = { -7.5, -7.5, -7.5, -7.5 };
#PARAM   scaleTwoY = { -1, -1, -1, -1 };
#PARAM   subOneY = { 1, 1, 1, 1 };
PARAM   scaleTwoY = -1;
PARAM   subOneY = 1;

#
# the amount of light contacting the fragment is the
# product of the two light projections and the surface
# bump mapping
#

# normalize the direction to the light
DP3		toLight, fragment.texcoord[0],fragment.texcoord[0];
RSQ		toLight, toLight.x;
MUL		toLight.xyz, toLight.x, fragment.texcoord[0];

# normalize the direction to the viewer
DP3		toViewer, fragment.texcoord[6],fragment.texcoord[6];
RSQ		toViewer, toViewer.x;
MUL		toViewer.xyz, toViewer.x, fragment.texcoord[6];

# load heightmap
TXB		R1, fragment.texcoord[1], texture[7], 2D;
MAD		R1, R1, program.local[0].x, program.local[0].y; # scale and bias
MAD		newtexcoord.xyz, R1, toViewer, fragment.texcoord[1];

# load the filtered normal map
TXB		localNormal, fragment.texcoord[1], texture[1], 2D;
MOV		localNormal.x, localNormal.a;
MOV   localNormal.z, 0.99;
MAD		localNormal.xyz, localNormal, scaleTwo, subOne;	#2.0, -1.0;
DP3   localNormal.w, localNormal, localNormal;
RSQ   localNormal.w, localNormal.w;
MUL   localNormal.xyz, localNormal.w, localNormal;
MUL localNormal.zw, localNormal.xyxy, localNormal.xyxy;

ADD_SAT localNormal.z, localNormal, localNormal.w;
ADD localNormal.z, -localNormal, GlobalLight;
RSQ localNormal.z, localNormal.z;
RCP localNormal.z, localNormal.z;
MUL R2, R2, GlobalLight; # dfm added

# soften
#LRP		localNormal.xyz, program.local[6].x, toViewer, localNormal;

# normalize localNormal
#DP3   localNormal.w, localNormal, localNormal;
#RSQ   localNormal.w, localNormal.w;
#MUL   localNormal.xyz, localNormal, localNormal.w;

MOV	R1.xyz, localNormal;
MOV	R1.z, ZeroFive;
DP3	R1, R1, R1;
MAD_SAT	R1.z, R1.w, scaleTwoY, subOneY;
RSQ	R1, R1.z;
RCP	localNormal.z, R1.w;

# localNormal filter
#MOV		localNormal.z, ZeroFive;
#DP3	R1.x, localNormal,localNormal;
#ADD_SAT	R1.x, 1, -R1.x;
#RSQ		R1.x, R1.x;
#RCP		localNormal.z, R1.x;

# localNormal filter
ADD_SAT	R1, newtexcoord, ZeroFive;
TXB	R2, R1, texture[1], 2D;
MOV R2.x, R2.a;
MAD_SAT	R2, R2, scaleTwoX, subOneX;

#ADD	localNormal, localNormal, R2;
#MUL	localNormal, localNormal, 0.5;

# flatten normal over distance (Z)
MAD_SAT	R1, fragment.position.z, scaleTwoX, subOneX;
MAX	R1, R1.w, ZeroFive;
ADD_SAT	localNormal.z, localNormal.z, R1.w;

# normalize the bumpmap
DP3		R1, localNormal,localNormal;
RSQ		R1, R1.x;
MUL		localNormal.xyz, localNormal, R1;

#TXB		localNormal, localNormal, texture[0], CUBE;
#MAD		localNormal, localNormal, scaleTwo, subOne;

# normalize the bumpmap
#DP3		R1, localNormal,localNormal;
#RSQ		R1, R1.w;
#MUL		localNormal.xyz, localNormal, R1;

#DP3 R2.x, toViewer, R2;
#POW_SAT R2.x, R2.x, 16.0;

DP3		light, toLight, localNormal;
MAD_SAT	light, 0.75, light, 0.25;

# load the filtered tangent map
TXB		tangent, fragment.texcoord[1], texture[6], 2D;
#MOV		tangent.x, tangent.a;
MOV		result.color.xyz, tangent;
MAD		tangent.xyz, tangent, scaleTwo, subOne;	#2.0, -1.0;

# normalize the tangent
DP3		R1, tangent, tangent;
RSQ		R1, R1.x;
MUL		tangent.xyz, tangent, R1;

ADD		halfAngle.xyz, toLight, toViewer;
DP3		R1.xyz, halfAngle, halfAngle;
RSQ		R1, R1.x;
MUL		halfAngle.xyz, halfAngle, R1;

LRP		localNormal, program.local[1].x, localNormal, {0,0,1.0,0};	# flatten
DP3		R1, localNormal,localNormal;
RSQ		R1, R1.x;
MUL		localNormal.xyz, localNormal, R1;

DP3		R1, localNormal, tangent;
MAD		tangent, R1, -localNormal, tangent;	#orthonormal
DP3		R1, tangent, tangent;
RSQ		R1, R1.x;
MUL		tangent.xyz, tangent, R1;

MAD		T1, localNormal, program.local[2].x, tangent;	# shift

DP3		T1, T1, halfAngle;
MAD_SAT	R1, T1, -T1, 1.0;
POW		R1, R1.x, program.local[3].w;
MUL		color.xyz, R1, program.local[3];	#color

MAD		T1, localNormal, program.local[2].y, tangent;	#shift

DP3		T1, T1, halfAngle;
MAD_SAT	R1, T1, -T1, 1.0;
POW		R1, R1.x, program.local[4].w;

MAD		color.xyz, R1, program.local[4], color;		#color

TXB		R1, fragment.texcoord[1], texture[4], 2D;
MUL		R1, R1, program.local[5];
MAD		color.xyz, R1, light, color;

DP3		R1, localNormal, toLight;
MAD_SAT	R1, 1.75, R1, 0.25;
MUL		color.xyz, R1, color;

TXB		R1, fragment.texcoord[1], texture[5], 2D;
MUL		color.xyz, color, R1;

# modulate by the light projection
TXP		R1, fragment.texcoord[3], texture[3], 2D;
MUL		color.xyz, color, R1;

# boost projection, specular light reaches further - DFM added
ADD_SAT	R1.xyz, R1, R1;
MUL	R2.xyz, R2, R1;

# modulate by the light falloff
TXP		R2, fragment.texcoord[2], texture[2], 2D;
MUL		color.xyz, color, R2;

# mask
TXB		R1, fragment.texcoord[1], texture[7], 2D;
DP4		R1, R1, program.local[0];
MUL		color.xyz, color, R1;

MUL		color.xyz, color, program.env[0];
MUL		result.color.xyz, color, 0.75;

END
