C++ gluUnproject返回0,似乎与modelview矩阵有关
我正在使用2D图像查看器,我想在纹理上检索openGL鼠标位置,但如果在modelview矩阵上调用glTranslatef()或glScalef(),则无法使其工作。 我使用的是著名的Qt库的QGLWidget 以下是重要的呼吁: 调整大小功能:C++ gluUnproject返回0,似乎与modelview矩阵有关,c++,qt,opengl,model-view,C++,Qt,Opengl,Model View,我正在使用2D图像查看器,我想在纹理上检索openGL鼠标位置,但如果在modelview矩阵上调用glTranslatef()或glScalef(),则无法使其工作。 我使用的是著名的Qt库的QGLWidget 以下是重要的呼吁: 调整大小功能: void ViewerGL::resizeGL(int width, int height){ glViewport (0, 0, width, height); void ViewerGL::paintGL() { i
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);
显示功能:
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);
。。。稍后在paintGL中:
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是从glunproject返回的。我在论坛的某个地方读到,这可能是因为modelview矩阵,所以我改为使用标识矩阵,但是,如果我这样做,我会得到窗口中鼠标的精确坐标
之前,我使用正交投影处理缩放,但我无法使其完美工作,因此为了使其更简单,我决定检索openGL位置,并改用GLTRANSTALEF/glScalef
如果我删除paintGL函数中的所有平移/缩放功能,一切都正常……但缩放不起作用:x)
我请求您的帮助,使用GlunProject解决方案,使这该死的点缩放工作正常;) 不管怎样,我找到了解决办法:我把glScalef(x,y,z)中的z调零 所以它使得矩阵不可逆