!!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;
OPTION NV_fragment_program2;

# 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

SHORT TEMP	globalEye, localNormal, globalNormal, R0, R2, R3, specular;

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

# specular is actually now viewer
#DP3H	specular, fragment.texcoord[6],fragment.texcoord[6];
#RSQH	specular, specular.w;
#MULH	specular.xyz, specular.w, fragment.texcoord[6];

NRMH specular.xyz, fragment.texcoord[6];

# load the filtered normal map, then normalize to full scale,
TXL		localNormal, fragment.texcoord[0], texture[1], 2D;
MOVH		localNormal.x, localNormal.a;				# rxgb normal compression
#MOVH   localNormal.z, 0.75;
MADH		localNormal, localNormal, scaleTwo, subOne;
NRMH localNormal, localNormal;
MULH localNormal.zw, localNormal.xyxy, localNormal.xyxy;

ADDH_SAT localNormal.z, localNormal, localNormal.w;
ADDH localNormal.z, -localNormal, 1.0;
RSQH localNormal.z, localNormal.z;
RCPH localNormal.z, localNormal.z;
RFLH R3, localNormal, light;

#MULH R3, R3, 1.0; # dfm added

# soften
#LRPH		localNormal.xyz, program.local[6].x, specular, localNormal;

# normalize localNormal
DP3H_SAT   localNormal.w, localNormal, localNormal;
RSQH   localNormal.w, localNormal.w;
MULH   localNormal, localNormal, localNormal.w;

# localNormal filter
MOVH	R0.xyz, localNormal;
MOVH	R0.z, { 0, 0, 0, 0.5 };
DP3H_SAT	R0, R0, R0;
MADH_SAT	R0.z, R0.w, -1, 1;
RSQH	R0, R0.z;
RCPH	localNormal.z, R0.w;

# localNormal filter
#MOVH		localNormal.z, { 0, 0, 0, 0.5 };
#DP3H_SAT	R0.x, localNormal,localNormal;
#ADDH_SAT	R0.x, 1, -R0.x;
#RSQH		R0.x, R0.x;
#RCPH		localNormal.z, R0.x;

#TXL		localNormal, localNormal, texture[0], CUBE;
#MADH		localNormal, localNormal, scaleTwo, subOne;

# localNormal filter
ADDH_SAT	R0, fragment.texcoord[1], { 0, 0, 0, 0.5 };
TXB	R2, R0, texture[1], 2D;
MOVH R2.x, R2.a;
MADH_SAT	R2, R2, scaleTwo, subOne;

# flatten normal over distance (Z)
MADH_SAT	R0, fragment.position.z, 4.0, -3.5;
MAXH	R0, R0.w, { 0, 0, 0, 0.5 }; #0.0;
ADDH_SAT	localNormal.z, localNormal.z, R0.w;

# normalize the bumpmap
DP3H		R0, localNormal,localNormal;
RSQH		R0, R0.w;
MULH		localNormal.xyz, localNormal, R0.w;

#TXL		localNormal, localNormal, texture[0], CUBE;
#MADH		localNormal, localNormal, scaleTwo, subOne;

#DP3H		R0, localNormal,localNormal;
#RSQH		R0, R0.x;
#MULH		localNormal.xyz, localNormal, R0.x;

# specular.w is the specular volume
DP3H_SAT R3.x, specular, R3;
POWH_SAT R3.x, R3.x, 16.0;	#8.0

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

# normalize vector to eye
DP3H		R0, fragment.texcoord[1], fragment.texcoord[1];
RSQH		R0, R0.x;
MULH		globalEye, fragment.texcoord[1], R0;

# calculate reflection vector
DP3H		R0, globalEye, globalNormal;
MULH		R0, R0, globalNormal;
MADH		R0, R0, scaleTwo, -globalEye;

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

MULH		result.color.xyz, R0, fragment.color;
#MOVH		result.color.xyz, R0;

END
