update() function is not working in qopenlwidget properly
-
I am new in qt platform. Here i have added my code. I trying to use mouse functionalities. I have added Slot and signal in my code. In slot, i have added update() function to update while i change something. Whenever i use update() function, the qopenglwidget window do not show anything which is drawn .
MyOpenglWidget::MyOpenglWidget(QWidget *parent) : QOpenGLWidget(parent) { setFormat(QSurfaceFormat::defaultFormat()); } void MyOpenglWidget::initializeGL() { initializeOpenGLFunctions(); // obvious buffer = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer); buffer.setUsagePattern(QOpenGLBuffer::StaticDraw); Q_ASSERT(buffer.create()); Q_ASSERT(buffer.bind()); shaderProg.addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource); shaderProg.addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource); Q_ASSERT(shaderProg.link()); m_MVPMatrixLoc = shaderProg.uniformLocation("MVP"); Q_ASSERT(shaderProg.bind()); const int vPosition = 0; glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(vPosition); } void MyOpenglWidget::paintGL() { QTextStream cout(stdout); glClear(GL_COLOR_BUFFER_BIT); glClearColor(1.0, 1.0, 1.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); shaderProg.enableAttributeArray("vPosition"); modelMatrix.setToIdentity(); mvp = projectionMatrix * viewMatrix * modelMatrix; shaderProg.setUniformValue(m_MVPMatrixLoc, mvp); viewMatrix.lookAt( QVector3D(x, y, z), // Camera is at (x,y,z), in World Space QVector3D(x, y, 0), // and looks at (x,y) QVector3D(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down) ); for(unsigned int i=0;i<vec.size();i++){ for(unsigned int j=0;j<vec[i].size();j++){ buffer.allocate( vec[i].size() *sizeof (GLfloat)); buffer.write(0, &vec[i][0], vec[i].size() *sizeof (GLfloat)); glDrawArrays(GL_LINE_STRIP, 0, vec[i].size()/3); //cout<<i<<" "<<j<<" "<<vec[i].size()<<" \n"; } } glFlush(); } void MyOpenglWidget::resizeGL(int w, int h) { Q_UNUSED(w); Q_UNUSED(h); glViewport(0, 0, (GLint)width(), (GLint)height()); projectionMatrix.perspective(45.0f, (float)w/(float)h, z, z/1000); } void MyOpenglWidget::mousePressEvent(QMouseEvent *event) { m_lastPos = event->pos(); } void MyOpenglWidget::mouseMoveEvent(QMouseEvent *event) { int dx = event->position().x() - m_lastPos.x(); int dy = event->position().y() - m_lastPos.y(); if (event->buttons() & Qt::LeftButton) { setZRotation(m_zRot + 8 * dx); } m_lastPos = event->pos(); } void MyOpenglWidget::setZRotation(int angle) // slot { qNormalizeAngle(angle); if (angle != m_zRot) { m_zRot = angle; emit zRotationChanged(angle); update(); qDebug()<< m_zRot; } }
-
Hi,
You should take a look at the Hello GL 2 example to get started.