Game Development BASICS (7)

Source: Internet
Author: User
Tags bit set

 

Chapter 7
The fusion technology enables us to combine the colors of the pixels to be Raster with those previously raked and at the same position.
The color values of the elements being processed are merged (mixed) with the pixel color values stored in the background cache. With this technology, we can achieve a variety of effects, especially transparent effects.
This method of merging the currently calculated pixel (source pixel) color value with the previously calculated pixel (target pixel) color value is called fusion.
Note: The effect of fusion is not limited to the transparent effect of ordinary glass.
The following principles must be observed in converged operations:
First, draw the objects that do not need to be merged, and then sort the objects that need to be merged according to the depth value relative to the camera. if the object is already in the observation coordinate system, the efficiency of this operation is quite high, because at this time, you only need to sort the Z component, and finally draw the objects to be converged one by one in the order from the back to the back.
The following formula is used to combine two pixel color values:
Outputpixel = sourcepixel * sourceblendfactor + destpixel * destblendfactor
Each variable in the formula is a 4D color vector (R, G, B, A) symbol * indicating that the components are multiplied one by one.
Color value after outputpixel Fusion
The color value calculated by sourcepixel for fusion with the corresponding pixels in the background cache.
Sourceblendfactor: Source fusion factor, which specifies the proportion of the color value of the source pixel in the fusion. The value ranges from 0 to 1.
The pixel color value in the background cache of destpixel.
Destblendfactor: target fusion factor, which specifies the proportion of the color value of the target pixel in the fusion. The value ranges from 0 to 1.

The source fusion factor and target fusion factor allow us to modify the color values of the source and target pixels in various ways to achieve different effects.
In direct3d, the fusion operation is disabled by default. You can set d3drs_alphablendenable to true to enable the operation.
Device-> setrenderstate (d3drs_alphablendenable, true );

Note: the computing overhead of fusion is not low, so it should only be used in a must-have scenario. After you have drawn the ry to be converged, Alpha Fusion should be disabled, when merging triangle unit groups, it is best to batch process them and draw them immediately. This avoids enabling and disabling fusion operations in each frame of the image.

By setting the source and target fusion factors, we can create a series of different fusion effects.
You can set the source fusion factor and target fusion factor by setting the values of d3drs_srcblend and d3drs_destblend.
Device-> setrenderstate (d3drs_srcblend, source );
Device-> setrenderstate (d3drs_destlend, destination );
Among them, source and destination can use the following fusion factors:
D3dblend_zero blendfactor = (0, 0, 0)
D3dblend_one blendfactor = (1, 1, 1)
D3dblend_srccolor blendfactor = (RS, GS, BS,)
D3dblend_invsrccolor blendfactor = (1-rs, 1-gs, 1-bs, 1-)
...........
The default values of the source and target fusion factors are d3dblend_srcalph d3dlend_invsrcalpha.
The Alpha component in each vertex color is similar to the color value and gradient along the surface of the triangle unit. However, it is not used to determine the color value of a certain pixel, but to determine the Alpha component of the pixel.
The Alpha component is mainly used to specify the transparency of pixels. If we reserve 8 bits for the Alpha component of each pixel, the valid range of the Alpha component is [0,255, [0,255] corresponds to transparency [% 0,100%]. when the Alpha value is 0, the pixel is completely transparent. If the Alpha value of the pixel is 128, the transparency is 50%, and the pixel whose Alpha value is 255 is completely opaque.
To describe the transparency of pixels using the Alpha component, we must set the source fusion factor and target fusion factor to d3dblend_srcalpha and d3dblend_invsrcalpha respectively. These values are exactly the default values of the fusion factor.

Instead of directly using the calculated Alpha component, we often obtain Alpha information from the Alpha Channel of the texture, the alpha channel is an additional bit set reserved for texture elements that store the Alpha component. When a texture is mapped to an element, the Alpha component in the alpha channel is also mapped and becomes the Alpha component of the pixel in the element.

By default, if the current texture has an alpha channel, the Alpha value is taken from the Alpha Channel. If there is no alpha channel, the Alpha value is taken from the color of the vertex, however, you can also specify the Alpha value source (diffuse color value or alpha channel) with the following draw status)
// Compute Alpha from diffuse colors during Shading
Device-> settexturestagestate (0, d3dtss_alph1_g1, d3dta_diffuse );
Device-> settexturestagestate (0, d3dtss_alphaop, d3dta_selectarg1 );
// Take Alpha from Alpha Channel
Device-> settexturestagestate (0, d3dtss_alph1_g1, d3dta_texture );
Device-> settexturestagestate (0, d3dtss_alphaop, d3dta_selectarg1 );

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.