查看: 2686|回复: 4

MYGUI+OSG交流讨论共同学习

[复制链接]

该用户从未签到

发表于 2013-11-25 09:37:59 | 显示全部楼层 |阅读模式
在论坛里看到ANN工程中有一个关于MYGUI与OSG结合的Demo,小弟最近刚好需要这方面的信息。把代码下载下来后正在研究,为了深究,把源代码放出来,供大家共同讨论研究,共同进步。

  1. #ifndef H_MYGUIDRAWABLE
  2. #define H_MYGUIDRAWABLE

  3. #include <MYGUI/MyGUI.h>
  4. #include <MYGUI/MyGUI_OpenGLPlatform.h>
  5. #include <osg/Camera>
  6. #include <osg/Drawable>
  7. #include <osgGA/GUIEventHandler>
  8. #include <queue>

  9. class MYGUIManager;

  10. class MYGUIHandler : public osgGA::GUIEventHandler
  11. {
  12. public:
  13.     MYGUIHandler( osg::Camera* c, MYGUIManager* m ) : _camera(c), _manager(m) {}
  14.     virtual bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa );
  15.    
  16. protected:
  17.     osg::observer_ptr<osg::Camera> _camera;
  18.     MYGUIManager* _manager;
  19. };

  20. class MYGUIManager : public osg::Drawable, public MyGUI::OpenGLImageLoader
  21. {
  22. public:
  23.     MYGUIManager();
  24.     MYGUIManager( const MYGUIManager& copy, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY );
  25.     META_Object( osg, MYGUIManager )
  26.    
  27.     void setResourcePathFile( const std::string& file ) { _resourcePathFile = file; }
  28.     const std::string& getResourcePathFile() const { return _resourcePathFile; }
  29.    
  30.     void setResourceCoreFile( const std::string& file ) { _resourceCoreFile = file; }
  31.     const std::string& getResourceCoreFile() const { return _resourceCoreFile; }
  32.    
  33.     void pushEvent( const osgGA::GUIEventAdapter* ea )
  34.     { _eventsToHandle.push( ea ); }
  35.    
  36.     // image loader methods
  37.     virtual void* loadImage( int& width, int& height, MyGUI::PixelFormat& format, const std::string& filename );
  38.     virtual void saveImage( int width, int height, MyGUI::PixelFormat format, void* texture, const std::string& filename );
  39.    
  40.     // drawable methods
  41.     virtual void drawImplementation( osg::RenderInfo& renderInfo ) const;
  42.     virtual void releaseGLObjects( osg::State* state=0 ) const;
  43.    
  44. protected:
  45.     virtual ~MYGUIManager() {}
  46.    
  47.     virtual void updateEvents() const;
  48.     virtual void setupResources();
  49.     virtual void initializeControls() {}
  50.    
  51.     MyGUI::MouseButton convertMouseButton( int button ) const;
  52.     MyGUI::KeyCode convertKeyCode( int key ) const;
  53.    
  54.     std::queue< osg::ref_ptr<const osgGA::GUIEventAdapter> > _eventsToHandle;
  55.     MyGUI::Gui* _gui;
  56.     MyGUI::OpenGLPlatform* _platform;
  57.     std::string _resourcePathFile;
  58.     std::string _resourceCoreFile;
  59.     std::string _rootMedia;
  60.     unsigned int _activeContextID;
  61.     bool _initialized;
  62. };

  63. #endif
复制代码

该用户从未签到

 楼主| 发表于 2013-11-25 09:38:32 | 显示全部楼层
  1. #include "MYGUIManager.h"
  2. #include <osgDB/ReadFile>
  3. #include <osgDB/WriteFile>

  4. bool MYGUIHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
  5. {
  6.     int width = ea.getWindowWidth(), height = ea.getWindowHeight();
  7.     switch ( ea.getEventType() )
  8.     {
  9.     case osgGA::GUIEventAdapter::RESIZE:
  10.         if ( _camera.valid() )
  11.         {
  12.             _camera->setProjectionMatrix( osg::Matrixd::ortho2D(0.0, width, 0.0, height) );
  13.             _camera->setViewport( 0.0, 0.0, width, height );
  14.         }
  15.         break;
  16.     default:
  17.         break;
  18.     }
  19.    
  20.     // As MyGUI handle all events within the OpenGL context, we have to record the event here
  21.     // and process it later in the draw implementation
  22.     if ( ea.getEventType()!=osgGA::GUIEventAdapter::FRAME )
  23.         _manager->pushEvent( &ea );
  24.     return false;
  25. }

  26. MYGUIManager::MYGUIManager()
  27. :   _gui(0), _platform(0),
  28.     _resourcePathFile("resources.xml"), _resourceCoreFile("MyGUI_Core.xml"),
  29.     _activeContextID(0), _initialized(false)
  30. {
  31.     setSupportsDisplayList( false );
  32.     getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
  33.     getOrCreateStateSet()->setMode( GL_DEPTH_TEST, osg::StateAttribute::OFF );
  34. }

  35. MYGUIManager::MYGUIManager( const MYGUIManager& copy,const osg::CopyOp& copyop )
  36. :   osg::Drawable(copy, copyop), _eventsToHandle(copy._eventsToHandle),
  37.     _gui(copy._gui), _platform(copy._platform),
  38.     _resourcePathFile(copy._resourcePathFile),
  39.     _resourceCoreFile(copy._resourceCoreFile),
  40.     _rootMedia(copy._rootMedia),
  41.     _activeContextID(copy._activeContextID),
  42.     _initialized(copy._initialized)
  43. {}

  44. void* MYGUIManager::loadImage( int& width, int& height, MyGUI::PixelFormat& format, const std::string& filename )
  45. {
  46.     std::string fullname = MyGUI::OpenGLDataManager::getInstance().getDataPath( filename );
  47.     osg::ref_ptr<osg::Image> image = osgDB::readImageFile( fullname );
  48.     void* result = NULL;
  49.     if ( image.valid() )
  50.     {
  51.         width = image->s();
  52.         height = image->t();
  53.         if ( image->getDataType()!=GL_UNSIGNED_BYTE || image->getPacking()!=1 )
  54.         {
  55.             format = MyGUI::PixelFormat::Unknow;
  56.             return result;
  57.         }
  58.         
  59.         unsigned int num = 0;
  60.         switch ( image->getPixelFormat() )
  61.         {
  62.         case GL_LUMINANCE: case GL_ALPHA: format = MyGUI::PixelFormat::L8; num = 1; break;
  63.         case GL_LUMINANCE_ALPHA: format = MyGUI::PixelFormat::L8A8; num = 2; break;
  64.         case GL_RGB: format = MyGUI::PixelFormat::R8G8B8; num = 3; break;
  65.         case GL_RGBA: format = MyGUI::PixelFormat::R8G8B8A8; num = 4; break;
  66.         default: format = MyGUI::PixelFormat::Unknow; return result;
  67.         }
  68.         
  69.         unsigned int size = width * height * num;
  70.         unsigned char* dest = new unsigned char[size];
  71.         image->flipVertical();
  72.         if ( image->getPixelFormat()==GL_RGB || image->getPixelFormat()==GL_RGBA )
  73.         {
  74.             // FIXME: I don't an additional conversion here but...
  75.             // MyGUI will automatically consider it as BGR so I should do such stupid thing
  76.             unsigned int step = (image->getPixelFormat()==GL_RGB ? 3 : 4);
  77.             unsigned char* src = image->data();
  78.             for ( unsigned int i=0; i<size; i+=step )
  79.             {
  80.                 dest[i+0] = src[i+2];
  81.                 dest[i+1] = src[i+1];
  82.                 dest[i+2] = src[i+0];
  83.                 if ( step==4 ) dest[i+3] = src[i+3];
  84.             }
  85.         }
  86.         else
  87.             memcpy( dest, image->data(), size );
  88.         result = dest;
  89.     }
  90.     return result;
  91. }

  92. void MYGUIManager::saveImage( int width, int height, MyGUI::PixelFormat format, void* texture, const std::string& filename )
  93. {
  94.     GLenum pixelFormat = 0;
  95.     unsigned int internalFormat = 0;
  96.     switch ( format.getValue() )
  97.     {
  98.     case MyGUI::PixelFormat::L8: pixelFormat = GL_ALPHA; internalFormat = 1; break;
  99.     case MyGUI::PixelFormat::L8A8: pixelFormat = GL_LUMINANCE_ALPHA; internalFormat = 2; break;
  100.     case MyGUI::PixelFormat::R8G8B8: pixelFormat = GL_BGR; internalFormat = 3; break;
  101.     case MyGUI::PixelFormat::R8G8B8A8: pixelFormat = GL_BGRA; internalFormat = 4; break;
  102.     default: return;
  103.     }
  104.    
  105.     unsigned int size = width * height * internalFormat;
  106.     unsigned char* imageData = new unsigned char[size];
  107.     memcpy( imageData, texture, size );
  108.    
  109.     osg::ref_ptr<osg::Image> image = new osg::Image;
  110.     image->setImage( width, height, 1, internalFormat, pixelFormat, GL_UNSIGNED_BYTE,
  111.         static_cast<unsigned char*>(imageData), osg::Image::USE_NEW_DELETE );
  112.     image->flipVertical();
  113.     osgDB::writeImageFile( *image, filename );
  114. }

  115. void MYGUIManager::drawImplementation( osg::RenderInfo& renderInfo ) const
  116. {
  117.     unsigned int contextID = renderInfo.getContextID();
  118.     if ( !_initialized )
  119.     {
  120.         MYGUIManager* constMe = const_cast<MYGUIManager*>(this);
  121.         constMe->_platform = new MyGUI::OpenGLPlatform;
  122.         constMe->_platform->initialise( constMe );
  123.         constMe->setupResources();
  124.         
  125.         constMe->_gui = new MyGUI::Gui;
  126.         constMe->_gui->initialise( _resourceCoreFile );
  127.         constMe->initializeControls();
  128.         
  129.         constMe->_activeContextID = contextID;
  130.         constMe->_initialized = true;
  131.     }
  132.     else if ( contextID==_activeContextID )
  133.     {
  134.         osg::State* state = renderInfo.getState();
  135.         state->disableAllVertexArrays();
  136.         state->disableTexCoordPointer( 0 );
  137.         
  138.         glPushMatrix();
  139.         glPushAttrib( GL_ALL_ATTRIB_BITS );
  140.                 if ( _platform )
  141.                 {
  142.                     updateEvents();
  143.                     _platform->getRenderManagerPtr()->drawOneFrame();
  144.         }
  145.         glPopAttrib();
  146.         glPopMatrix();
  147.     }
  148. }

  149. void MYGUIManager::releaseGLObjects( osg::State* state ) const
  150. {
  151.     if ( state && state->getGraphicsContext() )
  152.     {
  153.         osg::GraphicsContext* gc = state->getGraphicsContext();
  154.         if ( gc->makeCurrent() )
  155.         {
  156.             MYGUIManager* constMe = const_cast<MYGUIManager*>(this);
  157.             if ( constMe->_gui )
  158.             {
  159.                 constMe->_gui->shutdown();
  160.                 delete constMe->_gui;
  161.                 constMe->_gui = nullptr;
  162.             }
  163.             if ( constMe->_platform )
  164.             {
  165.                 constMe->_platform->shutdown();
  166.                 delete constMe->_platform;
  167.                 constMe->_platform = nullptr;
  168.             }
  169.             gc->releaseContext();
  170.         }
  171.     }
  172. }

  173. void MYGUIManager::updateEvents() const
  174. {
  175.     unsigned int size = _eventsToHandle.size();
  176.     for ( unsigned int i=0; i<size; ++i )
  177.     {
  178.         const osgGA::GUIEventAdapter& ea = *(_eventsToHandle.front());
  179.         int x = ea.getX(), y = ea.getY(), key = ea.getKey();
  180.         if ( ea.getMouseYOrientation()==osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS )
  181.             y = ea.getWindowHeight() - y;
  182.         
  183.         switch ( ea.getEventType() )
  184.         {
  185.         case osgGA::GUIEventAdapter::PUSH:
  186.             MyGUI::InputManager::getInstance().injectMousePress( x, y, convertMouseButton(ea.getButton()) );
  187.             break;
  188.         case osgGA::GUIEventAdapter::RELEASE:
  189.             MyGUI::InputManager::getInstance().injectMouseRelease( x, y, convertMouseButton(ea.getButton()) );
  190.             break;
  191.         case osgGA::GUIEventAdapter::DRAG:
  192.         case osgGA::GUIEventAdapter::MOVE:
  193.             MyGUI::InputManager::getInstance().injectMouseMove( x, y, 0 );
  194.             break;
  195.         case osgGA::GUIEventAdapter::KEYDOWN:
  196.             if ( key<127 )
  197.                 MyGUI::InputManager::getInstance().injectKeyPress( convertKeyCode(key), (char)key );
  198.             else
  199.                 MyGUI::InputManager::getInstance().injectKeyPress( convertKeyCode(key) );
  200.             break;
  201.         case osgGA::GUIEventAdapter::KEYUP:
  202.             MyGUI::InputManager::getInstance().injectKeyRelease( convertKeyCode(key) );
  203.             break;
  204.         case osgGA::GUIEventAdapter::RESIZE:
  205.             _platform->getRenderManagerPtr()->setViewSize( ea.getWindowWidth(), ea.getWindowHeight() );
  206.             break;
  207.         default:
  208.             break;
  209.         }
  210.         const_cast<MYGUIManager*>(this)->_eventsToHandle.pop();
  211.     }
  212. }

  213. void MYGUIManager::setupResources()
  214. {
  215.     MyGUI::xml::Document doc;
  216.     if ( !_platform || !doc.open(_resourcePathFile) ) doc.getLastError();
  217.    
  218.     MyGUI::xml::ElementPtr root = doc.getRoot();
  219.     if ( root==nullptr || root->getName()!="Paths" ) return;
  220.    
  221.     MyGUI::xml::ElementEnumerator node = root->getElementEnumerator();
  222.     while ( node.next() )
  223.     {
  224.         if ( node->getName()=="Path" )
  225.         {
  226.             bool root = false;
  227.             if ( node->findAttribute("root")!="" )
  228.             {
  229.                 root = MyGUI::utility::parseBool( node->findAttribute("root") );
  230.                 if ( root ) _rootMedia = node->getContent();
  231.             }
  232.             _platform->getDataManagerPtr()->addResourceLocation( node->getContent(), false );
  233.         }
  234.     }
  235.     _platform->getDataManagerPtr()->addResourceLocation( _rootMedia + "/Common/Base", false );
  236. }

  237. MyGUI::MouseButton MYGUIManager::convertMouseButton( int button ) const
  238. {
  239.     switch ( button )
  240.     {
  241.     case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
  242.         return MyGUI::MouseButton::Left;
  243.     case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
  244.         return MyGUI::MouseButton::Middle;
  245.     case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
  246.         return MyGUI::MouseButton::Right;
  247.     default: break;
  248.     }
  249.     return MyGUI::MouseButton::None;
  250. }

  251. MyGUI::KeyCode MYGUIManager::convertKeyCode( int key ) const
  252. {
  253.     static std::map<int, MyGUI::KeyCode> s_keyCodeMap;
  254.     if ( !s_keyCodeMap.size() )
  255.     {
  256.         #define ADD_CHAR_PAIR(c, k) s_keyCodeMap[c] = MyGUI::KeyCode::##k
  257.         #define ADD_KEY_PAIR(k) s_keyCodeMap[osgGA::GUIEventAdapter::KEY_##k] = MyGUI::KeyCode::##k
  258.         #define ADD_KEY_PAIR2(k1, k2) s_keyCodeMap[osgGA::GUIEventAdapter::KEY_##k1] = MyGUI::KeyCode::##k2
  259.         
  260.         ADD_CHAR_PAIR('1', One); ADD_CHAR_PAIR('2', Two); ADD_CHAR_PAIR('3', Three); ADD_CHAR_PAIR('4', Four);
  261.         ADD_CHAR_PAIR('5', Five); ADD_CHAR_PAIR('6', Six); ADD_CHAR_PAIR('7', Seven); ADD_CHAR_PAIR('8', Eight);
  262.         ADD_CHAR_PAIR('9', Nine); ADD_CHAR_PAIR('0', Zero);
  263.         ADD_CHAR_PAIR('a', A); ADD_CHAR_PAIR('b', B); ADD_CHAR_PAIR('c', C); ADD_CHAR_PAIR('d', D);
  264.         ADD_CHAR_PAIR('e', E); ADD_CHAR_PAIR('f', F); ADD_CHAR_PAIR('g', G); ADD_CHAR_PAIR('h', H);
  265.         ADD_CHAR_PAIR('i', I); ADD_CHAR_PAIR('j', J); ADD_CHAR_PAIR('k', K); ADD_CHAR_PAIR('l', L);
  266.         ADD_CHAR_PAIR('m', M); ADD_CHAR_PAIR('n', N); ADD_CHAR_PAIR('o', O); ADD_CHAR_PAIR('p', P);
  267.         ADD_CHAR_PAIR('q', Q); ADD_CHAR_PAIR('r', R); ADD_CHAR_PAIR('S', S); ADD_CHAR_PAIR('t', T);
  268.         ADD_CHAR_PAIR('u', U); ADD_CHAR_PAIR('v', V); ADD_CHAR_PAIR('w', W); ADD_CHAR_PAIR('x', X);
  269.         ADD_CHAR_PAIR('y', Y); ADD_CHAR_PAIR('z', Z);
  270.         
  271.         ADD_KEY_PAIR(F1); ADD_KEY_PAIR(F2); ADD_KEY_PAIR(F3); ADD_KEY_PAIR(F4); ADD_KEY_PAIR(F5);
  272.         ADD_KEY_PAIR(F6); ADD_KEY_PAIR(F7); ADD_KEY_PAIR(F8); ADD_KEY_PAIR(F9); ADD_KEY_PAIR(F10);
  273.         ADD_KEY_PAIR(Escape); ADD_KEY_PAIR(Tab); ADD_KEY_PAIR(Return); ADD_KEY_PAIR(Space);
  274.         ADD_KEY_PAIR(Minus); ADD_KEY_PAIR(Equals); ADD_KEY_PAIR(Backslash); ADD_KEY_PAIR(Slash);
  275.         ADD_KEY_PAIR(Semicolon); ADD_KEY_PAIR(Equals); ADD_KEY_PAIR(Comma); ADD_KEY_PAIR(Period);
  276.         ADD_KEY_PAIR(Insert); ADD_KEY_PAIR(Delete); ADD_KEY_PAIR(Home); ADD_KEY_PAIR(End);
  277.         
  278.         ADD_KEY_PAIR2(Num_Lock, NumLock); ADD_KEY_PAIR2(Scroll_Lock, ScrollLock); ADD_KEY_PAIR2(Caps_Lock, Capital);
  279.         ADD_KEY_PAIR2(BackSpace, Backspace); ADD_KEY_PAIR2(Page_Down, PageDown); ADD_KEY_PAIR2(Page_Up, PageUp);
  280.         ADD_KEY_PAIR2(Leftbracket, LeftBracket); ADD_KEY_PAIR2(Rightbracket, RightBracket); ADD_KEY_PAIR2(Quotedbl, Apostrophe);
  281.         ADD_KEY_PAIR2(Left, ArrowLeft); ADD_KEY_PAIR2(Right, ArrowRight);
  282.         ADD_KEY_PAIR2(Up, ArrowUp); ADD_KEY_PAIR2(Down, ArrowDown);
  283.         ADD_KEY_PAIR2(KP_1, Numpad1); ADD_KEY_PAIR2(KP_2, Numpad2); ADD_KEY_PAIR2(KP_3, Numpad3);
  284.         ADD_KEY_PAIR2(KP_4, Numpad4); ADD_KEY_PAIR2(KP_5, Numpad5); ADD_KEY_PAIR2(KP_6, Numpad6);
  285.         ADD_KEY_PAIR2(KP_7, Numpad7); ADD_KEY_PAIR2(KP_8, Numpad8); ADD_KEY_PAIR2(KP_9, Numpad9);
  286.         ADD_KEY_PAIR2(KP_0, Numpad0); ADD_KEY_PAIR2(KP_Enter, NumpadEnter);
  287.         ADD_KEY_PAIR2(Control_L, LeftControl); ADD_KEY_PAIR2(Control_R, RightControl);
  288.         ADD_KEY_PAIR2(Alt_L, LeftAlt); ADD_KEY_PAIR2(Alt_R, RightAlt);
  289.         ADD_KEY_PAIR2(Shift_L, LeftShift); ADD_KEY_PAIR2(Shift_R, RightShift);
  290.     }
  291.    
  292.     std::map<int, MyGUI::KeyCode>::iterator itr = s_keyCodeMap.find(key);
  293.     if ( itr!=s_keyCodeMap.end() ) return itr->second;
  294.     return MyGUI::KeyCode::None;
  295. }
复制代码

该用户从未签到

 楼主| 发表于 2013-11-25 09:39:10 | 显示全部楼层
  1. #include <osg/Texture2D>
  2. #include <osg/Geometry>
  3. #include <osg/MatrixTransform>
  4. #include <osgDB/ReadFile>
  5. #include <osgGA/StateSetManipulator>
  6. #include <osgGA/TrackballManipulator>
  7. #include <osgViewer/ViewerEventHandlers>
  8. #include <osgViewer/Viewer>

  9. #include "MYGUIManager.h"

  10. // This class is modified from the Demo_Themes example of MyGUI
  11. class CustomMYGUIManager : public MYGUIManager
  12. {
  13. protected:
  14.     virtual void setupResources()
  15.     {
  16.         MYGUIManager::setupResources();
  17.         _platform->getDataManagerPtr()->addResourceLocation( _rootMedia + "/Demos/Demo_Themes", false );
  18.         _platform->getDataManagerPtr()->addResourceLocation( _rootMedia + "/Common/Demos", false );
  19.         _platform->getDataManagerPtr()->addResourceLocation( _rootMedia + "/Common/Themes", false );
  20.     }
  21.    
  22.     virtual void initializeControls()
  23.     {
  24.         MyGUI::LayoutManager::getInstance().loadLayout("Wallpaper.layout");
  25.         const MyGUI::VectorWidgetPtr& root = MyGUI::LayoutManager::getInstance().loadLayout("HelpPanel.layout");
  26.         if ( root.size()==1 )
  27.         {
  28.             root.at(0)->findWidget("Text")->castType<MyGUI::TextBox>()->setCaption(
  29.                 "Select skin theme in combobox to see default MyGUI themes.");
  30.         }
  31.         createDemo( 0 );
  32.     }
  33.    
  34.     void notifyComboAccept( MyGUI::ComboBox* sender, size_t index )
  35.     {
  36.         createDemo( index );
  37.     }
  38.    
  39.     void createDemo( int index )
  40.     {
  41.         destroyDemo();
  42.         switch ( index )
  43.         {
  44.         case 0:
  45.             MyGUI::ResourceManager::getInstance().load("MyGUI_BlueWhiteTheme.xml");
  46.             break;
  47.         case 1:
  48.             MyGUI::ResourceManager::getInstance().load("MyGUI_BlackBlueTheme.xml");
  49.             break;
  50.         case 2:
  51.             MyGUI::ResourceManager::getInstance().load("MyGUI_BlackOrangeTheme.xml");
  52.             break;
  53.         default: break;
  54.         }
  55.         
  56.         MyGUI::VectorWidgetPtr windows = MyGUI::LayoutManager::getInstance().loadLayout("Themes.layout");
  57.         if ( windows.size()<1 )
  58.         {
  59.             OSG_WARN << "Error load layout" << std::endl;
  60.             return;
  61.         }
  62.         
  63.         _demoView = windows[0];
  64.         _comboSkins = MyGUI::Gui::getInstance().findWidget<MyGUI::ComboBox>("Combo");
  65.         if ( _comboSkins )
  66.         {
  67.             _comboSkins->setComboModeDrop( true );
  68.             _comboSkins->addItem( "blue & white" );
  69.             _comboSkins->addItem( "black & blue" );
  70.             _comboSkins->addItem( "black & orange" );
  71.             _comboSkins->setIndexSelected( index );
  72.             _comboSkins->eventComboAccept += MyGUI::newDelegate(this, &CustomMYGUIManager::notifyComboAccept);
  73.         }
  74.     }
  75.    
  76.     void destroyDemo()
  77.     {
  78.         if ( _demoView )
  79.             MyGUI::WidgetManager::getInstance().destroyWidget( _demoView );
  80.         _demoView = NULL;
  81.         _comboSkins = NULL;
  82.     }
  83.    
  84.     MyGUI::Widget* _demoView;
  85.     MyGUI::ComboBox* _comboSkins;
  86. };

  87. int main( int argc, char** argv )
  88. {
  89.     osg::ref_ptr<CustomMYGUIManager> mygui = new CustomMYGUIManager;
  90.    
  91.     osg::ref_ptr<osg::Geode> geode = new osg::Geode;
  92.     geode->setCullingActive( false );
  93.     geode->addDrawable( mygui.get() );
  94.     geode->getOrCreateStateSet()->setMode( GL_BLEND, osg::StateAttribute::ON );
  95.     geode->getOrCreateStateSet()->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
  96.    
  97.     osg::ref_ptr<osg::Camera> camera = new osg::Camera;
  98.     camera->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
  99.     camera->setRenderOrder( osg::Camera::POST_RENDER );
  100.     camera->setAllowEventFocus( false );
  101.     camera->setProjectionMatrix( osg::Matrix::ortho2D(0.0, 1.0, 0.0, 1.0) );
  102.     camera->addChild( geode.get() );
  103.    
  104.     osg::ref_ptr<osg::Group> root = new osg::Group;
  105.     //root->addChild( osgDB::readNodeFile("cow.osg") );
  106.     root->addChild( camera.get() );
  107.    
  108.     osgViewer::Viewer viewer;
  109.     viewer.setSceneData( root.get() );
  110.     viewer.addEventHandler( new MYGUIHandler(camera.get(), mygui.get()) );
  111.     viewer.addEventHandler( new osgViewer::WindowSizeHandler );
  112.     viewer.addEventHandler( new osgViewer::StatsHandler );
  113.     viewer.realize();
  114.    
  115.     osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>( viewer.getCamera()->getGraphicsContext() );
  116.     if ( gw )
  117.     {
  118.         // Send window size for MyGUI to initialize
  119.         int x, y, w, h; gw->getWindowRectangle( x, y, w, h );
  120.         viewer.getEventQueue()->windowResize( x, y, w, h );
  121.     }
  122.     return viewer.run();
  123. }
复制代码

该用户从未签到

 楼主| 发表于 2013-11-25 09:40:21 | 显示全部楼层
QQ截图20131125093912.png

该用户从未签到

 楼主| 发表于 2013-11-25 09:41:18 | 显示全部楼层
所有相关代码已粘贴完毕。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表