【问题标题】:Zooming in on mouse position in OpenGl放大OpenGL中的鼠标位置
【发布时间】:2011-05-03 19:17:39
【问题描述】:

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

已经提出了类似的问题(例如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);

给出了这个结果:

我可以知道将世界原点移动到我希望的任何屏幕位置,而不是围绕光标位置缩放。很高兴拥有,但不是我想要的。

由于我已经坚持了很长一段时间,我想知道:是否可以仅通过修改投影矩阵来生成相对于任意屏幕位置的缩放?还是我必须修改我的 Modelview 矩阵?

【问题讨论】:

    标签: math opengl zooming


    【解决方案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)移动到鼠标位置。我会试试你的建议,因为我不确定我是否尝试过那个变种。
    • 我试过 - 这会导致类似的行为:场景原点转移到鼠标位置。
    • @sum1,我不确定这意味着什么。可以发个截图吗?
    • @LarsH 是的,我认为您的回答会导致突然跳转到鼠标所在的位置。我们正在根据谷歌地图寻求解决方案
    • @MickyDuncan: (1) 你是 sum1stolemyname 吗?在同一个项目上工作?我想知道您是否代表与 OP 相同的要求,或者不同的要求。 (2) 我想你是说你想要一个动画(渐变)缩放。对吗?
    猜你喜欢
    • 1970-01-01
    • 2020-03-16
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多