!!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 };


# calculate vector to light in R0
SUB		R0.xyz, program.env[4], vertex.position;

# put into texture space for TEX0
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;

# textures 1 takes the base coordinates by the texture matrix
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];

# texture 2 has one texgen
MOV		result.texcoord[2].xy, defaultTexCoord;
DP4		result.texcoord[2].x, vertex.position, program.env[9];

# texture 3 has three texgens
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];

# textures 4 takes the base coordinates by the texture matrix
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];

# textures 5 takes the base coordinates by the texture matrix
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];

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

# calculate normalized vector to viewer in R1
SUB		R1.xyz, program.env[5], vertex.position;

# put viewer to texture space
DP3		result.texcoord[6].x, vertex.attrib[9], R1;
DP3		result.texcoord[6].y, vertex.attrib[10], R1;
DP3		result.texcoord[6].z, vertex.attrib[11], R1;

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;

DP3		R2, R1, R1;
RSQ		R2, R2.x;

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

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

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

#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;

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

#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   subOne = { -1.0625, -1.0625, -1.0625, -1.0625 };
PARAM   scaleTwo = { 2.125, 2.125, 2.125, 2.125 };

PARAM	specExp = { 24, 0, 0, 0 };
#PARAM	specExp = { 16, 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   scaleTwoX = { 8, 8, 8, 8 };
PARAM   subOneX = { -7.5, -7.5, -7.5, -7.5 };
#PARAM   scaleTwoY = { -1, -1, -1, -1 };
#PARAM   subOneY = { 1, 1, 1, 1 };
PARAM   scaleTwoY = -1;
PARAM   subOneY = 1;

DP3	specular, fragment.texcoord[6], fragment.texcoord[6];
RSQ	specular, specular.w;
MUL	specular.xyz, specular.w, fragment.texcoord[6];

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

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

DP3	R1, fragment.texcoord[7], fragment.texcoord[7];
RSQ	R1, R1.w;
MUL	R1.xyz, R1.w, fragment.texcoord[7];

#TXB		R1, fragment.texcoord[7], texture[7], CUBE;
#MAD		R1.xyz, R1, scaleTwo, subOne;

TXB	R2, newheighttexcoord, texture[1], 2D;
MAD	R2.x, R2.z, 0.065, -0.065;	#, 0.0525, -0.0525;	#0.0425, -0.0425;	#0.0375, -0.0375;

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

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

##TXB	light, fragment.texcoord[0], texture[0], CUBE;
##MAD	light.xyz, light, scaleTwo, subOne;

# normalize the direction to the light
DP3		toLight, fragment.texcoord[0],fragment.texcoord[0];
RSQ		toLight, toLight.x;
MUL		toLight.xyz, toLight.x, fragment.texcoord[0];

#TXB		toLight, fragment.texcoord[0], texture[0], CUBE;
#MAD		toLight.xyz, toLight, scaleTwo, subOne;

# normalize the direction to the viewer
DP3		toViewer, newspeclook, newspeclook;
RSQ		toViewer, toViewer.x;
MUL		toViewer.xyz, toViewer.x, newspeclook;

#TXB		toViewer, newspeclook, texture[0], CUBE;
#MAD		toViewer.xyz, toViewer, scaleTwo, subOne;

TXB	localNormal, newbumpcoord, texture[1], 2D;
MOV	localNormal.x, localNormal.a;
MOV	localNormal.z, 0.99;
MAD	localNormal.xyz, localNormal, scaleTwo, subOne;
DP3   localNormal.w, localNormal, localNormal;
RSQ   localNormal.w, localNormal.w;
MUL   localNormal.xyz, localNormal.w, localNormal;
MUL localNormal.zw, localNormal.xyxy, localNormal.xyxy;

ADD_SAT localNormal.z, localNormal, localNormal.w;
ADD localNormal.z, -localNormal, GlobalLight;
RSQ localNormal.z, localNormal.z;
RCP localNormal.z, localNormal.z;
MUL R2, R2, GlobalLight; # dfm added

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

# normalize localNormal
#DP3   localNormal.w, localNormal, localNormal;
#RSQ   localNormal.w, localNormal.w;
#MUL   localNormal.xyz, localNormal, localNormal.w;

MOV	R1.xyz, localNormal;
MOV	R1.z, ZeroFive;
DP3	R1, R1, R1;
MAD_SAT	R1.z, R1.w, scaleTwoY, subOneY;
RSQ	R1, R1.z;
RCP	localNormal.z, R1.w;

# localNormal filter
#MOV		localNormal.z, ZeroFive;
#DP3	R1.x, localNormal,localNormal;
#ADD_SAT	R1.x, 1, -R1.x;
#RSQ		R1.x, R1.x;
#RCP		localNormal.z, R1.x;

# localNormal filter
#ADD_SAT	R1, newbumpcoord, ZeroFive;
ADD_SAT	R1, newheighttexcoord, ZeroFive;
TXB	R2, R1, texture[1], 2D;
MOV R2.x, R2.a;
MAD_SAT	R2, R2, scaleTwoX, subOneX;

#ADD	localNormal, localNormal, R2;
#MUL	localNormal, localNormal, 0.5;

# flatten normal over distance (Z)
MAD_SAT	R1, fragment.position.z, scaleTwoX, subOneX;
MAX	R1, R1.w, ZeroFive;
ADD_SAT	localNormal.z, localNormal.z, R1.w;

# normalize the bumpmap
DP3		R1, localNormal,localNormal;
RSQ		R1, R1.x;
MUL		localNormal.xyz, localNormal, R1;

#TXB		localNormal, localNormal, texture[0], CUBE;
#MAD		localNormal, localNormal, scaleTwo, subOne;

# normalize the bumpmap
#DP3		R1, localNormal,localNormal;
#RSQ		R1, R1.w;
#MUL		localNormal.xyz, localNormal, R1;

#DP3 R2.x, toViewer, R2;
#POW_SAT R2.x, R2.x, 16.0;

DP3	light, toLight, localNormal;

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

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

TXP	R2, fragment.texcoord[2], texture[2], 2D;
MUL	light.xyz, light, R2;

#TXB	R1, newtexcoord, texture[4], 2D;
TXB		color.xyz, newbumpcoord, texture[4], 2D;
#MUL	color.xyz, R1, program.env[0];

# calculate the half angle vector and normalize
ADD		R2.xyz, toLight, toViewer;
DP3		R1.xyz, R2, R2;
RSQ		R1, R1.x;
MUL		R2.xyz, R2, R1.x;

# calculate specular
DP3	R1, R2, localNormal;

# perform the specular bump mapping
#DP3 specular.xyz, R2, localNormal;
DP3 toViewer.xyz, R2, localNormal;

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

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

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

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

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

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

# use power instead of table
#POW		R1, R1.x, specExp.x;
#MUL		R1.xyz, R1, 2.0;

#SUB_SAT R1.w, toViewer.w, 0.24;
#MUL R1.w, R1.w, R1.w;
#MUL_SAT R1.w, R1.w, 16.0;	#8.0

MAD_SAT R1, toViewer, R2.w, R1;
MAD_SAT R1, R2.w, 0.32, R1;

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

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

#TXB	R2, newspeccoord, texture[5], 2D;
TXB	R2, newbumpcoord, texture[5], 2D;
#MUL	R2, R2, 1.4;
#ADD	R2, R2, 0.121875;
#ADD	R2, R2, 0.2575;
#ADD	R2, R2, 0.01875;
#ADD	R2, 0.7, 0.175;
ADD	R2, R2, R2;

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

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

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

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

END

