2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-03-04 06:47:50 +08:00
|
|
|
*
|
|
|
|
* 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 <osgUtil/Optimizer>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
#include <osgProducer/Viewer>
|
|
|
|
|
|
|
|
#include <osg/Material>
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/BlendFunc>
|
|
|
|
#include <osg/Depth>
|
2003-05-07 02:04:27 +08:00
|
|
|
#include <osg/PolygonOffset>
|
2003-03-04 06:47:50 +08:00
|
|
|
#include <osg/MatrixTransform>
|
2005-06-15 04:51:35 +08:00
|
|
|
#include <osg/CameraNode>
|
2003-03-04 06:47:50 +08:00
|
|
|
|
|
|
|
#include <osgText/Text>
|
|
|
|
|
|
|
|
|
|
|
|
osg::Node* createHUD()
|
|
|
|
{
|
|
|
|
osg::Geode* geode = new osg::Geode();
|
|
|
|
|
2003-05-07 02:04:27 +08:00
|
|
|
std::string timesFont("fonts/arial.ttf");
|
2003-03-04 06:47:50 +08:00
|
|
|
|
|
|
|
// 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);
|
2003-06-24 23:40:09 +08:00
|
|
|
|
2003-03-04 06:47:50 +08:00
|
|
|
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->setPosition(position);
|
2003-04-30 19:40:17 +08:00
|
|
|
text->setText("Head Up Displays are simple :-)");
|
2003-03-04 06:47:50 +08:00
|
|
|
|
|
|
|
position += delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
osgText::Text* text = new osgText::Text;
|
|
|
|
geode->addDrawable( text );
|
|
|
|
|
|
|
|
text->setFont(timesFont);
|
|
|
|
text->setPosition(position);
|
2003-04-30 19:40:17 +08:00
|
|
|
text->setText("All you need to do is create your text in a subgraph.");
|
2003-03-04 06:47:50 +08:00
|
|
|
|
|
|
|
position += delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
osgText::Text* text = new osgText::Text;
|
|
|
|
geode->addDrawable( text );
|
|
|
|
|
|
|
|
text->setFont(timesFont);
|
|
|
|
text->setPosition(position);
|
2005-12-10 03:54:31 +08:00
|
|
|
text->setText("Then place an osg::CameraNode above the subgraph\n"
|
|
|
|
"to create an orthographic projection.\n");
|
2003-03-04 06:47:50 +08:00
|
|
|
|
|
|
|
position += delta;
|
2005-12-10 03:54:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
osgText::Text* text = new osgText::Text;
|
|
|
|
geode->addDrawable( text );
|
|
|
|
|
|
|
|
text->setFont(timesFont);
|
|
|
|
text->setPosition(position);
|
|
|
|
text->setText("Set the CameraNode's ReferenceFrame to ABSOLUTE_RF to ensure\n"
|
|
|
|
"it remains independent from any external model view matrices.");
|
|
|
|
|
|
|
|
position += delta;
|
|
|
|
}
|
2003-03-04 06:47:50 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
osgText::Text* text = new osgText::Text;
|
|
|
|
geode->addDrawable( text );
|
|
|
|
|
|
|
|
text->setFont(timesFont);
|
|
|
|
text->setPosition(position);
|
2005-12-10 03:54:31 +08:00
|
|
|
text->setText("And set the CameraNode's clear mask to just clear the depth buffer.");
|
2003-03-04 06:47:50 +08:00
|
|
|
|
|
|
|
position += delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
osgText::Text* text = new osgText::Text;
|
|
|
|
geode->addDrawable( text );
|
|
|
|
|
|
|
|
text->setFont(timesFont);
|
|
|
|
text->setPosition(position);
|
2005-12-10 03:54:31 +08:00
|
|
|
text->setText("And finally set the CameraNode's RenderOrder to POST_RENDER\n"
|
|
|
|
"to make sure its drawn last.");
|
2003-03-04 06:47:50 +08:00
|
|
|
|
|
|
|
position += delta;
|
2005-12-10 03:54:31 +08:00
|
|
|
}
|
2003-05-07 02:04:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
osg::BoundingBox bb;
|
|
|
|
for(unsigned int i=0;i<geode->getNumDrawables();++i)
|
|
|
|
{
|
|
|
|
bb.expandBy(geode->getDrawable(i)->getBound());
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Geometry* geom = new osg::Geometry;
|
|
|
|
|
|
|
|
osg::Vec3Array* vertices = new osg::Vec3Array;
|
|
|
|
float depth = bb.zMin()-0.1;
|
|
|
|
vertices->push_back(osg::Vec3(bb.xMin(),bb.yMax(),depth));
|
|
|
|
vertices->push_back(osg::Vec3(bb.xMin(),bb.yMin(),depth));
|
|
|
|
vertices->push_back(osg::Vec3(bb.xMax(),bb.yMin(),depth));
|
|
|
|
vertices->push_back(osg::Vec3(bb.xMax(),bb.yMax(),depth));
|
|
|
|
geom->setVertexArray(vertices);
|
|
|
|
|
|
|
|
osg::Vec3Array* normals = new osg::Vec3Array;
|
|
|
|
normals->push_back(osg::Vec3(0.0f,0.0f,1.0f));
|
|
|
|
geom->setNormalArray(normals);
|
|
|
|
geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
|
|
|
|
|
|
|
|
osg::Vec4Array* colors = new osg::Vec4Array;
|
|
|
|
colors->push_back(osg::Vec4(1.0f,1.0,0.8f,0.2f));
|
|
|
|
geom->setColorArray(colors);
|
|
|
|
geom->setColorBinding(osg::Geometry::BIND_OVERALL);
|
|
|
|
|
|
|
|
geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));
|
|
|
|
|
|
|
|
osg::StateSet* stateset = geom->getOrCreateStateSet();
|
|
|
|
stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
|
|
|
|
//stateset->setAttribute(new osg::PolygonOffset(1.0f,1.0f),osg::StateAttribute::ON);
|
|
|
|
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
|
|
|
|
|
|
|
|
geode->addDrawable(geom);
|
|
|
|
}
|
2003-03-04 06:47:50 +08:00
|
|
|
|
2005-06-15 04:51:35 +08:00
|
|
|
osg::CameraNode* camera = new osg::CameraNode;
|
|
|
|
|
|
|
|
// set the projection matrix
|
|
|
|
camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
|
|
|
|
|
|
|
|
// set the view matrix
|
|
|
|
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
|
|
|
|
camera->setViewMatrix(osg::Matrix::identity());
|
|
|
|
|
|
|
|
// only clear the depth buffer
|
|
|
|
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
// draw subgraph after main camera view.
|
|
|
|
camera->setRenderOrder(osg::CameraNode::POST_RENDER);
|
|
|
|
|
|
|
|
camera->addChild(geode);
|
|
|
|
|
|
|
|
return camera;
|
|
|
|
|
2003-03-04 06:47:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2003-04-09 19:44:32 +08:00
|
|
|
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates how to do Head Up Displays.");
|
2003-04-07 05:32:44 +08:00
|
|
|
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] [filename] ...");
|
2003-03-04 06:47:50 +08:00
|
|
|
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"))
|
|
|
|
{
|
2003-03-05 03:52:32 +08:00
|
|
|
arguments.getApplicationUsage()->write(std::cout);
|
2003-03-04 06:47:50 +08:00
|
|
|
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())
|
|
|
|
{
|
2003-03-05 03:52:32 +08:00
|
|
|
arguments.writeErrorMessages(std::cout);
|
2003-03-04 06:47:50 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2003-04-07 05:32:44 +08:00
|
|
|
|
2003-03-04 06:47:50 +08:00
|
|
|
// read the scene from the list of file specified commandline args.
|
|
|
|
osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
|
|
|
|
|
2003-11-21 22:17:26 +08:00
|
|
|
osg::ref_ptr<osg::Group> group = new osg::Group;
|
|
|
|
|
2003-03-04 06:47:50 +08:00
|
|
|
// add the HUD subgraph.
|
2003-11-21 22:17:26 +08:00
|
|
|
if (scene.valid()) group->addChild(scene.get());
|
2003-03-04 06:47:50 +08:00
|
|
|
group->addChild(createHUD());
|
|
|
|
|
|
|
|
// set the scene to render
|
|
|
|
viewer.setSceneData(group.get());
|
|
|
|
|
|
|
|
// create the windows and run the threads.
|
2003-04-08 23:18:45 +08:00
|
|
|
viewer.realize();
|
2003-03-04 06:47:50 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-08-03 03:55:03 +08:00
|
|
|
// wait for all cull and draw threads to complete.
|
|
|
|
viewer.sync();
|
|
|
|
|
|
|
|
// run a clean up frame to delete all OpenGL objects.
|
|
|
|
viewer.cleanup_frame();
|
|
|
|
|
|
|
|
// wait for all the clean up frame to complete.
|
2003-03-25 18:05:09 +08:00
|
|
|
viewer.sync();
|
|
|
|
|
2003-03-04 06:47:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|