!!ARBfp1.0

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

# 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
# Texture10..13 = LFM

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

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 LMIntensityScale = fragment.texcoord[4];

PARAM DiffColor = program.env[0];	# { R, G, B, 0 } (0-2 range)
PARAM SpecColor = program.env[1];	# { R, G, B, SpecPower } (0-2 range)

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

TEMP DiffuseTexel;
TEMP SpecularTexel;
TEMP TSNormal;
TEMP LFM0Texel;
TEMP LFM1Texel;
TEMP LFM2Texel;
TEMP LFM3Texel;
TEMP LFM4Texel;
TEMP TSLV;
TEMP TSEV;		# Tangent space eye vector
TEMP R;
TEMP r0;
TEMP r1;
TEMP r2;
TEMP v_nosat;
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;

#MOV DiffuseTexel, 0.5;

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

TEX LFM0Texel, LFMTexCoord, texture[10], 2D;
TEX LFM1Texel, LFMTexCoord, texture[11], 2D;
TEX LFM2Texel, LFMTexCoord, texture[12], 2D;
TEX LFM3Texel, LFMTexCoord, texture[13], 2D;

# Unbake lightfieldmap data
MOV LFM4Texel.r, LFM1Texel.a;
MOV LFM4Texel.g, LFM2Texel.a;
MOV LFM4Texel.b, LFM3Texel.a;

#-----------------------------------------
# Normalize
MAD TSNormal.rgb, TSNormal, const_val.b, -const_val.g;	# Bias and scale the normalmap texel (only rgb) (from 0->1, -1->1)
DP3 TSNormal.w, TSNormal, TSNormal;
RSQ TSNormal.w, TSNormal.w;
MUL TSNormal.rgb, TSNormal, TSNormal.w;

#-----------------------------------------
# Diffuse
MOV_SAT r0.rgb, TSNormal;
MOV_SAT r1.rgb, -TSNormal;
MUL Result.rgb, r0.r, LFM0Texel;
MAD Result.rgb, r1.b, LFM1Texel, Result;
MAD Result.rgb, r1.g, LFM2Texel, Result;
MAD Result.rgb, r0.b, LFM3Texel, Result;
MAD Result.rgb, r0.g, LFM4Texel, Result;
MUL Result.rgb, Result, LMIntensityScale.r;
#MOV Result, { 1.5, 0.75, 0.75, 1 };

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

#MOV Result,0;

#-----------------------------------------
DP3 r0.r, LFM0Texel, const_val3;
DP3 r0.b, LFM3Texel, const_val3;
DP3 r0.g, LFM4Texel, const_val3;
MOV r1.r, 0;
DP3 r1.b, LFM1Texel, const_val3;
DP3 r1.g, LFM2Texel, const_val3;
SUB r0.rgb, r0, r1;
@if support_normalize
NRM TSLV.xyz, r0.xyz;
@else
DP3 r0.w, r0, r0;
RSQ r0.w, r0.w;
MUL TSLV.rgb, r0, r0.w;
@endif

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

#-----------------------------------------
# Calc eye-reflection vector
DP3 v_nosat.a, TSEV, TSNormal;
ADD r0.a, v_nosat.a, v_nosat.a;
MAD R.xyz, TSNormal, r0.a, -TSEV;

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

MOV_SAT r0.rgb, R;
MOV_SAT r1.rgb, -R;
MUL r2.rgb, r0.r, LFM0Texel;
MAD r2.rgb, r1.b, LFM1Texel, r2;
MAD r2.rgb, r1.g, LFM2Texel, r2;
MAD r2.rgb, r0.b, LFM3Texel, r2;
MAD r2.rgb, r0.g, LFM4Texel, r2;
#MUL r2.rgb, r2, 2;
MUL SpecularTexel.rgb, SpecularTexel, LMIntensityScale.r;
MUL SpecularTexel.rgb, r2, SpecularTexel;
MUL SpecularTexel, SpecularTexel, SpecColor;
MUL SpecularTexel.rgb, SpecularTexel, SpecularTexel;
MUL SpecularTexel.a, SpecularTexel, 0.0625;
#MUL r2.rgb, r2, SpecColor;
#MUL r2.rgb, r2, 2;
#MOV Result, 0;
#ADD Result.rgb, r2, Result;
#MUL SpecularTexel.rgb, SpecularTexel, r2;
#MUL Result, r2, 1;

#-----------------------------------------
# Calc halfangle vector
#ADD H.xyz, TSEV, TSLV;
#DP3 H.a, H, H;
#RSQ H.a, H.a;
#MUL H.xyz, H, H.a;
#DP3_SAT t.a, H, NormalMapTexel;

#-----------------------------------------
# Specular + EnvMap
#MUL SpecularTexel, SpecularTexel, SpecColor;
#MOV SpecularTexel.a, 16;
DP3_SAT r1.a, TSLV, R;
POW r1.a, r1.a, SpecularTexel.a;
#MOV Result, r1.a;
#MUL r1.rgb, SpecColor, r1.a;
#MUL r1.rgb, r1, LFM0Texel;
#MUL r1.rgb, r1, 16;
#MOV Result, 0;
MAD Result.rgb, SpecularTexel, r1.a, Result;	# Mul specular with specular color, add to final fragment

# Alpha from diffuse texture
MUL r0.a, LMIntensityScale.r, LMIntensityScale.r;
#MUL Result.rgb, Result, r0.a;
MOV Result.a, DiffuseTexel.a;
#MUL Result.r, w.a, 0.5;

#MOV r0.rgb, l.a;
#MUL r0.rgb, 0.25, t.a;

#ADD Result, NormalMapTexel2, Result;
#MOV Result, NormalMapTexel2;
#MAD Result, NormalMapTexel, 0.25, 0.25;
#MOV Result.r, NormalMapTexel2.a;

# Write result
#MOV Result, 0;
MOV oCol, Result;
#MUL oCol, LFM0Texel, 1;
#MAD oCol, R, 0.25, 0.25;
#MOV oCol, { 0.5, 0, 0, 0 };
#MUL oCol, AnisoDirTexel, 0.5;

END
