How to obtain OpenGL version from within OpenSceneGraph

2 minute read


When writing a shader for one of my projects, I encountered a need to define an OpenGL version and whether GLSL is supported on the used machine. Assume, I have some geometry and there are two ways to render it: simplified version and fancy version. The simplified version uses some default primitive, e.g., GL_LINE_STRIP_ADJACENCY. The fancy version requires certain minimal OpenGL version so that to use the shaders I just wrote.

Since in OpenSceneGraph we normally do not deal with OpenGL commands directly, I had to find a way how to define the supported OpenGL versions through the OSG library.

Tester class for supported OpenGL version

The OSG examples already have an example that provides an idea how to request certain OpenGL constants. For instance, OsgShaderTerrain example. I took the derived class from osg::GraphicsOperation and modified it so that it returned the OpenGL version. Below is an example of how such tester class is implemented:

class TestSupportOperation : public osg::GraphicsOperation
        : osg::Referenced(true)
        , osg::GraphicsOperation("TestSupportOperation", false)
        , m_supported(true)
        , m_errorMsg()
        , m_version(0.0)

    virtual void operator() (osg::GraphicsContext* gc)
        OpenThreads::ScopedLock<OpenThreads::Mutex> lock(m_mutex);
        osg::GLExtensions* gl2ext = gc->getState()->get<osg::GLExtensions>();

        if( gl2ext ){

            if( !gl2ext->isGlslSupported )
            m_supported = false;
            m_errorMsg = "ERROR: GLSL not supported by OpenGL driver.";
                m_version = gl2ext->glVersion;
            m_supported = false;
            m_errorMsg = "ERROR: GLSL not supported.";

    OpenThreads::Mutex  m_mutex;
    bool                m_supported;
    std::string         m_errorMsg;
    float               m_version;

Now, when using the TestSupportOperation class we can easily obtain the OpenGL version by refering to the class’ public variable: tester->m_version.

A very simplified case usage (empty scene) is presented below:

int main(int, char**)
    osgViewer::Viewer viewer;

    // openGL version:
    osg::ref_ptr<TestSupportOperation> tester = new TestSupportOperation;

    if (tester->m_supported)
        std::cout << "GLVersion=" << tester->m_version << std::endl;
        std::cout << tester->m_errorMsg << std::endl;


Leave a Comment