Here is my program, I want to call OpenGL functions to draw a triangle,and then QPainter to draw a string. My widget code is :
class CMyTestOpenGLWidget : public QOpenGLWidget,protected QOpenGLFunctions_4_3_Core
{
public:
CMyTestOpenGLWidget(QWidget* parent) : QOpenGLWidget(parent) {}
void initializeGL() override
{
initializeOpenGLFunctions();
ShaderInfo renderShaders[] = { {GL_VERTEX_SHADER,":shaders/shape.vert"},{GL_FRAGMENT_SHADER,":shaders/shape.frag"},{GL_NONE,NULL} };
program = loadShaders(renderShaders);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
double triangle[] = {
10,10,
90,10,
40,90
};
glGenBuffers(1,&vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
int nByte = sizeof(triangle);
glBufferData(GL_ARRAY_BUFFER,nByte,triangle,GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 2, GL_DOUBLE, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
void paintGL() override
{
mat4 model(1.0),view(1.0),pro(1.0);
pro = glm::ortho(0.0f,100.0f,0.0f,100.0f,0.0f, 10.0f);
pro = pro * view * model;
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
GLint loc = glGetUniformLocation(program,"pvm");
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(pro));
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES,0,3);
glBindVertexArray(0);
QPainter p(this);
p.setPen(Qt::red);
p.drawText(QRect(0,0,50,100),"Test");
}
void resizeGL(int w, int h) override
{
glViewport(0, 0, w, h);
}
private:
uint loadShaders(ShaderInfo* shaders){...}
private:
GLuint vao,
vbo,
program;
};
This program works, but I have to call glBindVertexArray(0); before drawing with QPainter. If I comment it out, there will be nothing but a black screen. Does the previous VAO bound to OpenGL affect latter drawing calls?