DirectX基础学习系列5 融合技术

7.1融合方程

1概念

融合技术将当前光栅化像素的颜色与以前已光栅化并处于同一个位置的像素颜色进行合成,即将当前要进行光栅化的三角形单元与已写入后台的像素进行融合

2需要遵循的原则:

(1)先绘制不需要融合的物体

(2)需要融合的物品按照摄像机的深度值进行排序

3融合方程

color = (RGBsrc * Ksrc) OP (RGBdst * Kdst)

Ksrc   OP    Kdst都为可以设置的参数

4开启融合运算

Device->SetRenderState(D3DRS_ALPHAENABLE,true);

7.2融合因子

设置融合因子

Device->SetRenderState(D3DRS_SRCBLEND,flag);

Device->SetRenderState(D3DRS_DESTBLEND,flag);

flag为D3DBLEND类型参数,可以为如下参数:

typedef enum D3DBLEND
{
D3DBLEND_ZERO = 1,
D3DBLEND_ONE = 2,
D3DBLEND_SRCCOLOR = 3,
D3DBLEND_INVSRCCOLOR = 4,
D3DBLEND_SRCALPHA = 5,
D3DBLEND_INVSRCALPHA = 6,
D3DBLEND_DESTALPHA = 7,
D3DBLEND_INVDESTALPHA = 8,
D3DBLEND_DESTCOLOR = 9,
D3DBLEND_INVDESTCOLOR = 10,
D3DBLEND_SRCALPHASAT = 11,
D3DBLEND_BOTHSRCALPHA = 12,
D3DBLEND_BOTHINVSRCALPHA = 13,
D3DBLEND_BLENDFACTOR = 14,
D3DBLEND_INVBLENDFACTOR = 15,
D3DBLEND_FORCE_DWORD = 0x7fffffff,
} D3DBLEND, *LPD3DBLEND;
半透明效果可以将SRC设置为D3DBLEND_SRCALPHA ,Dst设置为D3DBLEND_INVDESTALPHA ,可以实现透明效果
参考网页:http://www.cnblogs.com/wonderKK/archive/2011/11/07/2240372.html
7.3透明度的来源
HRESULT SetTextureStageState
  DWORD Stage,
D3DTEXTURESTAGESTATETYPE Type,
DWORD Value
);
typedef enum D3DTEXTURESTAGESTATETYPE
{
D3DTSS_COLOROP = 1,
D3DTSS_COLORARG1 = 2,
D3DTSS_COLORARG2 = 3,
D3DTSS_ALPHAOP = 4,
D3DTSS_ALPHAARG1 = 5,
D3DTSS_ALPHAARG2 = 6,
D3DTSS_BUMPENVMAT00 = 7,
D3DTSS_BUMPENVMAT01 = 8,
D3DTSS_BUMPENVMAT10 = 9,
D3DTSS_BUMPENVMAT11 = 10,
D3DTSS_TEXCOORDINDEX = 11,
D3DTSS_BUMPENVLSCALE = 22,
D3DTSS_BUMPENVLOFFSET = 23,
D3DTSS_TEXTURETRANSFORMFLAGS = 24,
D3DTSS_COLORARG0 = 26,
D3DTSS_ALPHAARG0 = 27,
D3DTSS_RESULTARG = 28,
D3DTSS_CONSTANT = 32,
D3DTSS_FORCE_DWORD = 0x7fffffff,
} D3DTEXTURESTAGESTATETYPE, *LPD3DTEXTURESTAGESTATETYPE;
设置aphla收来源于alpha通道:
Device->SetTextureStageState(0,D3DTSS_ALPHARG1,D3DDA_TEXTURE);
Device->SetTextureStageState(0,D3DTSS_ALPHA_OP,D3DOP_SELECTARG1);

7.4透明效果
(1)设置融合因子 D3DRS_SRCBLEND D3DRS_DESTBLEND
(2)设置alpha数据来源
(3)启用融合状态
 
 
#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;
 
//
// Classes and Structures
//
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;
 
//
// Framework Functions
//
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);
 
    //
    // 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 )
    {
        //
        // Update
        //
 
        // 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;
 
        //
        // Render
        //
 
        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;
}

上一篇:jQuery中事件绑定到bind、live、delegate、on方法的探究


下一篇:[Nginx笔记]关于线上环境CLOSE_WAIT和TIME_WAIT过高