2010-11-02 61 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