代码之家  ›  专栏  ›  技术社区  ›  sum1stolemyname

在OpenGl中放大鼠标位置

  •  4
  • sum1stolemyname  · 技术社区  · 14 年前

    我想实现一个缩放到鼠标位置与滚动轮算法的一个CAD应用程序,我的工作。

    This one 但是我找到的所有解决方案都使用了

    1. 转换为原点
    2. 向后翻译

    接近。虽然这在原则上是可行的,但它会导致对象在高缩放级别上被剪裁掉,因为它们变大了,那么观看音量就变大了。一个更优雅的解决方案是修改用于缩放的投影矩阵。

     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 回复  |  直到 7 年前
        1
  •  1
  •   LarsH    14 年前

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

    类似于:

     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?

    推荐文章