1

我有一个 2D 应用程序,它是一个图像查看器。我可以在窗口中平移图像,并且可以进行基本缩放。现在我想改进缩放,使其缩放到鼠标下方的点。到目前为止,我已经在谷歌上阅读了所有可能的内容,并且我已经接近一些可行的东西,但仍然找不到可行的东西,这就是我得到的:

          glMatrixMode (GL_PROJECTION);
          glLoadIdentity();

         // w and h are respectivly the width  and height of the window      
        // newzoom_pos is the point in openGL coordinates  where the user requested the zoom
        // zoomFactor is between [0.1,10.0]  , 0.1 -> 1.0 means downscale, 1.0 -> 10.0 means upscale
        // transX and transY are used to pan around the image
        float left = (0.f+transX -newzoom_pos.x())/zoomFactor  +newzoom_pos.x();
        float right = (w+transX -newzoom_pos.x())/zoomFactor +newzoom_pos.x();
        float bottom = (h-transY-newzoom_pos.y())/zoomFactor +newzoom_pos.y();
        float top = (0.f-transY -newzoom_pos.y())/zoomFactor +newzoom_pos.y();

        glOrtho(left, right, top, bottom, -1, 1);

        glMatrixMode (GL_MODELVIEW);
        glLoadIdentity ();

它几乎可以工作,但不行,它不能完全缩放到请求的点,并且当图像很小并且不完全适合查看器(周围有黑色)时,它很乱并且不起作用。 ..

顺便说一句,这是用户放大鼠标滚轮事件处理程序时的代码:

zoomFactor+=0.1;
QPoint oglpos = openGLpos(new_zoomPoint.x(), new_zoomPoint.y());
 this->newzoom_pos = oglpos;

openGLpos 函数如下:

QPoint ViewerGL::openGLpos(int x,int y){
    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLfloat winX=0, winY=0, winZ=0;
    GLdouble posX=0, posY=0, posZ=0;
    glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
    glGetDoublev( GL_PROJECTION_MATRIX, projection );
    glGetIntegerv( GL_VIEWPORT, viewport );
    winX = (float)x;
    winY = viewport[3]- y;
    if(winY == 0) winY =1.f;
    glReadPixels( x, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
    GLint b=gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
    if(b==GLU_FALSE) cout << "failed unprojection" << endl;
    return QPoint(posX,posY);
}

有人可以帮我解决这个问题吗?

4

0 回答 0