Jump to content
  • Advertisement
Sign in to follow this  

Drawing picked triangle

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

For some reason i cannot render a triangle that represents the picked triangle

Not sure where i am going wrong, the picking is working fine just cant do this simple task...


void detect_picking()
    // get the current transform matrices
    D3DXMATRIX matProjection, matView, matWorld, matInverse;
    d3ddev->GetTransform(D3DTS_PROJECTION, &matProjection);
    d3ddev->GetTransform(D3DTS_VIEW, &matView);
    d3ddev->GetTransform(D3DTS_WORLD, &matWorld);
    // use the mouse coordinates to get the mouse angle
    float xAngle = (((2.0f * MousePos.x) / SCREEN_WIDTH) - 1.0f) / matProjection(0, 0);
    float yAngle = (((-2.0f * MousePos.y) / SCREEN_HEIGHT) + 1.0f) / matProjection(1, 1);
    D3DXVECTOR3 origin, direction;
    origin = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
    direction = D3DXVECTOR3(xAngle, yAngle, 1.0f);
    // find the inverse matrix
    D3DXMatrixInverse(&matInverse, NULL, &(matWorld * matView));
    // convert origin and direction into model space
    D3DXVec3TransformCoord(&origin, &origin, &matInverse);
    D3DXVec3TransformNormal(&direction, &direction, &matInverse);
    D3DXVec3Normalize(&direction, &direction);
    // detect picking
    BOOL hit;
DWORD faceIndex;
    D3DXIntersect(meshTeapot, &origin, &direction, &hit, &faceIndex, NULL, NULL, NULL, NULL, NULL);
   // Yes, draw the picked triangle in solid mode.
IDirect3DVertexBuffer9* vb = 0;
IDirect3DIndexBuffer9* ib = 0;
d3ddev->SetStreamSource(0, vb, 0, sizeof(IDirect3DVertexBuffer9));
// faceIndex identifies the picked triangle to draw.
D3DPT_TRIANGLELIST, 0, 0, meshTeapot->GetNumVertices(), faceIndex*3, 1);

Share this post

Link to post
Share on other sites

What doesn't work? What are you seeing?


Looking at the docs (you can do that, too!), the last SetStreamSource argument is Stride, i.e., the stride of a vertex in the buffer. You have sizeof a structure.


The stride for the vertex will be related to the vertex declaration (or you can derive it from the FVF). Also, look at the docs for ID3DXBaseMesh::GetNumBytesPerVertex()


EDIT: Also, you're assuming that the vertices are in index order in the vertex buffer. That may or may not be true. I.e., the 3 vertices forming the hit face are determined by the values in the index buffer for that face, and may/may not be sequential in the vertex buffer. E.g., the hit face may be form by vertex numbers 40, 63, 21.


EDIT2: In addition, to use DrawIndexedPrimitive you have to set an index buffer. It's been a while but I think in DX9, that's SetIndices.


You can google for "drawindexedprimitive example"

Edited by Buckeye

Share this post

Link to post
Share on other sites

Ive done  what you said,but its still not showing the triangle,i think its something todo with d3ddev->SetVertexDeclaration();

I am unsure how to retrieve the declaration from the mesh and then set device->SetVertexDeclaration for the retrieved value

Share this post

Link to post
Share on other sites
How about ID3DXBaseMesh::GetDeclaration.

Also, if you're rendering the triangle the same way like you draw the mesh, it won't make a difference visually. You need to play with different render states (say, disable lighting/texturing/depth and/or give a fixed color).

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!