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.
Regards
Sumit
Screen to world transformation
-
- Gnoblar
- Posts: 5
- Joined: Fri Feb 25, 2011 2:34 pm
- syedhs
- Silver Sponsor
- Posts: 2702
- Joined: Mon Aug 29, 2005 3:24 pm
- Location: Kuala Lumpur, Malaysia
- x 47
Re: Screen to world transformation
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
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
-
- Gnoblar
- Posts: 5
- Joined: Fri Feb 25, 2011 2:34 pm
Re: Screen to world transformation
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
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
- syedhs
- Silver Sponsor
- Posts: 2702
- Joined: Mon Aug 29, 2005 3:24 pm
- Location: Kuala Lumpur, Malaysia
- x 47
Re: Screen to world transformation
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):-
What you want is rayTarget variable. So from here, it is not that hard to tailor your own function.
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;
}
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
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
-
- Gnoblar
- Posts: 5
- Joined: Fri Feb 25, 2011 2:34 pm
Re: Screen to world transformation
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
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
- syedhs
- Silver Sponsor
- Posts: 2702
- Joined: Mon Aug 29, 2005 3:24 pm
- Location: Kuala Lumpur, Malaysia
- x 47
Re: Screen to world transformation
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
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
-
- Gnoblar
- Posts: 5
- Joined: Fri Feb 25, 2011 2:34 pm
Re:[Solved] Screen to world transformation
Thanks syedhs,
I was able to solve the problem with your suggestions.Here is the code of mine which works.
Earlier I was doing the same thing without normalizing the screenX/Y.
Thanks for all your help once again !
Regards
Sumit
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;
}
Thanks for all your help once again !
Regards
Sumit