/////////////////////////////////////////////////////////////////////////////
// veinedmarble.gsl -- surface shader for a nice veined marble.
//
// Description:
//   Makes solid marble texture with strong veins.  The "veincolor"
//   parameter controls the color of the veins.  The background color
//   is given by the surface color (C).
// 
// Parameters:
//   Ka, Kd, Ks, roughness, specularcolor - same as plastic
//   veinfreq - controls fhe lowest frequency of the color veins
//   veinlevels - how many "levels" of vein tendrills it has
//   warpfreq - lowest frequency of the turbulent warping in the marble
//   warping - controls how much turbulent warping there will be
//   veincolor - the color of the veins
//   sharpness - controls how sharp or fuzzy the veins are (higher = sharper)
//   Pref - if supplied, gives the reference pose
//   shadingspace - space in which the pattern is laid out
//   shadingfreq - overall scaling factor for the pattern
//
/////////////////////////////////////////////////////////////////////////////
// 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 "noises.h"
#include "material.h"



surface
veinedmarble 
    [[ string help = "Marble, procedural, with veins" ]]
(
    float Ka = .5
        [[  string help = "Ambient scaling",
            float min = 0, float max = 1 ]],
    float Kd = .8
        [[  string help = "Diffuse scaling",
            float min = 0, float max = 1 ]],
    float Ks = .4
        [[  string help = "Specular scaling",
            float min = 0, float max = 1 ]],
    float roughness = .075
        [[  string help = "Surface roughness (specular highlight size)",
            float min = 0, float max = 1 ]],
    color specularcolor = 1
        [[  string help = "Specular highlight color" ]],
    float veinfreq = 1
        [[  string help = "Vein frequency" ]],
    float veinlevels = 2
        [[  string help = "Vein levels",
            int min = 1, int max = 8 ]],
    color basecolor = C
        [[ string help = "Base marble color (defaults to C)" ]],
    color veincolor = color(.6,.5,.1)
        [[  string help = "Vein color" ]],
    float sharpness = 8
        [[  string help = "Vein sharpness" ]],
    point Pref = P
        [[  string help = "Reference pose (defaults to P)" ]],
    string shadingspace = "shader"
        [[  string help = "Shading space" ]],
    float shadingfreq = 1
        [[  string help = "Shading frequency" ]]
  )
{
    // Shade based on shader space
    point PP = shadingfreq * transform (shadingspace, Pref);
    float dPP = sqrt(area(PP));

    // Warp the spatial lookup
    PP += 0.5 * vfBm (PP, dPP, 6, 2, 0.5);

    // Now calculate the veining function for the lookup area
    float turbsum = 0;
    float freq = 1;
    PP *= veinfreq;
    dPP *= veinfreq;
    for (int i = 0;  i < (int)veinlevels;  i++) {
	float turb = abs (filteredsnoise (PP*freq, dPP*freq));
	turb = pow (smoothstep (0.8, 1, 1 - turb), sharpness) / freq;
	turbsum += (1-turbsum) * turb;
	freq *= 3;
	PP *= 3;
    }

    // Mix between base color and vein color
    color Ct = mix (basecolor, veincolor, turbsum);

    // Construct a frontfacing, unit-length normal
    normal Nf = faceforward (normalize(N),I);
    C = MaterialPlastic (Nf, Ct, Ka, Kd, Ks, roughness);
    C *= opacity;
}
