📄 dx6trans.htm
字号:
<TR><TD HEIGHT=10></TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> HBITMAP hBM; </TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> BITMAP BM; </TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> HDC hDCImage, hDC; </TD></TR> <TR><TD HEIGHT=5></TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> hBM = ( HBITMAP ) LoadImage( NULL, FileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION ); </TD></TR> <TR><TD HEIGHT=5></TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> GetObject( hBM, sizeof( BM ), &BM ); </TD></TR> <TR><TD HEIGHT=5></TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> hDCImage = CreateCompatibleDC( NULL ); </TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> SelectObject( hDCImage, hBM ); </TD></TR> <TR><TD HEIGHT=5></TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> if( SUCCEEDED( TheSurface->GetDC( &hDC ))) </TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> { </TD></TR> <TR><TD></TD><TD></TD><TD></TD><TD COLSPAN=3> BitBlt( hDC, 0, 0, TextureWidth, TextureHeight, hDCImage, 0, 0, SRCCOPY ); </TD></TR> <TR><TD></TD><TD></TD><TD></TD><TD COLSPAN=3> TheSurface->ReleaseDC( hDC ); </TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> } </TD></TR> <TR><TD HEIGHT=5></TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> DeleteDC( hDCImage ); </TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> DeleteObject( hBM ); </TD></TR> <TR><TD HEIGHT=20></TD></TR> <TR><TD COLSPAN=6><CENTER> <A HREF="#TOP">Back to Top</A> </CENTER></TD></TR> <TR><TD HEIGHT=20></TD></TR></TABLE><A NAME="SETUP"><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100% BGCOLOR=#1010A0> <TR><TD><CENTER><FONT SIZE=+1>4. Setting up the Transparency</FONT></CENTER></TD></TR> </TABLE><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%> <TR><TD HEIGHT=10></TD></TR> <TR><TD WIDTH=5%></TD><TD WIDTH=5%></TD><TD WIDTH=5%></TD><TD WIDTH=5%></TD><TD WIDTH=5%></TD><TD WIDTH=75%></TD></TR> <TR><TD></TD><TD COLSPAN=5> Setting up the Transparency is a simple matter of locking the Texture's Surface, scanning through it one colour(as in 2 bytes for a 16-bit surface) at a time and setting the alpha value to 0 or 1. There is an example very similiar to this in the Frame work files. The code below locks the surface then retrieves the Alpha Mask and RGB Masks. Since the two possible formats are 1555 or 5551, AlphaMask | RGBMask will always equal 0xFFFF. This code then steps through each pixel in the surface. If the Pixel is black, it is cleared(just in case it already contained alpha information). Otherwise the pixel is OR'd with the AlphaMask(to make it visible). This code ensures that any value in the texture that was black is now non-visible. If you want other colors to be non-visible you need only compare the pixel to your color. The problem with this however is that colors have different values in different modes. One way to get around this is to make the first or last pixel in any bitmap your transparent color then check each pixel against that color. </TD></TR> <TR><TD HEIGHT=10></TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> DDSURFACEDESC2 Desc; </TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> ZeroMemory( &Desc, sizeof( Desc )); </TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> Desc.dwSize = sizeof( Desc ); </TD></TR> <TR><TD HEIGHT=5></TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> TheSurface->Lock( NULL, &Desc, DDLOCK_WAIT, NULL ); </TD></TR> <TR><TD HEIGHT=5></TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> WORD AlphaMask = ( WORD )Desc.ddpfPixelFormat.dwRGBAlphaBitMask; </TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> WORD RGBMask = ( WORD )( Desc.ddpfPixelFormat.dwRBitMask | Desc.ddpfPixelFormat.dwGBitMask | Desc.ddpfPixelFormat.dwBBitMask ); </TD></TR> <TR><TD HEIGHT=5></TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> WORD* SurfPtr; </TD></TR> <TR><TD HEIGHT=5></TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> for( DWORD y = 0; y < Desc.dwHeight; y++ ) </TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> { </TD></TR> <TR><TD></TD><TD></TD><TD></TD><TD COLSPAN=3> SurfPtr = ( WORD* )(( BYTE* )Desc.lpSurface + y * Desc.lPitch ); </TD></TR> <TR><TD HEIGHT=5></TD></TR> <TR><TD></TD><TD></TD><TD></TD><TD COLSPAN=3> for( DWORD x = 0; x < Desc.dwWidth; x++ ) </TD></TR> <TR><TD></TD><TD></TD><TD></TD><TD COLSPAN=3> { </TD></TR> <TR><TD></TD><TD></TD><TD></TD><TD></TD><TD COLSPAN=2> if(( *SurfPtr & RGBMask ) == 0 ) </TD></TR> <TR><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD> *SurfPtr = 0; </TD></TR> <TR><TD></TD><TD></TD><TD></TD><TD></TD><TD COLSPAN=2> else </TD></TR> <TR><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD> *SurfPtr |= AlphaMask; </TD></TR> <TR><TD HEIGHT=5></TD></TR> <TR><TD></TD><TD></TD><TD></TD><TD></TD><TD COLSPAN=2> SurfPtr++; </TD></TR> <TR><TD></TD><TD></TD><TD></TD><TD COLSPAN=3> } </TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> } </TD></TR> <TR><TD HEIGHT=5></TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> TheSurface->Unlock( NULL ); </TD></TR> <TR><TD HEIGHT=20></TD></TR> <TR><TD COLSPAN=6><CENTER> <A HREF="#TOP">Back to Top</A> </CENTER></TD></TR> <TR><TD HEIGHT=20></TD></TR></TABLE><A NAME="RENDER"> <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100% BGCOLOR=#1010A0> <TR><TD><CENTER><FONT SIZE=+1>5. Rendering the Texture</FONT></CENTER></TD></TR> </TABLE><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%> <TR><TD HEIGHT=10></TD></TR> <TR><TD WIDTH=5%></TD><TD WIDTH=5%></TD><TD WIDTH=5%></TD><TD WIDTH=5%></TD><TD WIDTH=5%></TD><TD WIDTH=75%></TD></TR> <TR><TD></TD><TD COLSPAN=5> Rendering the texture is done in the same way you would any other texture, using one of the DrawPrimitive functions. Alpha blending does not need to be on, since this is not blending. However, unless your transparent textures are only going to be used as overlays(like cursors or menu interfaces) you do need to turn on Alpha Testing. As far as I know, what this does is set the Z-Buffer based on the Alpha value. When it is disabled and you draw a rectangle on the screen, the Z-Buffer is updated so that even though you might see through a section of the texture you might chop off part of an object. For a simple example, render a rectangle with a texture where the center is transparent and render a cube in the center of the transparent section of this rectangle. You will only see the front half of the cube. To fix this, just set the renderstates accordingly: </TD></TR> <TR><TD HEIGHT=10></TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> pD3DDevice->SetRenderState( D3DRENDERSTATE_ALPHATESTENABLE, TRUE ); </TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> pD3DDevice->SetRenderState( D3DRENDERSTATE_ALPHAREF, 0x02 ); </TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> pD3DDevice->SetRenderState( D3DRENDERSTATE_ALPHAFUNC, D3DCMP_GREATEREQUAL ); </TD></TR> <TR><TD HEIGHT=10></TD></TR> <TR><TD></TD><TD COLSPAN=5> It's usually a good idea to Turn the Alpha Testing off when it's not needed: </TD></TR> <TR><TD HEIGHT=10></TD></TR> <TR><TD></TD><TD></TD><TD COLSPAN=4> pD3DDevice->SetRenderState( D3DRENDERSTATE_ALPHATESTENABLE, FALSE ); </TD></TR> <TR><TD HEIGHT=10></TD></TR> <TR><TD HEIGHT=20></TD></TR> <TR><TD COLSPAN=6><CENTER> <A HREF="#TOP">Back to Top</A> </CENTER></TD></TR> <TR><TD HEIGHT=20></TD></TR></TABLE><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100% BGCOLOR=#1010A0> <TR><TD HEIGHT=5><HR></TD></TR> </TABLE><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%> <TR><TD HEIGHT=10></TD></TR> <TR><TD></TD><TD COLSPAN=5> Well I hope this information was in some way useful. If you find any problems or typo's, please let me know at <A HREF="mailto:alamar@cgocable.net">alamar@cgocable.net</A>. </TD></TR> <TR><TD HEIGHT=20></TD></TR></TABLE><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100% BGCOLOR=#1010A0> <TR><TD><CENTER>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -