Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT5 and modern OpenGL
Forum Updated to NodeBB v4.3 + New Features

QT5 and modern OpenGL

Scheduled Pinned Locked Moved General and Desktop
21 Posts 7 Posters 20.1k 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.
  • Z Offline
    Z Offline
    ZapB
    wrote on last edited by
    #11

    Incidentally the wiki article you reference in the original post was written a long time ago for Qt 4. I should find some time to update it for Qt5 to show the option of QWindow+QOpenGLContext.

    Nokia Certified Qt Specialist
    Interested in hearing about Qt related work

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #12

      Speaking of the devil... ;-)

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

        [quote author="Lukas Geyer" date="1356023511"]Speaking of the devil... ;-)[/quote]

        I'm like Beetlejuice :)

        Nokia Certified Qt Specialist
        Interested in hearing about Qt related work

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kantaki
          wrote on last edited by
          #14

          Okay thanks a lot :). But I have one last question. You mentioned I should only use QWindow + QOpenGLContext.

          So I do not need QOpenGLFunctions at all if I only want to use OpenGL (no OpenGL ES)?
          It's mainly for a graphics engine showcase in the future.

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

            I didn't say you should only use QWindow+QOpenGLContext. The other QOpenGL* classes are perfectly fine to use. I highly recommend QOpenGLShaderProgram and friends. They will save you writing a lot of boiler-plate code.

            QOpenGLFunctions however does not expose all OpenGL functions, only the subset used by Qt itself. This is what I am trying to address with https://codereview.qt-project.org/#change,35408

            As mentioned earlier in this thread and on the development@qt-project.org mailing list, we have a whole bunch of other OpenGL enabler classes we could potentially add for Qt 5.1.

            Of course once you have a window and context you are free to use raw OpenGL calls. Just beware that you will have to handle resolving the function pointers for any functions you use. Especially on windows this can be a pain.

            Nokia Certified Qt Specialist
            Interested in hearing about Qt related work

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kantaki
              wrote on last edited by
              #16

              Ok thanks , I think I should be able to use it now. :)

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kantaki
                wrote on last edited by
                #17

                Okay I encountered a problem. :x

                I tried to create opengl window with the QWindow but I am not sure how I can tell QWindow "Hey you have to use OpenGL now".

                I did the following. I read the documentation but I probably misunderstood something.

                @#ifndef GLWINDOW_H
                #define GLWINDOW_H
                #include <QWindow>
                #include <QOpenGLBuffer>
                #include <QOpenGLContext>
                #include <QOpenGLShader>
                #include <QGLFormat>

                class GLWindow : public QWindow
                {
                QOpenGLContext* context;

                public:

                GLWindow();
                
                void initGL();
                void resize(int w, int h);
                

                };

                #endif // GLWINDOW_H@

                @#include "glwindow.h"
                #include <QDebug>

                GLWindow::GLWindow()
                {
                initGL();
                }
                void GLWindow::resize(int w, int h){

                glViewport(0,0,w,h);
                qDebug() << w << " " <<h;
                

                }

                void GLWindow::initGL()
                {
                context = new QOpenGLContext(this);
                context->setFormat(requestedFormat());
                context->create();

                }

                @

                @#include "mainwindow.h"
                #include <QGuiApplication>
                #include <glwindow.h>
                #include <QSurfaceFormat>
                int main(int argc, char *argv[])
                {
                QGuiApplication a(argc, argv);
                QSurfaceFormat format;
                format.setSamples(4);
                GLWindow window;
                window.setFormat(format);
                window.resize(800,600);
                window.show();
                return a.exec();
                }@

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on last edited by
                  #18

                  [quote author="kantaki" date="1356053482"]I tried to create opengl window with the QWindow but I am not sure how I can tell QWindow "Hey you have to use OpenGL now".[/quote]
                  "QWindow::setSurfaceType()":http://qt-project.org/doc/qt-5.0/qtgui/qwindow.html#setSurfaceType

                  Have you taken a look at the "OpenGL Window Example":http://qt-project.org/doc/qt-5.0/qtgui/openglwindow.html?

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

                    Something like this will do it:

                    @
                    OpenGLWindow::OpenGLWindow( const QSurfaceFormat& format,
                    QScreen* screen )
                    : QWindow( screen ),
                    m_context( 0 ),
                    m_scene( 0 )
                    {
                    // Tell Qt we will use OpenGL for this window
                    setSurfaceType( OpenGLSurface );

                    // Request a full screen button (if available)
                    setFlags( flags() | Qt::WindowFullscreenButtonHint );
                    
                    // Create the native window
                    setFormat( format );
                    create();
                    
                    // Create an OpenGL context
                    m_context = new QOpenGLContext;
                    m_context->setFormat( format );
                    m_context->create();
                    

                    }
                    @

                    where the QSurfaceFormat is created in your main() function (adjust to suit your needs of course):

                    @
                    #include <QGuiApplication>
                    #include <QSurfaceFormat>

                    #include "diffusescene.h"
                    #include "openglwindow.h"

                    int main( int argc, char* argv[] )
                    {
                    QGuiApplication a( argc, argv );

                    // Specify the format we wish to use
                    QSurfaceFormat format;
                    format.setMajorVersion( 3 );
                    

                    #if !defined(Q_OS_MAC)
                    format.setMinorVersion( 3 );
                    #else
                    format.setMinorVersion( 2 );
                    #endif
                    format.setDepthBufferSize( 24 );
                    format.setSamples( 4 );
                    format.setProfile( QSurfaceFormat::CoreProfile );

                    OpenGLWindow w( format );
                    w.setScene( new DiffuseScene );
                    
                    w.show();
                    return a.exec&#40;&#41;;
                    

                    }
                    @

                    Hope this helps.

                    ps The DiffuseScene class is just a custom class that does the actual OpenGL rendering in my case.

                    pps Remember to call QOpenGLContext::makeCurrent() on the QWindow before using it to render and to call QOpenGLContext::swapBuffers() after rendering.

                    Nokia Certified Qt Specialist
                    Interested in hearing about Qt related work

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kiro
                      wrote on last edited by
                      #20

                      This code works fine (displays a purple triangle) on Qt 4.8.1, but not on 5.0.0 (pre-compiled version, Windows 7 x64, MSVC2010 32bit compile). I get a window with a grey widget inside. I use "GLWidget" with "setCentralWidget(new GLWidget());" in a QMainWindow constructor... Most of the code is ripped straight from the "hellogl_es2" example btw.

                      Note that I uncommented the usage of native painting too:
                      @QPainter painter;
                      painter.begin(this);
                      painter.beginNativePainting();
                      ...
                      painter.endNativePainting();
                      @
                      because it just doesn't work for me, on BOTH Qt versions...

                      What am I doing wrong?!

                      GLWidget.h
                      @#pragma once

                      #include <vector>

                      #include <QGLWidget>
                      #include <QGLShaderProgram>
                      #include <QGLFunctions>
                      #include <QTime>

                      class GLWidget: public QGLWidget, protected QGLFunctions
                      {
                      int frames;
                      QTime time;

                      QGLShaderProgram program;
                      int vertexAttribute;
                      int normalAttribute;
                      int matrixUniform;

                      float viewAngleX;
                      float viewAngleY;
                      float viewScale;
                      QMatrix4x4 projection;

                      std::vector<QVector3D> vertices;
                      std::vector<QVector3D> normals;

                      public:
                      GLWidget(QWidget * parent = nullptr);

                      virtual void initializeGL();
                      virtual void resizeGL(int width, int height);
                      virtual void paintGL();

                      void setDefaultScene();

                      void drawScene();
                      };@

                      GLWidget.cpp
                      @#include "GLWidget.h"

                      #include <QMessageBox>

                      GLWidget::GLWidget(QWidget * parent)
                      : QGLWidget(parent), viewScale(1.0f), viewAngleX(0.0f), viewAngleY(0.0f)
                      {
                      setAttribute(Qt::WA_PaintOnScreen);
                      setAttribute(Qt::WA_NoSystemBackground);
                      setAutoBufferSwap(true);
                      setMinimumSize(512, 512);
                      }

                      void GLWidget::resizeGL(int width, int height)
                      {
                      //Set up OpenGL viewport
                      glViewport(0, 0, width, height);
                      float aspect = (float)width / ((float)height ? height : 1.0f);
                      const float zNear = 1.0f, zFar = 10.0f, fov = 45.0f;
                      projection.setToIdentity();
                      projection.perspective(fov, aspect, zNear, zFar);
                      }

                      void GLWidget::initializeGL()
                      {
                      initializeGLFunctions();

                      QGLShader *vshader = new QGLShader(QGLShader::Vertex, this);
                      const char *vsrc =
                          "attribute highp vec4 vertex;\n"
                          "attribute mediump vec3 normal;\n"
                          "uniform mediump mat4 matrix;\n"
                          "varying mediump vec4 color;\n"
                          "void main(void)\n"
                          "{\n"
                          "    vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n"
                          "    float angle = max(dot(normal, toLight), 0.0);\n"
                          "    vec4 col = vec4(0.8, 0.8, 0.8, 1.0) * angle + vec4(0.2, 0.2, 0.2, 1.0);\n"
                      

                      " color = clamp(col, 0.0, 1.0);\n"
                      " gl_Position = matrix * vertex;\n"
                      "}\n";
                      if (!vshader->compileSourceCode(vsrc) || vshader->log().size() > 0) {
                      QMessageBox::warning(this, "Error", QString("Error compiling vertex vertex shader source: %1").arg(vshader->log()));
                      return;
                      }

                      QGLShader *fshader = new QGLShader(QGLShader::Fragment, this);
                      const char *fsrc =
                          "varying mediump vec4 color;\n"
                          "void main(void)\n"
                          "{\n"
                          "    gl_FragColor = vec4(0.4, 0.0, 0.4, 1.0);\n"
                          "}\n";
                      if (!fshader->compileSourceCode(fsrc) || fshader->log().size() > 0) {
                      

                      QMessageBox::warning(this, "Error", QString("Error compiling fragment shader source: %1").arg(fshader->log()));
                      return;
                      }

                      if (!program.addShader(vshader)) {
                      QMessageBox::warning(this, "Error", QString("Error adding vertex shader: %1").arg(program.log()));
                      return;
                      }

                      if (!program.addShader(fshader)) {
                      QMessageBox::warning(this, "Error", QString("Error adding fragment shader: %1").arg(program.log()));
                      return;
                      }

                      if (!program.link() || !program.bind()) {
                      QMessageBox::warning(this, "Error", QString("Error linking shaders: %1").arg(program.log()));
                      return;
                      }

                      vertexAttribute = program.attributeLocation("vertex");
                      normalAttribute = program.attributeLocation("normal");
                      matrixUniform = program.uniformLocation("matrix");
                      

                      setDefaultScene();
                      }

                      void GLWidget::paintGL()
                      {
                      /*QPainter painter;
                      painter.begin(this);

                      painter.beginNativePainting();*/
                      
                      glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
                      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                      
                      glDisable(GL_CULL_FACE);
                      glDisable(GL_DEPTH_TEST);
                      
                      QMatrix4x4 modelview;
                      modelview.rotate(viewAngleX, 1.0f, 0.0f, 0.0f);
                      modelview.rotate(viewAngleY, 0.0f, 1.0f, 0.0f);
                      modelview.scale(viewScale);
                      modelview.translate(0.0f, 0.0f, -2.0f);
                      
                      program.bind();
                      program.setUniformValue(matrixUniform, projection * modelview);
                      
                      drawScene();
                      
                      program.release();
                      
                      //painter.endNativePainting();
                      

                      }

                      void GLWidget::setDefaultScene()
                      {
                      vertices.clear();
                      normals.clear();

                      vertices.push_back(QVector3D(-0.3f, -0.3f, 0.0f));
                      vertices.push_back(QVector3D( 0.3f, -0.3f, 0.0f));
                      vertices.push_back(QVector3D( 0.3f, 0.3f, 0.0f));

                      normals.push_back(QVector3D( 0.0f, 0.0f, 1.0f));
                      normals.push_back(QVector3D( 0.0f, 0.0f, 1.0f));
                      normals.push_back(QVector3D( 0.0f, 0.0f, 1.0f));
                      }

                      void GLWidget::drawScene()
                      {
                      program.enableAttributeArray(normalAttribute);
                      program.enableAttributeArray(vertexAttribute);
                      program.setAttributeArray(vertexAttribute, vertices.data());
                      program.setAttributeArray(normalAttribute, normals.data());
                      glDrawArrays(GL_TRIANGLES, 0, vertices.size());
                      program.disableAttributeArray(normalAttribute);
                      program.disableAttributeArray(vertexAttribute);
                      }@

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        steventaitinger
                        wrote on last edited by
                        #21

                        For an introduction to QT5.5 and modern OpenGL have a look at my post on that topic. It will give you most of the info you need to choose if and how you want to use OpenGL and Qt.

                        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