-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtray.cpp
393 lines (336 loc) · 15 KB
/
vtray.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
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
// File: vtray.cpp
// Author: Samuel McFalls
// Description: Defines the entry point of the vtray executable
#include "ray_tracer.hpp"
#include "sphere.hpp"
#include "plane.hpp"
#include "message_queue.h"
#include <string>
#include <iostream>
#include <stdexcept>
#include <thread>
#include <qjsonarray.h>
#include <qjsondocument.h>
#include <qjsonobject.h>
#include <qjsonvalue.h>
#include <qfile.h>
#include <qimage.h>
#include "qdebug.h"
typedef struct {
int startX;
int endX;
int startY;
int endY;
} Job;
Scene sceneFromJson(const QJsonObject &json);
void threadWorker(const RayTracer &rt, message_queue<Job> * in, message_queue<RayTracer::ImageChunk> * out);
int parseArgs(int argc, char * argv[], std::string &jsonFileName, std::string &pngFileName, int &numThreads);
void addJobs(message_queue<Job> * in, const Scene &scene);
void saveImage(message_queue<RayTracer::ImageChunk> * out, const Scene &scene, std::string pngFileName);
void camFromJson(const QJsonValue &camVal, Scene &scene);
void lightsFromJson(const QJsonValue &lVal, Scene &scene);
void objsFromJson(const QJsonValue &objVal, Scene &scene);
void cam1Json(const QJsonObject &camObj, double &camCenX, double &camCenY, double &camCenZ, double &camFocus, double &camNormX, double &camNormY, double &camNormZ);
int main(int argc, char * argv[]) {
std::string jsonFileName, pngFileName;
int numThreads;
if (parseArgs(argc, argv, jsonFileName, pngFileName, numThreads) == EXIT_FAILURE) {
return EXIT_FAILURE;
}
QString val;
QFile jsonFile;
jsonFile.setFileName(QString::fromStdString(jsonFileName));
if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
std::cerr << "Error reading file\n";
return EXIT_FAILURE;
}
val = jsonFile.readAll();
jsonFile.close();
QJsonDocument jdoc = QJsonDocument::fromJson(val.toUtf8());
QJsonObject jobj = jdoc.object();
Scene scene;
try {
scene = sceneFromJson(jobj);
}
catch (const std::logic_error &e) {
std::cout << "Error: " << e.what() << "\n";
return EXIT_FAILURE;
}
RayTracer rt = RayTracer(scene);
std::vector<std::thread> threads;
message_queue<Job> in;
message_queue<RayTracer::ImageChunk> out;
for (int i = 0; i < numThreads; i++) {
threads.emplace_back(std::thread(&threadWorker, rt, &in, &out));
}
addJobs(&in, scene);
for (int i = 0; i < numThreads; i++) {
Job job; job.startX = -1; job.endX = -1; job.startY = -1; job.endY = -1;
in.push(job);
}
for (int i = 0; i < numThreads; i++) {
threads[i].join();
}
saveImage(&out, scene, pngFileName);
return EXIT_SUCCESS;
}
void threadWorker(const RayTracer &rt, message_queue<Job> * in, message_queue<RayTracer::ImageChunk> * out) {
Job job;
while (true) {
in->wait_and_pop(job);
if (job.startX == -1 && job.endX == -1 && job.startY == -1 && job.endY == -1) {
return;
}
out->push(rt.renderChunk(job.startX, job.endX, job.startY, job.endY));
}
}
int parseArgs(int argc, char * argv[], std::string &jsonFileName, std::string &pngFileName, int &numThreads) {
std::string usage = "ERROR usage: vtray [-t <num_threads>] <scene.json> <scene.png\n";
if (argc == 5) {
std::string tString = argv[1];
if (tString != "-t") {
std::cerr << usage;
return EXIT_FAILURE;
}
numThreads = std::atoi(argv[2]);
if (numThreads < 1) {
std::cerr << "Cannot use less than 1 thread\n";
return EXIT_FAILURE;
}
jsonFileName = argv[3];
pngFileName = argv[4];
return 0;
}
if (argc == 3) {
numThreads = 1;
jsonFileName = argv[1];
pngFileName = argv[2];
return 0;
}
std::cerr << usage;
return EXIT_FAILURE;
}
void addJobs(message_queue<Job> * in, const Scene &scene) {
int width = scene.getCameraX();
int height = scene.getCameraY();
int chunkW = 4;
int chunkH = 2;
for (int i = 0; i < chunkW; i++) {
Job job;
job.startX = i * width / chunkW;
job.endX = (i == chunkW - 1) ? job.startX + width / chunkW + width % chunkW - 1 : job.startX + width / chunkW - 1;
for (int j = 0; j < chunkH; j++) {
job.startY = j * height / chunkH;
job.endY = (j == chunkH - 1) ? job.startY + height / chunkH + height % chunkH - 1 : job.startY + height / chunkH - 1;
in->push(job);
}
}
}
void saveImage(message_queue<RayTracer::ImageChunk> * out, const Scene &scene, std::string pngFileName) {
QImage image(scene.getCameraX(), scene.getCameraY(), QImage::Format_RGB32);
QRgb colorVal;
std::vector<RayTracer::ImageChunk> chunks;
Color globalMax = Color();
while (!out->empty()) {
RayTracer::ImageChunk chunk;
out->wait_and_pop(chunk);
chunks.push_back(chunk);
if (chunk.max.getRed() > globalMax.getRed()) {
globalMax.setRed(chunk.max.getRed());
}
if (chunk.max.getGreen() > globalMax.getGreen()) {
globalMax.setGreen(chunk.max.getGreen());
}
if (chunk.max.getBlue() > globalMax.getBlue()) {
globalMax.setBlue(chunk.max.getBlue());
}
}
for (auto chunk : chunks) {
for (unsigned int i = 0; i < chunk.image.size(); i++) {
auto row = chunk.image[i];
for (unsigned int j = 0; j < row.size(); j++) {
Color color = row[j];
color.scale(255, globalMax);
colorVal = qRgb(color.getRed(), color.getGreen(), color.getBlue());
image.setPixel(chunk.offsetX + i, chunk.offsetY + j, colorVal);
}
}
}
image.save(QString::fromStdString(pngFileName), 0, -1);
}
void camFromJson(const QJsonValue &camVal, Scene &scene) {
if (camVal.isUndefined()) {throw std::logic_error("Key \"camera\" does not exist");}
double camCenX, camCenY, camCenZ, camFocus, camNormX, camNormY, camNormZ;
cam1Json(camVal.toObject(), camCenX, camCenY, camCenZ, camFocus, camNormX, camNormY, camNormZ);
QJsonObject camObj = camVal.toObject();
QJsonValue camResVal = camObj.value("resolution"); // res
if (camResVal.isUndefined()) {throw std::logic_error("Key \"resolution\" does not exist");}
QJsonValue camSizeVal = camObj.value("size");
if (camSizeVal.isUndefined()) {throw std::logic_error("Key \"size\" does not exist");}
QJsonArray camResArr = camResVal.toArray({});
if (!camResVal.isArray() || camResArr.size() != 2) {throw std::logic_error("Resolution is not appropriate list");}
double camResX = camResArr[0].toDouble();
if (!camResArr[0].isDouble() || camResX < 0) {throw std::logic_error("Resolution x is not a positive double");}
double camResY = camResArr[1].toDouble();
if (!camResArr[1].isDouble() || camResY < 0) {throw std::logic_error("Resolution y is not a positive double");}
QJsonArray camSizeArr = camSizeVal.toArray({}); // size
if (!camSizeVal.isArray() || camSizeArr.size() != 2) {throw std::logic_error("Size is not appropriate list");}
int camSizeX = camSizeArr[0].toInt(-1);
if (camSizeX < 0) {throw std::logic_error("Size x is not a positive int");}
int camSizeY = camSizeArr[1].toInt(-1);
if (camSizeY < 0) {throw std::logic_error("Size y is not a positive int");}
// Assemble the information
Camera camera = Camera(
Vec3(camCenX, camCenY, camCenZ), Vec3(camNormX, camNormY, camNormZ),
camFocus, camSizeX, camSizeY, camResX, camResY);
scene.setCamera(camera);
}
void cam1Json(const QJsonObject &camObj, double &camCenX, double &camCenY, double &camCenZ, double &camFocus, double &camNormX, double &camNormY, double &camNormZ) {
QJsonValue camCenVal = camObj.value("center");
if (camCenVal.isUndefined()) { throw std::logic_error("Key \"center\" does not exist"); }
QJsonValue camFocusVal = camObj.value("focus");
if (camFocusVal.isUndefined()) { throw std::logic_error("Key \"focus\" does not exist"); }
QJsonValue camNormVal = camObj.value("normal");
if (camNormVal.isUndefined()) { throw std::logic_error("Key \"normal\" does not exist"); }
QJsonObject camCenObj = camCenVal.toObject(); // Center
QJsonValue camCenXVal = camCenObj.value("x");
if (camCenXVal.isUndefined()) { throw std::logic_error("Key \"x\" does not exist"); }
camCenX = camCenXVal.toDouble(-1);
if (!camCenXVal.isDouble()) { throw std::logic_error("x is not a double"); }
QJsonValue camCenYVal = camCenObj.value("y");
if (camCenYVal.isUndefined()) { throw std::logic_error("Key \"y\" does not exist"); }
camCenY = camCenYVal.toDouble();
if (!camCenYVal.isDouble()) { throw std::logic_error("y is not a double"); }
QJsonValue camCenZVal = camCenObj.value("z");
if (camCenZVal.isUndefined()) { throw std::logic_error("Key \"z\" does not exist"); }
camCenZ = camCenZVal.toDouble();
if (!camCenZVal.isDouble()) { throw std::logic_error("z is not a double"); }
camFocus = camFocusVal.toDouble(); // focus
if (!camFocusVal.isDouble()) { throw std::logic_error("focus is not a double"); }
QJsonObject camNormObj = camNormVal.toObject(); // normal
QJsonValue camNormXVal = camNormObj.value("x");
if (camNormXVal.isUndefined()) { throw std::logic_error("Key \"x\" does not exist"); }
camNormX = camNormXVal.toDouble();
if (!camNormXVal.isDouble()) { throw std::logic_error("x is not a double"); }
QJsonValue camNormYVal = camNormObj.value("y");
if (camNormYVal.isUndefined()) { throw std::logic_error("Key \"y\" does not exist"); }
camNormY = camNormYVal.toDouble();
if (!camNormYVal.isDouble()) { throw std::logic_error("y is not a double"); }
QJsonValue camNormZVal = camNormObj.value("z");
if (camNormZVal.isUndefined()) { throw std::logic_error("Key \"z\" does not exist"); }
camNormZ = camNormZVal.toDouble();
if (!camNormZVal.isDouble()) { throw std::logic_error("z is not a double"); }
}
void lightsFromJson(const QJsonValue &lVal, Scene &scene) {
if (lVal.isUndefined()) {
throw std::logic_error("Key \"lights\" does not exist");
}
QJsonArray lArr = lVal.toArray({});
if (!lVal.isArray()) {
throw std::logic_error("Invalid lights list");
}
for (int li = 0; li < lArr.size(); li++) {
QJsonObject l = lArr[li].toObject();
// Intensity
QJsonValue intenVal = l.value("intensity");
if (intenVal.isUndefined()) {
throw std::logic_error("Key \"intensity\" does not exist");
}
double inten = intenVal.toDouble();
if (!intenVal.isDouble() || inten < 0) {
throw std::logic_error("Intensity is not a positive double");
}
// Location
QJsonValue locVal = l.value("location");
if (locVal.isUndefined()) {
throw std::logic_error("Key \"location\" does not exist");
}
QJsonObject locObj = locVal.toObject();
QJsonValue xLocVal = locObj.value("x");
if (xLocVal.isUndefined()) {
throw std::logic_error("Key \"x\" does not exist");
}
double xLoc = xLocVal.toDouble();
if (!xLocVal.isDouble()) {
throw std::logic_error("x is not a double");
}
QJsonValue yLocVal = locObj.value("y");
if (yLocVal.isUndefined()) {
throw std::logic_error("Key \"y\" does not exist");
}
double yLoc = yLocVal.toDouble();
if (!yLocVal.isDouble()) {
throw std::logic_error("y is not a double");
}
QJsonValue zLocVal = locObj.value("z");
if (zLocVal.isUndefined()) {
throw std::logic_error("Key \"z\" does not exist");
}
double zLoc = zLocVal.toDouble();
if (!zLocVal.isDouble()) {
throw std::logic_error("z is not a double");
}
// Assemble the information
Light light = Light(Vec3(xLoc, yLoc, zLoc), inten);
scene.addLight(light);
}
}
void objsFromJson(const QJsonValue &objVal, Scene &scene) {
if (objVal.isUndefined()) {throw std::logic_error("Key \"objects\" does not exist");}
QJsonArray objArr = objVal.toArray({});if (!objVal.isArray()) {throw std::logic_error("Invalid objects list");}
for (int oi = 0; oi < objArr.size(); oi++) {
QJsonObject obj = objArr[oi].toObject();
QJsonValue colorVal = obj.value("color"); if (colorVal.isUndefined()) { throw std::logic_error("Key \"color\" does not exist"); }
QJsonObject colorObj = colorVal.toObject();
QJsonValue rVal = colorObj.value("r"); if (rVal.isUndefined()) { throw std::logic_error("Key \"r\" does not exist"); }
int r = rVal.toInt(-1); if (r < 0 || r > 255) { throw std::logic_error("r value is not an int [0, 255]"); }
QJsonValue gVal = colorObj.value("g"); if (gVal.isUndefined()) { throw std::logic_error("Key \"g\" does not exist"); }
int g = gVal.toInt(-1); if (g < 0 || g > 255) { throw std::logic_error("g value is not an int [0, 255]"); }
QJsonValue bVal = colorObj.value("b"); if (bVal.isUndefined()) { throw std::logic_error("Key \"b\" does not exist"); }int b = bVal.toInt(-1); if (b < 0 || b > 255) { throw std::logic_error("b value is not an int [0, 255]"); }
QJsonValue lamVal = obj.value("lambert"); if (lamVal.isUndefined()) { throw std::logic_error("Key \"lambert\" does not exist"); }
double lam = lamVal.toDouble(); if (!lamVal.isDouble() || lam < 0 || lam > 1) { throw std::logic_error("lambert value is not a double [0, 1]"); }
QJsonValue cenVal = obj.value("center"); if (cenVal.isUndefined()) { throw std::logic_error("Key \"center\" does not exist"); }
QJsonObject cenObj = cenVal.toObject();
QJsonValue cenXVal = cenObj.value("x");
if (cenXVal.isUndefined()) { throw std::logic_error("Key \"x\" does not exist"); }
double cenX = cenXVal.toDouble(); if (!cenXVal.isDouble()) { throw std::logic_error("x is not a double"); }
QJsonValue cenYVal = cenObj.value("y");
if (cenYVal.isUndefined()) { throw std::logic_error("Key \"y\" does not exist"); }
double cenY = cenYVal.toDouble(); if (!cenYVal.isDouble()) { throw std::logic_error("y is not a double"); }
QJsonValue cenZVal = cenObj.value("z");
if (cenZVal.isUndefined()) { throw std::logic_error("Key \"z\" does not exist"); }
double cenZ = cenZVal.toDouble(); if (!cenZVal.isDouble()) { throw std::logic_error("z is not a double"); }
QJsonValue typeVal = obj.value("type");
if (typeVal.isUndefined()) { throw std::logic_error("Key \"type\" does not exist"); }
QString type = typeVal.toString("q");
if (type == QString("sphere")) {
QJsonValue radVal = obj.value("radius");
if (radVal.isUndefined()) { throw std::logic_error("Key \"radius\" does not exist"); }
double rad = radVal.toDouble(-1); if (rad < 0) { throw std::logic_error("Radius is not a positive double"); }
Sphere sphere = Sphere(Color(r, g, b), lam, Vec3(cenX, cenY, cenZ), rad); scene.addSphere(sphere);
}
else if (type == QString("plane")) {
QJsonValue normVal = obj.value("normal");
QJsonObject normObject = normVal.toObject();
QJsonValue normXVal = normObject.value("x"); if (normXVal.isUndefined()) { throw std::logic_error("Key \"x\" does not exist"); }
double normX = normXVal.toDouble();
if (!normXVal.isDouble()) { throw std::logic_error("x is not a double"); }
QJsonValue normYVal = normObject.value("y");
if (normYVal.isUndefined()) { throw std::logic_error("Key \"y\" does not exist"); }
double normY = normYVal.toDouble();
if (!normYVal.isDouble()) { throw std::logic_error("y is not a double"); }
QJsonValue normZVal = normObject.value("z");
if (normZVal.isUndefined()) { throw std::logic_error("Key \"z\" does not exist"); }
double normZ = normZVal.toDouble(); if (!normZVal.isDouble()) { throw std::logic_error("z is not a double"); }
Plane plane = Plane(Color(r, g, b), lam, Vec3(cenX, cenY, cenZ), Vec3(normX, normY, normZ)); scene.addPlane(plane);
}
else { throw std::logic_error("Unrecognized object type"); }
}
}
Scene sceneFromJson(const QJsonObject &json) {
Scene scene = Scene();
camFromJson(json.value("camera"), scene);
lightsFromJson(json.value("lights"), scene);
objsFromJson(json.value("objects"), scene);
return scene;
}