我按链接说。但是我无法得到正确的结果。只有背景颜色。我的代码有什么不对吗?
我的代码:
class Render : public QOpenGLWindow
{
public:
Render();
~Render();
protected:
void initializeGL();
void paintGL();
void resizeGL(int w ,int h);
QOpenGLFunctions * f;
QOpenGLBuffer * triangle;
QOpenGLVertexArrayObject * vao;
QOpenGLShaderProgram * program;
QMatrix4x4 mv,p;
QTimer * time;
float rota;
//QPainter * painter;
};
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);
time = new QTimer;
connect(time,SIGNAL(timeout()),this,SLOT(update()));
time->start(50);
rota = 0;
}
Render::~Render()
{
}
void Render::initializeGL()
{
// f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
// f->initializeOpenGLFunctions();
f = context()->functions();
program = new QOpenGLShaderProgram;
program->addShaderFromSourceCode(QOpenGLShader::Vertex,
"#version 330 core \n\
layout(location = 0) in vec4 vertex;\
uniform mat4 mvp;\
void main() \
{\
gl_Position = mvp * vertex;\
}");
program->addShaderFromSourceCode(QOpenGLShader::Fragment,
"#version 330 core \n\
out vec4 fragColor;\
void main() \
{ \
fragColor = vec4(0.0f,1.0f,0.0f,1.0f);\
}");
program->link();
vao = new QOpenGLVertexArrayObject;
vao->create();
vao->bind();
triangle = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
triangle->create();
triangle->bind();
triangle->setUsagePattern(QOpenGLBuffer::StaticDraw);
triangle->allocate(tri,sizeof(tri));
program->enableAttributeArray(0);
program->setAttributeBuffer(0,GL_FLOAT,0,4,0);
f->glEnable(GL_DEPTH_TEST);
}
void Render::resizeGL(int w ,int h)
{
p.setToIdentity();
p.perspective(35.0f,float(w)/float(h),1.0f,30.0f);
}
void Render::paintGL()
{
program->bind();
f->glClearColor(0.5,0.5f,0.5f,1.0f);
f->glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
f->glViewport(0,0,width(),height());
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);
program->setUniformValue("mvp",p*mv);
vao->bind();
f->glDrawArrays(GL_TRIANGLES,0,3);
rota=rota+1.5;
rota=rota>360.0?0:rota;
QPainter pp(this);
pp.drawText(10,20,"dsdsds");
update();
}
答案 0 :(得分:0)
当我需要这样做时,我覆盖了paintEvent并使用了QPainter::beginNativePainting ();
/ QPainter::endNativePainting ();
void Render::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.beginNativePainting ();
makeCurrent ();
//restore and save state so they don't interact
glEnable (GL_DEPTH_TEST);
glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
paintGL ();
glClear (GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
glDisable (GL_DEPTH_TEST);
p.endNativePainting ();
//other painting
}
答案 1 :(得分:0)
我有同样的问题,可以通过在使用QPainter之前释放顶点缓冲区来解决它。所以你可能想尝试一下 vao-&GT;释放(); triangle-&GT;释放(); 程序 - &GT;释放(); 在初始化QPainter之前。
答案 2 :(得分:0)
这是我在&#34; QOpenGLWidget&#34;中的作用。我不需要在&#34; beginNativePainting()&#34;之间放置OpenGL代码。和&#34; beginNativePainting()&#34; Qpainter函数调用。我不需要覆盖paintEvent ...这与您共享的链接中的内容类似。
1)在&#34; QOpenGLWidget&#34;的构造函数中将格式设置为&#34; CompatibilityProfile&#34; 例如:
QSurfaceFormat format;
format.setProfile(QSurfaceFormat::CompatibilityProfile);
QSurfaceFormat::setDefaultFormat(format);
this->setFormat(format);
2)在你的游戏循环功能中 确保发布程序,VAO,VBO和&amp;完成OpenGL代码之后,在调用Qpainter并在函数的最后使用它之前的EBO。 例如:
VAO.release();
VBO->release();
EBO->release();
m_program->release();
QPainter painter(this);
painter.begin(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.drawText(10,20,"dsdsds");
painter.end();
答案 3 :(得分:0)
我在Qt 5.8中遇到了同样的问题。解决方案不仅仅是致电
vao->release();
triangle->release();
program->release();
丹尼尔建议,但也要打电话
program->disableAttributeArray(0);
在着色器程序发布之前。
因此,基本上任何在GPU上绑定或启用的东西都需要被禁用或释放。