/////////////////////////////////////////////////////////////////////////////
// glass.osl -- Shiny reflective & refractive glass, using ray tracing
//              or environment mapping.
//
// Description:
//   Makes semi-transparent glass, using ray tracing to calculate
//   reflections and refractions of the environment.
// 
// Parameters:
//    Ka, Kd, Ks, roughness - The usual meaning
//    Kr - coefficient for mirror-like reflections of environment
//    reflblur - how blurry are the reflections? (0 = perfectly sharp)
//    envname - name of environment map, or geometry set for ray tracing
//              (setting to "" means no mirror reflections)
//    envspace - coord sys for env map lookups (ignored
//          for ray tracing)
//    samples - how many samples, for ray tracing blurry reflections
//    Kt - coefficient for coherent refractions of environment
//    refrblur - how blurry are the reractions? (0 = perfectly sharp)
//    eta - coefficient of refraction (ordinary glass is about 1.5)
//    transmitcolor - filter color for refracted light
//    refrsamples - how many samples, for ray tracing blurry refractions
//    twosided - if nonzero both sides of the surface are shiny, otherwise
//        only the "outside" (where the surface normal points) will
//        have reflections and refractions.  This can be an important
//        optimization, specially when ray tracing.
//    bgenvname, bgenvspace - give a name and space
//        for an environment map lookup that will be used instead of
//        ray tracing when reflection or refraction rays miss everything
//        entirely.
//    extinction - the distance (through the crystal) in which the
//        light falls to 1/e.  This value is a color, so you can make
//        glass have tinted absorption by making unequal values in 
//        the color channels.
//    dispersive - amount of dispersive refraction.  When 0, there is
//        no dispersion.  When nonzero, the amount scales how spread out
//        the rainbow gets.
//    fakerefract - if nonzero, uses "fake" (opacity-based) refraction
//        rather than ray tracing.
//    shadowcolor - scales the color of cast shadows.  If 0, the shadow cast
//        by the glass will be fully opaque.  If 1, the shdow cast will
//        be a semi-transparent shadow determined by the fresnel factor.
//        This also lets you cast colored shadows.  It is analogous to
//        transmitcolor, except specifically for shadows.
//
/////////////////////////////////////////////////////////////////////////////
// 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"



surface
glass
    [[ string description = "Glass, fully refractive" ]]
(
    float Ka = 0.2
        [[  string description = "Ambient scaling",
            float UImin = 0, float UIsoftmax = 1 ]],
    float Kd = 0
        [[  string description = "Diffuse scaling",
            float UImin = 0, float UIsoftmax = 1 ]],
    float Ks = 0.5
        [[  string description = "Specular scaling",
            float UImin = 0, float UIsoftmax = 1 ]],
    float roughness = 0.05
        [[  string description = "Surface roughness (specular highlight size)",
            float UImin = 0, float UImax = 1,
            string UIenabler = "Ks" ]],

    float Kr = 1
        [[  string description = "Reflectivity / Environment scaling",
            string UIlabel = "Reflectivity",
            float UImin = 0, float UIsoftmax = 1,
            string UIgroupbegin = "Reflections" ]],
    float reflblur = 0
        [[  string description = "Reflection environment blur",
            string UIenabler = "Kr",
            float UImin = 0, float UIsoftmax = 1 ]],
    string envname = ""
        [[  string description = "Map or geometry set name for reflections",
            string UIlabel = "Environment name",
            string UIenabler = "Kr",
            string UItype = "environment" ]],
    string envspace = "world"
        [[  string description = "Coordinate system for environment map lookup",
            string UIlabel = "Environment space",
            string UIenabler = "Kr",
            string UItype = "coordsys" ]],
    float samples = 1
        [[  string description = "Reflection samples (for ray tracing)",
            string UIlabel = "Reflection samples",
            string UIenabler = "Kr",
            string UItype = "int", float UImin = 1, float UIsoftmax = 64,
            string UIgroupend = "Reflections" ]],

    float Kt = 1
        [[  string description = "Refraction / Environment scaling",
            string UIlabel = "Refraction scaling",
            float UImin = 0, float UIsoftmax = 1,
            string UIgroupbegin = "Refraction" ]],
    float refrblur = 0
        [[  string description = "Refraction environment blur",
            string UIenabler = "Kt",
            float UImin = 0, float UIsoftmax = 1 ]],
    float eta = 1.5
        [[  string description = "Index of refraction",
            float UImin = 1, float UIsoftmax = 2 ]],
    color transmitcolor = 1
        [[  string description = "Refraction tint color",
            string UIenabler = "Kt" ]],
    float refrsamples = 1
        [[  string description = "Refraction samples (for ray tracing)",
            string UIlabel = "Refraction samples",
            string UIenabler = "Kt",
            string UItype = "int", float UImin = 1, float UIsoftmax = 64 ]],
    color extinction = 0
        [[  string description = "Extinction (0 = none)",
            string UIenabler = "Kt",
            float UImin = 0 ]],
    float dispersive = 0
        [[  string description = "Dispersive refraction",
            string UItype = "bool", string UIgroupend = "Refraction" ]],
    float fakerefract = 0
        [[  string description = "Use fake refraction instead of ray tracing",
            string UItype = "bool" ]],
    color shadowcolor = 1
        [[  string descriptions = "Shadow cast color",
            string UItype = "color" ]],
    string bgenvname = ""
        [[  string description = "Background environment map",
            string UItype = "environment",
            string UIgroupbegin = "Background env map (if rays miss)" ]],
    string bgenvspace = "world"
        [[  string description = "Coordinate system for background environment map lookup",
            string UIlabel = "Background Environment space",
            string UIenabler = "bgenvname",
            string UItype = "coordsys" ]],

    float twosided = 0
        [[  string description = "Two-sided reflection/refractions",
            string UItype = "bool" ]]
  )
{
    if (raytype("shadow")) {
        // If the shader is being run just to find the shadow color,
        // compute only that and exit early.
        float kt = 1;
        if (shadowcolor && dot(I,N) < 0) {
            float kr;
            vector R, T;
            normal Nf = faceforward (normalize(N), I);
            fresnel (normalize(I), Nf, 1.0/eta, kr, kt, R, T);
            kt = 1-kr;   /* Physically incorrect, but portable */
        }
        opacity = 1 - kt*Kt*shadowcolor;
        exit();
    }

    normal Nf = faceforward (normalize(N), I);
    opacity = 1;
    float kt = fakerefract ? 0 : Kt;
    if (dispersive && raylevel() < 2) {
        float disp = 0.01 * dispersive * (eta-1);
        color disp_eta = color (eta-disp, eta, eta+disp);
        C = MaterialGlass (Nf, C, Ka, Kd, Ks, roughness, Kr, reflblur, samples,
                           kt, refrblur, disp_eta, transmitcolor, refrsamples,
                           twosided, envname, envspace,
                           bgenvname, bgenvspace, extinction);
    }
    else {
        C = MaterialGlass (Nf, C, Ka, Kd, Ks, roughness, Kr, reflblur, samples,
                           kt, refrblur, eta, transmitcolor, refrsamples,
                           twosided, envname, envspace,
                           bgenvname, bgenvspace, extinction);
    }
    if (fakerefract) {
        if (dot(I,N) < 0) {
            float kr;
            vector R, T;
            fresnel (normalize(I), Nf, 1.0/eta, kr, kt, R, T);
            kt = 1-kr;   /* Physically incorrect, but portable */
        } else {
            kt = 1;
        }
        opacity = (1-Kt*kt*transmitcolor);
    } else {
        opacity = 1;
    }
}
