QOpenGLWidget QPainter :: drawRect只绘制四个箭头

时间:2016-08-26 16:47:46

标签: c++ qt opengl

我现在正在尝试对我创建的QOpenGLWidget进行文本/图形叠加。正如您所看到的那样,3D已经非常紧凑但使用QPainter的2D覆盖不能正常工作。使用QPainter的drawRect函数,我希望看到一个矩形。相反,我得到了四支箭。

QOpenGLWidget with arrows instead of a rectangle

相关代码如下:

的main.cpp

QApplication a(argc, argv);

QSurfaceFormat format;
format.setProfile(QSurfaceFormat::OpenGLContextProfile::CompatibilityProfile);
QSurfaceFormat::setDefaultFormat(format);

HSIOpenGLQuadViewWidget qvwidget;
qvwidget.setFormat(format);
qvwidget.init();
qvwidget.addPoint(QVector3D(-1,  1,  1), Qt::red);
qvwidget.addPoint(QVector3D( 1,  1,  1), Qt::red);
qvwidget.addPoint(QVector3D( 1, -1,  1), Qt::red);
qvwidget.addPoint(QVector3D(-1, -1,  1), Qt::red);
qvwidget.addPoint(QVector3D( 0,  0,  0), Qt::red);
qvwidget.addPoint(QVector3D(-1,  1, -1), Qt::red);
qvwidget.addPoint(QVector3D( 1,  1, -1), Qt::red);
qvwidget.addPoint(QVector3D( 1, -1, -1), Qt::red);
qvwidget.addPoint(QVector3D(-1, -1, -1), Qt::red);
qvwidget.generateSphere(0.5);
qvwidget.show();

return a.exec();

MyQOpenGLWidget.cpp

void HSIOpenGLQuadViewWidget::initializeGL()
{
// Required by Qt
initializeOpenGLFunctions();

// Creates a 24 bit depth mask
glRenderbufferStorage(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width(), height());

// Sets the OpenGL screen clear color
glClearColor(0, 0, 0, 0);

glEnable(GL_POLYGON_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);

// Enables face culling
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glColorMaterial(GL_FRONT, GL_SPECULAR);

// Somewhere in the initialization part of your program…
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glShadeModel(GL_SMOOTH);

// Create light components
//GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat ambientLight[] = { 2.0f, 2.0f, 2.0f, 1.0f };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat position[] = { 0.0f, -10.0f, 0.0f, 1.0f };

// Assign created components to GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, position);

//glDisable(GL_DEPTH_TEST);
glEnable(GL_PROGRAM_POINT_SIZE);
glPointSize(5);
}

void HSIOpenGLQuadViewWidget::paintGL()
{
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

// Set the depth test and clear the depth field
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepth(1.0);
glDepthFunc(GL_LEQUAL);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

drawPoints();

drawSphere();

glPopMatrix();

QPainter painter(this);

QRect rect = this->rect();

float top = rect.y() + (rect.height() * 0.8);
float height = rect.height() * 0.15;
float left = rect.x() + (rect.width() * 0.8);
float width = rect.width() * 0.15;

QPen pen;
pen.setColor(Qt::red);
pen.setWidth(10);
painter.setPen(pen);
painter.setBrush(QBrush(Qt::red));
painter.drawRect(left, top, width, height);

QString textYaw = QString("  Yaw : ") + QString::number(100) + QString("\n");
QString textPitch = QString("Pitch : ") + QString::number(200) + QString("\n");
QString textRoll = QString(" Roll : ") + QString::number(300) + QString("\n");

painter.drawText(left, top + 10, textYaw);
painter.drawText(left, top + 25, textPitch);
painter.drawText(left, top + 40, textRoll);
}

所以,问题是,是什么导致QOpenGLWidget绘制四个箭头而不是一个纯红色矩形?有人有线索吗?感谢。

1 个答案:

答案 0 :(得分:1)

this answer。你需要首先开始一个画家,然后告诉它有关本土绘画,推动状态,使用GL调用进行本地绘画,弹出状态,结束本地绘画,最后使用画家绘制。