Results 1 to 10 of 10

Thread: Getting face from DirectX mesh

  1. #1

    Getting face from DirectX mesh

    Hi, I'm trying to set up poly-wise collisions. I call D3DXIntersect to test for intersection, and it has a faceIndex return. However, I can't seem to figure out how to turn that faceIndex value into a working face: triangle (p1, p2, p3) or a vertex + normal.

    The vertex buffer should store vertices in winding order, so, vertices[faceIndex] should be p1, vertices[faceIndex + 1] p2 and vertices[faceIndex + 2] p3, correct? When I try doing this, I end up with garbage (-0.001327 floats, not sure what that is in hex).

    So, I'm can anyone show me the correct way to get the value of the fave at the given index from a mesh?

    Edit: Another thing, is that using the vertex buffer directly requires that you know the FVF of the mesh, which could in theory change... I'm not sure how to set it up so that you always use the correct FVF structure either.

  2. #2

    Getting face from DirectX mesh

    The number of indices = number of faces * 3, so
    vertices[faceIndex * 3] = p1
    vertices[faceIndex * 3 + 1] = p2
    vertices[faceIndex * 3 + 2] = p3

    you dont need to worry about FVF structure because position data is always (well, almost) first in all vertex structures.

  3. #3

    Getting face from DirectX mesh

    Hmmm... but I need to know the size of the vertices in order to offset their vertex buffer correctly, no? Here's the current code.

    Code:
    D3DXIntersect(testMesh, &rayOrigin, &rayDir, &hit, &faceIndex, &u, &v, &distance, &allHits, &numHits);
    
    modelVertex* vertices;
    vertices = (modelVertex*)malloc(testMesh->GetNumVertices() * sizeof(modelVertex))
    
    testMesh->LockVertexBuffer(0, (void**)vertices);
    (*intersectTri).p1 = vertices[faceIndex].position;
    (*intersectTri).p2 = vertices[faceIndex + 1].position;
    (*intersectTri).p3 = vertices[faceIndex + 2].position;
    testMesh->UnlockVertexBuffer();

  4. #4

    Getting face from DirectX mesh

    Easy
    Code:
    testMesh->GetNumBytesPerVertex();

  5. #5

    Getting face from DirectX mesh

    That still doesn't help with the offset...

    modelVertex has to be the correct size, or else when you index the vertices you'll not offset by the correct amount....

    More over, can someone provide some WORKING code? :3

  6. #6

    Getting face from DirectX mesh

    Okay... so... here's my attempt to get it working...

    Code:
    					DWORD* indices32;
    					WORD* indices16;
    
    				
    					char* vertices;
    
    					testMesh->LockVertexBuffer(0, (void**)&vertices);
    					if (testMesh->GetOptions() & D3DXMESH_32BIT)
    					{
    						testMesh->LockIndexBuffer(0, (void**)&indices32);
    						memcpy(&(*intersectTri).p1, &vertices[indices32[faceIndex * 3 + 0] * testMesh->GetNumBytesPerVertex()], sizeof(D3DXVECTOR3));
    						memcpy(&(*intersectTri).p2, &vertices[indices32[faceIndex * 3 + 1] * testMesh->GetNumBytesPerVertex()], sizeof(D3DXVECTOR3));
    						memcpy(&(*intersectTri).p3, &vertices[indices32[faceIndex * 3 + 2] * testMesh->GetNumBytesPerVertex()], sizeof(D3DXVECTOR3));
    					}
    					else
    					{
    						testMesh->LockIndexBuffer(0, (void**)&indices16);
    						memcpy(&(*intersectTri).p1, &vertices[indices16[faceIndex * 3 + 0] * testMesh->GetNumBytesPerVertex()], sizeof(D3DXVECTOR3));
    						memcpy(&(*intersectTri).p2, &vertices[indices16[faceIndex * 3 + 1] * testMesh->GetNumBytesPerVertex()], sizeof(D3DXVECTOR3));
    						memcpy(&(*intersectTri).p3, &vertices[indices16[faceIndex * 3 + 2] * testMesh->GetNumBytesPerVertex()], sizeof(D3DXVECTOR3));
    					}
    					
    					testMesh->UnlockIndexBuffer();
    					testMesh->UnlockVertexBuffer();
    I use a char* for the vertex buffer so I can pick the offset with GetNumBytesPerVertex(). Then I rely ont he fact that position comes first to do a binary copy into the D3DXVECTOR3 fields. Models can have either 16 or 32-bit index buffers, hence the WORD and DWORD split. However, the indexes returned are invalid (way outside of the vertex buffers' range).

    Edit: So.. it just returned a faceIndex that was GREATER than the number of vertices..... that shouldn't be possible, right?

    Edit: Fixed the code to the working form for reference by whoever wants it. LockIndexBuffer and LockVertexBuffer do their own memory management, so malloc causes memory leaks.

  7. #7

    Getting face from DirectX mesh

    Aaight, the code works fine. The issue was that the lock calls need to be passed pointers to the WORD pointer.

    So... &vertices, and &indices16.

    However, if anyone has a better way to do this... please let me know

  8. #8

    Getting face from DirectX mesh

    Another thing I'd like to be able to do...

    Using the same method, I'd like to find the texel of the intersection.

    Once you find the face, you can get the UV information from the vertex (a bit more annoying, but still doable). However, I'm not sure how to turn the UV information of the triangle's 3 vertices into the the texel at intersection.

    Since D3DXIntersect gives you the UV coords using those would probably be the fastest. So, I have 2 main questions.

    1) How do you convert vertex UV coords to the UV coords at a given point on a triangle (Point on triangle defined by UV coords as well)
    I assume you can just interpolate in a Cartesian coordinate system, but I'd like to try to do it in a Barycentric system, so I don't have to convert out.

    and 2) Since a vertex can have multiple UV coordinates, how is that expressed in DirectX meshes? Are vertices with different texture coords just stored as individual vertices?

  9. #9

    Getting face from DirectX mesh

    To answer your second question:

    The texture-coords are stored in a single vertex.

    Like:

    [pascal]
    TMyMultitextureVertex = record
    Pos: TVector3;
    Normal: TVector3;
    Tex1: TVector2;
    Tex2: TVector2;
    Tex3:TVector2;
    end;

    //FVF would be (correct me if I'm wrong)
    D3DFVF_XYZ or D3DFVF_NORMAL or D3DFVF_TEXCOORD3
    [/pascal]

    Hope it helps.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  10. #10

    Getting face from DirectX mesh

    Errr, sorry, my wording was bad. You can store multiple UV coords in a vertex for multiple textures, yes. But in a single UV map the same vertex can in theory have multiple coordinates. That's what I was referring to in question 2.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •