Direct3D 半透明渲染

目的:

利用Direct3D实现图片A半透明渲染到图片B上面。

步骤:

1.创建标准WIN32程序,添加需要用到的对象定义和函数声明:

CComPtr pD3d9; CComPtr pD3dDevice9; CComPtr pTexture1; CComPtr pTexture2; D3DPRESENT_PARAMETERS gD3Dpp; HRESULT Create(HWND hWnd); HRESULT DrawTexture(IDirect3DTexture9* pTexture, float fLeft, float fTop, float fRight, float fBottom, D3DCOLOR clr); void SetState(); HRESULT Present();

 

2.创建需要的对象,即实现Create(HWND hWnd)函数:

HRESULT Create(HWND hWnd) { pD3d9 = Direct3DCreate9(D3D_SDK_VERSION); if (pD3d9 == NULL) { return S_FALSE; } D3DCAPS9 caps; pD3d9->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&caps); DWORD dwBF; if (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) { dwBF = D3DCREATE_HARDWARE_VERTEXPROCESSING; } else { dwBF = D3DCREATE_SOFTWARE_VERTEXPROCESSING; } if (caps.DevCaps & D3DDEVCAPS_PUREDEVICE) { dwBF |= D3DCREATE_PUREDEVICE; } D3DDISPLAYMODE d3ddm; pD3d9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm); gD3Dpp.BackBufferCount = 1; gD3Dpp.Windowed = TRUE; gD3Dpp.hDeviceWindow = hWnd; gD3Dpp.SwapEffect = D3DSWAPEFFECT_COPY; gD3Dpp.BackBufferFormat = d3ddm.Format; HRESULT hr = pD3d9->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, dwBF, &gD3Dpp, &pD3dDevice9); if (FAILED(hr)) { return hr; } D3DXCreateTextureFromFileEx(pD3dDevice9, L"B.jpg", D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &pTexture1.p); D3DXCreateTextureFromFileEx(pD3dDevice9, L"A.jpg", D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &pTexture2.p); return S_OK; }

 

3.渲染图片DrawTexture()函数:

HRESULT DrawTexture(IDirect3DTexture9* pTexture, float fLeft, float fTop, float fRight, float fBottom, D3DCOLOR clr) { pD3dDevice9->BeginScene(); pD3dDevice9->SetTexture(0,pTexture); pD3dDevice9->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1); struct { float x, y, z, rhw; D3DCOLOR clr; float tu, tv; } pVertices[4]; pVertices[0].x = fLeft; pVertices[0].y = fBottom; pVertices[0].z = 1.f; pVertices[0].rhw = 1.f; pVertices[0].clr = clr; pVertices[0].tu = 0.f; pVertices[0].tv = 1.f; pVertices[1].x = fLeft; pVertices[1].y = fTop; pVertices[1].z = 1.f; pVertices[1].rhw = 1.f; pVertices[1].clr = clr; pVertices[1].tu = 0.f; pVertices[1].tv = 0.f; pVertices[2].x = fRight; pVertices[2].y = fBottom; pVertices[2].z = 1.f; pVertices[2].rhw = 1.f; pVertices[2].clr = clr; pVertices[2].tu = 1.f; pVertices[2].tv = 1.f; pVertices[3].x = fRight; pVertices[3].y = fTop; pVertices[3].z = 1.f; pVertices[3].rhw = 1.f; pVertices[3].clr = clr; pVertices[3].tu = 1.f; pVertices[3].tv = 0.f; pD3dDevice9->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, pVertices, sizeof(pVertices[0])); pD3dDevice9->EndScene(); return S_OK; }

 

4.设置属性状态SetState()函数:

void SetState() { pD3dDevice9->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); pD3dDevice9->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); pD3dDevice9->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); pD3dDevice9->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE); pD3dDevice9->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE); }

 

5.显示Present()函数:

HRESULT Present() { SetState(); DrawTexture(pTexture1.p,300,40,567,440,0xFFFFFFFF); DrawTexture(pTexture2.p,400,20,667,420,0xA0FFFFFF); pD3dDevice9->Present(NULL,NULL,NULL,NULL); return S_OK; }

 

6.处理消息:在WM_CREATE消息中调用Create(hWnd);  在WM_PAINT消息的BeginPaint(hWnd, &ps)和EndPaint(hWnd, &ps)中间调用Present();

 

说明:

1.按照由远及近的顺序渲染;

2.通过设置rhw值的高8位来处理透明度,即0x**FFFFFF中的**位,00表示完全透明,FF表示不透明,中间的值是半透明。

 

 工程下载地址:http://download.csdn.net/source/3318014

 

 

 

 

你可能感兴趣的:(DirectX)