#ifndef _SKINNING_FX_INCLUDED
#define _SKINNING_FX_INCLUDED


//-----------------------------------------------------------------------------------------------------------------------------------------
struct VS_SKINNING
{
	float4 vBlendWeights	: BLENDWEIGHT0;
	float4 vBlendIndices	: BLENDINDICES0;
};

//-----------------------------------------------------------------------------------------------------------------------------------------
// CalcSkinning - Funktion für den VertexShader; gibt die geblendete Position&Normale im Worldspace zurück
float4x3
CalcSkinning(
	in VS_SKINNING i, 
	uniform int iNumBones,
	uniform float4x3 amBoneMatrixPalette[BONEMATRIXPALETTE_SIZE])
{
	// work around, wegen fehlenden support für UBYTE4 auf Geforce3
	int4 vIndexVector = D3DCOLORtoUBYTE4(i.vBlendIndices);


	// Vektoren zu Arrays casten (für den LookUp)
	float afBlendWeightsArray[4]	= (float[4])i.vBlendWeights;
	int   aiIndexArray[4]			= (int[4])vIndexVector;

	
	// gewichtetes Addieren der verschiedenen Transformationen
	float	 fLastWeight = 1.0f;
    float4x3 mSkinning = 0;

    {
    	for (int iBone = 0; iBone < iNumBones - 1; iBone++)
    	{
        	mSkinning += amBoneMatrixPalette[aiIndexArray[iBone]] * afBlendWeightsArray[iBone];
    	    fLastWeight -= afBlendWeightsArray[iBone];
        }
    }

    int iBone = iNumBones - 1;
	mSkinning += amBoneMatrixPalette[aiIndexArray[iBone]] * fLastWeight;
	
	return mSkinning;
}


#endif // _SKINNING_FX_INCLUDED
