-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRaytracer.cpp
64 lines (49 loc) · 1.46 KB
/
Raytracer.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
#include "Raytracer.hpp"
#include "PhongMaterial.hpp"
#include "Geometry.hpp"
#include <algorithm>
const std::vector<Color>& Raytracer::render_frame() {
clear(Color(0, 0, 0));
for (uint32_t x = 0; x < width; x++) {
for (uint32_t y = 0; y < height; y++) {
glm::vec2 deviceCoords = getDeviceCoords(x, y);
Ray ray = camera.getRay(deviceCoords);
Intersection closestIntersection(ray);
for (const auto& geom : scene.geometry) {
Intersection intersection = geom->intersect(ray);
if (!closestIntersection.hit || (intersection.hit && intersection.distance < closestIntersection.distance)) {
closestIntersection = intersection;
}
}
if (closestIntersection.hit) {
Color totalLight;
for (const auto& light : scene.lights) {
if (x == 400 && y == 399) {
int foo = 3;
}
totalLight = totalLight + closestIntersection.material->calculateColor(scene, closestIntersection, *light);
}
setPixel(x, y, totalLight);
}
}
}
return pixels;
}
uint32_t Raytracer::getWidth() const {
return width;
}
uint32_t Raytracer::getHeight() const {
return height;
}
void Raytracer::clear(const Color& color) {
std::fill(pixels.begin(), pixels.end(), color);
}
void Raytracer::setPixel(uint32_t x, uint32_t y, const Color& color) {
pixels[y * width + x] = color;
}
glm::vec2 Raytracer::getDeviceCoords(uint32_t x, uint32_t y) {
return glm::vec2(
(x / (float) width) * 2.0f - 1.0f,
-((y / (float) height) * 2.0f - 1.0f)
);
}