-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
365 lines (327 loc) · 13.2 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QFileDialog>
#include <QPushButton>
#include <QColorDialog>
#include <QLabel>
#include <QShowEvent>
#include <QToolBar>
#include "objectmodel.h"
#include "surreader.h"
#include "objreader.h"
#include "material.h"
#include "light.h"
#include "glviewport.h"
#define FPS 50
namespace jEle {
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
vp(new GLViewport(this)),
timer(new QTimer(this)), // used to tick the auto-rotate animation
background(new QPushButton(this)),
objectMaterial(new Material(QColor(135,160,180), QColor(255,255,255), QColor(170, 200, 225), QColor(0,0,0), 20.0f)),
isAnimated(false)
{
ui->setupUi(this);
#ifdef Q_WS_MAC
QString macMod = QString::fromUtf8("\u2318") + QString(" +");
ui->labelCtrl1->setText(macMod);
ui->labelCtrl2->setText(macMod);
#endif
setCentralWidget(vp);
ui->actionOpen->setIcon(style()->standardIcon(QStyle::SP_DialogOpenButton));
ui->actionAnimation->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
QToolBar *toolBar = new QToolBar();
toolBar->addAction(ui->actionOpen);
toolBar->addAction(ui->actionAnimation);
ui->dockWidgetContents->layout()->addWidget(toolBar);
// Background button setup
background->setText("Background");
QWidget *stretch = new QWidget(toolBar);
stretch->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
toolBar->addWidget(stretch);
toolBar->addWidget(background);
connect(background, SIGNAL(released()), this, SLOT(changeBackgroundColor()));
//Right pane setup
ui->dockWidget->setTitleBarWidget(new QWidget(this));
//Object tab
ui->objectAmbientButton->setPalette(QPalette(objectMaterial->getKA()));
ui->objectAmbientButton->setText(objectMaterial->getKA().name());
ui->objectDiffuseButton->setPalette(QPalette(objectMaterial->getKD()));
ui->objectDiffuseButton->setText(objectMaterial->getKD().name());
ui->objectSpecularButton->setPalette(QPalette(objectMaterial->getKS()));
ui->objectSpecularButton->setText(objectMaterial->getKS().name());
ui->objectEmissionButton->setPalette(QPalette(objectMaterial->getKE()));
ui->objectEmissionButton->setText(objectMaterial->getKE().name());
ui->objectShininessSpinbox->setValue(objectMaterial->getQ());
connect(ui->objectAmbientButton, SIGNAL(released()), this, SLOT(changeObjectAmbient()));
connect(ui->objectDiffuseButton, SIGNAL(released()), this, SLOT(changeObjectDiffuse()));
connect(ui->objectSpecularButton, SIGNAL(released()), this, SLOT(changeObjectSpecular()));
connect(ui->objectEmissionButton, SIGNAL(released()), this, SLOT(changeObjectEmission()));
connect(ui->objectShininessSpinbox, SIGNAL(valueChanged(double)), this, SLOT(changeObjectShininess(double)));
//Lights tab
connect(ui->lightsAddButton, SIGNAL(released()), this, SLOT(addLight()));
connect(ui->lightsDeleteButton, SIGNAL(released()), this, SLOT(deleteSelectedLight()));
connect(ui->lightsListWidget, SIGNAL(itemSelectionChanged()), this, SLOT(updateLightButtons()));
connect(ui->lightsDiffuseButton, SIGNAL(released()), this, SLOT(changeLightDiffuse()));
connect(ui->lightsSpecularButton, SIGNAL(released()), this, SLOT(changeLightSpecular()));
connect(ui->lightsAmbientButton, SIGNAL(released()), this, SLOT(changeLightAmbient()));
connect(ui->lightsXSpinbox, SIGNAL(valueChanged(double)), this, SLOT(changeLightPosition()));
connect(ui->lightsYSpinbox, SIGNAL(valueChanged(double)), this, SLOT(changeLightPosition()));
connect(ui->lightsZSpinbox, SIGNAL(valueChanged(double)), this, SLOT(changeLightPosition()));
addLight();
lightsContext.at(0)->setID(QColor("#e6e0c0"));
lightsContext.at(0)->setPos(QVector3D(0.5, 1.0, 1.0));
updateLightButtons();
vp->setLights(lightsContext);
updateLightButtons();
vp->setDefaultMaterial(objectMaterial);
timer->setInterval(1000/FPS);
connect(timer, SIGNAL(timeout()), this, SLOT(autoRotate()));
on_actionOpen_triggered(); // Load dialog at create
ui->actionAnimation->toggle();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::on_actionOpen_triggered()
{
bool wasAnimated = isAnimated;
if (wasAnimated)
ui->actionAnimation->toggle();
QString filepath = QFileDialog::getOpenFileName(this, QString("Select the surface to load"), NULL, "All supported formats (*.sur *.SUR *.obj *.OBJ); Surfaces (*.sur *.SUR); Wavefront OBJ (*.obj *.OBJ");
if (filepath != NULL) {
ObjectModel *_new = NULL;
bool mod_material;
if (filepath.endsWith(".sur", Qt::CaseInsensitive)) {
_new = SURReader::openSUR(filepath);
mod_material = true;
} else if (filepath.endsWith(".obj", Qt::CaseInsensitive)) {
_new = OBJReader::openOBJ(filepath);
mod_material = false;
}
if (_new != NULL) {
ObjectModel *_old = vp->getObjectModel();
vp->setObjectModel(_new);
if (_old != NULL)
delete _old;
ui->objectTab->setEnabled(mod_material);
}
}
if (wasAnimated)
ui->actionAnimation->toggle();
else
vp->updateGL();
}
void MainWindow::on_actionAnimation_toggled(bool s)
{
if (s)
{
timer->start();
} else {
timer->stop();
}
isAnimated = s;
}
void MainWindow::autoRotate()
{
vp->increaseRotation(360.0/(FPS*4), 360.0/(FPS*40));
}
void MainWindow::addLight()
{
Light *l = new Light();
lightsContext.push_back(l);
ui->lightsListWidget->setCurrentItem(new QListWidgetItem(vector2String(l->getPos()), ui->lightsListWidget));
vp->setLights(lightsContext);
}
void MainWindow::deleteSelectedLight()
{
unsigned pos = ui->lightsListWidget->currentRow();
delete (ui->lightsListWidget->takeItem(pos)); // This fires an event that calls updateLightsButtons().
lightsContext.erase(lightsContext.begin() + pos); //Don't remove the light before that event is processed!
ui->lightsAddButton->setEnabled(ui->lightsListWidget->count() < 8);
vp->setLights(lightsContext);
}
QString MainWindow::vector2String(QVector3D v)
{
return QString("(%1, %2, %3)").arg(v.x()).arg(v.y()).arg(v.z());
}
void MainWindow::updateLightButtons()
{
ui->lightsDiffuseButton->blockSignals(true);
ui->lightsSpecularButton->blockSignals(true);
ui->lightsAmbientButton->blockSignals(true);
ui->lightsXSpinbox->blockSignals(true);
ui->lightsYSpinbox->blockSignals(true);
ui->lightsZSpinbox->blockSignals(true);
int pos = ui->lightsListWidget->currentRow();
if (pos == -1) {
ui->lightsDiffuseButton->setPalette(QPalette(Qt::gray));
ui->lightsDiffuseButton->setText("");
ui->lightsSpecularButton->setPalette(QPalette(Qt::gray));
ui->lightsSpecularButton->setText("");
ui->lightsAmbientButton->setPalette(QPalette(Qt::gray));
ui->lightsAmbientButton->setText("");
//Vector
ui->lightsXSpinbox->setValue(0.0);
ui->lightsYSpinbox->setValue(0.0);
ui->lightsZSpinbox->setValue(0.0);
} else {
Light l = lightsContext.at(pos);
//Intensities
ui->lightsDiffuseButton->setPalette(QPalette(l.getID()));
ui->lightsDiffuseButton->setText(l.getID().name());
ui->lightsSpecularButton->setPalette(QPalette(l.getIS()));
ui->lightsSpecularButton->setText(l.getIS().name());
ui->lightsAmbientButton->setPalette(QPalette(l.getIA()));
ui->lightsAmbientButton->setText(l.getIA().name());
//Vector
ui->lightsXSpinbox->setValue(l.getPos().x());
ui->lightsYSpinbox->setValue(l.getPos().y());
ui->lightsZSpinbox->setValue(l.getPos().z());
}
bool valid = (pos != -1);
ui->lightsDiffuseButton->setEnabled(valid);
ui->lightsSpecularButton->setEnabled(valid);
ui->lightsAmbientButton->setEnabled(valid);
ui->lightsXSpinbox->setEnabled(valid);
ui->lightsYSpinbox->setEnabled(valid);
ui->lightsZSpinbox->setEnabled(valid);
ui->lightsDeleteButton->setEnabled(valid);
ui->lightsDiffuseButton->blockSignals(false);
ui->lightsSpecularButton->blockSignals(false);
ui->lightsAmbientButton->blockSignals(false);
ui->lightsXSpinbox->blockSignals(false);
ui->lightsYSpinbox->blockSignals(false);
ui->lightsZSpinbox->blockSignals(false);
}
/*
* Handler for color change
*/
void MainWindow::changeBackgroundColor()
{
QColor color;
if((color = QColorDialog::getColor(vp->getBackgroundColor(), this)).isValid())
{
vp->setBackgroundColor(color);
}
}
void MainWindow::changeObjectAmbient()
{
QColor color;
if((color = QColorDialog::getColor(objectMaterial->getKA(), this)).isValid())
{
objectMaterial->setKA(color);
ui->objectAmbientButton->setPalette(QPalette(color));
ui->objectAmbientButton->setText(color.name());
vp->updateGL();
}
}
void MainWindow::changeObjectDiffuse()
{
QColor color;
if((color = QColorDialog::getColor(objectMaterial->getKD(), this)).isValid())
{
objectMaterial->setKD(color);
ui->objectDiffuseButton->setPalette(QPalette(color));
ui->objectDiffuseButton->setText(color.name());
vp->updateGL();
}
}
void MainWindow::changeObjectSpecular()
{
QColor color;
if((color = QColorDialog::getColor(objectMaterial->getKS(), this)).isValid())
{
objectMaterial->setKS(color);
ui->objectSpecularButton->setPalette(QPalette(color));
ui->objectSpecularButton->setText(color.name());
vp->updateGL();
}
}
void MainWindow::changeObjectEmission()
{
QColor color;
if((color = QColorDialog::getColor(objectMaterial->getKE(), this)).isValid())
{
objectMaterial->setKE(color);
ui->objectEmissionButton->setPalette(QPalette(color));
ui->objectEmissionButton->setText(color.name());
vp->updateGL();
}
}
void MainWindow::changeObjectShininess(double d)
{
objectMaterial->setQ(d);
vp->updateGL();
}
void MainWindow::changeLightAmbient()
{
int pos = ui->lightsListWidget->currentRow();
if (pos != -1) {
Light *l = lightsContext.at(pos);
QColor color;
if((color = QColorDialog::getColor(l->getIA(), this)).isValid())
{
l->setIA(color);
ui->lightsAmbientButton->setPalette(QPalette(color));
ui->lightsAmbientButton->setText(color.name());
vp->setLights(lightsContext);
}
}
}
void MainWindow::changeLightDiffuse()
{
int pos = ui->lightsListWidget->currentRow();
if (pos != -1) {
Light *l = lightsContext.at(pos);
QColor color;
if((color = QColorDialog::getColor(l->getID(), this)).isValid())
{
l->setID(color);
ui->lightsDiffuseButton->setPalette(QPalette(color));
ui->lightsDiffuseButton->setText(color.name());
vp->setLights(lightsContext);
}
}
}
void MainWindow::changeLightSpecular()
{
int pos = ui->lightsListWidget->currentRow();
if (pos != -1) {
Light *l = lightsContext.at(pos);
QColor color;
if((color = QColorDialog::getColor(l->getIS(), this)).isValid())
{
l->setIS(color);
ui->lightsSpecularButton->setPalette(QPalette(color));
ui->lightsSpecularButton->setText(color.name());
vp->setLights(lightsContext);
}
}
}
void MainWindow::changeLightPosition()
{
int pos = ui->lightsListWidget->currentRow();
if (pos != -1) {
Light *l = lightsContext.at(pos);
l->setPos(QVector3D(ui->lightsXSpinbox->value(), ui->lightsYSpinbox->value(), ui->lightsZSpinbox->value()));
ui->lightsListWidget->currentItem()->setText(vector2String(l->getPos()));
vp->setLights(lightsContext);
}
}
}