# Position of pixel in 3D world space?

Ok, I’m assuming you’re using a perspective projection here…

Using the parameters you used in glFrustum/gluPerspective, you should easily be able to find the location of what m was before applying the MODELVIEW matrix. R is essentially the near clip plane, I think. (If I’m understanding correctly) So the z is easy. x and y would be found by knowing what your left, right, top, and bottom clip planes are. (If you use gluPerspective you can calculate those using the FOV, aspect and near clip plane. Think of those as defining a pyramid, then you should be able to figure out the trig involved.)

Once you have that location, just multiply it by the MODELVIEW matrix after any “camera transformations” you may have done.

[This message has been edited by Deiussum (edited 05-30-2003).]

Hi Jebus,

R is a representation of the final rasterized image. Its the rectangle that represents the image being synthesized. Its position is independent of the near clipping plane.

Regards,
Slammo

Hi Deiussum,

Thanks for the message. I should have started this thread with an image, but now I think you’re starting to understand me.

R is not the near clipping plane though, it is the plane on which the image is formed. R is located a distance f (where f is the focal length) away from the center of projection C. So for a given focal length, the location of R is fixed. If I change the position of the clipping planes, R does not move.

Regards,
Slammo

here’s something that i think would work. ignore everything i had previously said though… i just didn’t understand what you were asking.

first you need to get a definition of the plane that you want to intersect in world space. you have the distance from the center of projection (which i assume is the eye point in standard opengl terms), and the normalized view direction. multiply the distance by the view direction and add it to the eye location. this will get you a point in the plane. the normal to the plane is the vector from the point you just computed to the eye location. now you have a complete specification of a plane.

next you can find a line segment to intersect against the plane. take your (x,y) screen coordinates and supply them to gluUnProject(), specifying a z value of 0.0 and the appropriate transformations. this’ll get you the world space location of the point projected onto the near clip plane. now do the same thing, except specify a z value of 1.0. this’ll get you the world space location of the screen point projected onto the back clipping plane. these two points specify your line segment. one thing to watch out for is that you don’t include the modeling transformation when you call gluUnProject(), or else you’ll get coordinates in object space instead of world space. just specify the viewing transformation and the unprojected points should be in world space.

now interset this line segment with the plane to get the world coordinates. code for line segment-plane intersection can be found almost anywhere, including here .

eh… may as well do up some pseudo code.

``````Vector3 eyeLoc; //initialize to your eye location in world coordinates
Vector3 viewDir; //make sure this is normalized
float focalPtDistanceFromEye; //initialize as appropriate

//first form the plane
Vector3 ptOnPlane = eyeLoc + focalPtDistanceFromEye*viewDir;
Vector3 planeNormal = eyeLoc - ptOnPlane;

//now compute the line segment in world space
int x, y; //screen coordinates
Matrix modelView, projection;
Rect viewport;
//initialize transformations as appropriate
//make sure to only include the viewing transformation in the modelview matrix

Vector3 worldPtOnNearPlane = gluUnProject(x,y,0.0,modelView,projection,viewport);
Vector3 worldPtOnFarPlane = gluUnProject(x,y,1.0,modelView,projection,viewport);

Vector3 intersectionLocation = SegmentPlaneIntersection(worldPtOnNearPlane,worldPtOnFarPlane,planeNormal,ptOnPlane);
``````

as before, that code hasn’t been tested or anything, so be careful. hope i have the right idea here.

-steve

Hi Steve,

Thanks for the idea. I think your solution may work. Perhaps I will give it a try.

Regards,
Slammo

Thanks everyone for your help. I finally figured this out – many of you were close to the right solution.

The easiest way to do it is to use gluUnproject(), but use the negative of the near clipping plane for the z coordinate.

Thanks to all who helped.

Regards,
Slammo

I found it entertaining to read this thread because of the repeated misunderstanding of the OP’s question.

I’m also trying to figure out the 3d coordinates of points on the image plane, but as a relative newbie to OpenGL, to me at least the whole concept of the image plane seems elusive in the OpenGL framework!

Slammo: from your last post, can I deduce that the image plane is at the same distance from the focal / view point as the near clipping plane?

In fact, would it be correct to say that that is just a convention for gluUnproject to take the z coordinate of the pixel as -(near clipping distance), but that in general, choosing the “focal length” is also to choose: the actual size of objects relative to the actual size of the pixels.

Why did you resurrect a 9 year old thread? Wonder if we could consider locking threads after 6 months or so of inactivity…

Because OpenGL’s API hasn’t changed much in 9 years and this particular issue is discussed practically nowhere else on the net…

Because OpenGL’s API hasn’t changed much in 9 years[…]

:doh: (random characters here to reach minimum)

Hehe, ok i’ll control my forum policy transgressions in future. My contributions can only improve from here!