The question - is the alternative way to get input color instead of keeping global AmbientMtrl and DiffuseMtrl? The idea from the book is to set AmbientMtrl and DiffuseMtrl and then after calculating light set output color according to the below formula:
output.diffuse = (AmbientMtrl * AmbientLightIntensity) + (s * (DiffuseLightIntensity * DiffuseMtrl));
But maybe there is alternative method, based on TEXCOORD or some other approach???
uniform extern float4x4 MatWorld; //or uniform extern x, y, z;
uniform extern float4x4 MatWorldInvTrans;
uniform extern float4x4 MatView;
uniform extern float4x4 MatViewProj;
uniform extern float4x4 F[35]; //Final transformation matrix for each bone (bind space -> root space)
uniform extern vector LightDirection; //Light direction
//Material
uniform extern vector AmbientMtrl;
uniform extern vector DiffuseMtrl;
vector DiffuseLightIntensity = {1.0f, 1.0f, 1.0f, 1.0f};
vector AmbientLightIntensity = {0.3f, 0.3f, 0.3f, 1.0f};
struct VS_INPUT
{
float3 position : POSITION; //bind space
float3 normal : NORMAL;
float2 coord : TEXCOORD;
float3 weights : BLENDWEIGHT;
int4 boneIndices : BLENDINDICES;
};
struct VS_OUTPUT
{
vector position : POSITION;
vector diffuse : COLOR;
//float3 normal : NORMAL;
//float2 tex : TEXCOORD;
};
VS_OUTPUT Main(VS_INPUT input)
{
VS_OUTPUT output = (VS_OUTPUT)0;
//output position
//Transform root space -> root space acc. to weights
float weights3 = 1.0f - input.weights[0] - input.weights[1] - input.weights[2];
float4 pos = input.weights[0] * mul(float4(input.position, 1.0f), F[input.boneIndices[0]]);
pos += input.weights[1] * mul(float4(input.position, 1.0f), F[input.boneIndices[1]]);
pos += input.weights[2] * mul(float4(input.position, 1.0f), F[input.boneIndices[2]]);
pos += weights3 * mul(float4(input.position, 1.0f), F[input.boneIndices[3]]);
pos.w = 1.0f;
// Transform to World space
pos = mul(pos, MatWorld); //or pos.x += x; pos.y += y; pos.z += z;
// Transform to homogeneous clip space
output.position = mul(pos, MatViewProj);
//Output diffuse
//Transform root space -> root space acc. to weights
float4 normal = input.weights[0] * mul(float4(input.normal, 0.0f), F[input.boneIndices[0]]);
normal += input.weights[1] * mul(float4(input.normal, 0.0f), F[input.boneIndices[1]]);
normal += input.weights[2] * mul(float4(input.normal, 0.0f), F[input.boneIndices[2]]);
normal += weights3 * mul(float4(input.normal, 0.0f), F[input.boneIndices[3]]);
normal.w = 0.0f;
normal = normalize(normal);
// Transform to World space
normal = mul(normal, MatWorldInvTrans);
// Light calculation
LightDirection.w = 0.0f;
LightDirection = mul(LightDirection, MatView);
normal = mul(normal, MatView);
float s = dot(LightDirection, normal);
if(s < 0.0f) s = 0.0f;
output.diffuse = (AmbientMtrl * AmbientLightIntensity) +
(s * (DiffuseLightIntensity * DiffuseMtrl));
return output;
}