2010-07-01 72 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