// Software vertex shader generated by Wild Magic.
//
// var float3   kModelPosition       $vin.POSITION
// var float3   kModelNormal         $vin.NORMAL
// var float4   kClipPosition        $vout.POSITION
// var float4   kVertexColor         $vout.COLOR
// var float4x4 WVPMatrix            c[0]
// var float4x4 WMatrix              c[4]
// var float3   CameraModelPosition  c[8]
// var float3   MaterialEmissive     c[9]
// var float3   MaterialAmbient      c[10]
// var float4   MaterialDiffuse      c[11]
// var float4   MaterialSpecular     c[12]
// var float3   Light0ModelPosition  c[13]
// var float3   Light0ModelDirection c[14]
// var float3   Light0Ambient        c[15]
// var float3   Light0Diffuse        c[16]
// var float3   Light0Specular       c[17]
// var float4   Light0SpotCutoff     c[18]
// var float4   Light0Attenuation    c[19]

#include "Wm4SoftRendererPCH.h"
#include "Wm4SoftRenderer.h"
#include "Wm4Matrix4.h"
#include "Wm4ColorRGBA.h"
#include "Wm4Vector3.h"

namespace Wm4
{

//----------------------------------------------------------------------------
void v_L1s (const float* afRegister, const float* afInVertex,
    float* afOutVertex)
{
    // Get the register items.
    const Matrix4f& rkWVPMatrix = *(const Matrix4f*)&afRegister[0];
    const Matrix4f& rkWMatrix = *(const Matrix4f*)&afRegister[16];
    const Vector3f& rkCameraModelPosition = *(const Vector3f*)&afRegister[32];
    const ColorRGB& rkMatEms = *(const ColorRGB*)&afRegister[36];
    const ColorRGB& rkMatAmb = *(const ColorRGB*)&afRegister[40];
    const ColorRGB& rkMatDif = *(const ColorRGB*)&afRegister[44];
    float fMatAlpha = afRegister[47];
    const ColorRGB& rkMatSpc = *(const ColorRGB*)&afRegister[48];
    float fMatSpecExp = afRegister[51];
    const Vector3f& rkL0ModelPosition = *(const Vector3f*)&afRegister[52];
    const Vector3f& rkL0ModelDirection = *(const Vector3f*)&afRegister[56];
    const ColorRGB& rkL0Amb = *(const ColorRGB*)&afRegister[60];
    const ColorRGB& rkL0Dif = *(const ColorRGB*)&afRegister[64];
    const ColorRGB& rkL0Spc = *(const ColorRGB*)&afRegister[68];
    const float* afL0Spot = &afRegister[72];
    const float* afL0Atten = &afRegister[76];

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

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

    // *** program ***

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

    float fDiff, fSpec, fSpot;
    GetSpotLightFactors((Vector3f)kModelPosition,rkModelNormal,
        rkCameraModelPosition,rkL0ModelPosition,fMatSpecExp,
        rkL0ModelDirection,afL0Spot[1],afL0Spot[3],fDiff,fSpec,fSpot);

    float fAttn = GetAttenuation(rkWMatrix,(Vector3f)kModelPosition,
        rkL0ModelPosition,afL0Atten);

    ColorRGB kColor = rkMatAmb*rkL0Amb;
    if (fSpot > 0.0f)
    {
        if (fDiff > 0.0f)
        {
            kColor += (fSpot*fDiff)*rkMatDif*rkL0Dif;
            if (fSpec > 0.0f)
            {
                kColor += (fSpot*fSpec)*rkMatSpc*rkL0Spc;
            }
        }
    }

    rkVertexColor = rkMatEms + fAttn*kColor;
    rfVertexAlpha = fMatAlpha;
}
//----------------------------------------------------------------------------

WM4_IMPLEMENT_VPROGRAM(L1s);

}
