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. Console application with OpenGL
QtWS25 Last Chance

Console application with OpenGL

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.6k Views
  • 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
    Zingam
    wrote on last edited by Zingam
    #1

    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;
    

    }

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      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;
      }
      
      1 Reply Last reply
      0
      • R RoApPr referenced this topic on

      • Login

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