aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow.cpp
blob: 5da720be8cf53cede91cebdc15ac8e77d474c92a (plain)
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#include "mainwindow.h"
#include "settings.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFileDialog>
#include <QSettings>
#include <QLabel>
#include <QGroupBox>
#include <iostream>

void MainWindow::initialize() {
    // create RayTracer
    rayTracer = new RayTracer(this);
    imageLabel = new QLabel(this); 
    // aspectRatioWidget = new AspectRatioWidget(this);
    // aspectRatioWidget->setAspectWidget(imageLabel, 3.f/4.f);
    QHBoxLayout *hLayout = new QHBoxLayout; // horizontal alignment
    QVBoxLayout *vLayout = new QVBoxLayout(); // vertical alignment
    vLayout->setAlignment(Qt::AlignTop);
    hLayout->addLayout(vLayout);
    hLayout->addWidget(imageLabel, 1);
    this->setLayout(hLayout);

    QFont font;
    font.setPointSize(12);
    font.setBold(true);
    QLabel *xy_label = new QLabel(); // XY label
    xy_label->setText("XY value:");
    xy_label->setFont(font);
    QLabel *xz_label = new QLabel(); // XZ label
    xz_label->setText("XZ value:");
    xz_label->setFont(font);
    QLabel *xw_label = new QLabel(); // XW label
    xw_label->setText("XW value:");
    xw_label->setFont(font);
    QLabel *yz_label = new QLabel(); // YZ label
    yz_label->setText("YZ value:");
    yz_label->setFont(font);
    QLabel *yw_label = new QLabel(); // YW label
    yw_label->setText("YW value:");
    yw_label->setFont(font);
    QLabel *zw_label = new QLabel(); // ZW label
    zw_label->setText("ZW value:");
    zw_label->setFont(font);

    QLabel *rotation_label = new QLabel(); // Rotation label
    rotation_label->setText("Rotation value:");
    rotation_label->setFont(font);
    

    // Create file uploader for scene file
    uploadFile = new QPushButton();
    uploadFile->setText(QStringLiteral("Upload Scene File"));
    
    saveImage = new QPushButton();
    saveImage->setText(QStringLiteral("Save image"));

    QGroupBox *xyLayout = new QGroupBox(); // horizonal w slider alignment
    QHBoxLayout *lxy = new QHBoxLayout();

    xySlider = new QSlider(Qt::Orientation::Horizontal); // XY value slider
    xySlider->setTickInterval(1);
    xySlider->setMinimum(-36000);
    xySlider->setMaximum(36000);
    xySlider->setValue(0);

    xyBox = new QDoubleSpinBox();
    xyBox->setMinimum(-360.0f);
    xyBox->setMaximum(360.f);
    xyBox->setSingleStep(1.f);
    xyBox->setValue(0.f);

    lxy->addWidget(xySlider);
    lxy->addWidget(xyBox);
    xyLayout->setLayout(lxy);

    // XZ Slider
    QGroupBox *xzLayout = new QGroupBox(); // horizonal w slider alignment
    QHBoxLayout *lxz = new QHBoxLayout();

    xzSlider = new QSlider(Qt::Orientation::Horizontal); // XY value slider
    xzSlider->setTickInterval(1);
    xzSlider->setMinimum(-36000);
    xzSlider->setMaximum(36000);
    xzSlider->setValue(0);

    xzBox = new QDoubleSpinBox();
    xzBox->setMinimum(-360.0f);
    xzBox->setMaximum(360.f);
    xzBox->setSingleStep(1.f);
    xzBox->setValue(0.f);

    lxz->addWidget(xzSlider);
    lxz->addWidget(xzBox);
    xzLayout->setLayout(lxz);

    // XW Slider
    QGroupBox *xwLayout = new QGroupBox(); // horizonal w slider alignment
    QHBoxLayout *lxw = new QHBoxLayout();

    xwSlider = new QSlider(Qt::Orientation::Horizontal); // XY value slider
    xwSlider->setTickInterval(1);
    xwSlider->setMinimum(-36000);
    xwSlider->setMaximum(36000);
    xwSlider->setValue(0);

    xwBox = new QDoubleSpinBox();
    xwBox->setMinimum(-360.0f);
    xwBox->setMaximum(360.f);
    xwBox->setSingleStep(1.f);
    xwBox->setValue(0.f);

    lxw->addWidget(xwSlider);
    lxw->addWidget(xwBox);
    xwLayout->setLayout(lxw);

    // YZ Slider
    QGroupBox *yzLayout = new QGroupBox(); // horizonal w slider alignment
    QHBoxLayout *lyz = new QHBoxLayout();

    yzSlider = new QSlider(Qt::Orientation::Horizontal); // XY value slider
    yzSlider->setTickInterval(1);
    yzSlider->setMinimum(-36000);
    yzSlider->setMaximum(36000);
    yzSlider->setValue(0);

    yzBox = new QDoubleSpinBox();
    yzBox->setMinimum(-360.0f);
    yzBox->setMaximum(360.f);
    yzBox->setSingleStep(1.f);
    yzBox->setValue(0.f);

    lyz->addWidget(yzSlider);
    lyz->addWidget(yzBox);
    yzLayout->setLayout(lyz);

    // YW Slider
    QGroupBox *ywLayout = new QGroupBox(); // horizonal w slider alignment
    QHBoxLayout *lyw = new QHBoxLayout();
    
    ywSlider = new QSlider(Qt::Orientation::Horizontal); // XY value slider
    ywSlider->setTickInterval(1);
    ywSlider->setMinimum(-36000);
    ywSlider->setMaximum(36000);
    ywSlider->setValue(0);

    ywBox = new QDoubleSpinBox();
    ywBox->setMinimum(-360.0f);
    ywBox->setMaximum(360.f);
    ywBox->setSingleStep(1.f);
    ywBox->setValue(0.f);

    lyw->addWidget(ywSlider);
    lyw->addWidget(ywBox);
    ywLayout->setLayout(lyw);

    // ZW Slider
    QGroupBox *zwLayout = new QGroupBox(); // horizonal w slider alignment
    QHBoxLayout *lzw = new QHBoxLayout();

    zwSlider = new QSlider(Qt::Orientation::Horizontal); // XY value slider
    zwSlider->setTickInterval(1);
    zwSlider->setMinimum(-36000);
    zwSlider->setMaximum(36000);
    zwSlider->setValue(0);

    zwBox = new QDoubleSpinBox();
    zwBox->setMinimum(-360.0f);
    zwBox->setMaximum(360.f);
    zwBox->setSingleStep(1.f);
    zwBox->setValue(0.f);

    lzw->addWidget(zwSlider);
    lzw->addWidget(zwBox);
    zwLayout->setLayout(lzw);

    // Rotation Slider
    QGroupBox *rotationLayout = new QGroupBox(); // horizonal w slider alignment
    QHBoxLayout *lrotation = new QHBoxLayout();

    rotationSlider = new QSlider(Qt::Orientation::Horizontal); // XY value slider
    rotationSlider->setTickInterval(1);
    rotationSlider->setMinimum(0);
    rotationSlider->setMaximum(18000);
    rotationSlider->setValue(1);

    rotationBox = new QDoubleSpinBox();
    rotationBox->setMinimum(0.0f);
    rotationBox->setMaximum(180.f);
    rotationBox->setSingleStep(1.f);
    rotationBox->setValue(1.f);

    lrotation->addWidget(rotationSlider);
    lrotation->addWidget(rotationBox);
    rotationLayout->setLayout(lrotation);

    // checkbox
    rotateNegative = new QCheckBox();
    rotateNegative->setText(QStringLiteral("Reverse Rotation"));
    rotateNegative->setChecked(false);

    vLayout->addWidget(uploadFile);
    vLayout->addWidget(saveImage);
    vLayout->addWidget(xy_label);
    vLayout->addWidget(xyLayout);
    vLayout->addWidget(xz_label);
    vLayout->addWidget(xzLayout);
    vLayout->addWidget(xw_label);
    vLayout->addWidget(xwLayout);
    vLayout->addWidget(yz_label);
    vLayout->addWidget(yzLayout);
    vLayout->addWidget(yw_label);
    vLayout->addWidget(ywLayout);
    vLayout->addWidget(zw_label);
    vLayout->addWidget(zwLayout);
    vLayout->addWidget(rotation_label);
    vLayout->addWidget(rotationLayout);

    vLayout->addWidget(rotateNegative);

    connectUIElements();

    onValChangexyBox(0.0f);
    onValChangexzBox(0.0f);
    onValChangexwBox(0.0f);
    onValChangeyzBox(0.0f);
    onValChangeywBox(0.0f);
    onValChangezwBox(0.0f);
    onValChangeRotationBox(1.0f);
}

void MainWindow::finish() {
//    realtime->finish();
//    delete(realtime);
}

void MainWindow::connectUIElements() {
    connectUploadFile();
    connectSaveImage();
    connectxy();
    connectxz();
    connectxw();
    connectyz();
    connectyw();
    connectzw();
    connectRotationSlider();
    connectNegativeRotation();
    connect(rayTracer, &RayTracer::xyRotationChanged, this, &MainWindow::updateXySlider);
    connect(rayTracer, &RayTracer::xzRotationChanged, this, &MainWindow::updateXzSlider);
    connect(rayTracer, &RayTracer::xwRotationChanged, this, &MainWindow::updateXwSlider);
    connect(rayTracer, &RayTracer::yzRotationChanged, this, &MainWindow::updateYzSlider);
    connect(rayTracer, &RayTracer::ywRotationChanged, this, &MainWindow::updateYwSlider);
    connect(rayTracer, &RayTracer::zwRotationChanged, this, &MainWindow::updateZwSlider);
}

void MainWindow::connectUploadFile() {
    connect(uploadFile, &QPushButton::clicked, this, &MainWindow::onUploadFile);
}

void MainWindow::connectSaveImage() {
    connect(saveImage, &QPushButton::clicked, this, &MainWindow::onSaveImage);
}

void MainWindow::connectxy() {
    connect(xySlider, &QSlider::valueChanged, this, &MainWindow::onValChangexySlider);
    connect(xyBox, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
            this, &MainWindow::onValChangexyBox);
}

void MainWindow::connectxz() {
    connect(xzSlider, &QSlider::valueChanged, this, &MainWindow::onValChangexzSlider);
    connect(xzBox, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
            this, &MainWindow::onValChangexzBox);
}

void MainWindow::connectxw() {
    connect(xwSlider, &QSlider::valueChanged, this, &MainWindow::onValChangexwSlider);
    connect(xwBox, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
            this, &MainWindow::onValChangexwBox);
}

void MainWindow::connectyz() {
    connect(yzSlider, &QSlider::valueChanged, this, &MainWindow::onValChangeyzSlider);
    connect(yzBox, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
            this, &MainWindow::onValChangeyzBox);
}

void MainWindow::connectyw() {
    connect(ywSlider, &QSlider::valueChanged, this, &MainWindow::onValChangeywSlider);
    connect(ywBox, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
            this, &MainWindow::onValChangeywBox);
}

void MainWindow::connectzw() {
    connect(zwSlider, &QSlider::valueChanged, this, &MainWindow::onValChangezwSlider);
    connect(zwBox, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
            this, &MainWindow::onValChangezwBox);
}

void MainWindow::connectRotationSlider() {
    connect(rotationSlider, &QSlider::valueChanged, this, &MainWindow::onValChangeRotationSlider);
    connect(rotationBox, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
            this, &MainWindow::onValChangeRotationBox);
}

void MainWindow::connectNegativeRotation() {
    connect(rotateNegative, &QCheckBox::clicked, this, &MainWindow::onRotateNegative);
}

void MainWindow::onUploadFile() {
    // Get abs path of scene file
    QString configFilePath = QFileDialog::getOpenFileName(this, tr("Upload File"),
                                                          QDir::currentPath()
                                                              .append(QDir::separator())
                                                              .append("scenefiles")
                                                              .append(QDir::separator())
                                                              .append("lights-camera")
                                                              .append(QDir::separator())
                                                              .append("required"), tr("Scene Files (*.json)"));
    if (configFilePath.isNull()) {
        std::cout << "Failed to load null scenefile." << std::endl;
        return;
    }

    settings.sceneFilePath = configFilePath.toStdString();

    std::cout << "Loaded scenefile: \"" << configFilePath.toStdString() << "\"." << std::endl;

    rayTracer->sceneChanged(imageLabel);
}

void MainWindow::onSaveImage() {
    if (settings.sceneFilePath.empty()) {
        std::cout << "No scene file loaded." << std::endl;
        return;
    }
    std::string sceneName = settings.sceneFilePath.substr(0, settings.sceneFilePath.find_last_of("."));
    sceneName = sceneName.substr(sceneName.find_last_of("/")+1);
    QString filePath = QFileDialog::getSaveFileName(this, tr("Save Image"),
                                                    QDir::currentPath()
                                                        .append(QDir::separator())
                                                        .append("student_outputs")
                                                        .append(QDir::separator())
                                                        .append("lights-camera")
                                                        .append(QDir::separator())
                                                        .append("required")
                                                        .append(QDir::separator())
                                                        .append(sceneName), tr("Image Files (*.png)"));
    std::cout << "Saving image to: \"" << filePath.toStdString() << "\"." << std::endl;
//    realtime->saveViewportImage(filePath.toStdString());
}

void MainWindow::onValChangexySlider(int newValue) {
    //wSlider->setValue(newValue);
    xyBox->setValue(newValue/100.f);
    settings.xy = xyBox->value();
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::onValChangexyBox(double newValue) {
    xySlider->setValue(int(newValue*100.f));
    //wBox->setValue(newValue);
    settings.xy = xyBox->value();
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::onValChangexzSlider(int newValue) {
    //wSlider->setValue(newValue);
    xzBox->setValue(newValue/100.f);
    settings.xz = xzBox->value();
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::onValChangexzBox(double newValue) {
    xzSlider->setValue(int(newValue*100.f));
    //wBox->setValue(newValue);
    settings.xz = xzBox->value();
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::onValChangexwSlider(int newValue) {
    //wSlider->setValue(newValue);
    xwBox->setValue(newValue/100.f);
    settings.xw = xwBox->value();
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::onValChangexwBox(double newValue) {
    xwSlider->setValue(int(newValue*100.f));
    //wBox->setValue(newValue);
    settings.xw = xwBox->value();
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::onValChangeyzSlider(int newValue) {
    //wSlider->setValue(newValue);
    yzBox->setValue(newValue/100.f);
    settings.yz = yzBox->value();
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::onValChangeyzBox(double newValue) {
    yzSlider->setValue(int(newValue*100.f));
    //wBox->setValue(newValue);
    settings.yz = yzBox->value();
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::onValChangeywSlider(int newValue) {
    //wSlider->setValue(newValue);
    ywBox->setValue(newValue/100.f);
    settings.yw = ywBox->value();
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::onValChangeywBox(double newValue) {
    ywSlider->setValue(int(newValue*100.f));
    //wBox->setValue(newValue);
    settings.yw = ywBox->value();
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::onValChangezwSlider(int newValue) {
    //wSlider->setValue(newValue);
    zwBox->setValue(newValue/100.f);
    settings.zw = zwBox->value();
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::onValChangezwBox(double newValue) {
    zwSlider->setValue(int(newValue*100.f));
    //wBox->setValue(newValue);
    settings.zw = zwBox->value();
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::onValChangeRotationSlider(int newValue) {
    //wSlider->setValue(newValue);
    rotationBox->setValue(newValue/100.f);
    settings.rotation = rotationBox->value();
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::onValChangeRotationBox(double newValue) {
    rotationSlider->setValue(int(newValue*100.f));
    //wBox->setValue(newValue);
    settings.rotation = rotationBox->value();
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::onRotateNegative() {
    settings.negative = !settings.negative;
}

void MainWindow::updateXySlider(double value) {
    xySlider->setValue(int(value*100.f));
    xyBox->setValue(value);
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::updateXzSlider(double value) {
    xzSlider->setValue(int(value*100.f));
    xzBox->setValue(value);
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::updateXwSlider(double value) {
    xwSlider->setValue(int(value*100.f));
    xwBox->setValue(value);
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::updateYzSlider(double value) {
    yzSlider->setValue(int(value*100.f));
    yzBox->setValue(value);
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::updateYwSlider(double value) {
    ywSlider->setValue(int(value*100.f));
    ywBox->setValue(value);
    rayTracer->settingsChanged(imageLabel);
}

void MainWindow::updateZwSlider(double value) {
    zwSlider->setValue(int(value*100.f));
    zwBox->setValue(value);
    rayTracer->settingsChanged(imageLabel);
}