-
Notifications
You must be signed in to change notification settings - Fork 61
/
PickHandler.cpp
56 lines (39 loc) · 1.35 KB
/
PickHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "PickHandler.h"
#include <osg/io_utils>
#include <osgUtil/IntersectionVisitor>
#include <osgUtil/LineSegmentIntersector>
#include <osgViewer/Viewer>
#include <iostream>
PickHandler::PickHandler( double devicePixelRatio )
: devicePixelRatio_( devicePixelRatio )
{
}
PickHandler::~PickHandler()
{
}
bool PickHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
{
if( ea.getEventType() != osgGA::GUIEventAdapter::RELEASE &&
ea.getButton() != osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON )
{
return false;
}
osgViewer::View* viewer = dynamic_cast<osgViewer::View*>( &aa );
if( viewer )
{
osgUtil::LineSegmentIntersector* intersector
= new osgUtil::LineSegmentIntersector( osgUtil::Intersector::WINDOW, ea.getX() * devicePixelRatio_, ea.getY() * devicePixelRatio_ );
osgUtil::IntersectionVisitor iv( intersector );
osg::Camera* camera = viewer->getCamera();
if( !camera )
return false;
camera->accept( iv );
if( !intersector->containsIntersections() )
return false;
auto intersections = intersector->getIntersections();
std::cout << "Got " << intersections.size() << " intersections:\n";
for( auto&& intersection : intersections )
std::cout << " - Local intersection point = " << intersection.localIntersectionPoint << "\n";
}
return true;
}