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

30 篇文章 1 订阅

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();
}

OMNeT++本身是一个基于离散事件仿真的网络仿真框架,它并没有直接支持3D模拟的功能。但是,你可以结合其他工具和库来扩展OMNeT++,实现3D模拟的需求。 OpenSceneGraph是一个强大的开源3D图形引擎,osgEarth是基于OpenSceneGraph的地理信息系统(GIS)扩展库。你可以使用OpenSceneGraphosgEarth来创建和渲染3D场景,并将其与OMNeT++的仿真结果进行集成。 下面是一种可能的集成方式: 1. 在OMNeT++中,你可以定义网络拓扑和节点模型,并实现网络通信的逻辑。你可以使用OMNeT++提供的模块和消息传递机制来建立网络模型。 2. 在仿真过程中,收集网络节点的状态和通信数据。你可以将这些数据传递给一个外部的控制程序。 3. 使用控制程序,将收集到的数据转换为OpenSceneGraph可接受的格式。这可能涉及将数据映射到3D场景中的节点、位置、属性等。 4. 利用OpenSceneGraphosgEarth创建一个3D场景,并将节点位置、状态等信息可视化。你可以使用OpenSceneGraph提供的渲染引擎来呈现场景,同时利用osgEarth来加载地理数据和纹理等。 5. 在仿真过程中,通过控制程序不断更新3D场景的状态和视图,以反映网络仿真的进展和节点状态的变化。 通过这种方式,你可以将OMNeT++的仿真结果与OpenSceneGraphosgEarth的3D可视化能力相结合,实现网络仿真的3D模拟效果。 需要注意的是,这种集成方式需要一定的编程和开发能力,并且需要对OMNeT++、OpenSceneGraphosgEarth有一定的了解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值