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 »

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.
Oogst
OGRE Expert User
OGRE Expert User
Posts: 1067
Joined: Mon Mar 29, 2004 8:49 pm
Location: the Netherlands
x 43
Contact:

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

Post 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.
My dev blog
Awesomenauts: platforming MOBA (PC/Mac/Linux/XBox360/X1/PS3/PS4)
Blightbound: coop online dungeon crawler (PC)
Swords & Soldiers: side-scrolling RTS (Switch/PS3/Wii/PC/Mac/Linux/iPhone/iPad/Android)
Proun: abstract racing game (PC)
Cello Fortress: mixing game and live cello performance
The Ageless Gate: cello album
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 »

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.
Post Reply