// 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 float4   kTCoord              $vout.TEXCOORD0
// var float4x4 WVPMatrix            c[0]
// var float4x4 ProjectorMatrix      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   Light0ModelDirection c[13]
// var float3   Light0Ambient        c[14]
// var float3   Light0Diffuse        c[15]
// var float3   Light0Specular       c[16]

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

namespace Wm4
{

void v_ProjectedTexture (const float* afRegister,
    const float* afInVertex, float* afOutVertex)
{
    // Get the register items.
    const Matrix4f& rkWVPMatrix = *(const Matrix4f*)&afRegister[0];
    const Matrix4f& rkProjMatrix = *(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& rkL0ModelDirection = *(const Vector3f*)&afRegister[52];
    const ColorRGB& rkL0Amb = *(const ColorRGB*)&afRegister[56];
    const ColorRGB& rkL0Dif = *(const ColorRGB*)&afRegister[60];
    const ColorRGB& rkL0Spc = *(const ColorRGB*)&afRegister[64];

    // 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];
    Vector4f& rkTCoord = *(Vector4f*)&afOutVertex[8];

    // *** program ***

    // Transform the position from model space to clip space.
    rkClipPosition = kModelPosition*rkWVPMatrix;
    
    // Compute directional lighting.
    float fDiff, fSpec;
    GetDirectionalLightFactors((Vector3f)kModelPosition,rkModelNormal,
        rkCameraModelPosition,rkL0ModelDirection,fMatSpecExp,fDiff,fSpec);

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

    rkVertexColor = rkMatEms + kColor;
    rfVertexAlpha = fMatAlpha;

    // Compute the projected texture coordinates.
    rkTCoord = kModelPosition*rkProjMatrix;
}

WM4_IMPLEMENT_VPROGRAM(ProjectedTexture);
WM4_REGISTER_VPROGRAM(ProjectedTexture);

}
