!!ARBfp1.0
#
# Demonstrates per-pixel environment mapping:
#   reflects view direction off surface normal
#
# Orion Sky Lawlor, olawlor@acm.org, 2005/2/7 (Public Domain)
#

# Vertex/fragment communication protocol
ATTRIB world  =fragment.texcoord[4]; # World-coordinates position
ATTRIB worldNi=fragment.texcoord[5]; # World-coordinates normal (interpolated)
ATTRIB albedo =fragment.texcoord[6]; # Material "diffuse color"
ATTRIB viewdir=fragment.texcoord[7]; # Viewing direction (points toward eye)

# Perturb world normal
TEMP worldP,worldS;
MUL worldS,world,20.0; # scaled-up world coordinates (sets noise scale)
TEX worldP,worldS, texture[0],3D; # look up displacement at scaled coordinates
SUB worldS,worldS,0.5; # shift displacement to [-0.5,0.5]
MAD worldP,worldP,0.2, worldNi; # Add in scaled version of displacement to normal

#  Make world normal have unit length
TEMP L,worldN;
DP3 L,worldP,worldP; # worldN = normalize(worldP)
RSQ L,L.x;
MUL worldN,worldP,L;

# Do a diffuse lighting computation with the perturbed normal
TEMP diffuse;
DP3_SAT diffuse, worldN, state.light[0].position; # or viewdir;
MAD diffuse,diffuse,0.8,0.2;
MUL result.color, diffuse,albedo;

END
