Direct-X study notes--alpha color blend

Source: Internet
Author: User

Alpha blending is often useful. Wait for me to study hard.

I. INTRODUCTIONFirst look at the alpha channel, the alpha channel is the computer to store the image transparency information channel, it is a 8-bit grayscale channel, with 256 levels of gray records the image of transparent information, defined transparent, opaque, translucent, etc., where black is completely transparent, white is opaque, gray is translucent.Without alpha blending, the color of our drawing is always replaced with the color present in the current color buffer, so that the object behind it is always covered with the original object. But when you want to draw something like glass, water, and other transparent objects, this method obviously doesn't meet the requirements. By defining an alpha value that represents the semi-transparency of an object and a translucent formula, you can mix the color of the object you want to draw with the color that exists in the color buffer to draw an object with a translucent effect, the legendary Alpha Blend.
Two. Alpha Fusion formula in Direct-X the Alpha Fusion formula in DirectX is as follows:
Outputcolor = (RGBSRC * ksrc) OP (RGBDST * kdst)

Outputcolor represents the color value after alpha blending. RGBSRC represents the source color value, the color value of the entity to be drawn KSRC represents the source blending factor, usually assigned to an alpha value that represents the degree of translucency, or any value that belongs to the d3dblend of the enumeration type, which is used to multiply with rgbsrc. RGBDST represents the target color value, that is, the color value in the current color buffer KDST represents the target blending factor, which can be any value that belongs to the enumeration d3dblend, and is used to multiply rgbdst. OP represents a mixed method of the result of the source calculation and the color buffer calculation, by default the OP is D3dblend_add, that is, the source calculation results and the color buffer calculation results are added.


In the graphical display, the most common use of alpha blending is to assign the OP value to D3dblend_add, which adds the result of the source calculation and the color buffer calculation, so that the formula for the Alpha blend color becomes:

color = (RGBSRC * ksrc) + (RGBDST * kdst)

Note that the * in this formula, in fact, refers to the dot product of the vector, because the so-called RGBSRC, and Rgbdest are a combination of color and alpha four-tuple (r,g,b,a) namely red, green, blue, alpha value, each corresponding fusion parameters. So in more detail, the above formula should be written as:
color = (RSRC * Ksrc1 + rdst * Kdes1, GSRC * Ksrc2 + GDST * Kdst2, BSRC * Ksrc3 + Bdst * Kdst3, ASRC * KSRC4 + ADST * Kds T4);
In fact, this formula can be more simplified, that is, we want to transparent objects using a coefficient K, and to be transparent to the place only need to set (1-K), in the DX has set up for us the macro: that is, the ksrc assignment to D3dblend_srcalpha, That is, the alpha value of the currently drawn pixel, and the KDST is assigned a value of D3dblend_invsrcalpha, which is 1 minus the alpha value of the currently drawn pixel.
In fact, the above settings have been able to better simulate the effect of most translucent objects. Of course, we can set up other ways of merging ourselves. So it's important to remember the other fusion methods and the fusion coefficients.

Three. Fusion method and integration factor collation 1. Blending mode and operator:
D3dblendop_add The result of the source pixel calculation is added to the calculated result of the target pixel, i.e. "end result" = "source" + "target"
D3dblendop_subtract The result of the source pixel calculation is subtracted from the computed result of the target pixel, i.e. "end result" = "source"-"target"
D3dblendop_revsubtract The calculated result of the target pixel minus the result of the source pixel calculation, i.e. "end result" = "target"-"source"
D3dblendop_min Whichever is smaller between the calculated result of the source pixel and the result of the target pixel. i.e. "final Result" = MIN ("target", "source")
D3dblendop_max Whichever is larger between the calculated result of the source pixel and the calculated result of the target pixel. That is, "final result" = MAX ("target", "source")
2. Fusion Factor: D3dblend_zero Fusion factor = (0,0,0,0)
D3dblend_one Fusion factor = (1,1,1,1)
D3dblend_srccolor Fusion factor = (R_SRC,G_SRC,B_SRC,A_SRC)
D3dblend_invsrccolor Fusion factor = (1-R_SRC,1-G_SRC,1-B_SRC,1-A_SRC)
D3dblend_srcalpha Fusion factor = (1-A_SRC,A_SRC,A_SRC,A_SRC)
D3dblend_invsrcalpha Fusion factor = (1-A_SRC,1-A_SRC,1-A_SRC,1-A_SRC)
D3dblend_destalpha Fusion factor = (A_DST, A_DST, A_DST, A_DST)
D3dblend_invdestalpha Fusion factor = (1-A_DST, 1-A_DST, 1-A_DST, 1-A_DST).
D3dblend_destcolor Fusion factor = (R_DST, G_DST, B_DST, A_DST).
D3dblend_invdestcolor Fusion factor = (1-R_DST, 1-G_DST, 1-B_DST, 1-A_DST).
D3dblend_srcalphasat Fusion factor = (F, F, F, 1), where f = min (A_SRC,1-A_DST)

Where R_src, G_src, B_SRC, a_src represent the source (that is, source) pixels of red, green, blue, transparent four component values, while R_DST, G_DST, B_DST, A_DST represents the target (that is, destination) pixels of red, green, blue, A transparent four component value.


Four. When Alpha fusion is used, the origin of the alpha value needs to be clarified. There are three ways to set the color properties of an object: 1. Vertex color: This is the oldest method and the most troublesome. The first time you use a vertex buffer or an index buffer to draw a drawing, set the Point property, which has a color attribute, you can set the alpha value. 2. Lighting and materials: the reflection coefficient of various light in a material is a four-tuple, which contains the alpha value. 3. Texture: The easiest way is to set the texture to determine the color of a model, so this is also the most commonly used.
Since there are three ways to set the color alpha value of an object, and the usual degree is texture > light material > Vertex color, the origin order of alpha values is very clear, if there is texture, it is obtained from the texture, if there is no texture, it is obtained from the light material, if the light material is not, That is obtained from the vertex attribute.
Five. Using Alpha blend personal feel blend for r,g,b,a is possible because transparency is used more often, so it is common to use alpha blend (purely personal guessing).
Here we use the second case, set the light material, change the alpha value of the model material, to observe the effect of different alpha value fusion.
Feeling Alpha blend is a rendering state that we turn on when we draw a transparent object.
There are several parts to using alpha blend:
1. Set the alpha value of the drawing object, the default value is 1.0f, that is, opaque, so if we need to set an object to be transparent, we can make it transparent by changing its alpha value. In this, we use the object's material information directly, that is, the default 1.0f, and add a function that can change the alpha value of the material, so that the alpha value changes.
2. Turn on Alpha Test DX by default is to turn off alpha test, so we need to pass the universal function of DX ... SetRenderState to open the alpha test.
<span style= "White-space:pre" ></span>//turn on Alpha Fusion M_pdevice->setrenderstate (D3DRS_ALPHABLENDENABLE , true);
Of course, if you close, the second argument is false. When we finish drawing a transparent object, we don't need alpha blend to close it.
3. Setting the Fusion factor
This is what we have said above, DX for us to prepare the most commonly used two fusion factor on it!
<span style= "White-space:pre" ></span>//set Fusion Factor m_pdevice->setrenderstate (D3DRS_SRCBLEND, D3DBLEND_ Srcalpha);//Set Source Fusion Factor M_pdevice->setrenderstate (D3drs_destblend, D3dblend_invsrcalpha);//Set Target fusion factor

4. Set up the fusion operation method This step can be omitted, dx default Alpha Fusion method is the Add method, but we still write, in case one day without this, but also to set other Ah!
<span style= "White-space:pre" ></span>//set the fusion operation mode (can be omitted, dx default is D3dblendop_add Fusion operation mode) m_pdevice-> SetRenderState (D3drs_blendop, D3dblendop_add);

Six. Full demo in this, we use a dragon model (OK, or steal from the light ink), after reading, do not use texture information, use the default material information, and add a can change the material alpha value of the function, to change the object's transparency. First, a piece of material is not removed:


Remove the texture information, turn on Alpha Fusion, and the default alpha value is 1.0f, which is completely opaque

Reduces alpha values in diffuse in materials, translucent:

Less, less, eh? What about dragons? Answer: I ate ....

Code (the code for the Alpha blend and material information sections is placed in the previously abbreviated Cmesh class, so only Mesh.cpp related content is posted):
#include "stdafx.h" #include "Mesh.h" Cmesh::cmesh (Lpdirect3ddevice9 pdevice): M_pdevice (pdevice) {}cmesh::~cmesh ( void) {//Releases related resources Safe_delete_array (m_pmaterials); if (m_ptextures) {for (int i = 0; i < M_dwnummtrls; i++) {safe_release (M _ptextures[i]);} Safe_delete_array (m_ptextures);} Safe_release (M_pmesh);} void Cmesh::createmesh (LPSTR filename) {lpd3dxbuffer padjbuffer = NULL; Lpd3dxbuffer pmtrlbuffer = NULL;D3DXLOADMESHFROMX (filename, d3dxmesh_managed, M_pdevice, &padjbuffer, & Pmtrlbuffer, NULL, &m_dwnummtrls, &m_pmesh);//read material and texture data d3dxmaterial *pmtrl = (d3dxmaterial*) pmtrlbuffer-> GetBufferPointer (); m_pmaterials = new d3dmaterial9[m_dwnummtrls];m_ptextures = new Lpdirect3dtexture9[m_dwnummtrls]; for (int i = 0; i < M_dwnummtrls; i++) {//material information m_pmaterials[i] = Pmtrl[i]. Matd3d;m_ptextures[i] = null;//does not use texture information for mesh models//d3dxcreatetexturefromfilea (M_pdevice, Pmtrl[i].ptexturefilename, &m_ptextures[i]);} Optimized mesh model M_pmesh->optimizeinplace (d3dxmeshopt_compact | D3dxmeshopt_attrsoRT | D3dxmeshopt_stripreorder, (dword*) padjbuffer->getbufferpointer (), NULL, NULL, NULL); Safe_release (Padjbuffer); Safe_release (Pmtrlbuffer);} void Cmesh::D rawmesh (const d3dxmatrixa16& matworld) {m_pdevice->settransform (D3dts_world, &matWorld);/// Alpha Blend related//turn on Alpha Fusion M_pdevice->setrenderstate (d3drs_alphablendenable, true);//Set Fusion factor m_pdevice-> SetRenderState (D3drs_srcblend, D3dblend_srcalpha);//Set Source Fusion Factor M_pdevice->setrenderstate (D3drs_destblend, D3dblend_invsrcalpha);//Set the target fusion factor//Set the Fusion operation mode (can omit, dx default is the D3dblendop_add Fusion operation mode) M_pdevice->setrenderstate (D3drs _blendop, D3dblendop_add);//Draw meshfor (int i = 0; i < M_dwnummtrls; i++) {m_pdevice->setmaterial (&m_pmaterials [i]); M_pdevice->settexture (0, m_ptextures[i]); M_pmesh->drawsubset (i);}} The function of adding alpha value is void Cmesh::addalphavalue () {for (int i = 0; i < M_dwnummtrls; i++) {m_pmaterials[i]. Diffuse.a + = 0.1f;if (M_pmaterials[i]. Diffuse.a > 1.0f) m_pmaterials[i]. Diffuse.a = 1.0f;}} function void Cmesh::reducealp to reduce alpha valueHavalue () {for (int i = 0; i < M_dwnummtrls; i++) {m_pmaterials[i]. Diffuse.a-= 0.1f;if (M_pmaterials[i]. Diffuse.a < 0.0f) M_pmaterials[i]. Diffuse.a = 0.0f;}}





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Direct-X study notes--alpha color blend

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.