Class SoGLContext
- java.lang.Object
-
- com.openinventor.inventor.Inventor
-
- com.openinventor.inventor.devices.SoDeviceContext
-
- com.openinventor.inventor.devices.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() anSoGLContext
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(); 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 theSoGLContext
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 Classes Modifier and Type Class Description static class
SoGLContext.SharedGroupPolicies
Sharing Policy.-
Nested 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
Constructors Constructor Description SoGLContext(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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
assertContext()
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.SbGPUCapabilities
getContextGraphicsCapabilities()
Retrieve graphics capabilities from this context.static SoGLContext
getCurrent()
Calls getCurrent((boolean)false).static SoGLContext
getCurrent(boolean checkGLState)
Returns the current active OpenGL context (if any).SoGLFormat
getFormat()
Returns theSoGLFormat
associated to theSoGLContext
.static SbGPUCapabilities
getGraphicsCapabilities()
Retrieve graphics capabilities from the current bound context, if any.int
getId()
Returns the internal id for this context.static SoGLContext.SharedGroupPolicies
getSharedGroupPolicy()
Returns the current sharedGroupPolicy.void
invalidate()
Set this context as invalid so it won't be used anymore.boolean
isValidForCurrent()
Returns true if the context is the current active context or if it is shared with the current active context.void
setNoGLContextDelete()
Prevent deletion of the native OpenGL context bySoGLContext
.boolean
swapBuffers()
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 Detail
-
SoGLContext
public SoGLContext(SoGLContext context, boolean shared)
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.
-
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.
-
-
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.
-
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 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).
-
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 bySoGLContext
. Note: Can be useful when the OpenGL context is created and managed by the application or a third-party library such as Qt.
-
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.
-
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
public SoGLFormat getFormat()
Returns theSoGLFormat
associated to theSoGLContext
.- Returns:
- the
SoGLFormat
.
-
getSharedGroupPolicy
public static SoGLContext.SharedGroupPolicies getSharedGroupPolicy()
Returns the current sharedGroupPolicy.
-
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.
-
swapBuffers
public boolean swapBuffers()
Swaps the buffers with the value stored in theSoGLFormat
, which is set to the main plane by default.
-
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...).
-
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.
-
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.
-
-