用自己的main loop替代系统的main loop
#include "ogre/Ogre.h"
#include "ois/OIS.h"
class MyFrameListener : public Ogre::FrameListener
{
public:
MyFrameListener(Ogre::RenderWindow* win)
{
size_t winHandle = 0;
std::ostringstream winHandleStr;
OIS::ParamList pl;
win->getCustomAttribute("WINDOW", &winHandle);
winHandleStr << winHandle;
pl.insert(make_pair("WINDOW", winHandleStr.str()));
inputMgr_ = OIS::InputManager::createInputSystem(pl);
keyBoard_ = static_cast<OIS::Keyboard*>(inputMgr_->createInputObject(OIS::OISKeyboard, false));
}
~MyFrameListener()
{
inputMgr_->destroyInputObject(keyBoard_);
OIS::InputManager::destroyInputSystem(inputMgr_);
}
// 返回false则渲染结束
bool frameStarted(const Ogre::FrameEvent& evt)
{
keyBoard_->capture();
if (keyBoard_->isKeyDown(OIS::KC_ESCAPE))
{
return false;
}
return true;
}
// 返回false则渲染结束
bool frameRenderingQueued(const Ogre::FrameEvent& evt)
{
return true;
}
// 返回false则渲染结束
bool frameEnded(const Ogre::FrameEvent& evt)
{
return true;
}
private:
OIS::InputManager* inputMgr_;
OIS::Keyboard* keyBoard_;
};
class MyAppliaction
{
public:
MyAppliaction()
{
root_ = 0;
sceneMgr_ = 0;
frameListener_ = 0;
keepRunning_ = true;
}
~MyAppliaction()
{
delete root_;
delete frameListener_;
}
void LoadResources()
{
Ogre::ConfigFile cf;
cf.load("resources_d.cfg");
Ogre::ConfigFile::SectionIterator ite = cf.getSectionIterator();
Ogre::String sectionName, typeName, dataName;
while (ite.hasMoreElements())
{
sectionName = ite.peekNextKey();
Ogre::ConfigFile::SettingsMultiMap* setting = ite.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator i;
for (i = setting->begin(); i != setting->end(); ++i)
{
typeName = i->first;
dataName = i->second;
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(dataName, typeName, sectionName);
}
}
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}
void CreateScene()
{
Ogre::Entity* ent = sceneMgr_->createEntity("Sinbad.mesh");
sceneMgr_->getRootSceneNode()->createChildSceneNode()->attachObject(ent);
}
bool KeepRunning() const
{
return keepRunning_;
}
void RenderOneFrame()
{
// 处理消息
Ogre::WindowEventUtilities::messagePump();
keepRunning_ = root_->renderOneFrame();
}
int StartUp()
{
root_ = new Ogre::Root("plugins_d.cfg");
if (!root_->showConfigDialog())
return -1;
Ogre::RenderWindow* window = root_->initialise(true, "HKX");
sceneMgr_ = root_->createSceneManager(Ogre::ST_GENERIC);
Ogre::Camera* camera = sceneMgr_->createCamera("camera");
camera->setPosition(Ogre::Vector3(0, 0, 50));
camera->lookAt(0, 0, 0);
camera->setNearClipDistance(5);
Ogre::Viewport* vp = window->addViewport(camera, 0, 0, 0, 1.0, 1.0);
vp->setBackgroundColour(Ogre::ColourValue(0.0, 0.0, 0.0, 0.0));
camera->setAspectRatio(Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
frameListener_ = new MyFrameListener(window);
root_->addFrameListener(frameListener_);
LoadResources();
CreateScene();
// root_->startRendering();
return 0;
}
private:
Ogre::Root* root_;
Ogre::SceneManager* sceneMgr_;
MyFrameListener* frameListener_;
bool keepRunning_;
};
int main()
{
MyAppliaction app;
app.StartUp();
while(app.KeepRunning())
{
app.RenderOneFrame();
}
return 0;
}
添加鼠标和键盘WASD控制:
#include "ogre/Ogre.h"
#include "ois/OIS.h"
class MyFrameListener : public Ogre::FrameListener
{
public:
MyFrameListener(Ogre::RenderWindow* win, Ogre::Camera* cam)
{
camera_ = cam;
movementSpeed_ = 50.0f;
size_t winHandle = 0;
std::ostringstream winHandleStr;
OIS::ParamList pl;
win->getCustomAttribute("WINDOW", &winHandle);
winHandleStr << winHandle;
pl.insert(make_pair("WINDOW", winHandleStr.str()));
inputMgr_ = OIS::InputManager::createInputSystem(pl);
keyBoard_ = static_cast<OIS::Keyboard*>(inputMgr_->createInputObject(OIS::OISKeyboard, false));
mouse_ = static_cast<OIS::Mouse*>(inputMgr_->createInputObject(OIS::OISMouse, false));
}
~MyFrameListener()
{
inputMgr_->destroyInputObject(keyBoard_);
inputMgr_->destroyInputObject(mouse_);
OIS::InputManager::destroyInputSystem(inputMgr_);
}
// 返回false则渲染结束
bool frameStarted(const Ogre::FrameEvent& evt)
{
keyBoard_->capture();
if (keyBoard_->isKeyDown(OIS::KC_ESCAPE))
return false;
Ogre::Vector3 translate(0, 0, 0);
if (keyBoard_->isKeyDown(OIS::KC_W))
translate += Ogre::Vector3(0, 0, -1);
if (keyBoard_->isKeyDown(OIS::KC_S))
translate += Ogre::Vector3(0, 0, 1);
if (keyBoard_->isKeyDown(OIS::KC_A))
translate += Ogre::Vector3(-1, 0, 0);
if (keyBoard_->isKeyDown(OIS::KC_D))
translate += Ogre::Vector3(1, 0, 0);
camera_->moveRelative(translate * evt.timeSinceLastFrame * movementSpeed_);
mouse_->capture();
float rotX = mouse_->getMouseState().X.rel * evt.timeSinceLastFrame * -1;
float rotY = mouse_->getMouseState().Y.rel * evt.timeSinceLastFrame * -1;
camera_->yaw(Ogre::Radian(rotX));
camera_->pitch(Ogre::Radian(rotY));
return true;
}
// 返回false则渲染结束
bool frameRenderingQueued(const Ogre::FrameEvent& evt)
{
return true;
}
// 返回false则渲染结束
bool frameEnded(const Ogre::FrameEvent& evt)
{
return true;
}
private:
OIS::InputManager* inputMgr_;
OIS::Keyboard* keyBoard_;
OIS::Mouse* mouse_;
Ogre::Camera* camera_;
float movementSpeed_;
};
class MyAppliaction
{
public:
MyAppliaction()
{
root_ = 0;
sceneMgr_ = 0;
frameListener_ = 0;
keepRunning_ = true;
}
~MyAppliaction()
{
delete root_;
delete frameListener_;
}
void LoadResources()
{
Ogre::ConfigFile cf;
cf.load("resources_d.cfg");
Ogre::ConfigFile::SectionIterator ite = cf.getSectionIterator();
Ogre::String sectionName, typeName, dataName;
while (ite.hasMoreElements())
{
sectionName = ite.peekNextKey();
Ogre::ConfigFile::SettingsMultiMap* setting = ite.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator i;
for (i = setting->begin(); i != setting->end(); ++i)
{
typeName = i->first;
dataName = i->second;
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(dataName, typeName, sectionName);
}
}
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}
void CreateScene()
{
Ogre::Entity* ent = sceneMgr_->createEntity("Sinbad.mesh");
sceneMgr_->getRootSceneNode()->createChildSceneNode()->attachObject(ent);
}
bool KeepRunning() const
{
return keepRunning_;
}
void RenderOneFrame()
{
// 处理消息
Ogre::WindowEventUtilities::messagePump();
keepRunning_ = root_->renderOneFrame();
}
int StartUp()
{
root_ = new Ogre::Root("plugins_d.cfg");
if (!root_->showConfigDialog())
return -1;
Ogre::RenderWindow* window = root_->initialise(true, "HKX");
sceneMgr_ = root_->createSceneManager(Ogre::ST_GENERIC);
Ogre::Camera* camera = sceneMgr_->createCamera("camera");
camera->setPosition(Ogre::Vector3(0, 0, 50));
camera->lookAt(0, 0, 0);
camera->setNearClipDistance(5);
Ogre::Viewport* vp = window->addViewport(camera, 0, 0, 0, 1.0, 1.0);
vp->setBackgroundColour(Ogre::ColourValue(0.0, 0.0, 0.0, 0.0));
camera->setAspectRatio(Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
frameListener_ = new MyFrameListener(window, camera);
root_->addFrameListener(frameListener_);
LoadResources();
CreateScene();
// root_->startRendering();
return 0;
}
private:
Ogre::Root* root_;
Ogre::SceneManager* sceneMgr_;
MyFrameListener* frameListener_;
bool keepRunning_;
};
int main()
{
MyAppliaction app;
app.StartUp();
while(app.KeepRunning())
{
app.RenderOneFrame();
}
return 0;
}
添加合成器、地面、光照、阴影:
#include "ogre/Ogre.h"
#include "ois/OIS.h"
class MyFrameListener : public Ogre::FrameListener
{
public:
MyFrameListener(Ogre::RenderWindow* win, Ogre::Camera* cam, Ogre::Viewport* vp)
{
camera_ = cam;
movementSpeed_ = 50.0f;
vp_ = vp;
size_t winHandle = 0;
std::ostringstream winHandleStr;
OIS::ParamList pl;
win->getCustomAttribute("WINDOW", &winHandle);
winHandleStr << winHandle;
pl.insert(make_pair("WINDOW", winHandleStr.str()));
inputMgr_ = OIS::InputManager::createInputSystem(pl);
keyBoard_ = static_cast<OIS::Keyboard*>(inputMgr_->createInputObject(OIS::OISKeyboard, false));
mouse_ = static_cast<OIS::Mouse*>(inputMgr_->createInputObject(OIS::OISMouse, false));
bComp1On_ = false;
bComp2On_ = false;
bComp3On_ = false;
bDown1_ = false;
bDown2_ = false;
bDown3_ = false;
}
~MyFrameListener()
{
inputMgr_->destroyInputObject(keyBoard_);
inputMgr_->destroyInputObject(mouse_);
OIS::InputManager::destroyInputSystem(inputMgr_);
}
// 返回false则渲染结束
bool frameStarted(const Ogre::FrameEvent& evt)
{
keyBoard_->capture();
if (keyBoard_->isKeyDown(OIS::KC_ESCAPE))
return false;
Ogre::Vector3 translate(0, 0, 0);
if (keyBoard_->isKeyDown(OIS::KC_W))
translate += Ogre::Vector3(0, 0, -1);
if (keyBoard_->isKeyDown(OIS::KC_S))
translate += Ogre::Vector3(0, 0, 1);
if (keyBoard_->isKeyDown(OIS::KC_A))
translate += Ogre::Vector3(-1, 0, 0);
if (keyBoard_->isKeyDown(OIS::KC_D))
translate += Ogre::Vector3(1, 0, 0);
camera_->moveRelative(translate * evt.timeSinceLastFrame * movementSpeed_);
mouse_->capture();
float rotX = mouse_->getMouseState().X.rel * evt.timeSinceLastFrame * -1;
float rotY = mouse_->getMouseState().Y.rel * evt.timeSinceLastFrame * -1;
camera_->yaw(Ogre::Radian(rotX));
camera_->pitch(Ogre::Radian(rotY));
if (keyBoard_->isKeyDown(OIS::KC_1) && !bDown1_)
{
bDown1_ = true;
bComp1On_ = !bComp1On_;
Ogre::CompositorManager::getSingleton().setCompositorEnabled(vp_, "Compositor1", bComp1On_);
}
if (keyBoard_->isKeyDown(OIS::KC_2) && !bDown2_)
{
bDown2_ = true;
bComp2On_ = !bComp2On_;
Ogre::CompositorManager::getSingleton().setCompositorEnabled(vp_, "Compositor2", bComp2On_);
}
if (keyBoard_->isKeyDown(OIS::KC_3) && !bDown3_)
{
bDown3_ = true;
bComp3On_ = !bComp3On_;
Ogre::CompositorManager::getSingleton().setCompositorEnabled(vp_, "Compositor3", bComp3On_);
}
if (!keyBoard_->isKeyDown(OIS::KC_1))
bDown1_ = false;
if (!keyBoard_->isKeyDown(OIS::KC_2))
bDown2_ = false;
if (!keyBoard_->isKeyDown(OIS::KC_3))
bDown3_ = false;
return true;
}
// 返回false则渲染结束
bool frameRenderingQueued(const Ogre::FrameEvent& evt)
{
return true;
}
// 返回false则渲染结束
bool frameEnded(const Ogre::FrameEvent& evt)
{
return true;
}
private:
OIS::InputManager* inputMgr_;
OIS::Keyboard* keyBoard_;
OIS::Mouse* mouse_;
Ogre::Camera* camera_;
float movementSpeed_;
Ogre::Viewport* vp_;
// which compositor on
bool bComp1On_;
bool bComp2On_;
bool bComp3On_;
bool bDown1_;
bool bDown2_;
bool bDown3_;
};
class MyAppliaction
{
public:
MyAppliaction()
{
root_ = 0;
sceneMgr_ = 0;
frameListener_ = 0;
keepRunning_ = true;
}
~MyAppliaction()
{
delete root_;
delete frameListener_;
}
void LoadResources()
{
Ogre::ConfigFile cf;
cf.load("resources_d.cfg");
Ogre::ConfigFile::SectionIterator ite = cf.getSectionIterator();
Ogre::String sectionName, typeName, dataName;
while (ite.hasMoreElements())
{
sectionName = ite.peekNextKey();
Ogre::ConfigFile::SettingsMultiMap* setting = ite.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator i;
for (i = setting->begin(); i != setting->end(); ++i)
{
typeName = i->first;
dataName = i->second;
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(dataName, typeName, sectionName);
}
}
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}
void CreateScene()
{
Ogre::Plane plane(Ogre::Vector3::UNIT_Y, -5);
Ogre::MeshManager::getSingleton().createPlane("plane",
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane, 1500, 1500, 200, 200,
true, 1, 5, 5, Ogre::Vector3::UNIT_Z);
Ogre::Entity* ground = sceneMgr_->createEntity("planeEntity", "plane");
sceneMgr_->getRootSceneNode()->createChildSceneNode()->attachObject(ground);
ground->setMaterialName("Examples/BeachStones");
Ogre::Light* light = sceneMgr_->createLight("light1");
light->setType(Ogre::Light::LT_DIRECTIONAL);
light->setDirection(Ogre::Vector3(1, -1, 0));
sceneMgr_->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE);
Ogre::Entity* ent = sceneMgr_->createEntity("Sinbad.mesh");
sceneMgr_->getRootSceneNode()->createChildSceneNode()->attachObject(ent);
}
bool KeepRunning() const
{
return keepRunning_;
}
void RenderOneFrame()
{
// 处理消息
Ogre::WindowEventUtilities::messagePump();
keepRunning_ = root_->renderOneFrame();
}
int StartUp()
{
root_ = new Ogre::Root("plugins_d.cfg");
if (!root_->showConfigDialog())
return -1;
Ogre::RenderWindow* window = root_->initialise(true, "HKX");
sceneMgr_ = root_->createSceneManager(Ogre::ST_GENERIC);
Ogre::Camera* camera = sceneMgr_->createCamera("camera");
camera->setPosition(Ogre::Vector3(0, 0, 50));
camera->lookAt(0, 0, 0);
camera->setNearClipDistance(5);
Ogre::Viewport* vp = window->addViewport(camera, 0, 0, 0, 1.0, 1.0);
vp->setBackgroundColour(Ogre::ColourValue(0.0, 0.0, 0.0, 0.0));
camera->setAspectRatio(Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
frameListener_ = new MyFrameListener(window, camera, vp);
root_->addFrameListener(frameListener_);
// 添加三个合成器
Ogre::CompositorManager::getSingleton().addCompositor(vp, "Compositor1");
Ogre::CompositorManager::getSingleton().addCompositor(vp, "Compositor2");
Ogre::CompositorManager::getSingleton().addCompositor(vp, "Compositor3");
LoadResources();
CreateScene();
// root_->startRendering();
return 0;
}
private:
Ogre::Root* root_;
Ogre::SceneManager* sceneMgr_;
MyFrameListener* frameListener_;
bool keepRunning_;
};
int main()
{
MyAppliaction app;
app.StartUp();
while(app.KeepRunning())
{
app.RenderOneFrame();
}
return 0;
}