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. Minimalistic QOpenGLWidget class does not draw
Forum Updated to NodeBB v4.3 + New Features

Minimalistic QOpenGLWidget class does not draw

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.4k Views 3 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.
  • F Offline
    F Offline
    FemtoAeon
    wrote on 27 Jul 2018, 08:36 last edited by
    #1

    Hello,

    I would be very happy about any help, because I'm currently not even getting started in opengl. I build a minimalistic programm which does not work.
    Here the code:

    #include "CMainWindow.h"
    #include <QApplication>
    
    #include "COpenGLWidget.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        COpenGLWidget widget;
        widget.resize(800, 600);
        widget.show();
    
        return a.exec();
    }
    
    #ifndef COPENGLWIDGET_H
    #define COPENGLWIDGET_H
    
    #include "QOpenGLWidget"
    #include "QOpenGLFunctions"
    
    class COpenGLWidget: public QOpenGLWidget, protected QOpenGLFunctions
    {
        Q_OBJECT
    public:
        COpenGLWidget();
    protected:
        void initializeGL();
        void resizeGL(int w, int h);
        void paintGL();
    };
    
    #endif // COPENGLWIDGET_H
    
    #include "COpenGLWidget.h"
    #include <QtDebug>
    
    
    COpenGLWidget::COpenGLWidget()
    {
    }
    
    void COpenGLWidget::initializeGL()
    {
        initializeOpenGLFunctions();           
        glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
        glEnable(GL_DEPTH_TEST);
    
        qInfo() << "Widget OpenGl: " << format().majorVersion() << "." << format().minorVersion();
        qInfo() << "Context valid: " << context()->isValid();
        qInfo() << "Really used OpenGl: " << context()->format().majorVersion() << "." << context()->format().minorVersion();
        qInfo() << "OpenGl information: VENDOR:       " << (const char*)glGetString(GL_VENDOR);
        qInfo() << "                    RENDERDER:    " << (const char*)glGetString(GL_RENDERER);
        qInfo() << "                    VERSION:      " << (const char*)glGetString(GL_VERSION);
        qInfo() << "                    GLSL VERSION: " << (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
    }
    
    
    void COpenGLWidget::resizeGL(int w, int h)
    {
    
    }
    
    void COpenGLWidget::paintGL()
    {
        glClear(GL_COLOR_BUFFER_BIT);		//Clears buffer
        glTranslatef(-1.5f, 0.0f, -6.0f);
    
        //Trying different things:
        glColor3f(1.0,0.0,0.0);
        glBegin(GL_TRIANGLES);
            glVertex3f(-0.5, -0.5, 0);
            glVertex3f( 0.5, -0.5, 0);
            glVertex3f( 0.0,  0.5, 0);
        glEnd();
    
        glBegin(GL_LINES);
        glVertex3f(0.0f, 0.0f, 0.0f);
        glVertex3f(-1.0f, -1.0f, 0.0f);
        glEnd();
    
        glBegin(GL_TRIANGLES);
            glVertex3f(1, 0, 0);
            glVertex3f(0, 1, 0);
            glVertex3f(0, 0, 1);
        glEnd();
        glFlush();
    
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glColor3f(0.0, 0.0, 1.0);
        glBegin(GL_TRIANGLES);
            glVertex2f(0.0, 1.0);
            glVertex2f(-1.0, -1.0);
            glVertex2f(1.0, -1.0);
        glEnd();
    
        glBegin(GL_QUADS);
            glColor3ub(255, 0, 0);
            glVertex2i(0, 240);
            glColor3ub(0, 255, 0);
            glVertex2i(240, 240);
            glColor3ub(0, 0, 255);
            glVertex2i(240, 480);
            glColor3ub(255, 255, 0);
            glVertex2i(0, 480);
        glEnd();
    }
    

    Output:
    Widget OpenGl: 2 . 0
    Context valid: true
    Really used OpenGl: 2 . 0
    OpenGl information: VENDOR: Google Inc.
    RENDERDER: ANGLE (Software Adapter Direct3D11 vs_5_0 ps_5_0)
    VERSION: OpenGL ES 2.0 (ANGLE 2.1.0.8613f4946861)
    GLSL VERSION: OpenGL ES GLSL ES 1.00 (ANGLE 2.1.0.8613f4946861)

    I am able to change the background color.
    My problem is, that nothing is drawn.
    I am not able to debug inside the function glBegin(), glVertex3f(), ....

    If you need any more information please ask for it.

    Thanks for any help.

    K 1 Reply Last reply 27 Jul 2018, 14:51
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 27 Jul 2018, 10:01 last edited by
      #2

      Hi and welcome to the forums
      You seems to be using the very old way
      which might not be supported anymore.
      the glBegin, glVertex3f, glEnd(); calls.
      http://www.falloutsoftware.com/tutorials/gl/gl2p5.htm
      "June 18th, 2014 update: Using glVertex, glBegin and glEnd, the method described in this tutorial, is no longer acceptable way of creating graphics with OpenGL"

      http://www.falloutsoftware.com/tutorials/gl/gl3.htm

      You should rather look at the modern way/samples
      https://learnopengl.com/

      1 Reply Last reply
      2
      • F FemtoAeon
        27 Jul 2018, 08:36

        Hello,

        I would be very happy about any help, because I'm currently not even getting started in opengl. I build a minimalistic programm which does not work.
        Here the code:

        #include "CMainWindow.h"
        #include <QApplication>
        
        #include "COpenGLWidget.h"
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
            COpenGLWidget widget;
            widget.resize(800, 600);
            widget.show();
        
            return a.exec();
        }
        
        #ifndef COPENGLWIDGET_H
        #define COPENGLWIDGET_H
        
        #include "QOpenGLWidget"
        #include "QOpenGLFunctions"
        
        class COpenGLWidget: public QOpenGLWidget, protected QOpenGLFunctions
        {
            Q_OBJECT
        public:
            COpenGLWidget();
        protected:
            void initializeGL();
            void resizeGL(int w, int h);
            void paintGL();
        };
        
        #endif // COPENGLWIDGET_H
        
        #include "COpenGLWidget.h"
        #include <QtDebug>
        
        
        COpenGLWidget::COpenGLWidget()
        {
        }
        
        void COpenGLWidget::initializeGL()
        {
            initializeOpenGLFunctions();           
            glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
            glEnable(GL_DEPTH_TEST);
        
            qInfo() << "Widget OpenGl: " << format().majorVersion() << "." << format().minorVersion();
            qInfo() << "Context valid: " << context()->isValid();
            qInfo() << "Really used OpenGl: " << context()->format().majorVersion() << "." << context()->format().minorVersion();
            qInfo() << "OpenGl information: VENDOR:       " << (const char*)glGetString(GL_VENDOR);
            qInfo() << "                    RENDERDER:    " << (const char*)glGetString(GL_RENDERER);
            qInfo() << "                    VERSION:      " << (const char*)glGetString(GL_VERSION);
            qInfo() << "                    GLSL VERSION: " << (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
        }
        
        
        void COpenGLWidget::resizeGL(int w, int h)
        {
        
        }
        
        void COpenGLWidget::paintGL()
        {
            glClear(GL_COLOR_BUFFER_BIT);		//Clears buffer
            glTranslatef(-1.5f, 0.0f, -6.0f);
        
            //Trying different things:
            glColor3f(1.0,0.0,0.0);
            glBegin(GL_TRIANGLES);
                glVertex3f(-0.5, -0.5, 0);
                glVertex3f( 0.5, -0.5, 0);
                glVertex3f( 0.0,  0.5, 0);
            glEnd();
        
            glBegin(GL_LINES);
            glVertex3f(0.0f, 0.0f, 0.0f);
            glVertex3f(-1.0f, -1.0f, 0.0f);
            glEnd();
        
            glBegin(GL_TRIANGLES);
                glVertex3f(1, 0, 0);
                glVertex3f(0, 1, 0);
                glVertex3f(0, 0, 1);
            glEnd();
            glFlush();
        
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glColor3f(0.0, 0.0, 1.0);
            glBegin(GL_TRIANGLES);
                glVertex2f(0.0, 1.0);
                glVertex2f(-1.0, -1.0);
                glVertex2f(1.0, -1.0);
            glEnd();
        
            glBegin(GL_QUADS);
                glColor3ub(255, 0, 0);
                glVertex2i(0, 240);
                glColor3ub(0, 255, 0);
                glVertex2i(240, 240);
                glColor3ub(0, 0, 255);
                glVertex2i(240, 480);
                glColor3ub(255, 255, 0);
                glVertex2i(0, 480);
            glEnd();
        }
        

        Output:
        Widget OpenGl: 2 . 0
        Context valid: true
        Really used OpenGl: 2 . 0
        OpenGl information: VENDOR: Google Inc.
        RENDERDER: ANGLE (Software Adapter Direct3D11 vs_5_0 ps_5_0)
        VERSION: OpenGL ES 2.0 (ANGLE 2.1.0.8613f4946861)
        GLSL VERSION: OpenGL ES GLSL ES 1.00 (ANGLE 2.1.0.8613f4946861)

        I am able to change the background color.
        My problem is, that nothing is drawn.
        I am not able to debug inside the function glBegin(), glVertex3f(), ....

        If you need any more information please ask for it.

        Thanks for any help.

        K Offline
        K Offline
        kenchan
        wrote on 27 Jul 2018, 14:51 last edited by kenchan
        #3

        @FemtoAeon

        You probably need a compatible profile to make that work. Make your main function something like this and it might work. It did work with my AMD Radeon R9 M395X on my mac.
        Also did you add QT += opengl to your pro file?
        You should get rid of that translate or you will not see anything.

        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
        
            QSurfaceFormat format;
            format.setDepthBufferSize(24);
            format.setStencilBufferSize(8);
            format.setVersion(2, 2);
            format.setProfile(QSurfaceFormat::CompatibilityProfile);
            QSurfaceFormat::setDefaultFormat(format);
        
            COpenGLWidget w;
            w.show();
        
            return a.exec();
        }
        
        1 Reply Last reply
        1
        • Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on 27 Jul 2018, 14:56 last edited by Chris Kawa
          #4

          You are not setting a viewport size (glViewport) in your resize handler and you're not setting any transformation or perspective matrices. Also since you have glTranslatef in your paintGL function you are moving further and further away on each paint (It does not reset on its own).

          1 Reply Last reply
          2
          • K Offline
            K Offline
            kenchan
            wrote on 27 Jul 2018, 15:16 last edited by kenchan
            #5

            As Chris Kawa says, that glTranslatef in the paint function is the biggest reason you don't see anything.
            It worked for me with or without setting a surface format.

            1 Reply Last reply
            2
            • F Offline
              F Offline
              FemtoAeon
              wrote on 27 Jul 2018, 17:45 last edited by
              #6

              Thanks for all answers.
              I really appreciate your effort.

              @ mrjj,

              I started reading the tutorials at https://learnopengl.com/. It is easy to follow but I will need a little bit more time. I will reply again after I finished the first program without relying on the functions. Does this also explain why the methodes glVertex, glBegin and glEnd are not included in the class QOpenGLFunctions?

              @ Chris Kawa,

              I delted the function "glTranslatef(-1.5f, 0.0f, -6.0f);". This did not lead to any change. My windows looks like this: 0_1532712511902_Window.jpg . I will experiment later with viewport size as well as transformation and perspective matrices.

              @ kenchan ,

              yes I added QT += opengl to my pro file and also LIBS += -lOpengl32.

              QT       += core gui widgets opengl
              LIBS += -lOpengl32
              

              I added your code in my main:

              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
              
                  QSurfaceFormat format;
                  format.setDepthBufferSize(24);
                  format.setStencilBufferSize(8);
                  format.setVersion(2, 2);
                  format.setProfile(QSurfaceFormat::CompatibilityProfile);
                  QSurfaceFormat::setDefaultFormat(format);
              
              
                  COpenGLWidget widget;
                  //widget.resize(800, 600);
                  widget.show();
              
                  return a.exec();
              }
              

              There is still no change.
              Can there be an error with my opengl?
              I tested compiling with Qt Creator 4.7.0 (32 Bit compilation) as well as with Microsoft Visual Studio 2017 (64 bit compilation).
              Both with Qt 5.11.1.
              I installed OpenGL Extensions Viewer 4.1 and it shows OpenGl version 4.4 to be available.

              1 Reply Last reply
              0
              • F Offline
                F Offline
                FemtoAeon
                wrote on 28 Jul 2018, 21:43 last edited by
                #7

                Update:

                I have found a more sophisticated example which works without glBegin, glVertex3f, glEnd(); calls.
                This works at my Computer.

                Thanks @ mrjj 

                1 Reply Last reply
                1

                1/7

                27 Jul 2018, 08:36

                • Login

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