2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgpick.
|
|
|
|
*
|
|
|
|
* 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-04-13 21:26:41 +08:00
|
|
|
*/
|
2007-06-12 22:20:16 +08:00
|
|
|
|
2003-04-13 21:26:41 +08:00
|
|
|
/* osgpick sample
|
|
|
|
* demonstrate use of osgUtil/PickVisitor for picking in a HUD or
|
|
|
|
* in a 3d scene,
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <osgUtil/Optimizer>
|
|
|
|
#include <osgDB/ReadFile>
|
2007-01-10 21:52:22 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2008-03-04 22:19:03 +08:00
|
|
|
#include <osgViewer/CompositeViewer>
|
2003-04-13 21:26:41 +08:00
|
|
|
|
|
|
|
#include <osg/Material>
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/BlendFunc>
|
|
|
|
#include <osg/Depth>
|
|
|
|
#include <osg/Projection>
|
|
|
|
#include <osg/MatrixTransform>
|
2006-11-27 22:52:07 +08:00
|
|
|
#include <osg/Camera>
|
2005-04-08 17:51:56 +08:00
|
|
|
#include <osg/io_utils>
|
2003-04-13 21:26:41 +08:00
|
|
|
|
|
|
|
#include <osgText/Text>
|
|
|
|
|
2003-04-25 21:47:33 +08:00
|
|
|
#include <sstream>
|
2003-04-16 04:54:10 +08:00
|
|
|
|
2003-04-13 21:26:41 +08:00
|
|
|
// class to handle events with a pick
|
|
|
|
class PickHandler : public osgGA::GUIEventHandler {
|
|
|
|
public:
|
|
|
|
|
2007-01-10 21:52:22 +08:00
|
|
|
PickHandler(osgText::Text* updateText):
|
2003-04-13 21:26:41 +08:00
|
|
|
_updateText(updateText) {}
|
|
|
|
|
|
|
|
~PickHandler() {}
|
|
|
|
|
2007-01-10 21:52:22 +08:00
|
|
|
bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa);
|
2003-04-13 21:26:41 +08:00
|
|
|
|
2008-03-04 22:19:03 +08:00
|
|
|
virtual void pick(osgViewer::View* view, const osgGA::GUIEventAdapter& ea);
|
2003-04-13 21:26:41 +08:00
|
|
|
|
|
|
|
void setLabel(const std::string& name)
|
|
|
|
{
|
|
|
|
if (_updateText.get()) _updateText->setText(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
osg::ref_ptr<osgText::Text> _updateText;
|
|
|
|
};
|
|
|
|
|
2007-01-10 21:52:22 +08:00
|
|
|
bool PickHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
|
2003-04-13 21:26:41 +08:00
|
|
|
{
|
|
|
|
switch(ea.getEventType())
|
|
|
|
{
|
2008-03-04 22:19:03 +08:00
|
|
|
case(osgGA::GUIEventAdapter::PUSH):
|
2003-04-13 21:26:41 +08:00
|
|
|
{
|
2008-03-04 22:19:03 +08:00
|
|
|
osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
|
|
|
|
if (view) pick(view,ea);
|
2007-01-10 21:52:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
2003-04-13 21:26:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-04 22:19:03 +08:00
|
|
|
void PickHandler::pick(osgViewer::View* view, const osgGA::GUIEventAdapter& ea)
|
2003-04-13 21:26:41 +08:00
|
|
|
{
|
2007-01-10 21:52:22 +08:00
|
|
|
osgUtil::LineSegmentIntersector::Intersections intersections;
|
|
|
|
|
Added convinence methods to osgProducer::Viewer:
/** compute, from normalized mouse coords, for sepecified Camera, the pixel coords relative to that Camera's RenderSurface.*/
bool computePixelCoords(float x,float y,unsigned int cameraNum,float& pixel_x,float& pixel_y);
/** compute, from normalized mouse coords, for sepecified Camera, the near and far points in worlds coords.*/
bool computeNearFar(float x,float y,unsigned int cameraNum,osg::Vec3& near, osg::Vec3& far);
/** compute, from normalized mouse coords, for sepecified Camera, intersections with the scene.*/
bool computeIntersections(float x,float y,unsigned int cameraNum,osgUtil::IntersectVisitor::HitList& hits);
/** compute, from normalized mouse coords, for all Cameras, intersections with the scene.*/
bool computeIntersections(float x,float y,osgUtil::IntersectVisitor::HitList& hits);
2003-04-16 22:17:49 +08:00
|
|
|
std::string gdlist="";
|
2008-03-04 22:19:03 +08:00
|
|
|
float x = ea.getX();
|
|
|
|
float y = ea.getY();
|
|
|
|
if (view->computeIntersections(x,y,intersections))
|
2003-04-13 21:26:41 +08:00
|
|
|
{
|
2007-01-10 21:52:22 +08:00
|
|
|
for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
|
|
|
|
hitr != intersections.end();
|
Added convinence methods to osgProducer::Viewer:
/** compute, from normalized mouse coords, for sepecified Camera, the pixel coords relative to that Camera's RenderSurface.*/
bool computePixelCoords(float x,float y,unsigned int cameraNum,float& pixel_x,float& pixel_y);
/** compute, from normalized mouse coords, for sepecified Camera, the near and far points in worlds coords.*/
bool computeNearFar(float x,float y,unsigned int cameraNum,osg::Vec3& near, osg::Vec3& far);
/** compute, from normalized mouse coords, for sepecified Camera, intersections with the scene.*/
bool computeIntersections(float x,float y,unsigned int cameraNum,osgUtil::IntersectVisitor::HitList& hits);
/** compute, from normalized mouse coords, for all Cameras, intersections with the scene.*/
bool computeIntersections(float x,float y,osgUtil::IntersectVisitor::HitList& hits);
2003-04-16 22:17:49 +08:00
|
|
|
++hitr)
|
2003-04-13 21:26:41 +08:00
|
|
|
{
|
2003-04-25 21:47:33 +08:00
|
|
|
std::ostringstream os;
|
2007-01-10 21:52:22 +08:00
|
|
|
if (!hitr->nodePath.empty() && !(hitr->nodePath.back()->getName().empty()))
|
2003-04-13 21:26:41 +08:00
|
|
|
{
|
2003-04-25 21:47:33 +08:00
|
|
|
// the geodes are identified by name.
|
2007-01-10 21:52:22 +08:00
|
|
|
os<<"Object \""<<hitr->nodePath.back()->getName()<<"\""<<std::endl;
|
2003-04-25 21:47:33 +08:00
|
|
|
}
|
2007-01-10 21:52:22 +08:00
|
|
|
else if (hitr->drawable.valid())
|
2003-04-25 21:47:33 +08:00
|
|
|
{
|
2007-01-10 21:52:22 +08:00
|
|
|
os<<"Object \""<<hitr->drawable->className()<<"\""<<std::endl;
|
2003-04-25 21:47:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
os<<" local coords vertex("<< hitr->getLocalIntersectPoint()<<")"<<" normal("<<hitr->getLocalIntersectNormal()<<")"<<std::endl;
|
|
|
|
os<<" world coords vertex("<< hitr->getWorldIntersectPoint()<<")"<<" normal("<<hitr->getWorldIntersectNormal()<<")"<<std::endl;
|
2007-01-10 21:52:22 +08:00
|
|
|
const osgUtil::LineSegmentIntersector::Intersection::IndexList& vil = hitr->indexList;
|
2003-04-25 21:47:33 +08:00
|
|
|
for(unsigned int i=0;i<vil.size();++i)
|
|
|
|
{
|
|
|
|
os<<" vertex indices ["<<i<<"] = "<<vil[i]<<std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
gdlist += os.str();
|
2003-04-13 21:26:41 +08:00
|
|
|
}
|
|
|
|
}
|
Added convinence methods to osgProducer::Viewer:
/** compute, from normalized mouse coords, for sepecified Camera, the pixel coords relative to that Camera's RenderSurface.*/
bool computePixelCoords(float x,float y,unsigned int cameraNum,float& pixel_x,float& pixel_y);
/** compute, from normalized mouse coords, for sepecified Camera, the near and far points in worlds coords.*/
bool computeNearFar(float x,float y,unsigned int cameraNum,osg::Vec3& near, osg::Vec3& far);
/** compute, from normalized mouse coords, for sepecified Camera, intersections with the scene.*/
bool computeIntersections(float x,float y,unsigned int cameraNum,osgUtil::IntersectVisitor::HitList& hits);
/** compute, from normalized mouse coords, for all Cameras, intersections with the scene.*/
bool computeIntersections(float x,float y,osgUtil::IntersectVisitor::HitList& hits);
2003-04-16 22:17:49 +08:00
|
|
|
setLabel(gdlist);
|
2003-04-13 21:26:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
osg::Node* createHUD(osgText::Text* updateText)
|
2005-11-02 23:23:54 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
// create the hud. derived from osgHud.cpp
|
2003-04-13 21:26:41 +08:00
|
|
|
// adds a set of quads, each in a separate Geode - which can be picked individually
|
|
|
|
// eg to be used as a menuing/help system!
|
|
|
|
// Can pick texts too!
|
2005-11-02 23:23:54 +08:00
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
osg::Camera* hudCamera = new osg::Camera;
|
2005-11-02 23:23:54 +08:00
|
|
|
hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
|
|
|
|
hudCamera->setProjectionMatrixAsOrtho2D(0,1280,0,1024);
|
|
|
|
hudCamera->setViewMatrix(osg::Matrix::identity());
|
2006-11-27 22:52:07 +08:00
|
|
|
hudCamera->setRenderOrder(osg::Camera::POST_RENDER);
|
2005-11-02 23:23:54 +08:00
|
|
|
hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
|
2003-04-13 21:26:41 +08:00
|
|
|
|
|
|
|
std::string timesFont("fonts/times.ttf");
|
|
|
|
|
|
|
|
// turn lighting off for the text and disable depth test to ensure its always ontop.
|
|
|
|
osg::Vec3 position(150.0f,800.0f,0.0f);
|
|
|
|
osg::Vec3 delta(0.0f,-60.0f,0.0f);
|
|
|
|
|
|
|
|
{
|
|
|
|
osg::Geode* geode = new osg::Geode();
|
|
|
|
osg::StateSet* stateset = geode->getOrCreateStateSet();
|
|
|
|
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
|
|
|
|
stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
|
|
|
|
geode->setName("simple");
|
2005-11-02 23:23:54 +08:00
|
|
|
hudCamera->addChild(geode);
|
2003-04-13 21:26:41 +08:00
|
|
|
|
|
|
|
osgText::Text* text = new osgText::Text;
|
|
|
|
geode->addDrawable( text );
|
|
|
|
|
|
|
|
text->setFont(timesFont);
|
2005-12-16 22:53:51 +08:00
|
|
|
text->setText("Picking in Head Up Displays is simple!");
|
2003-04-13 21:26:41 +08:00
|
|
|
text->setPosition(position);
|
|
|
|
|
|
|
|
position += delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (int i=0; i<5; i++) {
|
|
|
|
osg::Vec3 dy(0.0f,-30.0f,0.0f);
|
|
|
|
osg::Vec3 dx(120.0f,0.0f,0.0f);
|
|
|
|
osg::Geode* geode = new osg::Geode();
|
|
|
|
osg::StateSet* stateset = geode->getOrCreateStateSet();
|
|
|
|
const char *opts[]={"One", "Two", "Three", "January", "Feb", "2003"};
|
|
|
|
osg::Geometry *quad=new osg::Geometry;
|
|
|
|
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
|
|
|
|
stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
|
|
|
|
std::string name="subOption";
|
|
|
|
name += " ";
|
|
|
|
name += std::string(opts[i]);
|
|
|
|
geode->setName(name);
|
|
|
|
osg::Vec3Array* vertices = new osg::Vec3Array(4); // 1 quad
|
|
|
|
osg::Vec4Array* colors = new osg::Vec4Array;
|
|
|
|
colors = new osg::Vec4Array;
|
|
|
|
colors->push_back(osg::Vec4(0.8-0.1*i,0.1*i,0.2*i, 1.0));
|
|
|
|
quad->setColorArray(colors);
|
|
|
|
quad->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE);
|
|
|
|
(*vertices)[0]=position;
|
|
|
|
(*vertices)[1]=position+dx;
|
|
|
|
(*vertices)[2]=position+dx+dy;
|
|
|
|
(*vertices)[3]=position+dy;
|
|
|
|
quad->setVertexArray(vertices);
|
|
|
|
quad->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
|
|
|
|
geode->addDrawable(quad);
|
2005-11-02 23:23:54 +08:00
|
|
|
hudCamera->addChild(geode);
|
2003-04-13 21:26:41 +08:00
|
|
|
|
|
|
|
position += delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{ // this displays what has been selected
|
|
|
|
osg::Geode* geode = new osg::Geode();
|
|
|
|
osg::StateSet* stateset = geode->getOrCreateStateSet();
|
|
|
|
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
|
|
|
|
stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
|
2003-04-25 21:47:33 +08:00
|
|
|
geode->setName("The text label");
|
2003-04-13 21:26:41 +08:00
|
|
|
geode->addDrawable( updateText );
|
2005-11-02 23:23:54 +08:00
|
|
|
hudCamera->addChild(geode);
|
2003-04-13 21:26:41 +08:00
|
|
|
|
2003-04-25 21:47:33 +08:00
|
|
|
updateText->setCharacterSize(20.0f);
|
2003-04-13 21:26:41 +08:00
|
|
|
updateText->setFont(timesFont);
|
2003-04-25 21:47:33 +08:00
|
|
|
updateText->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
|
|
|
|
updateText->setText("");
|
2003-04-13 21:26:41 +08:00
|
|
|
updateText->setPosition(position);
|
2007-02-14 20:42:10 +08:00
|
|
|
updateText->setDataVariance(osg::Object::DYNAMIC);
|
2003-04-13 21:26:41 +08:00
|
|
|
|
|
|
|
position += delta;
|
|
|
|
}
|
|
|
|
|
2005-11-02 23:23:54 +08:00
|
|
|
return hudCamera;
|
2003-04-13 21:26:41 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
// use an ArgumentParser object to manage the program arguments.
|
|
|
|
osg::ArgumentParser arguments(&argc,argv);
|
|
|
|
|
|
|
|
// read the scene from the list of file specified commandline args.
|
|
|
|
osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
|
2007-06-08 23:37:20 +08:00
|
|
|
|
|
|
|
// if not loaded assume no arguments passed in, try use default mode instead.
|
|
|
|
if (!scene) scene = osgDB::readNodeFile("fountain.osg");
|
2003-04-13 21:26:41 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osg::Group> group = dynamic_cast<osg::Group*>(scene.get());
|
|
|
|
if (!group)
|
|
|
|
{
|
|
|
|
group = new osg::Group;
|
|
|
|
group->addChild(scene.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::ref_ptr<osgText::Text> updateText = new osgText::Text;
|
|
|
|
|
|
|
|
// add the HUD subgraph.
|
|
|
|
group->addChild(createHUD(updateText.get()));
|
|
|
|
|
2008-03-04 22:19:03 +08:00
|
|
|
if (arguments.read("--CompositeViewer"))
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osgViewer::View> view = new osgViewer::View;
|
|
|
|
// add the handler for doing the picking
|
|
|
|
view->addEventHandler(new PickHandler(updateText.get()));
|
|
|
|
|
|
|
|
// set the scene to render
|
|
|
|
view->setSceneData(group.get());
|
|
|
|
|
|
|
|
view->setUpViewAcrossAllScreens();
|
|
|
|
|
|
|
|
osgViewer::CompositeViewer viewer;
|
|
|
|
viewer.addView(view.get());
|
|
|
|
|
|
|
|
return viewer.run();
|
2003-04-13 21:26:41 +08:00
|
|
|
|
2008-03-04 22:19:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
osgViewer::Viewer viewer;
|
|
|
|
|
|
|
|
// add the handler for doing the picking
|
|
|
|
viewer.addEventHandler(new PickHandler(updateText.get()));
|
|
|
|
|
|
|
|
// set the scene to render
|
|
|
|
viewer.setSceneData(group.get());
|
|
|
|
|
|
|
|
return viewer.run();
|
|
|
|
}
|
2003-04-13 21:26:41 +08:00
|
|
|
|
|
|
|
}
|