Page 1 of 1

Screen/World projection and conversion, in and outside Ogre

Posted: Wed Jul 03, 2013 3:40 pm
by iLucian

I have a 3D point in world coordinates, lets say

Code: Select all

Vector3 point_world; 
. I then project it onto the screen with

Code: Select all

 Vector3 point_screen = camera->getProjectionMatrix() * (camera->getViewMatrix() * point_world); 
. Then the position on the screen is of course ((point_screen.x/2+0.5) * Width, (1-(point_screen.y/2+0.5)) * Height). The Z coordinate of the pixel I get when rendering the image as a depth map.

My question is how to do the reverse transformation: If I find a pixel on the screen I'm interested in, with coordinates (X,Y,Z), I then declare my Vector3 as

Code: Select all

Vector3 point_screen;
point_screen.x = (X/Width-0.5)*2;
point_screen.y = ((1-(Y/Height))-0.5)*2;
point_screen.z = Z;
How do I get the corresponding point_world? Is it just this? :

Code: Select all

point_world = camera->getViewMatrix().inverse() * (camera->getProjectionMatrix().inverse()  * point_screen) 
I'm asking because I don't understand how it works in the first place that you have a Vector3, and you multiply it by 2 Matrix4's. I thought that in matrix algebra you can't multiply matrixes that don't have the corresponding "common" dimension equal. Is there something in the blender internals that eliminates a column and a row?

And, most importantly, I need to do these two operations (world point to screen point and reverse) outside blender. Assuming I can copy the two matrixes (viewmatrix and projection matrix), how do I use them in the equation to multiply them by a 3x1 Vector?

This is really important for my project so I thank in advance anyone willing to help.

Re: Screen/World projection and conversion, in and outside O

Posted: Sat Jul 06, 2013 4:07 am
by Oogst
You are missing the W part of the equation. It is called homogeneous coordination, go look them up, since it is too big a topic to explain here.

In short: your 3D vector is used a 4D vector, with a 1 added: (x, y, z, 1). After multiplying by those 4D matrices, you get the screen space 4D vector: (x, y, z, w). Now the point, here is that you need to divide the whole thing by w to get back to a 1 at the end: (x/w, y/w, z/w, 1). The numbers you get then are your actual screen space coordinates.

To do the inverse, you will need to know the w somewhere, I think.

Re: Screen/World projection and conversion, in and outside O

Posted: Wed Jul 10, 2013 7:55 pm
by iLucian
Hi! Thank you for the reply.

I finally figured it out by looking in the Ogre source code and found out how the multiplication is being done. I'm still not sure if I understand what exactly w is, but I managed to get the formula that I needed out of it.