Class SoGLContext
- All Implemented Interfaces:
SafeDisposable
The application can control sharing of OpenGL contexts. When you share OpenGL contexts, any context in that shared group can use GPU resources (such as textures, buffers, etc.) created in any other context in the group. This saves time and memory when rendering into multiple windows, buffers, etc. We recommend using context sharing when scene graphs are shared between multiple viewers. This approach saves memory on the GPU and improves rendering performance. To create a shared gl context, use a SoGLContext
constructor with the bool shared argument, and specify true.
In a typical Open Inventor application, the viewer class automatically creates an OpenGL device context and manages it for rendering and other GPU-related operations. However, this context is only made current when the viewer is actively using it, for example, to render the scene graph.
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:
If the OpenGL context is created and managed by the application or a third- party library, the application should create anSoGLContext glContext = new SoGLContext( true ); glContext.bind(); SoGpuBufferObject gpuBuffer = new SoGpuBufferObject( SoGpuBufferObject.BufferAccessFrequencies.STATIC, SoGpuBufferObject.BufferAccessNatures.SHARED ); glContext.unbind();
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();
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class com.openinventor.inventor.Inventor
Inventor.ConstructorCommand
-
Field Summary
Fields inherited from class com.openinventor.inventor.Inventor
VERBOSE_LEVEL, ZeroHandle
-
Constructor Summary
ConstructorsConstructorDescriptionSoGLContext
(boolean shared) Constructor which creates anSoGLContext
based on the attributes of the current context.SoGLContext
(SoGLContext context, boolean shared) Constructor which creates anSoGLContext
based on the attributes of the specified context. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Assert this context and the current active context are the same.static SoGLContext
getContextFromId
(int id) Returns the context corresponding to an internal id.static SoGLContext
getContextFromSharedId
(int sharedIdGroup) Returns the first context that belongs to the specified sharedIdGroup.Retrieve graphics capabilities from this context.static SoGLContext
Calls getCurrent((boolean)false).static SoGLContext
getCurrent
(boolean checkGLState) Returns the current active OpenGL context (if any).Returns theSoGLFormat
associated to theSoGLContext
.static SbGPUCapabilities
Retrieve graphics capabilities from the current bound context, if any.int
getId()
Returns the internal id for this context.Returns the current sharedGroupPolicy.void
Set this context as invalid so it won't be used anymore.boolean
Returns true if the context is the current active context or if it is shared with the current active context.void
Prevent deletion of the native OpenGL context bySoGLContext
.boolean
Swaps the buffers with the value stored in theSoGLFormat
, which is set to the main plane by default.boolean
tryBind()
Try to bind the OpenGL context to the current thread.Methods inherited from class com.openinventor.inventor.devices.SoDeviceContext
bind, dispose, getSharedGroup, getSharedId, isCompatible, isCurrent, isDisposable, isSharable, isSharedWith, isValid, setSharable, setSharedGroup, setSharedWith, unbind
Methods inherited from class com.openinventor.inventor.Inventor
getNativeResourceHandle
-
Constructor Details
-
SoGLContext
public SoGLContext(boolean shared) Constructor which creates anSoGLContext
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 resultingSoGLContext
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
Constructor which creates anSoGLContext
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 Details
-
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
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 bySoGLContext
. 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 theSoGLFormat
, which is set to the main plane by default. -
getContextFromId
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
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 newSoGLContext
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
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 theSoGLContext
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. -
getFormat
Returns theSoGLFormat
associated to theSoGLContext
.- Returns:
- the
SoGLFormat
.
-