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


######### pbmax fresnel stuff ##########


#program.env[5] is the player's eye origin
#vertex.position is the vertex's position [x,y,z,w]
#subtracing the two gives us a directional vector
#store that vector in R0
SUB R0.xyz, program.env[5], vertex.position;

#ok the following code normalizes the directional vector
#(returns the unit vecotr (length of 1) of the directional vector)
#this is even defined by arb and its standard stuff
#dot product of R0 and R0 (returns a scalar)
#[x*x]+[y*y]+[z*z] = s
#R1 then is [s,s,s,s]
#not unlike c^2 = a^2 + b^2
DP3 R1, R0, R0;

#reciprocal square root
#1/sqrt(s)
#R1 then is [1/sqrt(s),1/sqrt(s),1/sqrt(s),1/sqrt(s)]
RSQ R1, R1.x;

#multiply R0 by 1/sqrt(s)
#now R0 has length of 1 unit
#vector from player's eye to this vertex
MUL R0, R0, R1.x;

#dot product of view angle vector and the vertex normal
#in this case, vertex normal should be pointing striaght up
#returns the vertical length of view angle
#viewing staight down would be 1
#viewing straight across would be 0
DP3 R1, R0, vertex.normal;

#now across would be 1 (most reflection)
#down would be 0 (least amount of reflection)
SUB result.texcoord[3].x, 1.0, R1;


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;

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[1], texture[1], 2D;
MOV		localNormal.x, localNormal.a;
MAD		localNormal, localNormal, scaleTwo, subOne;

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





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


TEX		R0, R0, texture[0], 2D;
MUL		R0, R0, .5;
MOV		result.color.xyz, R0;


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


MOV result.color.a, fragment.texcoord[3].x;

END

