Menu

Tutorial 3 - HUD-type Text in OpenFrames

R. S.
Attachments
HUDWithCow-50.jpg (10408 bytes)

In this tutorial we build on Tutorial 2 by adding the display of HUD-type text to the view. The full listing of the program for this tutorial is here. For brevity we discuss only the code that has been added to the second tutorial. The text that we will show in a HUD-type display is the name(s) of the geometric model or models that have been loaded by this application.

The following code excerpt shows that we first define a string-type variable called "stringForHUD". It will hold the names of models that are loaded. Inside the for-loop we successively append to this string the names of models that were successfully loaded. The names are separated by a single space-

std::string stringForHUD;  // Holds model names for the HUD-type display.

// Create Models to hold user-specified objects.
for ( int i=1; i<argc; ++i )
{
  Model *theModel = new Model( "Model", 0.5, 0.5, 0.5, 0.9 );
  if ( !theModel->setModel( argv[i] ) )
  {
    continue;
  }

  // Add the model's name, sans any extension, to the window's title/name.
  std::string fname = osgDB::getSimpleFileName( argv[i] );
  std::string fname_noext = osgDB::getNameLessAllExtensions( fname );
  theModel->setName( fname_noext );
  windowName   += " " + fname_noext;

  stringForHUD += " " + fname_noext;  // Accumulate names for HUD display.

  // Reset model pivot so that its origin coincides with the root scene origin.
  theModel->setModelPivot( 0.0, 0.0, 0.0 );

  // Add the model to the scene.
  rootFrame->addChild( theModel );

  // Create a view for the model.
  View *modelView = new View( rootFrame, theModel );
  myWindow->getGridPosition(0, 0)->addView( modelView );
}

After loading the models we begin setting up the HUD display. As mentioned in Tutorial 2, OpenFrames relies on a package called OpenSceneGraph. That package has a class specifically for text- osgText::Text. We use a number of methods from that class, as shown here-

// Make some HUD-type text. First declare a text instance then set the font,
// text color and other characteristics.
osg::ref_ptr<osgText::Text> hudText = new osgText::Text;
hudText->setFont( "arial.ttf" );
hudText->setColor( osg::Vec4( 1, 1, 0, 1 ) );  // Red + green = yellow.
hudText->setCharacterSizeMode( osgText::Text::SCREEN_COORDS );
hudText->setCharacterSize( 20.0 );    // In pixels.
hudText->setFontResolution( 40, 40 ); // In texels (texture pixels).

We then position the text on-screen and specify its content as a literal string plus the accumulated model names-

// Set text's position (screen coords go from (0,0) bottom-left to (1,1) top-right).
hudText->setAlignment( osgText::Text::LEFT_BOTTOM );
hudText->setPosition( osg::Vec3( 0.025, 0.025, 0.0 ) );

// Now set the text to be the model(s') name(s).
hudText->setText( "Viewing:" + stringForHUD );

Finally, we add the text entity to myWindow, to make it visible-

// Attach text to HUD part of WindowProxy.
osg::Geode* geode = new osg::Geode;
geode->addDrawable( hudText );
myWindow->getGridPosition(0, 0)->getHUD()->addChild( geode );

In this image the results of running the following command- "tutorial2 cow.osg" - can be seen-

HUD with one model

As in the last tutorial, the scene has been slightly tilted and zoomed-in to better illustrate the effect. Notice however that despite the tilted view, the on-screen HUD text in the lower-left is upright. It will also remain at a constant size, as expected, regardless of the orientation of the scene's 3-D content.

This image-

HUD with two models

shows the results of running the command, "tutorial2 cow.osg cessna.osg". Once again the view has been tilted and zoomed-in for clarity. The HUD text this time includes the names of both object models (cow and cessna).


Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.