Console application with OpenGL



  • I want to create a console application that just uses Compute Shaders. I don't need any GUI. So I thought that Qt can help me get a context. Then I want to use GLEW or GL3W for loading the OpenGL functions.

    Can I have something like that:

    #include "GL/gl3w.h"
    
    #include <QtCore/QDebug>
    #include <QtGui/QSurfaceFormat>
    #include <QtGui/QOpenGlContext>
    
    
    using namespace std;
    
    int main()
    {
       QSurfaceFormat surfaceFormat;
       surfaceFormat.setMajorVersion(4);
       surfaceFormat.setMinorVersion(3);
       QOpenGLContext openGLContext;
       openGLContext.setFormat(surfaceFormat);
       openGLContext.create();
    
    if (openGLContext.isValid())
    {
        qDebug() << "OpenGL version: ";
    }
    else
    {
        qDebug() << "Could not create an OpenGL context";
    }
    
    
    if (gl3wInit()) {
                    fprintf(stderr, "failed to initialize OpenGL\n");
                    return -1;
            }
            if (!gl3wIsSupported(3, 2)) {
                    fprintf(stderr, "OpenGL 3.2 not supported\n");
                    return -1;
            }
            printf("OpenGL %s, GLSL %s\n", glGetString(GL_VERSION),
                   glGetString(GL_SHADING_LANGUAGE_VERSION));
    
    return 0;
    

    }


  • Moderators

    No, that's not enough.
    Creating OpenGL context requires a QGuiApplication instance (even in a console app) and a surface to make the context current on.

    Also, if you're already using Qt anyway why bringing in another library for OpenGL handling? Qt already has everything you need to create a core profile context and it's one less dependency.

    Here'a s minimal example:

    #include <QGuiApplication>
    #include <QSurfaceFormat>
    #include <QOpenGlContext>
    #include <QOffscreenSurface>
    #include <QOpenGLFunctions_4_3_Core>
    #include <QDebug>
    
    int main(int argc, char* argv[])
    {
       QGuiApplication a(argc, argv);
    
       QSurfaceFormat surfaceFormat;
       surfaceFormat.setMajorVersion(4);
       surfaceFormat.setMinorVersion(3);
    
       QOpenGLContext openGLContext;
       openGLContext.setFormat(surfaceFormat);
       openGLContext.create();
       if(!openGLContext.isValid()) return -1;
    
       QOffscreenSurface surface;
       surface.setFormat(surfaceFormat);
       surface.create();
       if(!surface.isValid()) return -2;
    
       openGLContext.makeCurrent(&surface);
    
       QOpenGLFunctions_4_3_Core f;
       if(!f.initializeOpenGLFunctions()) return -3;
    
       qDebug() << QString::fromLatin1((const char*)f.glGetString(GL_VERSION));
    
       return 0;
    }
    

Log in to reply
 

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