////////////////////////////////////////////////////////////////////////////////
// Filename: lightshader.hlsl
////////////////////////////////////////////////////////////////////////////////

/////////////
// GLOBALS //
/////////////
cbuffer MatrixBuffer : register(vs, cb0)
{
    matrix worldMatrix;
    matrix viewMatrix;
    matrix projectionMatrix;
    matrix objectMatrix;
};
cbuffer CameraBuffer : register(vs, cb1)
{
    float3 cameraPosition;
	float CameraBuffer_padding;
};
Texture2D shaderTextures[2];
SamplerState SampleType;

struct SPointLight
{
	float4 ambientColor;
	float4 diffuseColor;
	float4 specularColor;
	float3 position;
	float  intensityDecrease;
};

cbuffer LightBuffer : register(ps, cb0)
{
    float4 ambientColor;
    float4 diffuseColor;
	float4 specularColor;
    float3 globalLightDirection;
	float pointLightsCount;
	SPointLight pointLights[5];
};
cbuffer MaterialBuffer : register(ps, cb1)
{
    float4 materialAmbientColor;
    float4 materialDiffuseColor;
    float4 materialSpecularColor;
    float specularExponent;
	int currentMaterialId;
	int materialFlags;
	float MaterialBuffer_padding;
};

//////////////
// TYPEDEFS //
//////////////
struct VertexInputType
{
    float4 position : POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : NORMAL;
	int materialId : MATERIALID;
};

struct PixelInputType
{
    float4 position : SV_POSITION;
	float4 worldPosition : POSITION1;
    float2 tex : TEXCOORD0;
    float3 normal : NORMAL;
	float3 viewDirection : TEXCOORD1;
	int materialId : MATERIALID;
};

////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////
PixelInputType LightVertexShader(VertexInputType input)
{
    PixelInputType output;
    float4 worldPosition;
	matrix normalTransform;

    // Change the position vector to be 4 units for proper matrix calculations.
    input.position.w = 1.0f;
	
	normalTransform = mul(objectMatrix,worldMatrix);

    // Calculate the position of the vertex against the world, view, and projection matrices.
    output.worldPosition = mul(input.position, normalTransform);
    output.position = mul(output.worldPosition, viewMatrix);
    output.position = mul(output.position, projectionMatrix);

    // Store the texture coordinates for the pixel shader.
    output.tex = input.tex;

    // Calculate the normal vector against the world matrix only.
    output.normal = mul(input.normal, (float3x3)normalTransform);

    // Normalize the normal vector.
    output.normal = normalize(output.normal);

	// Calculate the position of the vertex in the world.
    worldPosition = mul(input.position, normalTransform);
	
    // Determine the viewing direction based on the position of the camera and the position of the vertex in the world.
    output.viewDirection = cameraPosition.xyz - worldPosition.xyz;
	
    // Normalize the viewing direction vector.
    output.viewDirection = normalize(output.viewDirection);
	
	output.materialId = input.materialId;
    return output;
}

////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 LightPixelShader(PixelInputType input) : SV_TARGET
{
    float4 textureColor;
    float3 lightDir;
    float lightIntensity;
    float4 ambientColorComponent = float4(.0f,.0f,.0f,.0f);
	float4 diffuseColorComponent = float4(.0f,.0f,.0f,.0f);
	float4 specularColorComponent = float4(.0f,.0f,.0f,.0f);
	float3 reflection;
    float4 specular;
	float4 bumpFragment;
    float3 resultNormal;
	clip((input.materialId == currentMaterialId || currentMaterialId == -1) ? 1 : -1);

    // Sample the pixel color from the texture using the sampler at this texture coordinate location.
    textureColor = shaderTextures[0].Sample(SampleType, input.tex);
	
	resultNormal = input.normal;
	
	if (materialFlags & 1)
	{
		// Sample the pixel in the bump map.
		bumpFragment = shaderTextures[1].Sample(SampleType, input.tex);

		// Expand the range of the normal value from (0, +1) to (-1, +1).
		//bumpFragment = (bumpFragment * 2.0f) - 1.0f;

		// Calculate the normal from the data in the bump map.
		//bumpNormal = (bumpMap.x * input.tangent) + (bumpMap.y * input.binormal) + (bumpMap.z * input.normal);
		resultNormal = (resultNormal + bumpFragment)/2;
	}

	// Normalize the resulting bump normal.
	resultNormal = normalize(resultNormal);
	
	// Set the default output color to the ambient light value for all pixels.
	ambientColorComponent = ambientColor;
	
	// Initialize the specular color.
    specular = float4(0.0f, 0.0f, 0.0f, 0.0f);
	
    // Invert the light direction for calculations.
    lightDir = -globalLightDirection;
	
    // Calculate the amount of light on this pixel.
    lightIntensity = saturate(dot(resultNormal, lightDir));
    if(lightIntensity > 0.0f)
    {
        // Determine the final diffuse color based on the diffuse color and the amount of light intensity.
        diffuseColorComponent += diffuseColor * lightIntensity;
				
		// Calculate the reflection vector based on the light intensity, normal vector, and light direction.
        reflection = normalize(2 * lightIntensity * resultNormal - lightDir); 

        // Determine the amount of specular light based on the reflection vector, viewing direction, and specular power.
        specularColorComponent += specularColor*pow(saturate(dot(reflection, input.viewDirection)), specularExponent);
    }

    // Calculate the amount of light on this pixel for POINT LIGHT
	for (int i = 0; i < pointLightsCount; ++i)
	{
		lightDir = pointLights[i].position - input.worldPosition;
		float deltaIntensity = pointLights[i].intensityDecrease*length(lightDir);
		ambientColorComponent += clamp(pointLights[i].ambientColor - deltaIntensity,0.0f,100.0f);
		lightDir = normalize(lightDir);
		lightIntensity = saturate(dot(resultNormal, lightDir));
		if(lightIntensity > 0.0f)
		{
			// Determine the final diffuse color based on the diffuse color and the amount of light intensity.
			diffuseColorComponent += clamp(pointLights[i].diffuseColor - deltaIntensity,0.0f,100.0f)* lightIntensity;
				
			// Calculate the reflection vector based on the light intensity, normal vector, and light direction.
			reflection = normalize(2 * lightIntensity * resultNormal - lightDir); 

			// Determine the amount of specular light based on the reflection vector, viewing direction, and specular power.
			specularColorComponent += clamp(pointLights[i].specularColor - deltaIntensity,0.0f,100.0f)*pow(saturate(dot(reflection, input.viewDirection)), specularExponent);
		}
	}
	
    // Multiply the texture pixel and the final diffuse color to get the final pixel color result.
    diffuseColorComponent = (ambientColorComponent*materialAmbientColor + diffuseColorComponent*materialDiffuseColor) * textureColor;

	// Add the specular component last to the output color.
	specularColorComponent = saturate(diffuseColorComponent + specularColorComponent * materialSpecularColor);
	
    return specularColorComponent;
}


