Git Product home page Git Product logo

qt3d-offscreenrenderer's Introduction

Qt3D-OffscreenRenderer


Note: If you want to use the offscreen renderer in your project to embedd Qt3D without having to use Qt3DWindow, checkout my implementation of a Qt3DWidget. It renders everything offline and uses the offscreen texture to paint a quad in a QOpenGLWidget.


This project is based on the Qt 3D basic shapes C++ example (https://doc.qt.io/qt-5.10/qt3d-basicshapes-cpp-example.html).

It renders the whole scene using a QOffscreenSurface offline and displays the image in a QLabel. To get the image to resize adjust the OffscreenEngines rendering size on resize of the widget. I guess the rest should be self-explanatory.

This is supposed to show how to construct an offline framegraph and save everyone who needs such a renderer the hours of trial and error and the despair I encountered.

Qt version

Make sure to use Qt >= 5.9.2 due to a bug in earlier versions: https://bugreports.qt.io/browse/QTBUG-61382

The framegraph

                            root
                             |
                        renderSettings
                             |
                     renderSurfaceSelector
                             |
                      renderTargetSelector
                             |
         ---------------------------------------
         |                   |                 |
    renderTarget        clearBuffers        viewport
         |                                     |
 renderTargetOutput                      cameraSelector
         |
     texture2D

Note

Somehow the first image that is captured is blank and without rendered objects. I suspected the problem to be that the render capture was triggered right after the clear buffers node. But it doesn't seem to have any effect when we place the capture node anywhere else. So basically the first image has to be skipped.

To enable depth tests just add a QRenderStateSet as a child to the camera selector and a QDepthTest as a child to the render state set and add it as a state. Then, in the render target add a texture to the depth attatchment point. This is it.

I experiemented with multisampling, and added a QMultiSampleAntiAliasing render state to the render state set and instead of adding a QTexture2D used a QMultisampleTexture2D but it gave me version errors with OpenGL. If maybe someone gets it running they can fork the project and I can then do a pull.

Disclaimer

Please note that part of the code was written by the Qt Company or KDAB and they hold the respective copyright. For further information read the copyright notice in the source code.

qt3d-offscreenrenderer's People

Contributors

florianblume avatar wonder-sk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

qt3d-offscreenrenderer's Issues

Doesn't display correctly

Hi, i'm trying to run your code, just replace MainWidget::MainWidget(QWidget *parent) like this:

MainWidget::MainWidget(QWidget *parent) : QWidget(parent)
{
    setWindowTitle(QStringLiteral("Basic shapes"));

    QLabel *graphicsLabel = new QLabel();
    graphicsLabel->setGeometry(QRect(0, 0, 400, 400));
    graphicsLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    QPushButton *genButton = new QPushButton();
    genButton->setText("Generate");
    genButton->setMaximumWidth(100);

    QHBoxLayout *hLayout = new QHBoxLayout(this);
    hLayout->addWidget(graphicsLabel, 1);
    hLayout->addWidget(genButton);

    Qt3DCore::QEntity *rootEntity = new  Qt3DCore::QEntity();

    Qt3DRender::QCamera *cameraEntity = new Qt3DRender::QCamera(rootEntity);
    cameraEntity->lens()->setPerspectiveProjection(45.0f, 1.0, 0.1f, 1000.0f);
    cameraEntity->setPosition(QVector3D(0, 20.0, 0));
    cameraEntity->setViewCenter(QVector3D(0, 0, 0));

    // Set up a light to illuminate the shapes.
    Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(rootEntity);
    Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);
    light->setColor("white");
    light->setIntensity(1);
    lightEntity->addComponent(light);
    Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity);
    lightTransform->setTranslation(cameraEntity->position());
    lightEntity->addComponent(lightTransform);
    // Plane
    Qt3DExtras::QPlaneMesh *plane = new Qt3DExtras::QPlaneMesh();
    plane->setWidth(2.0);
    plane->setHeight(2.0);

    Qt3DCore::QTransform *planeTr = new Qt3DCore::QTransform();
    planeTr->setScale(5.0);
    planeTr->setRotationX(19);
    planeTr->setRotationY(13);
    planeTr->setRotationZ(10);
    // Material
    Qt3DExtras::QPhongMaterial *mat = new Qt3DExtras::QPhongMaterial();
    mat->setDiffuse(Qt::green);
    // Plane ent
    Qt3DCore::QEntity *planeEnt = new  Qt3DCore::QEntity(rootEntity);
    planeEnt->addComponent(plane);
    planeEnt->addComponent(planeTr);
    planeEnt->addComponent(mat);

    // Create the offscreen engine. This is the object which is responsible for handling the 3D scene itself.
    offscreenEngine = new OffscreenEngine(cameraEntity, QSize(1200, 800));

    // The offscreen engine delegate handles requesting frames from the engine, and drawing their results in the graphics label.
    offscreenEngineDelegate = new OffscreenEngineDelegate(offscreenEngine, graphicsLabel);

    // Set our scene to be rendered by the offscreen engine.
    offscreenEngine->setSceneRoot(rootEntity);
}

And i get big green strip over the plane image:
image
It's doesn't work and with other rotations of plane. Can you help please? I've spent a lot of time and did't find what's wrong.

How to get if rendering is complete ?

First of all let me thank you for your wonderfull work. I needed some engine to generate 2D shots of 3D scene in offscreen mode. This is being done for pupose to generate some synthetic images for CNN training. Your render passes for this purpose perfect. But there is one question: how to get the rendering is complete before to do a capture ?
For example if to modify code of OffscreenEngineDelegate.cpp for a single image capture with delay like as follows:

OffscreenEngineDelegate::OffscreenEngineDelegate(OffscreenEngine *engine, QLabel *label) :
    engine(engine),
    label(label),
    reply(nullptr)
{
    auto captureDelay = 20;
    QTimer::singleShot(captureDelay, this,[this](){requestRenderCapture();});
}

OffscreenEngineDelegate::~OffscreenEngineDelegate()
{
    delete reply;
}

void OffscreenEngineDelegate::requestRenderCapture()
{
    delete reply;
    reply = engine->getRenderCapture()->requestCapture();
    connect(reply, SIGNAL(completed()), this, SLOT(onImageRendered()));
}

void OffscreenEngineDelegate::onImageRendered()
{
    label->setPixmap(QPixmap::fromImage(reply->image()));
}

the program will capture what is on screen in 20 msec (auto captureDelay = 20;). And on my PC 20 msec is enough to get the image, if this value is less than 20 msec I get image with background only. With more slower hardware such delay has to be bigger as I checked.
This situation rises a question when one may do the capture ? or, in other words, how to get if the rendering is completed ?

Crash on shutdown (Linux and Mac)

On Linux, when shutdown crash occurs, this text can be found in output:
Xlib: sequence lost (0x10675 > 0x67b) in reply type 0x23!
Xlib: sequence lost (0x11b93 > 0x1b95) in reply type 0x23!
Xlib: sequence lost (0x148f4 > 0x48fa) in reply type 0x23!
Segmentation fault (core dumped)

On Mac, it simply segfaults at shutdown.

Qt6 Graph issue incomplete rendering

In order the frame graph should work correctly, make the render capture node as a the last child. In this example it must be the child of cameraSelector.
Otherwise the rendering can be incomplete and the rendering produces strange artefacts.

Frame rate limits ?

Hi @Sonnentierchen,
thank you for your implementation. you did save me a lot of time !

I am experiencing differences in frame rate (smoothness of changes in the image) when I compare the display inside the offscreen renderer and the basic view of Qt3DWindow.
I would like to know if there is any limitations in your approach that makes it impossible to be as fast as Qt3DWindow.

I am new to 3D rendering and I am having trouble understanding why we can't, somehow, just get directly the image shown on the screen at the speed the renderer renders the 2D view of the scene.

Thanks again for sharing this =)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.