Jump to content
  • Advertisement
Sign in to follow this  
fs1

Parsing Vertex Buffer to find Vertices Coordinates (x, y, z)

This topic is 2137 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 want to retrieve all vertices from an instanced and loaded IDirect3DIndexBuffer9 and IDirect3DVertexBuffer9 buffers.

 

How can I do this?

 

Thanks,

 

fs1

Share this post


Link to post
Share on other sites
Advertisement

No one took on this?

 

I want to see if I can loop through the IDirect3DIndexBuffer9, to find the Vertices information (x,y,z).

 

Can anyone help me?

Edited by fs1

Share this post


Link to post
Share on other sites

You need to know format of vertices it holds, then:

struct Vertex
{
    D3DXVECTOR3 pos;
    ... // whatever else you have
};

D3DVERTEXBUFFER_DESC desc;
buffer->GetDesc(&desc);
DWORD numVerts = desc.Size / sizeof(Vertex); // or however you obtain number of verts

Vertex* pVert;
buffer->Lock(0, 0, (void**)&pVert, lockFlags);

for(DWORD i = 0; i < numVerts; ++i)
{
    D3DXVECTOR currentPos = pVert[i].pos;
}

buffer->Unlock();

 
 

Share this post


Link to post
Share on other sites
Without knowing the vertex stream declaration you're pretty much out of luck. The vertex buffer is just a chunk of memory, nothing more. You need additional information (ie the vertex stream declaration or some a priori knowledge of the buffer layout) to reliably parse any information out of it.

If you know the vertex declaration, then parsing the positions should be trivially easy: they're at the offsets (vertexStride * n + posOffset) where posOffset is the offset indicated by the vertex element with D3DDECLUSAGE_POSITION and n is the vertex index. The format of the position can be gotten from the Type field of the vertex element.

Share this post


Link to post
Share on other sites

Locking and unlocking vertex/index buffers causes stalls and very poor performance.  We don’t parse vertex buffers/index buffers for any but the most serious issues.

If you need to know which vertices are at a given point, keep a CPU-side vertex array and optional index array and parse that/those.

 

 

L. Spiro

Share this post


Link to post
Share on other sites

 

You need to know format of vertices it holds, then:

struct Vertex
{
    D3DXVECTOR3 pos;
    ... // whatever else you have
};

D3DVERTEXBUFFER_DESC desc;
buffer->GetDesc(&desc);
DWORD numVerts = desc.Size / sizeof(Vertex); // or however you obtain number of verts

Vertex* pVert;
buffer->Lock(0, 0, (void**)&pVert, lockFlags);

for(DWORD i = 0; i < numVerts; ++i)
{
    D3DXVECTOR currentPos = pVert[i].pos;
}

buffer->Unlock();

 
 

 

Without knowing the vertex stream declaration you're pretty much out of luck. The vertex buffer is just a chunk of memory, nothing more. You need additional information (ie the vertex stream declaration or some a priori knowledge of the buffer layout) to reliably parse any information out of it.

If you know the vertex declaration, then parsing the positions should be trivially easy: they're at the offsets (vertexStride * n + posOffset) where posOffset is the offset indicated by the vertex element with D3DDECLUSAGE_POSITION and n is the vertex index. The format of the position can be gotten from the Type field of the vertex element.

 

Samith, belfegor thank you so much, I understand the logic, how about when you have an Index Buffer? The idea is quite the same?

 

Locking and unlocking vertex/index buffers causes stalls and very poor performance.

 

 

L. Spiro

Thanks, could you be more specific on this issue? Appreciate it.

Edited by fs1

Share this post


Link to post
Share on other sites

Samith, belfegor thank you so much, I understand the logic, how about when you have an Index Buffer? The idea is quite the same?


The index buffer only tells the primitive assembler on the GPU which vertices to use and in which order, essentially. It doesn't say anything about the internal layout of the vertex buffer. So unfortunately the index buffer can't help you find the position values inside of a vertex buffer.

Share this post


Link to post
Share on other sites

Thanks, could you be more specific on this issue? Appreciate it.

When you are creating the vertex buffer, keep a second copy of the vertex positions in your own CPU array (std::vector for example) and do the same with the index buffer.


L. Spiro

Share this post


Link to post
Share on other sites

 

Thanks, could you be more specific on this issue? Appreciate it.

When you are creating the vertex buffer, keep a second copy of the vertex positions in your own CPU array (std::vector for example) and do the same with the index buffer.


L. Spiro

 

Thanks, I appreciate it.

 

Just one last quick question, let's say you don't know the VERTEX format, but you know it's size (Stride). In the following code:

struct Vertex
{
    D3DXVECTOR3 pos;
    ... // whatever else you have
};

D3DVERTEXBUFFER_DESC desc;
buffer->GetDesc(&desc);
DWORD numVerts = desc.Size / sizeof(Vertex); // or however you obtain number of verts

Vertex* pVert;
buffer->Lock(0, 0, (void**)&pVert, lockFlags);

for(DWORD i = 0; i < numVerts; ++i)
{
    D3DXVECTOR currentPos = pVert[i].pos;
}

buffer->Unlock();

Can you always assume that the coordinates of each vertex are the 3 floats starting from pVert ? Let's say pVert.x, pVert.y, pVert.z ?

 

Thanks.

Edited by fs1

Share this post


Link to post
Share on other sites

Usually position semantic is the first thing in the vertex and it is usually a vector of 3 floats.
You can obtain more vertex information if you are grabbing this just after vertex declaration and vertex buffers are set to be "active" on the device. Something like this:

...
// let say here is set
device->SetVertexDeclaration(...);
device->SetStreamSource(...);
device->Draw...();
...
// later here you want to grab data
IDirect3DVertexDeclaration9* pDecl;
device->GetVertexDeclaration(&pVDecl);
D3DVERTEXELEMENT9 decl[MAXD3DDECLLENGTH];
UINT numElements;
pVDecl->GetDeclaration( decl, &numElements);

for(UINT i = 0; i < numElements; ++i)
{
    // decl type
    auto currentType = decl[i].Type;
    
    // decl usage
    auto currentUsage = decl[i].Usage;
    ...

    // let say we want to determine when it is a position semantic
    if(D3DDECLUSAGE_POSITION == currentUsage)
        ...// do something about it
}
...

From D3DVERTEXELEMENT9  of vertex decl. you can obtain all information you need.

What are you trying to do?

Edited by belfegor

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!