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

时间:2022-12-09 07:59:54

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;
}

DirectX基础学习系列5 融合技术的更多相关文章

  1. DirectX 基础学习系列5 纹理映射

    1 纹理坐标 类似BMP图像坐标系,左上为原点 纹理坐标为了规范化,范围限定在[0,1]之间,使用纹理的时候,需要修改顶点结构 struct ColorVetex { float x, y,z; fl ...

  2. DirectX基础学习系列8 渐进网格以及外接体

    1 IUnknown--> ID3DXBUFFER D3D泛型接口: GetBufferPointer Retrieves a pointer to the data in the buffer ...

  3. DirectX基础学习系列1

    1.3 基础 1.3.1表面 表面接口:     IDirect3DSurface9 获得表面信息:GetDesc(D3DSURFACE_DESC) 获得表面接口指针 :LockRect( D3DLO ...

  4. directX基础学习系列7 网格(自己创建)

    D3DXMesh 以及 D3DXPMesh都是从ID3DXBaseMesh类中集成,mesh基本是对三角单元进行操作 ID3DXBaseMesh主要函数: HRESULT DrawSubset( DW ...

  5. DirectX 基础学习系列6 字体

    DIRECTX9自带ID3DXFONT类 内部调用GDI的接口,效率一般,但能够处理一些复杂的字体 HRESULT D3DXCreateFontIndirect( LPDIRECT3DDEVICE9 ...

  6. DirectX基础学习系列4 颜色和光照

    4.1颜色表示 RGB颜色:D3DCOLOR  可以用宏D3DCOLOR_ARGB(a,r,g,b)  D3DCOLOR_XRGB(255,r,g,b) 另外一种浮点表示:D3DCOLORVALUE, ...

  7. DirectX基础学习系列2

    补充第一章矩阵内容 向量 1 3D空间向量,包含浮点数类型坐标 D3DXVECTOR-->D3DXVECTOR3 2向量的长度 D3DXVector3Length(const D3DXVECTO ...

  8. Linux基础学习系列目录导航

    Linux基础学习-通过VM安装RHEL7.4 Linux基础学习-命令行与图形界面切换 Linux基础学习-基本命令 Linux基础学习-RHEL7.4之YUM更换CentOS源 Linux基础学习 ...

  9. Bootstrap基础学习 ---- 系列文章

    [Bootstrap基础学习]05 Bootstrap学习总结 [Bootstrap基础学习]04 Bootstrap的HTML和CSS编码规范 [Bootstrap基础学习]03 Bootstrap ...

随机推荐

  1. compass tables 表格 表格常见样式&lbrack;Sass和compass学习笔记&rsqb;

    demo 源码 地址 https://github.com/qqqzhch/webfans compass  的表格提供了集中常见样式 表格边框 outer-table-borders($width, ...

  2. XPath Axes(轴)

    XML 实例文档 我们将在下面的例子中使用此 XML 文档: <?xml version="1.0" encoding="ISO-8859-1"?> ...

  3. lvs的dr模式分析(二)

      #vim /etc/init.d/lvsdrrip #!/bin/bash #DR server VIP=192.168.46.200 case "$1" in start) ...

  4. github添加ssh key报错Key is invalid&period; Ensure you&&num;39&semi;ve copied the file correctly

    github添加ssh key的时候报错:Key is invalid. Ensure you've copied the file correctly 将秘钥复制粘贴到文本编辑器中,再粘贴复制到

  5. Windows下快捷键

    1.任务管理器ctrl+alt+delete 2.切换任务窗口alt+tab 3.命令行Win+R 命令: {打开任务管理器:taskmgr} {打开远程桌面连接:mstsc} 4.回到桌面Win+D ...

  6. MBR所在位置

    如果offset的000000000位置如下图所示(主要看红色框框位置是否出现NTFS字样),说明系统文件是NTFS "EB 52"至"55 AA"位置是MBR ...

  7. 原生的AJAX写法,可以直接复制过来套用

    方法一: function createXMLHTTPRequest() { //1.创建XMLHttpRequest对象 //这是XMLHttpReuquest对象无部使用中最复杂的一步 //需要针 ...

  8. python3&period;6 pip 出现locations that require TLS&sol;SSL异常解决方案

    在给CentOS服务器安装完Python3.6后,使用pip命令出现问题,提示说无法找到ssl模块. 上网查询后发现在安装Python3.6时没有安装openssl-devel依赖库,解决方案如下: ...

  9. FM在特征组合中的应用

    原文来自:博客园(华夏35度)http://www.cnblogs.com/zhangchaoyang 作者:Orisun 特征组合   x1年龄 x2北京 x3上海 x4深圳 x5男 x6女 用户1 ...

  10. Java实现对ftp的读写文件

    这里仅仅是对ftp工具类的简单使用,很多东西还不是很了解.当然学以致用,先用到这里吧. public class FtpTest { /** * 向ftp写文件(数据) */ @Test public ...