Hello there!
I've struggling a while with the setup of a HUD camera.
What I want to achieve is to show cross in the middle of the vieport.
I have been able to display text, but not the cross, so I'm requesting a little
help with this ... :)
Here's the code for the viewer (it is based in the osgQt and osghud examples):
Code:
class Viewer: public QWidget, public osgViewer::Viewer {
public:
static const unsigned int MAIN_MASK = 0x1;
static const unsigned int HUD_MASK = 0x2;
virtual void paintEvent( QPaintEvent* event )
{
frame();
}
Viewer( osg::ref_ptr<osg::Node> g): QWidget(), osgViewer::Viewer() {
osg::ref_ptr<osg::Camera> cam = createCamera( 150, 10, 640, 800 );
QGridLayout* layout = new QGridLayout;
layout->setContentsMargins( 1, 1, 1, 1 );
QWidget* widget = addViewWidget( cam.get(), g.get() );
layout->addWidget( widget );
setLayout( layout );
connect( &( this->timer_ ), SIGNAL( timeout() ), this, SLOT( update() )
);
getDatabasePager()->setDoPreCompile( true );
}
void startTimer() { timer_.start(24); }
~Viewer(){}
private:
osg::Camera* createCamera( int x, int y, int w, int h )
{
try {
osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
if( !ds ) {
return NULL;
}
osg::ref_ptr<osg::GraphicsContext::Traits> traits =
new osg::GraphicsContext::Traits
;
traits->windowName = "no name";
traits->windowDecoration = true;
traits->x = x;
traits->y = y;
traits->width = w;
traits->height = h;
traits->doubleBuffer = true;
traits->alpha = ds->getMinimumNumAlphaBits();
traits->stencil = ds->getMinimumNumStencilBits();
traits->sampleBuffers = ds->getMultiSamples();
traits->samples = 4;
osgViewer::Viewer::Views views;
getViews( views );
osg::ref_ptr<osg::Camera> camera = views[ 0 ]->getCamera();
camera->setGraphicsContext(
new osgQt::GraphicsWindowQt( traits.get() ) )
;
camera->setClearColor( osg::Vec4( 0,0,0,1 ) );
camera->setViewport(
new osg::Viewport( 0, 0, traits->width, traits->height)
)
;
const double tw = static_cast<double>( traits->width );
const double th = static_cast<double>( traits->height );
camera->setProjectionMatrixAsPerspective( 30.0f, tw / th, 1.0f,
10000.0f )
;
camera->setCullMask( MAIN_MASK );
return camera.release();
} catch( const std::bad_alloc& ) {
return NULL;
}
}
QWidget* addViewWidget( osg::Camera *camera, osg::Node *scene )
{
try {
addEventHandler( new osgViewer::StatsHandler );
osgGA::CameraManipulator* cameraManipulator =
new osgGA::TrackballManipulator(
osgGA::StandardManipulator::DEFAULT_SETTINGS
)
;
setCameraManipulator( cameraManipulator );
osg::observer_ptr<osgGA::TrackballManipulator> cm =
dynamic_cast<osgGA::TrackballManipulator*>(
cameraManipulator )
;
cm->setAnimationTime( -1 );
cm->setMinimumDistance( 10 );
hud = new osg::Camera;
hud->setCullMask( HUD_MASK );
hud->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
hud->setClearMask(GL_DEPTH_BUFFER_BIT /*|
GL_COLOR_BUFFER_BIT*/ );
hud->setRenderOrder(osg::Camera::POST_RENDER);
hud->setAllowEventFocus(false);
osgViewer::Viewer::Windows windows;
getWindows( windows );
hud->setClearColor( osg::Vec4( 0.0, 1.0, 0.0, 255.0 ) );
hud->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
hud->setGraphicsContext( windows.front() );
osg::Viewport* vport = camera->getViewport();
const osg::Matrix wmat = vport->computeWindowMatrix();
osg::Vec3d vc( vport->x() + vport->width()/2,
vport->y() + vport->height()/2, 1.0 );
hud->setViewport( vc.x() - 50, vc.y() - 50, 100,100 );
hud->setProjectionMatrix( osg::Matrix::ortho2D( vc.x()
- 50, vc.y() - 50, vc.x() + 50, vc.y() + 50 ) );
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
osg::ref_ptr<osg::Vec4Array> color = new osg::Vec4Array;
color->push_back( osg::Vec4( 1.0, 0.0, 0.0, 1.0 ) );
#if 0
osg::ref_ptr<osgText::Text> text = new osgText::Text;
geode->addDrawable( text.get() );
text->setPosition( osg::Vec3( vc.x(), vc.y(), 0.0 ) );
text->setAlignment( osgText::Text::CENTER_CENTER );
text->setText( "BAH" );
text->setColor( color->front() );
text->getOrCreateStateSet()->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
text->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::OFF);
text->getOrCreateStateSet()->setMode(GL_LIGHTING,
osg::StateAttribute::OFF);
#else
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
geode->addDrawable( geom.get() );
osg::ref_ptr<osg::Vec2Array> vert = new osg::Vec2Array;
const float buf = 0.5f;
vert->push_back( osg::Vec2d( vc.x() - buf, vc.y() + buf
) );
vert->push_back( osg::Vec2d( vc.x() + buf, vc.y() - buf
) );
vert->push_back( osg::Vec2d( vc.x() - buf, vc.y() - buf
) );
vert->push_back( osg::Vec2d( vc.x() + buf, vc.y() + buf
) );
geom->setVertexArray( vert.get() );
geom->setColorArray( color.get() );
geom->setColorBinding( osg::Geometry::BIND_OVERALL );
geom->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::OFF);
geom->getOrCreateStateSet()->setMode( GL_DEPTH_TEST,
osg::StateAttribute::OFF )
;
geom->getOrCreateStateSet()->setMode( GL_LIGHTING,
osg::StateAttribute::OFF )
;
geom->getOrCreateStateSet()->setRenderBinDetails( 25,
"RenderBin" );
geom->addPrimitiveSet(
new osg::DrawArrays( GL_LINES,
0,
vert->size() ) )
;
geom->getOrCreateStateSet()->setAttributeAndModes(
new osg::LineWidth( 3.0f ) )
;
geom->getOrCreateStateSet()->setAttributeAndModes(
new osg::Point( 5.0f ) )
;
geom->addPrimitiveSet(
new osg::DrawArrays( GL_POINTS,
0,
vert->size() ) )
;
#endif
geode->setNodeMask( HUD_MASK );
hud->addChild( geode.get() );
osg::Group* g = dynamic_cast<osg::Group*>(scene);
std::cout << "G-" << g->getNumChildren() << "\n";
g->addChild( hud.get() );
osg::Vec3d eye , cent = scene->getBound().center();
eye[2] += 100;
cm->setHomePosition( eye, cent, osg::Y_AXIS );
setCamera( camera );
setSceneData( g );
setRunFrameScheme( osgViewer::Viewer::ON_DEMAND );
osgQt::GraphicsWindowQt* gw =
dynamic_cast<osgQt::GraphicsWindowQt*>(
camera->getGraphicsContext() )
;
return gw ? gw->getGLWidget() : 0;
} catch( const std::bad_alloc& ) {
return NULL;
}
}
private:
QTimer timer_;
osg::ref_ptr<osg::Camera> hud;
osg::ref_ptr<osg::Geode> hudgeode;
};
The code inside the #if 0 part in the addViewWidget function shows the text,
but the code for the cross shows nothing.
Do you have any idea?
...
Cheers,
Andrés
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=60156#60156
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org