Jump to content
  • Advertisement


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


Vertex Buffer - Reading "Issue"

This topic is 5861 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

Hello, I've searched the forum and haven't found anything that resolves my problem. I'm hoping the problem/solution is obvious to someone out there. Basically I'm creating a mesh, much the same way as the simple D3D Mesh Tutorial in the SDK. Then, I transfer the vertices within the mesh to another vertex buffer, so that I can access the vertices for collision detection handling at a polygonal level. The problem is, while some vertices show up as they should, some are also incorrect. Somewhere, somehow, something happens to them, unless I'm missing something. Heres the gist of my code:
struct CUSTOMVERTEX { float x, y, z; };

// Mesh loading around here

// Get vertex count

int iVertexCount = m_pMesh->GetNumVertices();
// Create storage for vertices

CUSTOMVERTEX* pVertices = new CUSTOMVERTEX[iVertexCount];
// Create Vertex Buffer

device->CreateVertexBuffer( iVertexCount * sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &m_pVB );
// Get Vertex Buffer of Mesh (put it in VB)

m_pMesh->GetVertexBuffer( &m_pVB );
m_pVB->Lock( 0, sizeof(iVertexCount * sizeof(CUSTOMVERTEX)), (BYTE**)&pVertices, 0 );
// Now lets write the vertex positions to text file for examination

ofstream out("vertices.txt");
for(int v = 0; v < iVertexCount; v++)
   out << "Vertex# " << v << " x:" << pVertices[v].x << " y:" << pVertices[v].y << " z:" << pVertices[v].z << endl;
Fair enough, the problem is, some vertices are correct while others are not. Imagine a mesh that contains a single triangle over the origin. This is what the text result is: Vertex# 0 x:-100 y:-100 z:0 Vertex# 1 x:0 y:0 z:1 Vertex# 2 x:100 y:-100 z:0 Vertex# 3 x:0 y:0 z:1 Vertex 0 and 2 are right, but whats going on at 1 and 3? Can anyone tell me whats wrong here, or clarify more on accessing the correct vertex positions of vertices in a mesh? Much appreciated, thanks. [edited by - Elaboratus on June 6, 2002 8:45:36 AM]

Share this post

Link to post
Share on other sites
When you''re locking a vertex buffer, DX allocated memory for the vertices and points your pointer to that memory. This is why Lock accepts pointer-to-pointer: it overwrites your pointer with a new value, instead of copying the data to the pointer you allocated. Thus, you don''t allocate any memory with new and you don''t free the pointer you''re given with delete; DX takes care of that.

The locked memory is valid only between calls to Lock and Unlock. The proper sequence for retrieving vertex data is this:

Declare a pointer to CUSTOMVERTEX, don''t initialize it.
Call Lock. The pointer you created will be initialized to memory allocated by DX.
Do operations on the vertices here. You can copy them to a buffer you allocated if you want to store them permanently.
Call Unlock. This invalidates the pointer you declared in step 1.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!