Jump to content
  • Advertisement
Sign in to follow this  
Asesh

Accessing vertex data of x files

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

Actually am trying to save the vertices, normals and texture coordinates of all the vertices in a mesh but the code below will only save the vertices, normals and texture coordinates of some of the vertices, not all. For eg, if there are 744 vertices in a mesh, an exception occurs after processing 461 vertices that's because of invalid memory access. This is the data type of this mesh:


// Vertex element description for new cloned mesh
D3DVERTEXELEMENT9 oD3DVertexElement[] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, // Position
{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0}, // Normal
{0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, // Texture
D3DDECL_END()
};

Code to iterate through all the vertices and store the vertex and normal data in vector containers


DWORD dwNumOfMeshVertices = m_pD3DXSceneMesh->GetNumVertices(); // Number of vertices in the mesh
DWORD dwStride = m_pD3DXSceneMesh->GetNumBytesPerVertex(); // Stride per-vertex

// Lock the vertex buffer and obtain a pointer to the vertex data
if(SUCCEEDED((m_pD3DXSceneMesh->LockVertexBuffer(D3DLOCK_READONLY, reinterpret_cast<void **>(&pdwVertexData)))))
{
// Loop through all the vertices and separate the vertices and normals
for(DWORD dwVertex = 0; dwVertex < dwNumOfMeshVertices; dwVertex++)
{
const D3DXVECTOR3 *pD3DXVertex = reinterpret_cast<const D3DXVECTOR3 *>(pdwVertexData); // Vertex
const D3DXVECTOR3 *pD3DXNormal = reinterpret_cast<const D3DXVECTOR3 *>(pdwVertexData + sizeof(D3DXVECTOR3)); // Normal
const D3DXVECTOR2 *pD3DXTexture = reinterpret_cast<const D3DXVECTOR2 *>(pdwVertexData + sizeof(D3DXVECTOR3) + sizeof(D3DXVECTOR3)); // Texture

m_oMeshVertexPositionVector.push_back(*pD3DXVertex); // Save the vertex position
m_oMeshNormalsVector.push_back(*pD3DXNormal); // Save the normal vector

pdwVertexData += dwStride; // Next vertex data
}

m_pD3DXSceneMesh->UnlockVertexBuffer();
}

The vertex stride is 32 bytes so it seems like it's ok. What's wrong with the code? Thanks

Share this post


Link to post
Share on other sites
Advertisement
ok, solved it. I changed the data type of [color="#1C2837"]pdwVertexData which was a DWORD * to BYTE * and now it's working. But can someone explain to me the reasons for using 8-bit offset to calculate vertex offset rather than using 32-bit offset?

Share this post


Link to post
Share on other sites

ok, solved it. I changed the data type of [color="#1C2837"]pdwVertexData which was a DWORD * to BYTE * and now it's working. But can someone explain to me the reasons for using 8-bit offset to calculate vertex offset rather than using 32-bit offset?

You are misunderstanding how pointers are working in C. When you have this code:
[source]
DWORD* pData = ....;
pData++;
[/source]
The ++ on pData is strictly equivalent to:
[source]
pData = (DWORD*)(((BYTE*)pData) + sizeof(DWORD));
[/source]
BUT is not equivalent to : pData = (DWORD*)(((BYTE*)pData) + 1); as you were expecting.

So in your code, when you are performing additions like: pdwVertexData + sizeof(D3DXVECTOR3) on a DWORD* pdwVertexData, you are adding in fact sizeof(D3DXVECTOR3) * sizeof(DWORD) bytes(!) to the pdwVertexData pointer.
When you changed pdwVertexData to BYTE*, everything was correctly calculated according to the size of the underlying value of the pointer (sizeof(BYTE))

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!