Advertisement
Guest User

Index and Vertex Buffer Issue

a guest
Dec 7th, 2013
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. /* Code to create the buffers and etc; Buffers can only hold 3 Quads of data before being "full" */
  2.  
  3. //Map flag at start
  4. mappingFlag = D3D11_MAP_WRITE_NO_OVERWRITE;
  5.  
  6. void SystemBatcher::endBatch()
  7. {
  8. //Lock the buffers for mapping
  9. batchContext->Map(vertexBuffer, 0, mappingFlag, 0, &mapVertexResource);
  10. batchContext->Map(indexBuffer, 0, mappingFlag, 0, &mapIndexResource);
  11.  
  12. //For all the quads that need to be drawn place them in the buffer
  13. for(std::vector<Quad>::iterator i = drawData.begin(); i != drawData.end(); i++)
  14. {
  15. //If the buffer is full
  16. if(vertexBufferSize - drawDataAmount == 0)
  17. {
  18. //State the buffer is full
  19. std::cout<<"Vertex Buffer is full"<<std::endl;
  20.  
  21. //Unlock the buffer
  22. batchContext->Unmap(indexBuffer, 0);
  23. batchContext->Unmap(vertexBuffer, 0);
  24.  
  25. //Draw the quad data
  26. //indexDrawCount: number of indices to draw
  27. //indexOffset: The start location to use in the index buffer
  28. //drawnVertexCount: The start location to use in the Vertex Buffer
  29. batchContext->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  30. batchContext->DrawIndexed(indexDrawCount, indexOffset, drawnVertexCount);
  31.  
  32. //Change the flag to discard since we are fill; Reset the buffer positions
  33. //Reset the other counts
  34. mappingFlag = D3D11_MAP_WRITE_DISCARD;
  35. vertexBufferPosition = 0;
  36. indexBufferPosition = 0;
  37. indexDrawCount = 0;
  38. drawDataAmount = 0;
  39.  
  40. //Reset the start location
  41. drawnVertexCount = 0;
  42. indexOffset = 0;
  43.  
  44. //Re lock the buffers with the DISCARD Flag; Change th flag back to NO OVERWRITE
  45. batchContext->Map(vertexBuffer, 0, mappingFlag, 0, &mapVertexResource);
  46. batchContext->Map(indexBuffer, 0, mappingFlag, 0, &mapIndexResource);
  47. mappingFlag = D3D11_MAP_WRITE_NO_OVERWRITE;
  48. }
  49.  
  50. //Copy the vertex buffer data in from the Quad
  51. memcpy((Vertex*)mapVertexResource.pData + vertexBufferPosition, (*i).vertices, sizeof((*i).vertices));
  52.  
  53. //Calculate the index positions based on the vertex buffer
  54. ((SHORT*)mapIndexResource.pData)[indexBufferPosition] = vertexBufferPosition;
  55. ((SHORT*)mapIndexResource.pData)[indexBufferPosition + 1] = vertexBufferPosition + 1;
  56. ((SHORT*)mapIndexResource.pData)[indexBufferPosition + 2] = vertexBufferPosition + 2;
  57. ((SHORT*)mapIndexResource.pData)[indexBufferPosition + 3] = vertexBufferPosition + 3;
  58. ((SHORT*)mapIndexResource.pData)[indexBufferPosition + 4] = vertexBufferPosition;
  59. ((SHORT*)mapIndexResource.pData)[indexBufferPosition + 5] = vertexBufferPosition + 2;
  60.  
  61. //Add on to the buffer positions
  62. vertexBufferPosition += 4;
  63. indexBufferPosition += 6;
  64.  
  65. //Add onto the drawDataAmount we have (Used when calculating if the buffer is full);
  66. //Add onto the index count used when drawning
  67. drawDataAmount += 4 * vertexSize;
  68. indexDrawCount += 6;
  69. }
  70.  
  71. //Unlock the buffers
  72. batchContext->Unmap(indexBuffer, 0);
  73. batchContext->Unmap(vertexBuffer, 0);
  74.  
  75. //Draw the quad data
  76. //indexDrawCount: number of indices to draw
  77. //indexOffset: The start location to use in the index buffer
  78. //drawnVertexCount: The start location to use in the Vertex Buffer
  79. batchContext->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  80. batchContext->DrawIndexed(indexDrawCount, indexOffset, drawnVertexCount);
  81.  
  82. //Add onto the offsets used in the DrawIndexed calls
  83. drawnVertexCount += vertexBufferPosition;
  84. indexOffset += indexDrawCount;
  85. indexDrawCount = 0;
  86.  
  87. //Clear out all of the drawData that was in the drawData vector
  88. drawData.clear();
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement