//////////////////////////////////////////////////////////////////////////
// Character Animation with Direct3D //
// Author: C. Granberg //
// 2008 - 2009 //
//////////////////////////////////////////////////////////////////////////
//Transformation Matrices
matrix matW;
matrix matVP;
//World Light Position
float3 lightPos;
float4 materialColor;
//Texture
texture texDiffuse;
//Sampler
sampler DiffuseSampler = sampler_state
{
Texture = (texDiffuse);
MinFilter = Linear; MagFilter = Linear; MipFilter = Linear;
AddressU = Wrap; AddressV = Wrap; AddressW = Wrap;
MaxAnisotropy = 16;
};
//Vertex Input
struct VS_INPUT
{
float4 position : POSITION0;
float3 normal : NORMAL;
float2 tex0 : TEXCOORD0;
};
//Vertex Output / Pixel Shader Input
struct VS_OUTPUT
{
float4 position : POSITION0;
float2 tex0 : TEXCOORD0;
float shade : TEXCOORD1;
};
//Vertex Shader
VS_OUTPUT vs_lighting(VS_INPUT IN)
{
VS_OUTPUT OUT = (VS_OUTPUT)0;
//getting the position of the vertex in the world
float4 posWorld = mul(IN.position, matW);
float4 normal = normalize(mul(IN.normal, matW));
//getting to position to object space
OUT.position = mul(posWorld, matVP);
OUT.shade = max(dot(normal, normalize(lightPos - posWorld)), 0.2f);
OUT.tex0 = IN.tex0;
return OUT;
}
//Pixel Shader
float4 ps_lighting(VS_OUTPUT IN) : COLOR0
{
float4 color = tex2D(DiffuseSampler, IN.tex0);
return IN.shade * materialColor;
}
//Lighting Technique
technique Lighting
{
pass P0
{
Lighting = false;
VertexShader = compile vs_2_0 vs_lighting();
PixelShader = compile ps_2_0 ps_lighting();
}
}
//Pixel Shader
float4 ps_shadow(VS_OUTPUT IN) : COLOR0
{
return float4(0.3f, 0.3f, 0.3f, 1.0f);
}
//Shadow Technique
technique Shadow
{
pass P0
{
Lighting = false;
VertexShader = compile vs_2_0 vs_lighting();
PixelShader = compile ps_2_0 ps_shadow();
}
}
////////////////////////////////////////////////////////////////////////////
extern float4x4 FinalTransforms[100];
extern int NumVertInfluences = 2; // <--- Normally set dynamically.
//Vertex Input
struct VS_INPUT_SKIN
{
float4 position : POSITION0;
float3 normal : NORMAL;
float2 tex0 : TEXCOORD0;
float4 weights : BLENDWEIGHT0;
int4 boneIndices : BLENDINDICES0;
};
VS_OUTPUT vs_Skinning(VS_INPUT_SKIN IN)
{
VS_OUTPUT OUT = (VS_OUTPUT)0;
float4 p = float4(0.0f, 0.0f, 0.0f, 1.0f);
float3 norm = float3(0.0f, 0.0f, 0.0f);
float lastWeight = 0.0f;
int n = NumVertInfluences-1;
IN.normal = normalize(IN.normal);
for(int i = 0; i < n; ++i)
{
lastWeight += IN.weights;
p += IN.weights * mul(IN.position, FinalTransforms[IN.boneIndices]);
}
lastWeight = 1.0f - lastWeight;
p += lastWeight * mul(IN.position, FinalTransforms[IN.boneIndices[n]]);
p.w = 1.0f;
p.w = 1.0f;
float4 posWorld = mul(p, matW);
OUT.position = mul(posWorld, matVP);
OUT.tex0 = IN.tex0;
//Calculate Lighting
norm = normalize(norm);
norm = mul(norm, matW);
OUT.shade = max(dot(norm, normalize(lightPos - posWorld)), 0.2f);
return OUT;
}
technique Skinning
{
pass P0
{
Lighting = false;
VertexShader = compile vs_2_0 vs_Skinning();
PixelShader = compile ps_2_0 ps_lighting();
}
}
if (boneMesh->pSkinInfo != NULL)
{
// set up bone transforms
int numBones = boneMesh->pSkinInfo->GetNumBones();
for(int i=0;i < numBones;i++)
{
D3DXMatrixMultiply(&boneMesh->currentBoneMatrices,
&boneMesh->pBoneOffsetMatrices,
boneMesh->ppBoneMatrixPtrs);
}
D3DXMATRIX view, proj, identity;
g_pEffect->SetMatrixArray("FinalTransforms", &boneMesh->currentBoneMatrices[0], boneMesh->pSkinInfo->GetNumBones());
//D3DXMatrixIdentity(&identity);
//Render the mesh
for(int i=0;i < (int)boneMesh->NumAttributeGroups;i++)
{
int mtrlIndex = boneMesh->pAttributeTable.AttribId;
g_pDevice->SetMaterial(&(boneMesh->materials[mtrlIndex]));
g_pDevice->SetTexture(0, boneMesh->textures[mtrlIndex]);
g_pEffect->SetMatrix("matW", &bone->CombinedTransformationMatrix);
g_pEffect->SetVector( "materialColor",
( D3DXVECTOR4* )&(
boneMesh->materials.Diffuse ) );
g_pEffect->SetTexture("texDiffuse", boneMesh->textures[mtrlIndex]);
D3DXHANDLE hTech = g_pEffect->GetTechniqueByName("Skinning");
g_pEffect->SetTechnique(hTech);
g_pEffect->Begin(NULL, NULL);
g_pEffect->BeginPass(0);
boneMesh->pOrigMesh->DrawSubset(mtrlIndex);
g_pEffect->EndPass();
g_pEffect->End();
}
Is this the model's problem? However, how can I shrink/scale it down? I have to do this myself and it's a pre-rigged model.
You also seeing that the bottom part of the character is clipped off. Why is that?
I don't know yet because the view and projection matrices seem to be correct as other models are displayed correctly, apart from skinned ones.
Thanks
Jack