Global fields are fields in the Inventor database that you can access by name and that are not contained in any specific node or engine. One built-in global field is provided: the realTime global field, which is of type SoSFTime( C++ | Java | .NET ). This field contains the current real-clock time and can be connected to fields or engines to create clock-based animation. You can create additional global fields as required. If you were creating a key-frame animation editor, for example, you might want to create a “current frame” field that could be connected to various engines. Once the field is created, you use the standard field methods to connect it to other parts of the scene graph.
Use the createGlobalField method of SoDB( C++ | Java | .NET ) to create a global field:
static SoField *createGlobalField(const SbName &name, SoType type);
static SoField CreateGlobalField(string name, Type type);
static SoField createGlobalField(String name, Class type);
There can be only one global field with a given name. If there is already a field with the given name and type, it is returned. If there is already a field with the given name, but it is of an incompatible type, NULL is returned.
The getGlobalField method returns the global field with the given name:
static SoField *getGlobalField(const SbName &name);
static SoField GetGlobalField(string name);
static SoField getGlobalField(String name);
The type of the returned field can be checked using the field class's getTypeId() method. For example,
if (globalField->isOfType(SoSFFloat::getClassTypeId()) ...
An example of using the realTime global field is
engineA->input1.connectFrom(SoDB::getGlobalField("realTime"));
engineA.input1.ConnectFrom(SoDB.GetGlobalField("realTime"));
engineA.input1.connectFrom(SoDB.getGlobalField("realTime"));
Example 15.1, “ Using the Real-Time Global Field ” creates a digital clock that connects an SoText3( C++ | Java | .NET ) string to the realTime global field. Figure 15.7, “ Scene Graph for the Digital Clock Example ” shows the scene graph for this example. Figure 15.8, “ Digital Clock That Uses the Real-Time Global Field ” shows the digital clock.
Example 15.1. Using the Real-Time Global Field
#include <Inventor/SoDB.h> #include <Inventor/Xt/SoXt.h> #include <Inventor/Xt/SoXtRenderArea.h> #include <Inventor/nodes/SoDirectionalLight.h> #include <Inventor/nodes/SoMaterial.h> #include <Inventor/nodes/SoPerspectiveCamera.h> #include <Inventor/nodes/SoSeparator.h> #include <Inventor/nodes/SoText3.h>
main(int , char **argv) { // Initialize Inventor and Xt Widget myWindow = SoXt::init(argv[0]); if (myWindow == NULL) exit(1); SoSeparator *root = new SoSeparator; root->ref(); // Add a camera, light, and material SoPerspectiveCamera *myCamera = new SoPerspectiveCamera; root->addChild(myCamera); root->addChild(new SoDirectionalLight); SoMaterial *myMaterial = new SoMaterial; myMaterial->diffuseColor.setValue(1.0, 0.0, 0.0); root->addChild(myMaterial); // Create a Text3 object, and connect to the realTime field SoText3 *myText = new SoText3; root->addChild(myText); myText->string.connectFrom(SoDB::getGlobalField("realTime")); SoXtRenderArea *myRenderArea = new SoXtRenderArea(myWindow); myCamera->viewAll(root, myRenderArea->getSize()); myRenderArea->setSceneGraph(root); myRenderArea->setTitle("Date & Time"); myRenderArea->show(); SoXt::show(myWindow); SoXt::mainLoop(); }
public void CreateSample() { SoSeparator root = new SoSeparator(); // Add a camera, light, and material SoPerspectiveCamera myCamera = new SoPerspectiveCamera(); root.AddChild(myCamera); root.AddChild(new SoDirectionalLight()); SoMaterial myMaterial = new SoMaterial(); myMaterial.diffuseColor.SetValue(1.0f, 0.0f, 0.0f); root.AddChild(myMaterial); // Create a Text3 object, and connect to the realTime field SoText3 myText = new SoText3(); root.AddChild(myText); myText.stringField.ConnectFrom(SoDB.GetGlobalField("realTime")); myRenderArea = new SoWinRenderArea(this, "", true, true, true); SbViewportRegion vpr = myRenderArea.GetViewportRegion(); myCamera.ViewAll(root, vpr); myRenderArea.SetSceneGraph(root); myRenderArea.SetTitle("Date & Time"); }
SoSeparator root = new SoSeparator(); // Add a camera, light, and material SoPerspectiveCamera myCamera = new SoPerspectiveCamera(); root.addChild(myCamera); root.addChild(new SoDirectionalLight()); SoMaterial myMaterial = new SoMaterial(); myMaterial.diffuseColor.setValue(1.0f, 0.0f, 0.0f); root.addChild(myMaterial); // Create a Text3 object, and connect to the realTime field SoText3 myText = new SoText3(); root.addChild(myText); myText.string.connectFrom(SoDB.getGlobalField("realTime")); myRenderArea = new SwRenderArea(); SbViewportRegion vpr = myRenderArea.getViewportRegion(); myCamera.viewAll(root, vpr); myRenderArea.setSceneGraph(root);