Direct3d basics-lighting d3dlight9

Source: Internet
Author: User
Tags float range

There are three types of lights in d3d, which can make the 3D world look real.

I. Lighting type

1. Point Light)
The uniform light emitted from a point to the surrounding area has the maximum illumination range and the brightness degrades with the distance. The most obvious example is the light bulb used in our house.

2. directional light)
Direction light is a set of parallel light without attenuation, similar to the effect of sunlight.

3. Spot Light)
One of the most complex types of lamps is light source position, direction, range, and inner diameter and outer diameter. The illumination intensity degrades with distance. A similar example is callback.

Ii. Lighting Structure

The light in d3d is represented by the d3dlight9 structure:

Typedef struct d3dlight9 {
D3dlighttype type;
D3dcolorvalue diffuse;
D3dcolorvalue specular;
D3dcolorvalue ambient;
D3dvector position;
D3dvector direction;
Float range;
Float falloff;
Float attenuation0;
Float attenuation1;
Float attenuation2;
Float Theta;
Float Phi;
} D3dlight9, * lpd3dlight;
  • Type-lighting types: d3dlight_point, d3dlight_spot, and d3dlight_directional.
  • Diffuse-the color of the diffuse light emitted by the light source.
  • Specular-the color of the mirror light emitted by the light source.
  • Ambient-ambient light color from the light source.
  • Position-the position of the world coordinate of the light source represented by a vector (the opposite side of the light source is invalid ).
  • Direction-the direction of the global coordinates of the light source represented by a vector (it is invalid for the point light source ).
  • Range-maximum range of light that can be transmitted (the target light is invalid). The value cannot be smaller
  • Falloff-attenuation of the light intensity from the inner cone to the outer cone (effective only for the spotlight). This value is always 1.0f.
  • Attenuation0, attenuation1, attenuation2-light attenuation variable, used to define the propagation distance attenuation of the light intensity (the opposite side is invalid ). Attenuation0 defines constant attenuation, attenuation1 defines linear attenuation, and attenuation2 defines secondary attenuation. Attenuation0 is usually 0.0f, attenuation1 is usually 1.0f, and attenuation2 is usually 0.0f. Use the following formula as appropriate. D indicates the distance to the light source. A0, A1, and A2 match attenuation0, attenuation1, and attenuation2 respectively.
  • Theta-specifies the angle of the cone in the light (only valid for the spotlight), in radians.
  • Phi-specifies the angle of the External Cone (only valid for the spotlight), in radians.

3. lighting settings

After setting the lighting attributes, you need to add the light to the scene:

// Run this command to make the light take effect during rendering
G_pd3ddevice-> setrenderstate (d3drs_lighting, true );

// Set the Light 0 for us
G_pd3ddevice-> setlight (0, & Light );

// Turn on the 0 light
G_pd3ddevice-> lightenable (0, true );

When using the light, you must note that you must set the normal for each vertex. The light works only when the normal is set. What is a normal? It is actually a vector perpendicular to the surface of the point.

References:
Frank D. Luna: Introduction to 3D game programming with DirectX 9.0
Weng yunbing: getting started with 3D game programming (directx9.0)
Golden Point space soft: 3D game from scratch

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.