Screen to world transformation

Problems building or running the engine, queries about how to use features etc.
Post Reply
sumitkulkarni23
Gnoblar
Posts: 5
Joined: Fri Feb 25, 2011 2:34 pm

Screen to world transformation

Post by sumitkulkarni23 »

Hi,
I am working on a sample application, where I have embedded a Mogre window inside WPF. I am trying to draw a interactive line on the canvas there. In this I am facing a problem where I am not able to convert the selected screen co-ordiantes to world co ordinates. I tried lot of things to perform this conversion (including the hints given in other forums) but its not helping. Does Mogre have any API whcih does this for me ? I simply want to convert the mouse positions which are in pixel units (ex. 176, 174 etc) to world co - ordinates. The attachment shows the problem.
shows the screen to world transform not working
shows the screen to world transform not working
screenToworld.JPG (2.18 KiB) Viewed 1269 times
Regards
Sumit

User avatar
syedhs
Silver Sponsor
Silver Sponsor
Posts: 2702
Joined: Mon Aug 29, 2005 3:24 pm
Location: Kuala Lumpur, Malaysia
x 47

Re: Screen to world transformation

Post by syedhs »

I think you can use Camera::getCameraToViewportRay. For an example of this, look at Mouse Picking Wiki Tutorial
A willow deeply scarred, somebody's broken heart
And a washed-out dream
They follow the pattern of the wind, ya' see
Cause they got no place to be
That's why I'm starting with me

sumitkulkarni23
Gnoblar
Posts: 5
Joined: Fri Feb 25, 2011 2:34 pm

Re: Screen to world transformation

Post by sumitkulkarni23 »

Thanks for help.
I tried that, basically the "result" contains world geometries. it has some thing in X, Y, Z but those values are not useful.
It doesn't show me a generic method for converting the pixel co-ordinates to world/object ?
I need those methods. is there anything like that in OGRE/MOGRE ?

Regards
Sumit

User avatar
syedhs
Silver Sponsor
Silver Sponsor
Posts: 2702
Joined: Mon Aug 29, 2005 3:24 pm
Location: Kuala Lumpur, Malaysia
x 47

Re: Screen to world transformation

Post by syedhs »

Actually we are not going to do mouse-picking, the sample just show the usage. What you are interested is to pick up the 3d coordinate based on the 2d position in the viewport. So it means that you need to pick up the end point of the ray, which is okay not possible just by calling the function because it just returns the origin and direction. But looking at the source code (simplified):-

Code: Select all

void Camera::getCameraToViewportRay(Real screenX, Real screenY, Ray* outRay) const
{
		Matrix4 inverseVP = (getProjectionMatrix() * getViewMatrix(true)).inverse();

		Real nx = (2.0f * screenX) - 1.0f;
		Real ny = 1.0f - (2.0f * screenY);
		Vector3 midPoint (nx, ny,  0.0f);

		// Get ray origin and ray target on near plane in world space
		Vector3 rayOrigin;
		
		rayTarget = inverseVP * midPoint;

}
What you want is rayTarget variable. So from here, it is not that hard to tailor your own function.
A willow deeply scarred, somebody's broken heart
And a washed-out dream
They follow the pattern of the wind, ya' see
Cause they got no place to be
That's why I'm starting with me

sumitkulkarni23
Gnoblar
Posts: 5
Joined: Fri Feb 25, 2011 2:34 pm

Re: Screen to world transformation

Post by sumitkulkarni23 »

Hi Syedhs,
Thanks a lot for your help. I tried the second approach as suggested by you, but still I am not getting the correct results.
Just to highlight a point, I was reading the documentation and it says getCameraToViewportRay needs "normalised screen coordinates [0,1]"
which I don't have ! I have Pixel screen co-ordinates. Does that make any differance ?

Regards
Sumit

User avatar
syedhs
Silver Sponsor
Silver Sponsor
Posts: 2702
Joined: Mon Aug 29, 2005 3:24 pm
Location: Kuala Lumpur, Malaysia
x 47

Re: Screen to world transformation

Post by syedhs »

Normalised in this case means mousex/windows_width and similarly, mousey/windows_height. So both parameter should not exceed 1.0.
A willow deeply scarred, somebody's broken heart
And a washed-out dream
They follow the pattern of the wind, ya' see
Cause they got no place to be
That's why I'm starting with me

sumitkulkarni23
Gnoblar
Posts: 5
Joined: Fri Feb 25, 2011 2:34 pm

Re:[Solved] Screen to world transformation

Post by sumitkulkarni23 »

Thanks syedhs,
I was able to solve the problem with your suggestions.Here is the code of mine which works.

Code: Select all

public Point Unproject(float screenX, float screenY)
      {
          // Actual width of Viewport - I have set my camera(mCam) as viewPort
          float scrx = screenX / mVPort.ActualWidth; 
          float scry = screenY / mVPort.ActualHeight;
          Ray ray = mCam.GetCameraToViewportRay((float)(scrx), (float)(scry));
          Vector3 vect = ray.GetPoint(mCam.NearClipDistance);
          Point ret = new Point(vect.x, vect.y);
          return ret;

      }
Earlier I was doing the same thing without normalizing the screenX/Y.
Thanks for all your help once again !

Regards
Sumit

Post Reply