Reputation: 395
I want to draw text over 3D point. The text request 2D points rect x y x1 y1
i use irrlight engine. But i need just formula.
i have:
core::vector3df point;
core::rect<s32> viewport = driver->getViewPort();
core::matrix4 matProj = driver->getTransform(video::ETS_PROJECTION);
core::matrix4 matView = driver->getTransform(video::ETS_VIEW);
core::matrix4 matWorld = driver->getTransform(video::ETS_WORLD);
core::quaternion point_qua(point.X ,point.Y , point.Z , 1);
// formula
point_qua = point_qua*(matWorld*matView*matProj);
std::cout << "\nX=" << point_qua.X;
std::cout << "\nY=" << point_qua.Y;
but x and y coord not correct. They give me negative y. And text drawing at top left. Is this formula correct?
Upvotes: 1
Views: 152
Reputation: 7488
Almost.
The formula you have gives you the position in OpenGL screenspace, which goes from [-1, -1] to [1, 1]. Positions in OpenGL screen space look like this:
[-1, 1]-----------------------------------------[1, 1]
| |
| |
| |
| |
| |
| [0, 0] |
| |
| |
| |
| |
| |
[-1, -1]----------------------------------------[1, -1]
To get it in pixels, transform as follows:
pixelsX = (1 + point.X) * Viewport.Width / 2;
pixelsY = (1 - point.Y) * Viewport.Height / 2;
Upvotes: 2