[ad_1]

I am struggling so as to add specular lighting on-top of diffuse lighting and never solely positive the place I am going unsuitable. I’ve adopted the directions given in our lecture but it surely simply would not appear to be showing on-top in any respect.

picture of textured sphere with diffuse lighting

light_vs.hlsl

// Mild vertex shader
// Commonplace situation vertex shader, apply matrices, go data to pixel shader
cbuffer MatrixBuffer : register(b0)
{
    matrix worldMatrix;
    matrix viewMatrix;
    matrix projectionMatrix;
};

cbuffer CameraBuffer : register(b1)
{
    float3 cameraPosition;
    float padding;
};

struct InputType
{
    float4 place : POSITION;
    float2 tex : TEXCOORD0;
    float3 regular : NORMAL;
};

struct OutputType
{
    float4 place : SV_POSITION;
    float2 tex : TEXCOORD0;
    float3 regular : NORMAL;
    float3 worldPosition : TEXCOORD1;
    float3 viewVector : TEXCOORD2;
};

OutputType principal(InputType enter)
{
    OutputType output;

    // Calculate the place of the vertex on this planet
    float4 worldPosition = mul(enter.place, worldMatrix);
    output.viewVector = cameraPosition.xyz - worldPosition.xyz;
    output.viewVector = normalize(output.viewVector);
    
    // Calculate the place of the vertex towards the world, view, and projection matrices.
    output.place = mul(enter.place, worldMatrix);
    output.place = mul(output.place, viewMatrix);
    output.place = mul(output.place, projectionMatrix);

    output.worldPosition = mul(enter.place, worldMatrix).xyz;
    
    // Retailer the feel coordinates for the pixel shader.
    output.tex = enter.tex;
    
    // Calculate the traditional vector towards the world matrix solely and normalise.
    output.regular = mul(enter.regular, (float3x3) worldMatrix);
    output.regular = normalize(output.regular);

    return output;
}

light_ps.hlsl

// Mild pixel shader
// Calculate diffuse lighting for a single directional mild (additionally texturing)

Texture2D texture0 : register(t0);
SamplerState sampler0 : register(s0);

cbuffer LightBuffer : register(b0)
{
    float4 ambient;
    float4 diffuse;
    float3 place;
    float padding;
    float specularPower;
    float4 specular;
};

struct InputType
{
    float4 place : SV_POSITION;
    float2 tex : TEXCOORD0;
    float3 regular : NORMAL;
    float3 worldPosition : TEXCOORD1;
    float3 viewVector : TEXCOORD2;
};

// Calculate lighting depth based mostly on path and regular. Mix with mild color.
float4 calculateLighting(float3 lightDirection, float3 regular, float4 ldiffuse)
{
    float depth = saturate(dot(regular, lightDirection));
    float4 color = saturate(ldiffuse * depth);
    return color;
}

float4 calcSpecular(float3 lightDirection, float3 regular, float3 viewVector, float4 specularColour, float specularPower)
{
    // blinn-phong specular calculation
    float3 midway = normalize(lightDirection + viewVector);
    float specularIntensity = pow(max(dot(regular, midway), 0.0), specularPower);
    return saturate(specularColour * specularIntensity);

}

float4 principal(InputType enter) : SV_TARGET
{
    // Pattern the feel. Calculate mild depth and color, return mild*texture for ultimate pixel color.
    float4 textureColour = texture0.Pattern(sampler0, enter.tex);
    float3 lightVector = normalize(place - enter.worldPosition);
    float4 lightColour = ambient + calculateLighting(lightVector, enter.regular, diffuse);
    
    float4 specularColour = float4(1.0f, 1.0f, 1.0f, 1.0f);
    float specularPower = 512.0f;
    float4 specular = calcSpecular(lightVector, enter.regular, enter.viewVector, specularColour, specularPower);
    
    return (lightColour + specular) * textureColour;
}

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *