• <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>

            天行健 君子當(dāng)自強(qiáng)而不息

            D3D中用3D方法繪制2D圖象的例子

            思路與一個用D3D繪制2D圖形的例子相差不多,主要的區(qū)別在頂點的數(shù)據(jù)結(jié)構(gòu)定義不一樣。

            2D的VERTEX結(jié)構(gòu)定義為:

            // The 2D vertex format and descriptor
            typedef struct
            {
                
            float x, y, z;  // 2D coordinates
                float rhw;      // rhw
                float u, v;     // texture coordinates
            } VERTEX;

            #define VERTEX_FVF   (D3DFVF_XYZRHW | D3DFVF_TEX1)

            3D的VERTEX結(jié)構(gòu)定義為:

            // The 3D vertex format and descriptor
            typedef struct
            {
                
            float x, y, z;  // 3D coordinates    
                float u, v;     // texture coordinates
            } VERTEX;

            #define VERTEX_FVF   (D3DFVF_XYZ | D3DFVF_TEX1)

            其他的區(qū)別主要在Do_Init函數(shù),3D模式啟用深度蒙板,并且格式為16位z-緩存位深;接著禁用光照,并且啟用z緩存;再接著設(shè)置透視矩陣和視口矩陣。

               present_param.EnableAutoDepthStencil = TRUE;
               present_param.AutoDepthStencilFormat 
            = D3DFMT_D16;

                // 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);

            函數(shù)Do_Frame也有一些區(qū)別,調(diào)用Clear來清除后備緩沖時增加了一個D3DCLEAR_ZBUFFER提示D3D要清除z 緩存,接著設(shè)置世界矩陣。

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

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

            完整源碼如下:

            /***************************************************************************************
            PURPOSE:
                3D Drawing 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[] = "Draw3DClass";
            static char g_caption[]    = "Draw3D 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    
                float u, v;     // texture coordinates
            } VERTEX;

            #define VERTEX_FVF   (D3DFVF_XYZ | D3DFVF_TEX1)

            IDirect3DVertexBuffer9
            * g_vertex_buffer = NULL;
            IDirect3DTexture9
            *      g_texture = 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, texutre; 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.0f0.0f0.0f },
                  {  
            100.0f,  100.0f0.0f1.0f0.0f },
                  { 
            -100.0f-100.0f0.0f0.0f1.0f },
                  {  
            100.0f-100.0f0.0f1.0f1.0f }
                }; 

                
            // 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(VERTEX) * 40, 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();

                
            // load the texture map
                D3DXCreateTextureFromFile(g_d3d_device, "Texture.bmp"&g_texture);

                
            return TRUE;
            }

            //--------------------------------------------------------------------------------
            // Release all d3d resource.
            //--------------------------------------------------------------------------------
            BOOL Do_Shutdown()
            {
                Safe_Release(g_vertex_buffer);
                Safe_Release(g_texture);
                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(064128255), 1.0f0);

                
            // Begin scene
                if(SUCCEEDED(g_d3d_device->BeginScene()))
                {
                    
            // create and set the world transformation matrix
                    
            // rotate object along z-axis
                    D3DXMatrixRotationZ(&mat_world, (float) (timeGetTime() / 1000.0));
                    
                    g_d3d_device
            ->SetTransform(D3DTS_WORLD, &mat_world);

                    
            // set the vertex stream, shader, and texture.

                    
            // 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);

                    
            // assigns a texture to a stage for a device
                    g_d3d_device->SetTexture(0, g_texture);

                    
            // 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, 02);

                    
            // release texture
                    g_d3d_device->SetTexture(0, NULL);

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

            效果(圖象繞z軸旋轉(zhuǎn)):



            posted on 2007-07-02 19:50 lovedday 閱讀(2416) 評論(0)  編輯 收藏 引用 所屬分類: ■ DirectX 9 Program

            公告

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關(guān)鏈接

            搜索

            最新評論

            区久久AAA片69亚洲| 99久久99久久| 欧洲成人午夜精品无码区久久| 久久亚洲中文字幕精品有坂深雪| 999久久久无码国产精品| 久久久久久av无码免费看大片| 亚洲精品tv久久久久久久久| 伊人久久精品线影院| 囯产极品美女高潮无套久久久| 一本大道久久a久久精品综合 | 97久久精品人妻人人搡人人玩| 国产A级毛片久久久精品毛片| 久久精品人人做人人爽电影 | 中文字幕无码av激情不卡久久| 久久久久久久人妻无码中文字幕爆| 久久强奷乱码老熟女网站| 精品国产福利久久久| 亚洲va中文字幕无码久久不卡| 久久精品国产亚洲7777| 精品国产91久久久久久久| 亚洲午夜久久久久妓女影院 | 伊人久久大香线蕉av不卡| 久久国产美女免费观看精品 | 亚洲国产精品久久久久| 亚洲中文字幕无码一久久区| 麻豆久久| 久久综合伊人77777麻豆| 国产精品美女久久久免费| 久久久久国产精品| 国产成人久久精品激情 | 久久久网中文字幕| 青青青国产精品国产精品久久久久 | 久久精品中文字幕一区| 一本久久综合亚洲鲁鲁五月天| 久久精品三级视频| 国产综合精品久久亚洲| 国产福利电影一区二区三区久久久久成人精品综合 | 天天躁日日躁狠狠久久| 少妇高潮惨叫久久久久久 | 国产亚州精品女人久久久久久 | 久久精品国产一区二区|