我只是喜欢这个链接:source=tuicool
我做的链接say.But,我不能得到正确的result.Only有背景颜色。我的代码有什么问题?
我的代码:
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();
}发布于 2014-12-10 14:09:50
当我需要这样做时,我使用了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
}发布于 2015-01-07 21:38:48
我也有同样的问题,在使用QPainter之前可以通过释放顶点缓冲区来解决这个问题。因此,在初始化QPainter之前,您可能需要尝试vao->release();try >release();program->release();。
发布于 2015-05-11 15:33:54
这就是在"QOpenGLWidget“中对我起作用的地方。我不需要将OpenGL代码放在"beginNativePainting()“和"beginNativePainting()”Qpainter函数调用之间。我不需要覆盖paintEvent .这与您共享的链接中的内容类似。
1)在"QOpenGLWidget“的构造函数中,将格式设置为"CompatibilityProfile”,例如:
QSurfaceFormat format;
format.setProfile(QSurfaceFormat::CompatibilityProfile);
QSurfaceFormat::setDefaultFormat(format);
this->setFormat(format);2)在游戏循环函数中,确保在您完成OpenGL代码之后,在调用您的Q画家并在函数的末尾使用它之前,释放程序VAO、VBO和& 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();https://stackoverflow.com/questions/27403036
复制相似问题