Backwards Projection

Started by
5 comments, last by abolfoooud 17 years, 4 months ago
Dear experts, I would like to do back-projection from screen coords to 3D OpenGL coords. Suppoze i have pixel (sx, sy) on screen and i want to get its actual position in the 3D space of OGL (gx, gy, gz). How can I do that espicially that i will move from 2D to 3D? Regards Abolfoooud
Advertisement
Hi!

You can achieve this with the gluUnProject function if you have the depth value.
Thanx for the reply.

What is Depth exactly?

And how can i achieve this back-projection manually, ie i want to do the calculations myself (the dirty way using matrises :D). Don want to use API functions!!!

Not that i am using glfrustum() not gluperspective()

cheers
Because one dimension is lost in the process of projection, there is no simple way of reverting the transformation by an inverse projection matrix or such. The general approach then, is to shoot a ray in that direction and test for intersections. The first object that intersects this ray will give the depth. I wrote a detailed description of the process a while back, you can find that here:
http://www.gamedev.net/community/forums/topic.asp?topic_id=421237
Thankx Ashkan.
That is really a good work!!
Just have some questions regarding these points in your reply:

Now Devide by:
proj[0] = proj(0, 0) -> Projection transformation scaling multiplyer in x direction
proj[5] = proj(1, 1) -> Projection transformation scaling multiplyer in y direction

And the result is x and y in homogenous clip space.

float x = normX / proj[0];
float y = normY / proj[5];

what do you mean by proj[0] = proj(0, 0) and proj[5] = proj(1, 1)?? I did not get is? Are you recalculating it using some function not shown in your description???

Also in:

Vector4 rayDirection( x, y, -1.0f, 0.0f );

why do you use -1.0f for z? Do you have to use -ZNearHeight instead of -1 if you are using gluperspective??

best
Abolfoooud
Quote:what do you mean by proj[0] = proj(0, 0) and proj[5] = proj(1, 1)?? I did not get is? Are you recalculating it using some function not shown in your description???


No, proj[0] is just the array representation of the element located at the zeroth row and zeroth column. Same goes for proj[5], its just the array cell containing the element located at the first row and column. Remember that OpenGL, contrary to Direct3D, uses column major matrices. I used this array representation to follow the OpenGL notation where matrices are represented as column major arrays. A better way of writing that to help avoid such confusion is:
proj[0] == proj(0, 0)proj[1] == proj(1, 1)


In case you're wondering what a perspective projection matrix (and consequently its (0, 0) and (1, 1) elements) look like check the 31st slide of the following powerpoint presentaion. Its also a good intro on the subject:
http://www.cs.wisc.edu/graphics/Courses/559-f2004/lectures/cs559-11.ppt

*Off topic*: You may also find these other presentations useful:
http://www.cs.wisc.edu/graphics/Courses/559-f2004/lectures/

Quote:why do you use -1.0f for z? Do you have to use -ZNearHeight instead of -1 if you are using gluperspective??


OpenGL, again contrary to Direct3D, places the clipping planes down the negative Z axis so the view volume that is formed is totally placed on the negative side of the XY plane. Maybe this decision was partly because OpenGL's use of a right handed coordinate system. As a side effect zNear > zFar, because we are looking down the negative Z axis at this volume of space. Our common sense says that the near value should be a lower number than the far and that's exactly why all those functions (gluPerspective, glFrustum and glOrtho) accept positive values and internally negate them.

If you're interested in the mathematics behind the pipeline, "3D Computer Graphics" by Alan Watt is a great book to look into.

Hope that helps,
Ashkan
Thanx Ashkan.
You are a star!

Abolfoooud

This topic is closed to new replies.

Advertisement