!!ARBvp1.0 OPTION ARB_position_invariant ;

# VPROG_INTERACTION
#
# input:
#
# attrib[8] TEX0	texture coordinates
# attrib[9] TEX1	normal
# attrib[10] TEX2	tangent[0]
# attrib[11] TEX3	tangent[1]
# COL			vertex color
#
# c[4]	localLightOrigin
# c[5]	localViewOrigin
# c[6]	lightProjection S
# c[7]	lightProjection T
# c[8]	lightProjection Q
# c[9]	lightFalloff S
# c[10]	bumpMatrix S
# c[11]	bumpMatrix T
# c[12]	diffuseMatrix S
# c[13]	diffuseMatrix T
# c[14]	specularMatrix S
# c[15]	specularMatrix T
# c[16]	vertex color modulate
# c[17]	vertex color add
#
# output:
#
# texture 0 is the cube map
# texture 1 is the per-surface bump map
# texture 2 is the light falloff texture
# texture 3 is the light projection texture
# texture 4 is the per-surface diffuse map
# texture 5 is the per-surface specular map
# texture 6 is the specular lookup table

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

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

# generate the vertex color, which can be 1.0, color, or 1.0 - color
# for 1.0 : env[16] = 0, env[17] = 1
# for color : env[16] = 1, env[17] = 0
# for 1.0-color : env[16] = -1, env[17] = 1

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;

# texture 0 is the cube map
# texture 1 is the per-surface bump map
# texture 2 is the light falloff texture
# texture 3 is the light projection texture
# texture 4 is the per-surface diffuse map
# texture 5 is the per-surface specular map
# texture 6 is the specular lookup table

# env[0] is the diffuse modifier
# env[1] is the specular modifier

TEMP	toLight, light, toViewer, halfAngle, color, R1, R2, localNormal, newtexcoord;

PARAM	subOne = { -1, -1, -1, -1 };
PARAM	scaleTwo = { 2, 2, 2, 2 };
#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.5, -1.5, -1.5, -1.5 };
#PARAM   scaleTwo = { 3, 3, 3, 3 };
#PARAM   subOne = { -1.125, -1.125, -1.125, -1.125 };
#PARAM   scaleTwo = { 2.25, 2.25, 2.25, 2.25 };

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

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

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

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

# normalize the direction to the viewer
DP3		toViewer, fragment.texcoord[6],fragment.texcoord[6];
RSQ		toViewer, toViewer.w;
MUL		toViewer.xyz, toViewer.w, fragment.texcoord[6];

#TEX		toViewer, fragment.texcoord[6], texture[0], CUBE;
#MAD		toViewer, toViewer, scaleTwo, subOne;

# load heightmap
TEX		R1, fragment.texcoord[1], texture[7], 2D;
MAD		R1, R1, program.local[0].x, program.local[0].y; # scale and bias
MAD		newtexcoord, R1, toViewer, fragment.texcoord[1];

# load the filtered normal map
TEX		localNormal, newtexcoord, texture[1], 2D;
MOV		localNormal.x, localNormal.a;
#MOV   localNormal.z, 0.99;
MAD		localNormal.xyz, localNormal, scaleTwo, subOne;

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

# normalize localNormal
DP3_SAT   localNormal.w, localNormal, localNormal;
RSQ   localNormal.w, localNormal.w;
MUL   localNormal, localNormal, localNormal.w;

# localNormal filter
#MOV	R1.xyz, localNormal;
#MOV	R1.z, { 0, 0, 0, 0.5 };	#0.0;
#DP3_SAT	R1, R1, R1;
#MAD_SAT	R1.z, R1.w, -1, 1;
#RSQ	R1, R1.z;
#RCP	localNormal.z, R1.w;

# localNormal filter
#MOV		localNormal.z, { 0, 0, 0, 0.5 }; #0.0;
#DP3_SAT	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, newtexcoord, { 0, 0, 0, 0.5 };
TXB	R2, R1, texture[1], 2D;
MOV R2.x, R2.a;
MAD_SAT	R2, R2, scaleTwo, subOne;

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

# flatten normal over distance (Z)
MAD_SAT	R1.w, fragment.position.z, 4.0, -3.5;
MAX	R1.w, R1.w, { 0, 0, 0, 0.5 }; #0.0;
ADD_SAT	localNormal.z, localNormal.z, R1.w;

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

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

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

# diffuse dot product
DP3_SAT		light, toLight, localNormal;

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

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

# modulate by the diffuse map and constant diffuse factor
TEX		color, newtexcoord, texture[4], 2D;
#MUL		color, R1, program.env[0];

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

# calculate specular
DP3_SAT	R1.w, halfAngle, localNormal;

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

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

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

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

# use power instead of table
#POW		R1.w, R1.w, program.local[1].x;
#POW_SAT		R1.w, R1.w, specExp.x;

# modulate by the constant specular factor
#MUL	R1.w, R1, program.env[1];

# modulate by the specular map * 2
TEX	R2, newtexcoord, texture[5], 2D;
ADD	R2, R2, R2;

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

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

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

# modify by the vertex color
MUL result.color.xyz, color, fragment.color;

END

