void NightModeGraphicsEffect::draw(QPainter* painter) { int pixelRatio = painter->device()->devicePixelRatio(); QSize size(painter->device()->width() * pixelRatio, painter->device()->height() * pixelRatio); if (fbo && fbo->size() != size) { delete fbo; fbo = NULL; } if (!fbo) { QGLFramebufferObjectFormat format; format.setAttachment(QGLFramebufferObject::CombinedDepthStencil); format.setInternalTextureFormat(GL_RGBA); fbo = new NightModeGraphicsEffectFbo(size, format, pixelRatio); } QPainter fboPainter(fbo); drawSource(&fboPainter); painter->save(); painter->beginNativePainting(); program->bind(); const GLfloat pos[] = {-1, -1, +1, -1, -1, +1, +1, +1}; const GLfloat texCoord[] = {0, 0, 1, 0, 0, 1, 1, 1}; program->setUniformValue(vars.source, 0); program->setAttributeArray(vars.pos, pos, 2); program->setAttributeArray(vars.texCoord, texCoord, 2); program->enableAttributeArray(vars.pos); program->enableAttributeArray(vars.texCoord); glBindTexture(GL_TEXTURE_2D, fbo->texture()); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); program->release(); painter->endNativePainting(); painter->restore(); }
void CubeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { // Render the inner scene into a framebuffer object. // We do this while the ordinary Qt paint engine has // control of the GL context rather than later when the // QGLPainter has control of the GL context. if (mScene) { if (!fbo) { QGLFramebufferObjectFormat format; format.setAttachment(QGLFramebufferObject::CombinedDepthStencil); //format.setSamples(8); fbo = new QGLFramebufferObject (mScene->sceneRect().size().toSize(), format); } QRectF rect(0, 0, fbo->size().width(), fbo->size().height()); QPainter fboPainter(fbo); fboPainter.save(); QLinearGradient gradient(rect.topLeft(), rect.bottomRight()); gradient.setColorAt(0, QColor(0, 128, 192, 255)); gradient.setColorAt(1, QColor(0, 0, 128, 255)); fboPainter.setPen(QPen(Qt::black, 3)); fboPainter.setBrush(gradient); fboPainter.drawRect(rect); fboPainter.restore(); mScene->render(&fboPainter, rect); } // Now render the GL parts of the item using QGLPainter. QGLGraphicsViewportItem::paint(painter, option, widget); }
void QwtPlotGLCanvas::paintGL() { const bool hasFocusIndicator = hasFocus() && focusIndicator() == CanvasFocusIndicator; QPainter painter; #if QT_VERSION < 0x040600 painter.begin( this ); draw( &painter ); #else if ( testPaintAttribute( QwtPlotGLCanvas::BackingStore ) ) { if ( d_data->fbo == NULL || d_data->fbo->size() != size() ) { invalidateBackingStore(); const int numSamples = 16; QGLFramebufferObjectFormat format; format.setSamples( numSamples ); format.setAttachment(QGLFramebufferObject::CombinedDepthStencil); QGLFramebufferObject fbo( size(), format ); QPainter fboPainter( &fbo ); draw( &fboPainter); fboPainter.end(); d_data->fbo = new QGLFramebufferObject( size() ); QRect rect(0, 0, width(), height()); QGLFramebufferObject::blitFramebuffer(d_data->fbo, rect, &fbo, rect); } // drawTexture( QRectF( -1.0, 1.0, 2.0, -2.0 ), d_data->fbo->texture() ); if ( hasFocusIndicator ) painter.begin( this ); glBindTexture(GL_TEXTURE_2D, d_data->fbo->texture()); glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f); glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0f, -1.0f); glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f); glEnd(); } else { painter.begin( this ); draw( &painter ); } #endif if ( hasFocus() && focusIndicator() == CanvasFocusIndicator ) drawFocusIndicator( &painter ); }