!!ARBfp1.0

#/*ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ*\
#	File:			Program for CXR_Shader::RenderShading_FP20
#					
#	Author:			Magnus Högdahl
#					
#	Copyright:		Starbreeze AB 2004
#					
#	History:
#
#\*____________________________________________________________________________________________*/


#-----------------------------------------
#Texture0 = Deferred Diffuse Map
#Texture1 = Deferred Specular map
#Texture2 = Deferred Normal map
#Texture3 = [Projection Map]

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

ATTRIB MappingTexCoord = fragment.texcoord[0];
ATTRIB WSPixelPosition = fragment.texcoord[1];
ATTRIB WSSurfNormal = fragment.texcoord[2];
ATTRIB WSSurfTangU = fragment.texcoord[3];
ATTRIB WSSurfTangV = fragment.texcoord[4];
ATTRIB DeferredTexCoord = fragment.texcoord[5];

OUTPUT oCol = result.color;

ATTRIB vCol = fragment.color;


PARAM WSEyePosition = program.env[0];	# { X, Y, Z, 0 }
PARAM WSLightPosition = program.env[1];	# { X, Y, Z, 0 }
PARAM LightRange = program.env[2];	# { 1.0 / Range, Range, 1.0 / Range^2, RangeExp }
PARAM DiffColor = program.env[3];	# { R, G, B, 0 }
PARAM SpecColor = program.env[4];	# { R, G, B, SpecPower }

PARAM const_val = { 0.5, 1.0, 2.0, 4.0 };
PARAM const_val2 = { 0, 0.25, 4, 0 };

TEMP DiffuseTexel;
TEMP SpecularTexel;
TEMP TSNormal;
TEMP WSNormal;
TEMP WSLV;		# World space light vector
TEMP WSEV;		# Tangent space eye vector
TEMP WSReflection;
TEMP WSH;
TEMP r0;
TEMP r1;
TEMP r2;
TEMP Result;

#-----------------------------------------
# Fetch Textures
RCP r0.w, DeferredTexCoord.w;
MUL r0.xyz, DeferredTexCoord, r0.w;

TEX DiffuseTexel, r0, texture[0], 2D;
TEX SpecularTexel, r0, texture[1], 2D;
TEX TSNormal, r0, texture[2], 2D;

#-----------------------------------------
# Attenuation + WSLV Normalize
SUB WSLV.xyz, WSLightPosition, WSPixelPosition;
DP3 WSLV.w, WSLV, WSLV;
MUL_SAT r1.w, WSLV.w, LightRange.z;
ADD r1.w, const_val.g, -r1.w;
POW r1.w, r1.w, LightRange.w;
#MUL r1.w, r1.w, r1.w;
@if support_normalize
NRM WSLV.xyz, WSLV.xyz;
@else
RSQ WSLV.w, WSLV.w;
MUL WSLV.xyz, WSLV, WSLV.w;
@endif

#-----------------------------------------
# Transform normal to worldspace and normalize
MAD TSNormal.rgb, TSNormal, const_val.b, -const_val.g;	# Bias and scale the normalmap texel (only rgb) (from 0->1, -1->1)
MUL r0.xyz, TSNormal.x, WSSurfNormal;
MAD r0.xyz, TSNormal.y, WSSurfTangV, r0;
MAD r0.xyz, TSNormal.z, WSSurfTangU, r0;
@if support_normalize
NRM WSNormal.xyz, r0.xyz;
@else
DP3 WSNormal.w, r0, r0;
RSQ WSNormal.w, WSNormal.w;
MUL WSNormal.rgb, r0, WSNormal.w;
@endif

#-----------------------------------------
# Normalize WSEV
SUB WSEV.xyz, WSEyePosition, WSPixelPosition;
@if support_normalize
NRM WSEV.xyz, WSEV.xyz;
@else
DP3 WSEV.w, WSEV, WSEV;
RSQ WSEV.w, WSEV.w;
MUL WSEV.xyz, WSEV, WSEV.a;
@endif

#-----------------------------------------
# Calc halfangle vector
ADD WSH.xyz, WSEV, WSLV;
@if support_normalize
NRM WSH.xyz, WSH.xyz;
@else
DP3 WSH.a, WSH, WSH;
RSQ WSH.a, WSH.a;
MUL WSH.xyz, WSH, WSH.a;
@endif

#-----------------------------------------
# Calc reflection vector
# DP3 r0.a, WSNormal, WSEV;
# ADD r0.a, r0.a, r0.a;
# MAD WSReflection.xyz, WSNormal, r0.a, -WSEV;

#-----------------------------------------
# Self shadowing
DP3 r0.a, WSLV, WSSurfNormal;			# WSSurfNormal isn't normalized but that should be ok here
SUB r0.a, const_val2.y, -r0.a;
MUL_SAT r0.a, r0.a, const_val2.z;
MUL r1.w, r1.w, r0.a;

#-----------------------------------------
# Diffuse
MUL r2, DiffColor, DiffuseTexel;		# Multiply diffusemap with light color
DP3_SAT r0.rgb, WSNormal, WSLV;
MUL Result.rgb, r0, r2;				# Diffuse dotprod * Diffuse color
MOV Result.a, r2.a;

#-----------------------------------------
# Specular
MUL SpecularTexel, SpecularTexel, SpecColor;
MAX SpecularTexel.a, SpecularTexel.a, const_val.y;
DP3_SAT r1.x, WSNormal, WSH;
POW r1.x, r1.x, SpecularTexel.a;
MAD Result.rgb, SpecularTexel, r1.x, Result;
#MUL oCol, SpecularTexel, r1.x;
#MUL oCol, SpecularTexel, 0.25;


MUL Result.rgb, Result, r1.w;			# Result *= Proj

#-----------------------------------------
# Write result
MOV oCol, Result;

END
