# Doom 3 Parallax Mapping mod by [x26]VOLAND
# E-mail: x26.voland@gmail.com
# ICQ: 680277
# Site: http://x26-voland.narod.ru/
# You may not modify, share, include this script into your own product without ny permission.

!!ARBvp1.0 OPTION ARB_position_invariant;
TEMP R0, R1, R2;

PARAM DTexCoord = { 0, 0.5, 0, 1 };

SUB R0, program.env[4], vertex.position;

DP3 result.texcoord[0].x, vertex.attrib[9], R0;
DP3 result.texcoord[0].y, vertex.attrib[10], R0;
DP3 result.texcoord[0].z, vertex.attrib[11], R0;
MOV result.texcoord[1], DTexCoord;
DP4 result.texcoord[1].x, vertex.attrib[8], program.env[10];
DP4 result.texcoord[1].y, vertex.attrib[8], program.env[11];
MOV result.texcoord[2], DTexCoord;
DP4 result.texcoord[2].x, vertex.position, program.env[9];
DP4 result.texcoord[3].x, vertex.position, program.env[6];
DP4 result.texcoord[3].y, vertex.position, program.env[7];
DP4 result.texcoord[3].w, vertex.position, program.env[8];
MOV result.texcoord[4], DTexCoord;
DP4 result.texcoord[4].x, vertex.attrib[8], program.env[12];
DP4 result.texcoord[4].y, vertex.attrib[8], program.env[13];
MOV result.texcoord[5], DTexCoord;
DP4 result.texcoord[5].x, vertex.attrib[8], program.env[14];
DP4 result.texcoord[5].y, vertex.attrib[8], program.env[15];

SUB R0, program.env[4], vertex.position;
DP3 R1, R0, R0;
RSQ R1, R1.x;
MUL R0, R0, R1.x;
SUB R1, program.env[5], vertex.position;
DP3 R2, R1, R1;
RSQ R2, R2.x;

#MAD R0, R1, R2.x, R0;
MUL R1, R1, R2.x;
ADD R0, R0, R1;

DP3 result.texcoord[6].x, vertex.attrib[9], R0;
DP3 result.texcoord[6].y, vertex.attrib[10], R0;
DP3 result.texcoord[6].z, vertex.attrib[11], R0;

SUB R1, program.env[5], vertex.position;

DP3 result.texcoord[7].x, vertex.attrib[9], R1;
DP3 result.texcoord[7].y, vertex.attrib[10], R1;
DP3 result.texcoord[7].z, vertex.attrib[11], R1;

MOV result.texcoord[7].w, 1.0;

#MAD result.color.xyz, vertex.color, program.env[16], program.env[17];
MAD result.color.xyz, vertex.color, program.env[16].x, program.env[16].y;

END

!!ARBfp1.0 
OPTION ARB_precision_hint_nicest;

TEMP svet, color, R1, R2, lN, otr, hd, vis, txc, bpc, spc, spl;

PARAM sbb = { -1, -1, -1, -1 };
PARAM sll = { 2, 2, 2, 2 };
#PARAM sbb = { -2, -2, -2, -2 };
#PARAM sll = { 4, 4, 4, 4 };

#PARAM	specExp = { 16, 0, 0, 0 };
PARAM	specExp = { 32, 0, 0, 0 };

#PARAM	xero = { 0, 0, 0, 1 };

PARAM	saab = { 0, 0, 0, 0.35 };	# 0.5 specular AA bias

DP3 R1, fragment.texcoord[7], fragment.texcoord[7];
RSQ R1, R1.x;
MUL hd, fragment.texcoord[7], R1;

TEX vis, fragment.texcoord[1], texture[1], 2D;
MAD vis.x, vis.z, -0.0375, 0.0375;

MAD bpc, vis.xxxx, hd, fragment.texcoord[1];
MAD txc, vis.xxxx, hd, fragment.texcoord[4];
MAD spc, vis.xxxx, hd, fragment.texcoord[5];
MAD spl, vis.xxxx, hd, fragment.texcoord[6];

DP3 otr, spl, spl;
RSQ otr, otr.x;
MUL otr.xyz, otr.x, spl;

#TEX svet, fragment.texcoord[0], texture[0], CUBE;
#MAD svet, svet, sll, sbb;

# instead of using the normalization cube map, normalize with math
DP3	svet, fragment.texcoord[0], fragment.texcoord[0];
RSQ	svet, svet.w;
MUL	svet.xyz, svet.w, fragment.texcoord[0];

TEX lN, bpc, texture[1], 2D;
MOV lN.x, lN.a;
MOV lN.z, 0.75;
MAD lN, lN, sll, sbb;
#DP3 svet, svet, lN;

# localNormal filter
ADD	R1, bpc, saab;
TXB	R2, R1, texture[1], 2D;
MOV 	R2.x, R2.a;
MAD	R2, R2, sll, sbb;

ADD	lN, lN, R2;
MUL	lN, lN, 0.35;

# localNormal (normalize)
DP3	R1.w, lN, lN;
RSQ	R1.w, R1.w;
MUL	lN.xyz, R1.w, lN;

# -------- diffuse map modulated by light color -- #

TEX	R1, txc, texture[4], 2D;
MUL	color.xyz, R1, program.env[0];

# ------------------------------------- reflect -- #

# reflect light from localNormal

DP3	R2.w, lN, lN;
DP3	R2.x, lN, svet;
MUL	R2.x, R2, 2.10;		# 2.0

# divide by mul
RCP	R1.w, R2.w;
MUL	R2.x, R2.x, R1.w;

MAD	R1.xyz, R2.xxxx, lN, -svet;

# ------------------------------------------------ #

# otr.w is the specular volume
DP3	otr.w, R1, otr;
#SUB	otr.w, otr, clamp.w;

DP3	svet, svet, lN;
#SUB_SAT svet, svet, clamp.w;

# --------------------------------------------- #

TXP R1, fragment.texcoord[3], texture[3], 2D;
MUL svet, svet, R1;

# boost projection, specular light reaches further - DFM added
ADD_SAT	R1, R1, R1;
MUL	R2, R2, R1;

TXP R1, fragment.texcoord[2], texture[2], 2D;
MUL svet, svet, R1;

TEX R1, txc, texture[4], 2D;
MUL color.xyz, R1, program.env[0];

DP3_SAT otr, otr, lN;

# modulate falloff by diffusemap alpha
RCP   R2, R1.w;
MUL   R2, specExp, R2;

# perform a dependent table read for the specular falloff
#TEX R1, otr, texture[6], 2D;

# perform a dependent table read for the specular falloff
##TEX	R1, otr, texture[6], 2D;
POW_SAT   R1, otr.x, R2.x;
#POW_SAT R1, otr.x, specExp.x;
#MAD_SAT R1, otr, 18.0, -16.5;	#ALTERNATIVO CON ACCENTUAZIONE SPECULARE
#MAD_SAT R1, otr, 10.0, -8.5;
#MAD_SAT R1, otr, 4.0, -3.0;	#VERSIONE DI HUMUS ESTREMAMENTE SIMILE ALL'ORIGINALE
MUL R1, R1, R1;

MUL R1, R1, program.env[1];

TEX R2, spc, texture[5], 2D;
#MUL R2, R2, 1.4;
ADD R2, R2, R2;

MAD color.xyz, R1, R2, color;
MUL color.xyz, svet, color;

MUL result.color.xyz, color, fragment.color;

END
