2010-11-02 59 views
4

我希望爲我正在使用的CAD應用程序實現一個放大到鼠標位置與滾動輪盤算法。在OpenGl中放大鼠標位置

類似的問題已經被問(This one爲例),但所有的解決方案,我發現使用的

  1. 翻譯原籍
  2. 規模放大
  3. 翻譯回

方法。雖然這在原理上是有效的,但它會導致物體在高縮放級別被剪切掉,因爲它們變得比觀看體積大。更優雅的解決方案是修改放大投影矩陣。

我試圖實現這一點,但只獲得了窗口中心的縮放工作。

glGetIntegerv(GL_VIEWPORT, @fViewport); 
//convert window coordinates of the mouse to gl's coordinates 
zoomtoxgl := mouse.zoomtox; 
zoomtoygl := (fviewport[3] - (mouse.zoomtoy)); 
//set up projection matrix 
glMatrixMode(GL_PROJECTION); 
glLoadidentiy; 
left := -width/2 * scale; 
right := width/2 * scale; 
top := height/2 * scale; 
bottom := -height/2 * scale; 
glOrtho(left, right, bottom, top, near, far); 

我的問題是:是否有可能在正交視圖單獨使用投影矩陣的任意一點進行放大,如果是這樣,怎麼能變焦目標位置被分解成投影矩陣?

更新 我改變了我的代碼

zoomtoxgl := camera.zoomtox; 
zoomtoygl := (fviewport[3] - camera.zoomtoy); 
dx := zoomtoxgl-width/2; 
dy := zoomtoygl-height/2; 
a := width * 0.5 * scale; 
b := width * 0.5 * scale; 
c := height * 0.5 * scale; 
d := height * 0.5 * scale; 

left := - a - dx * scale; 
right := +b - dx * scale; 
bottom := -c - dy * scale; 
top := d - dy * scale; 
glOrtho(left, right, bottom, top, -10000.5, Camera.viewdepth); 

這給了這樣的結果:

Zooming issues

而是圍繞光標位置縮放的,我可以知道這個世界的起源轉向我希望的任何屏幕位置。很高興,但不是我想要的。

因爲我現在已經停留了很長一段時間,所以我想知道:只需通過修改投影矩陣就可以生成相對於任意屏幕位置的縮放?或者我將不得不修改我的Modelview矩陣?

回答

1

如何更改左/右/上/下的值以反映鼠標的位置?

喜歡的東西:

left := zoomtoxgl - width/2 * scale; 
right := zoomtoxgl + width/2 * scale; 
top := zoomtoygl + height/2 * scale; 
bottom := zoomtoygl - height/2 * scale; 
glOrtho(left, right, bottom, top, near, far); 

你可能需要調整規模,爲這種用法。

順便說一句你見過嗎8.070 How can I automatically calculate a view that displays my entire model? (I know the bounding sphere and up vector.)我跟着他們的例子在這個答案,以及8.040 How do I implement a zoom operation?

+0

感謝您的想法。我一直在與我的鼠標座標和比例雜耍,但我迄今爲止管理的最好的事情是將我的場景的原點(世界座標中的0,0,0)移動到鼠標位置。我會嘗試你的建議,因爲我不知道我是否嘗試過這個變種。 – sum1stolemyname 2010-11-03 06:45:58

+0

我嘗試過 - 這會導致類似的行爲:場景的原點被移到鼠標位置。 – sum1stolemyname 2010-11-03 07:06:00

+0

@ sum1,我不確定那是什麼意思。你可以張貼截圖嗎? – LarsH 2010-11-03 12:26:19