用翻译放大鼠标位置?

时间:2010-07-01 01:52:17

标签: c++ c opengl vector

放大我正在使用的鼠标位置:

glTranslatef(current.ScalePoint.x,current.ScalePoint.y,0);
    glScalef(current.ScaleFactor,current.ScaleFactor,current.ScaleFactor);
    glTranslatef(-current.ScalePoint.x,-current.ScalePoint.y,0);

基本上我转换为新原点(鼠标位置),然后按当前比例因子缩放,然后转换回来。

这种作品一般都很好,但它可能有点儿麻烦。我的问题是,现在我引入了相机偏移,所以我尝试了这样的事情:

glTranslatef(controls.MainGlFrame.GetCameraX(),
    controls.MainGlFrame.GetCameraY(),0);
glTranslatef(current.ScalePoint.x,current.ScalePoint.y,0);


glScalef(current.ScaleFactor,current.ScaleFactor,current.ScaleFactor);
glTranslatef(-current.ScalePoint.x,-current.ScalePoint.y,0);

但这并没有按照我的意图行事。我怎么能这样做才知道:

矩阵的原点是左上角(0,0)

1个单位== 1个像素

我的比例因子

我的相机相对于距离(0,0)(原点)和

的距离

鼠标位置(屏幕到客户端)。

由于

1 个答案:

答案 0 :(得分:2)

即使您知道如何完成投影,也可以首先将鼠标坐标点(从窗口坐标到模型坐标)取消投影,这样更安全(也可用于代码重用)。

您可以使用以下功能:

void unProject(int ix, int iy, int &ox, int &oy)
{
 // First, ensure that your OpenGL context is the selected one
 GLint viewport[4];
 GLdouble projection[16];
 GLdouble modelview[16];

 glGetIntegerv(GL_VIEWPORT, viewport);
 glGetDoublev(GL_PROJECTION_MATRIX, projection);
 glGetDoublev(GL_MODELVIEW_MATRIX, modelview);

 int xx = ix;
 int yy = viewport[3] - iy;
 GLdouble x, y, z;
 gluUnProject(xx, yy, 0 /*check*/, modelview, projection, viewport, &x, &y, &z);

 ox = (int) x;
 oy = (int) y;
}

然后输出是缩放模型坐标上的正确点