2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osghud.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2003-03-04 06:47:50 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <osgUtil/Optimizer>
|
|
|
|
#include <osgDB/ReadFile>
|
2007-02-23 19:52:28 +08:00
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2007-02-23 19:52:28 +08:00
|
|
|
#include <osgViewer/CompositeViewer>
|
|
|
|
|
|
|
|
#include <osgGA/TrackballManipulator>
|
2003-03-04 06:47:50 +08:00
|
|
|
|
|
|
|
#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>
|
2006-11-27 22:52:07 +08:00
|
|
|
#include <osg/Camera>
|
2008-02-29 23:25:57 +08:00
|
|
|
#include <osg/RenderInfo>
|
|
|
|
|
|
|
|
#include <osgDB/WriteFile>
|
2003-03-04 06:47:50 +08:00
|
|
|
|
|
|
|
#include <osgText/Text>
|
|
|
|
|
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
osg::Camera* createHUD()
|
2003-03-04 06:47:50 +08:00
|
|
|
{
|
2009-05-19 23:11:49 +08:00
|
|
|
// create a camera to set up the projection and model view matrices, and the subgraph to draw in the HUD
|
2007-02-23 19:52:28 +08:00
|
|
|
osg::Camera* camera = new osg::Camera;
|
2003-03-04 06:47:50 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
// set the projection matrix
|
|
|
|
camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
|
2003-06-24 23:40:09 +08:00
|
|
|
|
2013-06-27 17:54:12 +08:00
|
|
|
// set the view matrix
|
2007-02-23 19:52:28 +08:00
|
|
|
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
|
|
|
|
camera->setViewMatrix(osg::Matrix::identity());
|
2003-03-04 06:47:50 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
// only clear the depth buffer
|
|
|
|
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
|
2003-03-04 06:47:50 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
// draw subgraph after main camera view.
|
|
|
|
camera->setRenderOrder(osg::Camera::POST_RENDER);
|
|
|
|
|
|
|
|
// we don't want the camera to grab event focus from the viewers main camera(s).
|
|
|
|
camera->setAllowEventFocus(false);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-04 06:47:50 +08:00
|
|
|
|
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
// add to this camera a subgraph to render
|
2003-03-04 06:47:50 +08:00
|
|
|
{
|
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
osg::Geode* geode = new osg::Geode();
|
2003-03-04 06:47:50 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
std::string timesFont("fonts/arial.ttf");
|
2003-03-04 06:47:50 +08:00
|
|
|
|
2009-05-19 23:11:49 +08:00
|
|
|
// turn lighting off for the text and disable depth test to ensure it's always ontop.
|
2007-02-23 19:52:28 +08:00
|
|
|
osg::StateSet* stateset = geode->getOrCreateStateSet();
|
|
|
|
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
|
2003-03-04 06:47:50 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
osg::Vec3 position(150.0f,800.0f,0.0f);
|
|
|
|
osg::Vec3 delta(0.0f,-120.0f,0.0f);
|
2005-12-10 03:54:31 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
{
|
|
|
|
osgText::Text* text = new osgText::Text;
|
|
|
|
geode->addDrawable( text );
|
2003-03-04 06:47:50 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
text->setFont(timesFont);
|
|
|
|
text->setPosition(position);
|
|
|
|
text->setText("Head Up Displays are simple :-)");
|
2003-03-04 06:47:50 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
position += delta;
|
2013-06-27 17:54:12 +08:00
|
|
|
}
|
2003-03-04 06:47:50 +08:00
|
|
|
|
|
|
|
|
2003-05-07 02:04:27 +08:00
|
|
|
{
|
2007-02-23 19:52:28 +08:00
|
|
|
osgText::Text* text = new osgText::Text;
|
|
|
|
geode->addDrawable( text );
|
2003-05-07 02:04:27 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
text->setFont(timesFont);
|
|
|
|
text->setPosition(position);
|
|
|
|
text->setText("All you need to do is create your text in a subgraph.");
|
2003-05-07 02:04:27 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
position += delta;
|
2013-06-27 17:54:12 +08:00
|
|
|
}
|
2003-05-07 02:04:27 +08:00
|
|
|
|
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
{
|
|
|
|
osgText::Text* text = new osgText::Text;
|
|
|
|
geode->addDrawable( text );
|
2003-05-07 02:04:27 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
text->setFont(timesFont);
|
|
|
|
text->setPosition(position);
|
|
|
|
text->setText("Then place an osg::Camera above the subgraph\n"
|
|
|
|
"to create an orthographic projection.\n");
|
2003-05-07 02:04:27 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
position += delta;
|
2013-06-27 17:54:12 +08:00
|
|
|
}
|
2003-03-04 06:47:50 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
{
|
|
|
|
osgText::Text* text = new osgText::Text;
|
|
|
|
geode->addDrawable( text );
|
2005-06-15 04:51:35 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
text->setFont(timesFont);
|
|
|
|
text->setPosition(position);
|
|
|
|
text->setText("Set the Camera's ReferenceFrame to ABSOLUTE_RF to ensure\n"
|
|
|
|
"it remains independent from any external model view matrices.");
|
2005-06-15 04:51:35 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
position += delta;
|
2013-06-27 17:54:12 +08:00
|
|
|
}
|
2005-06-15 04:51:35 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
{
|
|
|
|
osgText::Text* text = new osgText::Text;
|
|
|
|
geode->addDrawable( text );
|
2005-06-15 04:51:35 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
text->setFont(timesFont);
|
|
|
|
text->setPosition(position);
|
|
|
|
text->setText("And set the Camera's clear mask to just clear the depth buffer.");
|
2005-06-15 04:51:35 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
position += delta;
|
2013-06-27 17:54:12 +08:00
|
|
|
}
|
2005-06-15 04:51:35 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
{
|
|
|
|
osgText::Text* text = new osgText::Text;
|
|
|
|
geode->addDrawable( text );
|
|
|
|
|
|
|
|
text->setFont(timesFont);
|
|
|
|
text->setPosition(position);
|
|
|
|
text->setText("And finally set the Camera's RenderOrder to POST_RENDER\n"
|
2009-05-19 23:11:49 +08:00
|
|
|
"to make sure it's drawn last.");
|
2007-02-23 19:52:28 +08:00
|
|
|
|
|
|
|
position += delta;
|
2013-06-27 17:54:12 +08:00
|
|
|
}
|
2007-02-23 19:52:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
osg::BoundingBox bb;
|
|
|
|
for(unsigned int i=0;i<geode->getNumDrawables();++i)
|
|
|
|
{
|
2014-05-15 00:01:40 +08:00
|
|
|
bb.expandBy(geode->getDrawable(i)->getBoundingBox());
|
2007-02-23 19:52:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2013-06-27 17:54:12 +08:00
|
|
|
geom->setNormalArray(normals, osg::Array::BIND_OVERALL);
|
2007-02-23 19:52:28 +08:00
|
|
|
|
|
|
|
osg::Vec4Array* colors = new osg::Vec4Array;
|
|
|
|
colors->push_back(osg::Vec4(1.0f,1.0,0.8f,0.2f));
|
2013-06-27 17:54:12 +08:00
|
|
|
geom->setColorArray(colors, osg::Array::BIND_OVERALL);
|
2007-02-23 19:52:28 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
camera->addChild(geode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return camera;
|
2003-03-04 06:47:50 +08:00
|
|
|
}
|
|
|
|
|
2008-02-29 23:25:57 +08:00
|
|
|
struct SnapImage : public osg::Camera::DrawCallback
|
|
|
|
{
|
|
|
|
SnapImage(const std::string& filename):
|
|
|
|
_filename(filename),
|
|
|
|
_snapImage(false)
|
|
|
|
{
|
2013-06-27 17:54:12 +08:00
|
|
|
_image = new osg::Image;
|
2008-02-29 23:25:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void operator () (osg::RenderInfo& renderInfo) const
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!_snapImage) return;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2008-02-29 23:25:57 +08:00
|
|
|
osg::notify(osg::NOTICE)<<"Camera callback"<<std::endl;
|
|
|
|
|
|
|
|
osg::Camera* camera = renderInfo.getCurrentCamera();
|
|
|
|
osg::Viewport* viewport = camera ? camera->getViewport() : 0;
|
|
|
|
|
|
|
|
osg::notify(osg::NOTICE)<<"Camera callback "<<camera<<" "<<viewport<<std::endl;
|
|
|
|
|
|
|
|
if (viewport && _image.valid())
|
|
|
|
{
|
|
|
|
_image->readPixels(int(viewport->x()),int(viewport->y()),int(viewport->width()),int(viewport->height()),
|
|
|
|
GL_RGBA,
|
|
|
|
GL_UNSIGNED_BYTE);
|
|
|
|
osgDB::writeImageFile(*_image, _filename);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
|
|
|
osg::notify(osg::NOTICE)<<"Taken screenshot, and written to '"<<_filename<<"'"<<std::endl;
|
2008-02-29 23:25:57 +08:00
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2008-02-29 23:25:57 +08:00
|
|
|
_snapImage = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string _filename;
|
|
|
|
mutable bool _snapImage;
|
|
|
|
mutable osg::ref_ptr<osg::Image> _image;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SnapeImageHandler : public osgGA::GUIEventHandler
|
|
|
|
{
|
|
|
|
|
|
|
|
SnapeImageHandler(int key,SnapImage* si):
|
|
|
|
_key(key),
|
|
|
|
_snapImage(si) {}
|
|
|
|
|
2009-01-07 19:24:47 +08:00
|
|
|
bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&)
|
2008-02-29 23:25:57 +08:00
|
|
|
{
|
|
|
|
if (ea.getHandled()) return false;
|
|
|
|
|
|
|
|
switch(ea.getEventType())
|
|
|
|
{
|
|
|
|
case(osgGA::GUIEventAdapter::KEYUP):
|
|
|
|
{
|
|
|
|
if (ea.getKey() == _key)
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"event handler"<<std::endl;
|
|
|
|
_snapImage->_snapImage = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2008-02-29 23:25:57 +08:00
|
|
|
int _key;
|
|
|
|
osg::ref_ptr<SnapImage> _snapImage;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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);
|
2007-02-23 19:52:28 +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);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2009-05-19 23:11:49 +08:00
|
|
|
// if not loaded assume no arguments passed in, try use default model instead.
|
2011-06-15 00:54:20 +08:00
|
|
|
if (!scene) scene = osgDB::readNodeFile("dumptruck.osgt");
|
2013-06-27 17:54:12 +08:00
|
|
|
|
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
if (!scene)
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"No model loaded"<<std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-03-04 06:47:50 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
if (arguments.read("--Viewer"))
|
|
|
|
{
|
|
|
|
// construct the viewer.
|
|
|
|
osgViewer::Viewer viewer;
|
|
|
|
|
|
|
|
// create a HUD as slave camera attached to the master view.
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
viewer.setUpViewAcrossAllScreens();
|
2007-01-08 18:00:16 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
osgViewer::Viewer::Windows windows;
|
|
|
|
viewer.getWindows(windows);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
if (windows.empty()) return 1;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
osg::Camera* hudCamera = createHUD();
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2009-05-19 23:11:49 +08:00
|
|
|
// set up cameras to render on the first window available.
|
2007-02-23 19:52:28 +08:00
|
|
|
hudCamera->setGraphicsContext(windows[0]);
|
|
|
|
hudCamera->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height);
|
2003-03-04 06:47:50 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
viewer.addSlave(hudCamera, false);
|
|
|
|
|
|
|
|
// set the scene to render
|
|
|
|
viewer.setSceneData(scene.get());
|
|
|
|
|
|
|
|
return viewer.run();
|
|
|
|
|
|
|
|
}
|
|
|
|
if (arguments.read("--CompositeViewer"))
|
|
|
|
{
|
|
|
|
// construct the viewer.
|
|
|
|
osgViewer::CompositeViewer viewer;
|
|
|
|
|
|
|
|
// create the main 3D view
|
|
|
|
osgViewer::View* view = new osgViewer::View;
|
|
|
|
viewer.addView(view);
|
|
|
|
|
|
|
|
view->setSceneData(scene.get());
|
|
|
|
view->setUpViewAcrossAllScreens();;
|
|
|
|
view->setCameraManipulator(new osgGA::TrackballManipulator);
|
|
|
|
|
|
|
|
// now create the HUD camera's view
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
osgViewer::Viewer::Windows windows;
|
|
|
|
viewer.getWindows(windows);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
if (windows.empty()) return 1;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
osg::Camera* hudCamera = createHUD();
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2009-05-19 23:11:49 +08:00
|
|
|
// set up cameras to render on the first window available.
|
2007-02-23 19:52:28 +08:00
|
|
|
hudCamera->setGraphicsContext(windows[0]);
|
|
|
|
hudCamera->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
osgViewer::View* hudView = new osgViewer::View;
|
|
|
|
hudView->setCamera(hudCamera);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-02-23 19:52:28 +08:00
|
|
|
viewer.addView(hudView);
|
|
|
|
|
|
|
|
return viewer.run();
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// construct the viewer.
|
|
|
|
osgViewer::Viewer viewer;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2008-03-01 20:29:49 +08:00
|
|
|
SnapImage* postDrawCallback = new SnapImage("PostDrawCallback.png");
|
2013-06-27 17:54:12 +08:00
|
|
|
viewer.getCamera()->setPostDrawCallback(postDrawCallback);
|
2008-03-01 20:29:49 +08:00
|
|
|
viewer.addEventHandler(new SnapeImageHandler('p',postDrawCallback));
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2008-03-01 20:29:49 +08:00
|
|
|
SnapImage* finalDrawCallback = new SnapImage("FinalDrawCallback.png");
|
2013-06-27 17:54:12 +08:00
|
|
|
viewer.getCamera()->setFinalDrawCallback(finalDrawCallback);
|
2008-02-29 23:25:57 +08:00
|
|
|
viewer.addEventHandler(new SnapeImageHandler('f',finalDrawCallback));
|
2007-02-23 19:52:28 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osg::Group> group = new osg::Group;
|
|
|
|
|
2013-06-27 17:54:12 +08:00
|
|
|
// add the HUD subgraph.
|
2007-02-23 19:52:28 +08:00
|
|
|
if (scene.valid()) group->addChild(scene.get());
|
|
|
|
group->addChild(createHUD());
|
|
|
|
|
|
|
|
// set the scene to render
|
|
|
|
viewer.setSceneData(group.get());
|
|
|
|
|
|
|
|
return viewer.run();
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-04 06:47:50 +08:00
|
|
|
}
|