• Advertisement


This topic is now archived and is closed to further replies.

A better way to add data to a vb

This topic is 6045 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to add vertices to a d3d8 vertexbuffer. It´s for a dungeon to my editor. The dungeon grow every time the user add a new square. This is the way I do it right now:

bool CMesh::Create(LPDIRECT3DDEVICE8 device, CUSTOMVERTEX *pVertices, WORD wSize)
  if(wSize < 3) return false; 
  if(pVertices == NULL) return false;

  m_wNumFaces = wSize/3;

  if(FAILED(device->CreateVertexBuffer( wSize*sizeof(CUSTOMVERTEX),
                                      0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, 
                                      &m_lpVertexBuffer ))) return false;
  // Fill vb

  if(FAILED(m_lpVertexBuffer->Lock( 0, 0, (BYTE**)&pVBVertices, 0 ))) return false;
  memcpy(pVBVertices, pVertices, sizeof(CUSTOMVERTEX)*wSize);

  return true;

bool CMesh::AddVertices(LPDIRECT3DDEVICE8 device, CUSTOMVERTEX *pNewVertices, WORD wSize)
  WORD wNumVerticesOldVB = m_wNumFaces*3;
  WORD wNumVerticesNewVB = wNumVerticesOldVB+wSize;


  // Copy old data in vb to a new dynamic vertex list array 

  if(FAILED(m_lpVertexBuffer->Lock( 0, 0, (BYTE**)&pOldVL, D3DLOCK_READONLY))) exit(0); 
  memcpy(pNewVL, pOldVL, sizeof(CUSTOMVERTEX)*wNumVerticesOldVB);

  // Add new data in pNewVL from CUSTOMVERTEX pointer ''pNewVertices''

  memcpy(pNewVL+wNumVerticesOldVB, pNewVertices, sizeof(CUSTOMVERTEX)*wSize);

  // Destroy old vb


  // Create a new one

  Create(device, pNewVL, wNumVerticesNewVB);

  delete[] pNewVL;

  return true;

I wonder if there is a better way to do this. Maybe I don´t need to destroy my vertex buffer every time? Zeblar Nagrim, Lord of Chaos

Share this post

Link to post
Share on other sites

  • Advertisement