Wu Xiaodong
2008-05-06 01:26:48 UTC
All. I try to render a hud-text;
I created a hud text in a geode and added it to a slave-camera. It's
good. but when i resize the view and set a new position to the text bases
on the new window rect. The text's position seems based on the old window
rect when when the slave-camera is created.
when created, window rect is (0,0, 100,100) and the text position is :
50,50, it's good;
when resize the window to rect (0,0,200,200) and i try to set the text
position (100,100); I hope the text should be in the center of window, but
it's out of the window range and I can see it in the view.
how can I fix my problem?
Thanks.
xiaodong
the codes is following.
if( _hudCamera.valid() ) _hudCamera.release();
_hudCamera = new osg::Camera;
_hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
osg::ref_ptr<osg::Camera> camera = this->getCamera();
//update the main camera;
osg::ref_ptr<osg::GraphicsContext::Traits> traits;
if( camera.valid() )
{
osg::ref_ptr<osg::GraphicsContext> context =
camera->getGraphicsContext();
if( context.valid() )
{
traits = const_cast<osg::GraphicsContext::Traits*>(
context->getTraits() );
if( traits.valid() )
{
_hudCamera->setGraphicsContext( context.get() );
_hudCamera->setViewport(0,0,traits->width, traits->height );
}
}
}
if( traits.valid() ) {
_hudCamera->setProjectionMatrixAsOrtho2D(0,traits->width,0,traits->height);
_hudCamera->setViewMatrix(osg::Matrix::identity());
_hudCamera->setRenderOrder(osg::Camera::POST_RENDER);
_hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
std::string timesFont("fonts/times.ttf");
turn lighting off for the text and disable depth test to ensure its
always ontop.
osg::Vec3 position( 10.0f,traits->height/2.0,0.0f);
osg::Geode* geode = new osg::Geode();
osg::StateSet* stateset = geode->getOrCreateStateSet();
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
//--
stateset->setRenderBinDetails(11, "RenderBin");
_hudCamera->addChild( geode );
osgText::Text* text = new osgText::Text;
geode->addDrawable( text );
text->setFont(timesFont);
std::stringstream ss;
ss<<"width:"<<traits->width<<"Height:"<<traits->height<<std::endl;
std::string str;
ss>>str;
std::wstring wstr(str.begin(), str.end());
text->setText( wstr.data() );
text->setPosition(position);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I created a hud text in a geode and added it to a slave-camera. It's
good. but when i resize the view and set a new position to the text bases
on the new window rect. The text's position seems based on the old window
rect when when the slave-camera is created.
when created, window rect is (0,0, 100,100) and the text position is :
50,50, it's good;
when resize the window to rect (0,0,200,200) and i try to set the text
position (100,100); I hope the text should be in the center of window, but
it's out of the window range and I can see it in the view.
how can I fix my problem?
Thanks.
xiaodong
the codes is following.
if( _hudCamera.valid() ) _hudCamera.release();
_hudCamera = new osg::Camera;
_hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
osg::ref_ptr<osg::Camera> camera = this->getCamera();
//update the main camera;
osg::ref_ptr<osg::GraphicsContext::Traits> traits;
if( camera.valid() )
{
osg::ref_ptr<osg::GraphicsContext> context =
camera->getGraphicsContext();
if( context.valid() )
{
traits = const_cast<osg::GraphicsContext::Traits*>(
context->getTraits() );
if( traits.valid() )
{
_hudCamera->setGraphicsContext( context.get() );
_hudCamera->setViewport(0,0,traits->width, traits->height );
}
}
}
if( traits.valid() ) {
_hudCamera->setProjectionMatrixAsOrtho2D(0,traits->width,0,traits->height);
_hudCamera->setViewMatrix(osg::Matrix::identity());
_hudCamera->setRenderOrder(osg::Camera::POST_RENDER);
_hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
std::string timesFont("fonts/times.ttf");
turn lighting off for the text and disable depth test to ensure its
always ontop.
osg::Vec3 position( 10.0f,traits->height/2.0,0.0f);
osg::Geode* geode = new osg::Geode();
osg::StateSet* stateset = geode->getOrCreateStateSet();
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
//--
stateset->setRenderBinDetails(11, "RenderBin");
_hudCamera->addChild( geode );
osgText::Text* text = new osgText::Text;
geode->addDrawable( text );
text->setFont(timesFont);
std::stringstream ss;
ss<<"width:"<<traits->width<<"Height:"<<traits->height<<std::endl;
std::string str;
ss>>str;
std::wstring wstr(str.begin(), str.end());
text->setText( wstr.data() );
text->setPosition(position);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
--
Xiaodong Wu
ÎâÏþ¶«
"'Xiao' means the time and the view when the sun rises and 'dong' means the
east in Chinese"
Xiaodong Wu
ÎâÏþ¶«
"'Xiao' means the time and the view when the sun rises and 'dong' means the
east in Chinese"