天天看点

OSG+CEGUI程序框架OpenSceneGraph-2.8.2+CEGUI-0.6.2-vc9

http://blog.csdn.net/pizi0475

痞子龙3D编程

CEGuiDrawable.h

#pragma once

#include <osg/Drawable>

class CEGuiDrawable :

 public osg::Drawable

{

public:

 CEGuiDrawable();

 CEGuiDrawable(const CEGuiDrawable& drawable,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):

 Drawable(drawable,copyop) {}

 META_Object(osg,CEGuiDrawable);

 //初始话CEGUI,添加所需要的资源以及设定相关的属性

 void InitCEGUI();

 //这个函数在基类Drawable里是一个纯虚函数,实现OpenGL渲染时必须重写

 void drawImplementation(osg::RenderInfo& renderInfo) const;

protected:

 //析构函数。在结束时释放资源

 virtual ~CEGuiDrawable();

 unsigned int m_activeContextID;

};

CEGuiDrawable.cpp

#include "StdAfx.h"

#include "CEGuiDrawable.h"

#include "CEGuiEventCallback.h"

CEGuiDrawable::CEGuiDrawable()

{

 setSupportsDisplayList(false);

 setEventCallback(new CEGuiEventCallback());

    new CEGUI::System( new CEGUI::OpenGLRenderer(0));

 m_activeContextID = 0;

}

void CEGuiDrawable::drawImplementation(osg::RenderInfo& renderInfo) const

{

 osg::State& state = *renderInfo.getState();

 if (state.getContextID()!=m_activeContextID) return;

 glPushAttrib(GL_ALL_ATTRIB_BITS);

 state.disableAllVertexArrays();

 //在每帧渲染后调用

 CEGUI::System::getSingleton().renderGUI();

 glPopAttrib();

 state.checkGLErrors("CEGuiDrawable::drawImplementation");

}

void CEGuiDrawable::InitCEGUI()

{

 using namespace CEGUI;

 // initialise the required dirs for the DefaultResourceProvider

 DefaultResourceProvider* rp = static_cast<DefaultResourceProvider*>

  (System::getSingleton().getResourceProvider());

 //注意:这里要修改为下载的CEGUI界面文件对应的目录

 rp->setResourceGroupDirectory("schemes", "datafiles/schemes/");

 rp->setResourceGroupDirectory("imagesets", "datafiles/imagesets/");

 rp->setResourceGroupDirectory("fonts", "datafiles/fonts/");

 rp->setResourceGroupDirectory("layouts", "datafiles/layouts/");

 rp->setResourceGroupDirectory("looknfeels", "datafiles/looknfeel/");

 rp->setResourceGroupDirectory("lua_scripts", "datafiles/lua_scripts/");

 // set the default resource groups to be used

 Imageset::setDefaultResourceGroup("imagesets");

 Font::setDefaultResourceGroup("fonts");

 Scheme::setDefaultResourceGroup("schemes");

 WidgetLookManager::setDefaultResourceGroup("looknfeels");

 WindowManager::setDefaultResourceGroup("layouts");

 ScriptModule::setDefaultResourceGroup("lua_scripts");

 // we will make use of the WindowManager.

 WindowManager& winMgr = WindowManager::getSingleton();

 // load scheme and set up defaults

 SchemeManager::getSingleton().loadScheme("TaharezLook.scheme");

 System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");

 if(!FontManager::getSingleton().isFontPresent("Commonwealth-10"))

  FontManager::getSingleton().createFont("Commonwealth-10.font");

 // create root window

 DefaultWindow* root = (DefaultWindow*)winMgr.createWindow("DefaultWindow", "Root");

 // set root as the active GUI sheet

 System::getSingleton().setGUISheet(root);

 // create a FrameWindow for the test

 FrameWindow* wnd = (FrameWindow*)winMgr.createWindow("TaharezLook/FrameWindow", "TestWindow");

 // add the frame window to the root

 root->addChildWindow(wnd);

 // set window initial position and size

 wnd->setPosition(UVector2(cegui_reldim(0.25f), cegui_reldim( 0.25f)));

 wnd->setSize(UVector2(cegui_reldim(0.5f), cegui_reldim( 0.5f)));

 // set window text

 wnd->setText("UIAE Demo Window");

}

CEGuiDrawable::~CEGuiDrawable()

{

 // delete CEGUI??

}

CEGuiEventCallback.h

#pragma once

class CEGuiEventCallback :

 public osgGA::GUIEventHandler

{

public:

 CEGuiEventCallback(void);

 ~CEGuiEventCallback(void);

 virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object* obj, osg::NodeVisitor* nv);

};

CEGuiEventCallback.cpp

#include "StdAfx.h"

#include "CEGuiEventCallback.h"

#include "CEGuiDrawable.h"

CEGuiEventCallback::CEGuiEventCallback(void)

{

}

CEGuiEventCallback::~CEGuiEventCallback(void)

{

}

bool CEGuiEventCallback::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object* obj, osg::NodeVisitor* nv)

{

 osgGA::EventVisitor* ev = dynamic_cast<osgGA::EventVisitor*>(nv);

 CEGuiDrawable* cd = dynamic_cast<CEGuiDrawable*>(obj);

 if (!ev || !cd) return false;

 switch(ea.getEventType())

 {

 case(osgGA::GUIEventAdapter::DRAG):

 case(osgGA::GUIEventAdapter::MOVE):

  CEGUI::System::getSingleton().injectMousePosition(ea.getX(),ea.getY());

  return true;

 case(osgGA::GUIEventAdapter::PUSH):

  {

   CEGUI::System::getSingleton().injectMousePosition(ea.getX(), ea.getY());

   if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)  // left

    CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton);

   else if (ea.getButton() == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON)  // middle

    CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);

   else if (ea.getButton() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)  // right

    CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton);

   return true;

  }

 case(osgGA::GUIEventAdapter::RELEASE):

  {

   CEGUI::System::getSingleton().injectMousePosition(ea.getX(), ea.getY());

   if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)  // left

    CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::LeftButton);

   else if (ea.getButton() == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON)  // middle

    CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::MiddleButton);

   else if (ea.getButton() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)  // right

    CEGUI::System::getSingleton().injectMouseButtonUp(CEGUI::RightButton);

   return true;

  }

 case(osgGA::GUIEventAdapter::DOUBLECLICK):

  {

   // do we need to do something special here to handle double click???  Will just assume button down for now.

   CEGUI::System::getSingleton().injectMousePosition(ea.getX(), ea.getY());

   if (ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)  // left

    CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::LeftButton);

   else if (ea.getButton() == osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON)  // middle

    CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::MiddleButton);

   else if (ea.getButton() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)  // right

    CEGUI::System::getSingleton().injectMouseButtonDown(CEGUI::RightButton);

   return true;

  }

 case(osgGA::GUIEventAdapter::KEYDOWN):

  CEGUI::System::getSingleton().injectKeyDown( static_cast<CEGUI::uint>(ea.getKey()) );

  CEGUI::System::getSingleton().injectChar( static_cast<CEGUI::utf32>( ea.getKey() ) );

  return true;

 case(osgGA::GUIEventAdapter::KEYUP):

  CEGUI::System::getSingleton().injectKeyUp( static_cast<CEGUI::uint>(ea.getKey()) );

  return true;

 default:

  break;

 }

 return false;

}

main.CPP

// OsgCeguiFirst.cpp : 定义控制台应用程序的入口点。

//

#include <osgDB/ReadFile>

#include <osgUtil/Optimizer>

#include <osgViewer/Viewer>

#include <osg/CoordinateSystemNode>

#include <osgGA/GUIEventAdapter>

#include <CEGUI.h>

#include <CEGUISystem.h>

#include <RendererModules/OpenGLGUIRenderer/openglrenderer.h>

#include <CEGUIScriptModule.h>

#include <CEGUIFontManager.h>

#include <CEGUISchemeManager.h>

#include <CEGUIWindowManager.h>

#include <CEGUIExceptions.h>

#include <CEGUIImagesetManager.h>

#include <CEGUIFontManager.h>

#include <CEGUIDefaultResourceProvider.h>

#include "stdafx.h"

#include "CEGuiDrawable.h"

using namespace osg;

using namespace osgDB;

using namespace osgViewer;

int _tmain(int argc, _TCHAR* argv[])

{

 //创建OSG场景的根结点

 osg::ref_ptr<osg::Group> rootNode = new osg::Group;

 osgViewer::Viewer viewer;

 //此句比较关键。因为CEGUI只支持单线程,所以必须在构建CEGuiDrawable类的对象之前调用此句,不然就

 //会出现OpenGL资源正在使用中,无法获取的问题。

 viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);

 //下面两句代码也很关键。其作用是初始化OpenGL环境,如果没有这两句运行后界面将无法显示

 viewer.realize();

 viewer.getCamera()->getGraphicsContext()->makeCurrent();

 osg::Geode *ceguiGeode = new osg::Geode;

 CEGuiDrawable *cd = new CEGuiDrawable();

 cd->InitCEGUI();

 ceguiGeode->addDrawable(cd);

 osg::Node *model = osgDB::readNodeFile("glider.osg");

 rootNode->addChild(ceguiGeode);

 rootNode->addChild(model);

 viewer.setSceneData(rootNode.get());

 viewer.run();

}

继续阅读