Commit 425f27a6 authored by schurade's avatar schurade
Browse files

[ADD] first work on picking objects in the 3D scene

parent 9ed07d13
......@@ -40,6 +40,9 @@
#include <osgDB/ReadFile>
#include "exceptions/WGEInitFailed.h"
#include "WPickHandler.h"
#include "WGEViewer.h"
WGEViewer::WGEViewer( osg::ref_ptr<WindowData> wdata, int x, int y, int width, int height, WGECamera::ProjectionMode projectionMode ):
......@@ -82,6 +85,9 @@ WGEViewer::WGEViewer( osg::ref_ptr<WindowData> wdata, int x, int y, int width, i
m_View->setLightingMode( osg::View::HEADLIGHT ); // this is the default anyway
osg::ref_ptr<osgText::Text> updateText = new osgText::Text;
m_View->addEventHandler( new WPickHandler( updateText.get() ) );
// finally add view
// there is the possibility to use ONE single composite viewer instance for every view, but
// currently this possibility is not used.
......
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <iostream>
#include <string>
#include "WPickHandler.h"
WPickHandler::WPickHandler( osgText::Text* updateText ) :
m_updateText( updateText )
{
}
WPickHandler::~WPickHandler()
{
}
void WPickHandler::setLabel( const std::string& name )
{
//std::cout << name << std::endl;
if ( m_updateText.get() )
{
m_updateText->setText( name );
}
}
bool WPickHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
{
switch ( ea.getEventType() )
{
case( osgGA::GUIEventAdapter::PUSH ):
{
osgViewer::View* view = static_cast< osgViewer::View* >( &aa );
if ( view )
pick( view, ea );
return false;
}
case( osgGA::GUIEventAdapter::KEYDOWN ):
{
if ( ea.getKey() == 'c' )
{
osgViewer::View* view = static_cast< osgViewer::View* >( &aa );
osg::ref_ptr< osgGA::GUIEventAdapter > event = new osgGA::GUIEventAdapter( ea );
event->setX( ( ea.getXmin() + ea.getXmax() ) * 0.5 );
event->setY( ( ea.getYmin() + ea.getYmax() ) * 0.5 );
if ( view )
{
pick( view, *event );
}
}
return false;
}
default:
return false;
}
}
void WPickHandler::pick( osgViewer::View* view, const osgGA::GUIEventAdapter& ea )
{
osgUtil::LineSegmentIntersector::Intersections intersections;
std::string gdlist = "";
float x = ea.getX();
float y = ea.getY();
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;
}
gdlist += os.str();
}
}
setLabel( gdlist );
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WPICKHANDLER_H
#define WPICKHANDLER_H
#include <sstream>
#include <string>
#include <osgUtil/Optimizer>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgViewer/CompositeViewer>
#include <osg/Material>
#include <osg/Geode>
#include <osg/BlendFunc>
#include <osg/Depth>
#include <osg/Projection>
#include <osg/MatrixTransform>
#include <osg/Camera>
#include <osg/io_utils>
#include <osg/ShapeDrawable>
#include <osgText/Text>
/**
* class to handle events with a pick
*/
class WPickHandler: public osgGA::GUIEventHandler
{
public:
explicit WPickHandler( osgText::Text* updateText );
virtual ~WPickHandler();
bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa );
virtual void pick( osgViewer::View* view, const osgGA::GUIEventAdapter& ea );
void setLabel( const std::string& name );
protected:
osg::ref_ptr< osgText::Text > m_updateText;
};
#endif // WPICKHANDLER_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment