//////////////////////////////////////////////////////////////////////////////////////////////////
//
// File: mtrlAlpha.cpp
//
// Author: Frank Luna (C) All Rights Reserved
//
// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0
//
// Desc: Renders a semi transparent teapot using alpha blending. In this
// sample, the alpha is taken from the material's diffuse alpha value.
// You can increase the opaqueness with the 'A' key and can descrease
// it with the 'S' key.
//
//////////////////////////////////////////////////////////////////////////////////////////////////
#include "d3dUtility.h"
//
// Globals
//
IDirect3DDevice9* Device = 0;
const int Width = 640;
const int Height = 480;
ID3DXMesh* Teapot = 0;
D3DMATERIAL9 TeapotMtrl;
IDirect3DVertexBuffer9* BkGndQuad = 0;
IDirect3DTexture9* BkGndTex = 0;
D3DMATERIAL9 BkGndMtrl;
struct Vertex
{
Vertex(){}
Vertex(
float x, float y, float z,
float nx, float ny, float nz,
float u, float v)
{
_x = x; _y = y; _z = z;
_nx = nx; _ny = ny; _nz = nz;
_u = u; _v = v;
}
float _x, _y, _z;
float _nx, _ny, _nz;
float _u, _v; // texture coordinates
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
bool Setup()
{
// Init Materials
TeapotMtrl = d3d::RED_MTRL;
TeapotMtrl.Diffuse.a = 0.5f; // set alpha to 50% opacity
BkGndMtrl = d3d::WHITE_MTRL;
// Create the teapot.
D3DXCreateTeapot(Device, &Teapot, 0);
// Create the background quad.
Device->CreateVertexBuffer(
6 * sizeof(Vertex),
D3DUSAGE_WRITEONLY,
Vertex::FVF,
D3DPOOL_MANAGED,
&BkGndQuad,
0);
Vertex* v;
BkGndQuad->Lock(0, 0, (void**)&v, 0);
v[0] = Vertex(-10.0f, -10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[1] = Vertex(-10.0f, 10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
v[2] = Vertex( 10.0f, 10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[3] = Vertex(-10.0f, -10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[4] = Vertex( 10.0f, 10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[5] = Vertex( 10.0f, -10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
BkGndQuad->Unlock();
// Setup a directional light.
D3DLIGHT9 dir;
::ZeroMemory(&dir, sizeof(dir));
dir.Type = D3DLIGHT_DIRECTIONAL;
dir.Diffuse = d3d::WHITE;
dir.Specular = d3d::WHITE * 0.2f;
dir.Ambient = d3d::WHITE * 0.6f;
dir.Direction = D3DXVECTOR3(0.707f, 0.0f, 0.707f);
Device->SetLight(0, &dir);
Device->LightEnable(0, true);
Device->SetRenderState(D3DRS_NORMALIZENORMALS, true);
Device->SetRenderState(D3DRS_SPECULARENABLE, true);
// Create texture and set texture filters.
D3DXCreateTextureFromFile(
Device,
"crate.jpg",
&BkGndTex);
Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
// Set alpha blending states.
// use alpha in material's diffuse component for alpha
Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
// set blending factors so that alpha component determines transparency
Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
Device->SetRenderState(D3DRS_LIGHTING, TRUE);
// Set camera.
D3DXVECTOR3 pos(0.0f, 0.0f, -3.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &pos, &target, &up);
Device->SetTransform(D3DTS_VIEW, &V);
// Set projection matrix.
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)Width / (float)Height,
1.0f,
1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);
return true;
}
void Cleanup()
{
d3d::Release<IDirect3DVertexBuffer9*>(BkGndQuad);
d3d::Release<ID3DXMesh*>(Teapot);
d3d::Release<IDirect3DTexture9*>(BkGndTex);
}
bool Display(float timeDelta)
{
if( Device )
{
// increase/decrease alpha via keyboard input
if( ::GetAsyncKeyState('A') & 0x8000f )
TeapotMtrl.Diffuse.a += 0.01f;
if( ::GetAsyncKeyState('S') & 0x8000f )
TeapotMtrl.Diffuse.a -= 0.01f;
// force alpha to [0, 1] interval
if(TeapotMtrl.Diffuse.a > 1.0f)
TeapotMtrl.Diffuse.a = 1.0f;
if(TeapotMtrl.Diffuse.a < 0.0f)
TeapotMtrl.Diffuse.a = 0.0f;
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
Device->BeginScene();
// Draw the background
D3DXMATRIX W;
D3DXMatrixIdentity(&W);
Device->SetTransform(D3DTS_WORLD, &W);
Device->SetFVF(Vertex::FVF);
Device->SetStreamSource(0, BkGndQuad, 0, sizeof(Vertex));
Device->SetMaterial(&BkGndMtrl);
Device->SetTexture(0, BkGndTex);
Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
// Draw the teapot
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
{
D3DXMatrixScaling(&W, 1.5f, 1.5f, 1.5f);
Device->SetTransform(D3DTS_WORLD, &W);
Device->SetMaterial(&TeapotMtrl);
Device->SetTexture(0, 0);
Teapot->DrawSubset(0);
}
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
Device->EndScene();
Device->Present(0, 0, 0, 0);
}
return true;
}
//
// WndProc
//
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg )
{
case WM_DESTROY:
::PostQuitMessage(0);
break;
case WM_KEYDOWN:
if( wParam == VK_ESCAPE )
::DestroyWindow(hwnd);
break;
}
return ::DefWindowProc(hwnd, msg, wParam, lParam);
}
//
// WinMain
//
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE prevInstance,
PSTR cmdLine,
int showCmd)
{
if(!d3d::InitD3D(hinstance,
Width, Height, true, D3DDEVTYPE_HAL, &Device))
{
::MessageBox(0, "InitD3D() - FAILED", 0, 0);
return 0;
}
if(!Setup())
{
::MessageBox(0, "Setup() - FAILED", 0, 0);
return 0;
}
d3d::EnterMsgLoop( Display );
Cleanup();
Device->Release();
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
D3D9 alpha混合 demo (自定义alpha和纹理自带alpha) (160个子文件)
autoclean.bat 280B
mtrlAlpha.cpp 6KB
d3dUtility.cpp 6KB
d3dUtility.cpp 6KB
texAlpha.cpp 5KB
cube.cpp 4KB
texAlpha.vcxproj.filters 1KB
MtrlAlpha.vcxproj.filters 1KB
D3D11.h 365KB
ddraw.h 241KB
D2D1.h 237KB
D3D10.h 228KB
dinput.h 222KB
DWrite.h 207KB
d3d9.h 160KB
d3dx9mesh.h 125KB
D3D11SDKLayers.h 119KB
xnamath.h 113KB
dsound.h 111KB
d3d9types.h 88KB
d3d10sdklayers.h 87KB
DXGI.h 81KB
xact3.h 77KB
D3D10_1.h 71KB
D3D10effect.h 70KB
XAudio2.h 62KB
D3DX10math.h 61KB
d3dx9tex.h 60KB
d3dx9math.h 58KB
d3dx9anim.h 42KB
d3dx9effect.h 41KB
d3dx9shader.h 38KB
XAPO.h 34KB
dvp.h 32KB
dinputd.h 32KB
xma2defs.h 32KB
D3D10shader.h 31KB
rpcsal.h 29KB
D3DX11tex.h 26KB
D3DX10tex.h 26KB
D2D1Helper.h 25KB
d3d9caps.h 24KB
XDSP.h 24KB
gameux.h 24KB
d3dx9core.h 23KB
X3DAudio.h 22KB
XAudio2fx.h 21KB
rmxftmpl.h 17KB
xact3wb.h 17KB
XAPOBase.h 17KB
D3D11Shader.h 16KB
multimon.h 16KB
d3dx10async.h 15KB
D3DCSX.h 15KB
D3DX10core.h 14KB
D3D10_1shader.h 12KB
d3dx9xof.h 12KB
D3DX10mesh.h 11KB
audiodefs.h 11KB
xact3d3.h 11KB
dsconf.h 10KB
dsetup.h 9KB
XInput.h 9KB
rmxfguid.h 9KB
D3DX11async.h 8KB
D3Dcompiler.h 8KB
dxfile.h 8KB
dxdiag.h 8KB
d3dx9shape.h 8KB
XAPOFX.h 7KB
PIXPlugin.h 6KB
d3d10misc.h 5KB
D2Derr.h 5KB
DXGIFormat.h 5KB
DXGIType.h 4KB
D3DX11core.h 4KB
D2DBaseTypes.h 3KB
DxErr.h 3KB
comdecl.h 3KB
D3Dcommon.h 2KB
d3dUtility.h 2KB
d3dUtility.h 2KB
Dcommon.h 2KB
d3dx9.h 2KB
D3DX11.h 2KB
D3DX10.h 2KB
vertex.h 853B
cube.h 793B
dxsdkver.h 486B
xnamathvector.inl 383KB
xnamathconvert.inl 190KB
xnamathmatrix.inl 102KB
xnamathmisc.inl 76KB
D3DX10math.inl 45KB
d3dx9math.inl 44KB
crate.jpg 38KB
lobbyxpos.JPG 31KB
DxErr.lib 5.45MB
DxErr.lib 5.16MB
dxguid.lib 787KB
共 160 条
- 1
- 2
资源评论
felicityWSH
- 粉丝: 58
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功