gpt4 book ai didi

c++ - QOpenGLWidget QPainter::drawRect 只绘制四个箭头

转载 作者:行者123 更新时间:2023-11-30 05:23:28 25 4
gpt4 key购买 nike

我现在正在尝试对我创建的 QOpenGLWidget 进行文本/图形叠加。如您所见,我已经将 3D 压缩得很紧,但是使用 QPainter 的 2D 叠加层无法正常工作。使用 QPainter 的 drawRect 函数,我希望看到一个矩形。相反,我得到了四个箭头。

QOpenGLWidget with arrows instead of a rectangle

相关代码如下:

主要.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 绘制四个箭头而不是实心红色矩形?有人有线索吗?谢谢。

最佳答案

参见 this answer .你需要先启动一个画家,然后通知它原生绘画,推送状态,使用 GL 调用进行原生绘画,弹出状态,结束原生绘画,最后使用画家绘制。

关于c++ - QOpenGLWidget QPainter::drawRect 只绘制四个箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39171120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com