Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Qt5] QML and OpenGL
Forum Updated to NodeBB v4.3 + New Features

[Qt5] QML and OpenGL

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 4.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Merinas
    wrote on last edited by
    #1

    Hi,

    I'm trying to draw OpenGL content with the new Qt5 architecture. I found one example doing that, the one named Squircle. But it draw on the background. And I want to draw like before (with Qt4 like "here":http://qt-project.org/forums/viewthread/4109 ) in a QQuickItem. So I Tried to use a QuickPaintedItem like I use a QDeclarativeItem. But nothing is draw. Here a my Code :

    PaintedOpenGL.h

    @#ifndef PAINTEDOPENGL_H
    #define PAINTEDOPENGL_H

    #include <QQuickPaintedItem>

    class PaintedOpenGL : public QQuickPaintedItem
    {
    Q_OBJECT
    public:
    explicit PaintedOpenGL(QQuickItem *parent = 0);

    void paint(QPainter *painter);
    

    private:
    int m_prog;
    };

    #endif // PAINTEDOPENGL_H@

    PaintedOpenGL.cpp
    @#include "paintedopengl.h"
    #include <QPainter>
    #include <QtGui/QOpenGLShaderProgram>
    #include <QtGui/QOpenGLContext>

    extern GLuint Init ( GLbyte* vert,GLbyte* frag); //classic function which load, build an link shader

    PaintedOpenGL::PaintedOpenGL(QQuickItem *parent)
    : QQuickPaintedItem(parent)
    , m_prog(0)
    {
    }

    void PaintedOpenGL::paint(QPainter *painter)
    {
    painter->beginNativePainting();
    if(m_prog == 0)
    {
    GLbyte vShader[] =
    "attribute vec4 vPosition; \n"
    "uniform mat4 matrix; \n"
    "void main() \n"
    "{ \n"
    " gl_Position = matrix * vPosition; \n"
    "} \n";

        GLbyte fShader[] =
                "precision mediump float;                     \n"
                "void main()                                  \n"
                "{                                            \n"
                "  gl_FragColor = vec4 ( 0.0, 1.0, 0.0, 1.0 );\n"
                "}                                            \n";
    
        m_prog = Init(vShader,fShader);
        glBindAttribLocation ( m_prog, 0, "vPosition" );
    
    }
    glUseProgram ( m_prog );
    glEnableVertexAttribArray(0);
    
    GLfloat tab_matrix[] = { 1.0 , 0.0 , 0.0 , 0.0 ,
                             0.0 , 1.0 , 0.0 , 0.0 ,
                             0.0 , 0.0 , 1.0 , 0.0 ,
                             0.0 , 0.0 , 0.0 , 1.0
                           };
    
    glUniformMatrix4fv(glGetUniformLocation(m_prog, "matrix"), 1, GL_FALSE, tab_matrix);
    GLfloat vVertices[] = {  0.0f,  0.5f, 0.0f,
                             0.5f,  0.5f, 0.0f,
                             0.5f, -0.5f, 0.0f };
    
    
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    
    glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vVertices );
    glEnableVertexAttribArray ( 0 );
    
    glDrawArrays ( GL_TRIANGLES, 0, 3 );
    
    glDisableVertexAttribArray(0);
    glUseProgram(0);
    
    painter->endNativePainting();
    

    }@

    I use the same code in the squircle example and I've got my blue triangle drawn;
    Did someone knows how to make it work ?

    I've got another question. In the doc of QQuickPaintedItem I've read this :"QQuickPaintedItem is meant to make it easier to port old code that is using the QPainter API to the QML Scene Graph API and it should be used only for that purpose." That's not what I want. I just want to draw OpenGL content in a QQuickItem, is there a better way, like subclasse SGNoge or something like that?

    Thank for reading this.

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #2

      Try subclassing QQuickItem instead.

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Merinas
        wrote on last edited by
        #3

        I found what was mssing from the previous code. I've juste added @PaintedOpenGL::PaintedOpenGL(QQuickItem *parent)
        : QQuickPaintedItem(parent)
        , m_prog(0)
        {
        setRenderTarget(QQuickPaintedItem::FramebufferObject);
        }@

        In my constructor and thing are working pretty well.

        I'm note quite sur subclassing QQuickItem and using QSGMatarial will more simple since I need to draw using raw opengl.

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          ZapB
          wrote on last edited by
          #4

          OK as long as you found something that works.

          Nokia Certified Qt Specialist
          Interested in hearing about Qt related work

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved