/*********************************************************************NVMH3****
Comments:
    Bumpy, fresnel-shiny, dielectric, textured, with two quadratic-falloff lights

******************************************************************************/

float Script : STANDARDSGLOBAL <
    string UIWidget = "none";
    string ScriptClass = "object";
    string ScriptOrder = "standard";
    string ScriptOutput = "color";
    string Script = "Technique=DirectX9;";
> = 0.8;

/************* TWEAKABLES **************/

float4x4 WorldIT : WorldInverseTranspose < string UIWidget = "None"; >;
float4x4 WorldViewProj : WorldViewProjection < string UIWidget = "None"; >;
float4x4 World : World < string UIWidget = "None"; >;
float4x4 ViewInv : ViewInverse < string UIWidget = "None"; >;

////////////////////////////////////////////// lamp 1

float4 LightDirInverse1
<
    string Object = "LightDirInverse1";
    string Space = "World";
> = {1.0f, 0.0f, 0.0f, 0.0f};


float4 LightColor1 : Specular <
    string UIName =  "Light1 Color";
    string Object = "Pointlight";
    string UIWidget = "Color";
> = {1.0f, 1.0f, 1.0f, 1.0f};

float LightIntensity1
<
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 50.0;
    float UIStep = 0.1;
    string UIName =  "lamp1 power";
> = 2.0;

////////////////////////////////////////////// lamp 2

float4 LightDirInverse2 
<
    string Object = "LightDirInverse2";
    string Space = "World";
> = {0.0f, 1.0f, 0.0f, 0.0f};


float4 LightColor2 : Specular <
    string UIName =  "Light2 Color";
    string Object = "Pointlight";
    string UIWidget = "Color";
> = {1.0f, 1.0f, 1.0f, 1.0f};


float LightIntensity2
<
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 50.0;
    float UIStep = 0.1;
    string UIName =  "lamp2 power";
> = 2.0;

////////////////////////////////////////////// surface

float4 AmbiColor : Ambient <
    string UIName =  "Ambient Light Color";
    string UIWidget = "Color";
> = {0.37f, 0.37f, 0.37f, 1.0f};

float4 DiffuseColor : Diffuse
<
    string UIName =  "Surface Color";
    string UIWidget = "Color";
> = {1.0f, 1.0f, 1.0f, 1.0f};

float Kd <
    string UIWidget = "slider";
    float UIMin = 0.0;
    float UIMax = 1.5;
    float UIStep = 0.01;
    string UIName =  "diffuse";
> = 1.0;

float Ks <
    string UIWidget = "slider";
    float UIMin = 0.0;
    float UIMax = 1.5;
    float UIStep = 0.01;
    string UIName =  "specular";
> = 1.0;


float SpecExpon : SpecularPower <
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 128.0;
    float UIStep = 1.0;
    string UIName =  "specular power";
> = 42.0;

float Bumpy <
    string UIWidget = "slider";
    float UIMin = 0.0;
    float UIMax = 10.0;
    float UIStep = 0.1;
    string UIName =  "bumpiness";
> = 1.0;

float Kr <
    string UIWidget = "slider";
    float UIMin = 0.0;
    float UIMax = 1.5;
    float UIStep = 0.01;
    string UIName =  "Reflection Max";
> = 1.0;


float KrMin <
    string UIWidget = "slider";
    float UIMin = 0.0;
    float UIMax = 0.2;
    float UIStep = 0.001;
    string UIName =  "Reflection Min";
> = 0.002;

float FresExp : SpecularPower <
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 7.0;
    float UIStep = 0.1;
    string UIName =  "edging";
> = 5.0;


float  Opacity
<
    string SasUiLabel = "Opacity";
    string SasUiControl = "Slider"; 
    float SasUiMin = 0.0f; 
    float SasUiMax = 1.0f; 
    int SasUiSteps = 0.01;
> = 1.0f;

/////////////////////////////////

texture colorTexture : Diffuse <
    string ResourceName = "default_color.dds";
    string ResourceType = "2D";
>;

texture normalTexture : NORMAL <
    string ResourceName = "default_bump_normal.dds";
    string ResourceType = "2D";
>;


texture envTexture : ENVIRONMENT <
    string ResourceName = "EnvironmentCube.dds";
    string ResourceType = "Cube";
>;

////////

sampler2D colorSampler = sampler_state {
	Texture = <colorTexture>;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
	AddressU = Wrap;
	AddressV = Wrap;
};

sampler2D normalSampler = sampler_state {
	Texture = <normalTexture>;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
	AddressU = Wrap;
	AddressV = Wrap;
};


samplerCUBE envSampler = sampler_state {
	Texture = <envTexture>;
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
	AddressU = clamp;
	AddressV = clamp;
	AddressW = clamp;
};

/************* DATA STRUCTS **************/

/* data from application vertex buffer */
struct appdata {
    float3 Position	: POSITION;
    float3 Normal	: NORMAL;
    float2 UV		: TEXCOORD0;
    float3 Tangent	: TANGENT0;
    float3 Binormal	: BINORMAL0;
};

/* data passed from vertex shader to pixel shader */
struct vertexOutput {
    float4 HPosition	: POSITION;
    float2 TexCoord	: TEXCOORD0;
    float3 WorldNormal	: TEXCOORD1;
    float3 WorldPos	: TEXCOORD2;
    float3 WorldEyePos	: TEXCOORD3;
    float3 WorldTangent	: TEXCOORD4;
    float3 WorldBinorm	: TEXCOORD5;
};

/*********** vertex shader ******/

vertexOutput mainVS(appdata IN) {
    vertexOutput OUT;
    OUT.WorldNormal = mul(IN.Normal, WorldIT).xyz;
    OUT.WorldTangent = mul(IN.Tangent, WorldIT).xyz;
    OUT.WorldBinorm = mul(IN.Binormal, WorldIT).xyz;
    float4 vertPosIn = float4(IN.Position.xyz,1.0);
    OUT.WorldPos = mul(vertPosIn, World).xyz;
    OUT.TexCoord = IN.UV;
    OUT.WorldEyePos = ViewInv[3].xyz;
    OUT.HPosition = mul(vertPosIn, WorldViewProj);
    return OUT;
}


/********* pixel shader ********/

float4 mainPS2(vertexOutput IN) : COLOR {

float4 map = tex2D(colorSampler,IN.TexCoord.xy);
float3 Nn = normalize(IN.WorldNormal);
float3 ViewDir = normalize(IN.WorldEyePos - IN.WorldPos);			// viewDir

float3 reflVect = reflect(ViewDir, Nn);
float4 reflAgy = texCUBE(envSampler, float4(reflVect.x, reflVect.y, reflVect.z, 1.0));
return reflAgy;
//return float4(1.0, 1, 0, 1);
//return float4(IN.TexCoord.x, IN.TexCoord.y, 0, 0.5);
//return float4(Nn.x, Nn.y, Nn.z, 1);

}


float4 mainPS(vertexOutput IN) : COLOR {
    float4 map = tex2D(colorSampler,IN.TexCoord.xy);
    float3 bumps = Bumpy * (tex2D(normalSampler,IN.TexCoord.xy).xyz-(0.5).xxx);
    float3 Nn = normalize(IN.WorldNormal);
    float3 Tn = normalize(IN.WorldTangent);
    float3 Bn = normalize(IN.WorldBinorm);
    float3 Nperturbed = normalize(Nn + (bumps.x * Tn + bumps.y * Bn));		// perturbedNormal
    float3 ViewDir = normalize(IN.WorldEyePos - IN.WorldPos);			// viewDir

    float3 Hn = normalize(ViewDir + LightDirInverse1);
    float hdn = pow(max(0.00001f,dot(Hn,Nperturbed)),SpecExpon);
    float lightInFloat = dot(LightDirInverse1,Nperturbed);
    lightInFloat = max(0.0,lightInFloat) * LightIntensity1;
    float4 diffContrib = lightInFloat * LightColor1;
    float4 specContrib = lightInFloat * LightColor1 * hdn ;

    // second lamp
    Hn = normalize(ViewDir + LightDirInverse2);
    hdn = pow(max(0.00001f,dot(Hn,Nperturbed)),SpecExpon);
    lightInFloat = dot(LightDirInverse2,Nperturbed);
    lightInFloat = max(0.0,lightInFloat) * LightIntensity2;
    diffContrib = diffContrib + lightInFloat * LightColor2;
    specContrib = specContrib + lightInFloat * LightColor2 * hdn;

    float3 reflVect = reflect(ViewDir, Nperturbed);
    float4 reflAgy = texCUBE(envSampler, float4(reflVect.x, reflVect.y, reflVect.z, 1.0));
    //float4 result = float4((DiffuseColor*map*Kd*diffContrib * 0.3 + specContrib * Ks * 0.6 + DiffuseColor*reflAgy).xyz, Opacity);
    float4 result = float4((DiffuseColor*Kd*diffContrib * 0.5 + specContrib * Ks * 0.3 + DiffuseColor*reflAgy * 0.3).xyz, Opacity);
	return result;
//return float4(1.0, 1, 0, 1);
//return float4(Nn.x, Nn.y, Nn.z, 1);
	
//	return float4(IN.TexCoord.x, IN.TexCoord.y, 0, 0.5);
	//return float4(IN.WorldEyePos.x, IN.WorldEyePos.y, IN.WorldEyePos.z, 0.5);
	//return float4(IN.WorldPos.x, IN.WorldPos.y, IN.WorldPos.z, 0.5);
	//float3 normalizedWorldPos = normalize(IN.WorldPos);
	//return float4(normalizedWorldPos.x, normalizedWorldPos.y, normalizedWorldPos.z, 0.5);
	//float3 normalizedEyePos = normalize(IN.WorldEyePos) / 2 + 0.5;
	//return float4(normalizedEyePos.x, normalizedEyePos.y, normalizedEyePos.z, 0.5);

}

/*
float4 mainPS(vertexOutput IN) : COLOR {
    float4 map = tex2D(colorSampler,IN.TexCoord.xy);
    float3 bumps = Bumpy * (tex2D(normalSampler,IN.TexCoord.xy).xyz-(0.5).xxx);
    float gloss = Ks;
    float3 Nn = normalize(IN.WorldNormal);
    float3 Tn = normalize(IN.WorldTangent);
    float3 Bn = normalize(IN.WorldBinorm);
    float3 Nb = Nn + (bumps.x * Tn + bumps.y * Bn);
    Nb = normalize(Nb);
    float3 Vn = normalize(IN.WorldEyePos - IN.WorldPos);

    float3 Ln = normalize(IN.LightVec1.xyz);
    float3 Hn = normalize(Vn + Ln);
    float hdn = pow(max(0.00001f,dot(Hn,Nb)),SpecExpon) * gloss;
    float ldn = dot(Ln,Nb);
    ldn = (IN.LightVec1.w* max(0.0,ldn)) * LightIntensity1;
    float4 diffContrib = IN.LightVec1.w*(ldn*LightColor1);
    float4 specContrib = IN.LightVec1.w*((ldn * 1) * LightColor1);

    // second lamp
    Ln = normalize(IN.LightVec2.xyz);
    Hn = normalize(Vn + Ln);
    hdn = pow(max(0.00001f,dot(Hn,Nb)),SpecExpon) * gloss;
    ldn = dot(Ln,Nb);
    ldn = (IN.LightVec2.w* max(0.0,ldn)) * LightIntensity2;
    diffContrib = diffContrib + IN.LightVec2.w*(ldn*LightColor2);
    specContrib = specContrib + IN.LightVec2.w*((ldn * hdn) * LightColor2);

    float3 reflVect = reflect(Vn,Nb);
    float vdn = dot(Vn,Nb);
    float fres = KrMin + (Kr-KrMin) * pow(max(0.00001f,(1-abs(vdn))),FresExp);
    float4 reflColor = fres * texCUBE(envSampler,float4(reflVect.x, reflVect.y, reflVect.z, 1));

    //float4 result = (DiffuseColor*map*(Kd*diffContrib+AmbiColor)) + specContrib + reflColor;
    // for GS: float4 result = (DiffuseColor*map*(Kd*diffContrib+AmbiColor))* reflColor * 8;
    // float4 result = (DiffuseColor*map*(Kd*diffContrib+AmbiColor))* reflColor * 8 + specContrib;	// this was the used
	float4 result = float4(2 *texCUBE(envSampler,float4(reflVect.x, reflVect.y, reflVect.z, 0.1)).xyz, Opacity);
     //float4 result = specContrib;
   // float4 result = float4(1, 0, 1, Opacity);
	//float4 result = float4(map.xyz, Opacity);
	//float4 result = float4(tex2D(normalSampler,IN.TexCoord.xy).xyz, Opacity);
	 
    

    //return float4(Vn.x, Vn.y, Vn.z, 0.5);
    //return float4(Nb.x, Nb.y, Nb.z, 1);
    //return float4(Nn.x, Nn.y, Nn.z, 1);
   //return DiffuseColor*map*(Kd*diffContrib);
  return result;
}
*/

/*************/

technique DirectX9 <
	string Script = "Pass=p0;";
> {
	pass p0 <
	string Script = "Draw=geometry;";
> {
		VertexShader = compile vs_2_0 mainVS();
		ZEnable = true;
		ZWriteEnable = true;
		//CullMode = None;
		//AlphaBlendEnable = true;
		PixelShader = compile ps_2_0 mainPS();
	}
}

/***************************** eof ***/
