Jump to content
  • Advertisement
Sign in to follow this  
whather

Get arbitrary vertex format from ID3DXMesh

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

Hey guys, I have a mesh object and I'm trying to lock the vertex buffer to get the triangles into memory to perform collision detection. I don't have a predefined struct or anything which has the vertex format of the mesh. What is the best way to lock the vertex buffer and get the vertex positions. I tried to use D3DVERTEX but it isn't recognized in my project (Do I need a special include file for this)? void* verts_data; LPDIRECT3DVERTEXBUFFER9 vb = m_pMesh->GetVertexBuffer(); if( SUCCEEDED( vb->Lock(0, 0, &verts_data, D3DLOCK_READONLY) ) ) { // How do I get the vertex positions !?? ____________* verts = (____________*)verts_data; ... .... } Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Ok, what you want to do is something like this:

(1) Get the vertex declaration using ID3DXMesh::GetDeclaration(). This will give you the offsets into the stream at which vertex element occurs. Ie position elements occur at offset 0, texcoords at offset 12, ect.

(2) Finding a particular element now is just a case of simple pointer arithmetic:


uint positionOffset; // Offset of the position element, from the vertex declaration
void* verts_data;
LPDIRECT3DVERTEXBUFFER9 vb = m_pMesh->GetVertexBuffer();
if( SUCCEEDED( vb->Lock(0, 0, &verts_data, D3DLOCK_READONLY) ) )
{
// Cast to char* so we can perform addition on it, in 1-byte increments
char* dataPtr = verts_data;

// Increase pointer to the start of the position stream
dataPtr += positionOffset;

// Now just cast to a vector3, and this is your position
D3DXVECTOR3* position = (D3DXVECTOR3*)dataPtr;
}

Share this post


Link to post
Share on other sites
GetNumBytesPerVertex() returns the size of a mesh vertex. The position is likely to be the first element of each vertex, though as Dustin suggested you could go over the vertex declaration and find the offset of the D3DDECLUSAGE_POSITION[0] element.

I'd suggest making a copy of the mesh's positions so that you won't have to lock the VB at runtime for your collision detection. Actually, I'd suggest using something simpler than the rendered mesh for collision detection, but that's another subject.

Edit: by the way
Quote:
Original post by whather
I tried to use D3DVERTEX but it isn't recognized in my project (Do I need a special include file for this)?

I guess you mean D3DVECTOR. Next time you write something and it's not recognised, try to do a doc search. That'd show you that it's not in the docs, and that you must have mis-remembered.

Share this post


Link to post
Share on other sites
Quote:
Original post by ET3D
Actually, I'd suggest using something simpler than the rendered mesh for collision detection, but that's another subject.

Yea, you really should be doing some other tests instead. Per-triangle collision detection is a little...hardcore, to say the least. To get you started, you can always find the bounding box of your mesh, and then test for intersection against that. D3DX has some functions to do that:

D3DXComputeBoundingBox will give you the maximum and minimum corner points of the bounding box.

D3DXBoxBoundProbe will test that bounding box for intersection with the given ray.

That should get you started quickly, and with a large performance increase, too. Note for more complex algorithms, you can check this great page.

Share this post


Link to post
Share on other sites
Yeah, I'm actually temporarily using the .X file format for my map information until I create my own file format, because there are many .X file exporters out there for modeling programs. I then create a BSP tree out of all the triangles in the mesh and perform collision that way.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!