|
本帖最后由 taylor66 于 2014-5-7 16:33 编辑
在网上找了一个HUD文字更新的帖子,编译通过但是在多视图模式下却显示不出来文字,不知道为什么,求大神指导;
第一部分代码为单视图模式下的,可以动态更新,第二部分代码是多视图下的,不能动态更新
#ifdef _DEBUG
#pragma comment(lib,"osgd.lib")
#pragma comment(lib,"osgDBd.lib")
#pragma comment(lib,"osgViewerd.lib")
#pragma comment(lib,"osgGAd.lib")
#pragma comment(lib,"osgTextd.lib")
#pragma comment(lib,"OpenThreadsd.lib")
#else
#pragma comment(lib,"osg.lib")
#pragma comment(lib,"osgDB.lib")
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osgGA.lib")
#pragma comment(lib,"osgText.lib")
#pragma comment(lib,"osgThreads.lib")
#endif
#include <osgViewer/Viewer>
#include <osg/NodeCallback>
#include <osg/Group>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osg/Notify>
#include <osgText/Font>
#include <osgText/Text>
#include <osg/CameraNode>
#include <iostream>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/Geode>
#include <osg/Depth>
#include <osg/CameraNode>
#include <osgText/Text>
#include <osgGA/TrackballManipulator>
#include <sstream>
#pragma comment( lib, "osgd.lib"); //.在Debug版本下的库名都加d,如"osgd.lib"
#pragma comment( lib, "osgDBd.lib")
#pragma comment( lib, "osgViewerd.lib");
#pragma comment( lib, "osgTextd.lib");
#pragma comment( lib, "osgGAd.lib");
using namespace std;
//事件类
class CHUD_viewPoint: public osgGA::GUIEventHandler
{
public:
/**构造函数*/
CHUD_viewPoint(osgText::Text* updateText):
m_text(updateText) {}
~CHUD_viewPoint(){}
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa);
void UpdateText(osgViewer::Viewer* viewer,const osgGA::GUIEventAdapter&);
/**LABEL*/
void setLabel(const std::string& name)
{
if ( m_text.get())
{
m_text->setText(name);
}
}
protected:
osg::Vec3 m_vPosEye;//视点坐标
osg::ref_ptr<osgText::Text> m_text;//视点信息,会动态改变
};
bool CHUD_viewPoint::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::FRAME):
{
osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
if (viewer)
{
osg::Vec3 vCenter, vUp;
viewer->getCamera()->getViewMatrixAsLookAt( m_vPosEye, vCenter, vUp);//获取视点信息
UpdateText( viewer, ea);//更新文字信息
}
return true;
}
default:
return false;
}
}
void CHUD_viewPoint::UpdateText(osgViewer::Viewer* viewer,const osgGA::GUIEventAdapter&)
{
std::string gdlist="";
std:stringstream os;
os<<"Eye( X: "<< m_vPosEye[ 0]<<" Y: "<<m_vPosEye[ 1]<<" Z: "<<m_vPosEye[ 2]<<")";
gdlist = os.str();
setLabel(gdlist);
}
osg::Node* createHUD_viewPoint( osgText::Text* text)
{
//文字
//osgText::Text* text = new osgText::Text;
//设置字体
std::string caiyun("fonts /STCAIYUN.TTF");//此处设置的是汉字字体
text->setFont(caiyun);
//设置文字显示的位置
osg::Vec3 position(100.0f,100.0f,0.0f);
text->setPosition(position);
text->setColor( osg::Vec4( 1, 1, 0, 1));
text->setText(L"");//设置显示的文字
text->setCharacterSize(15);
text->setDataVariance(osg::Object:YNAMIC);//一定要设置字体为动态,否则程序会卡住,死在那里。(参照osgcatch)
//几何体节点
osg::Geode* geode = new osg::Geode();
geode->addDrawable( text );//将文字Text作这drawable加入到Geode节点中
//设置状态
osg::StateSet* stateset = geode->getOrCreateStateSet();
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);//关闭灯光
stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);//关闭深度测试
//打开GL_BLEND混合模式(以保证Alpha纹理正确)
stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
//相机
osg::Camera* camera = new osg::Camera;
//设置透视矩阵
camera->setProjectionMatrix(osg::Matrix::ortho2D(0,600,0,600));//正交投影
//设置绝对参考坐标系,确保视图矩阵不会被上级节点的变换矩阵影响
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
//视图矩阵为默认的
camera->setViewMatrix(osg::Matrix::identity());
//设置背景为透明,否则的话可以设置ClearColor
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
camera->setAllowEventFocus( false);//不响应事件,始终得不到焦点
//设置渲染顺序,必须在最后渲染
camera->setRenderOrder(osg::CameraNode:OST_RENDER);
camera->addChild(geode);//将要显示的Geode节点加入到相机
return camera;
};
int main( int argc, char **argv )
{
osgViewer::Viewer viewer;
osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("fountain.osg");
osg::ref_ptr<osg::Group> root= new osg::Group;
root->addChild( model.get());//加入某个模型
osgText::Text* text = new osgText::Text;
osg::ref_ptr< CHUD_viewPoint> pHUD= new CHUD_viewPoint( text);
root->addChild( createHUD_viewPoint( text));//加入HUD文字
viewer.addEventHandler( pHUD.get());
viewer.setSceneData( root.get());
viewer.realize();
viewer.run() ;
return 0;
}
但是在多视图模式下,就是不行,不知道为什么求大神指导
#ifdef _DEBUG
#pragma comment(lib,"osgd.lib")
#pragma comment(lib,"osgDBd.lib")
#pragma comment(lib,"osgViewerd.lib")
#pragma comment(lib,"osgGAd.lib")
#pragma comment(lib,"osgTextd.lib")
#pragma comment(lib,"OpenThreadsd.lib")
#else
#pragma comment(lib,"osg.lib")
#pragma comment(lib,"osgDB.lib")
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osgGA.lib")
#pragma comment(lib,"osgText.lib")
#pragma comment(lib,"OpenThreads.lib")
#endif
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgViewer/CompositeViewer>
#include <osg/Geode>
#include <osg/Depth>
#include <osg/CameraNode>
#include <osgText/Text>
#include <osgGA/TrackballManipulator>
#include <sstream>
#pragma comment( lib, "osgd.lib"); //.在Debug版本下的库名都加d,如"osgd.lib"
#pragma comment( lib, "osgDBd.lib")
#pragma comment( lib, "osgViewerd.lib");
#pragma comment( lib, "osgTextd.lib");
#pragma comment( lib, "osgGAd.lib");
using namespace std;
//事件类
class CHUD_viewPoint: public osgGA::GUIEventHandler
{
public:
/**构造函数*/
CHUD_viewPoint(osgText::Text* updateText):
m_text(updateText) {}
~CHUD_viewPoint(){}
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa);
void UpdateText(osgViewer::Viewer* viewer,const osgGA::GUIEventAdapter&);
/**LABEL*/
void setLabel(const std::string& name)
{
if ( m_text.get())
{
m_text->setText(name);
}
}
protected:
osg::Vec3 m_vPosEye;//视点坐标
osg::ref_ptr<osgText::Text> m_text;//视点信息,会动态改变
};
bool CHUD_viewPoint::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::FRAME):
{
osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
if (viewer)
{
osg::Vec3 vCenter, vUp;
viewer->getCamera()->getViewMatrixAsLookAt( m_vPosEye, vCenter, vUp);//获取视点信息
UpdateText( viewer, ea);//更新文字信息
}
return true;
}
default:
return false;
}
}
void CHUD_viewPoint::UpdateText(osgViewer::Viewer* viewer,const osgGA::GUIEventAdapter&)
{
std::string gdlist="";
std::ostringstream os;
os<<"Eye( X: "<< m_vPosEye[ 0]<<" Y: "<<m_vPosEye[ 1]<<" Z: "<<m_vPosEye[ 2]<<")";
gdlist = os.str();
setLabel(gdlist);
}
osg::Node* createHUD_viewPoint( osgText::Text* text)
{
//文字
//osgText::Text* text = new osgText::Text;
//设置字体
std::string caiyun("fonts /STCAIYUN.TTF");//此处设置的是汉字字体
text->setFont(caiyun);
//设置文字显示的位置
osg::Vec3 position(100.0f,100.0f,0.0f);
text->setPosition(position);
text->setColor( osg::Vec4( 1, 1, 0, 1));
text->setText(L"");//设置显示的文字
text->setCharacterSize(15);
text->setDataVariance(osg::Object::DYNAMIC);//一定要设置字体为动态,否则程序会卡住,死在那里。(参照osgcatch)
//几何体节点
osg::Geode* geode = new osg::Geode();
geode->addDrawable( text );//将文字Text作这drawable加入到Geode节点中
//设置状态
osg::StateSet* stateset = geode->getOrCreateStateSet();
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);//关闭灯光
stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);//关闭深度测试
//打开GL_BLEND混合模式(以保证Alpha纹理正确)
stateset->setMode(GL_BLEND,osg::StateAttribute::ON);
//相机
osg::Camera* camera = new osg::Camera;
//设置透视矩阵
camera->setProjectionMatrix(osg::Matrix::ortho2D(0,600,0,600));//正交投影
//设置绝对参考坐标系,确保视图矩阵不会被上级节点的变换矩阵影响
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
//视图矩阵为默认的
camera->setViewMatrix(osg::Matrix::identity());
//设置背景为透明,否则的话可以设置ClearColor
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
camera->setAllowEventFocus( false);//不响应事件,始终得不到焦点
//设置渲染顺序,必须在最后渲染
camera->setRenderOrder(osg::CameraNode::POST_RENDER);
camera->addChild(geode);//将要显示的Geode节点加入到相机
return camera;
};
osg::ref_ptr<osgViewer::CompositeViewer>viewer=new osgViewer::CompositeViewer;
void CreateCamera()
{
osg::GraphicsContext::WindowingSystemInterface*wsi=osg::GraphicsContext::getWindowingSystemInterface();
unsigned int width,height;
wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0),width,height);
osg::ref_ptr<osg::GraphicsContext::Traits>traits=new osg::GraphicsContext::Traits;
traits->x=0;
traits->x=0;
traits->height=height;
traits->width=width;
traits->windowDecoration=false;
traits->doubleBuffer =true;
traits->sharedContext=0;
osg::ref_ptr<osg::GraphicsContext>gc=osg::GraphicsContext::createGraphicsContext(traits);
gc->setClearMask(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
gc->setClearColor(osg::Vec4(113.0/255.0,145.0/255.0,194.0/255.0,1.0));
osg::ref_ptr<osgViewer::View>viewmaster=new osgViewer::View;
viewmaster->setName("master");
viewer->addView(viewmaster);
osg::ref_ptr <osg::Group >root=new osg::Group;
root->addChild(osgDB::readNodeFile("cow.osg"));
osgText::Text*text=new osgText::Text;
osg::ref_ptr<CHUD_viewPoint>pHUD=new CHUD_viewPoint(text);
root->addChild(createHUD_viewPoint(text));
viewmaster->addEventHandler(pHUD.get());
viewmaster->setSceneData(root.get());
viewmaster->getCamera()->setViewport(new osg::Viewport(0,0,width*4/5,height*4/5));
viewmaster->getCamera()->setGraphicsContext(gc);
viewmaster->getCamera()->setClearMask(GL_COLOR_BUFFER_BIT);
viewmaster->setCameraManipulator(new osgGA::TrackballManipulator);
}
int main( int argc, char **argv )
{
CreateCamera();
viewer->run();
return 0;
} |
|