!!ARBvp1.0 OPTION ARB_position_invariant ;

# Parallax Mapping Engine 2.0
# by Junkguy
# junkguy@gmail.com
# www.junkguy.org
# Released on October 29, 2005

TEMP		R0, R1, R2;

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

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];
DP4		result.texcoord[4].x, vertex.attrib[8], program.env[12];
DP4		result.texcoord[4].y, vertex.attrib[8], program.env[13];
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		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;

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

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;

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

END

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

!!ARBfp1.0
OPTION ARB_precision_hint_nicest;
OPTION NV_fragment_program2;

TEMP	toLight, light, toViewer, halfAngle, color, R1, R2, R3, localNormal, specular;
SHORT TEMP newtexcoord, newbumpcoord, newspeccoord, newspeclook, newheighttexcoord;

PARAM	subOne = { -1, -1, -1, -1 };
PARAM	scaleTwo = { 2, 2, 2, 2 };
#PARAM	subOne = { -2, -2, -3, -3 };
#PARAM	scaleTwo = { 4, 4, 4, 4 };
#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 = { 64, 0, 0, 0 };
PARAM	specExp = { 96, 0, 0, 0 };

#PARAM	pointFive = { 0.5, 0.5, 0.5, 0.5 };

PARAM	GlobalLight = 0.9;	#1.0
PARAM	ZeroFive = 0.5;
PARAM   scaleTwoX = { 4, 4, 4, 4 };
PARAM   subOneX = { -3.5, -3.5, -3.5, -3.5 };
PARAM   scaleTwoY = { -1, -1, -1, -1 };
PARAM   subOneY = { 1, 1, 1, 1 };

#DP3H	specular, fragment.texcoord[6], fragment.texcoord[6];
#RSQH	specular, specular.w;
#MULH	specular.xyz, specular.w, fragment.texcoord[6];

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

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

# load heightmap
TXL		R1, fragment.texcoord[1], texture[7], 2D;
MADH	R1, R1, program.local[0].x, program.local[0].y; # scale and bias
MADH	newheighttexcoord.xyz, R1, specular, fragment.texcoord[1];

DP3H	R1, fragment.texcoord[7], fragment.texcoord[7];
RSQH	R1, R1.w;
MULH	R1.xyz, R1.w, fragment.texcoord[7];

#TXL		R1, fragment.texcoord[7], texture[7], CUBE;
#MADH		R1.xyz, R1, scaleTwo, subOne;

TXL		R2, newheighttexcoord, texture[1], 2D;
MADH	R2.x, R2.z, 0.0525, -0.0525;	#0.0425, -0.0425;	#0.0375, -0.0375;

MADH	newbumpcoord.xyz, R2.x, R1, newheighttexcoord;
MADH	newtexcoord.xyz, R2.x, R1, fragment.texcoord[4];
MADH	newspeccoord.xyz, R2.x, R1, fragment.texcoord[5];
MADH	newspeclook.xyz, R2.x, R1, specular;

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

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

#NRMH light.xyz, fragment.texcoord[0];

# normalize the direction to the light
#DP3H		toLight, fragment.texcoord[0],fragment.texcoord[0];
#RSQH		toLight, toLight.x;
#MULH		toLight.xyz, toLight.x, fragment.texcoord[0];

##TXL		toLight, fragment.texcoord[0], texture[0], CUBE;
##MADH		toLight.xyz, toLight, scaleTwo, subOne;

# normalize the direction to the viewer
#DP3H		toViewer, newspeclook, newspeclook;
#RSQH		toViewer, toViewer.x;
#MULH		toViewer.xyz, toViewer.x, newspeclook;

##TXL		toViewer, newspeclook, texture[0], CUBE;
##MADH		toViewer.xyz, toViewer, scaleTwo, subOne;

NRMH toLight.xyz, fragment.texcoord[0];
NRMH toViewer.xyz, newspeclook;

TXL	localNormal, fragment.texcoord[1], texture[1], 2D;
MOVH	localNormal.x, localNormal.a;
MOVH	localNormal.z, 0.99;
MADH	localNormal.xyz, localNormal, scaleTwo, subOne;

MULH localNormal.zw, localNormal.xyxy, localNormal.xyxy;
ADDH_SAT localNormal.z, localNormal, localNormal.w;
ADDH localNormal.z, -localNormal, GlobalLight;
RSQH localNormal.z, localNormal.z;
RCPH localNormal.z, localNormal.z;
RFLH_SAT R3, localNormal, light;
MULH R3, R3, GlobalLight; # dfm added

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

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

MOVH	R1.xyz, localNormal;
MOVH	R1.z, ZeroFive;
DP3H_SAT	R1, R1, R1;
MADH_SAT	R1.z, R1.w, scaleTwoY, subOneY;
RSQH	R1, R1.z;
RCPH	localNormal.z, R1.w;

# localNormal filter
#MOVH		localNormal.z, ZeroFive;
#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], ZeroFive;
TXL	R2, R1, texture[1], 2D;	#TXB
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_SAT	R1, fragment.position.z, scaleTwoX, subOneX;
MAXH	R1, R1.w, ZeroFive;
ADDH_SAT	localNormal.z, localNormal.z, R1.w;

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

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

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

DP3H_SAT R3.x, toViewer, R3;
POWH_SAT R3.x, R3.x, 16.0;

DP3H_SAT	light, toLight, localNormal;

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;

TXP	R1, fragment.texcoord[2], texture[2], 2D;
MULH	light.xyz, light, R1;

TXL	R1, newtexcoord, 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;
# perform the specular bump mapping
DP3H_SAT toViewer.xyz, toViewer, localNormal;

# calculate the half angle vector and normalize
ADDH		halfAngle, toLight, toViewer;
DP3H		R1, halfAngle, halfAngle;
RSQH		R1, R1.x;
MULH		halfAngle.xyz, halfAngle, R1;

# calculate specular
DP3H_SAT	R1, halfAngle, 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;

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

# perform a dependent table read for the specular falloff
##TXL	R1, R1.x, texture[6], 2D;

# calculate specular falloff with maths
POWH_SAT   R1, R1.x, R2.x;
#POWH_SAT R1, R1.x, specExp.x;
#MADH_SAT R1, R1.x, 18.0, -16.5;	#ALTERNATIVO CON ACCENTUAZIONE SPECULARE
#MADH_SAT R1, R1.x, 10.0, -8.5;
#MADH_SAT R1, R1.x, 4.0, -3.0;	#VERSIONE DI HUMUS ESTREMAMENTE SIMILE ALL'ORIGINALE
#MULH R1, R1, R1.x;

# use power instead of table
#POWH		R1, R1.x, R2.a;

# use power instead of table
#POWH		R1, R1.x, specExp.x;
#MULH		R1.xyz, R1, 2.0;

#SUBH_SAT R1.w, toViewer.w, 0.24;
#MULH R1.w, R1.w, R1.w;
#MULH_SAT R1.w, R1.w, 16.0;	#8.0

MADH_SAT R1, toViewer, R3.w, R1;
MADH_SAT R1, R3.w, 0.32, R1;

# no color please!
#ADDH	R1.rgb, R1.gggg, R1.gggg;

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

TXL	R2, newspeccoord, texture[5], 2D;
#TXL	R2, fragment.texcoord[1], texture[5], 2D;
#MULH	R2, R2, 1.4;
#ADDH	R2, R2, 0.121875;
#ADDH	R2, R2, 0.2575;
#ADDH	R2, R2, 0.01875;
#ADDH	R2, 0.7, 0.175;
ADDH	R2, R2, R2;

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

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

# global illumination trick
MULH	R1, localNormal.z, GlobalLight;
MULH	R1, R1, R1;
MULH	color.xyz, color, R1.w;

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

END

