Hi Theo,
I'll have a camera that will be still for the whole time. 2D objects will be
put in front of the camera as it was a 2D application. Although, sometimes
these 2D objects will spin around an axis in 3D. So, I cannot use the Ortho
projection.
Why not? Your objects are still in 3D space when you use an ortho
projection, the difference is only that objects that are farther away
from the camera will not become smaller. If all your objects will always
be positioned on the same plane from the camera, and you just need to be
able to rotate them, you can still use an ortho projection.
But perhaps you want to see perspective within your objects too, in
which case you're right, you'll have to use a perspective projection.
However, I can't find out how to set the projection matrix so that I can work
with my coordinates right. I need to know what are the maximum coordinates that
fit my window, how many pixels there are, what real size are the objects,
etc... Because I'm getting some window coordinates from screen touchs! As these
coordinates are relative to the window I don't know how to relate them to my
OSG objects!
To transform screen position to world position, use this code:
osg::Matrix MVPW( camera->getViewMatrix() *
camera->getProjectionMatrix() *
camera->getViewport()->computeWindowMatrix());
osg::Matrixd inverseMVPW = osg::Matrixd::inverse(MVPW);
osg::Vec3 near_point = osg::Vec3(x,y,0.0f) * inverseMVPW;
osg::Vec3 far_point = osg::Vec3(x,y,1.0f) * inverseMVPW;
Note that what you get is actually a line that goes from the near plane
to the far plane, since you're transforming 2D coordinates into 3D it
means that a point in 2D becomes a line in 3D.
To transform a 3D position into 2D window coordinates you would just do
the opposite of that, using the same MVPW as above:
osg::Vec3 posIn2D = posIn3D * MVPW;
Just make sure the posIn3D is in world space and not in some object's
local coordinate system.
Hope this helps,
J-S
--
______________________________________________________
Jean-Sebastien Guay [email protected]
http://www.cm-labs.com/
http://whitestar02.webhop.org/
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org