Menu

Tutorial3.cpp

R. S.
/***********************************
   Copyright 2018 Ravishankar Mathur

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
***********************************/

#include <OpenFrames/FrameManager.hpp>
#include <OpenFrames/Model.hpp>
#include <OpenFrames/WindowProxy.hpp>
#include <osg/ArgumentParser>
#include <osgDB/FileNameUtils>

///////////////////////////////////////////////////
//
//   Tutorial 3 - HUD-type display in OpenFrames.
//
///////////////////////////////////////////////////

int main( int argc, char** argv )
{
  // Create the window interface.
  unsigned int x = 30, y = 30;
  unsigned int winWidth = 800, winHeight = 600;
  unsigned int numRows = 1, numCols = 1;
  osg::ref_ptr<OpenFrames::WindowProxy> myWindow = new OpenFrames::WindowProxy( x, y, winWidth, winHeight, numRows, numCols );

  // Create the root reference frame that will hold all specified models.
  osg::ref_ptr<OpenFrames::ReferenceFrame> rootFrame = new OpenFrames::ReferenceFrame( "Root" );
  rootFrame->showAxes( OpenFrames::ReferenceFrame::NO_AXES );
  rootFrame->showAxesLabels( OpenFrames::ReferenceFrame::NO_AXES );
  rootFrame->showNameLabel( false );

  // Create a frame manager for the scene.
  osg::ref_ptr<OpenFrames::FrameManager> myFrmMgr = new OpenFrames::FrameManager;
  myFrmMgr->setFrame( rootFrame );

  // Now place the scene contained in myFrmMgr at the given location.
  myWindow->setScene( myFrmMgr, 0, 0 );

  // Set up the window title.
  std::string windowName = "OpenFrames Viewer:";

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

  // Try to load user-specified files/models.
  for ( int i=1; i<argc; ++i )
  {
    OpenFrames::Model *theModel = new OpenFrames::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.
    OpenFrames::View *modelView = new OpenFrames::View( rootFrame, theModel );
    myWindow->getGridPosition(0, 0)->addView( modelView );
  }

  if ( 0 == rootFrame->getNumChildren() )
  {
    OSG_WARN << std::endl << "No models loaded, exiting." << std::endl;
    return 1;
  }

  // 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).

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

  myWindow->setWindowName( windowName );

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

  myWindow->startThread();  // Start window animation.
  myWindow->join();         // Wait for window animation to finish.

  return 0;
}

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.