DIRECT2D Development: Texture Blending

Source: Internet
Author: User

Reprint Please specify source: http://www.cnblogs.com/Ray1024

I. Overview

We all know that direct2d can load and display pictures, but don't know if you ever think that this 2D graphics engine can be blended with textures? If we can do texture blending, then our 2D graphics engine can do more things, such as the concept of masks, we can do a lot more work on the image.

People who have touched the knowledge of 3D rendering know the shader, in 3D rendering, the shader is divided into vertex shader and pixel shader, here we mainly implement the function of pixel shader similar to 3D rendering, namely texture (picture) mix.

Second, the Thinking analysis

In direct2d, to achieve the function of blending texture (picture), we can consider that if we can read and write the color data of each pixel of the texture (picture), we can realize the function of blending the texture (picture).

But how do we manipulate (read and write) The pixel data of a picture?

Because direct2d loads pictures with the Windows Image Processing Component (WIC), I found the method in the MSDN documentation for WIC.

1.iwicbitmap::lock Function Introduction

HRESULT Lock (  [in]  const wicrect        *prclock,  [in]        DWORD          flags,  [out]       Iwicbitmaplock **ppilock) Function: Provides access parameters to a rectangular area of a bitmap: Prclock [in] the rectangular area to access the flags [in] access mode (read/write) Ppilock [out] receives a pointer to the locked memory location, Iwicbitmaplock type returns: If successful, returns S_OK

2. For the Iwicbitmaplock type, let's describe one of its member functions:

HRESULT Getdatapointer (  [out] UINT *pcbbuffersize,  [out] BYTE **ppbdata); Function: Gets the pointer parameter of the upper-left pixel in the lock rectangle: pcbbuffersize [out] Gets the memory size ppbdata [out] gets the memory data

Next, we'll cover the process of implementing texture blending in detail.

Three, texture mixing implementation

1. Loading Iwicbitmap objects

This step is believed to be familiar to everyone, because each time you create a d2d bitmap, you must go through this procedure. Directly on the code:

id2d1bitmap*pbitmap= null;iwicbitmapdecoder*pdecoder= null;iwicbitmapframedecode*psource= NULL;IWICBitmap*pWIC= null;iwicformatconverter*pconverter= null;iwicbitmapscaler*pscaler= NULL; Uintoriginalwidth= 0; uintoriginalheight= 0;//1. Load Iwicbitmap object HRESULT hr = Piwicfactory->createdecoderfromfilename (uri,NULL,GENERIC_ Read,wicdecodemetadatacacheonload,&pdecoder); if (SUCCEEDED (HR)) {hr = Pdecoder->getframe (0, &pSource);} if (SUCCEEDED (HR)) {hr = Psource->getsize (&originalwidth,&originalheight);} if (SUCCEEDED (HR)) {hr = Piwicfactory->createbitmapfromsourcerect (Psource, 0,0, (UINT) originalwidth, (UINT) OriginalHeight, &pwic);}

2. Reading pixel data from a Iwicbitmap object

The Iwicbitmaplock object is created from the WIC bitmap, and then a pointer to the image pixel data is obtained from Iwicbitmaplock, with the following code:

2. Read pixel data from Iwicbitmap object Iwicbitmaplock *pilock = NULL; WICRect Rclock = {0, 0, originalwidth, originalheight};hr = Pwic->lock (&rclock, Wicbitmaplockwrite, &pILock); if (SUCCEEDED (HR)) {UINT cbbuffersize = 0; BYTE *PV = null;if (SUCCEEDED (HR)) {//Gets the pointer to the upper-left pixel in the lock rectangle hr = Pilock->getdatapointer (&cbbuffersize, &PV);}

3. Pixel calculation for texture blending

To calculate the pixel data of the captured picture pixel, the code is as follows:

3. Pixel calculation for texture blending for (unsigned int i=0; i<cbbuffersize; i+=4) {if (pv[i+3]! = 0) {pv[i]*=color.b;pv[i+1]*=color.g;pv[i+ 2]*=COLOR.R;PV[I+3] *=COLOR.A;}}

In the above code, it is important to note that the method for calculating pixels is multiplied by the component of color colors.

Also, a careful friend can see that the step of the pixel data is 4, the number of 4 in each step is composed of a pixel complete color value, and the color format is bgra format, each color value range is 0.f~1.f.

4. The color blending operation ends, releasing the Iwicbitmaplock object

After the texture blending calculation is complete, call the Rlease function to release the Iwicbitmaplock object to write the computed picture pixel data to the Iwicbitmap object, the WIC bitmap, as follows:

4. Color blending operation ends, release Iwicbitmaplock object Pilock->release ();

5. Create a D2D bitmap using a WIC bitmap

So far, the real sense of texture mixed pixel data reading, calculation and writing is complete. We can create D2D bitmaps directly using WIC bitmaps, as follows:

5. Use the Iwicbitmap object to create the D2D bitmap if (SUCCEEDED (HR)) {hr = Piwicfactory->createformatconverter (&pconverter);} if (SUCCEEDED (HR)) {hr = Piwicfactory->createbitmapscaler (&pscaler);} if (SUCCEEDED (HR)) {hr = Pscaler->initialize (Pwic, (UINT) originalwidth, (UINT) originalheight, Wicbitmapinterpolationmodecubic);} if (SUCCEEDED (HR)) {hr = Pconverter->initialize (Pscaler,guid_wicpixelformat32bpppbgra,wicbitmapdithertypenone, Null,0.f,wicbitmappalettetypemediancut);} if (SUCCEEDED (HR)) {hr = Prendertarget->createbitmapfromwicbitmap (PCONVERTER,NULL,&PBITMAP);}

6. Display

After a series of texture blending operations above, we can draw the blended texture to the window display.

in our example, we created 4 Id2d1bitmap objects, the d2d bitmap, M_pbitmap is the bitmap of the original image, M_pbitmapblended, m_pbitmapblended1, M_pbitmapblended2 are the original and red, green, blue after the texture blending bitmap, Create the code as follows (Getblendedbitmapfromfile function for all of the steps described above):

Create Bitmap if (SUCCEEDED (HR)) {Loadbitmapfromfile (m_prt,m_pwicfactory, L "Bitmap.png", 0,0, &m_pbitmap);} Create a bitmap and color blend if (SUCCEEDED (HR)) {//Create a WIC bitmap from a file, mix the WIC bitmap with color, and then create a d2d bitmap m_pbitmapblended = Getblendedbitmapfromfile (M _pwicfactory, M_prt, L "Bitmap.png", D2d1::colorf (1, 0, 0, 1));//Red m_pbitmapblended1 = Getblendedbitmapfromfile (m_ Pwicfactory, M_prt, L "Bitmap.png", D2d1::colorf (0, 1, 0, 1));//Green M_pbitmapblended2 = Getblendedbitmapfromfile (m_ Pwicfactory, M_prt, L "Bitmap.png", D2d1::colorf (0, 0, 1, 1));//Blue}

The results show the following diagram:

The previous only introduced the texture mix important code, the rest of the code is not listed, interested friends can click here to download, source code for D2dbitmapblend.

Iv. Extended Extension

The texture blending operation in direct2d is described above, but it is still a relatively simple operation because it only blends color with the texture.

In fact, we can also perform a blending operation between textures. The principle is simple, as follows:

1. Create overlay textures and read pixel data;

2. Create the main texture and read the superimposed pixel data;

3. Use the master pixel data and the overlay pixel data row blending operation;

4. Create a d2d bitmap using the computed master texture WIC bitmap;

5. Display.

Note that it is important to calculate the blending of two textures by using linear interpolation in 3D rendering for texture blending.

Friends who have touched 3D rendering will know that in 3D rendering, texture blending is calculated in linear interpolation, such as the mix function in GLSL, as follows:

Gentype Mix (Gentype x, Gentype y, float a)

The final fragment color value is obtained by mixing the two with the mix function. Mix this function is a special linear interpolation function in GLSL, the first two parameters are the pixel data of the main texture and overlay texture respectively, the third parameter is the proportion of the superimposed texture in the texture blending, the calculation principle is as follows:

After X and y are mixed = X⋅ (1−a) +y⋅a

This is how we use the blending of textures.

We are now doing 2 texture blending operations, here I only paste the portions of the mixed linear blend of textures:

for (unsigned int i=0; i<cbbuffersize; i+=4) {if (pv[i+3]! = 0) {pv[i]= pv[i]* (1-proportion) + pv1[i]*proportion;pv[i+1] = pv[i+1]* (1-proportion) + pv1[i+1]*proportion;pv[i+2]= pv[i+2]* (1-proportion) + pv1[i+2]*proportion;pv[i+3] = pv[i+3] * (1-proportion) + Pv1[i+3]*proportion;}}

The proportion of the above calculated part is the proportion of the superimposed texture, which is an essential part of the texture blending. The rest of the code omitted, interested friends can click here to download, the source code for D2DBITMAPBLENDWITHBITMAP.

This is the result of the two texture blends as follows:

V. Conclusion

The texture blending process in direct2d is all done here. This way we can also use DIRECT2D to achieve texture blending in 3D rendering.

DIRECT2D development: Texture 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.