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

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

# 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		result.texcoord[2], R1, program.local[1];

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, R2, dispersionMultiplier;

#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 = { 32, 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;

PARAM	dispersionFactor = 1.002;	#1.004;

# load the filtered normal map and convert to -1 to 1 range
TXB		localNormal, fragment.texcoord[1], texture[1], 2D;
MOV		localNormal.x, localNormal.a;
MOV   localNormal.z, 0.99;
MAD		localNormal, localNormal, scaleTwo, subOne;
DP3   localNormal.w, localNormal, localNormal;
RSQ   localNormal.w, localNormal.w;
MUL   localNormal.xyz, localNormal, localNormal.w;
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.xyz, 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;

#ADD	localNormal, localNormal, R2;
#MUL	localNormal, localNormal, ZeroFive;

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

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

# 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.w;
#MUL		localNormal.xyz, localNormal, R0;

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

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

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

# Texreads at y-coordinate near or beyond 1.0 limit do not give
# any result on some hardware. The dispersion fucks up the 0.0-1.0 clamp
# causing the upper margin of the screen only to show red colours as the
# shader cannot determine the g and b values. The following code simply disables
# dispersion for the upper margin.
SUB     R2.x, R0.y, 0.995;
CMP     dispersionMultiplier, R2.x, dispersionFactor, 1;

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

# sample the points by sampling the R, G and B values along the displacements
TXB		result.color.x, R0, texture[0], 2D;
MUL		R0, R0, dispersionMultiplier;
TXB		result.color.y, R0, texture[0], 2D;
MUL		R0, R0, dispersionMultiplier;
TXB		result.color.z, R0, texture[0], 2D;

# load the screen render
#TXB		result.color.xyz, R0, texture[0], 2D;

END
