[SOLVED] Get transf matrix from 3D World space to 2D window

Problems building or running the engine, queries about how to use features etc.
Post Reply
User avatar
RiccardoTurati
Halfling
Posts: 67
Joined: Thu Apr 19, 2012 4:59 pm
Location: Milano, Italy

[SOLVED] Get transf matrix from 3D World space to 2D window

Post by RiccardoTurati »

Good day,
I need to retrive the conversion matrix that allow me to transform points from 3D world space to local 2D window space ([-1;1]).
I have seen the way to convert the points:
http://www.ogre3d.org/tikiwiki/tiki-ind ... e=Cookbook
but i also need the conversion matrix.
Anyone could help me?
Thank you all.
Best Regards.
Riccardo.
Last edited by RiccardoTurati on Thu Oct 18, 2012 3:19 pm, edited 1 time in total.
User avatar
cose
Halfling
Posts: 46
Joined: Thu Jul 24, 2008 5:48 pm
Location: Marseille
Contact:

Re: Get transform matrix from 3D World space to 2D window sp

Post by cose »

you can check the conversion matrix here :
http://stackoverflow.com/questions/7242 ... projection
User avatar
RiccardoTurati
Halfling
Posts: 67
Joined: Thu Apr 19, 2012 4:59 pm
Location: Milano, Italy

Re: Get transform matrix from 3D World space to 2D window sp

Post by RiccardoTurati »

Hi all,
thank you cose for your hint, I have read the topic, but sadly is not strictly what i need.
To achieve my goal, even if the result will be the same, i need that the Screen Projection should be inside the matrix, and not calculated outside.
Anyone could help me?
Thank you.
Best Regards.
Riccardo.
User avatar
RiccardoTurati
Halfling
Posts: 67
Joined: Thu Apr 19, 2012 4:59 pm
Location: Milano, Italy

Re: Get transform matrix from 3D World space to 2D window sp

Post by RiccardoTurati »

Hi all,
I have found this documentation, that it seems quite usefull, but i'm still making an error that i couldn,t find.

http://robertokoci.com/world-view-proje ... -unveiled/

Basically I have read that we need this matrix:
matrix4x4 World_View_Projection_Matrix = World x View x Projection
I try to achieve this with this code:

Code: Select all

Ogre::Matrix4 l_Projection = mCamera->getProjectionMatrix();
Ogre::Matrix4 l_View = mCamera->getViewMatrix();
Ogre::Matrix4 l_Mat1_1 = mCamera->getProjectionMatrixWithRSDepth();
Ogre::Matrix4 l_Wt;
l_Wt = Ogre::Matrix4::IDENTITY;
mCamera->getWorldTransforms(&l_Wt);
Ogre::Matrix4 l_World_View_Projection_Matrix = l_Wt * l_View * l_Mat1_1;
Anyone could help me find where the error is?
Thank you.
Best Regards.
Riccardo.
User avatar
Wolfmanfx
OGRE Team Member
OGRE Team Member
Posts: 1525
Joined: Fri Feb 03, 2006 10:37 pm
Location: Austria - Leoben
x 99
Contact:

Re: Get transform matrix from 3D World space to 2D window sp

Post by Wolfmanfx »

Here is how you get the prj matrix

Code: Select all

Ogre::Matrix4 projMatrix = pCam->getProjectionMatrix(); // This only works if you do not use a custom projection matrix!
Ogre::Matrix4 deviceProjMatrix;
Ogre::Root::getSingleton().getRenderSystem()->_convertProjectionMatrix(projMatrix, deviceProjMatrix, true);
The world matrix must retrieved by your target scenenode with getFullTransform (be sure the scenenode is updated).
The view matrix is also easy to get from the camera (but only if you do not use a custom view mat)
User avatar
RiccardoTurati
Halfling
Posts: 67
Joined: Thu Apr 19, 2012 4:59 pm
Location: Milano, Italy

Re: Get transform matrix from 3D World space to 2D window sp

Post by RiccardoTurati »

I have modified the code as following:

Code: Select all

Ogre::Matrix4 projMatrix = mCamera->getProjectionMatrix(); // This only works if you do not use a custom projection matrix!
Ogre::Matrix4 deviceProjMatrix;
Ogre::Root::getSingleton().getRenderSystem()->_convertProjectionMatrix(projMatrix, deviceProjMatrix, true);
Ogre::Matrix4 l_FullMove = mCameraNode->_getFullTransform();
Ogre::Matrix4 l_View = mCamera->getViewMatrix();
Ogre::Matrix4 l_World_View_Projection_Matrix = l_FullMove.concatenate(l_View);
l_World_View_Projection_Matrix = l_World_View_Projection_Matrix.concatenate(deviceProjMatrix);
return l_World_View_Projection_Matrix;
The result is still wrong.
I have 3 points A = upper left corner, B = bottom right corner, C = upper right corner.
I expect that appling matrix to that point will transform them in A (-1;1) , B (1;-1) , C (1;1) and this is not true.
Am I doing something wrong?
Thank you.
Best Regards.
Riccardo
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 534

Re: Get transform matrix from 3D World space to 2D window sp

Post by Kojack »

getViewMatrix already takes the camera's parent node into account. You don't need l_FullMove.

The short version to get the matrix to convert from 3d space to 2d screen space is:

Code: Select all

Matrix4 convert = mCamera->getProjectionMatrix() * mCamera->getViewMatrix();
You might need to use getProjectionMatrixRS() instead, depending on how you are going to use the screen space points.
User avatar
RiccardoTurati
Halfling
Posts: 67
Joined: Thu Apr 19, 2012 4:59 pm
Location: Milano, Italy

Re: Get transform matrix from 3D World space to 2D window sp

Post by RiccardoTurati »

Thanks all for the help.
If someone else need this information i have used Kojack solution and it works perfectly.
Thanks again.
Best Regards.
Riccardo.
Post Reply