Direct3D 索引缓存
小学的时候我们知道3个顶点组成一个三角形,那么四个顶点我们会说有4个三角形.这就是一个顶点同时参与了四次绘制三角形的结果. 在程序中也一样,比如我们绘制的两个三角形是挨着一起的,总有几个顶点是重合的. 这个顶点是可以重复使用,可以使用顶点缓存来记录如何绘制顶点的顺序,多次使用同一个顶点.到达内存的节省. 下面我们就是用四个顶点绘制出2个三角形
创建索引缓存:
LPDIRECT3DINDEXBUFFER9 g_pIndexBuffer = NULL; //索引缓存 //创建索引缓存 if(FAILED(g_pd3dDevice->CreateIndexBuffer(6 * sizeof(WORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&g_pIndexBuffer,0))) { return E_FAIL; }
访问索引缓存:
WORD * indices = 0; g_pIndexBuffer->Lock(0,0,(void**)&indices,0); //第一个三角形由0,1,3顶点组成 indices[0] = 0; indices[1] = 1; indices[2] = 3; //第二个三角形由1,2,3顶点组成 indices[3] = 1; indices[4] = 2; indices[5] = 3; g_pIndexBuffer->Unlock();
绘制图形:
g_pd3dDevice->SetStreamSource(0,g_pVertexBuffer,0,sizeof(CUSTOMVERTEX)); //设置绘制的顶点数据 g_pd3dDevice->SetIndices(g_pIndexBuffer); //设置索引缓存 g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX); //设置灵活顶点格式 g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,4,0,2); //根据索引绘制图形,4代表使用4个顶点,2代表两个三角形,其他查询API了解吧
效果图:
全部代码:
#include <d3d9.h> #include <d3dx9.h> #include <tchar.h> #pragma comment(lib,"winmm.lib") #pragma comment(lib,"d3d9.lib") #pragma comment(lib,"d3dx9.lib") #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 #define WINDOW_TITLE L"盘子脸的程序" #define SAFE_RELEASE(p) { if(p) { (p) -> Release();(p)=NULL;} } struct CUSTOMVERTEX { float x,y,z,rhw; DWORD color; }; #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE) //声明变量 LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; ID3DXFont* g_pFont = NULL; float g_FPS = 0.0f; wchar_t g_strFPS[50]; //包含帧速率的字符 LPDIRECT3DVERTEXBUFFER9 g_pVertexBuffer = NULL; //顶点缓冲区对象 LPDIRECT3DINDEXBUFFER9 g_pIndexBuffer = NULL; //索引缓存 //全局函数声明部分 LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam); HRESULT Direct3DInit(HWND hwnd); HRESULT ObjectsInit(HWND hwnd); VOID Direct3DRender(HWND hwnd); VOID Direct3DCleanUp(); //主函数 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) { //设置一个完整的窗口类,没有设置图标 WNDCLASSEX wndClass = {0}; wndClass.cbSize = sizeof(WNDCLASSEX); wndClass.style = CS_HREDRAW | CS_VREDRAW; //设置窗口样式 wndClass.lpfnWndProc = WndProc; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = hInstance; wndClass.hCursor = LoadCursor(NULL,IDC_ARROW); wndClass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); wndClass.lpszMenuName = NULL; wndClass.lpszClassName = L"GameDevelop"; //注册窗口类 if(!RegisterClassEx(&wndClass)) { return -1; } //正式创建窗口 HWND hwnd = CreateWindow(L"GameDevelop",WINDOW_TITLE,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,WINDOW_WIDTH, WINDOW_HEIGHT,NULL,NULL,hInstance,NULL); //初始化Direct3D资源 if(!(S_OK == Direct3DInit(hwnd))) { MessageBox(hwnd,_T("初始化Direct3D失败"),_T("盘子脸的消息窗口"),0); } MoveWindow(hwnd,250,80,WINDOW_WIDTH,WINDOW_HEIGHT,true); ShowWindow(hwnd,nShowCmd); UpdateWindow(hwnd); //消息循环过程 MSG msg = {0}; while(msg.message != WM_QUIT) { if(PeekMessage(&msg,0,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); }else { Direct3DRender(hwnd); } } //窗口类的注销 UnregisterClass(L"GameDevelop",wndClass.hInstance); return 0; } //消息处理函数 LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) { switch (message) { case WM_PAINT: Direct3DRender(hwnd); ValidateRect(hwnd,NULL); break; case WM_KEYDOWN: if(wParam == VK_ESCAPE) { DestroyWindow(hwnd); } break; case WM_DESTROY: Direct3DCleanUp(); //失败Direct3D资源 PostQuitMessage(0); break; default: return DefWindowProc(hwnd,message,wParam,lParam); } return 0; } HRESULT Direct3DInit(HWND hwnd) { //创建Direct3D接口对象 LPDIRECT3D9 pD3D = NULL; if(NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION))) { return E_FAIL; } //获取硬件消息 D3DCAPS9 caps; int vp = 0; if(FAILED(pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&caps))) { return E_FAIL; } if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) { //支出硬件顶点运算 vp = D3DCREATE_HARDWARE_VERTEXPROCESSING; } else { vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING; } //填充Direct3Dpresent参数 D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp,sizeof(d3dpp)); d3dpp.BackBufferWidth = WINDOW_WIDTH; d3dpp.BackBufferHeight = WINDOW_HEIGHT; d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; d3dpp.BackBufferCount = 1; d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; d3dpp.MultiSampleQuality = 0; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.hDeviceWindow = hwnd; d3dpp.Windowed = true; d3dpp.EnableAutoDepthStencil = true; d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; d3dpp.Flags = 0; d3dpp.FullScreen_RefreshRateInHz = 0; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; //创建Direct3D设备接口 if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,vp,&d3dpp,&g_pd3dDevice))) { return E_FAIL; } SAFE_RELEASE(pD3D); if(!(S_OK == ObjectsInit(hwnd))) { return E_FAIL; } return S_OK; } HRESULT ObjectsInit(HWND hwnd) { //创建字体 if(FAILED(D3DXCreateFont(g_pd3dDevice,36,0,0,1,false,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,DEFAULT_QUALITY,0,_T("微软雅黑"),&g_pFont))) { return E_FAIL; } //创建顶点缓存 if(FAILED(g_pd3dDevice->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVertexBuffer,NULL))) { return E_FAIL; } //创建索引缓存 if(FAILED(g_pd3dDevice->CreateIndexBuffer(6 * sizeof(WORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&g_pIndexBuffer,0))) { return E_FAIL; } //创建顶点数据 CUSTOMVERTEX vertices [] = { { 100.0f, 100.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(36, 45,0), }, { 300.0f, 400.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 255, 255), }, { 500.0f, 100.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0,44,0), }, { 300.0f, 100.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0,0,0), }, }; //填充顶点缓冲区 VOID *pVertices; if(FAILED(g_pVertexBuffer->Lock(0,sizeof(vertices),(void**)&pVertices,0))) //加锁 { return E_FAIL; } memcpy(pVertices,vertices,sizeof(vertices)); //把顶点数据保存到顶点缓存中 g_pVertexBuffer->Unlock(); //解锁 //这是索引缓存数据 WORD * indices = 0; g_pIndexBuffer->Lock(0,0,(void**)&indices,0); indices[0] = 0; indices[1] = 1; indices[2] = 3; indices[3] = 1; indices[4] = 2; indices[5] = 3; g_pIndexBuffer->Unlock(); g_pd3dDevice->SetRenderState(D3DRS_CULLMODE,false); return S_OK; } void Direct3DRender(HWND hwnd) { g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0); //定义一个矩形 RECT formatRect; GetClientRect(hwnd,&formatRect); g_pd3dDevice->BeginScene(); g_pd3dDevice->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_GOURAUD); //绘制图形 g_pd3dDevice->SetStreamSource(0,g_pVertexBuffer,0,sizeof(CUSTOMVERTEX)); //设置绘制的顶点数据 g_pd3dDevice->SetIndices(g_pIndexBuffer); g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX); //设置灵活顶点格式 //g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1); g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,4,0,2); //绘制文本信息 g_pFont->DrawTextW(NULL,L"盘子脸",3,&formatRect,DT_TOP | DT_RIGHT , D3DCOLOR_XRGB(255,39,136)); g_pd3dDevice->EndScene(); g_pd3dDevice->Present(NULL,NULL,NULL,NULL); } void Direct3DCleanUp() { SAFE_RELEASE(g_pVertexBuffer); SAFE_RELEASE(g_pFont); SAFE_RELEASE(g_pd3dDevice); }
如果你感兴趣,你可以把你妹妹介绍给我