Working with EnvironmentObject

How to create a new EnvironmentObject

You can easily create a new EnvironmentObject (EO) by calling its default empty constructor.

// Creates a new instance of EnvironmentObject
vrecko::EnvironmentObject* eo = new vrecko::EnvironmentObject;

You may add the new EO directly to the scene either as a new node or as a child of one of the existing nodes.

// Option #1 - Add the EO directly to the scene graph.
world->getScenePtr()->addEnvironmentObject(eo);

// Option #2 - Add the EO as a child of an existing EO.
existingEO->addEOChild(eo);

In some cases, you might not wish to add the EO to the scene graph, yet still retain its ability to receive messages from vrecko. In that case, you may simply register the EO in the vrecko framework. This will also correctly assign the EO an appropriate ID.

// Register the EO in the vrecko framework
owner->pWorld->getScenePtr()->registerEnvironmentObject(eo);

How to add content to an existing EO

First of all, you can load any supported model from the disk and assign its geometry to the EO by a single call:

// Loads a 3D model from the disk.
eo->loadGeometry("plane.3ds");

Another option is to add any number of various OSG nodes as the EO’s child nodes.

// Create new instances of osg::Geode and osg::Group
osg::ref_ptr<osg::Geode> terrain = osg::Geode();
osg::ref_ptr<osg::Group> trees = osg::Group();

// Fill the group and geode with data
...

// Add the OSG nodes to the EO
eo->addChild(terrain);
eo->addChild(trees);

Attributes of the EnvironmentObject

Any EO can have its own set of attributes. Each attribute has a name and a numerical value (internally stored as int).

Attributes can be easily set in the input XML file:

<EnvironmentObject>
   <Geometry>
       <FileName>cube.obj</FileName>
   </Geometry>
   <Attribute Name="SELECTABLE">0</Attribute>
</EnvironmentObject>

Note: Attribute names are case-sensitive.

It is not necessary to define your own attributes in an XML file –  you can also simply create access them at run-time using one of the following methods:

  • void setAttribute(const std::string&, int) – Sets a value of the given attribute; creates a new attribute if necessary.
  • int getAttribute(const std::string&) – Returns value of an existing attribute. Returns 0 if the attribute doesn’t exist.
  • void deleteAttribute(const std::string&) – Deletes the (existing) attribute and its value.

These methods are inherited from vrecko::BaseClass which means that they are also available part of the classes vrecko::Ability and vrecko::Device. However, they are not used very much and they cannot be loaded from an XML file.

Note: std::map is used as an underlying structure, with attribute name being a key value.

Reserved attribute names

There are several attribute names which are reserved and used for specific purposes. These include:

  • LOCKED – Indicates that it is forbidden to modify/manipulate with the object at the moment. However, this rule is not enforced and should be manually checked on its own by each ability.
  • SELECTABLE – This attribute should be interpreted by all Abilities as “the user can select the object”.
  • MOVABLE – This attribute should be interpreted by all Abilities as “this object can be moved”.
  • NO_SNAP_TO – In the editor mode, it is possible to move an object with the ObjectMovement tool and automatically snap the object to the edges of adjacent objects while moving. This attribute can eliminate certain objects from this process.
  • CASTSHADOWS – DO NOT USE THIS ATTRIBUTE DIRECTLY. Instead use the EnvironmentObject::setCastShadows() method.
  • SmoothAfterLoadAfter loading the model, its surface is automatically smoothed.
  • NoOptimize – By default, osgUtil::Optimizer::optimize() is called after loading a model, to speed up the rendering. This attribute makes it possible to turn this functionality off (e.g. if you depend on the exact content of the mesh).
  • CollisionHierarchy – Obsolete attribute.
    In older versions of vrecko (2009 and earlier), the collision hierarchy was automatically generated through FDH structures. The calculations took a long time with more complicated models, therefore this function is disabled. It can be enabled again.
    However, the FDH hierarchy is almost never necessary anymore, therefore the default value of this variable is 0, i.e. the hierarchy is not generated.
  • FixGeometryMirroring – Highly specific attribute, used only by those working with models of chassis by Škoda.
    After loading objects, the normals for mirrored objects are saved in a nonstandard way. After setting this attribute to 1, all reflections are found and their normals are flipped.
  • RotateAfterLoad – Obsolete attribute.
    For compatibility with old versions of vrecko (2009 and earlier), where some models were automatically rotated after loading can this rotation be turned on again.