Advertisement

Window Coordinates to World coordinates

Started by September 26, 2003 08:42 AM
3 comments, last by doc_zero 21 years, 5 months ago
Hi, there. I would like to draw an object under mouse cursor. So question is, how do I calculate world coordinates given window coordinates. I understand that I will not have all 3 coordinates. But imagine that I have a plane in the scene and I would like my object to be moved on this plane (so only x,y should be changing). I need the object to stay under the mouse cursor as it moves on the plane. Looking at the transformation pipeline, I should inverse it to get to the world coordinates, what is the easy way to do it? Thanks.
Hi!

I think glOrtho is the best choice for that.

glOrtho(left, right, bottom, top, near, far)


example with WindowCoords (RECT windowRect):

glOrtho((double)windowRect.left,
(double)windowRect.right,
(double)windowRect.top,
(double)windowRect.bottom,-1.0,1.0);


Consider that windowRect.top corresponds to the bottom coordinate of glOrtho, so you have to flip that coord like in my example.


Advertisement
I belive gluUnProject is what you want. I have a demo on my site that shows one way to drag an object in 3D. It requires MFC, but if you don''t have MFC you should be able to just read the code and see how it works.

After finishing my demo, someone else on this site (sorry, I don''t remember who!) suggested a better way to do it than what I did: Use what I call the "depth value plane". When you look at gluUnProject and glReadPixels, you''ll see how to get the "depth value" for where the mouse is - I think it''s 0 for the front, 1 for the back, and then it''s just like a percentage if there''s an object in the frustum that the mouse is "pointing at".

Anyway, when you click an object and then drag the mouse, what you can do is calculate the point the mouse is pointing at in the same "depth value plane", and map that "depth value plane" to the plane you want to drag on - in my case it was XZ, but you could map it to anything with a one-to-one correspondence with the pixels in the window.

If that didn''t make sense, check the documentation for gluUnProject and glReadPixels in MSDN, and my demo if you want, and hopefully it soon will!

Good luck.

Love means nothing to a tennis player

My nothing-to-write-home-about OpenGL webpage. (please pardon the popups!)

Thanks for the hint on gluUnProject (by the way I found a very good example on how to use it here: http://openglforums.com/forums/viewtopic.php?t=397). Though I need a little bit different thing. Or maybe it is just my misunderstanding. Depth buffer returns values, which are actually distance from the projection plane to the point in space isn''t it? So I give it x,y,z in window coordinates and get x'',y'',z'' in world coordinates, but I have z'' and I do not have z (glReadPixels does not help here). It looks like I have to convert my z into z'' and then go back to get my x,y, but z depends on x and y of the window... Any comments?

I *think* I understand now - so the plane you want to drag the object on is parallel to the YZ plane? For example, the plane z = -50?

If so, here is how I would do it:

1. Call gluUnProject with a Z value of 0 to get the point on the front of the frustum your mouse points to. Let''s call it (x1, y1, z1)
2. Call gluUnProject with a Z value of 1 to get the point on the back of the frustum your mouse points to (remember, when you point at the screen you''re pointing at a ray that goes from the front of the view frustum to the back). Let''s call it (x2, y2, z2)
3. Use the point-slope formula to find x and y such that (x, y, -50) is on that line. As long as your ray from the front of the frustum to the back of the frustum is not parallel to the plane you''re trying to drag along, there should be exactly one solution.

(x, y, -50) = (x1, y1, z1) + n(x2 - x1, y2 - y1, z2 - z1)

4. Draw your object at the point (x, y, -50)!


You could extend this technique to any flat plane I belive, for example

Ax + By + Cz + D = 0 => z = (-D -Ax - By)/C

Plug in that stuff on the right instead of the -50 and what you end up with is 3 equations in 3 unknowns.


So, does that make sense? Your mouse points at a point x,y on the screen, but that''s actually an entire line whose endpoints you can get with gluUnProject, then a little math gets you which point on that line you want.

I hope this is what you were asking!

Good luck

Love means nothing to a tennis player

My nothing-to-write-home-about OpenGL webpage. (please pardon the popups!)

This topic is closed to new replies.

Advertisement