QOpenGLWidget显示黑屏 [英] QOpenGLWidget show black screen
问题描述
我尝试了此处描述的QOpenGLWidget示例: https://stackoverflow.com/a/31524956/4564882
I tried the QOpenGLWidget example described here: https://stackoverflow.com/a/31524956/4564882
但是我只有一个黑色的小部件.代码完全相同.这是与QopenGLWidget相关的代码:
but I get only a black widget. The code is exactly the same. this the code associated to the QopenGLWidget:
OGLWidget::OGLWidget(QWidget *parent)
: QOpenGLWidget(parent)
{
}
OGLWidget::~OGLWidget()
{
}
void OGLWidget::initializeGL()
{
glClearColor(0,0,0,1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
}
void OGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.5, -0.5, 0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 0.5, -0.5, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f( 0.0, 0.5, 0);
glEnd();
}
void OGLWidget::resizeGL(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (float)w/h, 0.01, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,5,0,0,0,0,1,0);
}
我在这里尝试了该示例: https://doc .qt.io/archives/qt-5.3/qtopengl-2dpainting-example.html .效果很好(尝试使用两个基类:QGLWidget和QOpenGLWidget.这是与Widget关联的代码:
I tried the example here: https://doc.qt.io/archives/qt-5.3/qtopengl-2dpainting-example.html. It works fine (trying the both base class: QGLWidget and QOpenGLWidget. this is the code associated to the Widget:
GLWidget::GLWidget(Helper *helper, QWidget *parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent), helper(helper)
{
elapsed = 0;
setFixedSize(200, 200);
setAutoFillBackground(false);
}
void GLWidget::animate()
{
elapsed = (elapsed + qobject_cast<QTimer*>(sender())->interval()) % 1000;
repaint();
}
void GLWidget::paintEvent(QPaintEvent *event)
{
QPainter painter;
painter.begin(this);
painter.setRenderHint(QPainter::Antialiasing);
helper->paint(&painter, event, elapsed);
painter.end();
}
我使用在我的机器上构建的Qt 5.5.1 binairies.我默认使用Build Configuration,因此它基于Qt ANGLE而不是Desktop OpenGL. 这样的行为有什么问题?
I use Qt 5.5.1 binairies built on my machine. I let the Build Configuration by default, so it is based on Qt ANGLE not Desktop OpenGL. What is the problem of such a behaviour?
推荐答案
问题是因为我使用默认配置构建的Qt5二进制文件. Qt 5.5中的默认值是动态" GL-两个角度(ES2)
The problem was because I use Qt5 binaries built with the default configuration. The default in Qt 5.5 is "dynamic" GL -- both ANGLE (ES2)
ANGLE((Almost Native Graphics Layer Engine)是由 谷歌.其目的是将OpenGL ES 2.0 API调用映射到DirectX 9 API.)
ANGLE ((Almost Native Graphics Layer Engine) is an open source project by Google. Its aim is to map OpenGL ES 2.0 API calls to DirectX 9 API.)
和桌面后端(Desktop OpenGL)建立在一起,在运行时决定使用哪个后端.
and Desktop backends (Desktop OpenGL)are built, the decision on which one to use is taken at runtime.
问题在于ANGLE仅支持OpenGL> 3.x,因此我测试的第一个代码已过时,ANGLE不支持.支持第二个,这就是它起作用的原因.
The problem is that ANGLE only supports OpenGL>3.x, so the first code that I test is deprecated and not supported by ANGLE. The second is supported, that's why it worked.
因此,我使用以下方法重建Qt以仅针对桌面OpenGL,以支持我已弃用的代码:
So, I rebuild Qt to target Desktop OpenGL only to support my deprecated code, using:
configure -debug-and-release -opensource -opengl desktop -platform win32-msvc2015
然后运行nmake
,将我的应用程序链接到新的二进制文件,我的代码运行良好!
and then run nmake
, link my application to the new binaries, and my code works well!
这篇关于QOpenGLWidget显示黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!