Commit 57ffaebd authored by Alexander Wiebel's avatar Alexander Wiebel

[ADD] first implementation that allows for interactive manipulation of

boxes.Therefore changed WPickhandler so thta it also sends kind of a
"unpick" signal (more precisely this is the pick with the strig "unpick"
ATM).
parent fea84e56
......@@ -45,12 +45,26 @@ bool WPickHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAda
{
switch ( ea.getEventType() )
{
case osgGA::GUIEventAdapter::DRAG : // Mouse pushed an dragged
case osgGA::GUIEventAdapter::PUSH : // Mousebutton pushed
{
unsigned int buttonMask = ea.getButtonMask();
if( buttonMask == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON )
{
osgViewer::View* view = static_cast< osgViewer::View* >( &aa );
if ( view )
{
pick( view, ea );
}
}
return false;
}
case osgGA::GUIEventAdapter::RELEASE : // Mousebutton released
{
osgViewer::View* view = static_cast< osgViewer::View* >( &aa );
if ( view )
{
pick( view, ea );
unpick();
}
return false;
}
......@@ -74,6 +88,15 @@ bool WPickHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAda
}
}
void WPickHandler::unpick( )
{
if( m_hitResult != "" )
{
m_hitResult = "unpick";
}
m_pickSignal( getHitResult() );
}
void WPickHandler::pick( osgViewer::View* view, const osgGA::GUIEventAdapter& ea )
{
osgUtil::LineSegmentIntersector::Intersections intersections;
......
......@@ -61,6 +61,7 @@ public:
bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa );
virtual void pick( osgViewer::View* view, const osgGA::GUIEventAdapter& ea );
virtual void unpick();
/**
* Gives information about the picked object.
......
......@@ -32,6 +32,8 @@
#include "WMPrototypeBoxManipulation.h"
#include <boost/lexical_cast.hpp>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/StateSet>
......@@ -41,7 +43,9 @@
#include <osg/LineWidth>
#include <osgDB/WriteFile>
#include "../../utils/WStringUtils.h"
#include "../../math/WVector3D.h"
#include "../../math/WPosition.h"
#include "../../kernel/WKernel.h"
#include "../../graphicsEngine/WShader.h"
......@@ -49,7 +53,8 @@
WMPrototypeBoxManipulation::WMPrototypeBoxManipulation():
WModule()
WModule(),
m_isPicked( false )
{
// WARNING: initializing connectors inside the constructor will lead to an exception.
// Implement WModule::initializeConnectors instead.
......@@ -93,7 +98,7 @@ void WMPrototypeBoxManipulation::moduleMain()
// loop until the module container requests the module to quit
while ( !m_shutdownFlag() )
{
draw();
draw( wmath::WPosition( 30, 30, 30 ), wmath::WPosition( 70, 70, 70 ) );
// this waits for m_moduleState to fire. By default, this is only the m_shutdownFlag condition.
// NOTE: you can add your own conditions to m_moduleState using m_moduleState.add( ... )
......@@ -206,27 +211,28 @@ void buildLinesFromPoints( osg::DrawElementsUInt* surfaceElements )
surfaceElements->push_back( 5 );
}
void setVertices( osg::Vec3Array* vertices )
void setVertices( osg::Vec3Array* vertices, wmath::WPosition minPos, wmath::WPosition maxPos )
{
const double offset = 30.;
vertices->push_back( osg::Vec3( 30, 30, 30 ) );
vertices->push_back( osg::Vec3( 30, 30, 30 + offset ) );
vertices->push_back( osg::Vec3( 30, 30 + offset, 30 ) );
vertices->push_back( osg::Vec3( 30, 30 + offset, 30 + offset ) );
vertices->push_back( osg::Vec3( 30 + offset, 30, 30 ) );
vertices->push_back( osg::Vec3( 30 + offset, 30, 30 + offset ) );
vertices->push_back( osg::Vec3( 30 + offset, 30 + offset, 30 ) );
vertices->push_back( osg::Vec3( 30 + offset, 30 + offset, 30 + offset ) );
vertices->push_back( osg::Vec3( minPos[0], minPos[1], minPos[2] ) );
vertices->push_back( osg::Vec3( minPos[0], minPos[1], maxPos[2] ) );
vertices->push_back( osg::Vec3( minPos[0], maxPos[1], minPos[2] ) );
vertices->push_back( osg::Vec3( minPos[0], maxPos[1], maxPos[2] ) );
vertices->push_back( osg::Vec3( maxPos[0], minPos[1], minPos[2] ) );
vertices->push_back( osg::Vec3( maxPos[0], minPos[1], maxPos[2] ) );
vertices->push_back( osg::Vec3( maxPos[0], maxPos[1], minPos[2] ) );
vertices->push_back( osg::Vec3( maxPos[0], maxPos[1], maxPos[2] ) );
}
void WMPrototypeBoxManipulation::draw()
void WMPrototypeBoxManipulation::draw( wmath::WPosition minPos, wmath::WPosition maxPos )
{
m_minPos = minPos;
m_maxPos = maxPos;
osg::Geometry* surfaceGeometry = new osg::Geometry();
m_geode = new osg::Geode;
m_geode->setName( "Box" );
osg::Vec3Array* vertices = new osg::Vec3Array;
setVertices( vertices );
setVertices( vertices, minPos, maxPos );
surfaceGeometry->setVertexArray( vertices );
osg::DrawElementsUInt* surfaceElements;
......@@ -251,7 +257,7 @@ void WMPrototypeBoxManipulation::draw()
// colors
osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back( osg::Vec4( .9f, .9f, 0.9f, 0.5f ) );
colors->push_back( osg::Vec4( 1.f, 1.f, 1.f, 0.5f ) );
surfaceGeometry->setColorArray( colors );
surfaceGeometry->setColorBinding( osg::Geometry::BIND_OVERALL );
......@@ -264,6 +270,15 @@ void WMPrototypeBoxManipulation::draw()
debugLog() << "Intial draw " << std::endl;
}
wmath::WPosition getPosFromText( std::string posText )
{
std::vector< std::string > coordText = string_utils::tokenize( posText );
double x = boost::lexical_cast< double >( coordText[0] );
double y = boost::lexical_cast< double >( coordText[1] );
double z = boost::lexical_cast< double >( coordText[2] );
return wmath::WPosition( x, y, z );
}
void WMPrototypeBoxManipulation::updateGFX( std::string text )
{
boost::shared_lock<boost::shared_mutex> slock;
......@@ -272,7 +287,32 @@ void WMPrototypeBoxManipulation::updateGFX( std::string text )
if( text.find( "Object ") != std::string::npos
&& text.find( "\"Box\"" ) != std::string::npos )
{
std::cout << "Picked Box with Info: " << text << std::endl;
std::string posText = string_utils::tokenize( text, "()" )[1];
wmath::WPosition newPos = getPosFromText( posText );
if( m_isPicked )
{
wmath::WVector3D moveVec = m_pickedPosition - newPos;
osg::Vec3Array* vertices = new osg::Vec3Array;
m_minPos -= moveVec;
m_maxPos -= moveVec;
setVertices( vertices, m_minPos, m_maxPos );
((osg::Geometry*)(m_geode->getDrawable( 0 )))->setVertexArray( vertices );
}
else
{
osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back( osg::Vec4( 1.f, .0f, .0f, 0.5f ) );
((osg::Geometry*)(m_geode->getDrawable( 0 )))->setColorArray( colors );
}
m_pickedPosition = newPos;
m_isPicked = true;
}
if( m_isPicked && text.find( "unpick" ) != std::string::npos )
{
osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back( osg::Vec4( 1.f, 1.f, 1.f, 0.5f ) );
((osg::Geometry*)(m_geode->getDrawable( 0 )))->setColorArray( colors );
m_isPicked = false;
}
slock.unlock();
}
......
......@@ -36,6 +36,7 @@
#include "../../kernel/WModule.h"
#include "../../kernel/WModuleInputData.h"
#include "../../math/WVector3D.h"
/**
* Prototype module
......@@ -105,14 +106,21 @@ private:
/**
* draw initial graphics
* \param minPos the lower left front corener of the box
* \param maxPos the upper right back corner of the box
*/
void draw();
void draw( wmath::WPosition minPos, wmath::WPosition maxPos );
boost::shared_mutex m_updateLock; //!< Lock to prevent concurrent threads trying to update the osg node
osg::Geode* m_geode; //!< Pointer to geode. We need it to be able to update it when callback is invoked.
boost::shared_ptr< WModuleInputData< WDataSetSingle > > m_input; //!< Input connector required by this module.
bool m_isPicked; //!< Indicates whether the box is currently picked or not.
wmath::WPosition m_pickedPosition; //!< Caches the old picked position to a allow for cmoparison
wmath::WPosition m_minPos; //!< The minimum position of the box
wmath::WPosition m_maxPos; //!< The maximum position of the box
};
/**
......
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