!!ARBfp1.0

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


# LightIntensity = ((Diffuse.rgb + FresnelFactor * Specular.rgb) * SilkFactor + Transmission.rgb) * DistanceAttenuation * Projection

# Diffuse = DiffuseMap.rgb * DiffuseColor.rgb * Max(0, N*L)

# Specular = SpecularMap.rgb * SpecColor.rgb * (Max(0, H*N)^(SpecularMap.a * SpecColor.a) + Environment);

# Environment = EnvironmentMap.rgb * EnvColor.rgb * Max(0, N*L)

# Transmission = TransmissionMap.rgb * (TransmissionMap.a * Max(0, -N*L) + AttribMap.a * (1 - Abs(L*N - HSCenter))^HSExp )

# FresnelFactor = (1 - AttribMap.r) + AttribMap.r * (1 - H*V)^5

# SilkFactor = (1 - AttribMap.g) + AttribMap.g*(1 - V*N);

# Projection = DistanceAttenuation * ProjectionMap1.rgb + (1 - DistanceAttenuation) * ProjectionMap2.rgb

# DistanceAttenuation = (1 - |LightPos - PixelPos| / LightRange^2)^2

# N = Normal
# L = Light vector
# H = Half angle vector
# V = Eye vector

#-----------------------------------------
# Texture0 = Diffuse Map		// Default = 1,1,1,1
# Texture1 = Specular Map		// Default = 1,1,1,1
# Texture2 = Normal map			// Default = 1, 0.5, 0.5
# Texture3 = Attribute Map		// r = Fresnel, g = Silkness, b = ?, a = ?, Default = 0,0,0,0
# Texture4 = Transmission Diffuse Map	// Default = 0,0,0,0
# Texture5 = Projection Map 1		// Default = 1,1,1
# Texture6 = Projection Map 2		// Default = 1,1,1
# Texture7 = Environment Map		// Default = 0,0,0
# Texture8 = Anisotropic Direction Map	// Default = 1,0,0,0

# Texture10..15 = LFM

#-----------------------------------------
# TexCoord0 = Mapping tex coord
# TexCoord1 = LFM tex coord
# TexCoord2 = Animated model space pixel position
# TexCoord3 = Interpolated tangent space eye vector (IPTSEV)
# TexCoord4 = ProjMap tex coord
# TexCoord5 = Tangentspace-2-world transform, row 0
# TexCoord6 = Tangentspace-2-world transform, row 1
# TexCoord7 = Tangentspace-2-world transform, row 2

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

OUTPUT oCol = result.color;

#ATTRIB vCol = fragment.color;

ATTRIB MappingTexCoord = fragment.texcoord[0];
ATTRIB LFMTexCoord = fragment.texcoord[1];
ATTRIB DeferredTexCoord = fragment.texcoord[2];
ATTRIB IPTSEV = fragment.texcoord[3];
#ATTRIB ProjMapTexCoord = fragment.texcoord[4];
ATTRIB TS2W_Mat_0 = fragment.texcoord[5];
ATTRIB TS2W_Mat_1 = fragment.texcoord[6];
ATTRIB TS2W_Mat_2 = fragment.texcoord[7];

PARAM WSLV = program.env[0];		# { X, Y, Z, 0 }
#PARAM LightRange = program.env[1];	# { 1.0 / Range, Range, 1.0 / Range^2, Range^2 }
PARAM LightColor = program.env[2];	# { R, G, B, 0 } (0-2 range)
PARAM SpecularColor = program.env[3];	# { R, G, B, SpecPower } (0-2 range)
#PARAM AttribScale = program.env[4];
#PARAM EnvMapColor = program.env[5];
#PARAM TransmissionColor = program.env[6];
PARAM LFM0Texel = program.env[7];
PARAM LFM1Texel = program.env[8];
PARAM LFM2Texel = program.env[9];
PARAM LFM3Texel = program.env[10];
PARAM LFM4Texel = program.env[11];
PARAM LFM5Texel = program.env[12];

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

TEMP DiffuseTexel;
TEMP SpecularTexel;
TEMP ProjMapTexel1;
TEMP ProjMapTexel2;
TEMP NormalMapTexel;
TEMP NormalMapTexelW;
TEMP AttribTexel;
TEMP TransmissionTexel;
TEMP AnisoDirTexel;
TEMP EnvMapTexel;
#TEMP WSLV;
TEMP TSEV;		# Tangent space eye vector
TEMP WSEV;		# World space eye vector
TEMP H;
TEMP HT;		# Half angle vector projected onto tangent plane
TEMP R;
TEMP RW;
TEMP r0;
TEMP r1;
TEMP r2;
TEMP m;
TEMP F;
TEMP u;	# u = H * TSEV
TEMP t;	# t = H * N
TEMP v; # v = TSEV * N
TEMP v_nosat;
TEMP l; # l = TSLV * N
TEMP lt;
TEMP w;
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 NormalMapTexel, r0, texture[2], 2D;

MUL DiffuseTexel.rgb, DiffuseTexel,DiffuseTexel;
MUL SpecularTexel.rgb, SpecularTexel, SpecularTexel;

#-----------------------------------------
# Reconstruct normal from g,b components
MAD NormalMapTexel.rgba, NormalMapTexel, const_val.b, -const_val.g;# Bias and scale the normalmap texel (only rgb) (from 0->1, -1->1)
SWZ NormalMapTexel.rgb, NormalMapTexel, 0, a, g, 0;
DP3 NormalMapTexel.r, NormalMapTexel, NormalMapTexel;	# r = g^2 + b^2
SUB NormalMapTexel.r, const_val.y, NormalMapTexel.r;	# r = 1 - r;
RSQ NormalMapTexel.r, NormalMapTexel.r;			# r = 1/sqrt(r)
RCP NormalMapTexel.r, NormalMapTexel.r;			# r = 1/r = sqrt(1 - g^2 - b^2)

#-----------------------------------------
# Rotate normal into world-space
DP3 NormalMapTexelW.r, NormalMapTexel, TS2W_Mat_0;			# Transform reflection vector to world space
DP3 NormalMapTexelW.g, NormalMapTexel, TS2W_Mat_1;
DP3 NormalMapTexelW.b, NormalMapTexel, TS2W_Mat_2;

#-----------------------------------------
# Diffuse
MOV_SAT r0.rgb, NormalMapTexelW;
MOV_SAT r1.rgb, -NormalMapTexelW;
MUL Result.rgb, r1.r, LFM0Texel;
MAD Result.rgb, r1.g, LFM2Texel, Result;
MAD Result.rgb, r1.b, LFM4Texel, Result;
MAD Result.rgb, r0.r, LFM1Texel, Result;
MAD Result.rgb, r0.g, LFM3Texel, Result;
MAD Result.rgb, r0.b, LFM5Texel, Result;

MUL Result.rgb, Result, Result;
MUL r1.rgb, LightColor, DiffuseTexel;		# Multiply diffusemap with light color
MUL Result.rgb, Result, r1;

#-----------------------------------------
# Normalize TSEV
@if support_normalize
NRM TSEV.xyz, IPTSEV.xyz;
@else
DP3 TSEV.a, IPTSEV, IPTSEV;
RSQ TSEV.a, TSEV.a;
MUL TSEV.xyz, IPTSEV, TSEV.a;
@endif
DP3 WSEV.r, TSEV, TS2W_Mat_0;			# Transform eye vector to world space
DP3 WSEV.g, TSEV, TS2W_Mat_1;
DP3 WSEV.b, TSEV, TS2W_Mat_2;

#-----------------------------------------
# Calc world space eye-reflection vector
DP3 v_nosat.a, WSEV, NormalMapTexelW;
ADD r0.a, v_nosat.a, v_nosat.a;
MAD RW.xyz, NormalMapTexelW, r0.a, -WSEV;

#-----------------------------------------
# Project RW on base axes and sum intensity for specular
MOV_SAT r0.rgb, RW;
MOV_SAT r1.rgb, -RW;
MUL r2.rgb, r1.r, LFM0Texel;
MAD r2.rgb, r1.g, LFM2Texel, r2;
MAD r2.rgb, r1.b, LFM4Texel, r2;
MAD r2.rgb, r0.r, LFM1Texel, r2;
MAD r2.rgb, r0.g, LFM3Texel, r2;
MAD r2.rgb, r0.b, LFM5Texel, r2;
MUL r2.rgb, r2, 2;

#-----------------------------------------
# Specular + EnvMap
DP3_SAT t.a, RW, -WSLV;
MUL SpecularTexel.a, SpecularTexel.a, SpecularColor.a;
MOV SpecularTexel.a, SpecularColor.a;
POW r1.a, t.a, SpecularTexel.a;
MUL r1.rgb, SpecularColor, r1.a;
MUL r1.rgb, r1, r2;
MAD Result.rgb, SpecularTexel, r1, Result;	# Mul specular with specular color, add to final fragment

# Alpha from diffuse texture
MOV Result.a, DiffuseTexel.a;

# Write result
MOV oCol, Result;

# Debug stuff
#MOV oCol, LFM3Texel;
#MAD oCol, WSLV, 0.25, 0.25;
#MUL oCol, r1.a, 0.5;
#MOV oCol, { 0.5, 0, 0, 0 };
#MUL oCol, AnisoDirTexel, 0.5;

END
