Full code attached below:
Code:
/* -*-c++-*- OpenSceneGraph Cookbook
* Chapter 6 Recipe 4
* Author: Wang Rui
*/
#include <osg/Texture2D>
#include <osg/Group>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include "CommonFunctions"
#include <osg/io_utils>
#include <sstream>
osg::ref_ptr<osg::Image> image = new osg::Image;
// class to handle events with a pick
class PickHandler : public osgGA::GUIEventHandler {
public:
PickHandler() {}
~PickHandler() {}
bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa);
virtual void pick(osgViewer::View* view, const osgGA::GUIEventAdapter& ea);
};
bool PickHandler::handle(const osgGA::GUIEventAdapter&
ea,osgGA::GUIActionAdapter& aa)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::PUSH):
{
if(ea.getButton()==osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON )
{
osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
if (view) pick(view,ea);
}
return false;
}
default:
return false;
}
}
void PickHandler::pick(osgViewer::View* view, const osgGA::GUIEventAdapter& ea)
{
osgUtil::LineSegmentIntersector::Intersections intersections;
std::string gdlist="";
float x = ea.getX();
float y = ea.getY();
float z, z_buffer;
if (view->computeIntersections(x,y,intersections))
{
for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr =
intersections.begin();
hitr != intersections.end();
++hitr)
{
std::ostringstream os;
if (!hitr->nodePath.empty() &&
!(hitr->nodePath.back()->getName().empty()))
{
// the geodes are identified by name.
os<<"Object
\""<<hitr->nodePath.back()->getName()<<"\""<<std::endl;
}
else if (hitr->drawable.valid())
{
os<<"Object \""<<hitr->drawable->className()<<"\""<<std::endl;
}
os<<" local coords vertex("<<
hitr->getLocalIntersectPoint()<<")"<<"
normal("<<hitr->getLocalIntersectNormal()<<")"<<std::endl;
os<<" world coords vertex("<<
hitr->getWorldIntersectPoint()<<")"<<"
normal("<<hitr->getWorldIntersectNormal()<<")"<<std::endl;
const osgUtil::LineSegmentIntersector::Intersection::IndexList& vil
= hitr->indexList;
for(unsigned int i=0;i<vil.size();++i)
{
os<<" vertex indices ["<<i<<"] = "<<vil[i]<<std::endl;
}
for(int k=0; k<32; k++)
{
for(int l=0; l<32; l++)
{
z_buffer=((float*)image->data(k,l))[0];
z=-1/(z_buffer-1);
std::cout<<" "<<z<< " ";
}
std::cout<<std::endl;
}
std::cout<<std::endl;
//z=-1/(z-1);
// std::cout<<os.str()<<" Z: "<<z<< " m"<<std::endl;
gdlist += os.str();
}
}
// setLabel(gdlist);
}
int main( int argc, char** argv )
{
osg::ArgumentParser arguments( &argc, argv );
osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles( arguments );
if ( !scene ) scene = osgDB::readNodeFile("E:\\Point2\\Point2.ive");
osg::ref_ptr<osg::Texture2D> tex2D = new osg::Texture2D;
tex2D->setTextureSize( 32, 32 );
tex2D->setInternalFormat( GL_DEPTH_COMPONENT24 );
tex2D->setSourceFormat( GL_DEPTH_COMPONENT );
tex2D->setSourceType( GL_FLOAT );
osg::ref_ptr<osg::Camera> rttCamera =
osgCookBook::createRTTCamera(osg::Camera::DEPTH_BUFFER, tex2D.get());
rttCamera->addChild( scene.get() );
osg::ref_ptr<osg::Camera> hudCamera = osgCookBook::createHUDCamera(0.0,
1.0, 0.0, 1.0);
hudCamera->addChild( osgCookBook::createScreenQuad(0.5f, 1.0f) );
hudCamera->getOrCreateStateSet()->setTextureAttributeAndModes( 0,
tex2D.get() );
osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild( rttCamera.get() );
root->addChild( hudCamera.get() );
root->addChild( scene.get() );
osgViewer::Viewer viewer;
viewer.getCamera()->setComputeNearFarMode(
osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR );
viewer.setSceneData( root.get() );
viewer.getCamera()->attach(osg::Camera::DEPTH_BUFFER,image);
image->allocateImage(32,32,1,GL_DEPTH_COMPONENT,GL_FLOAT);
viewer.addEventHandler(new PickHandler());
return viewer.run();
}
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=50033#50033
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org