-
Notifications
You must be signed in to change notification settings - Fork 10
/
MainWindow.cpp
329 lines (270 loc) · 10.5 KB
/
MainWindow.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "SSAONode.h"
#include "Osg3dSSAOView.h"
#include <QSettings>
#include <QFileDialog>
#include <QFileInfo>
#include <osg/ShapeDrawable>
#include <osg/Geode>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_world(new osg::Group)
{
ui->setupUi(this);
Osg3dSSAOView *ssaoView = ui->uiEventWidget->ssaoView();
applicationSetup();
connectHandlers(ssaoView);
setupOSGWidget(ssaoView);
setupSSAOWidget(ssaoView);
setMouseModeOrbit();
m_world->addChild(buildScene());
ui->osgWidget->setScene(m_world);
ui->uiEventWidget->ssaoView()->addNode(m_world);
ui->uiEventWidget->ssaoView()->cameraModel()->fitToScreen();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::applicationSetup()
{
// set up application name
QFileInfo applicationFile(QApplication::applicationFilePath());
// These allow us to simply construct a "QSettings" object without arguments
qApp->setOrganizationDomain("org.vissimlab");
qApp->setApplicationName(applicationFile.baseName());
qApp->setOrganizationName("LeeButler");
qApp->setApplicationVersion(__DATE__ __TIME__);
QActionGroup *group = new QActionGroup(this);
group->addAction(ui->actionOrbit);
group->addAction(ui->actionPan);
group->addAction(ui->actionZoom);
group->addAction(ui->actionRotate);
}
void MainWindow::connectHandlers(Osg3dSSAOView *ssaoView)
{
connect(ui->actionOrbit, SIGNAL(triggered()), this, SLOT(setMouseModeOrbit()) );
connect(ui->actionPan, SIGNAL(triggered()), this, SLOT(setMouseModePan()) );
connect(ui->actionRotate, SIGNAL(triggered()), this, SLOT(setMouseModeRotate()) );
connect(ui->actionZoom, SIGNAL(triggered()), this, SLOT(setMouseModeZoom()) );
connect(ui->radiusEdit, SIGNAL(editingFinished()),
this, SLOT(setRadius()));
connect(ui->thresholdEdit, SIGNAL(editingFinished()),
this, SLOT(setThreshold()));
connect(ui->powerEdit, SIGNAL(editingFinished()),
this, SLOT(setPower()));
connect(ui->displayModeCombo, SIGNAL(currentIndexChanged(int)),
this, SLOT(handleDisplayModeCombo()));
connect(ui->aoBlurr, SIGNAL(toggled(bool)),
this, SLOT(ssaoBlur(bool)));
connect(ui->haloRemoval, SIGNAL(toggled(bool)),
this, SLOT(ssaoHalo(bool)));
connect(ui->ssaoCheckBox, SIGNAL(toggled(bool)),
this, SLOT(setSSAOEnabled(bool)));
ui->powerEdit->setText(QString("%1").arg(ssaoView->ssaoPower()));
ui->radiusEdit->setText(QString("%1").arg(ssaoView->ssaoRadius() ));
ui->thresholdEdit->setText(QString("%1").arg(ssaoView->ssaoHaloThreshold() ));
ui->haloRemoval->setChecked(ssaoView->ssaoHaloRemovalIsEnabled());
ui->aoBlurr->setChecked(ssaoView->ssaoBlurIsEnabled());
ui->displayModeCombo->setCurrentIndex(ssaoView->ssaoDisplayMode());
}
void MainWindow::setupOSGWidget(Osg3dSSAOView *ssaoView)
{
ui->osgWidget->setSSAOPower(ssaoView->ssaoPower());
ui->osgWidget->setSSAORadius(ssaoView->ssaoRadius());
ui->osgWidget->setSSAOHaloThreshold(ssaoView->ssaoHaloThreshold());
ui->osgWidget->setSSAOHaloRemoval(ssaoView->ssaoHaloRemovalIsEnabled());
ui->osgWidget->setSSAOBlurEnabled(ssaoView->ssaoBlurIsEnabled());
ui->osgWidget->setSSAODisplayMode((SSAONode::DisplayMode)ssaoView->ssaoDisplayMode());
ui->osgWidget->setCameraModel(ssaoView->cameraModel());
ui->osgWidget->setSSAOEnabled(true);
}
void MainWindow::setupSSAOWidget(Osg3dSSAOView *ssaoView)
{
connect(ui->haloRemoval, SIGNAL(toggled(bool)),
ssaoView, SLOT(setSSAOHaloRemoval(bool)));
connect(ui->aoBlurr, SIGNAL(toggled(bool)),
ssaoView, SLOT(setSSAOBlurEnabled(bool)));
connect(ui->ssaoCheckBox, SIGNAL(toggled(bool)),
ssaoView, SLOT(setSSAOEnabled(bool)));
}
osg::ref_ptr<osg::Geode> MainWindow::buildAxes()
{
osg::ref_ptr<osg::Geometry> m_axisGeom = new osg::Geometry();
// allocate verticies
osg::ref_ptr<osg::Vec3Array> m_axisVerts = new osg::Vec3Array;
m_axisVerts->push_back(osg::Vec3(-1.0, 0.0, 0.0));
m_axisVerts->push_back(osg::Vec3(0.0, 0.0, 0.0));
m_axisVerts->push_back(osg::Vec3(1.0, 0.0, 0.0));
m_axisVerts->push_back(osg::Vec3(0.0, -1.0, 0.0));
m_axisVerts->push_back(osg::Vec3(0.0, 0.0, 0.0));
m_axisVerts->push_back(osg::Vec3(0.0, 1.0, 0.0));
m_axisVerts->push_back(osg::Vec3(0.0, 0.0, -1.0));
m_axisVerts->push_back(osg::Vec3(0.0, 0.0, 0.0));
m_axisVerts->push_back(osg::Vec3(0.0, 0.0, 1.0));
m_axisGeom->setVertexArray(m_axisVerts);
m_axisGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 2));
m_axisGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 1, 2));
m_axisGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 3, 2));
m_axisGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 4, 2));
m_axisGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 6, 2));
m_axisGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 7, 2));
// allocate colors
osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(.35f,0.0f,0.0f,1.0f));
colors->push_back(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
colors->push_back(osg::Vec4(0.0f,.35f,0.0f,1.0f));
colors->push_back(osg::Vec4(0.0f,1.0f,0.0f,1.0f));
colors->push_back(osg::Vec4(0.0f,0.0f,.35f,1.0f));
colors->push_back(osg::Vec4(0.0f,0.0f,1.0f,1.0f));
m_axisGeom->setColorArray(colors);
m_axisGeom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
m_axisGeom->setDataVariance(osg::Object::DYNAMIC);
// generate the Geode
osg::ref_ptr<osg::Geode> m_axisGeode = new osg::Geode();
m_axisGeode->addDrawable(m_axisGeom);
//m_axisGeode->setNodeMask( MASK_AXIS );
m_axisGeode->setName("Axis");
//turn off lighting so we always see the line color
osg::StateSet *ss = m_axisGeode->getOrCreateStateSet();
ss->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
// Set the linewidth a little wider so we can see the thing
osg::LineWidth *lineWidth = new osg::LineWidth;
lineWidth->setWidth( 2.0 );
ss->setAttributeAndModes(lineWidth);
return m_axisGeode;
}
// TODO: Remove
double drand48()
{
return ((double)rand()/(double)RAND_MAX);
}
double randColor()
{
return drand48() * 0.875 + 0.125;
}
osg::ref_ptr<osg::Node> MainWindow::buildScene()
{
osg::ref_ptr<osg::Group> node = new osg::Group;
osg::ref_ptr<osg::Geode> geode = buildAxes();
node->addChild(geode);
#if 1
geode = new osg::Geode;
#define RANDCOORD ((drand48() - 0.5) * 2.0)
float boxDimen = 30.0;
float sphMin = 0.125;
float sphMax = 4;
for (int i=0 ; i < 5000 ; i++ ) {
float x = boxDimen * RANDCOORD;
float y = boxDimen * RANDCOORD;
float z = boxDimen * RANDCOORD;
float radius = sphMin + (drand48() * (sphMax-sphMin));
osg::ShapeDrawable *sd = new osg::ShapeDrawable(new osg::Box(osg::Vec3(x, y, z), radius));
sd->setColor(osg::Vec4(randColor(), randColor(), randColor(), 1.0));
geode->addDrawable(sd);
}
node->addChild(geode);
osgDB::writeNodeFile(*node, "testScene.osg");
#endif
return node;
}
void MainWindow::on_actionOpen_triggered()
{
QSettings settings;
// if there is a current workFlowController, we need
QString fileName = QFileDialog::getOpenFileName(this, "Select File",
settings.value("currentDirectory").toString(),
"OSG File (*.osg *.ive *.osgt *.osgb *.obj *.ply)");
if (fileName.isEmpty() || fileName.isNull())
return;
osg::ref_ptr<osg::Node> loaded = osgDB::readNodeFile(fileName.toStdString());
if (!loaded.valid()) return;
const osg::BoundingSphere &bs = loaded->getBound();
if (bs.radius() <= 0.0) return;
m_world->removeChildren(0, m_world->getNumChildren());
m_world->addChild(loaded);
QFileInfo fi(fileName);
settings.setValue("currentDirectory", fi.absolutePath());
ui->uiEventWidget->ssaoView()->cameraModel()->fitToScreen();
}
void MainWindow::setMouseModeOrbit()
{
ui->actionOrbit->setChecked(true);
ui->uiEventWidget->setMouseMode(UiEventWidget::MM_ORBIT);
}
void MainWindow::setMouseModePan()
{
ui->actionPan->setChecked(true);
ui->uiEventWidget->setMouseMode(UiEventWidget::MM_PAN);
}
void MainWindow::setMouseModeRotate()
{
ui->actionRotate->setChecked(true);
ui->uiEventWidget->setMouseMode(UiEventWidget::MM_ROTATE);
}
void MainWindow::setMouseModeZoom()
{
ui->actionZoom->setChecked(true);
ui->uiEventWidget->setMouseMode(UiEventWidget::MM_ZOOM);
}
void MainWindow::handleDisplayModeCombo()
{
ui->osgWidget->setSSAODisplayMode(
(SSAONode::DisplayMode)
ui->displayModeCombo->currentIndex()
);
ui->uiEventWidget->ssaoView()->setSSAODisplayMode(
(SSAONode::DisplayMode)
ui->displayModeCombo->currentIndex()
);
ui->osgWidget->update();
ui->uiEventWidget->ssaoView()->update();
}
void MainWindow::ssaoHalo(bool tf)
{
ui->osgWidget->setSSAOHaloRemoval(tf);
ui->uiEventWidget->ssaoView()->setSSAOHaloRemoval(tf);
ui->osgWidget->update();
ui->uiEventWidget->ssaoView()->update();
}
void MainWindow::ssaoBlur(bool tf)
{
ui->osgWidget->setSSAOBlurEnabled(tf);
ui->uiEventWidget->ssaoView()->setSSAOBlurEnabled(tf);
ui->osgWidget->update();
ui->uiEventWidget->ssaoView()->update();
}
void MainWindow::setSSAOEnabled(bool tf)
{
ui->displayModeCombo->setEnabled(tf);
ui->osgWidget->setSSAOEnabled(tf);
ui->uiEventWidget->ssaoView()->setSSAOEnabled(tf);
}
void MainWindow::setRadius()
{
float radius = ui->radiusEdit->text().toFloat();
ui->osgWidget->setSSAORadius(radius);
ui->uiEventWidget->ssaoView()->setSSAORadius(radius);
ui->osgWidget->update();
ui->uiEventWidget->ssaoView()->update();
}
void MainWindow::setThreshold()
{
float threshold = ui->thresholdEdit->text().toFloat();
ui->osgWidget->setSSAOHaloThreshold(threshold);
ui->uiEventWidget->ssaoView()->setSSAOHaloThreshold(threshold);
ui->osgWidget->update();
ui->uiEventWidget->ssaoView()->update();
}
void MainWindow::setPower()
{
float power = ui->powerEdit->text().toFloat();
ui->osgWidget->setSSAOPower(power);
ui->uiEventWidget->ssaoView()->setSSAOPower(power);
ui->osgWidget->update();
ui->uiEventWidget->ssaoView()->update();
}