Archived

This topic is now archived and is closed to further replies.

Shadwdrak

Converting Pixel coords to 3d coordinates

Recommended Posts

I was wondering if someone could point me to an article or explain to me the process of converting from screen (pixel) coordinates to world (plane?) coordinates. I would like to know the formula and why it works... -Will

Share this post


Link to post
Share on other sites
I''m not aware of any articles, but you''ll probably have to be more specific as to your intentions. I could interpret your question is many ways.

Are you talking about building 3D models from 2d images? That''s a very complicated topic, but if you search for something like "image-based rendering" or "plenoptic functions", you might find the stuff you''re looking for.

Perhaps you just want a way to pick an object based on a mouse press or something. You can try searching Object Selection or Object Picking. If you''re using opengl, that have a selection mechanism to achieve just that.

maybe you''re trying to do the inverse of transforming and projecting polygons. In that case, you''ll need to know the rotations/translations/perspective/blah blah matricies that were used to project to that point....

It would help if you explained what kind of information you needed...

Share this post


Link to post
Share on other sites
Sorry for being unclear...

All that I am trying to do is make a quad that fills up the entire screen. The screen resolution is variable (640x480 or 800x600). So I was wondering how big I need to make the quad so it fits the screen exactly. If it helps I have the screen set looking at the origin and zoomed out 15.0 units back on the Z axis. I am using OpenGL with a perspective view.

I hope this information is enough.

-Will

Share this post


Link to post
Share on other sites
There are probably a lot more people better able to answer this question for you, but I'll give it a shot...

To go from screen coordinates to world coordinates is probably unnecessary. I'm going to assume you know how opengl projection works (basically, after the points are transformed by the modelview matrix, it is multiplied by the projection matrix, resulting in preprojection coordinates. These are then converted to windows coordinates depending on the resolution defined in glViewport).

From that knowledge, you'd know that the resolution of the screen should not affect the size of the quad. Only the shape of the frustum will determine that. IMHO, the easiest way to fit a quad exactly is to figure out the size of the far plane and draw the quad with that same dimensions and a tiny bit in front of the far plane so that parts of it don't get clipped out. Of course, then the quad won't fit "exactly", but there will be almost no difference.

It sounds like the frustum you're defining is symmetrical WRT the x and y axis. Therefore, you can simply use similar triangles to figure out the dimensions of the far plane. Basically, if:

eyeZ = z coord of camera's eye
farZ = far plane's z coord
fovY = field-of-view in y direction (usually 45/2 degrees)

then the height of the far plane is

2 * (farZ - eyeZ) * tan(fovY)

the width of the far plane can be calculated with the aspect ratio (unless the aspect ratio of your view plane is different from the window):

aspect * height

The z coord for the quad is the same as the far plane, with a small number to push it a bit closer to the camera.

I haven't checked the math...definitely don't use it without deriving the equations yourself. I only wanted you to understand what I was thinking. But this is only a suggestion. I'm sure there are sites that describes better methods, but I don't know of any off-hand

Edited by - davidko on March 20, 2001 8:04:20 AM

Share this post


Link to post
Share on other sites
In Direct3D, you use pre-transformed vertices for this sort of thing so you can specify the screen locations directly. When you use this trick, the only information you need is the screen height and width.
Now I don''t claim to be any sort of OpenGL expert, so I may be wrong (I''m sure someone will correct me if I am ), but I don''t think that OpenGL has the concept of pre-transformed vertices. Instead, if you want to directly specify screen pixel locations, you want to use a simple orthographic projection matrix and set up your viewport to be from (0,0) to (width, height).
I think that if you use gluOrtho2(0.0, w, 0.0, h) for GL_PROJECTION, the identity for GL_MODELVIEW, and glViewport(0,0,w,h), then you can just specify your vertex locations directly in screen space (0,0) to (h,w).

Hope this helps.
...Syzygy

Share this post


Link to post
Share on other sites