!!ARBfp1.0
#OPTION ARB_precision_hint_fastest;

#/*ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*\
#	File:			Program for darkness vision
#					
#	Author:			Magnus Högdahl
#					
#	Copyright:		Starbreeze AB 2004
#					
#	History:
#
#\*____________________________________________________________________________________________*/

#-----------------------------------------
OUTPUT oCol = result.color;

ATTRIB v0 = fragment.color;
ATTRIB tc0 = fragment.texcoord[0];
ATTRIB tc1 = fragment.texcoord[1];
ATTRIB tc2 = fragment.texcoord[2];

PARAM VPParam = program.env[0];
PARAM V2WRow0 = program.env[1];
PARAM V2WRow1 = program.env[2];
PARAM V2WRow2 = program.env[3];
PARAM FogPlane = program.env[4];
PARAM FogColor = program.env[5];

PARAM const_val = { 0,0,0,1 };
#PARAM ZFar = 5000;
#PARAM ZNear = 4;
#PARAM FogPlane = { 0, 0, 1, 0.0010625 };

TEMP t0;
TEMP t1;
TEMP t2;

TEMP r0;
TEMP PixelPosV;		# View space pixel position
TEMP PixelPosW;
TEMP FogPosW;
TEMP r1;
TEMP r2;
TEMP R3;
TEMP R4;
TEMP R5;
#-----------------------------------------

TEX t0, tc0, texture[0], 2D;
TEX t1, tc1, texture[1], 2D;
TEX t2, tc0, texture[2], 2D;

MOV r0.rgb, t0;
MOV t2.a, t2.r;

# Convert depth to z
MAD r2.x, t2.a, 2.0, -1.0;
SUB r2.y, VPParam.y, VPParam.x;
ADD r2.z, VPParam.y, VPParam.x;
MUL r2.y, r2.y, r2.x;
SUB r2.y, r2.y, r2.z;
MUL r2.w, VPParam.y, VPParam.x;
MUL r2.w, r2.w, -2.0;
RCP r2.y, r2.y;
MUL PixelPosV.z, r2.y, r2.w;

MUL PixelPosV.x, tc2.x, VPParam.z;
MUL PixelPosV.y, tc2.y, VPParam.w;
MUL PixelPosV.xy, PixelPosV, PixelPosV.z;

DP3 PixelPosV.a, PixelPosV, PixelPosV;
RSQ r2.a, PixelPosV.a;
RCP PixelPosV.a, r2.a;

DP3 PixelPosW.x, PixelPosV, V2WRow0;
DP3 PixelPosW.y, PixelPosV, V2WRow1;
DP3 PixelPosW.z, PixelPosV, V2WRow2;

MUL FogPosW, PixelPosW, r2.a;
MIN FogPosW.a, 512.0, PixelPosV.a;
MUL FogPosW.rgb, FogPosW, FogPosW.a;
ADD FogPosW.z, FogPosW.z, -100;

#MOV R4, c[$TEXPARAM0+1]; <-- eye
#MOV R4.w, c[$BASE+8].y;
MOV R4, const_val;
#ADD R5, R4, -R8;

DP3 R3.x, FogPosW, FogPlane;
ADD R3.x, R3.x, FogPlane.w;

DP4 R3.y, R4, FogPlane;
ADD R3.w, R3.x, -R3.y;
RCP R3.w, R3.w;
MIN R3.y, R3.y, 0.0;
MUL R3.y, R3.y, R3.w;
MUL R5, PixelPosW, R3.y;
ADD R4, R4, R5;
ADD R5, R4, -FogPosW;

DP3 R3.y, R4, FogPlane;
ADD R3.y, R3.y, FogPlane.w;
MIN R3.xy, R3, 1.0;
MAX R3.xy, R3, 0.0;
ADD R3.x, R3.x, R3.y;
DP3 R4.w, R5, R5;
RSQ R4.w, R4.w;
RCP R4.w, R4.w;
MUL R3.x, R3.x, R4.w;

#MUL R3.x, R3.x, c[$TEXPARAM0+1].w; <-density
#MOV R3.yzw, c[$BASE+8].yyyy;

#MOV @O_COL0, c[$TEXPARAM0+2];
MUL r0.a, R3.x, FogColor.a;
MOV r1.a, 0.7;
POW r0.a, r0.a, r1.a;
MIN r0.a, 0.9, r0.a;
SUB t1.r, 1, t1.r;
MUL_SAT r0.a, r0.a, t1.r;
MOV r0.rgb, FogColor;

#LRP r0.rgb, r0.a, r0, t0;
#MOV r0.rgb, t0;

#MOV r0.rgb, t1.r;
#MOV r0.a, 1.0;
#DP3 r0.rgb, FogPlane, PixelPosW;
#MUL r0.rgb, r0, 0.01;
#MOV r0.rgb, 0;
#SUB Z.a, 1, t2.a;
#MOV Z.a, t2.a;
#RCP Z.a, Z.a;
#MUL r0.rgb, PixelPosV.a, 0.00390625;

MOV oCol, r0;

END
