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

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;

# 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

#=======================================================================
# Brilliant highlights ver 0.3.1a			       9.11.2006
#=======================================================================
#
# Version history
# ---------------
#
# New version numbering;
#	0.1.x - per vertex half-angle vector
#	0.2.x - per pixel reflect vector
#	0.3.x - per pixel half-angle vector
#
# 0.3.1 # HQ per pixel half-angle vector model
#	  Backlights
#	  Normal map filtering
#	  "Pocket" shadows (a static effect)
#
#-----------------------------------------------------------------------

!!ARBfp1.0
OPTION ARB_precision_hint_nicest;
#OPTION NV_fragment_program2;

# 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, backlight, color, R1, R2, R3, localNormal, specular, mod1, newtexcoord;	#, halfAngle;

#PARAM	subOne = { -2, -2, -2, -2 };
#PARAM	scaleTwo = { 4, 4, 4, 4 };
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	xero = { 0, 0, 0, 1 };
#PARAM	saab = { 0, 0, 0, 0.5 };	# specular AA bias

#PARAM specExp = { 32, 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   scaleTwoY = { -1, -1, -1, -1 };
PARAM   subOneY = { 1, 1, 1, 1 };

# ----------------------------------- normalize -- #

# specular is actually now viewer
#DP3	specular, fragment.texcoord[6], fragment.texcoord[6];
#RSQ	specular, specular.w;
#MUL	specular.xyz, specular.w, fragment.texcoord[6];

# light
#DP3	light, fragment.texcoord[0], fragment.texcoord[0];
#RSQ	light, light.w;
#MUL	light.xyz, light.w, fragment.texcoord[0];

# 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		newtexcoord, R1, specular, fragment.texcoord[1];

# 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, fragment.texcoord[6],fragment.texcoord[6];
RSQ		toViewer, toViewer.x;
MUL		toViewer.xyz, toViewer.x, fragment.texcoord[6];

#TXB		toViewer, fragment.texcoord[6], texture[0], CUBE;
#MAD		toViewer.xyz, toViewer, 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		newtexcoord, R1, toViewer, fragment.texcoord[1];

# localNormal (load)
TXB	localNormal, newtexcoord, texture[1], 2D;
MOV localNormal.x, localNormal.a;
#MOV	localNormal.z, 0.99;
MAD	localNormal, localNormal, scaleTwo, subOne;

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_SAT   localNormal.w, localNormal, localNormal;
RSQ   localNormal.w, localNormal.w;
MUL   localNormal, localNormal, localNormal.w;

# localNormal filter
#MOV	R1.xyz, localNormal;
#MOV	R1.z, ZeroFive;	#0.0;
#DP3_SAT	R1, R1, R1;
#MAD_SAT	R1.z, R1.w, scaleTwoY, subOneY;	#-1, 1;
#RSQ	R1, R1.z;
#RCP	localNormal.z, R1.w;

# localNormal filter
#MOV		localNormal.z, ZeroFive; #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, ZeroFive;
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, scaleTwoX, subOneX;	#4.0, -3.5;
MAX	R1.w, R1.w, ZeroFive; #0.0;
ADD_SAT	localNormal.z, localNormal.z, R1.w;

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

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

# specular.w is the specular volume
#DP3_SAT R2.x, toViewer, R2;
#POW_SAT R2.x, R2.x, 16.0;	#8.0

# --------------------------------------- dots -- #

# produce diffuse falloff
DP3_SAT	light.w, toLight, localNormal;

# half angle makes a return... normalized halfangle
#ADD	R2.xyz, toViewer, toLight;	#, light;
#DP3	R1.xyz, R2, R2;
#RSQ	R1.w, R1.x;
#MUL	R2.xyz, R2, R1.w;

# specular falloff (raw)
#DP3_SAT	toViewer.w, R2, localNormal;

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

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

# perform the specular bump mapping
DP3_SAT toViewer.w, R2, localNormal;

ADD_SAT	backlight.w, -light.w, 0.33;		# backlight scatter
MUL	backlight.w, backlight.w, 0.15;		# backlight fade

# -------------------------- modulate diffuse  -- #

# modulate by the light projection
TXP	R2.rgb, fragment.texcoord[3], texture[3], 2D;
MUL	mod1.rgb, program.env[0], R2;

# modulate by diffuse map
#TXB	R1.rgb, fragment.texcoord[4], texture[4], 2D;
TXB	R1.rgb, newtexcoord, texture[4], 2D;
#TXB	color.rgb, newtexcoord, texture[4], 2D;
MUL	color.rgb, mod1, R1;

# diffuse to backlight
MUL	backlight.rgb, R1, backlight.w;
MUL_SAT	R2.rgb, mod1, 3.0;
MUL	backlight.rgb, R2, backlight;

MUL_SAT	color.rgb, color, light.w;

# ----------------------------------- specular -- #

# R3 is the specular map (* 2)
#TXB	R3.rg, fragment.texcoord[5], texture[5], 2D;
TXB	R3, newtexcoord, texture[5], 2D;

# no color please ?
ADD	R3.rg, R3, R3;

# MIN + MAX power to R1.w (modulated by specular map)
MAD	R1.w, R3.y, 48.0, 16.0;		#, maxpow, minpow;

# pow specular
POW_SAT	R2.rgb, toViewer.w, R1.w;

# mask specular with specular map
MUL	R2.rgb, R2, R3.x; # x is mask, y is pow

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

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

MAD_SAT R1, toViewer, R2.x, R1;
MAD_SAT R1, R2.x, 0.32, R1;		#, 0.85, R1;

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

# modulate specular
MUL	R2.rgb, R2, mod1;

# modulate by falloff (look up)
TXP	R1.r, fragment.texcoord[2], texture[2], 2D;
MUL	color.rgb, color, R1.r;

# add specular & backlight
ADD	color.rgb, color, backlight;
ADD	color.rgb, color, R2;

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

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

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

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

END


