-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindow.cpp
184 lines (154 loc) · 6.56 KB
/
Window.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
#include <GL/glew.h>
#include "Window.h"
#include <vector>
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <QDockWidget>
#include <QGroupBox>
#include <QButtonGroup>
#include <QMenuBar>
#include <QApplication>
#include <QLayout>
#include <QLabel>
#include <QProgressBar>
#include <QCheckBox>
#include <QRadioButton>
#include <QColorDialog>
#include <QLCDNumber>
#include <QPixmap>
#include <QFrame>
#include <QSplitter>
#include <QMenu>
#include <QScrollArea>
#include <QCoreApplication>
#include <QFont>
#include <QSizePolicy>
#include <QImageReader>
#include <QStatusBar>
#include <QPushButton>
#include <QMessageBox>
#include <QFileDialog>
#include <QStatusBar>
#include "RayTracer.h"
using namespace std;
Window::Window () : QMainWindow (NULL) {
try {
viewer = new GLViewer;
} catch (GLViewer::Exception e) {
cerr << e.getMessage () << endl;
exit (1);
}
setCentralWidget (viewer);
QDockWidget * controlDockWidget = new QDockWidget (this);
initControlWidget ();
controlDockWidget->setWidget (controlWidget);
controlDockWidget->adjustSize ();
addDockWidget (Qt::RightDockWidgetArea, controlDockWidget);
controlDockWidget->setFeatures (QDockWidget::AllDockWidgetFeatures);
statusBar()->showMessage("");
}
Window::~Window () {
}
void Window::renderRayImage () {
qglviewer::Camera * cam = viewer->camera ();
RayTracer * rayTracer = RayTracer::getInstance ();
qglviewer::Vec p = cam->position ();
qglviewer::Vec d = cam->viewDirection ();
qglviewer::Vec u = cam->upVector ();
qglviewer::Vec r = cam->rightVector ();
Vec3Df camPos (p[0], p[1], p[2]);
Vec3Df viewDirection (d[0], d[1], d[2]);
Vec3Df upVector (u[0], u[1], u[2]);
Vec3Df rightVector (r[0], r[1], r[2]);
float fieldOfView = cam->fieldOfView ();
float aspectRatio = cam->aspectRatio ();
unsigned int screenWidth = cam->screenWidth ();
unsigned int screenHeight = cam->screenHeight ();
QTime timer;
timer.start ();
viewer->setRayImage(rayTracer->render (camPos, viewDirection, upVector, rightVector,
fieldOfView, aspectRatio, screenWidth, screenHeight));
statusBar()->showMessage(QString ("Raytracing performed in ") +
QString::number (timer.elapsed ()) +
QString ("ms at ") +
QString::number (screenWidth) + QString ("x") + QString::number (screenHeight) +
QString (" screen resolution"));
viewer->setDisplayMode (GLViewer::RayDisplayMode);
}
void Window::setBGColor () {
QColor c = QColorDialog::getColor (QColor (133, 152, 181), this);
if (c.isValid () == true) {
cout << c.red () << endl;
RayTracer::getInstance ()->setBackgroundColor (Vec3Df (c.red (), c.green (), c.blue ()));
viewer->setBackgroundColor (c);
viewer->updateGL ();
}
}
void Window::showRayImage () {
viewer->setDisplayMode (GLViewer::RayDisplayMode);
}
void Window::exportGLImage () {
viewer->saveSnapshot (false, false);
}
void Window::exportRayImage () {
QString filename = QFileDialog::getSaveFileName (this,
"Save ray-traced image",
".",
"*.jpg *.bmp *.png");
if (!filename.isNull () && !filename.isEmpty ())
viewer->getRayImage().save (filename);
}
void Window::about () {
QMessageBox::about (this,
"About This Program",
"<b>RayMini</b> <br> by <i>Tamy Boubekeur</i>.");
}
void Window::initControlWidget () {
controlWidget = new QGroupBox ();
QVBoxLayout * layout = new QVBoxLayout (controlWidget);
QGroupBox * previewGroupBox = new QGroupBox ("Preview", controlWidget);
QVBoxLayout * previewLayout = new QVBoxLayout (previewGroupBox);
QCheckBox * wireframeCheckBox = new QCheckBox ("Wireframe", previewGroupBox);
connect (wireframeCheckBox, SIGNAL (toggled (bool)), viewer, SLOT (setWireframe (bool)));
previewLayout->addWidget (wireframeCheckBox);
QButtonGroup * modeButtonGroup = new QButtonGroup (previewGroupBox);
modeButtonGroup->setExclusive (true);
QRadioButton * flatButton = new QRadioButton ("Flat", previewGroupBox);
QRadioButton * smoothButton = new QRadioButton ("Smooth", previewGroupBox);
modeButtonGroup->addButton (flatButton, static_cast<int>(GLViewer::Flat));
modeButtonGroup->addButton (smoothButton, static_cast<int>(GLViewer::Smooth));
connect (modeButtonGroup, SIGNAL (buttonClicked (int)), viewer, SLOT (setRenderingMode (int)));
previewLayout->addWidget (flatButton);
previewLayout->addWidget (smoothButton);
QPushButton * snapshotButton = new QPushButton ("Save preview", previewGroupBox);
connect (snapshotButton, SIGNAL (clicked ()) , this, SLOT (exportGLImage ()));
previewLayout->addWidget (snapshotButton);
layout->addWidget (previewGroupBox);
QGroupBox * rayGroupBox = new QGroupBox ("Ray Tracing", controlWidget);
QVBoxLayout * rayLayout = new QVBoxLayout (rayGroupBox);
QPushButton * rayButton = new QPushButton ("Render", rayGroupBox);
rayLayout->addWidget (rayButton);
connect (rayButton, SIGNAL (clicked ()), this, SLOT (renderRayImage ()));
QPushButton * showButton = new QPushButton ("Show", rayGroupBox);
rayLayout->addWidget (showButton);
connect (showButton, SIGNAL (clicked ()), this, SLOT (showRayImage ()));
QPushButton * saveButton = new QPushButton ("Save", rayGroupBox);
connect (saveButton, SIGNAL (clicked ()) , this, SLOT (exportRayImage ()));
rayLayout->addWidget (saveButton);
layout->addWidget (rayGroupBox);
QGroupBox * globalGroupBox = new QGroupBox ("Global Settings", controlWidget);
QVBoxLayout * globalLayout = new QVBoxLayout (globalGroupBox);
QPushButton * bgColorButton = new QPushButton ("Background Color", globalGroupBox);
connect (bgColorButton, SIGNAL (clicked()) , this, SLOT (setBGColor()));
globalLayout->addWidget (bgColorButton);
QPushButton * aboutButton = new QPushButton ("About", globalGroupBox);
connect (aboutButton, SIGNAL (clicked()) , this, SLOT (about()));
globalLayout->addWidget (aboutButton);
QPushButton * quitButton = new QPushButton ("Quit", globalGroupBox);
connect (quitButton, SIGNAL (clicked()) , qApp, SLOT (closeAllWindows()));
globalLayout->addWidget (quitButton);
layout->addWidget (globalGroupBox);
layout->addStretch (0);
}