opengl屏幕坐标转换世界桌标,我的代码怎么就是结果不对,代码如下
void openglwidget::unPorject(real4 screen,real4* minWorld,real4* maxWorld) {
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLdouble posX, posY, posZ;
ViewSize viewSize;
viewSize.x = viewport[2];
viewSize.y = viewport[3];
glGetIntegerv(GL_VIEWPORT, viewport); /* 获取三个矩阵 */
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
GLfloat winX = (float)screen.x;
GLfloat winY = viewport[3] - (GLint)screen.y-1;
gluUnProject(winX, winY, 0, modelview, projection, viewport, &posX, &posY, &posZ);
minWorld->x = posX;
minWorld->y = posY;
minWorld->z = posZ;
gluUnProject(winX, winY, 1, modelview, projection, viewport, &posX, &posY, &posZ);
maxWorld->x = posX;
maxWorld->y = posY;
maxWorld->z = posZ;
}
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;
glPushMatrix();
// 变换要绘图函数里的顺序一样,否则坐标转换会产生错误
glScalef(m_srtMatrix[0], m_srtMatrix[1], m_srtMatrix[2]); // 缩放、平移、旋转变换
glRotatef(m_srtMatrix[3], 1.0f, 0.0f, 0.0f);
glRotatef(m_srtMatrix[4], 0.0f, 1.0f, 0.0f);
glRotatef(m_srtMatrix[5], 0.0f, 0.0f, 1.0f);
glTranslatef(m_srtMatrix[6], m_srtMatrix[7], m_srtMatrix[8]);
glGetIntegerv(GL_VIEWPORT, viewport); // 得到的是最后一个设置视口的参数
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glPopMatrix();
winX = x;
winY = screenHeight - y;
glReadPixels((int)winX, (int)winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ)