Point 2D -> 3D
I would like to convert a 2D screen point (retrieved from mouse events) to 3D space coordinates.
Could someone explain me the process to do it?
I saw this code in somewhere, I don''t remember when, I think it works, but I don''t understand why is done like this, I mean, I don''t know what I have to do, to make the conversion I talking about.
Thanks.
Vector4 &View3D::unProjectPoint( int x, int y )
{
Vector4 l1;
Vector4 l2;
Vector4 in;
y = height - y;
in.x = ( x - ( float ) viewport[ 0 ] ) * 2 / ( float ) viewport[ 2 ] - 1.0;
in.y = ( y - ( float ) viewport[ 1 ] ) * 2 / ( float ) viewport[ 3 ] - 1.0;
in.z = 1;
in.w = 1.0;
Matrix44 m;
m = *projMatrix * *modMatrix;
m.Invert();
/*Find the line corresponding to
*the point clicked.
*/
l1 = m * in;
l1 /= l1.w;
in.z = -1;
l2 = m * in;
l2 /= l2.w;
intersectPoint = l1.intersectPlane( l2, m_plane );
//clip the intersectPoint to the grid size;
if ( intersectPoint.x > grid_size )
intersectPoint.x = grid_size;
if ( intersectPoint.y > grid_size )
intersectPoint.y = grid_size;
if ( intersectPoint.z > grid_size )
intersectPoint.z = grid_size;
if ( intersectPoint.x < -grid_size )
intersectPoint.x = -grid_size;
if ( intersectPoint.y < -grid_size )
intersectPoint.y = -grid_size;
if ( intersectPoint.z < -grid_size )
intersectPoint.z = -grid_size;
return intersectPoint;
/*
tempPoint = m * in;
tempPoint.x /= tempPoint.w;
tempPoint.y /= tempPoint.w;
tempPoint.z /= tempPoint.w;
return tempPoint;
*/
}
Hi.My mind is similar to yours, but I know where you are.
i'm too lazy to read the code right now but...here's a basic algorithm (not sure if it's the one implemented in your code b/c of the afore mentioned laziness):
basically you have a 2D screen coord.
find the corresponding world coordinate on the near-clip plane. you can do this pretty easily by taking the x-coord/screenWidth and the y-coord/screenHeight. that will end up giving you an x & y coordinate that are each between 0 -> 1.
then armed with that number you can use the parametric equation of a line in 3D space and the points that bound your frustrum to figure out where that point is on the near clip plane. then also where it is on the far clip plane using the same equations. fairly straight forward.
then make a line between those 2 points in your 3D world. run an intersection test on the objects in your frustrum with that line. the object on that line that is closest to the near clip plane is your 3D point.
[EDIT: hrm, it occurs to me that you only need to project that point onto the far clip plane and then your line is your camera position -> the projected point. that will save on some calculation]
[EDIT2: hrm, it re-occurs to me that my EDIT comment is dumb b/c then you have the potential to intersect your line with something in front of the near clip-plane. aka your characters model if it has one....]
coolio?
-me
[edited by - Palidine on November 26, 2002 8:31:05 PM]
[edited by - Palidine on November 26, 2002 8:37:37 PM]
basically you have a 2D screen coord.
find the corresponding world coordinate on the near-clip plane. you can do this pretty easily by taking the x-coord/screenWidth and the y-coord/screenHeight. that will end up giving you an x & y coordinate that are each between 0 -> 1.
then armed with that number you can use the parametric equation of a line in 3D space and the points that bound your frustrum to figure out where that point is on the near clip plane. then also where it is on the far clip plane using the same equations. fairly straight forward.
then make a line between those 2 points in your 3D world. run an intersection test on the objects in your frustrum with that line. the object on that line that is closest to the near clip plane is your 3D point.
[EDIT: hrm, it occurs to me that you only need to project that point onto the far clip plane and then your line is your camera position -> the projected point. that will save on some calculation]
[EDIT2: hrm, it re-occurs to me that my EDIT comment is dumb b/c then you have the potential to intersect your line with something in front of the near clip-plane. aka your characters model if it has one....]
coolio?
-me
[edited by - Palidine on November 26, 2002 8:31:05 PM]
[edited by - Palidine on November 26, 2002 8:37:37 PM]
Palidine kind of glossed over a major fact. A single point in screen space (your 2D point) actually represents an infinite number of points in 3D space. Really, when you pick a point on the screen the corresponding 3D point could be any point corresponding to that projection between the near and far clip planes.
The code you have does this, and Palidine described it somewhat.
Your code also does one other thing. It intersects the 3D line corresponding with the screen space point with a 3D plane defined by the variable m_plane. Not sure what m_plane is since you didn''t post the class declaration, but my guess is that it may very well be the near clip plane. So, it is possible the function is giving you the point on the near clip plane that corresponds to the picked 2D screen point.
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
The code you have does this, and Palidine described it somewhat.
Your code also does one other thing. It intersects the 3D line corresponding with the screen space point with a 3D plane defined by the variable m_plane. Not sure what m_plane is since you didn''t post the class declaration, but my guess is that it may very well be the near clip plane. So, it is possible the function is giving you the point on the near clip plane that corresponds to the picked 2D screen point.
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Thanks for the two replies.
What I am doing is a proyect for the university. It consist of a basic (at the moment) "modeller/map editor" to create maps in the way of WorldCraft or QRadiant programs. Then I will make something to view this maps in a first person perspective and with the posibility to move around the map with the mouse and keyboard, just then to expose the advantages of BSP Trees.
The reason of my first post was because I want to let the users in the modeller, create parametric objects with mouse movememts. This is simply just like programs as 3D Studio MAX, and not like other free enviroments in which you have to make a cube or a sphere via a dialog box, entering the parameters and clicking OK on a dialog box (really ugly).
So to convert 2D screen, to 3D space, I have to know how to do it, and you have perfectly answered me. Thanks
If there is interest in view this proyect, mail me to waterlink@terra.es and then I''ll send you some screenshots, the code will be free. It''s on linux, easily portable to win32.
Thanks again.
What I am doing is a proyect for the university. It consist of a basic (at the moment) "modeller/map editor" to create maps in the way of WorldCraft or QRadiant programs. Then I will make something to view this maps in a first person perspective and with the posibility to move around the map with the mouse and keyboard, just then to expose the advantages of BSP Trees.
The reason of my first post was because I want to let the users in the modeller, create parametric objects with mouse movememts. This is simply just like programs as 3D Studio MAX, and not like other free enviroments in which you have to make a cube or a sphere via a dialog box, entering the parameters and clicking OK on a dialog box (really ugly).
So to convert 2D screen, to 3D space, I have to know how to do it, and you have perfectly answered me. Thanks
If there is interest in view this proyect, mail me to waterlink@terra.es and then I''ll send you some screenshots, the code will be free. It''s on linux, easily portable to win32.
Thanks again.
Hi.My mind is similar to yours, but I know where you are.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement