!!ARBvp1.0

# VPROG_MD5R_INTERACTION_1_BONE
#
# input:
#
# attrib[0]		vertex.position
# attrib[1]		blend (transform) index
# attrib[2]		normal
# attrib[3]		vertex.color (diffuse)
# attrib[6]		tangent
# attrib[7]		binormal
# attrib[8]		2D texture coordinate
#
# program.env[0]-program.env[2]		Blend-space (skin space) to model transform 0
# program.env[3]-program.env[6]		Blend-space (skin space) to model transform 1
# program.env[7]-program.env[9]		Blend-space (skin space) to model transform 2
# program.env[10]-program.env[11]	Blend-space (skin space) to model transform 3
# ...
# program.env[72]-program.env[74]	blend-space (skin space) to model transform 24
# program.env[75]-program.env[78]	model-view-projection transform
# program.env[79]					localLightOrigin (local to the model)
# program.env[80]					localViewOrigin (local to the model)
# program.env[81]					lightProjection S
# program.env[82]					lightProjection T
# program.env[83]					lightProjection Q
# program.env[84]					lightFalloff S
# program.env[85]					bumpMatrix S
# program.env[86]					bumpMatrix T
# program.env[87]					diffuseMatrix S
# program.env[88]					diffuseMatrix T
# program.env[89]					specularMatrix S
# program.env[90]					specularMatrix T
# program.env[91]					vertex color modulate x, add y
#
# 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

ADDRESS A0;

TEMP	R0, R4, R5, R6, R7, R8, R9, R11;

PARAM	defaultTexCoord = { 0, 0.5, 0, 1 };
PARAM	c94 = { 1., 1., 1., 765.005859 };
PARAM	transformPalette[75] = { program.env[0..74] };

#
# load the first matrix, scale by first weight
#
MUL R11, vertex.attrib[1].xyzw, c94.w;
ARL A0.x, R11.x;

#
# Transform the vertex position from skin-space to model-space, store model position in R0
#
DP4 R0.x, transformPalette[A0.x], vertex.position;
DP4 R0.y, transformPalette[A0.x+1], vertex.position;
DP4 R0.z, transformPalette[A0.x+2], vertex.position;
MOV R0.w, c94.x;

#
# Transform the normal and tangent from skin-space to model-space
#
DP3 R4.x, transformPalette[A0.x], vertex.normal;
DP3 R4.y, transformPalette[A0.x+1], vertex.normal;
DP3 R4.z, transformPalette[A0.x+2], vertex.normal;

DP3 R5.x, transformPalette[A0.x], vertex.attrib[6];
DP3 R5.y, transformPalette[A0.x+1], vertex.attrib[6];
DP3 R5.z, transformPalette[A0.x+2], vertex.attrib[6];

DP3 R6.x, transformPalette[A0.x], vertex.attrib[7];
DP3 R6.y, transformPalette[A0.x+1], vertex.attrib[7];
DP3 R6.z, transformPalette[A0.x+2], vertex.attrib[7];

#
# Calculate the vector from the model vertex position to the light (in model space),
# transform that light vector into tangent space
#
SUB R7, program.env[79], R0;
DP3 result.texcoord[0].x, R5, R7;
DP3 result.texcoord[0].y, R6, R7;
DP3 result.texcoord[0].z, R4, R7;

# texture 1 takes the base coordinates by the texture matrix
DP4	result.texcoord[1].x, vertex.attrib[8], program.env[85];
DP4	result.texcoord[1].y, vertex.attrib[8], program.env[86];

# texture 2 has one texgen
MOV	result.texcoord[2].xy, defaultTexCoord;
DP4	result.texcoord[2].x, R0, program.env[84];

# texture 3 has three texgens
DP4	result.texcoord[3].x, R0, program.env[81];
DP4	result.texcoord[3].y, R0, program.env[82];
DP4	result.texcoord[3].w, R0, program.env[83];

# texture 4 takes the base coordinates by the texture matrix
DP4	result.texcoord[4].x, vertex.attrib[8], program.env[87];
DP4	result.texcoord[4].y, vertex.attrib[8], program.env[88];

# textures 5 takes the base coordinates by the texture matrix
DP4	result.texcoord[5].x, vertex.attrib[8], program.env[89];
DP4	result.texcoord[5].y, vertex.attrib[8], program.env[90];

# texture 6's texcoords will be the halfangle in texture space

# calculate normalized vector to light in R7
DP3	R8, R7, R7;
RSQ	R8, R8.x;
MUL	R7, R7, R8.x;

# calculate normalized vector to viewer in R9
SUB	R9, program.env[80], R0;
DP3	R8, R9, R9;
RSQ	R8, R8.x;
MUL	R9, R9, R8.x;

# add together to become the half angle vector in object space (non-normalized)
ADD	R7, R7, R9;

# transform half vector into tangent space
DP3	result.texcoord[6].x, R5, R7;
DP3	result.texcoord[6].y, R6, R7;
DP3	result.texcoord[6].z, R4, R7;

# 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[91].x, program.env[91].y;

#
# Transform the model vertex position by the model-view-projection transform.
#
DP4 result.position.x, program.env[75], R0;
DP4 result.position.y, program.env[76], R0;
DP4 result.position.z, program.env[77], R0;
DP4 result.position.w, program.env[78], R0;

END

