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. [Solved] Qt5 OpenGL reports wrong glVersion
Forum Updated to NodeBB v4.3 + New Features

[Solved] Qt5 OpenGL reports wrong glVersion

Scheduled Pinned Locked Moved General and Desktop
23 Posts 5 Posters 24.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.
  • M Offline
    M Offline
    manishsharma
    wrote on last edited by
    #12

    ZapB thanks for reply.

    I am running this below sample app
    http://qt-project.org/doc/qt-5.0/qtquick/scenegraph-openglunderqml.html

    And inside "Squircle::paint()":http://qt-project.org/doc/qt-5.0/qtquick/scenegraph-openglunderqml-squircle-cpp.html function i am trying to get opengl verison like below

    @char *ver = (char *) glGetString(GL_VERSION);@

    And it returns opengl version 2.1.2.

    As a workaround i am doing following, this fixes the issue somewhat. But problem is that when i set version more than 3.1 i get a black blank screen.

    @ QQuickView view;
    QSurfaceFormat format;
    format.setProfile(QSurfaceFormat::CoreProfile);
    format.setVersion(3, 1);
    view.setFormat(format);
    @

    But i don't want to hard code any opengl version. Why Qt is not returning a correct opengl version? Other interesting thing which i noticed when i was debugging, inside the Qt in below code during initialization it returns correct version 4.3.0; but somehow it never gets reflected.

    @QWindowsOpenGLContextFormat QWindowsOpenGLContextFormat::current()
    {
    QWindowsOpenGLContextFormat result;
    const QByteArray version = QOpenGLStaticContext::getGlString(GL_VERSION);
    .
    .
    .
    }
    @

    1 Reply Last reply
    0
    • A Offline
      A Offline
      agrum
      wrote on last edited by
      #13

      Hi,

      I'm trying to use the new Qt / openGL design. Every time I get a success, I realize there is something wrong once again.

      First I would like to point out that the sample here doesn't work (Win7 VS2010):
      "OpenGL Window Example":http://qt-project.org/doc/qt-5/qtgui-openglwindow-example.html
      It gives me a black screen and that's it. But no worries, many samples out there seems to be from 5.0, 5.1 or an unknown version in between and none actually works on 5.2. Didn't try on 5.1, 5.0 nor 5.3 Beta.
      And I did install this version of Qt :
      "Qt5.2 VS2010 OpenGL":Qt 5.2.1 for Windows 32-bit (VS 2010, OpenGL, 517 MB)
      So I don't know if this bug applies to me :
      "Windows builds should ship a standard Open GL build":https://bugreports.qt-project.org/browse/QTBUG-28715

      The trick I found was to remove the QOpenGLFunctions from the inheritance of OpenGLWindow and used a QOpenGLFunctions_4_3_Core instead as an attribute of the class.
      Then instead of calling "initializeOpenGLFunctions();", I get the functions from the newly created context with versionFunctions() and then initialize this set of functions.

      After this step, I finally got a visual. The magical triangle that is, even ten years later, still a long and painful process to obtain when changing API.

      Anyway, after that I though I was finally free to do whatever. But no.
      In the code I was barbarically casting the return of "m_context->versionFunctions();" to "QOpenGLFunctions_4_3_Core*". But when I try to be more gentle and dynamic cast it to the proper class, I get a NULL. When debugging I noticed that the original class was in fact a QOpenGLFunctions_2_1.

      So I came back to the source. I realized that my surface format was by default set for 2.1. So I changed that to 4.3 before calling show(). Then I checked and :
      My surface format is 4.3, horayyy
      My context format is 4.3 horayyyyy
      The dynamic cast works horaayyyy!!
      But the window is black.

      So what ? please what ?

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

        First of all, yes, QSurfaceFormat defaults to requesting a 2.x context as this is the minimum required for Qt Quick 2. If you want an OpenGL 4.3 context you need to ask for it via QSurfaceFormat::setVersion() as you found.

        Second, there is no need to cast the result form QOpenGLContext::versionFunctions() if you use the templated version of it. e.g.

        @
        m_funcs = context->versionFunctions<QOpenGLFunctions_4_3_Core>();
        if (!m_funcs)
        // something went wrong. fallback or bail out
        @

        As to why you see nothing when using OpenGL 4.3 I suspect that is because that example is written against older style OpenGL as evidenced by the fact it worked with OpenGL 2.x profile. To test this you could ask for an OpenGL 4.3 Compatibility profile context so that the stuff which is deprecated and removed in the core profile is still available. See QSurfaceFormat::setProfile().

        Alternatively you would need to port the rest of the example to use only Core profile features. That is remove all of the fixed function pipeline calls and built in shader uniforms. Also you would need to tweak the shaders to use in/out rather than attribute/varying.

        Good luck.

        Nokia Certified Qt Specialist
        Interested in hearing about Qt related work

        1 Reply Last reply
        0
        • A Offline
          A Offline
          agrum
          wrote on last edited by
          #15

          Ohhhh god I love you.
          Compatibility profile worked. However, opengl.org is down from my side. I can't check what's supported in 3.1 core and what has been thrown away.
          On the shaders, I clearly specify the version 420, so if it compile, am I good to assume there is no problem there ? I use gl_Position still, but I don't recall having an alternative.
          On the code, the only functions used with QOpenGLFunctions are :
          glClear(), glViewport(), glDrawArrays().
          The other calls are made from the program itself :
          link(), bind(), enableAttributeArray(), setAttributeBuffer(), disableAttributeArray(), release().
          And finally, I use QOpenGLBuffers for my meshes info :
          create(), setUsagePattern(), bind(), allocate().

          That's it. I know glViewport and glDrawArrays are not deprecated. Is glClear my issue ?
          Also, the context does a swapBuffers(), is it ok ?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            agrum
            wrote on last edited by
            #16

            I check the glError after each call and an error pops up for each call to QOpenGLShaderProgram::setAttributeBuffer :
            "m_program->setAttributeBuffer("posAttr", GL_FLOAT, 0, 2);"
            "m_program->setAttributeBuffer("colAttr", GL_FLOAT, 0, 3);"
            This error doesn't show up for compatibility profile. Safe to assume this is my culprit ?
            Any idea ?

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

              The problems will be in the shaders themselves.

              The vertex shader has code like this for the input vertex attribute variables:

              @
              attribute vec2 posAttr;
              attribute vec3 colAttr;
              @

              The attribute keyword is gone in the Core profile. Instead yuo have to use "in":

              @
              in vec2 posAttr;
              in vec3 colAttr;
              @

              And in the vertex shader instead of using "varying" for the output you use "out". You still need to write to gl_Position as the input to the rasterizer though.

              Then in the fragment shader for the input variables you no longer use "varying" but rather "in".

              For the fragment shader output the old built-in variable gl_FragColor is gone. Instead you must define your own output variable for the fragment shader and write to that. Something like this...

              @
              out vec4 fragColor;
              ...
              void main()
              {
              ...
              fragColor = vec4(...);
              }
              @

              Hope this helps.

              Nokia Certified Qt Specialist
              Interested in hearing about Qt related work

              1 Reply Last reply
              0
              • A Offline
                A Offline
                agrum
                wrote on last edited by
                #18

                My shaders are arleady set to version 420

                @#version 420

                in vec2 posAttr;
                in vec3 colAttr;

                uniform mat4 matrix;

                out vec4 vCol;

                void main() {
                vCol = vec4(colAttr, 1.0);
                gl_Position = matrix * vec4(posAttr, 0.0, 1.0);
                }@

                @#version 420

                in vec4 vCol;

                out vec4 fCol;

                void main() {
                fCol = vCol;
                }@

                Is there an issue ?

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

                  You tell me ;) Do they compile and link? Can you obtain the locations of the attributes?

                  Are you creating and binidng a vertex array object anywhere? Core profile requires that you bind a vertex array object. This can be as simple as creating a QOpenGLVertexArrayObject instance and in your init function doing:

                  @
                  m_vao.create();
                  m_vao.bind();
                  @

                  Of course that is the minimal way to do it. You can have multiple VAOs in a program.

                  Nokia Certified Qt Specialist
                  Interested in hearing about Qt related work

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    agrum
                    wrote on last edited by
                    #20

                    Direct hit :D.
                    My shaders were compiling, no problem on this side (otherwise I wouldn't be able to see anything with the 2.1 profile anyway).
                    The problem was the VAO. As soon as I created/binded one in my init then bind it the rendering, "pouf" it worked.

                    Thanks for the help. That was some kick ass troubleshooting you've done here :D.

                    Cheers.

                    PS : Maybe update the sample in the Qt Wiki. I don't think there is any example that works for 5.2 out there.

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

                      Great! Glad you got it working.

                      I plan to add some examples to Qt for 5.4 showing some of the newer OpenGL features and the helpers in Qt.

                      BTW, in case you or anybody else is interested, KDAB offers training courses in "Modern OpenGL and Qt":http://www.kdab.com/training/courses/modern-opengl-with-qt/

                      Happy hacking!

                      Nokia Certified Qt Specialist
                      Interested in hearing about Qt related work

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #22

                        And if I may, the courses are worth both the time and money !

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                          Hehe, thanks Samuel. Also good to get feedback :)

                          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