OSG3.0 +cegui0.7.5 结合(支持中文显示)

来源:互联网 发布:柚子装修报价软件 编辑:程序博客网 时间:2024/06/06 01:17

cegui0.7.5 必须添加代码 setDefaultFont。设置 默认字体。 用0.6的 就没用就可以显示中文...浪费半天时间 惨啊。

也可以改变scheme文件 。。也可以 用代码可以这样写:

    CEGUI::FontManager::getSingleton().createFreeTypeFont("JianTi", 12/*pt*/, true, "msyh.ttf");
    CEGUI::System::getSingleton().setDefaultFont("simfang-12");

全部代码 如下。

CEGUIEventCallback.h

#pragma once#include<osgViewer/Viewer>#include<osgDB/ReadFile>#include <osgViewer/ViewerEventHandlers>#include <osgGA/StateSetManipulator>#include <osgGA/TrackballManipulator>#include <osg/ShapeDrawable>#include <osg/ImageSequence>#include<CEGUISystem.h>//#include <RendererModules/OpenGL/OpenGLRenderer.h>//#include <RendererModules/OpenGL/CEGUIOpenGL.h>//#include <RendererModules/OpenGLguirenderer/OpenGLRenderer.h>#include <osg/Geode>#include <CEGUIScriptModule.h>#include <CEGUIFontManager.h>#include <CEGUISchemeManager.h>#include <CEGUIWindowManager.h>#include <CEGUIExceptions.h>#include <CEGUIimageset.h>#include <CEGUIFont.h>#include <CEGUIScheme.h>#include <CEGUIDefaultResourceProvider.h>#include <CEGUIWidgetModule.h>#include <falagard/CEGUIFalWidgetLookManager.h>#include <elements/CEGUIPushButton.h>struct CEGUIEventCallback : public osgGA::GUIEventHandler{CEGUIEventCallback() {}/** do customized Event code. */virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object* obj, osg::NodeVisitor* nv){float x = ea.getX();float y = ea.getY();bool injectionRetVal = false;// CEGUI wants 0,0 in the top left corner; osg has 0,0 in the bottom left cornery = ea.getWindowHeight() - y;switch(ea.getEventType()){case(osgGA::GUIEventAdapter::DRAG):{injectionRetVal = CEGUI::System::getSingleton().injectMousePosition(x, y);break;}case(osgGA::GUIEventAdapter::MOVE):{CEGUI::System::getSingleton().injectMousePosition(x,y);break;}case(osgGA::GUIEventAdapter::PUSH):{CEGUI::System::getSingleton().injectMousePosition(x, y);if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)  // leftinjectionRetVal = CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton);else if (ea.getButton() == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON)  // middleinjectionRetVal = CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);else if (ea.getButton() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)  // rightinjectionRetVal = CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton);break;}case(osgGA::GUIEventAdapter::RELEASE):{CEGUI::System::getSingleton().injectMousePosition(x, y);if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)  // leftinjectionRetVal = CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::LeftButton);else if (ea.getButton() == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON)  // middleinjectionRetVal = CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::MiddleButton);else if (ea.getButton() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)  // rightinjectionRetVal = CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::RightButton);break;}case(osgGA::GUIEventAdapter::DOUBLECLICK):{CEGUI::System::getSingleton().injectMousePosition(x, y);if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)  // leftCEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton);else if (ea.getButton() == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON)  // middleCEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);else if (ea.getButton() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)  // rightCEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton);break;}case(osgGA::GUIEventAdapter::KEYDOWN):{injectionRetVal = CEGUI::System::getSingleton().injectKeyDown( static_cast<CEGUI::uint>(ea.getKey()) );injectionRetVal = injectionRetVal || CEGUI::System::getSingleton().injectChar( static_cast<CEGUI::utf32>( ea.getKey() ) );break;}case(osgGA::GUIEventAdapter::KEYUP):injectionRetVal = CEGUI::System::getSingleton().injectKeyUp( static_cast<CEGUI::uint>(ea.getKey()) );break;default:break;}CEGUI::Window* window = CEGUI::System::getSingleton().getGUISheet()->getCaptureWindow();if (window && window->getParent()){return injectionRetVal;}// do not consume the event, forward on to osg::Viewerreturn false;}virtual void accept(osgGA::GUIEventHandlerVisitor& v){v.visit(*this);}}; 


 

CEGUIDrawable.h

#pragma once#include "CEGUIEventCallback.h"#include<osgViewer/Viewer>#include<osgDB/ReadFile>//#include "MyDriveManipulator.h"//#include "CameraManipulator.h"#include <osgViewer/ViewerEventHandlers>#include <osgGA/StateSetManipulator>#include <osgGA/TrackballManipulator>#include <osg/ShapeDrawable>#include<CEGUISystem.h>#include <RendererModules/OpenGL/CEGUIOpenGLRenderer.h>//#include <RendererModules/OpenGLguirenderer/OpenGLRenderer.h>#include <RendererModules/OpenGL/CEGUIOpenGL.h>#include <osg/Geode>#include <CEGUI.h>#include <CEGUIScriptModule.h>#include <CEGUIFontManager.h>#include <CEGUISchemeManager.h>#include <CEGUIWindowManager.h>#include "CEGUIAnimationManager.h"#include "CEGUIAnimationInstance.h"#include <CEGUIExceptions.h>#include <CEGUIimageset.h>#include <CEGUIFont.h>#include <CEGUIScheme.h>#include <CEGUIDefaultResourceProvider.h>#include <CEGUIWidgetModule.h>#include <falagard/CEGUIFalWidgetLookManager.h>#include <elements/CEGUIPushButton.h>#include <elements/CEGUIRadioButton.h>#include <elements/CEGUICheckbox.h>#include <osgGA/AnimationPathManipulator>class CEGUIDrawable : public osg::Drawable{public:CEGUIDrawable();/** Copy constructor using CopyOp to manage deep vs shallow copy.*/CEGUIDrawable(const CEGUIDrawable& drawable,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):Drawable(drawable,copyop) {}META_Object(osg,CEGUIDrawable);void loadScheme(const std::string& scheme);void loadFont(const std::string& font);void loadLayout(const std::string& layout);void drawImplementation(osg::RenderInfo& renderInfo) const;osg::Camera* createHUD(int width, int height);protected:    void initializeResourceDirs();void setDefaultResourceGroups();virtual ~CEGUIDrawable();unsigned int _activeContextID;};

CEGUIDrawable.cpp

#include "CEGUIDrawable.h"#include <osg/PositionAttitudeTransform>CEGUIDrawable::CEGUIDrawable(){setSupportsDisplayList(false);setEventCallback(new CEGUIEventCallback());// Create an OpenGLRenderer object that uses the current GL viewport as// the default output surface.CEGUI::OpenGLRenderer& myRenderer = CEGUI::OpenGLRenderer::create();CEGUI::System::create(myRenderer);// clearing this queue actually makes sure it's created(!)myRenderer.getDefaultRenderingRoot().clearGeometry(CEGUI::RQ_OVERLAY);// initialise the required dirs for the DefaultResourceProviderinitializeResourceDirs();setDefaultResourceGroups();CEGUI::SchemeManager::getSingleton().create("TaharezLook.scheme");//CEGUI::FontManager::getSingleton().create("simfang-12.font");CEGUI::System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");CEGUI::WindowManager& wmgr = CEGUI::WindowManager::getSingleton();CEGUI::Window* myRoot = wmgr.createWindow( "DefaultWindow", "root" );CEGUI::System::getSingleton().setGUISheet( myRoot );//CEGUI::FontManager::getSingleton().createFreeTypeFont("JianTi", 12/*pt*/, true, "msyh.ttf");CEGUI::System::getSingleton().setDefaultFont("simfang-12");//CEGUI::FrameWindow* fWnd = static_cast<CEGUI::FrameWindow*>(wmgr.createWindow( "TaharezLook/FrameWindow", "testWindow" ));//fWnd->setProperty("Alpha", "0.8");//myRoot->addChildWindow( fWnd );//// position a quarter of the way in from the top-left of parent.//fWnd->setPosition( CEGUI::UVector2( CEGUI::UDim( 0.25f, 0 ), CEGUI::UDim( 0.25f, 0 ) ) );////// set size to be half the size of the parent//fWnd->setSize(CEGUI::UVector2(cegui_reldim(0.25f), cegui_reldim( 0.25f)));//fWnd->setMaxSize(CEGUI::UVector2(cegui_reldim(1.0f), cegui_reldim( 1.0f)));//fWnd->setMinSize(CEGUI::UVector2(cegui_reldim(0.1f), cegui_reldim( 0.1f)));//fWnd->setText( "Hello World!" );std::wstring swstt = L"帮助";char buff[128] = "";WideCharToMultiByte(CP_UTF8,0,swstt.c_str(),swstt.size(),buff,sizeof(buff),0,0);CEGUI::Window* buttonEast = CEGUI::WindowManager::getSingleton().createWindow("TaharezLook/Button");buttonEast->setPosition(CEGUI::UVector2(cegui_reldim(0.2f),cegui_reldim(0.1f)));buttonEast->setSize(CEGUI::UVector2(cegui_absdim(161),cegui_absdim(50)));buttonEast->setText((CEGUI::utf8*)buff);myRoot->addChildWindow(buttonEast);////加入 帮助 对话框std::wstring sws = L"帮助";char buffee[128] = "";WideCharToMultiByte(CP_UTF8,0,sws.c_str(),sws.size(),buffee,sizeof(buffee),0,0);//创建提示窗口CEGUI::WindowManager& winMgr = CEGUI::WindowManager::getSingleton();CEGUI::Window* mDialogHelp = (CEGUI::Window*)winMgr.createWindow("TaharezLook/FrameWindow","help");mDialogHelp->setPosition(CEGUI::UVector2(cegui_reldim(0.25f),cegui_reldim(0.25f)));mDialogHelp->setSize(CEGUI::UVector2(cegui_reldim(0.5f),cegui_reldim(0.5f)));mDialogHelp->setMaxSize(CEGUI::UVector2(cegui_reldim(1.0f),cegui_reldim(1.0f)));mDialogHelp->setMinSize(CEGUI::UVector2(cegui_reldim(0.1f),cegui_reldim(0.1f)));mDialogHelp->setText((CEGUI::utf8*)buff);mDialogHelp->setProperty("Alpha","0.80");myRoot->addChildWindow(mDialogHelp);CEGUI::Window* helpText = CEGUI::WindowManager::getSingleton().createWindow("TaharezLook/StaticText");helpText->setProperty("VertFormatting", "TopAligned");//设置顶对齐std::wstring swstr = L"操作说明: \n漫游操作控制:\n前 W键 后 S键\n左 A键 右 D键 \n摄像机旋转:按鼠标左键\n加速:+,减速:-\n上升:Q 下降:E\n\n\nF2:隐藏界面  F5:截图 \n帮助:F1\n退出:ESC.";char buffw[256] = "";WideCharToMultiByte(CP_UTF8,0,swstr.c_str(),swstr.size(),buffw,sizeof(buffw),0,0);helpText->setText((CEGUI::utf8*)buffw);helpText->setAlpha(0.8);//helpText->setProperty("BackgroundColours", "tl: FFFF0000 tr:FFFF0000 bl:FFFF0000 br:FFFF0000" ); helpText->setPosition(CEGUI::UVector2(cegui_reldim(0.1f),cegui_reldim(0.1f)));helpText->setSize(CEGUI::UVector2(cegui_reldim(0.8f),cegui_reldim(0.8f)));mDialogHelp->addChildWindow(helpText);//_activeContextID = 0;}CEGUIDrawable::~CEGUIDrawable(){CEGUI::System::getSingleton().destroy();}void CEGUIDrawable::initializeResourceDirs(){CEGUI::DefaultResourceProvider* rp = static_cast<CEGUI::DefaultResourceProvider*>(CEGUI::System::getSingleton().getResourceProvider());// update to the directory for your datafiles -- sample datafiles are available with the CEGUI SDK downloadrp->setResourceGroupDirectory("schemes", "F:/work/september/debug/resources/datafiles/schemes/");rp->setResourceGroupDirectory("imagesets", "F:/work/september/debug/resources/datafiles/imagesets/");rp->setResourceGroupDirectory("fonts", "F:/work/september/debug/resources/datafiles/fonts/");rp->setResourceGroupDirectory("layouts", "F:/work/september/debug/resources/datafiles/layouts/");rp->setResourceGroupDirectory("looknfeels", "F:/work/september/debug/resources/datafiles/looknfeel/");rp->setResourceGroupDirectory("lua_scripts", "F:/work/september/debug/resources/datafiles/lua_scripts/");rp->setResourceGroupDirectory("local_schemes", "F:/work/september/debug/resources/datafiles/schemes/");rp->setResourceGroupDirectory("local_imagesets", "F:/work/september/debug/resources/datafiles/imagesets/");rp->setResourceGroupDirectory("local_fonts", "F:/work/september/debug/resources/datafiles/fonts/");rp->setResourceGroupDirectory("local_layouts", "F:/work/september/debug/resources/datafiles/layouts/");rp->setResourceGroupDirectory("local_looknfeels", "F:/work/september/debug/resources/datafiles/looknfeel/");rp->setResourceGroupDirectory("local_lua_scripts", "F:/work/september/debug/resources/datafiles/lua_scripts/");// This is only really needed if you are using Xerces and need to// specify the schemas locationrp->setResourceGroupDirectory("schemas", "F:/work/september/debug/resources/datafiles/xml_schemas/");}void CEGUIDrawable::setDefaultResourceGroups(){CEGUI::Imageset::setDefaultResourceGroup("imagesets");CEGUI::Font::setDefaultResourceGroup("fonts");CEGUI::Scheme::setDefaultResourceGroup("schemes");CEGUI::WidgetLookManager::setDefaultResourceGroup("looknfeels");CEGUI::WindowManager::setDefaultResourceGroup("layouts");CEGUI::ScriptModule::setDefaultResourceGroup("lua_scripts");}void CEGUIDrawable::loadFont(const std::string& font){try{CEGUI::Font& f = CEGUI::FontManager::getSingleton().create(font);}catch (CEGUI::Exception e){std::cout<<"CEGUIDrawable::loadFont Error: "<<e.getMessage()<<std::endl;}}void CEGUIDrawable::loadScheme(const std::string& scheme){try{CEGUI::Scheme& f = CEGUI::SchemeManager::getSingleton().create(scheme);}catch (CEGUI::Exception e){std::cout<<"CEGUIDrawable::loadScheme Error: "<<e.getMessage()<<std::endl;}}void CEGUIDrawable::loadLayout(const std::string& layout){try{CEGUI::Window* myRoot = CEGUI::WindowManager::getSingleton().loadWindowLayout(layout.c_str());CEGUI::System::getSingleton().setGUISheet(myRoot);}catch (CEGUI::Exception e){std::cout<<"CEGUIDrawable::loadLayout error: "<<e.getMessage()<<std::endl;}}void CEGUIDrawable::drawImplementation(osg::RenderInfo& renderInfo) const{osg::State* state = renderInfo.getState();if (state->getContextID()!=_activeContextID) return;glPushAttrib(GL_ALL_ATTRIB_BITS);state->disableAllVertexArrays();CEGUI::System::getSingleton().renderGUI();glPopAttrib();state->checkGLErrors("CEGUIDrawable::drawImplementation");}


main.cpp

#include <osgGA/GUIEventHandler>#include <osgViewer/Viewer>#include <osgViewer/ViewerEventHandlers>#include <osgDB/ReadFile>#include <osgGA/GUIEventAdapter>#include <osgGA/GUIEventHandler>#include <osg/Drawable>#include <osg/Camera>#include <osg/PagedLOD>#include <RendererModules/OpenGL/CEGUIOpenGL.h>#include <RendererModules/OpenGL/CEGUIOpenGLRenderer.h>#include <osg/Depth>#include <iostream>#include "CEGUIDrawable.h"osg::Camera* createHUD(int width, int height){// create a camera to set up the projection and model view matrices, and the subgraph to draw in the HUDosg::Camera* camera = new osg::Camera;// set the projection matrixcamera->setProjectionMatrix(osg::Matrix::ortho2D(0,width,0,height));// set the view matrix    camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);camera->setViewMatrix(osg::Matrix::identity());// only clear the depth buffercamera->setClearMask(GL_DEPTH_BUFFER_BIT);// draw subgraph after main camera view.// The bin order is set to an arbitrarily large number to ensure that // there is no conflict with other post rendering activity (e.g., osgEarth)camera->setRenderOrder(osg::Camera::POST_RENDER/*, 10000*/);// we don't want the camera to grab event focus from the viewers main camera(s).camera->setAllowEventFocus(false);return camera;}int main(int argc, char** argv){osgViewer::Viewer viewer;//viewer.getCamera()->setClearColor(osg::Vec4(0.f,0.f,0.f,1.f));viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);viewer.realize();viewer.getCamera()->getGraphicsContext()->makeCurrent(); osgViewer::Viewer::Windows windows;viewer.getWindows(windows);if (windows.empty()){return 1;}osg::Node* node = osgDB::readNodeFile("F:\\work\\lou.osgt");//osg::Node* node2 = osgDB::readNodeFile("C:\\Users\\Administrator.PC-20110818ERIO\\Desktop\\test\\fz-plod.ive");osg::Group* root = new osg::Group(); root->addChild(node);   // root->addChild(node2);osg::ref_ptr<osg::Geode> geode = new osg::Geode;osg::ref_ptr<CEGUIDrawable> cd = new CEGUIDrawable();geode->addDrawable(cd.get());osg::StateSet* ss = geode->getOrCreateStateSet();ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF);ss->setMode(GL_DEPTH_TEST,osg::StateAttribute::ON);osg::Depth* depth = new osg::Depth(osg::Depth::LESS,0.0,1.0,false);ss->setAttributeAndModes(depth);// separate camera created to separate the 3d scene from the 2d GUIosg::Camera* hudCam = createHUD(windows[0]->getTraits()->width, windows[0]->getTraits()->height);hudCam->setGraphicsContext(windows[0]);hudCam->setViewport(0,0,windows[0]->getTraits()->width, windows[0]->getTraits()->height);viewer.addSlave(hudCam, false);hudCam->addChild(geode);viewer.setSceneData( root );return viewer.run();}




 

原创粉丝点击