螢幕座標系:左上方為(0, 0)右下角為(1, 1)
OGRE的2D座標系:左上方為(-1, 1)右下角為(1, -1)
CEGUI座標系:左上方為(0, 0),單位像素
轉換公式(滑鼠座標=>OGRE的2D座標)
void setCorners(float left, float top, float right, float bottom)
{
left = left * 2 - 1;
right = right * 2 - 1;
top = 1 - top * 2;
bottom = 1 - bottom * 2;
}
對於根據滑鼠位置來產生射線:
bool mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
...
CEGUI::Point mousePos = CEGUI::MouseCursor::getSingleton().getPosition();
Ray mouseRay = mCamera->getCameraToViewportRay(mousePos.d_x/float(arg.state.width), mousePos.d_y/float(arg.state.height));
...
}
其中函數
Ray getCameraToViewportRay(Real x, Real y) const;
// x and y are in “normalized” (0.0 to 1.0) screen coordinates
其中兩個參數是對螢幕座標系來說的,
所以
x = mousePos.d_x / float(arg.state.width)
y = mousePos.d_y / float(arg.state.height)
arg.state.width是渲染視窗的寬單位為像素
arg.state.height是渲染視窗的高單位為像素
mousePos.d_x是滑鼠所在位置到渲染視窗左邊界的距離單位為像素
mousePos.d_y是滑鼠所在位置到渲染視窗上邊界的距離單位為像素