缩小到兴趣点

use*_*005 2 opengl math graphics

我有以下变量:

  • 兴趣点,即焦点所在位置的像素位置(x,y).
  • 屏幕宽度,高度是窗口的尺寸.
  • 缩放级别,用于设置摄像机的缩放级别.

这是我到目前为止的代码.

void Zoom(int pointOfInterestX,int pointOfInterstY,int screenWidth,
   int screenHeight,int zoomLevel)
{   
glScalef(1,1,1);
glTranslatef( (pointOfInterestX/2) - (screenWidth/2), (pointOfInterestY/2) - (screenHeight/2),0);

glScalef(zoomLevel,zoomLevel,1);
}
Run Code Online (Sandbox Code Playgroud)

我想放大/缩小,但保持在屏幕中间的兴趣点.但到目前为止,我的所有尝试都失败了.

Vau*_*ato 5

您可以像这样开始渲染帧:

 glViewport(0, 0, w, h);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 GLdouble left = (0 - pointOfInterestX) / zoomLevel + pointOfInterestX;
 GLdouble right = (WindowW - pointOfInterestX) / zoomLevel + pointOfInterestX;
 GLdouble bottom = (WindowH - pointOfInterestY) / zoomLevel + pointOfInterestY;
 GLdouble top = (0 - pointOfInterestY) / zoomLevel + pointOfInterestY;
 glOrtho(left, right, bottom, top, -1, 1);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
Run Code Online (Sandbox Code Playgroud)