Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

OpenGL in Qt 5.1 - Part 1

Window::Window( QScreen* screen )
    : QWindow( screen ),
{
    // Tell Qt we will use OpenGL for this window
    setSurfaceType( OpenGLSurface );

    // Specify the format and create platform-specific surface
    QSurfaceFormat format;
    format.setDepthBufferSize( 24 );
    format.setMajorVersion( 4 );
    format.setMinorVersion( 3 );
    format.setSamples( 4 );
    format.setProfile( QSurfaceFormat::CoreProfile );
    setFormat( format );
    create();

    // Create an OpenGL context
    m_context = new QOpenGLContext;
    m_context->setFormat( format );
    m_context->create();

    // Make the context current on this window
    m_context->makeCurrent( this );

    // Obtain a functions object and resolve all entry points
    // m_funcs is declared as: QOpenGLFunctions_4_3_Core* m_funcs
    m_funcs = m_context->versionFunctions();
    if ( !m_funcs ) {
        qWarning( "Could not obtain OpenGL versions object" );
        exit( 1 );
    }
    m_funcs->initializeOpenGLFunctions();
}
// Set a vertex attribute divisor
// Used with instanced rendering
// Introduced in OpenGL 3.3
m_funcs->glVertexAttribDivisor( pointLocation, 1 );

// Dispatch processing via a compute shader
// Introduced in OpenGL 4.3
m_funcs->glDispatchCompute( 512 / 16, 512 / 16, 1 );
// Query extensions
 QList extensions = m_context->extensions().toList();
 std::sort( extensions );
 qDebug() << "Supported extensions (" << extensions.count() << ")";
 foreach ( const QByteArray &extension, extensions )
     qDebug() << "    " << extension;
// Check if extension is supported
 if ( !m_context->hasExtension( QByteArrayLiteral(
          "GL_ARB_instanced_arrays" ) )
     qFatal( "GL_ARB_instanced_arrays is not supported" );

 // Create instance of helper class and resolve functions
 QOpenGLExtension_ARB_instanced_arrays* m_instanceFuncs =
     new QOpenGLExtension_ARB_instanced_arrays();
 m_instanceFuncs->initializeOpenGLFunctions();

 // Call an extension function
 m_instanceFuncs->glVertexAttribDivisorARB( pointLocation, 1 );

About KDAB


53 Comments

15 - Mar - 2013

macoro

16 - Mar - 2013

Sean Harmer

15 - Mar - 2013

noizex

16 - Mar - 2013

Sean Harmer

class MyGLStuff : protected QOpenGLFunctions_4_3_Core
{
public:
    MyGLStuff()
    {
        initializeOpenGLFunctions(); // Assumes current context

        glVertexAttribDivisor(...);
    }
};

16 - Mar - 2013

cutiepie

16 - Mar - 2013

Sean Harmer

16 - Mar - 2013

Grumpy OpenGL

17 - Mar - 2013

Sean Harmer

17 - Mar - 2013

Grumpy OpenGL

17 - Mar - 2013

Sean Harmer

#define _DEBUG
#ifdef _DEBUG
static void checkOpenGLError( const char* stmt, const char* function, const char* file, int line )
{
    GLenum err = glGetError();
    if (err != GL_NO_ERROR)
    {
        qDebug() << "OpenGL error" << hex << err << dec << "at" << stmt
                 << "called from" << function << "in file" << file << "line" << line;
        abort();
    }
}

    #define GL_CHECK(stmt) do { \
            stmt; \
            checkOpenGLError(#stmt, Q_FUNC_INFO, __FILE__, __LINE__); \
        } while (0)
#else
    #define GL_CHECK(stmt) stmt
#endif
void BasicUsageScene::render()
{
    // Introduce a GL_INVALID_VALUE error
    GL_CHECK( glClear( /*GL_COLOR_BUFFER_BIT*/ GL_TEXTURE_2D ) );
    GL_CHECK( m_vao.bind() );
    GL_CHECK( m_shaderProgram.bind() );
    GL_CHECK( glDrawArrays( GL_TRIANGLES, 0, 3 ) );
}

17 - Mar - 2013

Grumpy OpenGL

17 - Mar - 2013

Sean Harmer

17 - Mar - 2013

Grumpy OpenGL

17 - Mar - 2013

Sean Harmer

17 - Mar - 2013

Steve

17 - Mar - 2013

Sean Harmer

22 - Mar - 2013

Mohamed Aissiou

22 - Mar - 2013

Sean Harmer

7 - May - 2013

stereomatching

7 - May - 2013

stereomatching

15 - May - 2013

mfeemster

15 - May - 2013

mfeemster

15 - Jul - 2013

richard

15 - Jul - 2013

Sean Harmer

m_funcs = m_context->versionFunctions<QOpenGLFunctions_4_3_Core>();

1 - Dec - 2013

Steve Starr

9 - Aug - 2013

Jay

7 - Oct - 2013

Jay

27 - Oct - 2013

SeVerage

30 - Oct - 2013

Sky

30 - Oct - 2013

Sean Harmer

31 - Oct - 2013

Sky

4 - Nov - 2013

QtAddict

4 - Nov - 2013

Sean Harmer

4 - Nov - 2013

QtAddict

4 - Nov - 2013

Sean Harmer

14 - Nov - 2013

He Yang

14 - Nov - 2013

Sean Harmer

3 - Dec - 2013

HGH

3 - Dec - 2013

Sean Harmer

8 - Jan - 2014

Steve Brunton

17 - Jan - 2014

Korchkidu

11 - Mar - 2014

Soruro

23 - Apr - 2014

Cobb

23 - Apr - 2014

Sean Harmer

23 - Apr - 2014

Cobb

12 - Jun - 2014

Lennart Rolland

12 - Jun - 2014

Sean Harmer

2 - Sept - 2014

Brian

18 - Mar - 2015

Zsolt

18 - Mar - 2015

Sean Harmer

18 - Mar - 2015

Zsolt

18 - Mar - 2015

Sean Harmer

19 - Mar - 2017

Seweall1816@gmail.com

SeanHarmer

Sean Harmer

Managing Director KDAB UK