/////////////////////////////////////////////////////////////////////////////
// oak.h - contains the oaktexture function, which is common to many
//         shader which make wood grain patterns.  It looks very much
//         like oak, and can also pass for ...
//
// Much of this is adapted from Apodaca & Gritz, "Advanced RenderMan",
// Morgan-Kaufmann 1999.
//
/////////////////////////////////////////////////////////////////////////////
// Copyright 2004 NVIDIA Corporation.  All Rights Reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// 
// * Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA nor the names of its contributors
//   may be used to endorse or promote products derived from this software
//   without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// (This is the Modified BSD License)
/////////////////////////////////////////////////////////////////////////////


#ifndef OAK_H
#define OAK_H


#include "noises.h"


// oaktexture -- generate a pattern much like the rings and grain of
//    oak wood.  Rings are centered on the z axis, so the caller should
//    position the space of Pshad so that the grain goes the way you want.
//    Note: this function takes derivatives, so it shouldn't be called 
//    from within varying conditionals or loops.
// Inputs:
//    Pshad, dPshad - sample point & its derivative
//    ringfreq - mean frequency of ring spacing
//    ringunevenness - 0=equally spaced rings, larger is unequally spaced
//    grainfreq - frequency of the fine grain
//    ringnoise, ringnoisefreq - general warping of the domain
//    trunkwobble, trunkwobblefreq - controls noise which wobbles the
//        axis of the trunk so that it's not perfectly on the z axis.
//    angularwobble, angularwobblefreq - warping indexed by angle about
//        the z axis.
//    ringy, grainy - overall scale on the degree to which rings and
//        grain are weighted.  0 turns one off, 1 makes full effect.
// Return value: 0 when in the "background" light wood, 1 when in the
//    darkest part of a ring or grain.
//
float
oaktexture (point Pshad, float dPshad,
	    float ringfreq, float ringunevenness, float grainfreq,
	    float ringnoise, float ringnoisefreq,
	    float trunkwobble, float trunkwobblefreq,
	    float angularwobble, float angularwobblefreq,
	    float ringy, float grainy)
{
    // We shade based on Pshad, but we add several layers of warping:
    // Some general warping of the domain
    vector offset = vfBm(Pshad*ringnoisefreq,dPshad*ringnoisefreq, 2, 4, 0.5);
    point Pring = Pshad + ringnoise*offset;
    // The trunk isn't totally steady xy as you go up in z
    Pring += trunkwobble * 
               (vector)snoise(Pshad[2]*trunkwobblefreq) * vector(1,1,0);

    // Calculate the radius from the center.
    float r = hypot(Pring[0], Pring[1]) * ringfreq;
    // Add some noise around the trunk
    r += angularwobble * smoothstep(0,5,r)
	   * snoise (angularwobblefreq*(Pring)*vector(1,1,0.1));

    // Now add some noise so all rings are not equal width
    float dr = filterwidth(r);
    r += ringunevenness*filteredsnoise(r,dr);

    float inring = smoothpulsetrain (.1, .55, .7, .95, 1, r);

    point Pgrain = Pshad*grainfreq*vector(1,1,.05);
    float dPgrain = sqrt(area(Pgrain));
    float grain = 0;
    float i, amp=1;
    for (i = 0;  i < 2;  i += 1) {
	float grain1valid = 1-smoothstep(.2,.6,dPgrain);
	if (grain1valid > 0) {
	    float g = grain1valid * snoise (Pgrain);
	    g *= (0.3 + 0.7*inring);
	    g = pow(clamp(0.8 - (g),0,1),2);
	    g = grainy * smoothstep (0.5, 1, g);
	    if (i == 0) {
		inring *= 1 - 0.4 * grain1valid;
            }
	    grain = max (grain, g);
	}
	Pgrain *= 2;
	dPgrain *= 2;
	amp *= 0.5;
    }

    return mix (inring*ringy, 1, grain);
}


#endif /* !defined(OAK_H) */
