Use direct3d to design fade-in and fade-out effects

Source: Internet
Author: User

DemoProgram: Http://download.csdn.net/detail/jiangcaiyang123/4156592

I have written an article about the effect of designing fade-in and fade-out with direct3d.ArticleNow, it seems to be a waste of time. More than a year later, I had a deep understanding of the principle of direct3d, coupled with some of my own experiments, and now finally achieved a fade-in and fade-out effect.

To help speed up, I will simply put it. The color mixing operation is performed during raster. This also includes Alpha mixing. Because Microsoft's direct3d is not open-source, we cannot touch the value of each pixel. However, Microsoft provides some functions for us. Using these functions, we can almost completely control what we want. At the initial stage of texture and vertex mixing, we can call the idirect3ddevice9: settexturestagestate function for operations. The declaration of this function is as follows:

 
Hresult settexturestagestate (DWORD stage, d3dtexturestagestatetype type, DWORD value );

Here is a brief introduction. Stage indicates the texture layer. If the texture is multi-texture, you can use it to specify the layer of the texture to be mixed. For example, if two textures are mixed, you can set the blending condition of layer 0th or layer 1st, which helps you set complex texture effects. Type comes from a series of enumeration types. Value refers to some values under type. Depends on type.

Well, to achieve the fade-in and fade-out effect, it is best to declare the vertex:

 
Struct stvertex // define your own vertex struct {void set (float _ x, float _ y, float _ u, float _ v) // set the vertex coordinate {x = _ x, y = _ y, z = 0.0f, u = _ u, v = _ v, rhw = 1.0f;} float X, Y, Z, rhw; // texture coordinate position unsigned long color; // color float U, V; // position in texture coordinate };

The rhw is related to a two-dimensional image, which can be ignored here. Pay attention to the order of member declarations. Flexible vertex format:

Static const unsigned short texture_fvf = d3dfvf_xyzrhw | d3dfvf_tex1 | d3dfvf_diffuse; // flexible vertex format

During initialization, pay attention to calling the settexturestagestate () function we just mentioned. In addition, enable Alpha mixing in the next phase of texture mixing. TheseCodeAs follows:

// Set the texture and vertex mixture g_pdevice-> settexturestagestate (0, gradient, d3dta_texture); g_pdevice-> gradient (0, gradient, d3dta_diffuse); g_pdevice-> gradient (0, d3dtss_alphaop, d3dtop_modulate); // set Alpha hybrid g_pdevice-> setrenderstate (expiration, true); g_pdevice-> setrenderstate (expiration, expiration); g_pdevice-> setrenderstate (d3drs_destblend, expiration );

First, set the value of d3dtss_alph1_g1 to d3dta_texture, which means that the first parameter of Alpha mixture comes from the texture, and then the value of d3dtss_alph1_g2 is set to d3dta_diffuse. Remember the flexible vertex format fvf we set? It contains d3dfvf_diffuse, and our vertex also has a color Member, which means the second parameter comes from the color of the vertex we set. Finally, we set d3dtss_alphaop to d3dtop_modulate, which indicates that the modulation method is used. This is very important because the fade-in and fade-out effect we need is like this. Its working principle is S (rgba)
= Arg1 × arg2. Multiply the two alpha values. There are also some available parameters such as d3dtop_modulate2x and d3dtop_modulate4x, which are multiplied by 2 and 4 on the original basis for brightening. Unless this effect is specifically used, it is better to use d3dtop_modulate.

Then there is the setrenderstate () function. First, it enables the Alpha hybrid mode to perform Alpha hybrid, and then it specifies the source and target blending. You can choose from multiple mixed modes, but to achieve a translucent effect, I can only set it as above.

The final result is that we can use the Alpha value of the defined vertex to control the Alpha value after mixing. I have also created a demo program here. If you are interested, you can download it here.

Http://download.csdn.net/detail/jiangcaiyang123/4156592

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.