PDA

View Full Version : How to convert camera coordinates to the coordinates of the view widget


kryakin
06-18-2012, 06:13 PM
How to convert camera (MFnCamera) coordinates to the coordinates of the view widget (M3dView)?

Any example please.

zoharl
06-19-2012, 12:53 AM
Camera projection matrix to multiply on the left of a column vector:

void cam_projection_matrix(MObject &oCam, ublas::matrix<double> &mat)
{
MFnCamera cam(oCam);
MFloatMatrix proj_mat = cam.projectionMatrix();
ublas::matrix<double> uproj;
mayaMat2ublas(proj_mat, uproj);

MFnDagNode node(oCam);
MDagPath dag;
node.getPath(dag);
MMatrix trans = dag.inclusiveMatrixInverse();
ublas::matrix<double> utrans;
mayaMat2ublas(trans, utrans);

ublas::matrix<double> invZ = ublas::identity_matrix(4,4);
invZ(2,2) = -1; // maya projection to opengl

ublas::matrix<double> tmp = ublas::prod(utrans,uproj);
mat = ublas::prod(tmp,invZ);
}

kryakin
06-19-2012, 12:24 PM
Thanks for this code. I'll remember.
But I mean something like M3dView::worldToView()
I already have the coordinates of the vertices in the Camera. In different ranges [-1:1], [0,1] or [640:480], [800:600].
I have know:

M3dView::widget()
M3dView::portWidth()
M3dView::portHeight()
kFillFilmFit, kHorizontalFilmFit, kVerticalFilmFit or kOverscanFilmFit
...
MEvent.getPosition( x, y ) or QMouseEvent->x and QMouseEvent->y


How to convert 0x0 ( left bottom corner of MFnCamera) to left-bottom corner of FilmGate border coordinates of viewport widget (coordinates on the screen ).

zoharl
06-19-2012, 01:59 PM
Sorry, but I didn't exactly understand what you want. Please be more specific: I.e. I have this point, and I'm looking for this point.

Concerning the worldToView(), actually the matrix I gave you is the projection matrix that when you multiply it by a world point it would transform it to a screen point (i.e. projection).

kryakin
06-19-2012, 04:23 PM
Thanks for the feedback
see a screenshot (1:1)
http://expertyres.ru/tmp/example.jpg

How to convert 1 to 3

zoharl
06-19-2012, 06:01 PM
If you want to convert the film gate (camera) value to your viewport widget, isn't it just a matter of translating the point by the difference of the top left corners of the two spaces (camera and widget)?

I hope I understood what you meant, else I think the image would be clearer if you use an orthographic view (top for example), and choose a point on the grid in 3D (put a small sphere there) and specify its coordinates, and then specify its coordinates in the other spaces.

kryakin
06-19-2012, 07:13 PM
If you want to convert the film gate (camera) value to your viewport widget, isn't it just a matter of translating the point by the difference of the top left corners of the two spaces (camera and widget)?

... No? Pixel size is different and offset is unknown.
Film Gate (camera) is painted on viewport widget.
But what size (display pixels) and where the center (filmFitOffset)?
What camera pixel size on viewport widget?

screenshot:
640x480 film gate is 503x376 film gate painted on widget (in case 653x736 widget size and kHorizontalFilmFit, only). Why?

Maya MEvent getPosition( x, y ) returns the coordinates in the coordinate system of the widget

Different pixel size, different filmFit (kHorizontalFilmFit, kVerticalFilmFit ), different offsets, + perspShape.filmFitOffset etc.

Trying to find examples

CGTalk Moderation
06-19-2012, 07:13 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.