!!ARBvp1.0 OPTION ARB_position_invariant ;

# input:
#
# texcoord[0] TEX0	texcoords
# attrib[9] TEX1	tangent[0]
# attrib[10] TEX2	tangent[1]
# attrib[2] TEX3	normal
#
# c[5]	localViewOrigin
# c[6]	modelMatrix[0]
# c[7]	modelMatrix[1]
# c[8]	modelMatrix[2]
#
# output:
#
# texture 0 is the environment cube map
# texture 1 is the normal map
#
# texCoord[0] is the normal map texcoord
# texCoord[1] is the vector to the eye in global space
# texCoord[2] is the surface tangent to global coordiantes
# texCoord[3] is the surface bitangent to global coordiantes
# texCoord[4] is the surface normal to global coordiantes

TEMP	R0, R1, R2;

# texture 0 takes the unodified texture coordinates
MOV		result.texcoord[0], vertex.texcoord[0];

# texture 1 is the vector to the eye in global coordinates
SUB		R0.xyz, program.env[5], vertex.position;
DP3		result.texcoord[1].x, R0, program.env[6];
DP3		result.texcoord[1].y, R0, program.env[7];
DP3		result.texcoord[1].z, R0, program.env[8];

# texture 2 gets the transformed tangent
DP3		result.texcoord[2].x, vertex.attrib[9], program.env[6];
DP3		result.texcoord[3].x, vertex.attrib[9], program.env[7];
DP3		result.texcoord[4].x, vertex.attrib[9], program.env[8];

# texture 3 gets the transformed tangent
DP3		result.texcoord[2].y, vertex.attrib[10], program.env[6];
DP3		result.texcoord[3].y, vertex.attrib[10], program.env[7];
DP3		result.texcoord[4].y, vertex.attrib[10], program.env[8];

# texture 4 gets the transformed tangent
DP3		result.texcoord[2].z, vertex.normal, program.env[6];
DP3		result.texcoord[3].z, vertex.normal, program.env[7];
DP3		result.texcoord[4].z, vertex.normal,program.env[8];

MOV		result.color.xyz, vertex.color;

END

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

!!ARBfp1.0
OPTION ARB_precision_hint_nicest;

# per-pixel cubic reflextion map calculation

# texture 0 is the environment cube map
# texture 1 is the normal map
#
# texCoord[0] is the normal map texcoord
# texCoord[1] is the vector to the eye in global space
# texCoord[3] is the surface tangent to global coordiantes
# texCoord[4] is the surface bitangent to global coordiantes
# texCoord[5] is the surface normal to global coordiantes

TEMP	globalEye, localNormal, globalNormal, R0, R1, R2, specular, newtexcoord;

#PARAM	subOne = { -1, -1, -1, -1 };
#PARAM	scaleTwo = { 2, 2, 2, 2 };
#PARAM	subOne = { -2, -2, -3, -3 };
#PARAM	scaleTwo = { 4, 4, 4, 4 };
#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.125, -1.125, -1.125, -1.125 };
#PARAM	scaleTwo = { 2.25, 2.25, 2.25, 2.25 };
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;

# specular is actually now viewer
DP3	specular, fragment.texcoord[6],fragment.texcoord[6];
RSQ	specular, specular.w;
MUL	specular.xyz, specular.w, 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, specular, fragment.texcoord[1];

# load the filtered normal map, then normalize to full scale,
TXB		localNormal, fragment.texcoord[0], texture[1], 2D;
MOV		localNormal.x, localNormal.a;				# rxgb normal compression
MOV   localNormal.z, 0.99;
MAD		localNormal, localNormal, scaleTwo, subOne;
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, specular, localNormal;

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

# localNormal filter
MOV	R0.xyz, localNormal;
MOV	R0.z, ZeroFive;
DP3	R0, R0, R0;
MAD_SAT	R0.z, R0.w, scaleTwoY, subOneY;
RSQ	R0, R0.z;
RCP	localNormal.z, R0.w;

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

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

# localNormal filter
ADD_SAT	R0, newtexcoord, ZeroFive;
TXB	R2, R0, 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	R0, fragment.position.z, scaleTwoX, subOneX;
MAX	R0, R0.w, ZeroFive;
ADD_SAT	localNormal.z, localNormal.z, R0.w;

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

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

DP3		R0, localNormal,localNormal;
RSQ		R0, R0.w;
MUL		localNormal.xyz, localNormal, R0.w;

# transform the surface normal by the local tangent space
DP3		globalNormal.x, localNormal, fragment.texcoord[2];
DP3		globalNormal.y, localNormal, fragment.texcoord[3];
DP3		globalNormal.z, localNormal, fragment.texcoord[4];

# normalize vector to eye
DP3		R0, newtexcoord, newtexcoord;
RSQ		R0, R0.x;
MUL		globalEye, newtexcoord, R0;

# calculate reflection vector
DP3		R0, globalEye, globalNormal;
MUL		R0, R0, globalNormal;
MAD		R0, R0, scaleTwo, -globalEye;

# read the environment map with the reflection vector
TXB		R0, R0, texture[0], CUBE;

MUL		result.color.xyz, R0, fragment.color;
#MOV		result.color.xyz, R0;

END
