[ad_1]
I’m struggling to add specular lighting on-top of diffuse lighting and not entirely sure where I’m going wrong. I’ve followed the instructions given in our lecture but it just doesn’t seem to be appearing on-top at all.
light_vs.hlsl
// Light vertex shader
// Standard issue vertex shader, apply matrices, pass info to pixel shader
cbuffer MatrixBuffer : register(b0)
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};
cbuffer CameraBuffer : register(b1)
{
float3 cameraPosition;
float padding;
};
struct InputType
{
float4 position : POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
};
struct OutputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
float3 worldPosition : TEXCOORD1;
float3 viewVector : TEXCOORD2;
};
OutputType main(InputType input)
{
OutputType output;
// Calculate the position of the vertex in the world
float4 worldPosition = mul(input.position, worldMatrix);
output.viewVector = cameraPosition.xyz - worldPosition.xyz;
output.viewVector = normalize(output.viewVector);
// Calculate the position of the vertex against the world, view, and projection matrices.
output.position = mul(input.position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);
output.worldPosition = mul(input.position, worldMatrix).xyz;
// Store the texture coordinates for the pixel shader.
output.tex = input.tex;
// Calculate the normal vector against the world matrix only and normalise.
output.normal = mul(input.normal, (float3x3) worldMatrix);
output.normal = normalize(output.normal);
return output;
}
light_ps.hlsl
// Light pixel shader
// Calculate diffuse lighting for a single directional light (also texturing)
Texture2D texture0 : register(t0);
SamplerState sampler0 : register(s0);
cbuffer LightBuffer : register(b0)
{
float4 ambient;
float4 diffuse;
float3 position;
float padding;
float specularPower;
float4 specular;
};
struct InputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
float3 worldPosition : TEXCOORD1;
float3 viewVector : TEXCOORD2;
};
// Calculate lighting intensity based on direction and normal. Combine with light colour.
float4 calculateLighting(float3 lightDirection, float3 normal, float4 ldiffuse)
{
float intensity = saturate(dot(normal, lightDirection));
float4 colour = saturate(ldiffuse * intensity);
return colour;
}
float4 calcSpecular(float3 lightDirection, float3 normal, float3 viewVector, float4 specularColour, float specularPower)
{
// blinn-phong specular calculation
float3 halfway = normalize(lightDirection + viewVector);
float specularIntensity = pow(max(dot(normal, halfway), 0.0), specularPower);
return saturate(specularColour * specularIntensity);
}
float4 main(InputType input) : SV_TARGET
{
// Sample the texture. Calculate light intensity and colour, return light*texture for final pixel colour.
float4 textureColour = texture0.Sample(sampler0, input.tex);
float3 lightVector = normalize(position - input.worldPosition);
float4 lightColour = ambient + calculateLighting(lightVector, input.normal, diffuse);
float4 specularColour = float4(1.0f, 1.0f, 1.0f, 1.0f);
float specularPower = 512.0f;
float4 specular = calcSpecular(lightVector, input.normal, input.viewVector, specularColour, specularPower);
return (lightColour + specular) * textureColour;
}
[ad_2]