现在的位置: 首页 > 综合 > 正文

OGRE的2D坐标、CEGUI坐标、鼠标坐标、 世界坐标转屏幕坐标

2018年01月16日 ⁄ 综合 ⁄ 共 1443字 ⁄ 字号 评论关闭

屏幕坐标系:左上角为(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是鼠标所在位置到渲染窗口上边界的距离单位为像素

 

 

将世界坐标转换为屏幕坐标:

bool worldCoordToScreen(Vector3 objPos, Camera* cam, Vector2 screenRect,  Vector2& screenPos)
 {
  Matrix4 viewMatrix = cam->getViewMatrix();
  Matrix4 projMatrix = cam->getProjectionMatrix();

  Vector4 in = Vector4(objPos.x, objPos.y, objPos.z, 1.0);
  Vector4 out = viewMatrix * in;
  out = projMatrix * out;

  if(out.w <= 0.0) return false;    // out.w<0时,objPos 在摄像机背面

  out.x /= out.w;
  out.y /= out.w;
  out.z /= out.w;

  // Map x, y and z to range 0-1
  out.x = out.x * 0.5 + 0.5;
  out.y = out.y * 0.5 + 0.5;
  out.z = out.z * 0.5 + 0.5;

  // Map x,y to viewport
  out.x = out.x * screenRect.x;
  out.y = (1-out.y) * screenRect.y;

  screenPos.x = out.x;
  screenPos.y = out.y;

  return true;
 }

 

抱歉!评论已关闭.