Archived

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

Drag0n

"Reverse" perspective and viewing transformation?

Recommended Posts

Hi! I''d like to convert screen coordinates to world coordinates. Is it possible with just the screen position (x, y) and the corresponding depth value? I''m using OpenGL and I wrote functions for inverting matrices already. I''d like to use this to, for example, select a specific position on the heightmap with the mouse. Thanks! Drag0n "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning..." -- Rich Cook

Share this post


Link to post
Share on other sites
Anybody?

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning..." -- Rich Cook

Share this post


Link to post
Share on other sites
look for gluunroject

it convertes your screen coordinate to worldcoordinate

you need to pass the view and projection matrix ...
read it up on you own

Share this post


Link to post
Share on other sites
Ok...that would be gluUnProject(), right? But I was actually looking for how to do it myself!

Thanks anyways...
Drag0n

Share this post


Link to post
Share on other sites
gluunproject is tested and very reliable and available with opengl header files

you can reinvent the wheel of course but that won t make it better

Share this post


Link to post
Share on other sites
I think what you need to do here is to take the inverse of your projection matrix (shouldnt be too hard to find a tutorial on calculating inverse matrices) and apply that.
If you are not using a projection matrix you''ll just have to undo the arithmetic you do when projecting...

for example
screen_x = (3d_x * x_scale / 3d_z) + center_x;
becomes
3d_x = (screen_x - center_x) * 3d_z / x_scale;

Marijn

Share this post


Link to post
Share on other sites
Ok...maybe I didn''t read the Red Book close enough. But is the projection transformation the last operation OpenGL performs vertices? I didn''t think so...

Drag0n

Share this post


Link to post
Share on other sites
OpenGL basically applies the following transformations to vertex coordinates (from the OpenGL 1.4 specs, section 2.10):

Po is your initial vertex in object space.

Modelview matrix:
Pe = Modelview * Po

Projection matrix:
Pc = Projection * Pe

Perspective division:
Pd.x = Pc.x / Pc.w
Pd.y = Pc.y / Pc.w
Pd.z = Pc.z / Pc.w

Final viewport transform:
Pw.x = (px/2)*Pd.x + ox
Pw.y = (py/2)*Pd.y + oy
Pw.z = ((f-n)/2)*Pd.z + (n+f)/2

px,py are the viewport''s width and height, ox,oy are it''s center. n,f is the depth range set by glDepthRange (default: n=0, f=1).

You have to perform all those steps backwards.

Share this post


Link to post
Share on other sites