In other side : 3D -> 2D Screen coordinates ?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
cyberbobjr
Posts: 64
Joined: Mon Sep 08, 2003 8:21 am
Location: Paris, France

In other side : 3D -> 2D Screen coordinates ?

Post by cyberbobjr »

Yep,
Another question :)
How can i have the 2D screen coordinate of a vector3df ?

stupid question i know but i need the answer :)

Thanks you...
cyberbobjr
Posts: 64
Joined: Mon Sep 08, 2003 8:21 am
Location: Paris, France

Post by cyberbobjr »

Argh...
Nobody can help me ?
niko
Site Admin
Posts: 1759
Joined: Fri Aug 22, 2003 4:44 am
Location: Vienna, Austria
Contact:

Post by niko »

Hi, sorry for the late answer. In the next version there will be a function in the engine, which will do this for you. Until then, you could do this:

it is assumed that pos3d is a vector3df with the input 3d position and pos2d is the 2d position you want.

Code: Select all

core::dimension2d<s32> dim = driver->getScreenSize();
dim.Width /= 2;
dim.Height /= 2;

f32 transformedPos[4];

core::matrix4 trans;
trans *= smgr->getActiveCamera()->getProjectionMatrix();
trans *= smgr->getActiveCamera()->getViewMatrix();

transformedPos[0] = pos3d.X;
transformedPos[1] = pos3d.Y;
transformedPos[2] = pos3d.Z;
transformedPos[3] = 1.0f;

trans.multiplyWith1x4Matrix(transformedPos);

f32 zDiv = transformedPos[3] == 0.0f ? 1.0f :
	(1.0f / transformedPos[3]);

pos2d.X = (s32)(dim.Width * transformedPos[0] * zDiv) + dim.Width;
pos2d.Y = ((s32)(dim.Height - (dim.Height * (transformedPos[1] * zDiv))));
cyberbobjr
Posts: 64
Joined: Mon Sep 08, 2003 8:21 am
Location: Paris, France

Post by cyberbobjr »

Thank you very much !!!
Post Reply