• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天行健 君子當自強而不息

            D3D中的Z緩存使用示例

            在渲染多邊形網格對象到場景中的時候,離觀察者越遠的對象應該越模糊,同時離觀察者越近的物體應該越清楚,這就是深度排序(depth sorting)。深度排序有兩種常用的方法。

            第一種方法稱為畫家算法(painter's algorithm)。這種方法將對象劃分成不同的多邊形,由后往前對這些多邊形進行排序,再按照排好的順序繪制出這些多邊形。采用這種方法繪制多邊形,能夠確保前面的多邊形總是在其后多邊形之前進行繪制。

            深度排序的第二種方法稱為z緩沖方法(z- buffer),它是圖形硬件設備使用最多的方法。這種方法依賴于像素,每個像素都有一個z值(z值是像素距離觀察者的距離)。當每個像素被寫入時,渲染器首先檢查是否已經存在一個z值更小的像素,如果不存在,這個像素就被繪制出來;如果存在,就跳過該像素。

            許多 3D圖形加速卡都有一個內置的z緩沖,這也是深度排序選擇z緩沖方法的原因。在應用程序中使用z緩沖,最容易的方法就是在創建設備對象以及設置顯示方式的時候初始化z緩沖,如下所示:

               D3DPRESENT_PARAMETERS d3dpp;

               ZeroMemory(
            &d3dpp, sizeof(d3dpp));

              d3dpp.Windowed                
            = TRUE;
              d3dpp.SwapEffect              
            = D3DSWAPEFFECT_DISCARD;
              d3dpp.BackBufferFormat        
            = d3ddm.Format;
              d3dpp.EnableAutoDepthStencil  
            = TRUE;
              d3dpp.AutoDepthStencilFormat  
            = D3DFMT_D16;

              
            if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd,
                                             D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
            &d3dpp, &g_pD3DDevice)))
                
            return FALSE;

              
            // Set the rendering states
              g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
              g_pD3DDevice
            ->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);

            同時在繪制每幀之前應該用IDirect3DDevice9::Clear來清除z緩存。

            // clear device back buffer
                g_d3d_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(000255), 1.0f0);

            完整代碼如下所示:

            /***************************************************************************************
            PURPOSE:
                ZBuffer Demo

            Required libraries:
              WINMM.LIB, D3D9.LIB, D3DX9.LIB.
             **************************************************************************************
            */

            #include 
            <windows.h>
            #include 
            <stdio.h>
            #include 
            "d3d9.h"
            #include 
            "d3dx9.h"

            #pragma comment(lib, 
            "winmm.lib")
            #pragma comment(lib, 
            "d3d9.lib")
            #pragma comment(lib, 
            "d3dx9.lib")

            #pragma warning(disable : 
            4305)

            #define WINDOW_WIDTH    400
            #define WINDOW_HEIGHT   400

            #define Safe_Release(p) if((p)) (p)->Release();

            // window handles, class and caption text.
            HWND g_hwnd;
            HINSTANCE g_inst;
            static char g_class_name[] = "ZBufferClass";
            static char g_caption[]    = "ZBuffer Demo";

            // the Direct3D and device object
            IDirect3D9* g_d3d = NULL;
            IDirect3DDevice9
            * g_d3d_device = NULL;

            // The 3D vertex format and descriptor
            typedef struct
            {
                
            float x, y, z;  // 3D coordinates    
                D3DCOLOR color; // diffuse color
            } VERTEX;

            #define VERTEX_FVF   (D3DFVF_XYZ | D3DFVF_DIFFUSE)

            IDirect3DVertexBuffer9
            * g_vertex_buffer = NULL;

            //--------------------------------------------------------------------------------
            // Window procedure.
            //--------------------------------------------------------------------------------
            long WINAPI Window_Proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
            {
                
            switch(msg)
                {
                
            case WM_DESTROY:
                    PostQuitMessage(
            0);
                    
            return 0;
                }

                
            return (long) DefWindowProc(hwnd, msg, wParam, lParam);
            }

            //--------------------------------------------------------------------------------
            // Initialize d3d, d3d device, vertex buffer; set render state for d3d;
            // set perspective matrix and view matrix.
            //--------------------------------------------------------------------------------
            BOOL Do_Init()
            {
                D3DPRESENT_PARAMETERS present_param;
                D3DDISPLAYMODE  display_mode;
                D3DXMATRIX mat_proj, mat_view;
                BYTE
            * vertex_ptr;

                
            // initialize vertex data
                VERTEX verts[] = {
                  { 
            -100.0f-100.0f0.0f, D3DCOLOR_RGBA(255,0,0,255) },
                  { 
            -100.0f,  100.0f0.0f, D3DCOLOR_RGBA(255,0,0,255) },
                  {  
            100.0f-100.0f0.0f, D3DCOLOR_RGBA(255,0,0,255) },
                  {  
            100.0f,  100.0f0.0f, D3DCOLOR_RGBA(255,0,0,255) },
                  { 
            -100.0f-100.0f0.0f, D3DCOLOR_RGBA(255,0,0,255) },
                  { 
            -100.0f,  100.0f0.0f, D3DCOLOR_RGBA(255,0,0,255) },

                  { 
            -100.0f-100.0f0.0f, D3DCOLOR_RGBA(0,0,255,255) },
                  { 
            -100.0f,  100.0f0.0f, D3DCOLOR_RGBA(0,0,255,255) },
                  {  
            100.0f-100.0f0.0f, D3DCOLOR_RGBA(0,0,255,255) },
                  {  
            100.0f,  100.0f0.0f, D3DCOLOR_RGBA(0,0,255,255) },
                }; 

                
            // do a windowed mode initialization of Direct3D
                if((g_d3d = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
                    
            return FALSE;

                
            // retrieves the current display mode of the adapter
                if(FAILED(g_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &display_mode)))
                    
            return FALSE;

                ZeroMemory(
            &present_param, sizeof(present_param));

                
            // initialize d3d presentation parameter
                present_param.Windowed               = TRUE;
                present_param.SwapEffect             
            = D3DSWAPEFFECT_DISCARD;
                present_param.BackBufferFormat       
            = display_mode.Format;
                present_param.EnableAutoDepthStencil 
            = TRUE;
                present_param.AutoDepthStencilFormat 
            = D3DFMT_D16;

                
            // creates a device to represent the display adapter
                if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hwnd,
                                              D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
            &present_param, &g_d3d_device)))
                    
            return FALSE;     

                
            // set render state

                
            // disable d3d lighting
                g_d3d_device->SetRenderState(D3DRS_LIGHTING, FALSE);
                
            // enable z-buffer
                g_d3d_device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);

                
            // create and set the projection matrix

                
            // builds a left-handed perspective projection matrix based on a field of view
                D3DXMatrixPerspectiveFovLH(&mat_proj, D3DX_PI/4.01.333331.01000.0);

                
            // sets a single device transformation-related state
                g_d3d_device->SetTransform(D3DTS_PROJECTION, &mat_proj);

                
            // create and set the view matrix
                D3DXMatrixLookAtLH(&mat_view, 
                                   
            &D3DXVECTOR3(0.00.0-500.0),
                                   
            &D3DXVECTOR3(0.0f0.0f0.0f), 
                                   
            &D3DXVECTOR3(0.0f1.0f0.0f));

                g_d3d_device
            ->SetTransform(D3DTS_VIEW, &mat_view);

                
            // create the vertex buffer and set data
                g_d3d_device->CreateVertexBuffer(sizeof(verts), 0, VERTEX_FVF, D3DPOOL_DEFAULT, &g_vertex_buffer, NULL);

                
            // locks a range of vertex data and obtains a pointer to the vertex buffer memory
                g_vertex_buffer->Lock(00, (void**)&vertex_ptr, 0);

                memcpy(vertex_ptr, verts, 
            sizeof(verts));

                
            // unlocks vertex data
                g_vertex_buffer->Unlock();

                
            return TRUE;
            }

            //--------------------------------------------------------------------------------
            // Release all d3d resource.
            //--------------------------------------------------------------------------------
            BOOL Do_Shutdown()
            {
                Safe_Release(g_vertex_buffer);
                Safe_Release(g_d3d_device);
                Safe_Release(g_d3d);

                
            return TRUE;
            }

            //--------------------------------------------------------------------------------
            // Render a frame.
            //--------------------------------------------------------------------------------
            BOOL Do_Frame()
            {
                D3DXMATRIX mat_world;

                
            // clear device back buffer
                g_d3d_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(000255), 1.0f0);

                
            // Begin scene
                if(SUCCEEDED(g_d3d_device->BeginScene()))
                {
                    
            // set the vertex stream, shader.

                    
            // binds a vertex buffer to a device data stream
                    g_d3d_device->SetStreamSource(0, g_vertex_buffer, 0sizeof(VERTEX));

                    
            // set the current vertex stream declation
                    g_d3d_device->SetFVF(VERTEX_FVF);

                    
            // create and set the world transformation matrix
                    
            // rotate object along y-axis
                    D3DXMatrixRotationY(&mat_world, (float) (timeGetTime() / 1000.0));
                    
                    g_d3d_device
            ->SetTransform(D3DTS_WORLD, &mat_world);              

                    
            // renders a sequence of noindexed, geometric primitives of the specified type from the current set
                    
            // of data input stream.
                    g_d3d_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 04);

                    
            // draw next four polygons, but rotate on z-axis.
                    D3DXMatrixRotationZ(&mat_world, -(float)(timeGetTime() / 1000.0));
                    g_d3d_device
            ->SetTransform(D3DTS_WORLD, &mat_world);
                    g_d3d_device
            ->DrawPrimitive(D3DPT_TRIANGLESTRIP, 62);

                    
            // end the scene
                    g_d3d_device->EndScene();
                }

                
            // present the contents of the next buffer in the sequence of back buffers owned by the device
                g_d3d_device->Present(NULL, NULL, NULL, NULL);

                
            return TRUE;
            }

            //--------------------------------------------------------------------------------
            // Main function, routine entry.
            //--------------------------------------------------------------------------------
            int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
            {
                WNDCLASSEX  win_class;
                MSG         msg;

                g_inst 
            = inst;

                
            // create window class and register it
                win_class.cbSize        = sizeof(win_class);
                win_class.style         
            = CS_CLASSDC;
                win_class.lpfnWndProc   
            = Window_Proc;
                win_class.cbClsExtra    
            = 0;
                win_class.cbWndExtra    
            = 0;
                win_class.hInstance     
            = inst;
                win_class.hIcon         
            = LoadIcon(NULL, IDI_APPLICATION);
                win_class.hCursor       
            = LoadCursor(NULL, IDC_ARROW);
                win_class.hbrBackground 
            = NULL;
                win_class.lpszMenuName  
            = NULL;
                win_class.lpszClassName 
            = g_class_name;
                win_class.hIconSm       
            = LoadIcon(NULL, IDI_APPLICATION);

                
            if(! RegisterClassEx(&win_class))
                    
            return FALSE;

                
            // create the main window
                g_hwnd = CreateWindow(g_class_name, g_caption, WS_CAPTION | WS_SYSMENU, 00,
                                      WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, inst, NULL);

                
            if(g_hwnd == NULL)
                    
            return FALSE;

                ShowWindow(g_hwnd, SW_NORMAL);
                UpdateWindow(g_hwnd);

                
            // initialize game
                if(Do_Init() == FALSE)
                    
            return FALSE;

                
            // start message pump, waiting for signal to quit.
                ZeroMemory(&msg, sizeof(MSG));

                
            while(msg.message != WM_QUIT)
                {
                    
            if(PeekMessage(&msg, NULL, 00, PM_REMOVE))
                    {
                        TranslateMessage(
            &msg);
                        DispatchMessage(
            &msg);
                    }
                    
                    
            // draw a frame
                    if(Do_Frame() == FALSE)
                        
            break;
                }

                
            // run shutdown function
                Do_Shutdown();

                UnregisterClass(g_class_name, inst);
                
                
            return (int) msg.wParam;
            }

            效果圖:



            posted on 2007-07-04 20:53 lovedday 閱讀(1436) 評論(0)  編輯 收藏 引用 所屬分類: ■ DirectX 9 Program

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            精品久久久久久久久久中文字幕 | 狠狠久久综合伊人不卡| 人妻少妇久久中文字幕| 国产成人精品免费久久久久| 狠狠狠色丁香婷婷综合久久五月| 国产精品久久午夜夜伦鲁鲁| 一本久久久久久久| 久久无码中文字幕东京热| 99久久精品国产一区二区| 久久99国产精品久久久| 亚洲国产高清精品线久久| 久久午夜伦鲁片免费无码| 亚洲国产天堂久久综合网站| 亚洲人成电影网站久久| 99热成人精品热久久669| 亚洲精品无码久久毛片 | 国产成年无码久久久久毛片| 精品视频久久久久| 2020久久精品国产免费| 久久成人小视频| 久久99精品国产99久久6男男| 青草久久久国产线免观| 国内精品久久久久影院一蜜桃| 亚洲а∨天堂久久精品9966| 久久99精品国产麻豆宅宅| 久久99久国产麻精品66| 久久天天躁狠狠躁夜夜2020老熟妇| 狠狠色丁香久久婷婷综合五月 | 久久天天躁狠狠躁夜夜avapp| 精品久久综合1区2区3区激情| AAA级久久久精品无码片| 久久综合亚洲色一区二区三区| 久久激情五月丁香伊人| 国产99久久久久久免费看| 少妇高潮惨叫久久久久久| 久久精品一区二区三区AV| 亚洲国产小视频精品久久久三级 | 久久免费视频一区| 国产一区二区精品久久凹凸| 日韩精品国产自在久久现线拍| 精品国产一区二区三区久久久狼|