-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathray_tracer.cpp
74 lines (54 loc) · 1.48 KB
/
ray_tracer.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
// File: ray_tracer.cpp
// Author: Samuel McFalls
// Description: Implements the RayTracer class
#include "ray_tracer.hpp"
RayTracer::RayTracer() {
scene = Scene();
}
RayTracer::RayTracer(const Scene &sceneCon) {
scene = sceneCon;
}
void RayTracer::setScene(const Scene &sceneSet) {
scene = sceneSet;
}
Scene RayTracer::getScene() const {
return scene;
}
RayTracer::ImageChunk RayTracer::renderChunk(int startX, int endX,
int startY, int endY) const {
if (startX < 0) {
throw std::logic_error("start X pixel out of bounds");
}
if (startY < 0) {
throw std::logic_error("start Y pixel out of bounds");
}
if (endX > scene.getCameraX() - 1) {
throw std::logic_error("start X pixel out of bounds");
}
if (endY > scene.getCameraY() - 1) {
throw std::logic_error("start Y pixel out of bounds");
}
ImageChunk imgChunk;
imgChunk.offsetX = startX;
imgChunk.offsetY = startY;
Color maxColor = Color(0, 0, 0);
for (int x = startX; x <= endX; x++) {
std::vector<Color> row;
for (int y = startY; y <= endY; y++) {
Color pixelColor = scene.pixelByTrace(x, y);
row.push_back(pixelColor);
if (pixelColor.getRed() > maxColor.getRed()) {
maxColor.setRed(pixelColor.getRed());
}
if (pixelColor.getGreen() > maxColor.getGreen()) {
maxColor.setGreen(pixelColor.getGreen());
}
if (pixelColor.getBlue() > maxColor.getBlue()) {
maxColor.setBlue(pixelColor.getBlue());
}
}
imgChunk.image.push_back(row);
}
imgChunk.max = maxColor;
return imgChunk;
}