// Software vertex shader generated by Wild Magic.
//
// var float4    kVertexColor $vin.COLOR
// var float2    kTCoord      $vin.TEXCOORD0
// var float4    kPixelColor  $vout.COLOR
// var sampler2D BaseSampler  texunit 0

#include "Wm4SoftRenderer.h"
#include "Wm4Matrix4.h"
#include "Wm4Vector2.h"
#include "Wm4ColorRGBA.h"

namespace Wm4
{

ColorRGBA p_VolumeFog (const float* afRegister, SoftSampler** apkSampler,
    const float* afInPixel)
{
    // Get samplers.
    SoftSampler& rkBaseSampler = *apkSampler[0];

    // Get input values.
    const ColorRGBA& rkVertexColor = *(const ColorRGBA*)&afInPixel[0];
    const float* afTCoord = &afInPixel[4];

    // *** program ***

    // The blending equation is
    //   (rf,gf,bf) = (1-av)*(rt,gt,bt) + av*(rv,gv,bv)
    // where (rf,gf,bf) is the final color, (rt,gt,bt) is the texture color,
    // and (rv,gv,bv,av) is the vertex color.

    ColorRGBA kTextureColor = rkBaseSampler(afTCoord);

    ColorRGBA kPixelColor;
    for (int i = 0; i < 3; i++)
    {
        kPixelColor[i] = (1.0f - rkVertexColor[3])*kTextureColor[i] +
            rkVertexColor[3]*rkVertexColor[i];
    }
    kPixelColor[3] = 1.0f;

    return kPixelColor;
}

WM4_IMPLEMENT_PPROGRAM(VolumeFog);
WM4_REGISTER_PPROGRAM(VolumeFog);

}
