【发布时间】:2011-05-03 19:17:39
【问题描述】:
我希望为我正在处理的 CAD 应用程序实现缩放到鼠标位置和滚轮的算法。
已经提出了类似的问题(例如This one),但我找到的所有解决方案都使用了
- 翻译到原点
- 缩放以缩放
- 向后翻译
方法。虽然这在原则上有效,但它会导致对象在高缩放级别上被剪掉,因为它们变得比查看体积更大。更优雅的解决方案是修改投影矩阵以进行缩放。
我尝试实现这一点,但只能缩放到窗口中心。
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 矩阵?
【问题讨论】: