索引緩存
一個索引緩存是一塊連續的存儲了索引資料的記憶體。
Length 配置設定給緩存的位元組大小。假如想得到一個能存儲3個頂點的頂點緩存,那麼我們就要在頂點結構中設定這個參數為3 * sizeof ( Vertex ) 。
Usage 指定關于怎樣使用緩存的額外資訊,可以為0,也可為一下幾個參數的一個或多個組合:
D3DUSAGE_DYNAMIC——設定這個參數可以使緩存是動态的。在下一頁說明靜态和動态緩存。
D3DUSAGE_POINTS——這個參數指定緩存存儲原始點。原始點将在第14章粒子系統中介紹。這個參數僅僅用在頂點緩沖中。
D3DUSAGE_SOFTWAREPROCESSING——使用軟體頂點處理。
D3DUSAGE_WRITEONLY——指定應用程式隻能寫緩存。它允許驅動程式配置設定最适合的記憶體位址作為寫緩存。注意如果從建立好的這種緩存中讀資料,将會傳回錯誤資訊。
FVF 存儲在緩存中的頂點格式。
Pool 緩存放置在哪一個記憶體池中。
ppIndexBuffer 傳回建立好的索引緩存的指針。
pShareHandle 目前沒有使用,一般傳NULL。
通路索引緩存
為了通路索引緩存,我們需要得到一個指針,是以我們需要使用IDirect3DIndexBuffer9::Lock與IDirect3DIndexBuffer9:: Unlock。
OffsetToLock 偏移量,以位元組為機關,從緩存開始位置到鎖定開始位置的距離。
SizeToLock 鎖定的位元組數。
Flags 标記描述怎樣鎖定記憶體。它可能是0 或者是下面參數中的1 個或多個的組合:
D3DLOCK_DISCARD——這個參數僅僅會在動态緩存時被使用。它訓示硬體丢棄緩存并傳回一個指向新配置設定的緩存的指針。這是很有用,因為當我們存取一個新配置設定的緩存時它允許硬體繼續從丢棄的緩存渲染。這防止了硬體延遲。
D3DLOCK_NOO VERWRITE——這個參數僅僅會在動态緩存時被使用。它聲明你将向緩存中添加資料。即,你不能向已經渲染的記憶體中寫資料。這是有好處的因為他允許你在添加新資料到緩存的同時讓硬體繼續渲染。
D3DLOCK_READONLY——這個參數聲明你鎖定的緩存隻能從中讀取資料而不能寫資料。這允許一些内在的優化。
視圖變換
把實體從世界空間轉換到視角空間,這個坐标變換被稱為視圖變換(View Transformation)。
pOut 指向傳回的視圖矩陣。
pEye 照相機在世界坐标系的位置。
pAt 照相機在世界坐标系的目标點。
pUp 世界坐标系的上方向。
繪制圖元(使用索引資訊)
Type 我們繪制的圖元類型。比如,我們能繪制點和線以及三角形。以後我們使用三角形用D3DPT_TRIANGLELIST參數。
BaseVertexIndex 一個基本數字,在調用中用它去加上索引。參看下面的說明。
MinIndex 将被引用的最小索引值。
NumVertices 在此調用中将被引用的頂點數。
StartIndex 索引到索引緩存中的某個位置,它标記開始渲染的開始索引點。
PrimitiveCount 繪制圖元的個數。
- #include <d3dx9.h>
- #include <string.h>
- #include <Windows.h>
- #define WINDOWNAME TEXT("D3DGameFrame")
- #define WINDOWCLASS TEXT("The GameFrame of Dull")
- #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
- #define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
- const int Width = 640;
- const int Height = 480;
- HWND mainWindowHandle = NULL;
- HINSTANCE hinstanceApp = NULL;
- bool ESC = true;
- IDirect3DDevice9* Device = NULL;
- IDirect3D9* d3d9 = NULL;
- HRESULT hr = NULL;
- IDirect3DVertexBuffer9* VB = NULL;
- IDirect3DIndexBuffer9* IB = NULL;
- struct Vertex
- {
- float x,y,z;
- };
- #define D3DFVF (D3DFVF_XYZ)
- LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- PAINTSTRUCT ps;
- HDC hdc;
- switch( msg )
- {
- case WM_PAINT:
- {
- hdc = BeginPaint(hWnd,&ps);
- EndPaint(hWnd,&ps);
- }
- break;
- case WM_DESTROY:
- {
- ::PostQuitMessage(0);
- }
- break;
- }
- return ::DefWindowProc(hWnd, msg, wParam, lParam);
- }
- int GameInit(void *parms = NULL, int num_parms = 0)
- {
- ESC = false;
- Device->CreateVertexBuffer(8*sizeof(Vertex),D3DUSAGE_WRITEONLY,D3DFVF,D3DPOOL_MANAGED,&VB,NULL);
- Device->CreateIndexBuffer(36*sizeof(WORD),D3DUSAGE_WRITEONLY,D3DFMT_INDEX16,D3DPOOL_MANAGED,&IB,NULL);
- Vertex Vertices[8] =
- {
- {-1.0f, -1.0f, -1.0f},
- {-1.0f, 1.0f, -1.0f},
- { 1.0f, 1.0f, -1.0f},
- { 1.0f, -1.0f, -1.0f},
- {-1.0f, -1.0f, 1.0f},
- {-1.0f, 1.0f, 1.0f},
- { 1.0f, 1.0f, 1.0f},
- { 1.0f, -1.0f, 1.0f}
- };
- void* pVertices;
- VB->Lock(0,0,(void**)&pVertices,NULL);
- memcpy(pVertices,Vertices,sizeof(Vertices));
- VB->Unlock();
- WORD* pIndices = NULL;
- IB->Lock(0,0,(void**)&pIndices,NULL);
- // 前面
- pIndices[0] = 0; pIndices[1] = 1; pIndices[2] = 2;
- pIndices[3] = 0; pIndices[4] = 2; pIndices[5] = 3;
- //後面
- pIndices[6] = 4; pIndices[7] = 6; pIndices[8] = 5;
- pIndices[9] = 4; pIndices[10] = 7; pIndices[11] = 6;
- //左面
- pIndices[12] = 4; pIndices[13] = 5; pIndices[14] = 1;
- pIndices[15] = 4; pIndices[16] = 1; pIndices[17] = 0;
- //右面
- pIndices[18] = 3; pIndices[19] = 2; pIndices[20] = 6;
- pIndices[21] = 3; pIndices[22] = 6; pIndices[23] = 7;
- //上面
- pIndices[24] = 1; pIndices[25] = 5; pIndices[26] = 6;
- pIndices[27] = 1; pIndices[28] = 6; pIndices[29] = 2;
- //下面
- pIndices[30] = 4; pIndices[31] = 0; pIndices[32] = 3;
- pIndices[33] = 4; pIndices[34] = 3; pIndices[35] = 7;
- IB->Unlock();
- //TODO:視圖變換
- D3DXMATRIX out;
- D3DXVECTOR3 eye(0.0f,0.0f,-5.0f);
- D3DXVECTOR3 at(0.0f,0.0f,0.0f);
- D3DXVECTOR3 up(0.0f,1.0f,0.0f);
- D3DXMatrixLookAtLH(&out,&eye,&at,&up);
- Device->SetTransform(D3DTS_VIEW, &out);
- //TODO:投影變換
- D3DXMATRIX proj;
- D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI*0.5f,(float)Width/(float)Height,1.0f,1000.0f);
- Device->SetTransform(D3DTS_PROJECTION,&proj);
- //TODO:設定渲染狀态
- Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
- return 1;
- }
- int GameMain(void *parms = NULL, int num_parms = 0)
- {
- if(ESC) return 0;
- //==========此處目前不必關心,隻知道是旋轉圖形即可==============
- D3DXMATRIX Rx, Ry;
- D3DXMatrixRotationX(&Rx, 3.14f / 4.0f);
- static float y = 0.0f;
- D3DXMatrixRotationY(&Ry, y);
- y += 0.0001f;
- if( y >= 6.28f )
- y = 0.0f;
- D3DXMATRIX p = Rx * Ry;
- Device->SetTransform(D3DTS_WORLD, &p);
- //=========================================================
- Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f , 0);
- //TODO:繪制圖形
- Device->BeginScene();
- Device->SetStreamSource( 0 , VB , 0 , sizeof(Vertex) );
- Device->SetIndices(IB);
- Device->SetFVF(D3DFVF);
- Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
- Device->EndScene();
- Device->Present(NULL,NULL,NULL,NULL);
- if (KEYDOWN(VK_ESCAPE))
- {
- ESC = true;
- SendMessage(mainWindowHandle,WM_CLOSE,0,0);
- }
- return 1;
- }
- int GameShutdown(void *parms = NULL, int num_parms = 0)
- {
- if( NULL != IB)
- {
- IB->Release();
- IB = NULL;
- }
- if( NULL != VB)
- {
- VB->Release();
- VB = NULL;
- }
- if( NULL != d3d9)
- {
- d3d9->Release();
- d3d9 = NULL;
- }
- if( NULL != Device)
- {
- Device->Release();
- Device = NULL;
- }
- return 1;
- }
- int WINAPI WinMain(HINSTANCE hInstance,
- HINSTANCE prevInstance,
- PSTR cmdLine,
- int showCmd)
- {
- HWND hWnd;
- MSG msg;
- WNDCLASS wndClass;
- wndClass.style = CS_HREDRAW | CS_VREDRAW;
- wndClass.lpfnWndProc = (WNDPROC)WndProc;
- wndClass.cbClsExtra = NULL;
- wndClass.cbWndExtra = NULL;
- wndClass.hInstance = hInstance;
- wndClass.hIcon = LoadIcon(0, IDI_APPLICATION);
- wndClass.hCursor = LoadCursor(0, IDC_ARROW);
- wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- wndClass.lpszMenuName = NULL;
- wndClass.lpszClassName = WINDOWCLASS;
- if( !RegisterClass(&wndClass) )
- {
- ::MessageBox(NULL, TEXT("RegisterClass() - FAILED"), NULL, NULL);
- return false;
- }
- hWnd = CreateWindow(WINDOWCLASS,
- WINDOWNAME,
- WS_OVERLAPPEDWINDOW | WS_VISIBLE,
- 0, 0,
- Width,
- Height,
- NULL,
- NULL,
- hInstance,
- NULL);
- if( !hWnd )
- {
- ::MessageBox(NULL,TEXT( "CreateWindow() - FAILED"), NULL, NULL);
- return false;
- }
- hinstanceApp = hInstance;
- mainWindowHandle = hWnd;
- ::ShowWindow(hWnd, SW_SHOW);
- ::UpdateWindow(hWnd);
- //1: 獲得一個IDirect3D9接口指針。
- d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
- /*Direct3DCreate9 的唯一一個參數總是D3D_SDK_VERSION,
- 這可以保證應用程式通過正确的頭檔案被生成。如果函數調用失敗,
- 那麼它将傳回一個空指針。*/
- if( !d3d9 )
- {
- ::MessageBox(NULL, TEXT("Direct3DCreate9() - FAILED"), NULL, NULL);
- return false;
- }
- //2:檢查裝置能力(D3DCAPS9),搞清楚主顯示卡是否支援硬體頂點處理。.
- //當我們建立一個IDirect3DDevice9對象來表示主顯示裝置時,必須要設定其頂點處理的類型。
- D3DCAPS9 caps;
- d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);// 填充主顯示裝置的能力(D3DCAPS9結構)
- int vp = 0;
- if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
- vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
- else
- vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
- //3: 初始化一個D3DPRESENT _PARAMETERS結構執行個體。
- D3DPRESENT_PARAMETERS d3dpp;
- d3dpp.BackBufferWidth = Width;
- d3dpp.BackBufferHeight = 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 = D3DPRESENT_RATE_DEFAULT;
- d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
- //4:建立一個基于已經初始化好的D3DPRESENT _PARAMETERS結構的IDirect3DDevice9對象。
- hr = d3d9->CreateDevice(//Creates a device to represent the display adapter.
- D3DADAPTER_DEFAULT, // primary adapter
- D3DDEVTYPE_HAL, // device type
- hWnd, // window associated with device
- vp, // vertex processing
- &d3dpp, // present parameters
- &Device); // return created device
- if( FAILED(hr) )
- {
- d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
- hr = d3d9->CreateDevice(
- D3DADAPTER_DEFAULT,
- D3DDEVTYPE_HAL,
- hWnd,
- vp,
- &d3dpp,
- &Device);
- if( FAILED(hr) )
- {
- d3d9->Release(); //釋放d3d9
- ::MessageBox(NULL, TEXT("CreateDevice() - FAILED"), NULL, NULL);
- return false;
- }
- }
- GameInit();
- while(TRUE)
- {
- if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
- {
- if (msg.message == WM_QUIT)
- break;
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- GameMain();
- }
- GameShutdown();
- return 0;
- }