D3D 9 randomly controls texture transparency with alpha blending

Source: Internet
Author: User

On the Internet do not see the technical article about D3D 9, I recently wrote a 2D function, I hope that through certain settings to arbitrarily control a 2D texture map transparency, online search, all articles, or copy the market textbooks, or copy the official documents, a little better, is in the official textbook based on the addition of Chinese translation, ouch I went, looked for two days ah, the function is realized, but the mind's doubts have been lingering, why? Because my code is copied, ah, I only know this kind of setting can get such a effect, but I do not know why Ah, I am special if the next time you encounter the same problem, I would like to copy the code again?

Well, the complaint will be sent here, first of all, the core issue of this article-in Direct3D 9, how to control the vertex of the diffuse in the Alpha component to achieve arbitrary transparency of the texture;

Using D3D to do 2D, the general habit is to use 4 vertices to draw a rectangle, and then set the map, this way a simplest Sprite is finished, vertex format we can define this:

1 struct d3d9vertex2d {2     float x, Y, z, RHW; 3     DWORD Diffuse; 4     float u, v; 5 };

Because the image file itself even with Alpha data, its alpha value is fixed, we need a means to control the transparency of the texture at random during the game operation, the author's own practice is to control the four vertex diffuse in the alpha value to achieve transparency control, The author of the vertex diffuse value by default is initialized to 0xFFFFFFFF, that is D3dcolor_argb (255, 255, 255, 255), but the simple control of transparency is not, we also have to consider the hollow effect, to show the hole to the Hollow, Where there is no need to cut out, the transparency is controlled according to our intentions, as shown in the following:

, the sister is the background map (laughter), the foreground is the middle of the pink ball, the ball is originally a PNG picture, with its own alpha channel, can achieve the skeleton, but the transparency of the ball, you need to use alpha blend to achieve additional;

To achieve this, we need to set three types of parameters, namely Alpha Blend, Alpha Test, and Texture Stage state, which is not good enough to translate, directly to English (laughter);

First, Alpha Blend, which many people know, is a formula:

Output_color = (Src_color * src_alpha) + (Dest_color * (1-src_alpha))

The method of setting is also very simple, that is, SetRenderState open d3drs_alphablendenable, set D3drs_srcblend to D3dblend_srcalpha, set D3drs_destblend to D3dblend_invsrcalpha, as for the middle of the plus, is set D3DRS_BLENDOP to D3dblendop_add, the code is as follows:

1 d3d9_device->setrenderstate (d3drs_alphablendenable, TRUE); 2 d3d9_device->SetRenderState (D3drs_blendop, d3dblendop_add); 3 d3d9_device->setrenderstate (D3drs_srcblend, d3dblend_srcalpha); 4 d3d9_device->setrenderstate (D3drs_destblend, D3dblend_invsrcalpha);

The question is, where does the two color in the formula and an alpha parameter come from?

Some people might say that Dest_color is the color data in the frame cache, where do src_color and Src_alpha come from? You can't answer it.

The answer is--derived from the results of the texture layer (Texture Stage) operation;

Let's look at how the Texture Stage is set up:

1D3d9_device->settexturestagestate (0, D3dtss_colorop, d3dtop_modulate);2D3d9_device->settexturestagestate (0, D3DTSS_COLORARG1, d3dta_texture);3D3d9_device->settexturestagestate (0, D3DTSS_COLORARG2, d3dta_current);4 5D3d9_device->settexturestagestate (0, D3DTSS_ALPHAOP, d3dtop_modulate);6D3d9_device->settexturestagestate (0, D3DTSS_ALPHAARG1, d3dta_diffuse);7D3d9_device->settexturestagestate (0, D3DTSS_ALPHAARG2, d3dta_texture);

What does this six code mean??

Smart people have seen that the first three code is responsible for generating Alpha Blend to use the Src_color, and then three lines of code, is responsible for generating src_alpha;

As we all know, there are two main parts of the Texture Stage-the color component operation and the Alpha component operation, each of which follows the following two formulas to produce the color data and alpha data that is output to the next layer:

color = color_arg1 Op color_arg2

Alpha = alpha_arg1 op alpha_arg2

Let's take a look at how the first formula above generates the color data, COLOR_ARG1 is set to come from the texture, COLOR_ARG2 is set to the color value from the previous layer, and if the layer is on the No. 0 level, the diffuse of the vertex color_arg2 the Gouraud As a result of the shading operation, we can approximate this as the diffuse value of the vertex, and OP is set to the multiplication operator, and for a PNG image, because our diffuse has been preset to 0xFFFFFF, this value will be interpolated in the D3D internal operation. (1.0, 1.0, 1.0), so actually is the floating-point arithmetic, because 1.0 times the color value, the result is the color value itself, so the color of the output is the texture of its own value;

Take a look at how the second formula generates Alpha data, ALPHA_ARG1 is set to the DIFFUSE,ALPHA_ARG2 from the vertex is set to come from the texture, OP is set to the multiplication operator, and for a PNG picture, the alpha value of the cutout part is 0, and 0 times any The number is 0, so the cutout part is always hollow, not the cutout part, the alpha value is not 0, multiplied by the texture's alpha value, the result depends on the texture's own alpha value, but when we create the texture by D3DXCreateTextureFromFileEx (), The alpha value of all non-perforated portions of the texture is automatically populated with 0xFF, which is 1.0, so the result of the operation is the alpha value of the diffuse;

That is, the above two formulas produce color, in fact, the color of the texture itself, and the resulting alpha, in fact, is diffuse alpha, this way, we can set the vertex of the diffuse alpha value, to arbitrarily control the transparency of the texture;

To verify the above statement, let us give an example:

Suppose we want to load a PNG image as a texture, the texture of the cutout part of the pixel value is automatically filled with 0X00RRGGBB, the non-skeleton portion is filled with 0xAARRGGBB, where the AA RR GG BB is the texture of its own color data and alpha data, and then we define their own vertex The diffuse value is set to 0xDDFFFFFF with an alpha value of 0xDD and a color value of 0xFFFFFF;

Next, based on the settings in the Texture Stage above, the values of the color and Alpha are calculated separately:

The color value is 0xRRGGBB * 0xFFFFFF, because the D3D internal interpolation is floating point number, so actually is (rr/255, gg/255, bb/255) * (1.0, 1.0, 1.0), the result is the texture itself color value, that is, 0xR RGGBB;

Alpha value in two cases, hollow and non-hollow, the results of the two cases are 0x00 * 0xAA and 0xdd* 0xAA, which is 0x00 and 0xDD, where 0xDD is the alpha value set in our diffuse;

The resulting color values and alpha will participate in the Alpha blend operation, applying the first formula above, there are still two cases, namely, hollow and non-pierced:

Output = (0xRRGGBB * 0.0f) + (Dest_color * (1.0f-0.0f))

Output = (0xRRGGBB * (0xdd/255)) + (Dest_color * (1.0f-(0xdd/255))

The first formula is hollow, the generated value is fixed is dest_color, that is, the color of the background map, the second formula is non-hollow, the resulting value is the alpha blend color value;

Okay, random control of the transparency of the texture is done, and finally we have to open the alpha test, let D3D for our skeleton, Alpha test set as follows:

1 d3d9_device->setrenderstate (d3drs_alphatestenable, TRUE); 2 d3d9_device->SetRenderState (D3drs_alphafunc, d3dcmp_greater); 3 0x00 );

The above three lines of code mean very simple-any pixel that will be rendered, if its alpha value is greater than 0, rendering, otherwise, not to render;

OK, done, the above code in addition to arbitrary control of transparency, but also control the tone of the texture, the same by modifying the vertex diffuse value, if we set diffuse to D3DCOLOR_ARGB (200, 255, 0, 0), You can get a transparent red-toned ball:

If set to D3dcolor_argb (200, 255, 255, 0), the ball turns into a transparent yellow hue:

You can also control the hue of two textures at the same time:

----------------------------------------------------------

In fact there are many ways to control these so flexible parameters, pixel shader and vertex shader are OK, but novice without learning programmable pipeline line, must be fixed pipe line function to achieve a variety of effects, only this article, dedicated to all who are learning D3D 9;

D3D 9 randomly controls texture transparency with alpha blending

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.