Class SoGLContext

  • All Implemented Interfaces:
    SafeDisposable

    public class SoGLContext
    extends SoDeviceContext
    OpenGL context management class. This class provides functions to manage OpenGL device contexts.

    Starting with Open Inventor 8.5, the application can control sharing of OpenGL contexts (previously this was done internally for rendering contexts). When OpenGL contexts are shared it means that display lists, texture objects, buffer objects, etc created in any context in that share group can be used by any other context in that group. This saves time and memory when rendering into multiple windows, buffers, etc.

    In a typical Open Inventor application an OpenGL device context is created automatically by the viewer class and is managed automatically by the viewer for rendering and other operations involving the GPU. However this context is only made current when the viewer is actively using it, for example to render the scene graph. (Prior to Open Inventor 8.5, the viewer made its OpenGL context "current" and this context remained current even when the viewer was not actively rendering the scene graph. This was convenient in some cases, but could conflict with application strategies for managing OpenGL contexts.)

    Do not assume that any OpenGL context is current. If you need a current OpenGL context, for example to create an SoGpuBufferObject, bind() and unbind() an SoGLContext object.

    If your application uses an Open Inventor viewer, the viewer's rendering context can be made current using the viewer's bindNormalContext() method and released using the viewer's unbindNormalContext() method. This context can also be queried using the viewer's getNormalSoContext() method.

    If you do not have (or do not have access to) a viewer, you can create a context by creating an SoGLContext object. In almost all cases, calling the constructor with a true value will create a context that is in a share group with the rendering context. In other words, resources such as textures and buffer objects are shared between these contexts and can be used in any of the contexts in the share group. For example:

     SoGLContext glContext = new SoGLContext( true );
     glContext.bind();
         SoGpuBufferObject gpuBuffer = new SoGpuBufferObject( SoGpuBufferObject.BufferAccessFrequencies.STATIC, SoGpuBufferObject.BufferAccessNatures.SHARED );
     glContext.unbind();

    If the OpenGL context is created and managed by the application or a third- party library, the application should create an SoGLContext object to wrap the actual current context, then bind() and unbind() this object around the Open Inventor render call. Note that this method will return null if there is no current OpenGL context. Also note that the SoGLContext object should be told not to manage (specifically not to delete) the OpenGL context since its lifespan is managed by other software. For example:

     if (context != null)
         context.setNoGLContextDelete(); // Context is managed by someone else
     else
         context = new SoGLContext( true );
     context.bind();
         . . .
     context.unbind();

    • Constructor Detail

      • SoGLContext

        public SoGLContext​(boolean shared)
        Constructor which creates an SoGLContext based on the attributes of the current context.
        If the current context is NULL, the function tries to create a context on a temporary window. If this doesn't work, the resulting SoGLContext will not be valid!

        Parameters:
        shared - Indicates if we want to share the context. If true, context is shared with current context, depending on the currently defined SharedGroupPolicy. If false, context is explicitly not shared.
      • SoGLContext

        public SoGLContext​(SoGLContext context,
                           boolean shared)
        Constructor which creates an SoGLContext based on the attributes of the specified context. If shared is true then the created OpenGL context will be shared with the given context, depending on the current SharedGroupPolicy.
    • Method Detail

      • getCurrent

        public static SoGLContext getCurrent()
        Calls getCurrent((boolean)false).
      • tryBind

        public boolean tryBind()
        Try to bind the OpenGL context to the current thread.
        Returns false if already bound, else calls bind() and returns true.
      • getContextGraphicsCapabilities

        public SbGPUCapabilities getContextGraphicsCapabilities()
        Retrieve graphics capabilities from this context. Make sure that this context is valid, otherwise capabilities will be wrong.
      • setNoGLContextDelete

        public void setNoGLContextDelete()
        Prevent deletion of the native OpenGL context by SoGLContext. Note: Can be useful when the OpenGL context is created and managed by the application or a third-party library such as Qt.
      • swapBuffers

        public boolean swapBuffers()
        Swaps the buffers with the value stored in the SoGLFormat, which is set to the main plane by default.
      • getContextFromSharedId

        public static SoGLContext getContextFromSharedId​(int sharedIdGroup)
        Returns the first context that belongs to the specified sharedIdGroup.

        Parameters:
        sharedIdGroup - The internal sharedIdGroup used to search for the context.

        Returns:
        The first context that belongs to the specified sharedIdGroup.

      • getContextFromId

        public static SoGLContext getContextFromId​(int id)
        Returns the context corresponding to an internal id.

        Parameters:
        id - The internal id used to search for the context.

        Returns:
        The context which has the specified id.

      • getCurrent

        public static SoGLContext getCurrent​(boolean checkGLState)
        Returns the current active OpenGL context (if any). This method returns the current active OpenGL context from the Open Inventor state. However it can also get the actual current context from OpenGL. It may be useful if Open Inventor should render using an OpenGL context created by the application or a third-party library. In that case, Open Inventor will create a new SoGLContext using contextId returned by system getCurrentContext() method.

        Parameters:
        checkGLState - This param is no longer used.

        Returns:
        The current OpenGL context (or NULL if there isn't one).

      • getGraphicsCapabilities

        public static SbGPUCapabilities getGraphicsCapabilities()
        Retrieve graphics capabilities from the current bound context, if any. A tempory context is created with default values if no context is bound.
      • getId

        public int getId()
        Returns the internal id for this context. The internal id is unique, it's not possible to get two contexts with the same id.

        Returns:
        The internal id of the context.

      • invalidate

        public void invalidate()
        Set this context as invalid so it won't be used anymore. This is mainly useful when the SoGLContext has been created from an OpenGL context managed by the application or a third-party library (e.g.: Qt, JOGL...).
      • assertContext

        public void assertContext()
        Assert this context and the current active context are the same.
      • isValidForCurrent

        public boolean isValidForCurrent()
        Returns true if the context is the current active context or if it is shared with the current active context.