⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dx6trans.htm

📁 关于windows游戏编程的一些文章还有相关图形
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD>   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">   <META NAME="GENERATOR" CONTENT="UltraEdit-32">   <META NAME="Author" CONTENT="Nathan Davies">   <TITLE>Transparency in Direct 3D Immediate Mode</TITLE></HEAD><BODY BACKGROUND="" BGCOLOR="#000000" TEXT="#FFFFFF" LINK="#00FF00" VLINK="#FFFF00" ALINK="#FF0000"><A NAME="TOP"><CENTER><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100% BGCOLOR=#1010A0>	<TR><TD>		<CENTER><FONT SIZE=+2>Transparency in D3D Immediate Mode</FONT></CENTER>	</TD></TR>		<TR><TD>		<CENTER>Written by <A HREF="mailto:alamar@cgocable.net">Nathan Davies</A> From <A HREF="http://www.cgocable.net/~alexpat/Alamar/">Alamar's Domain</A> on Feb 15, 1999</CENTER>	</TD></TR></TABLE><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%>	<TR><TD WIDTH=5%></TD><TD WIDTH=5%></TD><TD WIDTH=90%></TD></TR>		<TR><TD HEIGHT=20></TD></TR>		<TR><TD></TD><TD COLSPAN=2>		The purpose of this tutorial is to teach you, the reader what steps are necessary when drawing primitives in Direct 3D's Immediate Mode with textures containing transparency.  What I mean in this is that each pixel/texel of the texture is either visible or not visible.  In other words, the alpha value is either full or zero.  This method has the same effect as using source color keys in Direct Draw, but isn't nearly as easy to do in Direct 3D.	</TD></TR>	<TR><TD HEIGHT=10></TR>		<TR><TD></TD><TD COLSPAN=2>		This tutorial is NOT here to teach you about how to setup Direct 3D or how to render primitives.  I make the assumption that you already know how to do these things.  I have also removed most error checking code because I assume you have your own methods already.  In a perfect world you should check the response of every function call and react accordingly.	</TD></TR>	<TR><TD HEIGHT=10></TD><TD></TD><TD></TD></TR></TABLE><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%>	<TR><TD WIDTH=5%></TD><TD WIDTH=5%></TD><TD WIDTH=90%></TD></TR>	<TR><TD></TD><TD COLSPAN=2>		There are five main steps involved in rendering textures in this way:	</TD></TR>		<TR><TD HEIGHT=5></TD></TR>	<TR><TD></TD><TD></TD><TD>		1. <A HREF="#ENUM">Enumerating Texture Formats</A>	</TD></TR>	<TR><TD></TD><TD></TD><TD>		2. <A HREF="#CREATE">Creating the Texture</A>	</TD></TR>			<TR><TD></TD><TD></TD><TD>		3. <A HREF="#LOAD">Loading the Image</A>	</TD></TR>	<TR><TD></TD><TD></TD><TD>		4. <A HREF="#SETUP">Setting up the Transparency</A>	</TD></TR>	<TR><TD></TD><TD></TD><TD>		5. <A HREF="#RENDER">Rendering the Texture</A>	</TD></TR>		<TR><TD HEIGHT=20 BGCOLOR=#1010A0></TD></TR></TABLE><A NAME="ENUM"><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100% BGCOLOR=#1010A0>	<TR><TD><CENTER><FONT SIZE=+1>1. Enumerating Texture Formats</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>		Just like all the other DirectX Enumeration functions, EnumTextureFormats' first parameter is a pointer to your callback function.  The second paramter is a void pointer.  In this case we will be sending the address of a DDPIXELFORMAT Variable.  The EnumTextureFormats function is called using your LPDIRECT3DDEVICE3 pointer.  A good place for this code would be during DirectX Initialization right after QueryInterface'ing for the D3DDevice pointer.	</TD></TR>	<TR><TD HEIGHT=10></TD></TR>	<TR><TD></TD><TD></TD><TD COLSPAN=4>		DDPIXELFORMAT TexturePixelFormat;	</TD></TR>	<TR><TD></TD><TD></TD><TD COLSPAN=4>		pD3DDevice->EnumTextureFormats(( LPD3DENUMPIXELFORMATSCALLBACK )EnumTextures, ( void* )&TexturePixelFormat );			</TD></TR>	<TR><TD HEIGHT=5></TD></TR>	<TR><TD></TD><TD></TD><TD COLSPAN=4>	// If a Texture Format was NOT found, use the Current Back Surface Format instead<BR>	if( TexturePixelFormat.dwSize != sizeof( DDPIXELFORMAT ))	</TD></TR>	<TR><TD></TD><TD></TD><TD></TD><TD COLSPAN=3>		pBackSurface->GetPixelFormat( &TexturePixelFormat );	</TD></TR>	<TR><TD HEIGHT=10></TD></TR>	<TR><TD></TD><TD COLSPAN=5>		The LPD3DENUMPIXELFORMATSCALLBACK type is a pointer to a function that passes two parameters.  The first parameter is of type LPDDPIXELFORMAT.  This parameter will have the pixel format information for the current enumeration.  The following code is an example of how you might write this function.  This version will only accept 16bit textures with a single bit of alpha.  There are only two possible texture formats for this that I am aware of are:  RGBA 5551 and alternatively ARGB 1555.  If the desired texture format is found the enumeration is stopped and the supplied DDPIXELFORMAT value is filled.  If the desired format is not found the format will be set using the current primary/back format as above.	</TD></TR>	<TR><TD HEIGHT=10></TD></TR>	<TR><TD></TD><TD></TD><TD COLSPAN=4>		HRESULT CALLBACK EnumTextures( LPDDPIXELFORMAT DDPixelFormat, LPVOID pDDDesiredPixelFormat )	</TD></TR>		<TR><TD></TD><TD></TD><TD COLSPAN=4>		{	</TD></TR>		<TR><TD></TD><TD></TD><TD></TD><TD COLSPAN=3>			if( DDPixelFormat->dwFlags & DDPF_ALPHAPIXELS && DDPixelFormat->dwRGBBitCount == 16 )	</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( DDPixelFormat->dwRGBAlphaBitMask == 1 || DDPixelFormat->dwRGBAlphaBitMask == 0x8000 )	</TD></TR>	<TR><TD></TD><TD></TD><TD></TD><TD></TD><TD COLSPAN=2>				{	</TD></TR>	<TR><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD>			        memcpy( pDDDesiredPixelFormat, DDPixelFormat, sizeof(DDPIXELFORMAT) );	</TD></TR>	<TR><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD>					return D3DENUMRET_CANCEL;	</TD></TR>	<TR><TD></TD><TD></TD><TD></TD><TD></TD><TD COLSPAN=2>				}	</TD></TR>					<TR><TD></TD><TD></TD><TD></TD><TD COLSPAN=3>			}	</TD></TR>		<TR><TD></TD><TD></TD><TD></TD><TD COLSPAN=3>			return D3DENUMRET_OK;	</TD></TR>		<TR><TD></TD><TD></TD><TD COLSPAN=4>		}	</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="CREATE"><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100% BGCOLOR=#1010A0>	<TR><TD><CENTER><FONT SIZE=+1>2. Creating 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>		Creating the texture involves creating a surface and QueryInterface'ing for the texture.  For this you'll need a DDSURFACEDESC2 Variable initialized with the usual information.  You will also need to supply the PixelFormat from the section above.  You then create the surface using this information and Query for the texture.	</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( DDSURFACEDESC2 ));	</TD></TR>	<TR><TD></TD><TD></TD><TD COLSPAN=4>	Desc.dwSize = sizeof( Desc );	</TD></TR>	<TR><TD></TD><TD></TD><TD COLSPAN=4>	Desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;	</TD></TR>	<TR><TD></TD><TD></TD><TD COLSPAN=4>	Desc.dwWidth = Width;	</TD></TR>	<TR><TD></TD><TD></TD><TD COLSPAN=4>	Desc.dwHeight = Height;	</TD></TR>	<TR><TD></TD><TD></TD><TD COLSPAN=4>	Desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;	</TD></TR>	<TR><TD></TD><TD></TD><TD COLSPAN=4>	Desc.ddsCaps.dwCaps2 = DDSCAPS2_TEXTUREMANAGE;	</TD></TR>	<TR><TD></TD><TD></TD><TD COLSPAN=4>	Desc.ddpfPixelFormat = TexturePixelFormat;	</TD></TR>	<TR><TD HEIGHT=5></TD></TR>	<TR><TD></TD><TD></TD><TD COLSPAN=4>	pDD->CreateSurface( &Desc, &TheSurface, 0 );	</TD></TR>	<TR><TD></TD><TD></TD><TD COLSPAN=4>	TheSurface->QueryInterface( IID_IDirect3DTexture2, ( void **)&TheTexture );	</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="LOAD"><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100% BGCOLOR=#1010A0>	<TR><TD><CENTER><FONT SIZE=+1>3. Loading the Image</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>		Loading the Image, or setting up the surface, requires opening a bmp file and copying the information from it to the surface.  The following code uses a DC for this purpose since BitBlt will convert from the 24-bit BMP's I use to the 16-bit format of the surface supplied.  You could of course write all this yourself, or better yet load from a image file that already has an alpha value in it, but that's not the purpose of this tutorial.  That and I haven't done it myself :)	</TD></TR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -