DirectX tutorial (28) Simple Lighting Model (7)

Source: Internet
Author: User

In real life, the point light source degrades with distance. For example, a light bulb emits bright light near it, and the light in the distance is weak. In this section, we add an attenuation factor to the diffuse reflection and highlight based on the previous optical formula.

The attenuation of the light source with distance is not purely linear. the commonly used formula is:

  • DThe distance from the light source to the colored point.
  • KC,KL, AndKQThey are constants, linearity, and secondary attenuation coefficients.

Now in light. ps, the code for calculating the light is changed:

For (I = 0; I <num_lights; I ++)
{
// Self-emission color
Emissive = ke [I];

// Computing environment light
Ambient = Ka [I] * globalambient [I];

// Calculate the diffuse reflection light
// Lightdirection is a pure parallel light.
// The Position of the light source minus the vertex position
L = normalize (lightposition [I]. XYZ-P );
D = distance (lightposition [I]. XYZ, P );
// Attenuation coefficient
Atte = 1/(attenuation [I]. x + attenuation [I]. y * D + attenuation [I]. z * D );
Diffuselight = max (dot (n, l), 0 );
Diffuse = KD [I] * lightcolor [I] * diffuselight * atte;

// Calculate the highlight
V = normalize (cameraposition. XYZ-P );
H = normalize (L + V );
Specularlight = POW (max (dot (n, h), 0), shininess [I]);

If (diffuselight <= 0)
Specularlight = 0;
Specular = KS [I] * lightcolor [I] * specularlight * atte;

Finalcolor + = emissive + ambient + DIFFUSE + specular;
}

Correspondingly, the struct lightmaterialbuffertype in lightshaderclass. h must also be changed. Add a d3dxvector4 component attenuation. Its X, Y, and Z represent constants, linearity, and secondary attenuation coefficients respectively. D3dvector4 is used because the const buffer requirement is a multiple of 4. I tried to use three float statements, and the result program has a compilation error.

...

D3dxvector4 attenuation [num_lights]; // attenuation coefficient, which corresponds to constants X, Y, and Z, linear and quadratic Coefficients

...

In light. ps, const buffer lightmaterialbuffer also needs to increase the attenuation factor, which corresponds to attenuation in lightmaterialbuffertype.

Float4 attenuation [num_lights]; // attenuation coefficient

After the program is executed, the effect is as follows:

Complete code can be found:

Project File mytutoriald3d11_21

Download Code:

Http://files.cnblogs.com/mikewolf2002/myTutorialD3D11.zip

Next, we will implement the spotlight effect. As shown in, only the inner angle of the Cone can be reached by the illumination. However, if we only consider the Inner Corner, our light will be relatively stiff. Inside the Inner Corner cone, there will be light, outside the Inner Corner cone, it will be dark, so we add an outer ), for the space between the inner and outer corners, we use the HLSL difference function smoothstep to calculate a value between 0 and 1.

 

 

 

Main Code of light. PS:

The following function uses smoothstep to calculate the spotlight factor. Cosinnercone is the cosine of the inner angle, and cosoutercone is the cosine of the outer angle. If the calculated cosdirection value is greater than the cosine of the inner angle, the smoothstep value is 1. If the cosdirection value is smaller than the cosine of the outer angle, the value is 0. For values between the two, smoothstep uses the polynomial difference to obtain a value between 0 and 1.

// A function for calculating the spot light Coefficient
Float dualconespotlight (float3 P, float3 lightpos, float3 lightdir, float cosinnercone, float cosoutercone)
{

Float3 v = normalize (p-lightpos );

Float cosdirection = dot (v, normalize (lightdir ));

Return smoothstep (cosoutercone, cosinnercone, cosdirection );
}

For (I = 0; I <num_lights; I ++)
{
// Self-emission color
Emissive = ke [I];

// Computing environment light
Ambient = Ka [I] * globalambient [I];

// Calculate the diffuse reflection light
// Lightdirection is a pure parallel light, which represents the direction of light in the case of spotlight

Spoteffect = dualconespotlight (p, lightposition [I]. XYZ, lightdirection [I], spotattenuation [I]. X, spotattenuation [I]. y );

// The Position of the light source minus the vertex position
L = normalize (lightposition [I]. XYZ-P );
D = distance (lightposition [I]. XYZ, P );
// Attenuation coefficient
Atte = 1/(attenuation [I]. x + attenuation [I]. y * D + attenuation [I]. z * D );
Diffuselight = max (dot (n, l), 0 );
Diffuse = KD [I] * lightcolor [I] * diffuselight * atte * spoteffect;

// Calculate the highlight
V = normalize (cameraposition. XYZ-P );
H = normalize (L + V );
Specularlight = POW (max (dot (n, h), 0), shininess [I]);

If (diffuselight <= 0)
Specularlight = 0;
Specular = KS [I] * lightcolor [I] * specularlight * spoteffect;

Finalcolor + = emissive + ambient + DIFFUSE + specular;
}

Similarly, in our const buffer lightmaterialbuffer

Float4 spotattenuation [num_lights];

Its X and Y represent the cosine of the inner and outer corners, respectively.

The material illumination structure in lightshaderclass. H is also added.

D3dxvector4 spotattenuation [num_lights]; // The Inside and Outside cos values of spotlight, X, and Y are stored respectively.

The final execution result of the program is as follows:

 

Complete code can be found:

Project File mytutoriald3d11_22

Download Code:

Http://files.cnblogs.com/mikewolf2002/myTutorialD3D11.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.