I know this is an old post, but I have the same problem in 2020. I have tried lots of examples from github and other sources. My QQuickFramebufferObject has two problems:
1. it renders on complete window
2. It is flickering on resize.
This is the code:
//================================================================
// Item
//================================================================
OcctViewItem::OcctViewItem(QQuickItem *parent) : QQuickFramebufferObject(parent)
{
setMirrorVertically(true);
}
OcctViewItem::~OcctViewItem() {
}
QQuickFramebufferObject::Renderer * OcctViewItem::createRenderer() const
{
return new OcctFboRenderer();
}
//================================================================
// Renderer
//================================================================
OcctFboRenderer::OcctFboRenderer() {
}
OcctFboRenderer::~OcctFboRenderer()
{
context_.Nullify();
view_.Nullify();
viewer_.Nullify();
window_ = nullptr;
}
void OcctFboRenderer::render()
{
QOpenGLContext::currentContext()->functions()->glUseProgram(0);
if (!view_.IsNull()) {
view_->Redraw();
view_->MustBeResized();
}
window_->resetOpenGLState();
}
QOpenGLFramebufferObject * OcctFboRenderer::createFramebufferObject(const QSize &size)
{
QOpenGLFramebufferObjectFormat format;
//format.setSamples(4);
format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
return new QOpenGLFramebufferObject(size, format);
}
void OcctFboRenderer::synchronize(QQuickFramebufferObject * item)
{
if (!window_) {
window_ = item->window();
window_->setClearBeforeRendering(Standard_False);
}
OcctViewItem * self = static_cast<OcctViewItem*> (item);
// If the viewer is not yet initialized, initialize it.
if (viewer_.IsNull()) {
this->initializeViewer(Aspect_Drawable(window_->winId()), self);
}
// Get the control position and size.
QPoint viewportPos = self->mapToGlobal(QPointF(0, 0)).toPoint();
QSize viewportSize = self->size().toSize();
// Check if the viewPort needs to be resized.
if (viewportPos.x() != viewportPos_.x() || viewportPos.y() != viewportPos_.y()) {
view_->MustBeResized();
}
if (viewportSize.width() != viewportSize_.width() || viewportSize.height() != viewportSize_.height()) {
view_->MustBeResized();
view_->Invalidate();
}
// Store the current pos and size.
viewportPos_ = viewportPos;
viewportSize_ = viewportSize;
}
void OcctFboRenderer::initializeViewer(const Aspect_Drawable& drawable, OcctViewItem * occView)
{
Q_ASSERT(viewer_.IsNull());
// Request device and render context.
auto renderContext = window_->openglContext();
if (drawable == 0 || renderContext == 0)
return;
// Setup display driver.
Handle(Aspect_DisplayConnection) display = new Aspect_DisplayConnection();
Handle(OpenGl_GraphicDriver) driver = new OpenGl_GraphicDriver(display, Standard_False);
// Create and setup the viewer.
viewer_ = new V3d_Viewer(driver);
viewer_->SetDefaultBackgroundColor(Quantity_NOC_GRAY50);
viewer_->SetDefaultLights();
viewer_->SetLightOn();
// Create and setup interactivity context.
context_ = new AIS_InteractiveContext(viewer_);
context_->SetDisplayMode(AIS_Shaded, Standard_True);
// Create and setup window.
Handle(OcctWindow) occtWnd = new OcctWindow(window_);
if (!occtWnd->IsMapped()) {
occtWnd->Map();
}
// Create and setup view.
view_ = viewer_->CreateView();
view_->ChangeRenderingParams().NbMsaaSamples = 4;
view_->ChangeRenderingParams().IsAntialiasingEnabled = Standard_True;
view_->SetImmediateUpdate(Standard_False);
//view_->SetWindow(occtWnd, reinterpret_cast<Aspect_RenderingContext>(renderContext));
view_->SetWindow(occtWnd);
view_->TriedronDisplay(Aspect_TOTP_RIGHT_LOWER, Quantity_NOC_WHITESMOKE, 0.1, V3d_ZBUFFER);
// Create a demo scene.
this->initScene();
}
void OcctFboRenderer::initScene()
{
// Create a bisque cone at [0, 10, 0].
gp_Ax2 axis;
axis.SetLocation(gp_Pnt(0.0, 10.0, 0.0));
TopoDS_Shape bisqueCone = BRepPrimAPI_MakeCone(axis, 3.0, 1.5, 5.0).Shape();
Handle(AIS_Shape) bisqueConeShape = new AIS_Shape(bisqueCone);
bisqueConeShape->SetColor(Quantity_NOC_BISQUE);
// Create a chocolate cone at [8, 10, 0].
axis.SetLocation(gp_Pnt(8.0, 10.0, 0.0));
TopoDS_Shape chocoCone = BRepPrimAPI_MakeCone(axis, 3.0, 0.0, 5.0).Shape();
Handle(AIS_Shape) chocoConeShape = new AIS_Shape(chocoCone);
chocoConeShape->SetColor(Quantity_NOC_CHOCOLATE);
context_->Display(bisqueConeShape, Standard_True);
context_->Display(chocoConeShape, Standard_True);
// Fit all into the view.
view_->FitAll();
}
- First render is black screen: screenshot.png
https://www.opencascade.com/system/files/forum/screenshot.png
- While resizing, flick between screenshot_1.png and screenshot_2.png
https://www.opencascade.com/system/files/forum/screenshot_1.png
https://www.opencascade.com/system/files/forum/screenshot_2.png
Thank you for your help,
Commenter-1.