programming4us
programming4us
MULTIMEDIA

Programming with DirectX : Implementing Per-Pixel Lighting (part 1) - Creating the Shaders

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
Creating the Shaders

The HLSL code for the effects is stored in LambertBlinnPhong.fx. In the vertex shader, the code starts off by transforming the vertex position as usual, and then it moves on to compute the transformed normal vector to account for the object rotating. It computes the light vector, which is computed by normalizing the light position from the vertex position, and the view vector, which is the normalized difference of the camera position and the vertex position. The vectors are interpolated across the surface as they are passed to the pixel shader from the vertex shader, which is the same as if we calculated the vectors in the pixel shader using the computed pixel position. We hold off on normalizing until we reach the pixel shader stage since the interpolation could possibly de-normalize the vectors, and we’d have to re-normalize anyway just to keep this from happening.

In the pixel shader all vectors are normalized, and the half vector is computed for the Blinn-Phong algorithm. Once the vectors are normalized, the diffuse contribution is computed by calculating N dot L, and the specular contribution is computed from N dot H raised to a power that represents the shininess factor, which in this demo is 25. Since this demo is just a simple example of lighting, the diffuse and specular values are multiplied by the color white and stored as the output color. We could have multiplied them by a specific object color, light color, texture color, and so forth.

The LambertBlinnPhong.fx shader is shown in Listing 1.

Listing 1. The LambertBlinnPhong.fx Shader
float4 lightPos;
float4 eyePos;


DepthStencilState DepthStencilInfo
{
DepthEnable = true;
DepthWriteMask = ALL;
DepthFunc = Less;
};


cbuffer cbChangesEveryFrame
{
matrix World;
matrix View;
};

cbuffer cbChangeOnResize
{
matrix Projection;
};


struct VS_INPUT
{
float4 Pos : POSITION;
float3 Norm : NORMAL;
float2 Tex : TEXCOORD;
};

struct PS_INPUT
{
float4 Pos : SV_POSITION;
float3 Norm : NORMAL;
float3 LightVec : TEXCOORD0;
float3 ViewVec : TEXCOORD1;
};


PS_INPUT VS(VS_INPUT input)
{
PS_INPUT output = (PS_INPUT)0;

float4 Pos = mul(input.Pos, World);
Pos = mul(Pos, View);
output.Pos = mul(Pos, Projection);

output.Norm = mul(input.Norm, World);
output.Norm = mul(output.Norm, View);

output.LightVec = lightPos.xyz - Pos.xyz;
output.ViewVec = eyePos.xyz - Pos.xyz;

return output;
}


float4 PS(PS_INPUT input) : SV_Target
{
float3 normal = normalize(input.Norm);
float3 lightVec = normalize(input.LightVec);
float3 viewVec = normalize(input.ViewVec);
float3 halfVec = normalize(lightVec + viewVec);
float diffuse = saturate(dot(normal, lightVec));
float specular = pow(saturate(dot(normal, halfVec)), 25);

float4 white = float4(1, 1, 1, 1);

return white * diffuse + white * specular;
}


technique10 BlinnPhongSpecular
{
pass P0
{
SetDepthStencilState(DepthStencilInfo, 0);

SetVertexShader(CompileShader(vs_4_0, VS()));
SetGeometryShader(NULL);
SetPixelShader(CompileShader(ps_4_0, PS()));
}
}


Other  
  •  Programming with DirectX : Basic Lighting Information - Diffuse Light , Specular Light , Blinn-Phong Specular Model
  •  Programming with DirectX : Light Types - Directional Lights,Point Lights,Spot Lights, Area Lights
  •  Programming with DirectX : Overview of Lighting - Pros and Cons of Per-Vertex Lighting, Per-Pixel Lighting
  •  What Will 2014 Bring To Tech Lovers?
  •  DirectX 10 Game Programming : Direct3D - Textures and Details (part 3) - Drawing the Textured Object
  •  DirectX 10 Game Programming : Direct3D - Textures and Details (part 1) - Adding Support for Texture Mapping - Texturing Coordinates and Sampling
  •  DirectX 10 Game Programming : Direct3D - Textures and Details (part 1) - Adding Support for Texture Mapping - The Vertex Format
  •  DirectX 10 Game Programming : Shaders and Effects - Geometry Shaders
  •  Adobe Photoshop CS5 : Managing Color from Monitor to Print - Working with Rendering Intents
  •  Adobe Photoshop CS5 : Managing Color from Monitor to Print - Changing from Additive (RGB) to Subtractive (CMYK) Color
  •  
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us
    programming4us
     
     
    programming4us