|
想在窗口的右上角显示一个图片标志。我看到在例子osgpick中利用hudcamera显示文字,可以让文字一直处于最前端的屏幕上。
那么我要做的就是将文字替换成图片就可以。但是死活就是看不到图片。不知道这里面有什么需要注意的地方,谢谢。
另附我的代码如下:
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osg/Geode>
#include <osg/Texture2D>
#include <osgViewer/Viewer>
#include <osg/PositionAttitudeTransform>
#include <osgText/Text>
int main( int argc, char **argv )
{
osgViewer::Viewer viewer;
osg::ref_ptr<osg::Group> rootNode = new osg::Group();
rootNode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
osg::Geode* geode = new osg::Geode;
osg::Image* image = osgDB::readImageFile("Catch/a.png");
if (image)
{
osg::Vec3 _origin= osg::Vec3(-1.0f,0.0f,0.0f);
osg::Vec3 _width;
osg::Vec3 _height;
_width.set(2.56f,0.0f,0.0f);
_height.set(0.0f,0.0f,2.56f);
osg::Geometry* geometry = osg::createTexturedQuadGeometry(_origin,_width,_height);
osg::StateSet* stateset = geometry->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(0,new osg::Texture2D(image),osg::StateAttribute::ON);
stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
geode->addDrawable(geometry);
}
osg::Camera* hudCamera = new osg::Camera;
hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
hudCamera->setProjectionMatrixAsOrtho2D(0,1280,0,1024);
hudCamera->setViewMatrix(osg::Matrix::identity());
hudCamera->setRenderOrder(osg::Camera:OST_RENDER);
hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
osg::PositionAttitudeTransform* transform = new osg::PositionAttitudeTransform;
transform->setPosition(osg::Vec3(0.0,-10.0,0.0));
transform->addChild(geode);
hudCamera->addChild(transform);
rootNode->addChild(hudCamera);
{//为了验证hudcamera有效,添加了文字,但是文字能显示,但图片没见到。
std::string timesFont("fonts/times.ttf");
// turn lighting off for the text and disable depth test to ensure its always ontop.
osg::Vec3 position(50.0f,80.0f,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);
geode->setName("simple");
hudCamera->addChild(geode);
osgText::Text* text = new osgText::Text;
geode->addDrawable( text );
text->setFont(timesFont);
text->setText("icking in Head Up Displays is simple!");
text->setPosition(position);
}
// rootNode->addChild(transform);//如果加这句可以看到图片,说明图片调用正确。
viewer.setSceneData(rootNode.get());
viewer.realize();
osg::Matrix matrix;
matrix.makeLookAt(osg::Vec3(0.0f,1.0,0.0f),osg::Vec3(0.0f,0.0f,0.0f),osg::Vec3(0.0f,0.0f,1.0f));
viewer.getCamera()->setViewMatrix(matrix);
while( !viewer.done() )
{
viewer.frame();
}
return 0;
// return viewer.run();
} |
|