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
|
#include "render.hpp"
#include "vector.hpp"
#include "common.hpp"
#include <math.h>
#include <iostream>
#define FOV 1.74533
const Vec3d up = Vec3d(0, 1, 0);
void Color::clamp() {
if (m_x > 1) { m_x = 1; }
if (m_y > 1) { m_y = 1; }
if (m_z > 1) { m_z = 1; }
}
Renderer::Renderer(const Scene &scn, Vec3d eye, Vec3d target, unsigned width, unsigned height) :
m_scn(scn)
{
m_eye = eye;
m_target = target;
m_width = width;
m_height = height;
recalculate();
}
void Renderer::recalculate() {
auto tmp = m_target - m_eye;
// Orthogonal vector to E
auto b = up.cross(tmp);
b.normalize();
tmp.normalize();
auto v = tmp.cross(b);
// Calculate size of viewplane
double gx = tan( FOV / 2);
double gy = gx * ((double) m_height / m_width);
// Calculate scaling vectors
m_qx = b * ((2 * gx) / (m_width - 1));
m_qy = v * ((2 * gy) / (m_height - 1));
// Calculate starting point
m_blc = tmp - (b * gx) - (v * gy);
}
Ray Renderer::findray(double x, double y) const {
auto dir = m_blc + (m_qx * x) + (m_qy * y);
return Ray(m_eye, dir, true);
}
Color Renderer::render(unsigned x, unsigned y) {
auto r = findray(x, y);
return pathtrace_sample(r, 0);
}
Color Renderer::pathtrace_sample(const Ray &r, unsigned hop) {
double dist;
auto res = cast_ray(r, 0, &dist);
if (!res) {
return Color(0, 0, 0);
}
// Calculate endpoint
auto end = r.m_start + r.m_direction * dist;
auto norm = res->norm_at(end, r.m_direction);
// Simulate single light
auto l = Vec3d(0, 7, 0);
auto tolight = l - end;
auto distance = tolight.length();
tolight.normalize();
auto lightray = Ray(end, tolight, false);
if (cast_ray(lightray, distance, nullptr)) {
return Color(0, 0, 0);
}
auto green = Color(0, 1, 0);
return Color(green * norm.dot(tolight));
}
const Shape* Renderer::cast_ray(const Ray &r, double chk_dist, double *dist_ret) {
const Shape *smallest = nullptr;
double dist = 0;
for (auto obj : m_scn.objs) {
if (!obj) {
continue;
}
auto d = obj->intersect(r, false);
if (d > ZERO_APPROX) {
if (chk_dist > 0 && d < chk_dist) {
dist = d; smallest = obj;
goto exit;
}
if (d < dist || smallest == nullptr) {
dist = d; smallest = obj;
}
}
}
if (chk_dist > 0) {
// If we reach this it means none of the
// object where within distance.
return nullptr;
}
exit:
if (dist_ret) {
*dist_ret = dist;
}
return smallest;
}
|