GraphicOgreImpl具体render:
View Code
#ifndef __GraphicOgreImpl_H__
#define __GraphicOgreImpl_H__
#include <Ogre/Ogre.h>
#include <Ogre/OgreSingleton.h>
#include <boost/smart_ptr.hpp>
class GraphicOgreImpl
{
public:
GraphicOgreImpl(void);
~GraphicOgreImpl();
void init(HWND hwnd, size_t width, size_t height);
void setRenderProperties();
void setupResources();
void loadResources();
void clearResources();
bool update();
bool doFrame();
void createMainSceneManager();
void createMainViewPort();
void createMainCamera();
void createTestScene();
void ogreRenderUpdate();
boost::scoped_ptr<Ogre::Root> m_pRoot;
Ogre::SceneManager* m_pMainSceneManager;
Ogre::RenderWindow* m_pMainRenderWindow;
Ogre::Viewport* m_pMainViewPort;
Ogre::Camera* m_pMainCamera;
bool m_init;
bool m_exit;
};
#endif
View Code
#include "GraphicOgreImpl.h"
#include <boost/lexical_cast.hpp>
GraphicOgreImpl::GraphicOgreImpl(void)
{
}
GraphicOgreImpl::~GraphicOgreImpl()
{
}
void GraphicOgreImpl::init(HWND hwnd, size_t width, size_t height)
{
m_pRoot.reset(new Ogre::Root("plugins_d.cfg"));
//show pre config window
if( !m_pRoot->showConfigDialog() )
{
return;
}
Ogre::NameValuePairList params;
params["externalWindowHandle"] = boost::lexical_cast<std::string>((long)hwnd);
m_pRoot->initialise(false, "XEditor Window");
m_pMainRenderWindow = m_pRoot->createRenderWindow("MainRenderWindow", width, height, false, & params);
createMainSceneManager();
createMainCamera();
createMainViewPort();
setupResources();
loadResources();
createTestScene();
}
void GraphicOgreImpl::createMainSceneManager()
{
m_pMainSceneManager = m_pRoot->createSceneManager(Ogre::ST_GENERIC, "MainSceneManager");
}
void GraphicOgreImpl::createMainCamera()
{
m_pMainCamera = m_pMainSceneManager->createCamera("MainCamera");
m_pMainCamera->setNearClipDistance(5.0f);
m_pMainCamera->setPosition(Ogre::Vector3(0, 0, 50));
m_pMainCamera->lookAt(0, 0, 0);
}
void GraphicOgreImpl::createMainViewPort()
{
m_pMainViewPort = m_pMainRenderWindow->addViewport(m_pMainCamera);
//m_pMainCamera->setAspectRatio(m_pMainViewPort->getActualWidth() / m_pMainViewPort->getActualHeight());
m_pMainViewPort->setBackgroundColour(Ogre::ColourValue(0, 0, 0));
}
void GraphicOgreImpl::setupResources()
{
//Ogre::ResourceGroupManager::getSingletonPtr()->addResourceLocation("http://www.cnblogs.com/media/packs/Sinbad.zip", "Zip");
//Ogre::ResourceGroupManager::getSingletonPtr()->initialiseAllResourceGroups();
Ogre::ConfigFile cf;
cf.load("resources_d.cfg");
Ogre::ConfigFile::SectionIterator sect_iter = cf.getSectionIterator();
Ogre::String sectName, typeName, dataName;
while(sect_iter.hasMoreElements())
{
sectName = sect_iter.peekNextKey();
Ogre::ConfigFile::SettingsMultiMap* setting_map = sect_iter.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator it;
for (it = setting_map->begin(); it != setting_map->end(); ++it)
{
typeName = it->first;
dataName = it->second;
Ogre::ResourceGroupManager::getSingletonPtr()->addResourceLocation(dataName, typeName, sectName);
}
}
Ogre::ResourceGroupManager::getSingletonPtr()->initialiseAllResourceGroups();
}
void GraphicOgreImpl::loadResources()
{
}
void GraphicOgreImpl::clearResources()
{
}
void GraphicOgreImpl::setRenderProperties()
{
}
void GraphicOgreImpl::ogreRenderUpdate()
{
m_pRoot->renderOneFrame();
}
void GraphicOgreImpl::createTestScene()
{
Ogre::Entity* ent = m_pMainSceneManager->createEntity("Sinbad.mesh");
//Ogre::SceneNode* node = m_pMainSceneManager->createSceneNode("node");
//node->attachObject(ent);
m_pMainSceneManager->getRootSceneNode()->attachObject(ent);
}
搭载render的qt widget(timer也可以放在QMainWindow):
View Code
#include "OgreRenderWidget.h" OgreRenderWidget::OgreRenderWidget(QWidget *parent, Qt::WFlags flags, GraphicOgreImpl*m_pGraphicOgreImpl ) : QMainWindow(parent, flags), m_pXRenderScene(new XRenderScene) { //setupUI(); //createRenderWindow(); //createRenderScene(); //buildCallbacks(); //createActions(); //createToolbars(); //setFocusPolicy(Qt::StrongFocus); m_pXRenderScene->registerListener(); //renderFrame为你欲显示ogre内容的窗口,如一个QFrame类 m_pGraphicOgreImpl->init(renderFrame->winId(), renderFrame->width(), renderFrame->height()); //设置挂载ogre的窗体刷新速率. m_renderTimer.start(33, this); } void OgreRenderWidget::timerEvent(QTimerEvent* evt) { GraphicOgreImpl::getSingletonPtr()->ogreUpdate(); m_pXRenderScene->update(); }
QMainWindow:
View Code
#ifndef XEDITOR_H
#define XEDITOR_H
#include <QtGui/QMainWindow>
#include "GraphicOgreImpl.h"
#include "OgreRenderWidget.h"
#include <boost/smart_ptr.hpp>
class XEditor : public QMainWindow
{
Q_OBJECT
private:
OgreRenderWidget* m_renderWindow;
boost::scoped_ptr<GraphicOgreImpl> m_pGraphicImpl;
//QBasicTimer m_renderTimer;
public:
XEditor(QWidget *parent = 0, Qt::WFlags flags = 0);
~XEditor();
//void timerEvent(QTimerEvent * timerEvent);
};
#endif // XEDITOR_H
View Code
#include "xeditor.h"
XEditor::XEditor(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags),m_pGraphicImpl(new GraphicOgreImpl())
{
m_renderWindow = new OgreRenderWidget(this, m_pGraphicImpl.get());
m_pGraphicImpl->init(m_renderWindow->getHandle(), m_renderWindow->width(), m_renderWindow->height());
m_renderWindow->startOgreRender();
setCentralWidget(m_renderWindow);
//m_renderTimer.start(25,m_renderWindow);
resize(800, 600);
}
XEditor::~XEditor()
{
}
//void XEditor::timerEvent(QTimerEvent * timerEvent)
//{
// m_pGraphicImpl->ogreRenderUpdate();
//}