第八章索引緩沖_第1頁
第八章索引緩沖_第2頁
第八章索引緩沖_第3頁
第八章索引緩沖_第4頁
第八章索引緩沖_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、第八章:索引緩沖Introduction (序)這一章我們將學(xué)習(xí)一下關(guān)于索引緩沖的內(nèi)容。首先,我們要改進一下CCuboid使它能支持索引緩沖。然后我們會創(chuàng)建一個類CTerrain,一個很簡單的地形發(fā)生器。當(dāng)我們的地形生成后,我們還會賦予它草的紋理,使它看起來更真實。What is an Index Buffer? (什么是索引緩沖)索引緩沖就是內(nèi)存中的一塊用于索引頂點的區(qū)域。不明白?接著看。當(dāng)渲染場景時,DX會對每個頂點都進行許多計算,例如燈光、變幻等等,這樣,運算量會很大。但是我們總是希望讓DX做最少的運算以增加程序的效率,因此,我們需要把頂點的數(shù)目減到最少。這樣,索引緩沖就派上用場了。舉個

2、例子能更好地理解:假如我們想要繪制一個正方形,那末它需要由兩個三角形來構(gòu)成,總共6個頂點(用三角列)。但實際上4頂點就應(yīng)當(dāng)能確定一個正方形了(每角一個),用6個頂點是因為其中有2個頂點被重合(相同的值)了。因為我們有頂點重合了,所以用索引緩沖是一個好主意。它是這樣工作的:首先我們在頂點緩沖中只定義4個頂點(每角一個),然后在索引緩沖中定義6個索引,每個索引都會映射頂點緩沖中的一個頂點。然后我們用索引緩沖中的索引來渲染三角形,那末實際上我們只用了4個頂點。下圖(8.1)演示了所舉的這個例子:Fig 8.1在上面的例子中,我們只用了4個頂點就定義了一個正方形,實際節(jié)約了2個頂點。注意索引緩沖中的順

3、序是順時針的,這是頂點被渲染的順序。然后我們會像以前那樣渲染場景,只不過這次除了頂點緩沖之外我們還用了索引緩沖。那末如果我們把這種方法應(yīng)用到我們的立方體上面,我們就會節(jié)約12個頂點。不要說那并不多,想象一個有100個立方體的場景,我們在每一幀上都會節(jié)約1200個頂點呢!Implementing an Index Buffer in DirectX (在DX中實現(xiàn)索引緩沖)我們需要給CCuboid添加一個索引緩沖,要實現(xiàn)這個,我們需要添加如下代碼:Step 1: Creating the Index Buffer (創(chuàng)建索引緩沖)首先,我們需要兩個新的成員變量:一個LPDIRECT3DINDEX

4、BUFFER8型的m_pIndexBuffer,索引緩沖指針;一個DWORD變量m_dwNumOfIndices,對象(頂點)索引數(shù)目,在此為36(6個面X每面2個三角形X每個三角形3個頂點),構(gòu)造函數(shù)會這樣設(shè)置。我們還有一個新的模塊CreateIndexBuffer,在構(gòu)造函數(shù)中,我們會在設(shè)置頂點值之前調(diào)用它。使用索引緩沖的方法與使用頂點緩沖是類似的:首先我們創(chuàng)建索引緩沖并且得到它的指針(m_pIndexBuffer),然后我們臨時地把36個頂點索引都保存到一個數(shù)組當(dāng)中,然后鎖定索引緩沖,最后把數(shù)組復(fù)制給它并解鎖。LPDIRECT3DINDEXBUFFER8 m_pIndexBuffer;D

5、WORD m_dwNumOfIndices; bool CCuboid:CreateIndexBuffer() VOID* pBufferIndices; /Create the index buffer from our device if(FAILED(m_pD3DDevice->CreateIndexBuffer(m_dwNumOfIndices * sizeof(WORD), 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_pIndexBuffer) return false; /Set values for the index buffer

6、 WORD pIndices = 0, 1, 2, 3, 2, 1, /Top 4, 5, 6, 7, 6, 5, /Face 1 8, 9,10,11,10, 9, /Face 2 12,13,14,15,14,13, /Face 3 16,17,18,19,18,17, /Face 4 20,21,22,23,22,21; /Bottom /Get a pointer to the index buffer indices and lock the index buffer m_pIndexBuffer->Lock(0, m_dwNumOfIndices * sizeof(WORD)

7、, (BYTE*)&pBufferIndices, 0); /Copy our stored indices values into the index buffer memcpy(pBufferIndices, pIndices, m_dwNumOfIndices * sizeof(WORD); /Unlock the index buffer m_pIndexBuffer->Unlock(); return true; Step 2: Modify the Vertex Buffer (修改頂點緩沖)我們需要修改一下UpdateVertices模塊。首先也是最主要的就是這回我

8、們只需要24個頂點,而不是36個。然后我們會像上一章那樣初始化法線向量為0。此章的另一點不同就是我們將計算出三角形的共享頂點的均分法線(上一章我們只是把三角形的法線簡單地相加,而沒有均分),實際上我們并不真的需要對這個立方體這樣做,目的只是想充當(dāng)一個例子。在UpdateVertices中我們增添了兩個新的指針pNumOfSharedPolygons和pSumVertexNormal,它們分別指向兩個數(shù)組:一個WORD型的用于描述所有頂點周圍的三角形數(shù)目(也就是幾個三角形共享那個頂點);另一個D3DVECTOR型的用于保存所有頂點的均分法線向量。接下來我們將計算出每個頂點的均分法線向量。在Upd

9、ateVertices中當(dāng)我們初始化所有頂點之后,我們會有一個循環(huán),索引緩沖中的所有的頂點索引都會通過它。循環(huán)將計算出每個三角形的法線、累加被引用的頂點的計數(shù)(保存在pNumOfSharedPolygons中,被引用的次數(shù)也就是周圍的三角形數(shù)),還把得出的三角形法線向量加到它周圍的三個頂點的法線向量上(由pSumVertexNormal保存,這樣整個循環(huán)結(jié)束后就能得出所有頂點的法線向量了,只不過還沒有均分)。然后,我們會根據(jù)每個頂點的引用計數(shù)(也就是幾個三角形共享那個頂點)來計算出所有頂點的均分法線向量,接著我們會將它規(guī)格化,確保向量的x、y和z都能在0和1之間。最后,我們還得把得出的每個頂點

10、均分法線向量應(yīng)用到對應(yīng)的頂點上,然后像以往一樣復(fù)制到頂點緩沖中。bool CCuboid:UpdateVertices() DWORD i; VOID* pVertices; WORD* pBufferIndices; D3DXVECTOR3 vNormal; DWORD dwVertex1; DWORD dwVertex2; DWORD dwVertex3; /Array holds how many times this vertex is shared WORD* pNumOfSharedPolygons = new WORDm_dwNumOfVertices; /Array holds

11、 sum of all face normals for shared vertex D3DVECTOR* pSumVertexNormal = new D3DVECTORm_dwNumOfVertices; /Clear memory for(i = 0; i < m_dwNumOfVertices; i+) pNumOfSharedPolygonsi = 0; pSumVertexNormali = D3DXVECTOR3(0,0,0); CUBOID_CUSTOMVERTEX cvVertices = /Top Face m_rX - (m_rWidth / 2), m_rY +

12、(m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, /Vertex 0 m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, /Vertex 1 m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, /Vertex 2

13、m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, /Vertex 3 /Face 1 m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, /Vertex 4 m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.

14、0f, 0.0f, 0.0f, 0.0f, 0.0f, /Vertex 5 m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, /Vertex 6 m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, /Vertex 7 /Face 2 m_rX + (m_rWidth / 2), m_rY - (m_r

15、Height / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, /Vertex 8 m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, /Vertex 9 m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, /Vertex 10 m_r

16、X + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, /Vertex 11 /Face 3 m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, /Vertex 12 m_rX + (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0

17、f, 0.0f, 0.0f, 0.0f, 0.0f, /Vertex 13 m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, /Vertex 14 m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, /Vertex 15 /Face 4 m_rX - (m_rWidth / 2), m_rY - (m

18、_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, /Vertex 16 m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, /Vertex 17 m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, /Vertex 18

19、 m_rX - (m_rWidth / 2), m_rY + (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, /Vertex 19 /Bottom Face m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, /Vertex 20 m_rX + (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth

20、/ 2), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, /Vertex 21 m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ - (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, /Vertex 22 m_rX - (m_rWidth / 2), m_rY - (m_rHeight / 2), m_rZ + (m_rDepth / 2), 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, /Vertex 23 ; /Get a pointer to the index b

21、uffer indices and lock the index buffer m_pIndexBuffer->Lock(0, m_dwNumOfIndices * sizeof(WORD), (BYTE*)&pBufferIndices, D3DLOCK_READONLY); /For each triangle, count the number of times each vertex is used and /add together the normals of faces that share a vertex for(i = 0; i < m_dwNumOfI

22、ndices; i += 3) dwVertex1 = pBufferIndicesi; dwVertex2 = pBufferIndicesi + 1; dwVertex3 = pBufferIndicesi + 2; vNormal = GetTriangeNormal(&D3DXVECTOR3(cvVerticesdwVertex1.x, cvVerticesdwVertex1.y, cvVerticesdwVertex1.z), &D3DXVECTOR3(cvVerticesdwVertex2.x, cvVerticesdwVertex2.y, cvVerticesdw

23、Vertex2.z), &D3DXVECTOR3(cvVerticesdwVertex3.x, cvVerticesdwVertex3.y, cvVerticesdwVertex3.z); pNumOfSharedPolygonsdwVertex1+; pNumOfSharedPolygonsdwVertex2+; pNumOfSharedPolygonsdwVertex3+; pSumVertexNormaldwVertex1.x += vNormal.x; pSumVertexNormaldwVertex1.y += vNormal.y; pSumVertexNormaldwVer

24、tex1.z += vNormal.z; pSumVertexNormaldwVertex2.x += vNormal.x; pSumVertexNormaldwVertex2.y += vNormal.y; pSumVertexNormaldwVertex2.z += vNormal.z; pSumVertexNormaldwVertex3.x += vNormal.x; pSumVertexNormaldwVertex3.y += vNormal.y; pSumVertexNormaldwVertex3.z += vNormal.z; /Unlock the index buffer m_

25、pIndexBuffer->Unlock(); /For each vertex, calculate and set the average normal for(i = 0; i < m_dwNumOfVertices; i+) vNormal.x = pSumVertexNormali.x / pNumOfSharedPolygonsi; vNormal.y = pSumVertexNormali.y / pNumOfSharedPolygonsi; vNormal.z = pSumVertexNormali.z / pNumOfSharedPolygonsi; D3DXVe

26、c3Normalize(&vNormal, &vNormal); cvVerticesi.nx = vNormal.x; cvVerticesi.ny = vNormal.y; cvVerticesi.nz = vNormal.z; /Get a pointer to the vertex buffer vertices and lock the vertex buffer if(FAILED(m_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE*)&pVertices, 0) return false; /Copy

27、 our stored vertices values into the vertex buffer memcpy(pVertices, cvVertices, sizeof(cvVertices); /Unlock the vertex buffer m_pVertexBuffer->Unlock(); /Clean up delete pNumOfSharedPolygons; delete pSumVertexNormal; pNumOfSharedPolygons = NULL; pSumVertexNormal = NULL; return true;Step 3: Render (渲染)Render模塊也相應(yīng)地做了改動:首先,我們要通知DX我們要用索引緩沖來渲染三角形,所以我們得調(diào)用SetIndices并且把索引緩沖的指針給它;其次,我們要用DrawIndexedPrimitive代替原來的DrawPrimitive以支持用索引緩沖渲染三角形。/Select index bufferm_pD3DDevice->SetIndices(m_pIndexBuffer, 0);/Render polygon

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論