/**
 * Copyright (C) 2013 Stellar Jockeys.
 * All Rights Reserved.
 *
 * NOTICE: All information contained herein is, and remains the property of
 * Stellar Jockeys and its suppliers, if any.  The intellectual and technical
 * concepts contained herein are proprietary to Stellar Jockeys and its suppliers.
 * Dissemination of this information or reproduction of this material is
 * strictly forbidden unless prior written permission is obtained from
 * Stellar Jockeys.
 *
 * @file ground.frag
 * @author Harry Hsiao
 * @date 07/25/13
 */
#version 120
uniform sampler2D uTexture;
uniform float uFresnel;
uniform float uRoughness;
uniform float uPixelsPerDepth;
uniform vec2 uTapDelta;
varying vec2 vTextureCoord;

uniform float uPerturbation;

//#include <constants.glsl>
//#include <fragmaterial.glsl>
//#include <fragnormal.glsl>

void main()
{
    gl_FragData[0] = texture2D(uTexture, vTextureCoord);
    gl_FragData[1] = PackMaterialProperties(uFresnel, uRoughness);

    float perturbation = uPerturbation * PERTURBATION_SCALE;
    float delta = dot(gl_FragData[0].xyz, DESATURATE);
    delta *= perturbation;

    vec3 normal = DeriveNormalFromDiffuse(vTextureCoord,
                                          delta,
                                          uTapDelta,
                                          uPixelsPerDepth,
                                          perturbation,
                                          uTexture);

    gl_FragData[2] = vec4(NormalToColor(normal), 1.0);
}
