Jump to content
  • Advertisement
Sign in to follow this  
Anddos

Drawing picked triangles of a mesh

This topic is 3024 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 am trying to draw the picked triangles on a mesh but debugging is stopping on these lines with a crash... d3ddev->SetVertexDeclaration(Decl); d3ddev->SetStreamSource(0, vb, 0, sizeof(Decl)); does anyone know how to fix it?
void detect_picking(LPD3DXMESH Mesh,DWORD &gfaceIndex,LPDIRECT3DDEVICE9 d3ddev)
{
    // 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
	POINT MousePos;
    GetCursorPos(&MousePos);
	//SetCursorPos(800/2,600/2);
	//ScreenToClient(hWnd,&MousePos);
	//SetWindowText(hWnd,((LPCWSTR)MousePos.x));
    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;
	char Buffer[64];
	float Dist;
	DWORD faceIndex;
	D3DXIntersect(Mesh, &origin, &direction, &hit, &faceIndex, NULL, NULL, &Dist, NULL, NULL);
    if(hit)
	{
        d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
	    d3ddev->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
		gfaceIndex = faceIndex;
		// Yes, draw the picked triangle in solid mode.
			IDirect3DVertexBuffer9* vb = 0;
			IDirect3DIndexBuffer9* ib = 0;
			Mesh->GetVertexBuffer(&vb);
			Mesh->GetIndexBuffer(&ib);

			d3ddev->SetIndices(ib);
			d3ddev->SetVertexDeclaration(Decl);
			d3ddev->SetStreamSource(0, vb, 0, sizeof(Decl));

			// faceIndex identifies the picked triangle to draw.
			d3ddev->DrawIndexedPrimitive(
				D3DPT_TRIANGLELIST, 0, 0, Mesh->GetNumVertices(), faceIndex*3, 1);
			
			
	}
	else
	{
		d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);
		d3ddev->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
	}
}

Share this post


Link to post
Share on other sites
Advertisement
sizeof( Decl ) isn't the stride of the vertex; it's the size of an array (I assume).

Try using mesh->GetNumBytesPerVertex().

Share this post


Link to post
Share on other sites
With this it's still crashing.

d3ddev->SetVertexDeclaration(Decl);
d3ddev->SetStreamSource(0, vb, 0, sizeof(Mesh->GetNumBytesPerVertex()));

Share this post


Link to post
Share on other sites
Quote:
Try using mesh->GetNumBytesPerVertex().

Not sizeof(...), just the number of bytes. [wink]

EDIT: d3ddev->SetStreamSource(0, vb, 0, Mesh->GetNumBytesPerVertex());

sizeof( numberOfBytes ) will return the sizeof(DWORD), which is what GetNumBytesPerVertex returns.

Share this post


Link to post
Share on other sites
Do you use mesh->GetDeclaration to set it?

You need to have:
D3DVERTEXELEMENT9 Decl[MAX_FVF_DECL_SIZE];
mesh->GetDeclaration(Decl);

Share this post


Link to post
Share on other sites
No but i just tried and i am stuck with the parameter for it
Mesh->GetDeclaration , its asking for a D3DVERTEXELEMENT9
this stuff is new terriotry to me

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!