gluUnproject返回0,似乎与模型视图矩阵有关

gluUnproject returning 0, seems to be related to modelview matrix

本文关键字:视图 模型 返回 gluUnproject      更新时间:2023-10-16

我正在开发一个 2D 图像查看器,我想检索纹理上的 openGL 鼠标位置,但如果在模型视图矩阵上进行 glTranslatef() 或 glScalef() 调用,我无法让它工作。我正在使用著名的Qt库的QGLWidget。

以下是重要的电话:调整大小功能:

void ViewerGL::resizeGL(int width, int height){
     glViewport (0, 0, width, height); 

显示功能:

void ViewerGL::paintGL()
{        int w = width();
         int h = height();
        glMatrixMode (GL_PROJECTION);
        glLoadIdentity();
        //transX,transY are for panning around the image in the viewer
        float left = (0.f+transX) ; 
        float right = (w+transX) ; 
        float bottom = (h-transY);
        float top = (0.f-transY) ;        
        glOrtho(left, right, top, bottom, -1, 1);

。后来在油漆GL中:

        glMatrixMode (GL_MODELVIEW);
        glLoadIdentity ();
        //padx,pady are used to translate the image from the bottom left corner 
        // to the center of the viewer
        float padx,pady;
        padx= ((float)width() - _dw.w()*zoomFactor)/2.f; // _dw.w is the width of the texture
        pady =((float)height() - _dw.h()*zoomFactor)/2.f ;// _dw.h is the height of the texture
        glTranslatef( padx , pady, 0);
        //zoomX,zoomY are the position at which the user required a zoom
        glTranslatef(-zoomX,-zoomY, 0.f);
        glScalef(zoomFactor, zoomFactor,0.f); 
        glTranslatef(zoomX ,zoomY, 0.f);

现在这是我检索 openGL 坐标的函数:

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 = height()- y;
   if(winY == 0) winY =1.f;
   glReadPixels( x, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
   gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
   return QPoint(posX,posY);

}

到目前为止,这是我注意到的:像这样的代码总是返回 (0,0),GLU_FALSE从 gluUnproject 返回。我在论坛上的某处读到可能是因为模型视图矩阵,所以我放了单位矩阵,但是,如果我这样做,我会得到窗口中鼠标的坐标......

之前,我

使用正交投影处理缩放,但我无法使其完美工作,因此为了更简单,我决定检索 openGL 位置,并使用 glTranslatef/glScalef 代替。

如果我删除 paintGL 函数中的所有平移/缩放内容,一切都在工作......但是缩放不起作用:x)

我请求您帮助使用 gluUnProject 解决方案使这个该死的缩放到点工作;

Aigth , 没关系,我找到了解决方案:我在 glScalef(x,y,z) 中将 z 归零所以它使矩阵不可逆...