2010-07-01 71 views
2

要放大我用的是鼠标的位置:用翻译放大鼠标位置?

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

所以基本上我转换到新的原点(鼠标位置),那么当前缩放因子Scale,然后翻译回。

这种作品通常很好,但它可能有点bug。我的问题是真的,现在我心中已经推出了相机偏移所以我想是这样的:

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)(原点)和

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

感谢

+0

它干了什么啦不打算对模型正确的点坐标? – Cogwheel 2010-07-01 02:01:43

+0

这就好像它首先移动到了一点,这很难解释,但是当我第一次改变鼠标位置时,它感觉到了跳动,并不觉得它从停止的位置继续。 – jmasterx 2010-07-01 02:16:00

回答

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; 
} 

然后输出是你的缩放

+0

这是如何解决这个问题的? – paulm 2014-01-22 13:51:45