Nick, here is a working code showing the label disappearing problem:

Code:

#include <osgViewer/Viewer>
#include <osgText/Text>
#include <osg/LineWidth>
#include <osg/Geometry>
#include <osgGA/TrackballManipulator>

osgText::Text* createAxisLabel(const std::string& iLabel, const osg::Vec3& 
iPosition)
{
   osgText::Text* text = new  osgText::Text;
   text->setFont("arial.ttf");
   text->setText(iLabel);
   text->setPosition(iPosition);
   text->setCharacterSize(17);
   text->setAutoRotateToScreen(true);
   text->setColor(osg::Vec4(204, 204, 0, 1));
   text->setCharacterSizeMode(osgText::Text::SCREEN_COORDS);
   return text;
}

osg::Geometry* createArrow(const osg::Matrixd& iTransform, const osg::Vec4& 
iColor, double iHeight)
{
   osg::Geometry* geometry = new osg::Geometry;

   double pyramidBaseZ = iHeight/3.0*2.0;
   double outerBaseRadius = iHeight/9.0;
   osg::Vec3Array* vertices = new osg::Vec3Array(7);
   (*vertices)[0].set(iTransform.preMult(osg::Vec3d(outerBaseRadius, 0.0, 
pyramidBaseZ)));
   (*vertices)[1].set(iTransform.preMult(osg::Vec3d(0.0, outerBaseRadius, 
pyramidBaseZ)));
   (*vertices)[2].set(iTransform.preMult(osg::Vec3d(-outerBaseRadius, 0.0, 
pyramidBaseZ)));
   (*vertices)[3].set(iTransform.preMult(osg::Vec3d(0.0, -outerBaseRadius, 
pyramidBaseZ)));
   (*vertices)[4].set(iTransform.preMult(osg::Vec3d(0.0, 0.0, iHeight)));
   (*vertices)[5].set(iTransform.preMult(osg::Vec3d(0.0, 0.0, iHeight)));
   (*vertices)[6].set(iTransform.preMult(osg::Vec3d(0.0, 0.0, 0.0)));

   osg::UByteArray* indices = new osg::UByteArray(20);
   (*indices)[0]=0;  (*indices)[1]=1;  (*indices)[2]=4;
   (*indices)[3]=1;  (*indices)[4]=2;  (*indices)[5]=4;
   (*indices)[6]=2;  (*indices)[7]=3;  (*indices)[8]=4;
   (*indices)[9]=3;  (*indices)[10]=0; (*indices)[11]=4;
   (*indices)[12]=1; (*indices)[13]=0; (*indices)[14]=3;
   (*indices)[15]=2; (*indices)[16]=1; (*indices)[17]=3;
   (*indices)[18]=5;
   (*indices)[19]=6;

   geometry->setVertexArray(vertices);
   geometry->setVertexIndices(indices);

   osg::Vec4Array* colors = new osg::Vec4Array;
   colors->push_back(iColor);
   geometry->setColorArray(colors);
   geometry->setColorBinding(osg::Geometry::BIND_OVERALL);

   geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 
0, indices->size()-2));
   geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 
indices->size()-2, 2));

   return geometry;
}

osg::Geometry* createXAxis(double iHeight)
{
   osg::Matrixd transform = osg::Matrix::rotate(osg::inDegrees(90.0f), 0.0f, 
1.0f, 0.0f);
   osg::Vec4 color(0.5f,0.125f,0.125f,1.0f);
   osg::Geometry* geometry = createArrow(transform, color, iHeight);
   return geometry;
}

osg::Geometry* createYAxis(double iHeight)
{
   osg::Matrixd transform = osg::Matrix::rotate(osg::inDegrees(-90.0f), 1.0f, 
0.0f, 0.0f);
   osg::Vec4 color(0.125f,0.5f,0.125f,1.0f);
   osg::Geometry* geometry = createArrow(transform, color, iHeight);
   return geometry;
}

osg::Geometry* createZAxis(double iHeight)
{
   osg::Matrixd transform = osg::Matrix::identity();
   osg::Vec4 color(0.125f,0.125f,0.5f,1.0f);
   osg::Geometry* geometry = createArrow(transform, color, iHeight);
   return geometry;
}

osg::Geode* createAxesGeometry()
{
   osg::Geode* geode = new osg::Geode;

   osg::LineWidth* lineWidth = new osg::LineWidth(2);
   geode->getOrCreateStateSet()->setAttributeAndModes(lineWidth, 
osg::StateAttribute::ON);
   geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);

   double length = 70.0;
   geode->addDrawable(createXAxis(length));
   geode->addDrawable(createYAxis(length));
   geode->addDrawable(createZAxis(length));
   geode->addDrawable(createAxisLabel("X", osg::Vec3(length*1.05, 0, 0)));
   geode->addDrawable(createAxisLabel("Y", osg::Vec3(0, length*1.05, 0)));
   geode->addDrawable(createAxisLabel("Z", osg::Vec3(0, 0, length*1.05)));

   return geode;
}

class AxisCameraUpdateCallback:public osg::NodeCallback
{
public:
   virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
   {
      if(nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
      {
         osg::Camera* camera = dynamic_cast<osg::Camera*>(node);
         if (camera)
         {
            osg::View* view = camera->getView();
            if (view && view->getNumSlaves() > 0)
            {
               osg::View::Slave* slave = &view->getSlave(0);
               if(slave->_camera.get() == camera)
               {
                  osg::Camera* masterCam = view->getCamera();
                  osg::Vec3 eye, center, up;
                  masterCam->getViewMatrixAsLookAt(eye, center, up, 30);
                  osg::Matrixd matrix;
                  matrix.makeLookAt(eye-center, osg::Vec3(0, 0, 0), up); // 
always look at (0, 0, 0)
                  camera->setViewMatrix(matrix);
               }
            }
         }
      }
      traverse(node,nv);
   }
}; 

osg::Camera* createHUD()
{
   osg::Camera* camera = new osg::Camera;
   camera->setProjectionMatrix(osg::Matrix::ortho2D(-80, 1280-80, -80, 
1024-80));
   camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
   camera->setClearMask(GL_DEPTH_BUFFER_BIT);
   camera->setRenderOrder(osg::Camera::POST_RENDER);
   camera->setUpdateCallback(new AxisCameraUpdateCallback);

   osg::Geode* geode = createAxesGeometry();
   camera->addChild(geode);

   return camera;
}

int main( int argc, char **argv )
{
   osgViewer::Viewer viewer;
   osg::Group* root = new osg::Group;
   osg::Camera* camera = createHUD();
   viewer.addSlave(camera, false);
   root->addChild(camera);
   viewer.setSceneData(root);
   viewer.setCameraManipulator(new osgGA::TrackballManipulator);
   return viewer.run();
}




Regards
Gianni

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=29584#29584





_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to