# Thread: Position of pixel in 3D world space?

1. ## Position of pixel in 3D world space?

Hi,

Can anyone tell me how to compute the 3D world position of a 2D pixel (x, y)?

The 2D pixel lies in the image. The image is on a rectangle in 3D space. What are the 3D coordinates of this pixel?

Thanks,
Slammo

2. ## Re: Position of pixel in 3D world space?

hi,

After you do your transformation (it must always start from glLoadIdentity(); and before glPopMatrix() , use
glGetFloatv(GL_MODELVIEW_MATRIX,cvm) to get the tranfromed matrix;where cvm is used to hold it which is defined as: float cvm[16];
Then you call something like below;
convert_m(oldx,oldy,oldz,& newx,&newy,&newz);(cvm of course is the input to this function too)
where old is something you know before transformation and new is the output which is what you want to know after transforamtion.
Hope you get it

Originally posted by slammo:
Hi,

Can anyone tell me how to compute the 3D world position of a 2D pixel (x, y)?

The 2D pixel lies in the image. The image is on a rectangle in 3D space. What are the 3D coordinates of this pixel?

Thanks,
Slammo

3. ## Re: Position of pixel in 3D world space?

Hi, thanks for your post to my question.

If I understand what you're saying, the transformation between a pixel's screen coordinate and its world space coordinate depends on the modelview matrix. I agree, in fact, I think it also depends on the viewport and the projection matrix.

My question is, given a pixel's coordinate, (x, y) on the screen, and the modelview matrix, projection matrix, and viewport, how do I compute the pixel's location in 3D space?

Best regards,
Slammo

4. ## Re: Position of pixel in 3D world space?

read up on gluUnProject(). it does what you need

jebus

5. ## Re: Position of pixel in 3D world space?

Thanks for your suggestion, jebus.

gluUnproject requires an image-space z-coordinate. What z-coordinate should be used? I tried z = 0 but this didn't work -- I assume because z = 0 is the near clipping plane, which is in a different location than the image plane. Any suggestions?

Thanks,
Slammo

6. ## Re: Position of pixel in 3D world space?

read it back from the depth buffer using glReadPixels

7. ## Re: Position of pixel in 3D world space?

Thanks for the suggestion, chowe6685.

Reading the z-value from the depth buffer would give the position of the 3D point that projects onto the image pixel(x, y).

Instead, I am looking for the 3D position of the image pixel(x, y).

Thanks,
Slammo

8. ## Re: Position of pixel in 3D world space?

so what you want to do is convert from image/rectangle space to world space, right? how about something like the following:

Code :
//rectangle points in world space. assume they're initialized.
Vector3 lowerLeft, lowerRight, upperLeft, upperRight;

//assume that the image covers the entire rectangle.
int xMin = 0, xMax = imageWidth, yMin = 0, yMax = imageHeight;

//image space coords. assume they're initialized.
int x, y;

float xFactor = x/(xMax-xMin);
float yFactor = y/(yMax-yMin);

Vector3 worldPt = (1.0f-xFactor)*(1.0f-yFactor)*lowerLeft + (xFactor)*(1.0f-yFactor)*lowerRight + (xFactor)*(yFactor)*upperRight + (1.0f-xFactor)*(yFactor)*upperLeft;
there might be something wrong with the code there, but it illustrates the general idea.

9. ## Re: Position of pixel in 3D world space?

There is none. You cannot compute it.
If you are familiar with 3d space, you'll know that viewport is a plane. If you take a point on this plane(your pixel), you cannot know from where it was projected, decause you need z value. You can find a line, which projectzion onto the line is your pixel:

|
|
_____.___ plane with pixel
|

10. ## Re: Position of pixel in 3D world space?

slammo, what are you asking for here?

do you want the world coordinate corresponding to a particular screen coordinate? or do you have a textured rectangle in world space and you want the world coordinate of an image coordinate on the textured rectangle? if it's the latter, my previously posted code should work. if it's the former, you have to read back the projected z value from the depth buffer and call gluUnProject() with the appropriate transforms, as everyone's been saying.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•