Direct3d translucent Rendering

Source: Internet
Author: User

Purpose:

Use direct3d to render image a translucent to image B.

Steps:

1. Create a standard Win32 program and add the object definition and function declaration to be used:

Ccomptr <idirect3d9> pd3d9; <br/> ccomptr <idirect3ddevice9> pd3ddevice9; <br/> ccomptr <strong> ptexture1; <br/> ccomptr <strong> ptexture2; <br/> d3dpresent_parameters gd3dpp; </P> <p> hresult create (hwnd); <br/> hresult drawtexture (idirect3dtexture9 * ptexture, <br/> float fleft, float ftop, float fright, float fbottom, d3dcolor CLR); <br/> void setstate (); <br/> hresult present ();

 

2. Create the required object, that is, implement the CREATE (hwnd) function:

Hresult create (hwnd) <br/>{< br/> pd3d9 = direct3dcreate9 (d3d_sdk_version); <br/> If (pd3d9 = NULL) <br/>{< br/> return s_false; <br/>}</P> <p> d3dcaps9 caps; <br/> pd3d9-> getdevicecaps (d3dadapter_default, d3ddevtype_hal, & amp; caps); <br/> DWORD dwbf; <br/> If (caps. devcaps & d3ddevcaps_hwtransformanlight) <br/>{< br/> dwbf = Hangzhou; <br/>}< br/> else <br/>{< br/> dwbf = d3dcreate_software_vertexprocessing; <br/>}< br/> If (caps. devcaps & d3ddevcaps_puredevice) <br/>{< br/> dwbf | = d3dcreate_puredevice; <br/>}</P> <p> d3ddisplaymode d3ddm; <br/> pd3d9-> getadapterdisplaymode (d3dadapter_default, & d3ddm); <br/> gd3dpp. backbuffercount = 1; <br/> gd3dpp. required wed = true; <br/> gd3dpp. hdevicewindow = hwnd; <br/> gd3dpp. swapeffect = d3dswapeffect_copy; <br/> gd3dpp. backbufferformat = d3ddm. format; </P> <p> hresult hR = pd3d9-> createdevice (<br/> d3dadapter_default, <br/> d3ddevtype_hal, <br/> hwnd, <br/> dwbf, <br/> & gd3dpp, <br/> & pd3ddevice9); <br/> If (failed (HR )) <br/>{< br/> return hr; <br/>}</P> <p> d3dxcreatetexturefromfileex (pd3ddevice9, <br/> L "B .jpg ", <br/> d3dx_default, <br/> d3dx_default, <br/> d3dx_default, <br/> 0, <br/> d3dfmt_unknown, <br/> d3dpool_managed, <br/> d3dx_default, <br/> d3dx_default, <br/> 0, <br/> null, <br/> null, <br/> & ptexture1.p ); </P> <p> d3dxcreatetexturefromfileex (pd3ddevice9, <br/> L "a.jpg", <br/> d3dx_default, <br/> d3dx_default, <br/> d3dx_default, <br/> 0, <br/> d3dfmt_unknown, <br/> d3dpool_managed, <br/> d3dx_default, <br/> d3dx_default, <br/> 0, <br/> null, <br/> null, <br/> & ptexture2.p); </P> <p> return s_ OK; <br/>}

 

3. Function for rendering image drawtexture:

Hresult drawtexture (idirect3dtexture9 * ptexture, float fleft, float ftop, float fright, float fbottom, d3dcolor CLR) <br/>{< br/> pd3ddevice9-> beginscene (); </P> <p> pd3ddevice9-> settexture (0, ptexture); <br/> pd3ddevice9-> setfvf (d3dfvf_xyzrhw | d3dfvf_diffuse | d3dfvf_tex1 ); </P> <p> struct <br/> {<br/> float X, Y, Z, rhw; <br/> d3dcolor CLR; <br/> float tu, TV; <br/>} pvertices [4]; </P> <p> pvertices [0]. X = fleft; <br/> pvertices [0]. y = fbottom; <br/> pvertices [0]. z = 1.f; <br/> pvertices [0]. rhw = 1.f; <br/> pvertices [0]. CLR = CLR; <br/> pvertices [0]. tu = 0.f; <br/> pvertices [0]. TV = 1.f; </P> <p> pvertices [1]. X = fleft; <br/> pvertices [1]. y = ftop; <br/> pvertices [1]. z = 1.f; <br/> pvertices [1]. rhw = 1.f; <br/> pvertices [1]. CLR = CLR; <br/> pvertices [1]. tu = 0.f; <br/> pvertices [1]. TV = 0.f; </P> <p> pvertices [2]. X = fright; <br/> pvertices [2]. y = fbottom; <br/> pvertices [2]. z = 1.f; <br/> pvertices [2]. rhw = 1.f; <br/> pvertices [2]. CLR = CLR; <br/> pvertices [2]. tu = 1.f; <br/> pvertices [2]. TV = 1.f; </P> <p> pvertices [3]. X = fright; <br/> pvertices [3]. y = ftop; <br/> pvertices [3]. z = 1.f; <br/> pvertices [3]. rhw = 1.f; <br/> pvertices [3]. CLR = CLR; <br/> pvertices [3]. tu = 1.f; <br/> pvertices [3]. TV = 0.f; </P> <p> pd3ddevice9-> drawprimitiveup (d3dpt_trianglestrip, 2, pvertices, sizeof (pvertices [0]); </P> <p> pd3ddevice9-> endscene (); </P> <p> return s_ OK; <br/>}

 

4. setstate () function:

Void setstate () <br/>{< br/> pd3ddevice9-> setrenderstate (success, true); <br/> pd3ddevice9-> setrenderstate (d3drs_srcblend, d3dblend_srcalpha ); <br/> pd3ddevice9-> setrenderstate (d3drs_destblend, success); <br/> pd3ddevice9-> settexturestagestate (0, d3dtss_alphaop, d3dtop_modulate ); <br/> pd3ddevice9-> settexturestagestate (0, d3dtss_alph1_g1, d3dta_diffuse); <br/>}< br/>

 

5. display the present () function:

Hresult present () <br/>{< br/> setstate (); <br/> drawtexture (ptexture1.p, 567,440, 0 xffffffff ); <br/> drawtexture (ptexture2.p, 667,420, 0xa0ffffff); <br/> pd3ddevice9-> present (null, null); <br/> return s_ OK; <br/>}

 

6. process the message: Call create (hwnd) in the wm_create message; call present () in the middle of beginpaint (hwnd, & PS) and endpaint (hwnd, & PS) of the wm_paint message ();

 

Note:

1. rendering by distance and proximity;

2. Set a high 8-bit rhw value to handle transparency. That is, the ** bit in 0x ** ffffff, 00 indicates completely transparent, FF indicates opacity, and the intermediate value is translucent.

 

Engineering: http://download.csdn.net/source/3318014

 

 

 

 

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.