OpenGl中的QPainter中断坐标系统

时间:2018-10-16 13:15:01

标签: c++ qt opengl

我想通过单击选择对象上的点

成功实现

this->camera()->convertClickToLine(point, orig, dir);
bool found;
selectedPoint = this->camera()->pointUnderPixel(point, found);

if (selectedName() >= 0) {
glColor3f(0.9f, 0.2f, 0.1f);
glBegin(GL_POINTS);
glVertex3f(selectedPoint.x, selectedPoint.y, selectedPoint.z);
glEnd();
}

可选对象示例:

glBegin(GL_TRIANGLES);
glColor3f(0.5,0,0);
glVertex3f(xmin,ymin,zmin);
glVertex3f(xmin + (xmax-xmin)/2,ymin+(ymax-ymin)/2, zmin+(zmax-zmin)/2);
glVertex3f(xmax,ymin,zmin);
glEnd();

但是,如果我开始使用QPainter,selectedPoint会将坐标更改为错误

QPainter painter(this);
painter.setPen(Qt::black);
painter.setFont(QFont("Helvetica", 8));
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.drawText(textPosX + 10, textPosY, text);
painter.setBrush(QBrush(Qt::black, Qt::SolidPattern));
painter.drawEllipse(QPoint(textPosX, textPosY), 2, 2);
painter.end();

我该怎么办?

我的步骤:

  1. 我通过OpenGl绘制框和对象

  2. 我通过Qpainter绘制对象名称

  3. 我通过单击在对象上画点(由于前一项无效,如果我评论第2项,则一切正常)

1 个答案:

答案 0 :(得分:0)

解决

glPushAttrib(GL_ALL_ATTRIB_BITS);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();

/ QPainter /

glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();