/*
 * File: BasicGaussBlobShader.vert.txt
 * Shader for drawing of basic parameterized gaussian blob patches.
 *
 * This is the vertex shader. It takes the attributes (parameters)
 * provided by the Screen('DrawTexture(s)') command, performs some
 * basic calculations on it - the calculations that only need to be
 * done once per gauss blob patch and that can be reliably carried out
 * at sufficient numeric precision in a vertex shader - then it passes
 * results of computations and other attributes as 'varying' parameters
 * to the fragment shader.
 *
 * (c) 2014 by Mario Kleiner, licensed under MIT license.
 */

/* Constants that we need: square-root of 2*pi: */
const float sqrtof2pi = 2.5066282746;

/* Conversion factor from degrees to radians: */
const float deg2rad = 3.141592654 / 180.0;

/* Texel position of center of blob patch: Constant, set */
/* once when the shader is created: */
uniform vec2  Center;

/* If disableNorm is set to 1.0, then the normalization term below is    */
/* not applied to the blobs final values. Default is 0.0, ie. apply it. */
uniform float disableNorm;

/* Constant from setup code: Premultiply to contrast value: */
uniform float contrastPreMultiplicator;

/* Attributes passed from Screen(): See the ProceduralShadingAPI.m file for infos: */
attribute vec4 sizeAngleFilterMode;
attribute vec4 modulateColor;
attribute vec4 auxParameters0;

/* Information passed to the fragment shader: Attributes and precalculated per patch constants: */
varying vec4 baseColor;
varying float Expmultiplier;
varying float Angle;
varying float GammaSquared;

void main()
{
    /* Apply standard geometric transformations to patch: */
    gl_Position = ftransform();

    /* Don't pass real texture coordinates, but ones corrected for hardware offsets (-0.5,0.5) */
    /* and so that the center of the gabor patch has coordinate (0,0): */
    gl_TexCoord[0] = gl_MultiTexCoord0 - vec4(Center, 0.0, 0.0) + vec4(-0.5, 0.5, 0.0, 0.0);

    /* Contrast value is stored in auxParameters0[0]: */
    float Contrast = auxParameters0[0];

    /* Convert Angle from degrees to radians: */
    Angle = deg2rad * sizeAngleFilterMode.z;

    /* Precalc a couple of per-patch constant parameters: */
    float SpaceConstant = auxParameters0[1];
    Expmultiplier = -0.5 / (SpaceConstant * SpaceConstant);

    /* The spatial aspect ratio Gamma is stored in the 3rd aux parameter. */
    float Gamma = auxParameters0[2];
    GammaSquared = Gamma * Gamma;

    /* Conditionally apply non-standard normalization term iff disableNorm == 0.0 */
    float mc = disableNorm + (1.0 - disableNorm) * (1.0 / (sqrtof2pi * SpaceConstant));

    /* Premultiply the wanted Contrast to the color: */
    baseColor = modulateColor * mc * Contrast * contrastPreMultiplicator;
}
