Click or drag to resize
SoCallback Class

Provides custom behavior during actions.

Inheritance Hierarchy

Namespace: OIV.Inventor.Nodes
Assembly: OIV.Inventor (in OIV.Inventor.dll) Version: 9.9.12.0 (9.9.12.0)
Syntax
public class SoCallback : SoNode

The SoCallback type exposes the following members.

Constructors
  NameDescription
Public methodSoCallback

Creates a callback node with default settings.

Top
Methods
  NameDescription
Public methodAffectsState

Returns true if a node has an effect on the state during traversal.

(Inherited from SoNode.)
Public methodCallback
Public methodCopy
Calls Copy(false).
(Inherited from SoNode.)
Public methodCopy(Boolean)

Creates and returns an exact copy of the node.

(Inherited from SoNode.)
Public methodCopyFieldValues(SoFieldContainer)
Calls CopyFieldValues(fc, false).
(Inherited from SoFieldContainer.)
Public methodCopyFieldValues(SoFieldContainer, Boolean)

Copies the contents of fc's fields into this object's fields.

(Inherited from SoFieldContainer.)
Public methodDispose
Releases all resources used by SoDisposable.
(Inherited from SoDisposable.)
Public methodDistribute
(Inherited from SoNode.)
Public methodDoAction
Public methodEnableNotify

Notification at this Field Container is enabled (if flag == true) or disabled (if flag == false).

(Inherited from SoFieldContainer.)
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Public methodFieldsAreEqual

Returns true if this object's fields are exactly equal to fc's fields.

(Inherited from SoFieldContainer.)
Public methodGet

Returns the values of the fields of this object in the Open Inventor ASCII file format in the given string.

(Inherited from SoFieldContainer.)
Public methodGetAllFields

Returns a list of fields, including the eventIn's and eventOut's.

(Inherited from SoFieldContainer.)
Public methodGetAlternateRep

This method is called by actions to allow the node to provide an "alternate representation" when appropriate (typically depending on the action type).

(Inherited from SoNode.)
Public methodGetBoundingBox
Public methodGetEventIn

Returns a the eventIn with the given name.

(Inherited from SoFieldContainer.)
Public methodGetEventOut

Returns the eventOut with the given name.

(Inherited from SoFieldContainer.)
Public methodGetField

Returns a the field of this object whose name is fieldName.

(Inherited from SoFieldContainer.)
Public methodGetFieldName

Returns the name of the given field in the fieldName argument.

(Inherited from SoFieldContainer.)
Public methodGetFields

Appends references to all of this object's fields to resultList, and returns the number of fields appended.

(Inherited from SoFieldContainer.)
Public methodGetHashCode
Overrides GetHashCode().
(Inherited from SoNetBase.)
Public methodGetMatrix
Public methodGetName

Returns the name of an instance.

(Inherited from SoBase.)
Public methodGetPrimitiveCount
Public methodGetRenderUnitID
(Inherited from SoNode.)
Public methodGetStringName (Inherited from SoBase.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodGLRender
Public methodGLRenderBelowPath
(Inherited from SoNode.)
Public methodGLRenderInPath
(Inherited from SoNode.)
Public methodGLRenderOffPath
(Inherited from SoNode.)
Public methodGrabEventsCleanup
(Inherited from SoNode.)
Public methodGrabEventsSetup
(Inherited from SoNode.)
Public methodHandleEvent
Public methodHasDefaultValues

Returns true if all of the object's fields have their default values.

(Inherited from SoFieldContainer.)
Public methodIsBoundingBoxIgnoring

This method is used by getBoundingBox action traversal to know if the current node must be traversed or not, ie the bounding should be ignored.

(Inherited from SoNode.)
Public methodIsNotifyEnabled

Notification is the process of telling interested objects that this object has changed.

(Inherited from SoFieldContainer.)
Public methodIsOverride

Returns the state of the override flag.

(Inherited from SoNode.)
Public methodIsSynchronizable

Gets the ScaleViz synchronizable state of this object.

(Inherited from SoBase.)
Public methodPick
Public methodRayPick
(Inherited from SoNode.)
Public methodSearch
Public methodSet

Sets one or more fields in this object to the values specified in the given string, which should be a string in the Open Inventor file format.

(Inherited from SoFieldContainer.)
Public methodSetCallback(SoCallbackCallbackCB) Obsolete.
Use CallbackHandler property instead. Sets the delegate to be invoked
Public methodSetCallback(SoCallbackCallbackUserDataCB, Object) Obsolete.
Use CallbackHandler property instead.
Public methodSetName (Inherited from SoBase.)
Public methodSetOverride

Turns the override flag on or off.

(Inherited from SoNode.)
Public methodSetSynchronizable

Sets this to be a ScaleViz synchronizable object.

(Inherited from SoBase.)
Public methodSetToDefaults

Sets all fields in this object to their default values.

(Inherited from SoFieldContainer.)
Public methodToString
Converts this SoBase structure to a human readable string.
(Inherited from SoBase.)
Public methodTouch

Marks an instance as modified, simulating a change to it.

(Inherited from SoNode.)
Public methodWrite
Top
Properties
  NameDescription
Public propertyCallbackHandler

Sets pointer to callback function and user data.

Public propertyIsDisposable
ISafeDisposable interface implementation.
(Inherited from SoDisposable.)
Public propertyUserData
Gets or sets the user data to be contained by the field container.
(Inherited from SoFieldContainer.)
Top
Remarks

This node provides a general mechanism for executing application code during a scene graph traversal, i.e. when an action is applied to the scene graph.

In some cases, implementing a new node class (a so-called custom node) derived from one of the existing classes is the best and cleanest solution. But for simple cases, and for prototyping, an SoCallback node may solve the problem.

You can use this node to get values from the traversal state list using the SoElement classes and, conversely, to modify values in the traversal state list. For example, SoLineWidthElement contains the current line width and is normally set by an SoDrawStyle node.

You can also use this node to make OpenGL calls while rendering, i.e. when an SoGLRenderAction is applied to the scene graph. If you do, be careful not to interfere with Open Inventor's use of OpenGL and be careful to follow render caching rules. This is discussed further below.

In general you should not use an SoCallback node to count the number of "frames" rendered. You're really counting the number of render traversals and the scene graph may be traversed multiple times to render a single frame. During a render traversal you can get the actual frame count by calling the SoGLRenderAction method GetFrameCounter().See below how to determine if the current action is a render action.

Create the node:

SoCallback callbackNode = new SoCallback();
callbackNode.CallbackHandler = new SoCallback.CallbackCB( myCallback );
root.AddChild( callbackNode );

Delegate: The delegatefunction registered with the node will be executed during traversal of the scene graph by various Open Inventor actions (not only the render action). Actions commonly applied to the scene graph include SoGLRenderAction, SoHandleEventAction, SoRayPickAction, SoGetBoundingBoxAction, SoSearchAction and SoCallbackAction. You should consider which actions the function should, or should not, be executed for. For example, calling OpenGL functions requires a current OpenGL context and is generally only possible during an SoGLRenderAction traversal. In the delegate you can check the action type, for example:

void myCallback( SoAction action )
{
  // Only handle specific actions
  if (action.GetType() == typeof(SoGLRenderAction)) {
    // Safe to call OpenGL functions
  }
}

Callbacks that modify the Open Inventor traversal state, e.g. setting SoLineWidthElement, generally should do that for all actions.

Getting a value from the traversal state list requires access to the SoState object for the current traversal. This information (and more) is available from the SoAction object passed to the callback. Modifying a value in the traversal state list requires the SoState object and, in most cases, a reference to the node setting the element. This information is also available from the SoAction object as the "tail" node of the action's current path, which is updated for each node visited as the action traverses the scene graph. Getting and modifying the traversal state is shown in the next example. The goal is to highlight some line geometry by making the lines 3X wider than whatever the current line width is. We get the current line width from the traversal state, multiply by three, then set the new line width in the state.

void myCallback( SoAction action )
{
 // Traversal state
 SoState state = action.GetState();

 // Get line width in traversal state
 float lineWidth = SoLineWidthElement.Get( state );

 // Scale up line width (may be zero)
 lineWidth = (lineWidth > 0) ? (3 * lineWidth) : 3;

 // Set new value in state
 SoLineWidthElement.Set( state, action.GetCurPath().GetTail(), lineWidth );
}

Cautions:

Most actions do a straightforward “depth first” traversal of the scene graph. In other words, nodes are visited exactly as organized in the scene graph. But SoGLRenderAction’s traversal of the scene graph for rendering is much more complex. The “behavior” implied by the structure of the scene graph is always respected. For example nodes inherit traversal state from above/left and SoSeparator nodes save and restore traversal state. However you cannot assume that there is a single traversal that exactly follows the structure of the scene graph. For example, some parts of the scene graph may be traversed “out of order” to simulate transparency correctly and the scene graph may be traversed multiple times to implement rendering effects like shadows. Because of this, there are some limitations on what can be done in the callback function.

  • Do NOT change the "structure" of the scene graph (insert or remove nodes). This may cause Open Inventor to crash. If such a change is required, schedule an SoOneShotSensor and do the work in the sensorÂ’s callback function. This function will be called at a safe time when no traversal is being done and the change will take effect on the next traversal.

  • Avoid changing fields of other nodes. This causes notification, which is inefficient during traversal and may cause sensors to trigger with undesired results. Also, some fields, if changed, invalidate information that is cached during traversal and may result in incorrect rendering. Think about whether it is really necessary to make this change during the render traversal. If your callback is responding to a change in the traversal state, then it may be possible (and generally is safer) to use the "Observer" pattern and attach an SoNodeSensor or SoFieldSensor to the node or field of interest. For example, attach an SoFieldSensor to the position field of the camera (see the viewerÂ’s getCamera() method) to make decisions based on the distance from the camera to some geometry. Whenever possible, make changes to attributes and properties by setting elements in the traversal state list instead of changing field values. For example, line width can (and should) be changed by setting the SoLineWidthElement. You can even control which child of an SoSwitch node is traversed, using the SoSwitchElement. See the "Action Behavior" section of each property nodeÂ’s documentation to see which elements it uses. If it is necessary to change a field based on information obtained during traversal, the best solution is to use an SoOneShotSensor as described in the previous point. The next best solution is to temporarily disable notification on the node that contains the field (see SoNode.enableNotify()).

OpenGL calls:

A common use for an SoCallback node is to call specific OpenGL functions during the render traversal that Open Inventor does not provide a node for. In general this is quite possible, because Open Inventor itself uses OpenGL for rendering. However there are issues to consider and because of newer nodes that have been added to Open Inventor it is less necessary than with older versions. Here are some general guidelines:

  • Whenever possible, use one of the standard nodes to change OpenGL state. In other words, do not make unnecessary calls directly to OpenGL. For example, use the SoViewport node to change the OpenGL viewport, use the SoDepthBuffer node to change the OpenGL depth buffer test and so on.

  • Do not change OpenGL state that Open Inventor depends on. ItÂ’s not easy to document all these cases. When in doubt send a question to the Hotline. In some cases it might be necessary to push and pop the OpenGL state (using two SoCallback nodes), but push/pop should be avoided for best performance.

  • Avoid querying OpenGL state. Many of these calls require the GPU to wait for all pending operations to finish before responding. This can significantly reduce performance. Get state information from the Open Inventor traversal state (SoElement) whenever possible. If querying information that doesnÂ’t change, only do the query once. If necessary, pushing and popping OpenGL state (glPushAttrib) is more efficient than querying and restoring state.

Render caching:

One of the most important and most effective optimizations in Open Inventor is automatic building of “render caches” (mainly at SoSeparator nodes) for “static” parts of the scene graph. A render cache encapsulates geometry and state (attributes and properties) that can be rendered without scene graph traversal. One advantage is that geometry in a render cache can be optimized for best performance. Another important advantage is that when an SoSeparator has a valid render cache, Open Inventor does not need to traverse its children, avoiding the CPU time to traverse those nodes. In general you should try to ensure that your SoCallback node can be render cached. In most cases SoCallback nodes can be cached even if they access/set traversal state, set OpenGL state or even render geometry using direct OpenGL calls.

If the callback makes OpenGL calls and has no dependency on other data, then in most cases the SoCallback node can be part of a render cache because the OpenGL calls will be recorded as part of the render cache. For example, an SoCallback node that simply calls glEnable() to enable GL_POINT_SPRITE probably can be safely render cached.

If the callback modifies the Open Inventor traversal state based on information from other nodes in the scene graph, then you may be able to get that information from the SoElement object(s) set by those nodes. See the line width example above. In that case the SoCallback node can be part of a render cache, because Open Inventor remembers that the render cache has a dependency on the specific value of the element(s) whose “get” method was called. On subsequent traversals, if the element’s value matches the saved value, then the render cache is still valid and be used. If the element’s value has changed, then the render cache is automatically discarded and the SoSeparator’s children are traversed, so the SoCallback node’s callback will be called again. On later traversals, if the value of the element is no longer changing, Open Inventor will eventually rebuild the render cache. The line width example above can be safely render cached.

In a few cases, the callback node may not be cacheable, depending on what it does. If a callback node relies on any information outside of Inventor that may change (such as a global application variable), it should not be cached, because the callback function will not be executed when the render cache is valid (SoCallback will not be traversed). Also, if the callback node renders geometry that changes often, then it probably should not be cached. We can imagine other cases where the callback function must be executed on every traversal. For example a temporary callback that prints a debug message on every traversal. To prevent Inventor from automatically creating a cache, call the SoCacheElement's invalidate() method in the callback function. But keep in mind that when you do this, it prevents caching by the callback node's parent and that node's parent and every SoSeparator up to the top of the scene graph. To minimize the performance hit, try to only put non-cacheable nodes near the top of the scene graph. Here is a non-cacheable callback:

void myCallback( SoAction action )
{
  // Only handle specific actions
  if (action.GetType() == typeof(SoGLRenderAction)) {

       // Traversal state
       SoState state = action.GetState();

       // This node must not be render cached (must be executed).
       SoCacheElement.Invalidate(state);

       Console.WriteLine( "Render traversal: {0}", m_counter++ };
   }
}

FILE FORMAT/DEFAULT

Callback { }

ACTION BEHAVIOR

SoGLRenderAction, SoGetBoundingBoxAction, SoPickAction Calls the specified delegatefor all actions.

See Also