// Software vertex shader generated by Wild Magic.
//
// var float3   kModelPosition  $vin.POSITION
// var float3   kModelColor     $vin.COLOR
// var float4   kWeight         $vin.TEXCOORD0
// var float4   kClipPosition   $vout.POSITION
// var float3   kVertexColor    $vout.COLOR
// var float4x4 WVPMatrix       c[0]
// var float4x4 SkinningMatrix0 c[4]
// var float4x4 SkinningMatrix1 c[8]
// var float4x4 SkinningMatrix2 c[12]
// var float4x4 SkinningMatrix3 c[16]

#include "Wm4SoftRenderer.h"
#include "Wm4Matrix4.h"
#include "Wm4ColorRGB.h"

namespace Wm4
{

void v_Skinning (const float* afRegister, const float* afInVertex,
    float* afOutVertex)
{
    // Get the register items.
    const Matrix4f& rkWVPMatrix = *(const Matrix4f*)&afRegister[0];
    const Matrix4f& rkSkinningMatrix0 = *(const Matrix4f*)&afRegister[16];
    const Matrix4f& rkSkinningMatrix1 = *(const Matrix4f*)&afRegister[32];
    const Matrix4f& rkSkinningMatrix2 = *(const Matrix4f*)&afRegister[48];
    const Matrix4f& rkSkinningMatrix3 = *(const Matrix4f*)&afRegister[64];

    // Get the input items.
    Vector4f kModelPosition(afInVertex[0],afInVertex[1],afInVertex[2],1.0f);
    const ColorRGB& rkModelColor = *(const ColorRGB*)&afInVertex[3];
    const Vector4f& rkWeight = *(const Vector4f*)&afInVertex[6];

    // Access the output items.
    Vector4f& rkClipPosition = *(Vector4f*)&afOutVertex[0];
    ColorRGB& rkVertexColor = *(ColorRGB*)&afOutVertex[4];

    // *** program ***

    Vector4f kSkinPos0 = (kModelPosition*rkSkinningMatrix0)*rkWeight[0];
    Vector4f kSkinPos1 = (kModelPosition*rkSkinningMatrix1)*rkWeight[1];
    Vector4f kSkinPos2 = (kModelPosition*rkSkinningMatrix2)*rkWeight[2];
    Vector4f kSkinPos3 = (kModelPosition*rkSkinningMatrix3)*rkWeight[3];
    Vector4f kSkinPosition = kSkinPos0 + kSkinPos1 + kSkinPos2 + kSkinPos3;

    // Transform the position from model space to clip space.
    rkClipPosition = kSkinPosition*rkWVPMatrix;

    // Pass through the color.
    rkVertexColor = rkModelColor;
}

WM4_IMPLEMENT_VPROGRAM(Skinning);
WM4_REGISTER_VPROGRAM(Skinning);

}
