Customise Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

No cookies to display.

[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 *