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
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
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
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
read up on gluUnProject(). it does what you need
jebus
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
read it back from the depth buffer using glReadPixels
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
so what you want to do is convert from image/rectangle space to world space, right? how about something like the following:
there might be something wrong with the code there, but it illustrates the general idea.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 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
|
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.