09 September 2012  
Expert
portfolio
franky****inFourFingaz

Dresden,
Germany

world space coordinate to image space
Hi guys, i am struggeling with a project for a while now, unfortunately with no success.
I want to calculate a worlds space coordinate (x,y,z) equivalent coordinate in a cameras view rectangle (x,y). Until now i tried to figure out what all the cameraShapes attributes like filmOffset, lensSqueezeRatio, preScale, postScale, whatever mean and mix them all in a MEL script. So far it works partially because some attribute combinations (for example lensSqueezeRatio other than 1.0 in conjunction with cameraScale other than 1.0 and filmOffsetV/H other than 0.0) just let the script fail, the world space point doesnt match the right pixel in the renderViewEditors image anymore. Now i wanted to try the MFnCamera.projectionMatrix in conjunction with the cameras transformation matrix to get this working, but have no glue how to do that. I can read out the matrix values using some melpython calls but now i stuck on how to use those values. Maybe someone has done this before and could give some hints? If so, thanks in andvance, any suggestions would be greatly appreciated! 
09 September 2012  
FX / Lighting / Pipeline
portfolio
Skeel Lee
FX TD
Singapore

Hey zaskar,
Have you tried this script from Rob Bedrow? http://www.185vfx.com/resources/screenSpace.mel He wrote it quite a while back but it should still work pretty well! However, if you still want to use the MFnCamera.ProjectionMatrix: Just multiply the world space point by the camera's world inverse matrix, and then the projection matrix:
The way you transform points in Maya is by postmultiplication i.e. you multiply the transformation matrices on the righthandside, one by one, in sequence, like the example above. You can get the cameraInverseWorldMatrix using:
I don't have Maya with me now to test these snippets (and to be honest, I haven't used it in a while  sorry, using mostly Houdini now). See if you can figure out something from these. If not I might have to launch Maya at work and try to give you a more detailed answer. These are Python codes by the way, using Maya's Python API. Last edited by skeelogy : 09 September 2012 at 12:03 PM. Reason: providing more details 
09 September 2012  
Expert
portfolio
franky****inFourFingaz

Dresden,
Germany

Thanks skeelogy, as soon as I get my hands on maya again I will give it a try!

10 October 2012  
Expert
portfolio
franky****inFourFingaz

Dresden,
Germany

hello again, i did have a look into Rob Bedrow script. It transforms a world point into the cameras objects space by post multiplying it with the cameras inverse matrix, but then it only uses some angular calculations to get the pixel position. It doesnt involve the internal projection transformations using all the different film back manipulators. So i tried to multiply the projection matrix, and thats what i scripted so far:
HTML Code:
I think that i missed the point. Are these values normalized to the side length of the view rectangle? Then why are they negative? And shouldnt the centered point give some values around .5 for each dimension? Last edited by zaskar : 10 October 2012 at 09:55 AM. Reason: some errors in the script 
10 October 2012  
Expert
portfolio
franky****inFourFingaz

Dresden,
Germany

Think it works now. The problem was the missing w component using MEL to query a point and thus the shortened first matrix multiplication procedure. Using the API or python api and a MPoint to catch the position implies the use of the fourth vector component, and the MPoint methods to multiply with a MMatrix also involves the w component of course. Finally, the result x / y components had to be divided by the w component. The result then is a 2D coordinate with the center of the image as origin reaching from 1,1 at corner leftbottom to 1,1 at righttop. So some denormalization had to be done mutliplied by the current RenderGlobals width/height. If somebody else has any use for it:
Besides there seems to be a bug using orthographic cameras with ."cameraScale" attributes other than 1.0 in conjunction with mentalRay as renderer. MR simply ignores the value until some of the attributes inside the cameras AttributeEditor area for all the FilmBack values are changed, starting with "PreScale" as first of them. The script then misses the right pixel. However perspectivic cameras seems to work fine. Last edited by zaskar : 10 October 2012 at 07:18 PM. Reason: providing more information 
10 October 2012  
Expert

Thread automatically closed
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.
__________________
CGTalk Policy/Legalities Note that as CGTalk Members, you agree to the terms and conditions of using this website. 
Thread Closed share thread 
«
Previous Thread

Next Thread
»


