Sign in to follow this  
Anddos

D3DXVec3Unproject help

Recommended Posts

Anddos    588
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
Erik Rufelt    5901
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
Buckeye    10747
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
Anddos    588
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
Anddos    588
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[i].LineBuffer);
}

Share this post


Link to post
Share on other sites
Buckeye    10747
Quote:
D3DXVec3Unproject(&UnProjected,
&Src,
&V9,
&gCamera->proj(),
&gCamera->view(),
&i);


Anddos, for one thing, your arguments are out of order. Src and V9 need to be reversed.

Share this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by Buckeye
Anddos, for one thing, your arguments are out of order. Src and V9 need to be reversed.
Not according to the docs? Did you miss the first parameter when looking at his code like I did the first time [smile]?

Quote:
Original post by Anddos
This is what i am trying todo steve
And..?

Share this post


Link to post
Share on other sites
Anddos    588
what do you mean , Out is the first param so thats the UnProjected,*Pv is the source which is Src, i dont get what you mean :/

Share this post


Link to post
Share on other sites
Evil Steve    2017
What is the actual problem? Does the app crash? Do you get "wrong" values (and if so, what values, and how do you know they're wrong)? Do pixies fly out of the DVD drive?

Share this post


Link to post
Share on other sites
Anddos    588
i can draw lines from Src and CameraPos like this

l.LineBuffer = wl->LoadMultipleLinesToWorld(Src,gCamera->mPosW);

just not getting any lines rendering with UnProjected,gCamera->mPosW

so creating ,xyz positon from mouse cant be working

anyway here is a pic so you get the idea

http://img89.imageshack.us/img89/7407/gunline.jpg

Share this post


Link to post
Share on other sites
Evil Steve    2017
If mPosW is the camera's world position, then drawing a line from there to the picked point will give you a single pixel (And possibly none at all, I'm not sure how single pixel lines work), since the camera is looking directly along that vector. What if you draw a quad at the picked point instead?

Share this post


Link to post
Share on other sites
Buckeye    10747
Quote:
Did you miss the first parameter when looking at his code like I did the first time [smile] ?

[embarrass] Yoiks. Sorry, Anddos. Ignore my comment on the order of the arguments.

Share this post


Link to post
Share on other sites
Evil Steve    2017
Ah, I didn't see the screenshot before.

Where are you actually clicking on the screen in that screenshot? You should be getting two points, not just one, as Erik Rufelt said. And what is gCamera->mPosW? Is that your camera position in world space?

Share this post


Link to post
Share on other sites
Anddos    588
yeh, thats the world camera pos ,
i am trying to figure out if my level is like on a slope how do you make the camera.y goes up etc, i dont think picking will work and retrieve the XYZ with d3dxintersecttri, as it seems expensive to lock in the render thread..

so here is the picked face of the building
http://img259.imageshack.us/img259/4421/gunpick.jpg,
now if i use that to find the floor height , i dont think its going to work
so i am stuck with how to make the camera move up stairs and stuff ..

Share this post


Link to post
Share on other sites
Anddos    588
now i am trying to place a ray at the camera position and direction then its converted to the same space as the stairs,i cant seem to get it to intersect and i dont know why

D3DXVECTOR3 RayPos(gCamera->mPosW.x,gCamera->mPosW.y,gCamera->mPosW.z);
D3DXVECTOR3 RayDir(gCamera->mLookW.x,gCamera->mLookW.y,gCamera->mLookW.z);
D3DCOLOR_XRGB(255,0,0)));
D3DXVECTOR3 TransLook,TransNorml,TransPos;
DWORD FaceIndex;
BOOL Hit;
D3DXMATRIX i;
D3DXMatrixInverse(&i,NULL,&(bl2->BuildingWorld * gCamera->view()));
D3DXVec3TransformCoord(&TransPos,&RayPos,&i);
D3DXVec3TransformNormal(&TransLook,&RayDir,&i);
D3DXVec3Normalize(&TransLook,&TransLook);
D3DXIntersect(bl2->Mesh,&TransPos,&TransLook,&Hit,&FaceIndex,NULL,NULL,NULL,NULL,NULL);
if(Hit)
{
MessageBox(NULL,"Hit","",0);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this