OSG+CEGUI+lua 程序框架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"
#include <ScriptingModules/CEGUILua/LuaScriptModule/include/CEGUILua.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;
 // 初始化资源目录
 DefaultResourceProvider* rp = static_cast<DefaultResourceProvider*>
  (System::getSingleton().getResourceProvider());

 rp->setResourceGroupDirectory("schemes", "ceguiDataFiles/schemes/");
 rp->setResourceGroupDirectory("imagesets", "ceguiDataFiles/imagesets/");
 rp->setResourceGroupDirectory("fonts", "ceguiDataFiles/fonts/");
 rp->setResourceGroupDirectory("layouts", "ceguiDataFiles/layouts/");
 rp->setResourceGroupDirectory("looknfeels", "ceguiDataFiles/looknfeel/");
 rp->setResourceGroupDirectory("lua_scripts", "ceguiDataFiles/lua_scripts/");

 // 设置资源组
 Imageset::setDefaultResourceGroup("imagesets");
 Font::setDefaultResourceGroup("fonts");
 Scheme::setDefaultResourceGroup("schemes");
 WidgetLookManager::setDefaultResourceGroup("looknfeels");
 WindowManager::setDefaultResourceGroup("layouts");
 ScriptModule::setDefaultResourceGroup("lua_scripts");

 // 加载Lua脚本
 LuaScriptModule* script = new LuaScriptModule();
 System::getSingleton().setScriptingModule(script);
 System::getSingleton().executeScriptFile("GuiScript.lua");
}

CEGuiDrawable::~CEGuiDrawable()
{
 delete CEGUI::System::getSingleton().getScriptingModule();
}

 

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.getWindowHeight()- ea.getY());
  return true;
 case(osgGA::GUIEventAdapter::PUSH):
  {
   CEGUI::System::getSingleton().injectMousePosition(ea.getX(), ea.getWindowHeight()- 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.getWindowHeight()- 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):
  {
   CEGUI::System::getSingleton().injectMousePosition(ea.getX(), ea.getWindowHeight()- 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

#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>

 

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

#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只支持单线程
 viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);

 // 初始化OpenGL环境
 viewer.realize();
 viewer.getCamera()->getGraphicsContext()->makeCurrent();

 // 初始化CEGUI
 osg::ref_ptr<osg::Geode> ceguiGeode = new osg::Geode;
 osg::ref_ptr<CEGuiDrawable> ceguiDraw = new CEGuiDrawable();
 ceguiDraw->InitCEGUI();
 ceguiGeode->addDrawable(ceguiDraw);

 // 加载模型
 osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("osgDataFiles//cow.osg");
 rootNode->addChild(model);
 rootNode->addChild(ceguiGeode);
   
 // 设置数据并渲染
 viewer.setSceneData(rootNode.get());
 viewer.run();
}

 

GuiScript.lua

 

-----------------------------------------
-- Start of handler functions
-----------------------------------------

function luabtnOK_clicked(e)  
local we = CEGUI.toWindowEventArgs(e)  
we.window:setText("OK clicked");  
end

function luabtnCancel_clicked(e)  
local we = CEGUI.toWindowEventArgs(e)  
we.window:setText("Cancel clicked");  
end

-----------------------------------------
-- Script Entry Point
-----------------------------------------
local guiSystem = CEGUI.System:getSingleton()
local schemeMgr = CEGUI.SchemeManager:getSingleton()
local winMgr = CEGUI.WindowManager:getSingleton()

-- load our demo8 scheme
schemeMgr:loadScheme("TaharezLook.scheme");
-- load our demo8 window layout
local root = winMgr:loadWindowLayout("Cegui.layout")
-- set the layout as the root
guiSystem:setGUISheet(root)
-- set default mouse cursor
guiSystem:setDefaultMouseCursor("TaharezLook", "MouseArrow")
-- set the Tooltip type
guiSystem:setDefaultTooltip("TaharezLook/Tooltip")

 

Cegui.layout

 

<?xml version="1.0" encoding="UTF-8"?>

<GUILayout >
    <Window Type="DefaultWindow" Name="Root" >
        <Property Name="InheritsAlpha" Value="False" />
        <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
        <Property Name="UnifiedAreaRect" Value="{{0,0},{0,0},{1,0},{1,0}}" />
        <Window Type="TaharezLook/FrameWindow" Name="Hello" >
            <Property Name="Text" Value="Hello World!" />
            <Property Name="Alpha" Value="0.75" />
            <Property Name="TitlebarFont" Value="Commonwealth-10" />
            <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
            <Property Name="TitlebarEnabled" Value="True" />
            <Property Name="UnifiedAreaRect" Value="{{0.279703,0},{0.275744,0},{0.656124,0},{0.608014,0}}" />
            <Window Type="TaharezLook/StaticText" Name="Hello/StaticText" >
                <Property Name="Font" Value="Commonwealth-10" />
                <Property Name="Text" Value="Welcome to OSG + CEGUI" />
                <Property Name="HorzFormatting" Value="WordWrapLeftAligned" />
                <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
                <Property Name="VertFormatting" Value="TopAligned" />
                <Property Name="UnifiedAreaRect" Value="{{0.043543,0},{0.217033,0},{0.967456,0},{0.679647,0}}" />
            </Window>
            <Window Type="TaharezLook/Button" Name="Hello/Ok" >
                <Property Name="Text" Value="OK" />
                <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
                <Property Name="UnifiedAreaRect" Value="{{0.19414,0},{0.750594,0},{0.432419,0},{0.897941,0}}" />
                <Event Name="Clicked" Function="luabtnOK_clicked" />
            </Window>
            <Window Type="TaharezLook/Button" Name="Hello/Cancel" >
                <Property Name="Font" Value="Commonwealth-10" />
                <Property Name="Text" Value="Cancel" />
                <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
                <Property Name="UnifiedAreaRect" Value="{{0.52738,0},{0.753934,0},{0.76566,0},{0.901281,0}}" />
                <Event Name="Clicked" Function="luabtnCancel_clicked" />
            </Window>
        </Window>
    </Window>
</GUILayout>

你可能感兴趣的:(OSG+CEGUI+lua 程序框架OpenSceneGraph-2.8.2+CEGUI-0.6.2-vc9)