Open Inventor Release 2024.2.0
 
Loading...
Searching...
No Matches
Global Fields

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. 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 to create a global field:

C++ :

static SoField* createGlobalField( const SbName& name, SoType type );

C# :

static SoField CreateGlobalField( string name, Type type );

Java :

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:

C++ :

static SoField* getGlobalField( const SbName& name );

C# :

static SoField GetGlobalField( string name );

Java :

static SoField getGlobalField( String name );

C++ : The type of the returned field can be checked using the field class's getTypeId() method. For example,

C++ :

if (globalField->isOfType(SoSFFloat::getClassTypeId()) ...

An example of using the realTime global field is

C++ :

engineA->input1.connectFrom( SoDB::getGlobalField( "realTime" ) );

C# :

engineA.input1.ConnectFrom( SoDB.GetGlobalField( "realTime" ) );

Java :

engineA.input1.connectFrom( SoDB.getGlobalField( "realTime" ) );

Using the Real-Time Global Field creates a digital clock that connects an SoText3 string to the realTime global field. Scene Graph for the Digital Clock Example shows the scene graph for this example. Digital Clock That Uses the Real-Time Global Field shows the digital clock.

Example : Using the Real-Time Global Field

C++ :

#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>

Scene Graph for the Digital Clock Example

Digital Clock That Uses the Real-Time Global Field

C++ :

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();
}

C# :

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" );
}

Java :

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 );