!!ARBvp1.0
OPTION ARB_position_invariant;

TEMP R0, R1, R2;

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

SUB R0.xyz, 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].xy, defaultTexCoord;
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].xy, defaultTexCoord;
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].xy, defaultTexCoord;
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].xy, defaultTexCoord;
DP4 result.texcoord[5].x, vertex.attrib[8], program.env[14];
DP4 result.texcoord[5].y, vertex.attrib[8], program.env[15];

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

DP3 R1, R0, R0;
RSQ R1, R1.x;
MUL R0, R0, R1.x;

SUB R1.xyz, 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.xyz, 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].x, program.env[16].y;
MAD		result.color.xyz, vertex.color, program.env[16].x, program.env[17].y;

END

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

!!ARBfp1.0
OPTION ARB_precision_hint_nicest;
OPTION NV_fragment_program2;

#SHORT TEMP light, color, R1, R2, R3, R4, R5, localNormal, specular;
SHORT TEMP light, color, R1, R2, R3, localNormal, specular;
#TEMP view, view2, sample, height, R0, tmp, offset, Noffset, Toffset, Soffset, Eoffset;

#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.125, -1.125, -1.125, -1.125 };
#PARAM   scaleTwo = { 2.25, 2.25, 2.25, 2.25 };

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

# load the specular half angle first, because
# the ATI shader gives a "too many indirections" error
# if this is done right before the texture indirection

#-----------------
##TXL	specular, fragment.texcoord[6], texture[0], CUBE;	*ORIGINALE
##MADH	specular, specular, scaleTwo, subOne;			*ORIGINALE


# instead of using the normalization cube map, normalize with math
#DP3H		specular, fragment.texcoord[6],fragment.texcoord[6];
#RSQH		specular, specular.x;
#MULH		specular.xyz, specular.x, fragment.texcoord[6];
#-----------------


#
# the amount of light contacting the fragment is the
# product of the two light projections and the surface
# bump mapping
#

# perform the diffuse bump mapping

#-----------------
##TXL	light, fragment.texcoord[0], texture[0], CUBE;
##MADH	light, light, scaleTwo, subOne;

# instead of using the normalization cube map, normalize with math
#DP3H		light.w, fragment.texcoord[0],fragment.texcoord[0];
#RSQH		light.w, light.w;
#MULH		light.xyz, light.w, fragment.texcoord[0];
#-----------------

NRMH specular.xyz, fragment.texcoord[6];
NRMH light.xyz, fragment.texcoord[0];

#TXL localNormal, Noffset, texture[1], 2D;
TXL localNormal, fragment.texcoord[1], texture[1], 2D;
MOVH localNormal.x, localNormal.a;
#MOVH localNormal.z, 0.99;
MADH localNormal.xyz, localNormal, scaleTwo, subOne;
NRMH localNormal, localNormal;
MULH localNormal.zw, localNormal.xyxy, localNormal.xyxy;

ADDH_SAT localNormal.z, localNormal, localNormal.w;
ADDH_SAT localNormal.z, -localNormal, 1.0;
RSQH localNormal.z, localNormal.z;
RCPH localNormal.z, localNormal.z;
RFLH_SAT R3, localNormal, light;

#MULH R3, R3, 1.0; # dfm added

# soften
LRPH		localNormal.xyz, program.local[6].x, specular, localNormal;

# normalize localNormal
DP3H_SAT   localNormal.w, localNormal, localNormal;
RSQH   localNormal.w, localNormal.w;
MULH   localNormal, localNormal, localNormal.w;

# localNormal filter
MOVH	R1.xyz, localNormal;
MOVH	R1.z, 0.0;
DP3H_SAT	R1, R1, R1;
MADH_SAT	R1.z, R1.w, -1, 1;
RSQH	R1, R1.z;
RCPH	localNormal.z, R1.w;

# localNormal filter
#MOVH		localNormal.z, 0;
#DP3H_SAT	R1.x, localNormal,localNormal;
#ADDH_SAT	R1.x, 1, -R1.x;
#RSQH		R1.x, R1.x;
#RCPH		localNormal.z, R1.x;

# localNormal filter
ADDH_SAT	R1, fragment.texcoord[1], { 0, 0, 0, 0.5 };
TXB	R2, R1, texture[1], 2D;
MOVH R2.x, R2.a;
MADH_SAT	R2, R2, scaleTwo, subOne;

#ADDH	localNormal, localNormal, R2;
#MULH	localNormal, localNormal, 0.5;

# flatten normal over distance (Z)
MADH	R1.w, fragment.position.z, 4.0, -3.5;
MAXH	R1.w, R1.w, 0.0;
ADDH	localNormal.z, localNormal.z, R1.w;

# normalize the bumpmap
#DP3H		R1.w, localNormal,localNormal;
#RSQH		R1.w, R1.w;
#MULH		localNormal.xyz, localNormal, R1.w;

#TXL		localNormal, localNormal, texture[0], CUBE;
#MADH		localNormal, localNormal, scaleTwo, subOne;

# localNormal (normalize)
DP3H	R1.w, localNormal, localNormal;
RSQH	R1.w, R1.w;
MULH	localNormal.xyz, R1.w, localNormal;

# specular.w is the specular volume
DP3H_SAT R3.x, specular, R3;
POWH_SAT R3.x, R3.x, 8.0;

DP3H_SAT light.w, light, localNormal;

# modulate by the light projection
TXP R1, fragment.texcoord[3], texture[3], 2D;
MULH light.xyz, light, R1;

# boost projection, specular light reaches further - DFM added
#ADDH_SAT	R1.xyz, R1, R1;
#MULH	R2.xyz, R2, R1;

# modulate by the light falloff
TXP R1, fragment.texcoord[2], texture[2], 2D;
MULH light.xyz, light, R1;

#
# the light will be modulated by the diffuse and
# specular surface characteristics
#

# modulate by the diffuse map and constant diffuse factor
#TXL R1, Toffset, texture[4], 2D;
#TXL R1, fragment.texcoord[1], texture[4], 2D;
TXL		color.xyz, fragment.texcoord[1], texture[4], 2D;
#MULH color.xyz, R1, program.env[0];

# perform the specular bump mapping
DP3H_SAT specular.xyz, specular, localNormal;

# modulate falloff by diffusemap alpha
RCPH   R2, R1.w;
MULH   R2, specExp, R2;

# use power instead of table
#POWH		R1, R1.x, program.env[2].x;
#MULH		R1, R1, program.env[2].y;

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

#SUBH_SAT R1.x, specular.x, 0.75;
#MULH R1.x, R1.x, R1.x;
#MULH_SAT R1.x, R1.x, 16.0;

MADH_SAT R1, specular, R3.x, R1;
MADH_SAT R1, R3.x, 0.85, R1;

MULH R1.xyz, R1, program.env[1];

#TXL R2, Soffset, texture[5], 2D;
TXL R2, fragment.texcoord[1], texture[5], 2D;
#MULH R2, R2, 1.4;
ADDH R2, R2, R2;

MADH color.xyz, R1, R2, color;
MULH color.xyz, light, color;

# light color
MULH color.xyz, color, program.env[0];

MULH result.color.xyz, color, fragment.color;

END
