QT 5 mixed opengl and qml problem



  • Hi all,

    i want to mix opengl and QML stuff. I used qt 5 beta 2.
    I make a minimalist programm for show you the problem :

    main.ccp
    @
    #include <QGuiApplication>
    #include "back.h"
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Back b;
    b.setSource(QUrl("ui.qml"));
    b.show();

    return a.exec&#40;&#41;;
    

    }
    @

    back.h

    @
    #ifndef BACK_H
    #define BACK_H
    #include <QtQuick>
    #include <QtOpenGL>
    #include <QTimer>

    class Back : public QQuickView
    {
    Q_OBJECT
    public:
    ~Back();
    Back();
    public slots :
    void paint();
    };

    #endif // BACK_H
    @

    back.ccp

    @#include "back.h"

    Back::Back()
    {
    setClearBeforeRendering(false);
    connect(this, SIGNAL(beforeRendering()), this, SLOT(paint()), Qt::DirectConnection);
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()), Qt::DirectConnection);
    timer->start(3000);
    }

    Back::~Back()
    {
    }

    void Back::paint()
    {
    glViewport(0, 0, 150, 150);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 150, 150,0,0,10);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glColor3ub(255, 0, 0);
    glBegin(GL_QUADS);
        glVertex2i(0, 0);
        glVertex2i(100, 0);
        glVertex2i(100, 100);
        glVertex2i(0, 100);
    glEnd();
    

    }
    @

    ui.qml

    @
    import QtQuick 2.0

    Item {
    width : 150
    height : 150

    Rectangle {
         x : 50
         y : 50
         width: 100
         height: 100
         color: "green"
    }
    

    }
    @

    The first picture is correct :
    I have a red square with a green square with offset and a black background.
    "Picture 1":http://imageshack.us/photo/my-images/854/14898400.png

    After 3 seconds, the second is uncorrect :
    I have only the green square with withe background
    "Picture 2":http://imageshack.us/photo/my-images/690/56801100.png

    Is someone know what i make wrong ?

    Thank you

    matteli



  • Ok i inspect the problem and change a little my program.
    My new minimalist program.

    main.ccp
    @#include <QGuiApplication>
    #include "back.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Back b;
    b.setSource(QUrl("ui.qml"));
    b.show();

    return a.exec&#40;&#41;;
    

    }
    @

    back.h
    @#ifndef BACK_H
    #define BACK_H
    #include <QtQuick>
    #include <QtOpenGL>
    #include <QTimer>
    #include <QColor>

    class Back : public QQuickView
    {
    Q_OBJECT
    public:
    ~Back();
    Back();
    public slots :
    void paint();
    private :
    bool mColor;
    };

    #endif // BACK_H
    @

    back.ccp
    @
    #include "back.h"

    Back::Back()
    {
    setClearBeforeRendering(false);
    connect(this, SIGNAL(beforeRendering()), this, SLOT(paint()), Qt::DirectConnection);
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(update()), Qt::DirectConnection);
    timer->start(1000);
    mColor = true;
    }

    Back::~Back()
    {
    }

    void Back::paint()
    {
    glViewport(0, 0, 300, 300);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0f, 300.0f, 300.0f,0.0f,0.0f,1.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
    glDisable(GL_DEPTH_TEST);
    
    glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    
    
    mColor=!mColor;
    if (mColor) glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
    else glColor4f(1.0f, 1.0f, 0.0f,1.0f);
    glBegin(GL_QUADS);
        glVertex2f(100.0f, 100.0f);
        glVertex2f(200.0f, 100.0f);
        glVertex2f(200.0f, 200.0f);
        glVertex2f(100.0f, 200.0f);
    glEnd();
    

    }
    @

    ui.qml
    @
    import QtQuick 2.0

    Item {
    width : 300
    height : 300

    Rectangle {
         x : 0
         y : 0
         width: 50
         height: 50
         color: "purple"
    }
    

    }
    @

    The result is good for the 1st picture
    "Picture 1":http://imageshack.us/photo/my-images/694/picture11psi.png/

    After 1 second, the 2nd picture is not good (black square)
    "Picture 2":http://imageshack.us/photo/my-images/72/picture12mr.png/

    Now i change only the qml file (x and y) :
    @
    import QtQuick 2.0

    Item {
    width : 300
    height : 300

    Rectangle {
         x : 50
         y : 50
         width: 50
         height: 50
         color: "purple"
    }
    

    }
    @

    1st picture good
    "picture 1":http://imageshack.us/photo/my-images/571/picture21t.png/

    2nd picture bad : square in black and with an offset.
    "picture 2":http://imageshack.us/photo/my-images/820/picture22dh.png/

    Do you think it's a bug af QT 5 ?



  • In my example, i mixed old rendering opengl stuff with qquick item which used VBO i presume. I think, it's not a good idea to mixed that. So now i used VBO for my rendering and it's work.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.