【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 3657|回复: 1

[DirectX] D3D中的Alpha颜色混合(4)

[复制链接]
发表于 2008-9-23 10:51:20 | 显示全部楼层 |阅读模式

Each valid filter must contain exactly one of the following flags: D3DX_FILTER_NONE, D3DX_FILTER_POINT, D3DX_FILTER_LINEAR, D3DX_FILTER_TRIANGLE, or D3DX_FILTER_BOX. In addition, you can use the OR operator to specify zero or more of the following optional flags with a valid filter: D3DX_FILTER_MIRROR_U, D3DX_FILTER_MIRROR_V, D3DX_FILTER_MIRROR_W, D3DX_FILTER_MIRROR, D3DX_FILTER_DITHER, D3DX_FILTER_SRGB_IN, D3DX_FILTER_SRGB_OUT or D3DX_FILTER_SRGB.

Specifying D3DX_DEFAULT for this parameter is usually the equivalent of specifying D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER. However, D3DX_DEFAULT can have different meanings, depending on which method uses the filter. For example:

  • When using D3DXCreateTextureFromFileEx, D3DX_DEFAULT maps to D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER.
  • When using D3DXFilterTexture, D3DX_DEFAULT maps to D3DX_FILTER_BOX if the texture size is a power of two, and D3DX_FILTER_BOX | D3DX_FILTER_DITHER otherwise.

Reference each method to check for information about how D3DX_DEFAULT filter is mapped.


参数pSrcInfo的类型D3DXIMAGE_INFO详细说明:

Returns a description of the original contents of an image file.

typedef struct D3DXIMAGE_INFO {
UINT Width;
UINT Height;
UINT Depth;
UINT MipLevels;
D3DFORMAT Format;
D3DRESOURCETYPE ResourceType;
D3DXIMAGE_FILEFORMAT ImageFileFormat;
} D3DXIMAGE_INFO, *LPD3DXIMAGE_INFO;

Members

Width
Width of original image in pixels.
Height
Height of original image in pixels.
Depth
Depth of original image in pixels.
MipLevels
Number of mip levels in original image.
Format
A value from the D3DFORMAT enumerated type that most closely describes the data in the original image.
ResourceType
Represents the type of the texture stored in the file. It is either D3DRTYPE_TEXTURE, D3DRTYPE_VOLUMETEXTURE, or D3DRTYPE_CubeTexture.
ImageFileFormat
Represents the format of the image file.

参数pPalette的类型PALETTEENTRY的详细说明:

Specifies the color and usage of an entry in a logical palette.

typedef struct PALETTEENTRY {
BYTE peRed;
BYTE peGreen;
BYTE peBlue;
BYTE peFlags;
} PALETTEENTRY, *LPPALETTEENTRY;

Members

peRed
The red intensity value for the palette entry.
peGreen
The green intensity value for the palette entry.
peBlue
The blue intensity value for the palette entry.
peFlags
The alpha intensity value for the palette entry. Note that as of DirectX 8, this member is treated differently than documented in the Platform SDK.

当背景色为透明黑色的纹理创建以后,就可以将该纹理对象所表示的图象透明地显示在其他图象的上面。

此时,需要设置渲染管道流水线的D3DRS_ALPHATESTENABLE状态值为TRUE,以开启Alpha测试。
D3DRS_ALPHATESTENABLE

TRUE to enable per pixel alpha testing. If the test passes, the pixel is processed by the frame buffer. Otherwise, all frame-buffer processing is skipped for the pixel.
The test is done by comparing the incoming alpha value with the reference alpha value, using the comparison function provided by the D3DRS_ALPHAFUNC render state. The reference alpha value is determined by the value set for D3DRS_ALPHAREF. For more information, see Alpha Testing State (Direct3D 9).

The default value of this parameter is FALSE.
// enable per pixel alpha testing
_d3d_device->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);

接着设置D3DRS_ALPHAREF的状态为一个0~255范围内的整数值。
// specifies a reference alpha value against which pixels are tested
_d3d_device->SetRenderState(D3DRS_ALPHAREF, 0x01);

再接着设置3DRS_ALPHAFUNC状态为D3DCMPFUNC的枚举值,决定测试将使用比较方式。

Defines the supported compare functions.

typedef enum D3DCMPFUNC
{
D3DCMP_NEVER = 1,
D3DCMP_LESS = 2,
D3DCMP_EQUAL = 3,
D3DCMP_LESSEQUAL = 4,
D3DCMP_GREATER = 5,
D3DCMP_NOTEQUAL = 6,
D3DCMP_GREATEREQUAL = 7,
D3DCMP_ALWAYS = 8,
D3DCMP_FORCE_DWORD = 0x7fffffff,
} D3DCMPFUNC, *LPD3DCMPFUNC;
 楼主| 发表于 2008-9-23 10:51:44 | 显示全部楼层

Constants

D3DCMP_NEVER
Always fail the test.
D3DCMP_LESS
Accept the new pixel if its value is less than the value of the current pixel.
D3DCMP_EQUAL
Accept the new pixel if its value equals the value of the current pixel.
D3DCMP_LESSEQUAL
Accept the new pixel if its value is less than or equal to the value of the current pixel.
D3DCMP_GREATER
Accept the new pixel if its value is greater than the value of the current pixel.
D3DCMP_NOTEQUAL
Accept the new pixel if its value does not equal the value of the current pixel.
D3DCMP_GREATEREQUAL
Accept the new pixel if its value is greater than or equal to the value of the current pixel.
D3DCMP_ALWAYS
Always pass the test.
D3DCMP_FORCE_DWORD
Forces this enumeration to compile to 32 bits in size. Without this value, some compilers would allow this enumeration to compile to a size other than 32 bits. This value is not used.

Remarks

The values in this enumerated type define the supported compare functions for the D3DRS_ZFUNC, D3DRS_ALPHAFUNC, and D3DRS_STENCILFUNC render states.

// Accept the new pixel if its value is greater than the value of the current pixel
_d3d_device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);



好了,该死的DirectX API 使用说明介绍终于可以结束了,来看一个具体的例子。

需要在工程中设置链接d3dx9.lib d3d9.lib dinput8.lib dxguid.lib。
由于文件中用到了GE_APP和GE_INPUT这两个类,它的具体使用说明请参阅 主窗口和DirectInput的封装。


若发现代码中存在错误,敬请指出。

源码及素材下载

来看看头文件TransparentBlend.h的定义:
/*************************************************************************************
[Include File]

PURPOSE:
Defines for Transparent blend.
*************************************************************************************/


#ifndef TRANSPARENT_BLEND_H
#define TRANSPARENT_BLEND_H

struct CUSTOM_VERTEX
{
float x, y, z, rhw;
float u, v;
};

#define CUSTOM_VERTEX_FVF (D3DFVF_XYZRHW | D3DFVF_TEX1)

class TRANSPARENT_BLEND
{
private:
IDirect3D9* _d3d;
IDirect3DDevice9* _d3d_device;
IDirect3DVertexBuffer9* _tiger_vertex_buffer;
IDirect3DVertexBuffer9* _gun_vertex_buffer;
IDirect3DTexture9* _tiger_texture;
IDirect3DTexture9* _gun_texture;

public:
float m_gun_pos_x, m_gun_pos_y;

public:
TRANSPARENT_BLEND();
~TRANSPARENT_BLEND();

bool Create_D3D_Device(HWND hwnd, bool full_screen = true);
bool Init_Tiger_Vertex_Buffer();
bool Init_Gun_Vertex_Buffer();
bool Create_All_Texture();
void Render();
void Release_COM_Object();
};

#endif

再来看看TransparentBlend.cpp的定义:

/*************************************************************************************
[Implement File]

PURPOSE:
Defines for Transparent blend.
*************************************************************************************/


#include "GE_COMMON.h"
#include "TransparentBlend.h"

//------------------------------------------------------------------------------------
// Constructor, initialize data.
//------------------------------------------------------------------------------------
TRANSPARENT_BLEND::TRANSPARENT_BLEND()
{
_d3d = NULL;
_d3d_device = NULL;
_tiger_vertex_buffer = NULL;
_gun_vertex_buffer = NULL;
_tiger_texture = NULL;
_gun_texture = NULL;

m_gun_pos_x = 500.0f;
m_gun_pos_y = 180.0f;
}

//------------------------------------------------------------------------------------
// Destructor, release COM object.
//------------------------------------------------------------------------------------
TRANSPARENT_BLEND::~TRANSPARENT_BLEND()
{
Release_COM_Object();
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2025-5-14 12:26

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表