diff --git a/Make/makedirdefs b/Make/makedirdefs index 7539bef64..ad0f08c50 100644 --- a/Make/makedirdefs +++ b/Make/makedirdefs @@ -80,6 +80,7 @@ EXAMPLE_DIRS = \ osgcameragroup\ osgviewer\ osgconv\ + osghud\ osgtext\ osgstereoimage\ diff --git a/examples/osghud/Makefile b/examples/osghud/Makefile new file mode 100644 index 000000000..43f7f6f04 --- /dev/null +++ b/examples/osghud/Makefile @@ -0,0 +1,15 @@ +TOPDIR = ../.. +include $(TOPDIR)/Make/makedefs + +CXXFILES =\ + osghud.cpp\ + +LIBS += -losgText -losgProducer -lProducer $(OSG_LIBS) -L/usr/local/lib $(FREETYPE_LIB) $(GLUT_LIB) $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS) + +INSTFILES = \ + $(CXXFILES)\ + Makefile.inst=Makefile + +EXEC = osghud + +include $(TOPDIR)/Make/makerules diff --git a/examples/osghud/Makefile.inst b/examples/osghud/Makefile.inst new file mode 100644 index 000000000..4a276c5b7 --- /dev/null +++ b/examples/osghud/Makefile.inst @@ -0,0 +1,12 @@ +TOPDIR = ../.. +include $(TOPDIR)/Make/makedefs + +CXXFILES =\ + osghud.cpp\ + +LIBS += -osgText -losgProducer -lProducer $(OSG_LIBS) -L/usr/local/lib $(FREETYPE_LIB) $(GLUT_LIB) $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS) + +EXEC = osghud + + +include $(TOPDIR)/Make/makerules diff --git a/examples/osghud/osghud.cpp b/examples/osghud/osghud.cpp new file mode 100644 index 000000000..febd77827 --- /dev/null +++ b/examples/osghud/osghud.cpp @@ -0,0 +1,185 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + + +osg::Node* createHUD() +{ + osg::Geode* geode = new osg::Geode(); + + std::string timesFont("fonts/times.ttf"); + + // turn lighting off for the text and disable depth test to ensure its always ontop. + osg::StateSet* stateset = geode->getOrCreateStateSet(); + stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); + stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); + + osg::Vec3 position(150.0f,800.0f,0.0f); + osg::Vec3 delta(0.0f,-120.0f,0.0f); + + { + osgText::Text* text = new osgText::Text; + geode->addDrawable( text ); + + text->setFont(timesFont); + text->setText("Head Up Displays are simple :-)"); + text->setPosition(position); + + position += delta; + } + + + { + osgText::Text* text = new osgText::Text; + geode->addDrawable( text ); + + text->setFont(timesFont); + text->setText("All you need to do is create your text in a subgraph."); + text->setPosition(position); + + position += delta; + } + + + { + osgText::Text* text = new osgText::Text; + geode->addDrawable( text ); + + text->setFont(timesFont); + text->setText("Disable depth test in this subgraph to ensure its always ontop."); + text->setPosition(position); + + position += delta; + } + + { + osgText::Text* text = new osgText::Text; + geode->addDrawable( text ); + + text->setFont(timesFont); + text->setText("Then place a osg::Projection node above the subgraph\nto create an othrograph projection."); + text->setPosition(position); + + position += delta; + } + + { + osgText::Text* text = new osgText::Text; + geode->addDrawable( text ); + + text->setFont(timesFont); + text->setText("And add an osg::ModelViewMatrix set to ABSOLUTE to ensure\nit remains independent from any external model view matrices."); + text->setPosition(position); + + position += delta; + } + + // create the hud. + osg::MatrixTransform* modelview_abs = new osg::MatrixTransform; + modelview_abs->setReferenceFrame(osg::Transform::RELATIVE_TO_ABSOLUTE); + modelview_abs->setMatrix(osg::Matrix::identity()); + modelview_abs->addChild(geode); + + osg::Projection* projection = new osg::Projection; + projection->setMatrix(osg::Matrix::ortho2D(0,1280,0,1024)); + projection->addChild(modelview_abs); + + return projection; + +} + +int main( int argc, char **argv ) +{ + + // use an ArgumentParser object to manage the program arguments. + osg::ArgumentParser arguments(&argc,argv); + + // set up the usage document, in case we need to print out how to use this program. + arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] [filename] ..."); + arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); + + + // construct the viewer. + osgProducer::Viewer viewer(arguments); + + // set up the value with sensible default event handlers. + viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); + + // get details on keyboard and mouse bindings used by the viewer. + viewer.getUsage(*arguments.getApplicationUsage()); + + // if user request help write it out to cout. + if (arguments.read("-h") || arguments.read("--help")) + { + arguments.getApplicationUsage()->write(cout); + return 1; + } + + // any option left unread are converted into errors to write out later. + arguments.reportRemainingOptionsAsUnrecognized(); + + // report any errors if they have occured when parsing the program aguments. + if (arguments.errors()) + { + arguments.writeErrorMessages(cout); + return 1; + } + + + // read the scene from the list of file specified commandline args. + osg::ref_ptr scene = osgDB::readNodeFiles(arguments); + + osg::ref_ptr group = dynamic_cast(scene.get()); + if (!group) + { + group = new osg::Group; + group->addChild(scene.get()); + } + + // add the HUD subgraph. + group->addChild(createHUD()); + + // set the scene to render + viewer.setSceneData(group.get()); + + // create the windows and run the threads. + viewer.realize(Producer::CameraGroup::ThreadPerCamera); + + while( !viewer.done() ) + { + // wait for all cull and draw threads to complete. + viewer.sync(); + + // update the scene by traversing it with the the update visitor which will + // call all node update callbacks and animations. + viewer.update(); + + // fire off the cull and draw traversals of the scene. + viewer.frame(); + + } + + return 0; +}