索引缓冲区

 在Direct3D中,实体模型中的一个点可能被多个三角形面所共用,如果每个三角形都构造三个顶点,对比较浪费资源的,为了节省资源,引入了索引缓冲区:把顶点的具体数据和代表图元格式的顶点顺序分开存储,顶点数据仍然放到顶点缓存区中,索引缓存区则按照图元格式,顺序存放顶点的索引。

下面是一个简单的使用例子,画出两个三角形:

  1. /******************************************************
  2. FileName: CTDUIMessage.h   - TDOnline/UISystem
  3. Author: lsm   Version:        Date: 08/11/4
  4. Description: 索引缓冲区的使用
  5. History:
  6.   
  7. ******************************************************/
  8. /*-------------Head------------*/
  9. #include "assert.h"
  10. #include "d3dx9.h"
  11. #pragma warning(disable : 4996)
  12. #include "strsafe.h"
  13. #pragma warning(disable : 4996)
  14. LPDIRECT3D9                g_pD3DObj   = NULL; //D3D 对象
  15. LPDIRECT3DDEVICE9          g_pD3DDev   = NULL; //D3D 设备
  16. LPDIRECT3DVERTEXBUFFER9    g_pVerBuf   = NULL; //顶点缓冲区
  17. LPDIRECT3DINDEXBUFFER9     g_pInBuf    = NULL; //索引缓冲区
  18. TCHAR g_AppName[] = TEXT("D3D");//类名
  19. #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)//灵活顶点格式
  20. struct CUSTOMVERTEX 
  21. {
  22.     D3DXVECTOR3 position;
  23.     float       rhw;
  24.     D3DCOLOR    color;
  25. };
  26. /*---------------End------------*/
  27. /*==========================================
  28. Function name: InitD3D
  29. Description: 初始化D3D对象
  30. ==========================================*/
  31. HRESULT InitD3D(HWND hWnd)
  32. {
  33.     if((g_pD3DObj = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)//创建3D对象
  34.     {
  35.         assert(false);
  36.         return E_FAIL;
  37.     }
  38.     D3DDISPLAYMODE devMode;
  39.     if (FAILED(g_pD3DObj->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &devMode)))//获得显卡信息
  40.     {
  41.         assert(false);
  42.         return E_FAIL;
  43.     }
  44.     D3DPRESENT_PARAMETERS d3dpp;
  45.     ZeroMemory(&d3dpp, sizeof(d3dpp));
  46.     d3dpp.Windowed               = true;
  47.     d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;
  48.     d3dpp.BackBufferFormat       = D3DFMT_UNKNOWN;
  49.     if(FAILED(g_pD3DObj->CreateDevice(
  50.                                       D3DADAPTER_DEFAULT,
  51.                                       D3DDEVTYPE_HAL,
  52.                                       hWnd,
  53.                                       D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  54.                                       &d3dpp,
  55.                                       &g_pD3DDev
  56.                                       )))
  57.     {
  58.         assert(false);
  59.         return E_FAIL;
  60.     }
  61.     return S_OK;
  62. }
  63. /*==========================================
  64. Function name: InitVBuf
  65. Description: 填充顶点缓冲区
  66. ==========================================*/
  67. HRESULT InitVBuf()
  68. {
  69.     if (FAILED(g_pD3DDev->CreateVertexBuffer(
  70.                                              4*sizeof(CUSTOMVERTEX),
  71.                                              0,
  72.                                              D3DFVF_CUSTOMVERTEX,
  73.                                              D3DPOOL_DEFAULT,
  74.                                              &g_pVerBuf,
  75.                                              NULL
  76.                                              )))
  77.     {
  78.         return E_FAIL;
  79.     }
  80.     CUSTOMVERTEX *vertices;
  81.     if (FAILED(g_pVerBuf->Lock(0, 0, (void**)&vertices, 0)))//锁住内存
  82.     {
  83.         return E_FAIL;
  84.     }
  85.     vertices[0].position = D3DXVECTOR3(150.0f,  50.0f, 1.0f);//四个点,注意三角形的构造顺序
  86.     vertices[0].rhw      = 1.0f;
  87.     vertices[0].color    = 0xffff0000;
  88.     vertices[1].position = D3DXVECTOR3( 50.0f, 220.0f, 1.0f);
  89.     vertices[1].rhw      = 1.0f;
  90.     vertices[1].color    = 0xff0000ff;
  91.     vertices[2].position = D3DXVECTOR3(150.0f, 250.0f, 1.0f);
  92.     vertices[2].rhw      = 1.0f;
  93.     vertices[2].color    = 0xffffffff;
  94.     vertices[3].position = D3DXVECTOR3(250.0f, 250.0f, 0.0f);
  95.     vertices[3].rhw      = 1.0f;
  96.     vertices[3].color    = 0xff000fff;
  97.     g_pVerBuf->Unlock();
  98.     WORD indexBuf[] = {0, 3, 2, 0, 2, 1};//以下是索引缓冲区
  99.     void *pIndex;
  100.     g_pD3DDev->CreateIndexBuffer(sizeof(indexBuf), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT,&g_pInBuf, NULL);
  101.     g_pInBuf->Lock(0, sizeof(indexBuf), (void **)&pIndex, 0);
  102.     memcpy(pIndex, indexBuf, sizeof(indexBuf));
  103.     g_pInBuf->Unlock();
  104.     return S_OK;
  105. }
  106. /*==========================================
  107. Function name: Cleanup
  108. Description: 释放对象
  109. ==========================================*/
  110. VOID Cleanup()
  111. {
  112.     if (g_pVerBuf != NULL)
  113.     {
  114.         g_pVerBuf->Release();
  115.     }
  116.     if (g_pD3DDev != NULL)
  117.     {
  118.         g_pD3DDev->Release();
  119.     }
  120.     if (g_pD3DObj != NULL)
  121.     {
  122.         g_pD3DObj->Release();
  123.     }
  124. }
  125. /*==========================================
  126. Function name: Render
  127. Description: 负责渲染
  128. ==========================================*/
  129. VOID Render()
  130. {
  131.     if (g_pD3DDev == NULL)
  132.     {
  133.         return ;
  134.     }
  135.     g_pD3DDev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1, 0);
  136.     if (SUCCEEDED(g_pD3DDev->BeginScene()))
  137.     {
  138.         g_pD3DDev->SetStreamSource(0, g_pVerBuf, 0, sizeof(CUSTOMVERTEX));
  139.         g_pD3DDev->SetFVF(D3DFVF_CUSTOMVERTEX);
  140.         g_pD3DDev->SetIndices(g_pInBuf);
  141.         g_pD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 2);
  142.         //g_pD3DDev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
  143.         g_pD3DDev->EndScene();
  144.     }
  145.     g_pD3DDev->Present(NULL, NULL, NULL, NULL);
  146. }
  147. /*==========================================
  148. Function name: MsgProc
  149. Description: 负责消息循环
  150. ==========================================*/
  151. LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  152. {
  153.     switch(msg)
  154.     {
  155.     case WM_DESTROY:
  156.         Cleanup();
  157.         PostQuitMessage(0);
  158.         return 0;
  159.     }
  160.     return DefWindowProc(hWnd, msg, wParam, lParam);
  161. }
  162. /*==========================================
  163. Function name: WinMain
  164. Description: 程序启动函数
  165. ==========================================*/
  166. INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
  167. {
  168.     WNDCLASSEX wc = {
  169.                    sizeof(WNDCLASSEX), 
  170.                    CS_CLASSDC, 
  171.                    MsgProc, 
  172.                    0L, 
  173.                    0L, 
  174.                    GetModuleHandle(NULL),
  175.                    NULL,
  176.                    NULL,
  177.                    NULL,
  178.                    NULL,
  179.                    g_AppName,
  180.                    NULL
  181.                    };
  182.     RegisterClassEx(&wc);
  183.     HWND hWnd = CreateWindow(
  184.                              g_AppName, 
  185.                              TEXT("D3D Tutorial 1"), 
  186.                              WS_OVERLAPPEDWINDOW, 
  187.                              100, 
  188.                              100, 
  189.                              300, 
  190.                              300,
  191.                              NULL, 
  192.                              NULL, 
  193.                              wc.hInstance, 
  194.                              NULL 
  195.                              );
  196.     if (hWnd == NULL)
  197.     {
  198.         return -1;
  199.     }
  200.     if(SUCCEEDED(InitD3D(hWnd)))
  201.     {
  202.         if(SUCCEEDED(InitVBuf()))
  203.         {
  204.             ShowWindow(hWnd, SW_SHOWDEFAULT);
  205.             UpdateWindow(hWnd);
  206.             MSG msg;
  207.             ZeroMemory(&msg, sizeof(msg));
  208.             while (msg.message != WM_QUIT)
  209.             {
  210.                 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  211.                 {
  212.                     TranslateMessage(&msg);
  213.                     DispatchMessage(&msg);
  214.                 }
  215.                 else
  216.                 {
  217.                     Render();
  218.                 }
  219.             }
  220.         }
  221.     }
  222.     UnregisterClass(g_AppName, wc.hInstance);
  223.     return 0;
  224. }

你可能感兴趣的:(索引缓冲区)