传入一个(0,0,200,200),把这四个值用上面的函数转换为世界坐标相对应的点,然后就是四个点画矩形了,最后结果是下面的那个图了,画的结果不对,求指教啊void OpenGLWnd::drawGLRect(QRect rect )
{
m_rect = rect;
}
void OpenGLWnd::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-4.0);
glDisable( GL_DEPTH_TEST );
this->ScreenConvert(m_rect.x(), m_rect.y());
GLdouble xPox1 = pos.x;
GLdouble yPox1 = pos.y;
cout<<"1 point"<<endl;
this->ScreenConvert(m_rect.x()+m_rect.width(), m_rect.y());
GLdouble xPox2 = pos.x;
GLdouble yPox2 = pos.y;
cout<<"2 point"<<endl;
this->ScreenConvert(m_rect.x()+m_rect.width(),m_rect.y()+m_rect.height());
GLdouble xPox3 = pos.x;
GLdouble yPox3 = pos.y;
cout<<"3 point"<<endl;
this->ScreenConvert(m_rect.x(), m_rect.y()+m_rect.height());
GLdouble xPox4 = pos.x;
GLdouble yPox4 = pos.y;
cout<<"4 point"<<endl;
glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS);
glVertex2f(xPox1,yPox1);
glVertex2f(xPox2,yPox2);
glVertex2f(xPox3,yPox3);
glVertex2f(xPox4,yPox4);
glEnd();
}
void OpenGLWnd::ScreenConvert(int x, int y)
{
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
//GLdouble posX, posY, posZ;
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
winX = (float)x;
winY = viewport[3] - (float)y;
glReadPixels((int)winX, (int)winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
gluUnProject(winX, winY, winZ, modelview, projection, viewport, &pos.x, &pos.y, &pos.z);
cout<<"posX:"<<pos.x<<endl;
cout<<"posY:"<<pos.y<<endl;
cout<<"posZ:"<<pos.z<<endl;
}
// main 函数, fs是一个bool型,
OpenGLWnd w(0,0,fs);
app.setMainWidget(&w);
QRect rect(0,0,200,200);
w.drawGLRect(rect);
w.show();
{
m_rect = rect;
}
void OpenGLWnd::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0,0.0,-4.0);
glDisable( GL_DEPTH_TEST );
this->ScreenConvert(m_rect.x(), m_rect.y());
GLdouble xPox1 = pos.x;
GLdouble yPox1 = pos.y;
cout<<"1 point"<<endl;
this->ScreenConvert(m_rect.x()+m_rect.width(), m_rect.y());
GLdouble xPox2 = pos.x;
GLdouble yPox2 = pos.y;
cout<<"2 point"<<endl;
this->ScreenConvert(m_rect.x()+m_rect.width(),m_rect.y()+m_rect.height());
GLdouble xPox3 = pos.x;
GLdouble yPox3 = pos.y;
cout<<"3 point"<<endl;
this->ScreenConvert(m_rect.x(), m_rect.y()+m_rect.height());
GLdouble xPox4 = pos.x;
GLdouble yPox4 = pos.y;
cout<<"4 point"<<endl;
glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS);
glVertex2f(xPox1,yPox1);
glVertex2f(xPox2,yPox2);
glVertex2f(xPox3,yPox3);
glVertex2f(xPox4,yPox4);
glEnd();
}
void OpenGLWnd::ScreenConvert(int x, int y)
{
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
//GLdouble posX, posY, posZ;
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
winX = (float)x;
winY = viewport[3] - (float)y;
glReadPixels((int)winX, (int)winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
gluUnProject(winX, winY, winZ, modelview, projection, viewport, &pos.x, &pos.y, &pos.z);
cout<<"posX:"<<pos.x<<endl;
cout<<"posY:"<<pos.y<<endl;
cout<<"posZ:"<<pos.z<<endl;
}
// main 函数, fs是一个bool型,
OpenGLWnd w(0,0,fs);
app.setMainWidget(&w);
QRect rect(0,0,200,200);
w.drawGLRect(rect);
w.show();
请确认所传入的winX, winY, winZ,是否正确