/////////////////////////////////////////////////////////////////////////////
// checker.osl - layer shader that generates a checker pattern
//
// Description:
//   Generates an antialiased checker pattern, alternating between
//   two colors.  By default, color1 is C, color2 is black.
//   This shader does not compute any lighting -- it's meant to be used
//   as a layer, connecting Cout or fout outputs as inputs to other layers.
//
// Parameters:
//   color1, color2 - checker colors (default to C and black, respectively)
//   s,t - texture coordinates (default to u,v)
//   sfreq, tfreq - frequency of checker pattern in st space
//
// Outputs:
//   Cout - color texture
//   fout - float (0-1) representation of the checker 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 "patterns.h"


shader
checker 
    [[ string description = "Layer with an antialiased checker pattern",
       float layer = 1 ]]
(
    color color1 = C
        [[  string description = "Checker color #1" ]],
    color color2 = 0
        [[  string description = "Checker color #2" ]],
    float s = u
        [[  string description = "s coordinate (defaults to u)" ]],
    float t = v
        [[  string description = "t coordinate (defaults to v)" ]],
    float stilesize = 1
        [[  string description = "Tile size in the s direction",
            float UImin = 0 ]],
    float ttilesize = 1
        [[  string description = "Tile size in the t direction",
            float UImin = 0 ]],
    output color Cout = 0
        [[  string description = "Color (3 channel) output" ]],
    output float fout = 0
        [[  string description = "Float (1 channel) output" ]]
  )
{
    float sfreq = 1/stilesize;
    float tfreq = 1/ttilesize;
    float x = filteredpulsetrain (1, 2, s*sfreq, filterwidth(s) * sfreq);
    float y = filteredpulsetrain (1, 2, t*tfreq, filterwidth(t) * tfreq);
    fout = x*y + (1-x)*(1-y);
    Cout = mix (color1, color2, fout);
}
