Provides custom behavior during actions. More...
#include <Inventor/nodes/SoCallback.h>
Public Types | |
typedef void | SoCallbackCB(void *userData, SoAction *action) |
Callback functions registered with this node should be of this type. | |
Public Member Functions | |
virtual SoType | getTypeId () const |
Returns the type identifier for this specific instance. | |
SoCallback () | |
Creates a callback node with default settings. | |
void | setCallback (SoCallbackCB *func, void *localUserData=NULL) |
Sets pointer to callback function and user data. | |
Public Member Functions inherited from SoNode | |
virtual void | setOverride (const SbBool state) |
Turns the override flag on or off. | |
virtual SbBool | isOverride () const |
Returns the state of the override flag. | |
virtual SoNode * | copy (SbBool copyConnections=FALSE) const |
Creates and returns an exact copy of the node. | |
virtual SbBool | affectsState () const |
Returns TRUE if a node has an effect on the state during traversal. | |
virtual void | touch () |
Marks an instance as modified, simulating a change to it. | |
Public Member Functions inherited from SoFieldContainer | |
void | setToDefaults () |
Sets all fields in this object to their default values. | |
SbBool | hasDefaultValues () const |
Returns TRUE if all of the object's fields have their default values. | |
SbBool | fieldsAreEqual (const SoFieldContainer *fc) const |
Returns TRUE if this object's fields are exactly equal to fc's fields. | |
void | copyFieldValues (const SoFieldContainer *fc, SbBool copyConnections=FALSE) |
Copies the contents of fc's fields into this object's fields. | |
SoNONUNICODE SbBool | set (const char *fieldDataString) |
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. | |
SbBool | set (const SbString &fieldDataString) |
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. | |
void | get (SbString &fieldDataString) |
Returns the values of the fields of this object in the Open Inventor ASCII file format in the given string. | |
virtual int | getFields (SoFieldList &list) const |
Appends references to all of this object's fields to resultList, and returns the number of fields appended. | |
virtual int | getAllFields (SoFieldList &list) const |
Returns a list of fields, including the eventIn's and eventOut's. | |
virtual SoField * | getField (const SbName &fieldName) const |
Returns a the field of this object whose name is fieldName. | |
virtual SoField * | getEventIn (const SbName &fieldName) const |
Returns a the eventIn with the given name. | |
virtual SoField * | getEventOut (const SbName &fieldName) const |
Returns the eventOut with the given name. | |
SbBool | getFieldName (const SoField *field, SbName &fieldName) const |
Returns the name of the given field in the fieldName argument. | |
SbBool | enableNotify (SbBool flag) |
Notification at this Field Container is enabled (if flag == TRUE) or disabled (if flag == FALSE). | |
SbBool | isNotifyEnabled () const |
Notification is the process of telling interested objects that this object has changed. | |
virtual void | setUserData (void *data) |
Sets application data. | |
void * | getUserData (void) const |
Gets user application data. | |
Public Member Functions inherited from SoBase | |
virtual SbName | getName () const |
Returns the name of an instance. | |
virtual void | setName (const SbName &name) |
Sets the name of an instance. | |
void | setSynchronizable (const bool b) |
Sets this to be a ScaleViz synchronizable object. | |
bool | isSynchronizable () const |
Gets the ScaleViz synchronizable state of this object. | |
Public Member Functions inherited from SoRefCounter | |
void | ref () const |
Adds a reference to an instance. | |
void | unref () const |
Removes a reference from an instance. | |
void | unrefNoDelete () const |
unrefNoDelete() should be called when it is desired to decrement the reference count, but not delete the instance if this brings the reference count to zero. | |
int | getRefCount () const |
Returns current reference count. | |
void | lock () const |
lock this instance. | |
void | unlock () const |
unlock this instance. | |
Public Member Functions inherited from SoTypedObject | |
SbBool | isOfType (const SoType &type) const |
Returns TRUE if this object is of the type specified in type or is derived from that type. | |
template<typename TypedObjectClass > | |
SbBool | isOfType () const |
Returns TRUE if this object is of the type of class TypedObjectClass or is derived from that class. | |
Static Public Member Functions | |
static SoType | getClassTypeId () |
Returns the type identifier for this class. | |
Static Public Member Functions inherited from SoNode | |
static SoType | getClassTypeId () |
Returns the type identifier for this class. | |
static SoNode * | getByName (const SbName &name) |
A node's name can be set using SoBase::setName(). | |
static int | getByName (const SbName &name, SoNodeList &list) |
A node's name can be set using SoBase::setName(). | |
Static Public Member Functions inherited from SoFieldContainer | |
static SoType | getClassTypeId () |
Returns the type of this class. | |
Static Public Member Functions inherited from SoBase | |
static SoType | getClassTypeId () |
Returns type identifier for this class. | |
Static Public Member Functions inherited from SoTypedObject | |
static SoType | getClassTypeId () |
Returns the type identifier for this class. | |
Provides custom behavior during actions.
This node provides a general mechanism for executing some code during an Open Inventor 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.
This node cannot be used to make OpenGL calls. OpenGL calls can only be made using an SoGLCallback node. (This is a change starting with Open Inventor 10.0)
This node cannot be used to modify the structure of the scene graph. This may cause a crash. This node should not be used to modify fields in other nodes. The same effect can usually be obtained by setting the corresponding 'element'. See the Cautions section below for more details.
Create the node:
Create the Callback:
The callback function 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. In the callback you can check the action type, for example:
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 pointer 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.
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.
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.
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). 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:
SoAction, SoCallbackAction, SoEventCallback, SoGLCallback
Definition at line 441 of file SoCallback.h.
typedef void SoCallbackCB(void *userData, SoAction *action) |
Callback functions registered with this node should be of this type.
Definition at line 80 of file SoCallback.h.
SoCallback::SoCallback | ( | ) |
Creates a callback node with default settings.
|
static |
Returns the type identifier for this class.
|
virtual |
Returns the type identifier for this specific instance.
Reimplemented from SoNode.
Reimplemented in SoGLCallback.
|
inline |
Sets pointer to callback function and user data.
By default, the function pointer in the node is NULL and does nothing.
Definition at line 456 of file SoCallback.h.