sum*_*ame 5 opengl math zooming
我希望为我工作的 CAD 应用程序实现缩放到鼠标位置和滚轮算法。
已经问过类似的问题(例如这个问题),但我发现的所有解决方案都使用了
方法。虽然这在原理上是有效的,但它会导致对象在高缩放级别上被剪掉,因为它们变得比观看量更大。更优雅的解决方案是修改缩放的投影矩阵。
我试图实现这一点,但只能缩放到工作窗口的中心。
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);
Run Code Online (Sandbox Code Playgroud)
我的问题是:是否可以在正交视图中单独使用投影矩阵对任意点执行缩放,如果可以,如何将缩放目标位置纳入投影矩阵?
更新 我将代码更改为
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);
Run Code Online (Sandbox Code Playgroud)
这给出了这个结果:

我可以知道将世界原点移动到我希望的任何屏幕位置,而不是围绕光标位置缩放。很高兴拥有,但不是我想要的。
由于我已经坚持了很长一段时间,我想知道:是否可以通过修改投影矩阵来生成相对于任意屏幕位置的缩放?或者毕竟我必须修改我的模型视图矩阵?
如何更改左/右/上/下的值以反映鼠标的位置?
就像是:
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);
Run Code Online (Sandbox Code Playgroud)
您可能需要调整此用途的比例。
顺便说一句,您看过8.070 如何自动计算显示整个模型的视图?(我知道边界球体和向上向量。)我在这个答案中遵循他们的示例,以及8.040如何实现缩放操作?。