原先打算把CEGUI用到MFC Activex中,然后做成WEB3D,但是不成功,在MFC应用程序中没有问题,在IE就出错,这个是我写的一个简单的例子,只是实现了嵌入,其他功能比如鼠标键盘事件都没有处理,整合几个例子而成,目的在于测试,感兴趣的自己可以研究一下
BaseApplication.h

/**//*
-----------------------------------------------------------------------------
Filename: BaseApplication.h
-----------------------------------------------------------------------------
This source file is generated by the Ogre AppWizard.
Check out: http://conglomerate.berlios.de/wiki/doku.php?id=ogrewizards
Based on the Example Framework for OGRE
(Object-oriented Graphics Rendering Engine)
Copyright (c) 2000-2007 The OGRE Team
For the latest info, see http://www.ogre3d.org/
You may use this sample code for anything you like, it is not covered by the
LGPL like the rest of the OGRE engine.
-----------------------------------------------------------------------------
*/
#ifndef __BaseApplication_h_
#define __BaseApplication_h_
#include <ogre.h>
#include <OgreStringConverter.h>
#include <OgreException.h>
//Use this define to signify OIS will be used as a DLL
//(so that dll import/export macros are in effect)
#define OIS_DYNAMIC_LIB
#include <OIS/OIS.h>
using namespace Ogre;
#include <CEGUI.h>
#include <CEGUISystem.h>
#include <CEGUISchemeManager.h>
#include <OgreCEGUIRenderer.h>
class BaseApplication : public Ogre::Singleton<BaseApplication>, public FrameListener, public WindowEventListener, public OIS::KeyListener, public OIS::MouseListener

{
public:
BaseApplication(void);
virtual ~BaseApplication(void);
virtual void go(void);
void SetWnd(HWND w);
protected:
virtual bool setup();
virtual bool configure(void);
virtual void chooseSceneManager(void);
virtual void createCamera(void);
virtual void createFrameListener(void);
virtual void createScene(void) = 0; // Override me!
virtual void destroyScene(void);
virtual void createViewports(void);
virtual void setupResources(void);
virtual void createResourceListener(void);
virtual void loadResources(void);
virtual void updateStats(void);
virtual bool processUnbufferedKeyInput(const FrameEvent& evt);
virtual bool processUnbufferedMouseInput(const FrameEvent& evt);
virtual void moveCamera();
virtual bool frameStarted(const FrameEvent& evt);
virtual bool frameEnded(const FrameEvent& evt);
void showDebugOverlay(bool show);
void switchMouseMode();
void switchKeyMode();
bool keyPressed( const OIS::KeyEvent &arg );
bool keyReleased( const OIS::KeyEvent &arg );
bool mouseMoved( const OIS::MouseEvent &arg );
bool mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id );
bool mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id );
//Adjust mouse clipping area
virtual void windowResized(RenderWindow* rw);
//Unattach OIS before window shutdown (very important under Linux)
virtual void windowClosed(RenderWindow* rw);
void requestShutdown(void);
void setupEventHandlers(void);
bool handleQuit(const CEGUI::EventArgs& e);
Root *mRoot;
Camera* mCamera;
SceneManager* mSceneMgr;
RenderWindow* mWindow;
CEGUI::System* mGUISystem;
CEGUI::Renderer* mGUIRenderer;
bool mShutdownRequested;
int mSceneDetailIndex ;
Real mMoveSpeed;
Degree mRotateSpeed;
Overlay* mDebugOverlay;
std::string mDebugText;
//OIS Input devices
OIS::InputManager* mInputManager;
OIS::Mouse* mMouse;
OIS::Keyboard* mKeyboard;
Vector3 mTranslateVector;
bool mStatsOn;
bool mUseBufferedInputKeys, mUseBufferedInputMouse, mInputTypeSwitchingOn;
unsigned int mNumScreenShots;
float mMoveScale;
Degree mRotScale;
Real mTimeUntilNextToggle; // just to stop toggles flipping too fast
Radian mRotX, mRotY;
TextureFilterOptions mFiltering;
int mAniso;
HWND wnd;
};
#endif // #ifndef __BaseApplication_h_

/**//*
-----------------------------------------------------------------------------
Filename: BaseApplication.cpp
-----------------------------------------------------------------------------
This source file is generated by the Ogre AppWizard.
Check out: http://conglomerate.berlios.de/wiki/doku.php?id=ogrewizards
Based on the Example Framework for OGRE
(Object-oriented Graphics Rendering Engine)
Copyright (c) 2000-2007 The OGRE Team
For the latest info, see http://www.ogre3d.org/
You may use this sample code for anything you like, it is not covered by the
LGPL like the rest of the OGRE engine.
-----------------------------------------------------------------------------
*/
#include "stdafx.h"
#include "BaseApplication.h"


template<> BaseApplication* Ogre::Singleton<BaseApplication>::ms_Singleton = 0;
CEGUI::MouseButton convertOISMouseButtonToCegui(int buttonID)

{
switch (buttonID)
{
case 0: return CEGUI::LeftButton;
case 1: return CEGUI::RightButton;
case 2: return CEGUI::MiddleButton;
case 3: return CEGUI::X1Button;
default: return CEGUI::LeftButton;
}
}
//-------------------------------------------------------------------------------------
BaseApplication::BaseApplication(void)
: mRoot(0),
mCamera(0),
mSceneMgr(0),
mWindow(0),
mGUISystem(0),
mGUIRenderer(0),
mShutdownRequested(false),
mSceneDetailIndex(0),
mMoveSpeed(100),
mRotateSpeed(36),
mDebugOverlay(0),
mDebugText(""),
mInputManager(0),
mMouse(0),
mKeyboard(0),
mTranslateVector(Vector3::ZERO),
mStatsOn(true),
mUseBufferedInputKeys(false),
mUseBufferedInputMouse(false),
mInputTypeSwitchingOn(false),
mNumScreenShots(0),
mMoveScale(0.0f),
mRotScale(0.0f),
mTimeUntilNextToggle(0),
mRotX(0),
mRotY(0),
mFiltering(TFO_BILINEAR),
mAniso(1)

{
}
//-------------------------------------------------------------------------------------
BaseApplication::~BaseApplication(void)

{
//Remove ourself as a Window listener
WindowEventUtilities::removeWindowEventListener(mWindow, this);
windowClosed(mWindow);
if(mGUISystem)
{
delete mGUISystem;
mGUISystem = 0;
}
if(mGUIRenderer)
{
delete mGUIRenderer;
mGUIRenderer = 0;
}
delete mRoot;
}
void BaseApplication::SetWnd(HWND w)

{
wnd =w;
}
//-------------------------------------------------------------------------------------
bool BaseApplication::configure(void)

{
// Show the configuration dialog and initialise the system
// You can skip this and use root.restoreConfig() to load configuration
// settings if you were sure there are valid ones saved in ogre.cfg
if(mRoot->showConfigDialog())
{
// If returned true, user clicked OK so initialise
// Here we choose to let the system create a default rendering window by passing 'true'
mWindow = mRoot->initialise(false);
NameValuePairList parms;
parms["externalWindowHandle"] = StringConverter::toString((long)this->wnd);
mWindow = mRoot->createRenderWindow("MFC", 300, 300, false, &parms);
// Let's add a nice window icon
return true;
}
else
{
return false;
}
}
//-------------------------------------------------------------------------------------
void BaseApplication::chooseSceneManager(void)

{
// Get the SceneManager, in this case a generic one
mSceneMgr = mRoot->createSceneManager(ST_GENERIC);
}
//-------------------------------------------------------------------------------------
void BaseApplication::createCamera(void)

{
// Create the camera
mCamera = mSceneMgr->createCamera("PlayerCam");
// Position it at 500 in Z direction
mCamera->setPosition(Vector3(0,0,80));
// Look back along -Z
mCamera->lookAt(Vector3(0,0,-300));
mCamera->setNearClipDistance(5);
}
//-------------------------------------------------------------------------------------
void BaseApplication::createFrameListener(void)

{
mDebugOverlay = OverlayManager::getSingleton().getByName("Core/DebugOverlay");
mUseBufferedInputKeys = false;
mUseBufferedInputMouse = true;
mInputTypeSwitchingOn = mUseBufferedInputKeys || mUseBufferedInputMouse;
mRotateSpeed = 36;
mMoveSpeed = 100;
using namespace OIS;
LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
//ParamList pl;
//size_t windowHnd = 0;
//std::ostringstream windowHndStr;
//mWindow->getCustomAttribute("WINDOW", &windowHnd);
//windowHndStr << windowHnd;
//pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
//mInputManager = InputManager::createInputSystem( pl );
//mKeyboard = static_cast<Keyboard*>(mInputManager->createInputObject( OISKeyboard, mUseBufferedInputKeys ));
//mMouse = static_cast<Mouse*>(mInputManager->createInputObject( OISMouse, mUseBufferedInputMouse ));
//mMouse->setEventCallback(this);
//mKeyboard->setEventCallback(this);
mShutdownRequested = false;
//Set initial mouse clipping size
windowResized(mWindow);
//Register as a Window listener
WindowEventUtilities::addWindowEventListener(mWindow, this);
mStatsOn = true;
mNumScreenShots = 0;
mTimeUntilNextToggle = 0;
mSceneDetailIndex = 0;
mMoveScale = 0.0f;
mRotScale = 0.0f;
mTranslateVector = Vector3::ZERO;
mAniso = 1;
mFiltering = TFO_BILINEAR;
showDebugOverlay(true);
mRoot->addFrameListener(this);
}
//-------------------------------------------------------------------------------------
void BaseApplication::destroyScene(void)

{
}
//-------------------------------------------------------------------------------------
void BaseApplication::createViewports(void)

{
// Create one viewport, entire window
Viewport* vp = mWindow->addViewport(mCamera);
vp->setBackgroundColour(ColourValue(0,0,0));
// Alter the camera aspect ratio to match the viewport
mCamera->setAspectRatio(
Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
}
//-------------------------------------------------------------------------------------
void BaseApplication::setupResources(void)

{
// Load resource paths from config file
ConfigFile cf;
cf.load("resources.cfg");
// Go through all sections & settings in the file
ConfigFile::SectionIterator seci = cf.getSectionIterator();
String secName, typeName, archName;
while (seci.hasMoreElements())
{
secName = seci.peekNextKey();
ConfigFile::SettingsMultiMap *settings = seci.getNext();
ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); ++i)
{
typeName = i->first;
archName = i->second;
ResourceGroupManager::getSingleton().addResourceLocation(
archName, typeName, secName);
}
}
}
//-------------------------------------------------------------------------------------
void BaseApplication::createResourceListener(void)

{
}
//-------------------------------------------------------------------------------------
void BaseApplication::loadResources(void)

{
ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}
//-------------------------------------------------------------------------------------
void BaseApplication::go(void)

{
if (!setup())
return;
//showDebugOverlay(true);
//mRoot->startRendering();
mWindow->setActive(true);
mWindow->update();
// clean up
//destroyScene();
}
//-------------------------------------------------------------------------------------
bool BaseApplication::setup(void)

{
mRoot = new Root();
setupResources();
bool carryOn = configure();
if (!carryOn) return false;
chooseSceneManager();
createCamera();
createViewports();
// Set default mipmap level (NB some APIs ignore this)
TextureManager::getSingleton().setDefaultNumMipmaps(5);
// Create any resource listeners (for loading screens)
createResourceListener();
// Load resources
loadResources();
// Create the scene
createScene();
createFrameListener();
return true;
};
//-------------------------------------------------------------------------------------
void BaseApplication::updateStats(void)

{
static String currFps = "Current FPS: ";
static String avgFps = "Average FPS: ";
static String bestFps = "Best FPS: ";
static String worstFps = "Worst FPS: ";
static String tris = "Triangle Count: ";
// update stats when necessary
try
{
OverlayElement* guiAvg = OverlayManager::getSingleton().getOverlayElement("Core/AverageFps");
OverlayElement* guiCurr = OverlayManager::getSingleton().getOverlayElement("Core/CurrFps");
OverlayElement* guiBest = OverlayManager::getSingleton().getOverlayElement("Core/BestFps");
OverlayElement* guiWorst = OverlayManager::getSingleton().getOverlayElement("Core/WorstFps");
const RenderTarget::FrameStats& stats = mWindow->getStatistics();
guiAvg->setCaption(avgFps + StringConverter::toString(stats.avgFPS));
guiCurr->setCaption(currFps + StringConverter::toString(stats.lastFPS));
guiBest->setCaption(bestFps + StringConverter::toString(stats.bestFPS)
+" "+StringConverter::toString(stats.bestFrameTime)+" ms");
guiWorst->setCaption(worstFps + StringConverter::toString(stats.worstFPS)
+" "+StringConverter::toString(stats.worstFrameTime)+" ms");
OverlayElement* guiTris = OverlayManager::getSingleton().getOverlayElement("Core/NumTris");
guiTris->setCaption(tris + StringConverter::toString(stats.triangleCount));
OverlayElement* guiDbg = OverlayManager::getSingleton().getOverlayElement("Core/DebugText");
guiDbg->setCaption(mDebugText);
}
catch(
)
{
// ignore
}
}
//-------------------------------------------------------------------------------------
bool BaseApplication::processUnbufferedKeyInput(const FrameEvent& evt)

{
using namespace OIS;
OIS::Keyboard* mInputDevice = mKeyboard; // Nice hack, eh? :)
if (mInputDevice->isKeyDown(KC_A))
{
// Move camera left
mTranslateVector.x = -mMoveScale;
}
if (mInputDevice->isKeyDown(KC_D))
{
// Move camera RIGHT
mTranslateVector.x = mMoveScale;
}

/**//* Move camera forward by keypress. */
if (mInputDevice->isKeyDown(KC_UP) || mInputDevice->isKeyDown(KC_W) )
{
mTranslateVector.z = -mMoveScale;
}

/**//* Move camera backward by keypress. */
if (mInputDevice->isKeyDown(KC_DOWN) || mInputDevice->isKeyDown(KC_S) )
{
mTranslateVector.z = mMoveScale;
}
if (mInputDevice->isKeyDown(KC_PGUP))
{
// Move camera up
mTranslateVector.y = mMoveScale;
}
if (mInputDevice->isKeyDown(KC_PGDOWN))
{
// Move camera down
mTranslateVector.y = -mMoveScale;
}
if (mInputDevice->isKeyDown(KC_RIGHT))
{
mCamera->yaw(-mRotScale);
}
if (mInputDevice->isKeyDown(KC_LEFT))
{
mCamera->yaw(mRotScale);
}
if( mInputDevice->isKeyDown( KC_ESCAPE) )
{
return false;
}
// see if switching is on, and you want to toggle
if (mInputTypeSwitchingOn && mInputDevice->isKeyDown(KC_M) && mTimeUntilNextToggle <= 0)
{
switchMouseMode();
mTimeUntilNextToggle = 1;
}
if (mInputTypeSwitchingOn && mInputDevice->isKeyDown(KC_K) && mTimeUntilNextToggle <= 0)
{
// must be going from immediate keyboard to buffered keyboard
switchKeyMode();
mTimeUntilNextToggle = 1;
}
if (mInputDevice->isKeyDown(KC_F) && mTimeUntilNextToggle <= 0)
{
mStatsOn = !mStatsOn;
showDebugOverlay(mStatsOn);
mTimeUntilNextToggle = 1;
}
if (mInputDevice->isKeyDown(KC_T) && mTimeUntilNextToggle <= 0)
{
switch(mFiltering)
{
case TFO_BILINEAR:
mFiltering = TFO_TRILINEAR;
mAniso = 1;
break;
case TFO_TRILINEAR:
mFiltering = TFO_ANISOTROPIC;
mAniso = 8;
break;
case TFO_ANISOTROPIC:
mFiltering = TFO_BILINEAR;
mAniso = 1;
break;
default:
break;
}
MaterialManager::getSingleton().setDefaultTextureFiltering(mFiltering);
MaterialManager::getSingleton().setDefaultAnisotropy(mAniso);

showDebugOverlay(mStatsOn);
mTimeUntilNextToggle = 1;
}
if (mInputDevice->isKeyDown(KC_SYSRQ) && mTimeUntilNextToggle <= 0)
{
std::ostringstream ss;
ss << "screenshot_" << ++mNumScreenShots << ".png";
mWindow->writeContentsToFile(ss.str());
mTimeUntilNextToggle = 0.5;
mDebugText = "Saved: " + ss.str();
}
if (mInputDevice->isKeyDown(KC_R) && mTimeUntilNextToggle <=0)
{
mSceneDetailIndex = (mSceneDetailIndex+1)%3 ;
switch(mSceneDetailIndex)
{
case 0 : mCamera->setPolygonMode(PM_SOLID) ; break ;
case 1 : mCamera->setPolygonMode(PM_WIREFRAME) ; break ;
case 2 : mCamera->setPolygonMode(PM_POINTS) ; break ;
}
mTimeUntilNextToggle = 0.5;
}
static bool displayCameraDetails = false;
if (mInputDevice->isKeyDown(KC_P) && mTimeUntilNextToggle <= 0)
{
displayCameraDetails = !displayCameraDetails;
mTimeUntilNextToggle = 0.5;
if (!displayCameraDetails)
mDebugText = "";
}
if (displayCameraDetails)
{
// Print camera details
mDebugText = "P: " + StringConverter::toString(mCamera->getDerivedPosition()) +
" " + "O: " + StringConverter::toString(mCamera->getDerivedOrientation());
}
// Return true to continue rendering
return true;
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
bool BaseApplication::processUnbufferedMouseInput(const FrameEvent& evt)

{

return true;
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
void BaseApplication::moveCamera()

{
// Make all the changes to the camera
// Note that YAW direction is around a fixed axis (freelook style) rather than a natural YAW (e.g. airplane)
mCamera->yaw(mRotX);
mCamera->pitch(mRotY);
mCamera->moveRelative(mTranslateVector);

}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
void BaseApplication::showDebugOverlay(bool show)

{
}
//-------------------------------------------------------------------------------------
void BaseApplication::requestShutdown(void)

{
mShutdownRequested = true;
}
//-------------------------------------------------------------------------------------
bool BaseApplication::frameStarted(const FrameEvent& evt)

{
if(mWindow->isClosed())
return false;
//Need to capture/update each device
//mKeyboard->capture();
//mMouse->capture();
//mUseBufferedInputMouse = mMouse->buffered();
//mUseBufferedInputKeys = mKeyboard->buffered();

// if(mUseBufferedInputMouse)
// {
// CEGUI::MouseCursor::getSingleton().show( );
// }
// else
// {
// CEGUI::MouseCursor::getSingleton().hide( );
// }
//if ( !mUseBufferedInputMouse || !mUseBufferedInputKeys)
//{
// // one of the input modes is immediate, so setup what is needed for immediate mouse/key movement
// if (mTimeUntilNextToggle >= 0)
// mTimeUntilNextToggle -= evt.timeSinceLastFrame;
// // If this is the first frame, pick a speed
// if (evt.timeSinceLastFrame == 0)
// {
// mMoveScale = 1;
// mRotScale = 0.1;
// }
// // Otherwise scale movement units by time passed since last frame
// else
// {
// // Move about 100 units per second,
// mMoveScale = mMoveSpeed * evt.timeSinceLastFrame;
// // Take about 10 seconds for full rotation
// mRotScale = mRotateSpeed * evt.timeSinceLastFrame;
// }
// mRotX = 0;
// mRotY = 0;
// mTranslateVector = Vector3::ZERO;
//}
//if (mUseBufferedInputKeys)
//{
// // no need to do any processing here, it is handled by event processor and
// // you get the results as KeyEvents
//}
//else
//{
// if (processUnbufferedKeyInput(evt) == false)
// {
// return false;
// }
//}
//if (mUseBufferedInputMouse)
//{
// // no need to do any processing here, it is handled by event processor and
// // you get the results as MouseEvents
//}
//else
//{
// if (processUnbufferedMouseInput(evt) == false)
// {
// return false;
// }
//}
//if ( !mUseBufferedInputMouse || !mUseBufferedInputKeys)
//{
// // one of the input modes is immediate, so update the movement vector
// moveCamera();
//}
return true;
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
bool BaseApplication::frameEnded(const FrameEvent& evt)

{
if (mShutdownRequested)
return false;
//updateStats();
return true;
}
//-------------------------------------------------------------------------------------
void BaseApplication::switchMouseMode()

{
}
//-------------------------------------------------------------------------------------
void BaseApplication::switchKeyMode()

{
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
bool BaseApplication::keyPressed( const OIS::KeyEvent &arg )

{
return true;
}
bool BaseApplication::keyReleased( const OIS::KeyEvent &arg )

{
return true;
}
bool BaseApplication::mouseMoved( const OIS::MouseEvent &arg )

{
//CEGUI::System::getSingleton().injectMouseMove( arg.state.X.rel, arg.state.Y.rel );
return true;
}
bool BaseApplication::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )

{
return true;
}
bool BaseApplication::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )

{
return true;
}
//Adjust mouse clipping area
void BaseApplication::windowResized(RenderWindow* rw)

{
unsigned int width, height, depth;
int left, top;

//const OIS::MouseState &ms = mMouse->getMouseState();
//ms.width = width;
//ms.height = height;
}
//Unattach OIS before window shutdown (very important under Linux)
void BaseApplication::windowClosed(RenderWindow* rw)

{
}
//-------------------------------------------------------------------------------------
void BaseApplication::setupEventHandlers(void)

{
}
//-------------------------------------------------------------------------------------
bool BaseApplication::handleQuit(const CEGUI::EventArgs& e)

{
requestShutdown();
return true;
}

浙公网安备 33010602011771号