!!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, R2, 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
DP3	specular, fragment.texcoord[6],fragment.texcoord[6];
RSQ	specular, specular.w;
MUL	specular.xyz, specular.w, fragment.texcoord[6];

# load the filtered normal map, then normalize to full scale,
TEX		localNormal, fragment.texcoord[0], texture[1], 2D;
MOV		localNormal.x, localNormal.a;				# rxgb normal compression
#MOV   localNormal.z, 0.75;
MAD		localNormal, localNormal, scaleTwo, subOne;

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

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

# localNormal filter
MOV	R0.xyz, localNormal;
MOV	R0.z, { 0, 0, 0, 0.5 };
DP3_SAT	R0, R0, R0;
MAD_SAT	R0.z, R0.w, -1, 1;
RSQ	R0, R0.z;
RCP	localNormal.z, R0.w;

# localNormal filter
#MOV		localNormal.z, { 0, 0, 0, 0.5 };
#DP3_SAT	R0.x, localNormal,localNormal;
#ADD_SAT	R0.x, 1, -R0.x;
#RSQ		R0.x, R0.x;
#RCP		localNormal.z, R0.x;

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

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

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

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

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

#DP3		R0, localNormal,localNormal;
#RSQ		R0, R0.x;
#MUL		localNormal.xyz, localNormal, R0.x;

# 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, fragment.texcoord[1], fragment.texcoord[1];
RSQ		R0, R0.x;
MUL		globalEye, fragment.texcoord[1], 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
TEX		R0, R0, texture[0], CUBE;

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

END

