Offscreen rendering in Qt, from Irrlich frames to QImage?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
hipersayan_x
Posts: 3
Joined: Thu Apr 07, 2011 2:04 pm
Contact:

Offscreen rendering in Qt, from Irrlich frames to QImage?

Post by hipersayan_x »

Hello everyone, Im working on integrating Irrlicht to my Qt/Qml proyect.
I want to send all Irrlicht frames to a Qml item, this Qml item, receives a QImage as argument, the QImage is fixed size always, the arguments of QImage are:

QImage(unsinned char * image_pixels, int width, int height, int bytesPerLine, Format format)

I found some examples of integrating Irrlicht and Qt, but always drawing the scene en the window background.

http://www.matrix44.net/blog/?p=389

This is the code Im using to test this feature, but without Qml, I'm sending the frames to a QLabel.

Code: Select all

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include <irrlicht/irrlicht.h>

using namespace irr;
using namespace irr::video;
using namespace irr::core;
using namespace irr::scene;

namespace Ui
{
    class MainWindow;
}

class MainWindow: public QMainWindow
{
    Q_OBJECT

    public:
        // This is the widget Im using to draw my offscreen scene.
        QWidget offscreen;

        QTimer timer;
        IrrlichtDevice *device;
        IVideoDriver *driver;
        ISceneManager *smgr;
        ITexture *offscreen_texture;
        ICameraSceneNode* user_eyes;

        IAnimatedMesh *mesh;
        IAnimatedMeshSceneNode *node;

        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();

    private:
        Ui::MainWindow *ui;

    public slots:
        void show_frame();
};

#endif // MAINWINDOW_H

Code: Select all

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtDebug>

MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    offscreen.resize(640, 480);

    // If offscreen is not visible the scene is not rendered, but this must be hiden :(
    offscreen.show();

    SIrrlichtCreationParameters createParams;

    createParams.WindowId = (void *)offscreen.winId();
    createParams.DriverType = EDT_OPENGL;

    device = createDeviceEx(createParams);

    driver = device->getVideoDriver();
    smgr = device->getSceneManager();

    // Setup some nodes for the test.
    mesh = smgr->getMesh("media/sydney.md2");
    node = smgr->addAnimatedMeshSceneNode( mesh );
    node->setMaterialFlag(EMF_LIGHTING, false);
    node->setMD2Animation(scene::EMAT_STAND);
    node->setMaterialTexture( 0, driver->getTexture("media/sydney.bmp") );

    // Setup user view.
    user_eyes = smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));

    // The scene is rendered every 1 msec.
    timer.setInterval(1);
    connect(&timer, SIGNAL(timeout()), this, SLOT(show_frame()));
    timer.start();
}

MainWindow::~MainWindow()
{
    device->drop();
    delete ui;
}

void MainWindow::show_frame()
{
    driver->beginScene(true, false, SColor(255, 127, 127, 191));
    smgr->setActiveCamera(user_eyes);

    smgr->drawAll();
    driver->endScene();

    // Here I'm convert from IImage to QImage;
    IImage *image = driver->createScreenShot();
    QImage qimage((uchar *)image->lock(), offscreen.width(), offscreen.height(), 3 * offscreen.width(), QImage::Format_RGB888);

    // Then it is showed in QLabel
    QPixmap pixmap;
    pixmap.convertFromImage(qimage);
    ui->lblScene->setPixmap(pixmap);
}
The problem is that if the offscreen widget is not visible, then the scene is not rendered :(
Then, How I can create QImage from Irrlicht frames?
Thanks in advance.
kalvinorama
Posts: 10
Joined: Sat Jul 19, 2008 12:27 pm

Post by kalvinorama »

if i remember, you can use a QFramebufferObject.
hipersayan_x
Posts: 3
Joined: Thu Apr 07, 2011 2:04 pm
Contact:

Post by hipersayan_x »

QFramebufferObject doesn't exist in 4.7 series, the available class is QGLFramebufferObject, and I found this example, but only show OpenGL commands, I don't understand how to connect QGLFramebufferObject with Irrlicht.
Thank for you quick response.
hipersayan_x
Posts: 3
Joined: Thu Apr 07, 2011 2:04 pm
Contact:

Post by hipersayan_x »

I don't understand how to do it, It's possible to use Irrlicht for render in a buffer without connecting to or creating a window?
I'm noob in graphics engines :?
Post Reply