!!ARBvp1.0
OPTION ARB_position_invariant ;

# input:
# 
# texcoord[0] TEX0	texcoords
#
# local[0]	scroll
# local[1]	deform magnitude (1.0 is reasonable, 2.0 is twice as wavy, 0.5 is half as wavy, etc)
#
# output:
# 
# texture 0 is _currentRender
# texture 1 is a normal map that we will use to deform texture 0
#
# texCoord[1] is the model surface texture coords
# texCoord[2] is the copied deform magnitude

TEMP	R0, R1, R2;

#MAD		R0, -vertex.texcoord[0].y, vertex.normal, vertex.normal;
#MAD		R0, R0, -15.0, vertex.position;

#DP4		result.position.x, state.matrix.mvp.row[0], R0;
#DP4		result.position.y, state.matrix.mvp.row[1], R0;
#DP4		result.position.z, state.matrix.mvp.row[2], R0;
#DP4		result.position.w, state.matrix.mvp.row[3], R0;

# texture 1 takes the texture coordinates and adds a scroll
MAD		result.texcoord[0], vertex.texcoord[0], program.local[0], program.local[0].zwxy;

# texture 2 takes the deform magnitude and scales it by the projection distance
PARAM	vec = { 1, 0, 0, 1 };

MOV		R0, vec;
DP4		R0.z, vertex.position, state.matrix.modelview.row[2];

DP4		R1, R0, state.matrix.projection.row[0];
DP4		R2, R0, state.matrix.projection.row[3];

# don't let the recip get near zero for polygons that cross the view plane
MAX		R2, R2, 1;

RCP		R2, R2.w;
MUL		R1, R1, R2;

# clamp the distance so the the deformations don't get too wacky near the view
MIN		R1, R1, 0.02;
#MUL		R1, R1,R1;
#POW		R1, R1.x, 1.5.x;
#MUL		R1, R1, 10;

MUL		result.texcoord[1], R1, program.local[1];

# texture 2 gets the transformed tangent
DP3		result.texcoord[2].x, vertex.attrib[9], state.matrix.modelview.row[0];
DP3		result.texcoord[3].x, vertex.attrib[9], state.matrix.modelview.row[1];
DP3		result.texcoord[4].x, vertex.attrib[9], state.matrix.modelview.row[2];

# texture 3 gets the transformed bitangent
DP3		result.texcoord[2].y, vertex.attrib[10], state.matrix.modelview.row[0];
DP3		result.texcoord[3].y, vertex.attrib[10], state.matrix.modelview.row[1];
DP3		result.texcoord[4].y, vertex.attrib[10], state.matrix.modelview.row[2];

# texture 4 gets the transformed normal
DP3		result.texcoord[2].z, vertex.normal, state.matrix.modelview.row[0];
DP3		result.texcoord[3].z, vertex.normal, state.matrix.modelview.row[1];
DP3		result.texcoord[4].z, vertex.normal, state.matrix.modelview.row[2];

# calculate vector to viewer in R0
SUB		R0, 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.normal, R0;

END

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

!!ARBfp1.0 
OPTION ARB_precision_hint_nicest;

# texture 0 is _currentRender
# texture 1 is a normal map that we will use to deform texture 0
#
# env[0] is the 1.0 to _currentRender conversion
# env[1] is the fragment.position to 0.0 - 1.0 conversion

TEMP	localNormal, R0, globalNormal, toViewer;

PARAM	subOne = { -1, -1, -1, -1 };
PARAM	scaleTwo = { 2, 2, 2, 2 };

# load the filtered normal map and convert to -1 to 1 range
TEX		localNormal, fragment.texcoord[0], texture[1], 2D;
MOV		localNormal.x, localNormal.a;
MAD		localNormal, localNormal, scaleTwo, subOne;

MOV		localNormal.z, 0.0;

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

DP3		globalNormal.x, localNormal, fragment.texcoord[2];
DP3		globalNormal.y, localNormal, fragment.texcoord[3];
DP3		globalNormal.z, localNormal, fragment.texcoord[4];

# calculate the screen texcoord in the 0.0 to 1.0 range
MUL		R0, fragment.position, program.env[1];
MUL		globalNormal, globalNormal, toViewer.z;

# offset by the scaled localNormal and clamp it to 0.0 - 1.0
MAD_SAT	R0, globalNormal, fragment.texcoord[1], R0;

# scale by the screen non-power-of-two-adjust
MUL		R0, R0, program.env[0];


# load the screen render
TEX		result.color.xyz, R0, texture[0], 2D;
#MOV		result.color.xyz, toViewer.z;

END

