放大OpenGl中的鼠标位置

时间:2010-11-02 07:22:39

标签: math opengl zoom image-zoom

我希望为我正在使用的CAD应用程序实现一个缩放到鼠标位置的滚轮算法。

有人问过类似的问题(例如This one),但我找到的所有解决方案都使用了

  1. 翻译成原点
  2. 缩放以缩放
  3. 翻译
  4. 的方法。虽然这在原理上是有效的,但它会导致物体在高变焦水平下被剪掉,因为它们变得比观看体积大。更优雅的解决方案是修改投影矩阵以进行缩放。

    我试图实现这个,但只是缩放到窗口的中心工作。

     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 个答案:

答案 0 :(得分: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?