/*********************************************************************NVMH3****
File:  $Id: //sw/devtools/FXComposer2/Alpha4+/SDK/MEDIA/CgFX1.4/kajiya_kay.cgh#1 $

Copyright NVIDIA Corporation 2005
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS)
ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

Comments:
    Kajiya-Kay "hair" function along with texture-generation shortcuts

******************************************************************************/

#ifndef _H_KAJIYA_KAY
#define _H_KAJIYA_KAY

//////////////////////////////////////////////////////////
// CONSTANTS USED IN TEXTURING - USER-OVERRIDE-ABLE //////
//////////////////////////////////////////////////////////

#ifndef KK_SIZE 
#define KK_SIZE 128
#endif /* KK_SIZE  */

#ifndef KK_KD 
#define KK_KD 1.0
#endif /* KK_KD  */

#ifndef KK_KS 
#define KK_KS 1.0
#endif /* KK_KS  */

#ifndef KK_PHONG_EXPON 
#define KK_PHONG_EXPON 30.0
#endif /* KK_PHONG_EXPON  */

#ifndef KK_TEX_FORMAT 
#define KK_TEX_FORMAT "A8B8G8R8"
#endif /* KK_TEX_FORMAT  */

#ifndef KK_SURF_COLOR 
#define KK_SURF_COLOR float4(1.0,0.4,0.1,1.0)
#endif /* KK_SURF_COLOR  */

#ifndef KK_SPEC_COLOR 
#define KK_SPEC_COLOR float4(1.0,1.0,1.0,1.0)
#endif /* KK_SPEC_COLOR  */

//////////////////////////////////////////////////////////
// FUNCTIONS /////////////////////////////////////////////
//////////////////////////////////////////////////////////

//
// The base KK function, provided for shaders that want
//	to calculate it numerically
//
float4 kajiya_kay_core(float3 Tn, // direction of hair - normalized
			float3 En, // ey vector - normalized
			float3 Ln, // light vector - normalized
			float Kd,
			float Ks,
			float PhongExpon,
			float4 SurfColor,
			float4 SpecColor)
{
    float TdL = dot(Tn,Ln);
    float TdE = dot(Tn,En);
    float TsL = sin(acos(TdL));
    float TsE = sin(acos(TdE));
    float diff = Kd * TsL;
    float spec = Ks * pow((TdL*TdE + TsL*TsE),PhongExpon);
    float4 dc = SurfColor*diff;
    float4 sc = SpecColor*spec;
    return float4(float3(dc.xyz+sc.xyz),SurfColor.w);
}

/////////////////////////////////////
// Texture Shortcut Factorizations //
/////////////////////////////////////
//
// The K-K function can be factored on two dot products.
//   If T is the hair growth direction, and L and E are Light and Eye
//   Vectors, then the function can be precalculated and factorized
//   on (T.L,T.E) for any predetermined Ks, Kd, and Phong Exponent
//

/////////////// GRAYSCALE Version //////////////

#ifndef KK_COLOR_ONLY

// KK function used to fill the Kajiya-Kay function map
float4 kajiya_kay_gray_tex(float2 Pos : POSITION) : COLOR
{
    float TdL = 2.0*(Pos.x-0.5);
    float TdE = 2.0*(Pos.y-0.5);
    float TsL = sin(acos(TdL));
    float TsE = sin(acos(TdE));
    float diff = KK_KD * TsL;
    float spec = KK_KS * pow((TdL*TdE + TsL*TsE),KK_PHONG_EXPON);
    float result = (diff+spec);
    return float4(result.xxxx);
}

texture KKGrayTex  <
    string TextureType = "2D";
    string function = "kajiya_kay_gray_tex";
    string Format = (KK_TEX_FORMAT);
    string UIWidget = "None";
    int width = KK_SIZE, height = KK_SIZE;
>;

// samplers
sampler KKGraySamp = sampler_state 
{
    texture = <KKGrayTex>;
	MinFilter = LinearMipMapLinear;
	MagFilter = Linear;
    WrapS = Clamp;
    WrapT = Clamp;
};
#endif /* KK_COLOR_ONLY */

/////////////// COLOR Version //////////////

#ifndef KK_GRAY_ONLY
// KK function used to fill the Kajiya-Kay function map
float4 kajiya_kay_color_tex(float2 Pos : POSITION) : COLOR
{
    float TdL = 2.0*(Pos.x-0.5);
    float TdE = 2.0*(Pos.y-0.5);
    float TsL = sin(acos(TdL));
    float TsE = sin(acos(TdE));
    float diff = KK_KD * TsL;
    float spec = KK_KS * pow((TdL*TdE + TsL*TsE),KK_PHONG_EXPON);
    float4 dc = KK_SURF_COLOR*diff;
    float4 sc = KK_SPEC_COLOR*spec;
    return float4(float3(dc.xyz+sc.xyz),KK_SURF_COLOR.w);
}

texture KKColorTex  <
    string TextureType = "2D";
    string function = "kajiya_kay_color_tex";
    string Format = (KK_TEX_FORMAT);
    string UIWidget = "None";
    int width = KK_SIZE, height = KK_SIZE;
>;

// samplers
sampler KKColorSamp = sampler_state 
{
    texture = <KKColorTex>;
	MinFilter = LinearMipMapLinear;
	MagFilter = Linear;
    WrapS = Clamp;
    WrapT = Clamp;
};

#endif /* KK_GRAY_ONLY */
#endif /* _H_KAJIYA_KAY */

/////////////////////////////////////// eof ///
