PDA

View Full Version : Mouse click to 3d co ordinate



CoolBeanJ
09-25-2011, 07:03 PM
Hi everyone,

I'm looking for a way to get a mouse click location to a 3D coordinate.

I can get the X and Y easily but the Z has stumped what i'd like to do is a raycast out and use the mesh behind the click to calculate the Z position.

So on mouse click record X and Y location of mouse and raycast to the mesh behind the mouse click where it intersects is where I want my 3D coordinates.

Unfortunately I don't know how to do this so any help would be great!

Thanks

Edit: I'm not sure if raycast is the best way to do this, if there is another way to get the 3d mouse position based on the polygon underneath the mouse click that would be great, some more information is that the view can be changed and works like a fly camera.

ugluk
09-26-2011, 12:08 AM
There are thousands of posts like yours and just about as many answers, why don't you google for them?

CoolBeanJ
09-26-2011, 01:55 AM
I've been googling and searching for a while, couldn't find anything. Could you at least point me to a topic that has the answer?

I've been trying with glReadPixels and gluUnProject but my results are completely wrong. Not sure why this would be code is:


float[] winZ = new float[1];
Gl.glReadPixels(winX, winY, 1, 1, Gl.GL_DEPTH_COMPONENT, Gl.GL_FLOAT, winZ);
double objX = 0, objY = 0, objZ = 0;
Glu.gluUnProject(winX, winY, winZ[0], MV, P, viewport, out objX, out objY, out objZ);

Edit:

Ok i've really confused myself.

So what I've done is after clicking my mouse I do the above code, and make a pyramid at obj x,y and z.

This pyramid appears no where near where I click.

Is that to do with the fact that I have rotated my scene? What am I doing so wrong...

Edit:

Ok final edit

What I did is push the matrix add transforms and rotations that the "camera" has and then do the above code

and that seems to work.

CoolBeanJ
09-26-2011, 08:03 PM
nvm