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
|
#include <QList>
#include <QtConcurrent>
#include <iostream>
#include "raytracer.h"
#include "raytracescene.h"
#include "settings.h"
#include "mainwindow.h"
#include <QKeyEvent>
#include <QTimerEvent>
#include "vec4ops/vec4ops.h"
// RayTracer::RayTracer(const Config &config) : m_config(config) {}
RayTracer::RayTracer(QWidget *parent) : QWidget(parent) {
setFocusPolicy(Qt::StrongFocus);
// map the 1 key
m_keyMap[Qt::Key_1] = false;
m_keyMap[Qt::Key_2] = false;
m_keyMap[Qt::Key_3] = false;
m_keyMap[Qt::Key_4] = false;
m_keyMap[Qt::Key_5] = false;
m_keyMap[Qt::Key_6] = false;
m_width = 576;
m_height = 432;
m_depth = 500;
m_image = QImage(m_width, m_height, QImage::Format_RGBX8888);
}
// updated to use 4D
void RayTracer::render(RGBA *imageData, const RayTraceScene &scene) {
if (m_enableParallelism) {
renderParallel(imageData, scene);
} else {
// naive rendering
Camera camera = scene.getCamera();
float cameraDepth = 1.f;
float viewplaneHeight = 2.f*cameraDepth*std::tan(camera.getHeightAngle() / 2.f);
float viewplaneWidth = cameraDepth*viewplaneHeight*((float)scene.width()/(float)scene.height());
for (int imageRow = 0; imageRow < scene.height(); imageRow++) {
for (int imageCol = 0; imageCol < scene.width(); imageCol++) {
// FIXME: for now, use height as depth
for (int imageDepth = 0; imageDepth < scene.height(); imageDepth++) {
// compute the ray
float x = (imageCol - scene.width()/2.f) * viewplaneWidth / scene.width();
float y = (imageRow - scene.height()/2.f) * viewplaneHeight / scene.height();
float z = (imageDepth - scene.height()/2.f) * viewplaneHeight / scene.height();
float camera4dDepth = 1;
glm::vec4 pWorld = Vec4Ops::transformPoint4(glm::vec4(x, y, z, 0.f), camera.getViewMatrix(), camera.getTranslationVector());
glm::vec4 dWorld = glm::vec4(0.f, 0.f, 0.f, -1.f);
// get the pixel color
glm::vec4 pixelColor = getPixelFromRay(pWorld, dWorld, scene, 0);
// set the pixel color
int index = imageRow * scene.width() + imageCol;
imageData[index] = RGBA{
(std::uint8_t) (pixelColor.r * 255.f),
(std::uint8_t) (pixelColor.g * 255.f),
(std::uint8_t) (pixelColor.b * 255.f),
(std::uint8_t) (pixelColor.a * 255.f)
};
}
}
}
}
settings.rotation += 0.5f;
}
glm::vec4 RayTracer::getPixelFromRay(
glm::vec4 pWorld,
glm::vec4 dWorld,
const RayTraceScene &scene,
int depth)
{
if (depth > m_maxRecursiveDepth)
{
return glm::vec4(0.f);
}
// variables from computing the intersection
glm::vec4 closestIntersectionObj;
glm::vec4 closestIntersectionWorld;
RenderShapeData intersectedShape;
float minDist = FINF;
// shoot a ray at each shape
for (const RenderShapeData &shape : scene.getShapes()) {
glm::vec4 pObject = Vec4Ops::inverseTransformPoint4(pWorld, shape.inverseCTM, shape.inverseTranslation4d);
glm::vec4 dObject = glm::normalize(Vec4Ops::inverseTransformDir4(dWorld, shape.inverseCTM));
// std::cout << "pObject: " << pObject.x << ", " << pObject.y << ", " << pObject.z << ", " << pObject.w << std::endl;
// std::cout << "dObject: " << dObject.x << ", " << dObject.y << ", " << dObject.z << ", " << dObject.w << std::endl;
bool isHit = false;
glm::vec4 newIntersectionObj = findIntersection(pObject, dObject, shape, isHit);
if (!isHit) // no hit
{
continue;
}
auto newIntersectionWorld = shape.ctm * newIntersectionObj;
float newDist = glm::distance(newIntersectionWorld, pWorld);
if (
newDist < minDist // closer intersection
&& !floatEquals(newDist, 0) // and not a self intersection
)
{
minDist = newDist;
intersectedShape = shape;
closestIntersectionObj = newIntersectionObj;
closestIntersectionWorld = newIntersectionWorld;
}
}
if (minDist == FINF) // no hit
{
return glm::vec4(0.f);
}
glm::vec4 normalObject = glm::normalize(getNormal(closestIntersectionObj, intersectedShape, scene));
// update
glm::vec4 normalWorld = glm::inverse(glm::transpose(intersectedShape.ctm)) * glm::vec4(normalObject);
return illuminatePixel(closestIntersectionWorld, normalWorld, -dWorld, intersectedShape, scene, depth);
}
// EXTRA CREDIT -> depth of field
glm::vec4 RayTracer::secondaryRays(glm::vec4 pWorld, glm::vec4 dWorld, RayTraceScene &scene)
{
auto inv = scene.getCamera().getInverseViewMatrix();
float focalLength = scene.getCamera().getFocalLength();
float aperture = scene.getCamera().getAperture();
glm::vec4 illumination(0.f);
glm::vec4 focalPoint = pWorld + focalLength * dWorld;
int TIMES = 500;
for (int i = 0; i < TIMES; i++) {
// generate a random number from -aperature to aperature
float rand1 = ((float) rand() / (float) RAND_MAX) * aperture;
rand1 *= (rand() % 2 == 0) ? 1 : -1;
// generate another number also inside the aperature lens
float rand2 = ((float) rand() / (float) RAND_MAX) * std::sqrt(aperture - rand1*rand1);
rand2 *= (rand() % 2 == 0) ? 1 : -1;
glm::vec4 randEye = (rand() % 2 == 0) ? glm::vec4(rand1, rand2, 0.f, 1.f) : glm::vec4(rand2, rand1, 0.f, 1.f);
// convert this random point to world space
glm::vec4 eyeWorld = inv * randEye;
// make the ray
glm::vec4 randomDir = glm::vec4(glm::normalize(focalPoint.xyz() - eyeWorld.xyz()), 0.f);
illumination += getPixelFromRay(eyeWorld, randomDir, scene, 0);
}
return illumination / (float) TIMES;
}
void RayTracer::sceneChanged(QLabel* imageLabel) {
// RenderData metaData;
bool success = SceneParser::parse(settings.sceneFilePath, m_metaData);
if (!success) {
std::cerr << "Error loading scene: \"" << settings.sceneFilePath << "\"" << std::endl;
m_image.fill(Qt::black);
imageLabel->setPixmap(QPixmap::fromImage(m_image));
return;
}
// render the scene
m_imageData = reinterpret_cast<RGBA *>(m_image.bits());
RayTraceScene rtScene{ m_width, m_height, m_metaData, m_depth };
this->render(m_imageData, rtScene);
QImage flippedImage = m_image.mirrored(false, false);
// make the image larger
flippedImage = flippedImage.scaled(m_width, m_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
imageLabel->setPixmap(QPixmap::fromImage(flippedImage));
m_imageLabel = imageLabel;
// m_image = image;
}
void RayTracer::settingsChanged(QLabel* imageLabel) {
bool success = SceneParser::parse(settings.sceneFilePath, m_metaData);
if (!success) {
std::cerr << "Error loading scene: \"" << settings.sceneFilePath << "\"" << std::endl;
// return;
// render a blank image
QImage image = QImage(576, 432, QImage::Format_RGBX8888);
image.fill(Qt::black);
RGBA *data = reinterpret_cast<RGBA *>(image.bits());
imageLabel->setPixmap(QPixmap::fromImage(image));
return;
}
int width = 576;
int height = 432;
QImage image = QImage(width, height, QImage::Format_RGBX8888);
image.fill(Qt::black);
RGBA *data = reinterpret_cast<RGBA *>(image.bits());
RayTraceScene rtScene{ width, height, m_metaData };
this->render(data, rtScene);
QImage flippedImage = image.mirrored(false, false);
flippedImage = flippedImage.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
imageLabel->setPixmap(QPixmap::fromImage(flippedImage));
// QTimer::singleShot(3500, this, [this, imageLabel]() {
// // This code will be executed after a 2-second delay
// emit rotationChanged(settings.rotation);
// });
m_image = image;
}
// void RayTracer::wSliderChanged(QLabel* imageLabel) {
// }
void RayTracer::keyPressEvent(QKeyEvent *event) {
m_keyMap[Qt::Key(event->key())] = true;
std::cout << "key pressed" << std::endl;
if (m_keyMap[Qt::Key_1]) {
std::cout << "key 1" << std::endl;
if (settings.negative) {
settings.xy -= settings.rotation;
} else {
settings.xy += settings.rotation;
}
emit xyRotationChanged(settings.xy);
}
if (m_keyMap[Qt::Key_2]) {
if (settings.negative) {
settings.xz -= settings.rotation;
} else {
settings.xz += settings.rotation;
}
emit xzRotationChanged(settings.xz);
}
if (m_keyMap[Qt::Key_3]) {
if (settings.negative) {
settings.xw -= settings.rotation;
} else {
settings.xw += settings.rotation;
}
emit xwRotationChanged(settings.xw);
}
if (m_keyMap[Qt::Key_4]) {
if (settings.negative) {
settings.yz -= settings.rotation;
} else {
settings.yz += settings.rotation;
}
emit yzRotationChanged(settings.yz);
}
if (m_keyMap[Qt::Key_5]) {
if (settings.negative) {
settings.yw -= settings.rotation;
} else {
settings.yw += settings.rotation;
}
emit ywRotationChanged(settings.yw);
}
if (m_keyMap[Qt::Key_6]) {
if (settings.negative) {
settings.zw -= settings.rotation;
} else {
settings.zw += settings.rotation;
}
emit zwRotationChanged(settings.zw);
}
}
void RayTracer::keyReleaseEvent(QKeyEvent *event) {
m_keyMap[Qt::Key(event->key())] = false;
}
|