/////////////////////////////////////////////////////////////////////////////
// oak.osl
//
// help: makes procedural solid texture that looks very much like
//    wood grain.  The rings surround the z axis, so to position the
//    pattern, one should translate the shadingspace (which defaults to
//    "shader").  This makes a fairly plain, unfinished wood, that looks
//    very much like oak.
//   
// Parameters for the coordinate mapping: 
//   shadingspace - space in which the pattern is laid out
//   shadingfreq - overall scaling factor for the pattern
//   Pref - if supplied, gives the reference pose
//
// Parameters for the color and pattern: 
//   Clightwood - the light, "background" wood color
//   Cdarkwood - the darker color in the ring/grain
//   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.
//   divotdepth - depth (in shader units) of the displacement due to
//       ring or grain.
//
// Parameters for illumination model:
//   Ka, Kd, Ks, roughness - the usual meaning
//   
//
// 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)
/////////////////////////////////////////////////////////////////////////////


#include "material.h"

#include "oak.h"



surface
oak
    [[ string help = "Oak solid texture" ]]
(
    float Ka = 1
        [[  string help = "Ambient scaling",
            float min = 0, float max = 1 ]],
    float Kd = 1
        [[  string help = "Diffuse scaling",
            float min = 0, float max = 1 ]],
    float Ks = .25
        [[  string help = "Specular scaling",
            float min = 0, float max = 1 ]],
    float roughness = 0.2
        [[  string help = "Surface roughness (specular highlight size)",
            float min = 0, float UImax = 1 ]],

    float divotdepth = 0.05
        [[ string help = "Divot depth for rings and grain" ]],
    color Clightwood = color (.5, .2, .067)
        [[ string help = "Light wood color" ]],
    color Cdarkwood = color(0.15, 0.077, 0.028)
        [[ string help = "Dark wood color" ]],

    float ringy = 1
        [[ string help = "Ring amplitude",
           float min = 0 ]],
    float ringfreq = 8
        [[ string help = "Ring frequency" ]],
    float ringunevenness = 0.3
        [[ string help = "Ring unevenness" ]],
    float ringnoise = 0.02
        [[ string help = "Ring noise" ]],
    float ringnoisefreq = 1
        [[ string help = "Ring noise frequency" ]],
    float trunkwobble = 0.15
        [[ string help = "Trunk wobble" ]],
    float trunkwobblefreq = 0.025
        [[ string help = "Trunk wobble frequency" ]],
    float angularwobble = 1
        [[ string help = "Angular wobble" ]],
    float angularwobblefreq = 1.5
        [[ string help = "Angular wobble frequency" ]],

    float grainy = 1
        [[ string help = "Grain amplitude" ]],
    float grainfreq = 25
        [[ string help = "Grain frequency" ]],

    point Pref = P
        [[  string help = "Reference pose (defaults to P)" ]],
    string shadingspace = "shader"
        [[  string help = "Shading space" ]],
    float shadingfreq = 1
        [[  string help = "Shading frequency" ]]
  )
{
    point Pshad = shadingfreq * transform (shadingspace, Pref);
    float dPshad = sqrt(area(Pshad));

    float wood;
    wood = oaktexture (Pshad, dPshad, ringfreq, ringunevenness, grainfreq, 
		       ringnoise, ringnoisefreq, trunkwobble, trunkwobblefreq,
		       angularwobble, angularwobblefreq, ringy, grainy);
    color Cwood = mix (Clightwood, Cdarkwood, wood);
    bump ("shader", -wood*divotdepth);
    normal Nf = faceforward(normalize(N),I);
    C = MaterialPlastic (Nf, Cwood, Ka, Kd, Ks*(1-0.5*wood), roughness);
    C *= opacity;
}

