Screen/World projection and conversion, in and outside Ogre

Problems building or running the engine, queries about how to use features etc.
Post Reply
iLucian
Gnoblar
Posts: 21
Joined: Fri Jan 18, 2013 10:43 pm

Screen/World projection and conversion, in and outside Ogre

Post by iLucian » Wed Jul 03, 2013 3:40 pm

Hi!

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.
0 x

Oogst
OGRE Expert User
OGRE Expert User
Posts: 1034
Joined: Mon Mar 29, 2004 8:49 pm
Location: the Netherlands
Contact:

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

Post by Oogst » Sat Jul 06, 2013 4:07 am

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.
0 x
blog.oogst3d.net: my dev blog and portfolio
Ronimo Games: my game dev company
Awesomenauts: platforming MOBA (PC/Mac/Linux/XBox360/PS3/PS4)
Swords & Soldiers: side-scrolling RTS (PS3/Wii/PC/Mac/Linux/iPhone/iPad/Android)
Proun: abstract racing game (PC)
Cello Fortress: mixing game and live cello performance

iLucian
Gnoblar
Posts: 21
Joined: Fri Jan 18, 2013 10:43 pm

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

Post by iLucian » Wed Jul 10, 2013 7:55 pm

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.
0 x

Post Reply