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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

查看: 2966|回复: 2

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

[复制链接]
发表于 2008-9-23 10:52:16 | 显示全部楼层 |阅读模式
//------------------------------------------------------------------------------------
// Create direct3D interface and direct3D device.
//------------------------------------------------------------------------------------
bool TRANSPARENT_BLEND::Create_D3D_Device(HWND hwnd, bool full_screen)
{
// Create a IDirect3D9 object and returns an interace to it.
_d3d = Direct3DCreate9(D3D_SDK_VERSION);
if(_d3d == NULL)
return false;

// retrieve adapter capability
D3DCAPS9 d3d_caps;
_d3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3d_caps);

bool hardware_process_enable = (d3d_caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT ? true : false);

// Retrieves the current display mode of the adapter.
D3DDISPLAYMODE display_mode;
if(FAILED(_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &display_mode)))
return false;

// set present parameter for direct3D device
D3DPRESENT_PARAMETERS present_param;

ZeroMemory(&present_param,
sizeof(present_param));

present_param.BackBufferWidth = WINDOW_WIDTH;
present_param.BackBufferHeight = WINDOW_HEIGHT;
present_param.BackBufferFormat = display_mode.Format;
present_param.BackBufferCount = 1;
present_param.hDeviceWindow = hwnd;
present_param.Windowed = !full_screen;
present_param.SwapEffect = D3DSWAPEFFECT_FLIP;
present_param.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;

// Creates a device to represent the display adapter.
DWORD behavior_flags;

behavior_flags = hardware_process_enable ?
D3DCREATE_HARDWARE_VERTEXPROCESSING : D3DCREATE_SOFTWARE_VERTEXPROCESSING;

if(FAILED(_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, behavior_flags,
&present_param, &_d3d_device)))
{
return false;
}

// create successfully
return true;
}

//------------------------------------------------------------------------------------
// Initialize vertex buffer for tiger image.
//------------------------------------------------------------------------------------
bool TRANSPARENT_BLEND::Init_Tiger_Vertex_Buffer()
{
CUSTOM_VERTEX tiger_vertex[] =
{
{0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
{800.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f},
{0.0f, 600.0f, 0.0f, 1.0f, 0.0f, 1.0f},
{800.0f, 600.0f, 0.0f, 1.0f, 1.0f, 1.0f}
};

BYTE* vertex_data;

// create vertex buffer
if(FAILED(_d3d_device->CreateVertexBuffer(4 * sizeof(CUSTOM_VERTEX), 0, CUSTOM_VERTEX_FVF,
D3DPOOL_MANAGED, &_tiger_vertex_buffer, NULL)))
{
return false;
}

// get data pointer to vertex buffer
if(FAILED(_tiger_vertex_buffer->Lock(0, 0, (void **) &vertex_data, 0)))
return false;

// copy custom vertex data into vertex buffer
memcpy(vertex_data, tiger_vertex, sizeof(tiger_vertex));

// unlock vertex buffer
_tiger_vertex_buffer->Unlock();

return true;
}

//------------------------------------------------------------------------------------
// Initialize vertex buffer for gun image.
//------------------------------------------------------------------------------------
bool TRANSPARENT_BLEND::Init_Gun_Vertex_Buffer()
{
CUSTOM_VERTEX gun_vertex[] =
{
{m_gun_pos_x, m_gun_pos_y, 0.0f, 1.0f, 0.0f, 0.0f},
{m_gun_pos_x+130.0f, m_gun_pos_y, 0.0f, 1.0f, 1.0f, 0.0f},
{m_gun_pos_x, m_gun_pos_y+130.0f, 0.0f, 1.0f, 0.0f, 1.0f},
{m_gun_pos_x+130.0f, m_gun_pos_y+130.0f, 0.0f, 1.0f, 1.0f, 1.0f}
};

BYTE* vertex_data;

// create vertex buffer
if(FAILED(_d3d_device->CreateVertexBuffer(4 * sizeof(CUSTOM_VERTEX), 0, CUSTOM_VERTEX_FVF,
D3DPOOL_MANAGED, &_gun_vertex_buffer, NULL)))
{
return false;
}

// get data pointer to vertex buffer
if(FAILED(_gun_vertex_buffer->Lock(0, 0, (void **) &vertex_data, 0)))
return false;

// copy custom vertex data into vertex buffer
memcpy(vertex_data, gun_vertex, sizeof(gun_vertex));

// unlock vertex buffer
_gun_vertex_buffer->Unlock();

return true;

}

 楼主| 发表于 2008-9-23 10:56:48 | 显示全部楼层
//------------------------------------------------------------------------------------
// Create all texture interfaces from file.
//------------------------------------------------------------------------------------
bool TRANSPARENT_BLEND::Create_All_Texture()
{
// Creates tiger texture from file
if(FAILED(D3DXCreateTextureFromFile(_d3d_device, "tiger.jpg", &_tiger_texture)))
{
MessageBox(NULL, "Create texture interface for image tiger failed.", "ERROR", MB_OK);
return false;
}

// Creates gun texture from file
if(FAILED(D3DXCreateTextureFromFileEx(
_d3d_device,
// Pointer to an IDirect3DDevice9 interface
"gun.bmp", // Pointer to a string that specifies the filename
D3DX_DEFAULT, // Width in pixels
D3DX_DEFAULT, // Height in pixels.
D3DX_DEFAULT, // Number of mip levels requested, D3DX_DEFAULT means a complete mipmap chain is created.
0, // Usage
D3DFMT_A8R8G8B8, // describing the requested pixel format for the texture
D3DPOOL_MANAGED, // describing the memory class into which the texture should be placed
D3DX_FILTER_TRIANGLE, // how the image is filtered (Filter)
D3DX_FILTER_TRIANGLE, // how the image is filtered (MipFilter)
D3DCOLOR_RGBA(0, 255, 0, 255), // D3DCOLOR value to replace with transparent black
NULL, // Pointer to a D3DXIMAGE_INFO structure to be filled in with a description of the data
// in the source image file
NULL, // Pointer to a PALETTEENTRY structure, representing a 256-color palette to fill in
&_gun_texture))) // Address of a pointer to an IDirect3DTexture9 interface
{
MessageBox(NULL, "Create texture interface for image gun failed.", "ERROR", MB_OK);
return false;
}

return true;
}

//------------------------------------------------------------------------------------
// Render image tiger and gun.
//------------------------------------------------------------------------------------
void TRANSPARENT_BLEND::Render()
{
if(_d3d_device == NULL)
return;

// clear back surface with color black
_d3d_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0, 0);

// indicate d3d device begin draw scene
_d3d_device->BeginScene();

// 1) draw for image tiger

// Binds a vertex buffer to a device data stream.
_d3d_device->SetStreamSource(0, _tiger_vertex_buffer, 0, sizeof(CUSTOM_VERTEX));

// Sets the current vertex stream declaration.
_d3d_device->SetFVF(CUSTOM_VERTEX_FVF);

// Assigns a texture to a stage for a device.
_d3d_device->SetTexture(0, _tiger_texture);

// draw tiger image
_d3d_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

// 2) draw for image gun

// Binds a vertex buffer to a device data stream
_d3d_device->SetStreamSource(0, _gun_vertex_buffer, 0, sizeof(CUSTOM_VERTEX));

// Assigns a texture to a stage for a device
_d3d_device->SetTexture(0, _gun_texture);

// enable per pixel alpha testing
_d3d_device->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);

// specifies a reference alpha value against which pixels are tested
_d3d_device->SetRenderState(D3DRS_ALPHAREF, 0x01);

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

// draw gun image
_d3d_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

// disable per pixel alpha testing now
_d3d_device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);

// indicate d3d device to end scene
_d3d_device->EndScene();

// Presents the contents of the next buffer in the sequence of back buffers owned by the device.
_d3d_device->resent(NULL, NULL, NULL, NULL);
}

//------------------------------------------------------------------------------------
// Release all COM object.
//------------------------------------------------------------------------------------
void TRANSPARENT_BLEND::Release_COM_Object()
{
Safe_Release(_tiger_texture);
Safe_Release(_gun_texture);
Safe_Release(_tiger_vertex_buffer);
Safe_Release(_gun_vertex_buffer);
Safe_Release(_d3d_device);
Safe_Release(_d3d);
}

注释很详尽,看懂应该没问题了。

来看看测试文件main.cpp的定义:

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

 楼主| 发表于 2008-9-23 10:57:16 | 显示全部楼层
PURPOSE:
Test for transparent blending.
*************************************************************************************/

#define DIRECTINPUT_VERSION 0x0800

#include "GE_COMMON.h"
#include "GE_APP.h"
#include "GE_INPUT.h"
#include "TransparentBlend.h"

#pragma warning(disable : 4305 4996)

int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR cmd_line, int cmd_show)
{
GE_APP ge_app;
GE_INPUT ge_input;
TRANSPARENT_BLEND tb;

MSG msg = {0};

// create window
if(! ge_app.Create_Window("Transparent blending test", instance, cmd_show))
return false;

HWND hwnd = ge_app.Get_Window_Handle();

SetWindowPos(hwnd, 0, 0,0,0,0, SWP_NOSIZE);
SetCursorPos(0, 0);

// create direct input
ge_input.Create_Input(instance, hwnd);

// Create direct3D interface and direct3D device.
if(! tb.Create_D3D_Device(hwnd, false))
return false;

// initialize vertex buffer for tiger
if(! tb.Init_Tiger_Vertex_Buffer())
return false;

// initialize vertex buffer for gun
if(! tb.Init_Gun_Vertex_Buffer())
return false;

// Create all texture interfaces from file.
tb.Create_All_Texture();

// draw tiger and gun
tb.Render();

while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0,0 , PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
// get keyboard input
if(ge_input.Read_Keyboard())
{
if(ge_input.Is_Key_Pressed(DIK_ESCAPE))
PostQuitMessage(0);

bool key_right_pressed = ge_input.Is_Key_Pressed(DIK_RIGHT);
bool key_left_pressed = ge_input.Is_Key_Pressed(DIK_LEFT);
bool key_up_pressed = ge_input.Is_Key_Pressed(DIK_UP);
bool key_down_pressed = ge_input.Is_Key_Pressed(DIK_DOWN);

if(key_right_pressed || key_left_pressed || key_up_pressed || key_down_pressed)
{
if(key_right_pressed) // press key "→"
tb.m_gun_pos_x += 5.5f;

if(key_left_pressed) // press key "←"
tb.m_gun_pos_x -= 5.5f;

if(key_up_pressed) // press key "↑"
tb.m_gun_pos_y -= 5.5f;

if(key_down_pressed) // press key "↓"
tb.m_gun_pos_y += 5.5f;

// reset vertex buffer for gun
tb.Init_Gun_Vertex_Buffer();

// render tiger and gun
tb.Render();

}
}
}
}

UnregisterClass(WINDOW_CLASS_NAME, instance);

return true;
}

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-2-6 06:51

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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