-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogrewidget.cpp
159 lines (134 loc) · 4.46 KB
/
ogrewidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "OgreWidget.h"
OgreWidget::OgreWidget(QObject *parent) : QGLWidget((QWidget *)parent), m_ogreWindow(NULL)
{
this->m_isInit = false;
connect(&m_timer, SIGNAL(timeout()), this, SLOT(repaint()));
}
OgreWidget::~OgreWidget(void)
{
destroy();
}
void OgreWidget::initRender(QString pluginPath,
QString configPath,
QString logPath,
QString resPath,
QString renderSys,
QHash<QString, QString> renderSysConf)
{
if (!this->m_isInit)
{
this->m_ogreRoot = new Ogre::Root(pluginPath.toStdString(), configPath.toStdString(), logPath.toStdString());
// setup a renderer
Ogre::RenderSystemList::const_iterator renderers = m_ogreRoot->getAvailableRenderers().begin();
while (renderers != m_ogreRoot->getAvailableRenderers().end())
{
Ogre::String rName = (*renderers)->getName();
if (rName == renderSys.toStdString())
break;
renderers++;
}
m_ogreRoot->setRenderSystem((Ogre::RenderSystem *)(*renderers));
QHash<QString, QString> m_renderOptions = renderSysConf;
if (m_renderOptions.empty())
{
m_renderOptions.insert(QString("Video Mode"), QString("%1x%2").arg(this->width()).arg(this->height()));
m_renderOptions.insert(QString("Full Screen"), QString("No"));
}
QHashIterator<QString, QString> i(m_renderOptions);
while (i.hasNext())
{
i.next();
m_ogreRoot->getRenderSystem()->setConfigOption(i.key().toStdString(), i.value().toStdString());
}
m_ogreRoot->saveConfig();
m_ogreRoot->initialise(false); // don't create a window
// Load resource paths from config file
Ogre::ConfigFile cf;
cf.load(resPath.toStdString());
// Go through all sections & settings in the file
Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
QString secName, typeName, archName;
while (seci.hasMoreElements())
{
secName = QString::fromStdString((std::string)seci.peekNextKey());
Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); ++i)
{
typeName = QString::fromStdString((std::string)i->first);
archName = QString::fromStdString((std::string)i->second);
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(archName.toStdString(), typeName.toStdString(), secName.toStdString());
}
}
m_timer.start(10);
this->m_isInit = true;
}
}
void OgreWidget::stopRender()
{
if (this->m_isInit)
{
if (m_timer.isActive())
m_timer.stop();
m_ogreRoot->shutdown();
delete m_ogreRoot;
this->m_isInit = false;
}
}
void OgreWidget::initializeGL()
{
if (this->m_isInit)
{
//== Creating and Acquiring Ogre Window ==//
// Get the parameters of the window QT created
Ogre::String winHandle;
winHandle += Ogre::StringConverter::toString((unsigned long)(this->parentWidget()->winId()));
Ogre::NameValuePairList params;
// code for Windows and Linux
params["parentWindowHandle"] = winHandle;
m_ogreWindow = m_ogreRoot->createRenderWindow("QOgreWidget_RenderWindow",
this->width(),
this->height(),
false,
¶ms);
m_ogreWindow->setActive(true);
WId ogreWinId = 0x0;
m_ogreWindow->getCustomAttribute("WINDOW", &ogreWinId);
// bug fix, extract geometry
QRect geo = this->frameGeometry();
// create new window
this->create(ogreWinId);
this->setGeometry(geo);
setAutoBufferSwap(false);
setAttribute(Qt::WA_PaintOnScreen, true);
setAttribute(Qt::WA_NoBackground);
//== Ogre Initialization ==//
m_ogreSceneMgr = m_ogreRoot->createSceneManager(Ogre::ST_GENERIC);
m_ogreCamera = m_ogreSceneMgr->createCamera("QOgreWidget_Cam");
m_ogreViewport = m_ogreWindow->addViewport(m_ogreCamera);
m_ogreViewport->setBackgroundColour(Ogre::ColourValue(0,0,0));
m_ogreCamera->setAspectRatio(Ogre::Real(m_ogreViewport->getActualWidth()) / Ogre::Real(m_ogreViewport->getActualHeight()));
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
resizeGL(this->width(), this->height());
emit ogreWidgetReady(this->m_ogreRoot, this->m_ogreWindow, this->m_ogreCamera, this->m_ogreViewport, this->m_ogreSceneMgr);
}
}
void OgreWidget::paintGL()
{
if (this->m_isInit)
{
// Be sure to call "OgreWidget->repaint();"
swapBuffers();
Sleep(500);
m_ogreRoot->renderOneFrame();
}
}
void OgreWidget::resizeGL(int width, int height)
{
if (this->m_isInit)
{
m_ogreWindow->reposition(this->pos().x(), this->pos().y());
m_ogreWindow->resize(width, height);
paintGL();
}
}