!!ARBvp1.0 OPTION ARB_position_invariant ;

TEMP	R0, R1, R2;
MOV		result.texcoord[0], vertex.texcoord[0];
MOV		result.color.xyz, program.local[1];
END

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

!!ARBfp1.0 
OPTION ARB_precision_hint_nicest;

# registers
TEMP	R0, R1, R2;

# temps
TEMP	TEMP_color;
TEMP	TEMP_adjustedTC;
TEMP	TEMP_unadjustedTC;
TEMP	TEMP_warpFactor;
TEMP	TEMP_shift;

# default parms
PARAM	CONST_pointFive	= { 0.5, 0.5, 0.5, 0.5 };
PARAM	CONST_one		= { 1, 1, 1, 1 };
PARAM	CONST_two		= { 2, 2, 2, 1 };
PARAM	CONST_default	= { 0, 0, 0, 1 };
PARAM	CONST_pointOne	= { 0.1, 0.1, 0.1, 1 };
PARAM	CONST_blue		= { 1, 0, 0, 1 };

# program params
PARAM	PARM_scale		= { 1, 0.9, 0.8, 1 };
PARAM	PARM_shift		= { 0.005, -0.005, 0, 0 };
PARAM	PARM_numaccum	= { 0.125, 0.125, 0.125, 1 };
PARAM	PARM_test		= { 0.1, 0.1, 0.1, 1 };
PARAM	PARM_desat		= { 0.97, 0, 0, 1 };

PARAM	PARM_random		= { 0.003, -0.0003, 0.0004, -0.0007 };
PARAM	PARM_random2	= { 0.009, -0.0005, 0.0001, -0.0002 };


# =======================================
# COMPUTE UNADJUSTED/ADJUSTED TEXCOORDS
# =======================================
MUL		TEMP_unadjustedTC, fragment.position, program.env[1];

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

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





# =======================================
# COMPUTE WARP FACTOR
# =======================================
TXP		R0, TEMP_unadjustedTC, texture[1], 2D;
# MUL		R0, R0, fragment.color;
MOV		R0.x, R0.z;
SUB		TEMP_warpFactor, CONST_one, R0;

# MOV		TEMP_color, R0;



# =======================================
# COMPUTE PRETURBED COORDS
# =======================================
MOV		R0, TEMP_unadjustedTC;
MUL		R0, R0, program.env[0];				# scale by the screen non-power-of-two-adjust



# =======================================
# FX CODE
# =======================================

# initialize
MOV		TEMP_color, CONST_default;

MUL		R2, fragment.color, CONST_two;		# get it from (0,1) to (-1,1)
SUB		R2, R2, CONST_one;
MUL		TEMP_shift, R2, PARM_shift;

# do the sum up
TXP		R2, R0, texture[0], 2D;
ADD		TEMP_color, TEMP_color, R2;
ADD		R0, R0, TEMP_shift;
ADD		R0, R0, PARM_random.x;

# do we need to kill fragment?
# MOV		result.color.xyz, CONST_blue;
# SUB		R2, TEMP_color, PARM_test;
# KIL		R2;

# do the other sum ups
TXP		R2, R0, texture[0], 2D;
ADD		TEMP_color, TEMP_color, R2;
ADD		R0, R0, TEMP_shift;
ADD		R0, R0, PARM_random.y;

TXP		R2, R0, texture[0], 2D;
ADD		TEMP_color, TEMP_color, R2;
ADD		R0, R0, TEMP_shift;
ADD		R0, R0, PARM_random.z;

TXP		R2, R0, texture[0], 2D;
ADD		TEMP_color, TEMP_color, R2;
ADD		R0, R0, TEMP_shift;
ADD		R0, R0, PARM_random.w;

TXP		R2, R0, texture[0], 2D;
ADD		TEMP_color, TEMP_color, R2;
ADD		R0, R0, TEMP_shift;
ADD		R0, R0, PARM_random2.x;

TXP		R2, R0, texture[0], 2D;
ADD		TEMP_color, TEMP_color, R2;
ADD		R0, R0, TEMP_shift;
ADD		R0, R0, PARM_random2.y;

TXP		R2, R0, texture[0], 2D;
ADD		TEMP_color, TEMP_color, R2;
ADD		R0, R0, TEMP_shift;
ADD		R0, R0, PARM_random2.z;

TXP		R2, R0, texture[0], 2D;
MUL		R2, R2, PARM_desat;
ADD		TEMP_color, TEMP_color, R2;
ADD		R0, R0, TEMP_shift;
ADD		R0, R0, PARM_random2.w;


# final divide
MUL		TEMP_color, TEMP_color, PARM_numaccum;



# =======================================
# FINAL DUMP
# =======================================
MOV		result.color.xyz, TEMP_color;




END
