Commit b38cebdb authored by Alexander Wiebel's avatar Alexander Wiebel

[ADD] readded HUD module. Will add documentation and clean code soon

parent 470c0819
//---------------------------------------------------------------------------
//
// 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 <string>
#include <osg/MatrixTransform>
#include <osg/Projection>
#include <osgDB/WriteFile>
#include "core/common/WAssert.h"
#include "core/common/WPathHelper.h"
#include "core/kernel/WKernel.h"
#include "core/graphicsEngine/WGEViewer.h"
#include "WMHud.h"
#include "WMHud.xpm"
// This line is needed by the module loader to actually find your module.
W_LOADABLE_MODULE( WMHud )
WMHud::WMHud()
: m_updatedPickText( true )
{
}
WMHud::~WMHud()
{
}
boost::shared_ptr< WModule > WMHud::factory() const
{
return boost::shared_ptr< WModule >( new WMHud() );
}
const char** WMHud::getXPMIcon() const
{
return hud_xpm;
}
const std::string WMHud::getName() const
{
return "HUD";
}
const std::string WMHud::getDescription() const
{
return "This module provides a HUD (Head Up Display) for status displays";
}
void WMHud::connectors()
{
}
void WMHud::properties()
{
WModule::properties();
}
void WMHud::moduleMain()
{
// signal ready state
ready();
init();
// Since the modules run in a separate thread: wait
waitForStop();
// clean up stuff
// NOTE: ALWAYS remove your osg nodes!
WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->remove( m_rootNode );
}
void WMHud::init()
{
m_rootNode = osg::ref_ptr< osg::Projection >( new osg::Projection );
m_rootNode->setName( "HUDNode" );
// Initialize the projection matrix for viewing everything we
// will add as descendants of this node. Use screen coordinates
// to define the horizontal and vertical extent of the projection
// matrix. Positions described under this node will equate to
// pixel coordinates.
m_rootNode->setMatrix( osg::Matrix::ortho2D( 0, 1024, 0, 768 ) );
// For the HUD model view matrix use an identity matrix
osg::ref_ptr< osg::MatrixTransform > HUDModelViewMatrix = new osg::MatrixTransform;
HUDModelViewMatrix->setMatrix( osg::Matrix::identity() );
// Make sure the model view matrix is not affected by any transforms
// above it in the scene graph
HUDModelViewMatrix->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
// Add the HUD projection matrix as a child of the root node
// and the HUD model view matrix as a child of the projection matrix
// Anything under this node will be viewed using this projection matrix
// and positioned with this model view matrix.
//root->addChild(HUDProjectionMatrix);
m_rootNode->addChild( HUDModelViewMatrix );
// Add the Geometry node to contain HUD geometry as a child of the
// HUD model view matrix.
m_HUDs = osg::ref_ptr< WGEGroupNode >( new WGEGroupNode() );
// A geometry node for our HUD
osg::ref_ptr<osg::Geode> HUDGeode = osg::ref_ptr<osg::Geode>( new osg::Geode() );
HUDModelViewMatrix->addChild( m_HUDs );
m_HUDs->insert( HUDGeode );
// Set up geometry for the HUD and add it to the HUD
osg::ref_ptr< osg::Geometry > HUDBackgroundGeometry = new osg::Geometry();
osg::ref_ptr< osg::Vec3Array > HUDBackgroundVertices = new osg::Vec3Array;
HUDBackgroundVertices->push_back( osg::Vec3( 580, 0, -1 ) );
HUDBackgroundVertices->push_back( osg::Vec3( 1024, 0, -1 ) );
HUDBackgroundVertices->push_back( osg::Vec3( 1024, 100, -1 ) );
HUDBackgroundVertices->push_back( osg::Vec3( 580, 100, -1 ) );
osg::ref_ptr< osg::DrawElementsUInt > HUDBackgroundIndices = new osg::DrawElementsUInt( osg::PrimitiveSet::POLYGON, 0 );
HUDBackgroundIndices->push_back( 0 );
HUDBackgroundIndices->push_back( 1 );
HUDBackgroundIndices->push_back( 2 );
HUDBackgroundIndices->push_back( 3 );
osg::ref_ptr< osg::Vec4Array > HUDcolors = new osg::Vec4Array;
HUDcolors->push_back( osg::Vec4( 0.8f, 0.8f, 0.8f, 0.8f ) );
osg::ref_ptr< osg::Vec3Array > HUDnormals = new osg::Vec3Array;
HUDnormals->push_back( osg::Vec3( 0.0f, 0.0f, 1.0f ) );
HUDBackgroundGeometry->setNormalArray( HUDnormals );
HUDBackgroundGeometry->setNormalBinding( osg::Geometry::BIND_OVERALL );
HUDBackgroundGeometry->addPrimitiveSet( HUDBackgroundIndices );
HUDBackgroundGeometry->setVertexArray( HUDBackgroundVertices );
HUDBackgroundGeometry->setColorArray( HUDcolors );
HUDBackgroundGeometry->setColorBinding( osg::Geometry::BIND_OVERALL );
HUDGeode->addDrawable( HUDBackgroundGeometry );
// Create and set up a state set using the texture from above
osg::ref_ptr< osg::StateSet > HUDStateSet = new osg::StateSet();
HUDGeode->setStateSet( HUDStateSet );
// For this state set, turn blending on (so alpha texture looks right)
HUDStateSet->setMode( GL_BLEND, osg::StateAttribute::ON );
// Disable depth testing so geometry is draw regardless of depth values
// of geometry already draw.
HUDStateSet->setMode( GL_DEPTH_TEST, osg::StateAttribute::OFF );
HUDStateSet->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
// Need to make sure this geometry is draw last. RenderBins are handled
// in numerical order so set bin number to 11
HUDStateSet->setRenderBinDetails( 11, "RenderBin" );
m_osgPickText = osg::ref_ptr< osgText::Text >( new osgText::Text() );
//HUDGeode->addDrawable( m_osgPickText );
// this obviously caused a crash
// reason:
// the font is added to the scene, altough we got no costum updatecallback
// osg already starts drawing the node
// when setting the font, a variable is cleared which osg is already using -> crash
m_osgPickText->setCharacterSize( 14 );
m_osgPickText->setFont( WPathHelper::getAllFonts().Default.file_string() );
m_osgPickText->setText( "nothing picked" );
m_osgPickText->setAxisAlignment( osgText::Text::SCREEN );
m_osgPickText->setPosition( osg::Vec3( 600, 80, -1.5 ) );
m_osgPickText->setColor( osg::Vec4( 0, 0, 0, 1 ) );
m_rootNode->addUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WMHud::updateCallback, this ) ) );
HUDGeode->addDrawable( m_osgPickText );
WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->insert( m_rootNode );
if( m_active->get() )
{
m_rootNode->setNodeMask( 0xFFFFFFFF );
}
else
{
m_rootNode->setNodeMask( 0x0 );
}
// connect updateGFX with picking
boost::shared_ptr< WGEViewer > viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "main" );
WAssert( viewer, "Requested viewer (main) not found." );
if(viewer->getPickHandler() )
viewer->getPickHandler()->getPickSignal()->connect( boost::bind( &WMHud::updatePickText, this, _1 ) );
}
void WMHud::updatePickText( WPickInfo pickInfo )
{
boost::unique_lock< boost::shared_mutex > lock;
lock = boost::unique_lock< boost::shared_mutex >( m_updateLock );
std::ostringstream os;
os << std::setprecision( 5 )
<< "Name: " << pickInfo.getName() << "\n"
<< "Position: [" << pickInfo.getPickPosition()[0] << ", " << pickInfo.getPickPosition()[1] << ", " << pickInfo.getPickPosition()[2] << "]\n"
<< "Pixel coordinates: " << pickInfo.getPickPixel().x() << " " << pickInfo.getPickPixel().y() << "\n"
<< "Object Normal: [" << pickInfo.getPickNormal()[0] << ", " << pickInfo.getPickNormal()[1] << ", " << pickInfo.getPickNormal()[2] << "]\n";
m_pickText = os.str();
m_updatedPickText = true;
lock.unlock();
}
void WMHud::updateCallback()
{
if( m_updatedPickText )
{
m_osgPickText->setText( m_pickText.c_str() );
boost::unique_lock< boost::shared_mutex > lock;
lock = boost::unique_lock< boost::shared_mutex >( m_updateLock );
m_updatedPickText = false;
lock.unlock();
}
}
void WMHud::activate()
{
if( m_active->get() )
{
m_rootNode->setNodeMask( 0xFFFFFFFF );
}
else
{
m_rootNode->setNodeMask( 0x0 );
}
WModule::activate();
}
//---------------------------------------------------------------------------
//
// 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 WMHUD_H
#define WMHUD_H
#include <string>
#include <osg/Group>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Node>
#include <osgText/Text>
#include "core/kernel/WModule.h"
#include "core/graphicsEngine/WGEGroupNode.h"
#include "core/graphicsEngine/WPickInfo.h"
/**
* This module implements several onscreen status displays
* \ingroup modules
*/
class WMHud : public WModule
{
public:
/**
* standard constructor
*/
WMHud();
/**
* destructor
*/
virtual ~WMHud();
/**
* \par Description
* Gives back the name of this module.
* \return the module's name.
*/
virtual const std::string getName() const;
/**
* \par Description
* Gives back a description of this module.
* \return description to module.
*/
virtual const std::string getDescription() const;
/**
* Sets pick text member vairable
*
* \param pickInfo information about the pick
*/
void updatePickText( WPickInfo pickInfo );
protected:
/**
* \par Description
* Entry point after loading the module. Runs in separate thread.
*/
virtual void moduleMain();
/**
* Initialize the connectors this module is using.
*/
virtual void connectors();
/**
* Initialize the properties for this module.
*/
virtual void properties();
/**
* Due to the prototype design pattern used to build modules, this method returns a new instance of this method. NOTE: it
* should never be initialized or modified in some other way. A simple new instance is required.
*
* \return the prototype used to create every module in OpenWalnut.
*/
virtual boost::shared_ptr< WModule > factory() const;
/**
* Get the icon for this module in XPM format.
* \return The icon.
*/
virtual const char** getXPMIcon() const;
private:
boost::shared_mutex m_updateLock; //!< Lock to prevent concurrent threads trying to update the osg node
/**
* Projection node for defining view frustrum for HUD
*/
osg::ref_ptr<osg::Projection> m_rootNode;
/**
* Geometry group for all hud related things
*/
osg::ref_ptr< WGEGroupNode > m_HUDs;
/**
* Text instance that will show up in the HUD
*/
osg::ref_ptr< osgText::Text > m_osgPickText;
bool m_updatedPickText; //!< Tells us whether the picktext was updated an has to be rendered.
/**
* string to store the pick result from the picking method
*/
std::string m_pickText;
/**
* init method
*/
void init();
/**
* update method
*/
void update();
/**
* Gets signaled from the properties object when something was changed. Now, only m_active is used. This method therefore simply
* activates/deactivates the HUD.
*/
void activate();
/**
* The update callback that is called for the osg node of this module.
*/
void updateCallback();
};
#endif // WMHUD_H
/* XPM */
static const char * hud_xpm[] = {
"32 32 21 1",
" c None",
". c #C7C7C7",
"+ c #7C7C7C",
"@ c #7D7D7D",
"# c #AFAFAF",
"$ c #7E7E7E",
"% c #C5C5C5",
"& c #C4C4C4",
"* c #898989",
"= c #B8B8B8",
"- c #B7B7B7",
"; c #A8A8A8",
"> c #8C8C8C",
", c #BABABA",
"' c #BBBBBB",
") c #8E8E8E",
"! c #A9A9A9",
"~ c #A5A5A5",
"{ c #848484",
"] c #858585",
"^ c #A6A6A6",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"................................",
"...+.....+..+.....+..+++++@#....",
"...+.....+..+.....+..+....#$#...",
"...+.....+..+.....+..+.....#@...",
"...+.....+..+.....+..+......+...",
"...+++++++..+.....+..+......+...",
"...+.....+..+.....+..+......+...",
"...+.....+..$%...&$..+......+...",
"...+.....+..*=...-*..+.....#@...",
"...+.....+..;>,%')!..+....#$#...",
"...+.....+...~{+]^...+++++@#....",
"................................",
"................................"};
......@@ -23,6 +23,7 @@ ADD_MODULE( fiberSelection )
ADD_MODULE( fiberTransform )
ADD_MODULE( functionalMRIViewer )
ADD_MODULE( gaussProcesses )
ADD_MODULE( hud )
ADD_MODULE( imageExtractor )
ADD_MODULE( lineGuidedSlice )
ADD_MODULE( paintTexture )
......
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