Jump to content
  • Advertisement
Sign in to follow this  
Anddos

D3DXVec3Unproject help

This topic is 2778 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 want to take a 2d screen click and change it in to an x,y,z in worldspace ,whats wrong with this code?


D3DVIEWPORT9 V9;
gd3dDevice->GetViewport(&V9);
D3DXMATRIX i;
D3DXMatrixIdentity(&i);

Src.x = (float)pm->point.x;
Src.y = (float)pm->point.y;
Src.z = (float)0.0f;

D3DXVec3Unproject(&UnProjected,
&Src,
&V9,
&gCamera->proj(),
&gCamera->view(),
&i);

Share this post


Link to post
Share on other sites
Advertisement
You probably don't want Src.z as 0, as that will give you a point on the near-plane. Where in world-space do you want the point?
If you want to click on something you have already rendered and get the point on that object from the unproject, then you need to read back the z-buffer value at that point. One common way is to unproject two points, one at z = 0 and one at z = 1, which will give you a ray between the near and far plane, and then intersect this ray with the objects you want to click on.

Share this post


Link to post
Share on other sites
Quote:
whats wrong with this code?

It depends on what you're trying to do. Are you having a problem? Does it work? Do you get what you expect?

As mentioned above, that will give you the world point under the mouse at the near plane. Remember, there are an infinite number of world points under the mouse point in your world, accessible by unprojecting a Src.z value from 0 to 1, corresponding to world points under the mouse from the near plane to the far plane.

Make sure the mouse points are in client coordinates, not screen coordinates. Also, ensure your backbuffer, viewport and client rectangle sizes are all the same.

Share this post


Link to post
Share on other sites
ive added
ScreenToClient(FindWindow("D3DWndClassName",NULL),&pm->point);
but it dosent seem to make any diference, this is for a first person camera

Share this post


Link to post
Share on other sites
This is what i am trying todo steve


if(gd3dApp->Fire)
{

ScreenToClient(FindWindow("D3DWndClassName",NULL),&pm->point);

D3DXVECTOR3 Src;
D3DVIEWPORT9 V9;
Src.x = (float)pm->point.x;
Src.y = (float)pm->point.y;
Src.z = (float)1.0f;

D3DXVECTOR3 UnProjected;
D3DXMATRIX i;
D3DXMatrixIdentity(&i);
//MessageBox(NULL,"gd3dApp->Fire","",0);
D3DXVec3Unproject(&UnProjected,
&Src,
&V9,
&gCamera->proj(),
&gCamera->view(),
&i);

sprintf(buffer,"%f %f %f",UnProjected.x,UnProjected.y,UnProjected.z);
RECT r = {100, 100, 0, 0};
HR(mFont->DrawText(0, buffer, strlen(buffer), &r, DT_NOCLIP, D3DCOLOR_XRGB(0,255,0)));

LineStruct l;
l.LineBuffer = wl->LoadMultipleLinesToWorld(UnProjected,UnProjected);
ls.push_back(l);


gd3dApp->Fire = false;
}


for(int i = 0; i < ls.size(); i++)
{
wl->RenderMultipleLinesToWorld(ls.LineBuffer);
}

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!