Qt助手上说可以混合渲染:
It is also possible to draw 2D graphics onto a QOpenGLWidget subclass using QPainter:
1.In paintGL(), instead of issuing OpenGL commands, construct a QPainter object for use on the widget.
2.Draw primitives using QPainter's member functions.
3.Direct OpenGL commands can still be issued. However, you must make sure these are enclosed by a call to the painter's beginNativePainting() and endNativePainting().
我做了个实验,计划是显示一个三角形和一行文字,结果只能显示三角形,文字显示不出来。我的代码如下:
class Render : public QOpenGLWidget
{
public:
Render();
~Render();
protected:
void initializeGL();
void paintGL();
void resizeGL(int w ,int h);
QOpenGLFunctions_3_3_Core * f;
QOpenGLBuffer * triangle;
QOpenGLVertexArrayObject * vao;
QOpenGLShaderProgram * program;
QMatrix4x4 mv,p;
QTimer * time;
float rota;
QPainter * painter;
bool needInit;
//void paintEvent(QPaintEvent *e);
};
.cpp
#include "render.h"
GLfloat tri[] =
{
0.0f,1.0f,-1.0f,1.0f,
1.0f,1.0f,-1.0f,1.0f,
1.0f,0.0f,-1.0f,1.0f,
};
Render::Render()
{
QSurfaceFormat format;
format.setDepthBufferSize(24);
format.setStencilBufferSize(8);
// format.setVersion(3,3);
//format.setProfile(QSurfaceFormat::CoreProfile);
setFormat( format);
rota = 1.5;
needInit = true;
//setAutoFillBackground(false);
time = new QTimer;
connect(time,SIGNAL(timeout()),this,SLOT(update()));
time->start(50);
}
Render::~Render()
{
}
void Render::initializeGL()
{
f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
f->initializeOpenGLFunctions();
//f = context()->functions();
}
void Render::resizeGL(int w ,int h)
{
p.setToIdentity();
p.perspective(35.0f,float(w)/float(h),1.0f,30.0f);
}
void Render::paintGL()
{
QPainter pntr(this);
// painter->setViewport(50,50,100,100);
pntr.beginNativePainting();
QOpenGLShaderProgram programs ;
programs.addShaderFromSourceCode(QOpenGLShader::Vertex,
"#version 330 core \n\
layout(location = 0) in vec4 vertex;\
uniform mat4 mvp;\
void main() \
{\
gl_Position = mvp * vertex;\
}");
programs.addShaderFromSourceCode(QOpenGLShader::Fragment,
"#version 330 core \n\
out vec4 fragColor;\
void main() \
{ \
fragColor = vec4(0.0f,1.0f,0.0f,1.0f);\
}");
programs.link();
QOpenGLVertexArrayObject vaos;
vaos.create();
f->glBindVertexArray(vaos.objectId());
QOpenGLBuffer triangles(QOpenGLBuffer::VertexBuffer);
triangles.create();
f->glBindBuffer(GL_ARRAY_BUFFER,triangles.bufferId());
triangles.setUsagePattern(QOpenGLBuffer::DynamicDraw);
triangles.allocate(tri,sizeof(tri));
programs.enableAttributeArray(0);
programs.setAttributeBuffer(0,GL_FLOAT,0,4,0);
f->glEnable(GL_DEPTH_TEST);
needInit = false;
// this->makeCurrent();
f->glViewport(0,0,width(),height());
f->glClearColor(1.0f,0.5f,0.5f,1.0f);
f->glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
programs.bind();
mv.setToIdentity();
mv.lookAt(QVector3D(0.0f,0.0f,5.0f),QVector3D(0.0f,0.0f,0.0f),QVector3D(0.0f,1.0f,0.0f));
mv.rotate(0.5+rota,0,1,0);
programs.setUniformValue("mvp",p*mv);
f->glBindVertexArray(vaos.objectId());
f->glDrawArrays(GL_TRIANGLES,0,3);
rota=rota+1.5;
pntr.endNativePainting();
pntr.drawText(50,100,"Look This!");
}
------解决思路----------------------
Qt 5.4这点比较新,全靠你们大力探索了,成功后麻烦写一篇日志。