directd3d-Lighting and textures

Source: Internet
Author: User
Tags reflection

This series of articles is written by zhmxy555 (Mao), please specify the source.

Article Link: http://blog.csdn.net/zhmxy555/article/details/8499438

Author: Mao (Light ink) e-mail: happylifemxy@163.com



In this article, we have a detailed analysis of the knowledge about 3D illumination programming in fixed function pipeline in Direct3D, and the article at the end of the paper is still to provide the detailed comments of the demo source code to appreciate, and at the end of the article provides source code download.


First, Introduction



Light is the source of all things. We can not imagine that this beautiful and pleasant world, if there is no light of the company, will be what kind of a devastated. Computer 3D World as the real world of highly realistic imitation, must also be accompanied by light. Returning to our Direct3D application, using lighting in Direct3D can effectively enhance the realism of a 3D scene. The use of lighting in a 3D scene is actually very simple, and we don't need to specify a color value for every vertex of an object, as long as we tell Direct3D what type of light we are using, the specific parameters of our object's material and the orientation of the object's surface relative to the light source. Direct3D calculates the color value of each vertex based on its built-in algorithm, producing realistic lighting effects.

Of course, with the deepening of our study, the ability to deepen, can not only rely on the Direct3D in the built-in lighting algorithm, can be based on a variety of functions of the shader, write their own more optimized more realistic lighting effect.

As the current exposure to Direct3D in the light and material this piece of content, we still honestly first put the fixed function line in this set of very good mastery of the lighting and material system to explain the systematic, first put the foundation to fight, first to go to learn, so as to the back of our takeoff to do the foreshadowing.

Speaking of a complete set of lighting systems, there are two pairs of components, first, light, second, material. The two are naturally a pair of good partners, we can think of them as a light calculation of both elements, want to draw a real three-dimensional world with light, both indispensable.

Let's take a look at the four types of illumination:

Two or four large illumination types




1. Ambient light (Ambient)




An object may be seen even if it is not directly illuminated by the light source, but as long as the light is refracted and reflected by other objects. This overall brightness, based on the entire natural environment, is called ambient light or background light. Ambient Ambient light has no position or direction characteristics, only one color luminance value, and does not decay, so the amount of ambient light projected in all directions and on all surfaces is constant. It is a good choice to open the environment directly if you want to approximate the lighting with low costs and overhead.

The setting of ambient light in Direct3D is very simple, that is, using SetRenderState, the code is as follows:

[CPP] view plain copy print?   Pd3ddevice->setrenderstate (D3drs_ambient,d3dcolor_xrgb (36, 36, 36)); Set the ambient light

The first parameter is filled with d3drs_ambient, which represents the setting of the ambient light, and the second parameter fills a color value.

2. Diffuse reflection Light (diffuselight)




Diffuse light is the most common in our lives, the sun's direct, fluorescent lighting can be seen as the approximation of diffuse reflection. This type of light travels in a particular direction. When it reaches a certain surface, it will be reflected evenly in all directions, so we can see from which direction the brightness of the surface is the same, so the light model with diffuse reflection does not have to consider the position of the observer, but it needs to consider the spatial position and direction of the diffuse light. Light emitted from a light source is generally of this type. Diffuse light does not have a concise set-up method, as described below, please continue to look down.

3. Specular reflection Light (specularlight)




Specular light, as the name implies, travels in a particular direction, and when such light reaches a surface, it will be reflected strictly in the other direction, resulting in a high luminance exposure that can only be observed within an angle range. This light model simulates the reflection of light from a smooth luminous surface such as a mirror, a piece of metal, or a piece of luminescent plastic. If we move the light source, we will see the change in the specular light area, which means that specular reflection depends on the angle of the observer. We can conclude that diffuse reflection is irrelevant to vision, whereas specular reflection is related to vision.

It is important to note that the specular light is much larger than other types of light, and the Direct3D by default is to turn the specular reflection off. If we want to enable specular reflection, use the following code to set the render state d3drs_specularenable to true:

[CPP] view plain copy print? Pd3ddevice->setrenderstate (d3drs_specularenable,true);

4. Spontaneous Light




Self-illumination is the light emitted by the object itself, in fact, he is based on the object of the self-luminous material realized, the following we explain the material in the D3DMATERIAL9 structure, the structure of the member emissive describes the color and transparency of self-illumination. Self-illumination affects the color of an object, for example, by setting the color properties of the self-illumination to make some of the gray materials brighter. What needs to be suggested is that we can use the spontaneous light properties of the material to "illuminate" the object instead of adding lights inside the scene, reducing the amount of computation. The material created from the Glow property does not emit light that can be reflected by other objects in the scene, that is, the light emitted by it does not participate in the light operation, and in order to achieve the reflected light, additional lights need to be added to the scene.

After explaining the four types of illumination, of course, there are three major types of light sources.



Three or three large light source types



The light source type and illumination type in the Direct3D are two completely different concepts, the illumination model describes the reflection characteristics of the light, and the type of light source mainly emphasizes the way to produce these illumination models, as well as the location, direction, intensity and other characteristics of the light.

There are 3 main types of light sources in Direct3D, which we call the three major light sources: Point light, directional (directional light) and spotlight.

In Direct3D 9.0c, when it comes to the light source, we must talk about a structure, that is, the D3DLIGHT9 structure, before unfolding the various types of light sources, let us look at the specific contents of this structure, we can find in MSDN that the structure has the following prototype:

[CPP] view plain copy print?     typedef STRUCTD3DLIGHT9 {D3dlighttype Type;     D3dcolorvalue Diffuse; D3dcolorvalue S

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.