/* Shader description goes here */
surface
grass(float    Kd = 0.7,
            Ks = 0.2,
            roughness = 0.1,
            KdFront =1,
            KdRear =1,
            translucency =0.8,
            texrepeats = 1,
            texblend =0.5;
    string  texname ="";
    float texflip = 0 /* [0 or 1] */;
    color glowColor = color (0.8,1.0,0.4))
{
color surfcolor = 1;
  
/* STEP 1 - make a copy of the surface normal one unit in length */
normal    n = normalize(N);
normal    nf = faceforward(n, I);
  
  
// Establish the true front and rear norma------------------------------
normal     front =n,
        rear = -n;
        
if (nf !=n)
    {
    front =-n;  
    rear =n;
    }
  
  
//Make two lighting caluculations - front/rear
color frontColor = KdFront * diffuse(front);
color rearColor = KdRear * diffuse(rear);
  
//Combine the front/rear colors
color diffuseColor = mix(rearColor,frontColor,translucency)*Kd;
  
// specular color hilites
color specularcolor = specular (nf,  normalize(-I), roughness) * Ks;
color specularHSV = ctransform ( "rgb", "hsv", specularcolor);
  
// Calculate brightness of rear and front lighting.
color frontHSV = ctransform ( "rgb", "hsv", frontColor);
  
    float frontBrightness = frontHSV [2];    //extract brightness
  
color rearHSV = ctransform ( "rgb", "hsv", rearColor);
  
    float rearBrightness = rearHSV [2];
    
//------------------------------------------------------------------------
// Mix the glow Color and the true leaf color based on the 
// relative difference between the front and the rear lighting value
  
float T = smoothstep ( 0.0 , 1.0, rearBrightness - frontBrightness);
color finalColor = mix(Cs, glowColor, T);
  
  
  
// read out texture_v02 ----------------------------------------------
  
color texColor = 1;
if    (texname != "") // "!=" is one character
    {
        if (texflip == 0 )
            texColor = texture (texname, s* texrepeats, t);
        else 
            texColor = texture (texname, t* texrepeats, s);
    }
finalColor = mix ( finalColor ,texColor , texblend);
  
//--------------------------------------------------------
// force white on hilite        
  
finalColor = mix (finalColor, color ( 1,1,1), specularHSV [2]);
  
  
Oi = Os;
Ci = Oi * finalColor * diffuseColor  ;
}